OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [fortran/] [resolve.c] - Blame information for rev 826

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 285 jeremybenn
/* Perform type resolution on the various structures.
2
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3
   Free Software Foundation, Inc.
4
   Contributed by Andy Vaught
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 3, or (at your option) any later
11
version.
12
 
13
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16
for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING3.  If not see
20
<http://www.gnu.org/licenses/>.  */
21
 
22
#include "config.h"
23
#include "system.h"
24
#include "flags.h"
25
#include "gfortran.h"
26
#include "obstack.h"
27
#include "bitmap.h"
28
#include "arith.h"  /* For gfc_compare_expr().  */
29
#include "dependency.h"
30
#include "data.h"
31
#include "target-memory.h" /* for gfc_simplify_transfer */
32
 
33
/* Types used in equivalence statements.  */
34
 
35
typedef enum seq_type
36
{
37
  SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38
}
39
seq_type;
40
 
41
/* Stack to keep track of the nesting of blocks as we move through the
42
   code.  See resolve_branch() and resolve_code().  */
43
 
44
typedef struct code_stack
45
{
46
  struct gfc_code *head, *current;
47
  struct code_stack *prev;
48
 
49
  /* This bitmap keeps track of the targets valid for a branch from
50
     inside this block except for END {IF|SELECT}s of enclosing
51
     blocks.  */
52
  bitmap reachable_labels;
53
}
54
code_stack;
55
 
56
static code_stack *cs_base = NULL;
57
 
58
 
59
/* Nonzero if we're inside a FORALL block.  */
60
 
61
static int forall_flag;
62
 
63
/* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block.  */
64
 
65
static int omp_workshare_flag;
66
 
67
/* Nonzero if we are processing a formal arglist. The corresponding function
68
   resets the flag each time that it is read.  */
69
static int formal_arg_flag = 0;
70
 
71
/* True if we are resolving a specification expression.  */
72
static int specification_expr = 0;
73
 
74
/* The id of the last entry seen.  */
75
static int current_entry_id;
76
 
77
/* We use bitmaps to determine if a branch target is valid.  */
78
static bitmap_obstack labels_obstack;
79
 
80
int
81
gfc_is_formal_arg (void)
82
{
83
  return formal_arg_flag;
84
}
85
 
86
/* Is the symbol host associated?  */
87
static bool
88
is_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
}
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
 
103
static gfc_try
104
resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
105
{
106
  if (ts->type == BT_DERIVED && ts->u.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->u.derived->name);
113
          else
114
            gfc_error ("ABSTRACT type '%s' used at %L",
115
                       ts->u.derived->name, where);
116
        }
117
 
118
      return FAILURE;
119
    }
120
 
121
  return SUCCESS;
122
}
123
 
124
 
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
 
134
static void
135
resolve_formal_arglist (gfc_symbol *proc)
136
{
137
  gfc_formal_arglist *f;
138
  gfc_symbol *sym;
139
  int i;
140
 
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))
149
    {
150
      proc->attr.always_explicit = 1;
151
      sym->attr.always_explicit = 1;
152
    }
153
 
154
  formal_arg_flag = 1;
155
 
156
  for (f = proc->formal; f; f = f->next)
157
    {
158
      sym = f->sym;
159
 
160
      if (sym == NULL)
161
        {
162
          /* Alternate return placeholder.  */
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);
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);
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
            {
181
              gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182
                         "also be PURE", sym->name, &sym->declared_at);
183
              continue;
184
            }
185
 
186
          if (gfc_elemental (proc))
187
            {
188
              gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189
                         "procedure", &sym->declared_at);
190
              continue;
191
            }
192
 
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
 
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);
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
220
         shape until we know if it has the pointer or allocatable attributes.
221
      */
222
      if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
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
        }
229
 
230
      if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
231
          || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232
          || sym->attr.optional)
233
        {
234
          proc->attr.always_explicit = 1;
235
          if (proc->result)
236
            proc->result->attr.always_explicit = 1;
237
        }
238
 
239
      /* If the flavor is unknown at this point, it has to be a variable.
240
         A procedure specification would have already set the type.  */
241
 
242
      if (sym->attr.flavor == FL_UNKNOWN)
243
        gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
244
 
245
      if (gfc_pure (proc) && !sym->attr.pointer
246
          && sym->attr.flavor != FL_PROCEDURE)
247
        {
248
          if (proc->attr.function && sym->attr.intent != INTENT_IN)
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
 
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);
257
        }
258
 
259
      if (gfc_elemental (proc))
260
        {
261
          if (sym->as != NULL)
262
            {
263
              gfc_error ("Argument '%s' of elemental procedure at %L must "
264
                         "be scalar", sym->name, &sym->declared_at);
265
              continue;
266
            }
267
 
268
          if (sym->attr.pointer)
269
            {
270
              gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271
                         "have the POINTER attribute", sym->name,
272
                         &sym->declared_at);
273
              continue;
274
            }
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
            }
283
        }
284
 
285
      /* Each dummy shall be specified to be scalar.  */
286
      if (proc->attr.proc == PROC_ST_FUNCTION)
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.u.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
        }
307
    }
308
  formal_arg_flag = 0;
309
}
310
 
311
 
312
/* Work function called when searching for symbols that have argument lists
313
   associated with them.  */
314
 
315
static void
316
find_arglists (gfc_symbol *sym)
317
{
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
 
328
static void
329
resolve_formal_arglists (gfc_namespace *ns)
330
{
331
  if (ns == NULL)
332
    return;
333
 
334
  gfc_traverse_ns (ns, find_arglists);
335
}
336
 
337
 
338
static void
339
resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
340
{
341
  gfc_try t;
342
 
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)
347
    return;
348
 
349
  /* Try to find out of what the return type is.  */
350
  if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
351
    {
352
      t = gfc_set_default_type (sym->result, 0, ns);
353
 
354
      if (t == FAILURE && !sym->result->attr.untyped)
355
        {
356
          if (sym->result == sym)
357
            gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358
                       sym->name, &sym->declared_at);
359
          else if (!sym->result->attr.proc_pointer)
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;
364
        }
365
    }
366
 
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 and results of module
371
     procedures are not on this list, ergo, not permitted.  */
372
 
373
  if (sym->result->ts.type == BT_CHARACTER)
374
    {
375
      gfc_charlen *cl = sym->result->ts.u.cl;
376
      if (!cl || !cl->length)
377
        {
378
          /* See if this is a module-procedure and adapt error message
379
             accordingly.  */
380
          bool module_proc;
381
          gcc_assert (ns->parent && ns->parent->proc_name);
382
          module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
383
 
384
          gfc_error ("Character-valued %s '%s' at %L must not be"
385
                     " assumed length",
386
                     module_proc ? _("module procedure")
387
                                 : _("internal function"),
388
                     sym->name, &sym->declared_at);
389
        }
390
    }
391
}
392
 
393
 
394
/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
395
   introduce duplicates.  */
396
 
397
static void
398
merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
399
{
400
  gfc_formal_arglist *f, *new_arglist;
401
  gfc_symbol *new_sym;
402
 
403
  for (; new_args != NULL; new_args = new_args->next)
404
    {
405
      new_sym = new_args->sym;
406
      /* See if this arg is already in the formal argument list.  */
407
      for (f = proc->formal; f; f = f->next)
408
        {
409
          if (new_sym == f->sym)
410
            break;
411
        }
412
 
413
      if (f)
414
        continue;
415
 
416
      /* Add a new argument.  Argument order is not important.  */
417
      new_arglist = gfc_get_formal_arglist ();
418
      new_arglist->sym = new_sym;
419
      new_arglist->next = proc->formal;
420
      proc->formal  = new_arglist;
421
    }
422
}
423
 
424
 
425
/* Flag the arguments that are not present in all entries.  */
426
 
427
static void
428
check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
429
{
430
  gfc_formal_arglist *f, *head;
431
  head = new_args;
432
 
433
  for (f = proc->formal; f; f = f->next)
434
    {
435
      if (f->sym == NULL)
436
        continue;
437
 
438
      for (new_args = head; new_args; new_args = new_args->next)
439
        {
440
          if (new_args->sym == f->sym)
441
            break;
442
        }
443
 
444
      if (new_args)
445
        continue;
446
 
447
      f->sym->attr.not_always_present = 1;
448
    }
449
}
450
 
451
 
452
/* Resolve alternate entry points.  If a symbol has multiple entry points we
453
   create a new master symbol for the main routine, and turn the existing
454
   symbol into an entry point.  */
455
 
456
static void
457
resolve_entries (gfc_namespace *ns)
458
{
459
  gfc_namespace *old_ns;
460
  gfc_code *c;
461
  gfc_symbol *proc;
462
  gfc_entry_list *el;
463
  char name[GFC_MAX_SYMBOL_LEN + 1];
464
  static int master_count = 0;
465
 
466
  if (ns->proc_name == NULL)
467
    return;
468
 
469
  /* No need to do anything if this procedure doesn't have alternate entry
470
     points.  */
471
  if (!ns->entries)
472
    return;
473
 
474
  /* We may already have resolved alternate entry points.  */
475
  if (ns->proc_name->attr.entry_master)
476
    return;
477
 
478
  /* If this isn't a procedure something has gone horribly wrong.  */
479
  gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
480
 
481
  /* Remember the current namespace.  */
482
  old_ns = gfc_current_ns;
483
 
484
  gfc_current_ns = ns;
485
 
486
  /* Add the main entry point to the list of entry points.  */
487
  el = gfc_get_entry_list ();
488
  el->sym = ns->proc_name;
489
  el->id = 0;
490
  el->next = ns->entries;
491
  ns->entries = el;
492
  ns->proc_name->attr.entry = 1;
493
 
494
  /* If it is a module function, it needs to be in the right namespace
495
     so that gfc_get_fake_result_decl can gather up the results. The
496
     need for this arose in get_proc_name, where these beasts were
497
     left in their own namespace, to keep prior references linked to
498
     the entry declaration.*/
499
  if (ns->proc_name->attr.function
500
      && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
501
    el->sym->ns = ns;
502
 
503
  /* Do the same for entries where the master is not a module
504
     procedure.  These are retained in the module namespace because
505
     of the module procedure declaration.  */
506
  for (el = el->next; el; el = el->next)
507
    if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
508
          && el->sym->attr.mod_proc)
509
      el->sym->ns = ns;
510
  el = ns->entries;
511
 
512
  /* Add an entry statement for it.  */
513
  c = gfc_get_code ();
514
  c->op = EXEC_ENTRY;
515
  c->ext.entry = el;
516
  c->next = ns->code;
517
  ns->code = c;
518
 
519
  /* Create a new symbol for the master function.  */
520
  /* Give the internal function a unique name (within this file).
521
     Also include the function name so the user has some hope of figuring
522
     out what is going on.  */
523
  snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
524
            master_count++, ns->proc_name->name);
525
  gfc_get_ha_symbol (name, &proc);
526
  gcc_assert (proc != NULL);
527
 
528
  gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
529
  if (ns->proc_name->attr.subroutine)
530
    gfc_add_subroutine (&proc->attr, proc->name, NULL);
531
  else
532
    {
533
      gfc_symbol *sym;
534
      gfc_typespec *ts, *fts;
535
      gfc_array_spec *as, *fas;
536
      gfc_add_function (&proc->attr, proc->name, NULL);
537
      proc->result = proc;
538
      fas = ns->entries->sym->as;
539
      fas = fas ? fas : ns->entries->sym->result->as;
540
      fts = &ns->entries->sym->result->ts;
541
      if (fts->type == BT_UNKNOWN)
542
        fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
543
      for (el = ns->entries->next; el; el = el->next)
544
        {
545
          ts = &el->sym->result->ts;
546
          as = el->sym->as;
547
          as = as ? as : el->sym->result->as;
548
          if (ts->type == BT_UNKNOWN)
549
            ts = gfc_get_default_type (el->sym->result->name, NULL);
550
 
551
          if (! gfc_compare_types (ts, fts)
552
              || (el->sym->result->attr.dimension
553
                  != ns->entries->sym->result->attr.dimension)
554
              || (el->sym->result->attr.pointer
555
                  != ns->entries->sym->result->attr.pointer))
556
            break;
557
          else if (as && fas && ns->entries->sym->result != el->sym->result
558
                      && gfc_compare_array_spec (as, fas) == 0)
559
            gfc_error ("Function %s at %L has entries with mismatched "
560
                       "array specifications", ns->entries->sym->name,
561
                       &ns->entries->sym->declared_at);
562
          /* The characteristics need to match and thus both need to have
563
             the same string length, i.e. both len=*, or both len=4.
564
             Having both len=<variable> is also possible, but difficult to
565
             check at compile time.  */
566
          else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
567
                   && (((ts->u.cl->length && !fts->u.cl->length)
568
                        ||(!ts->u.cl->length && fts->u.cl->length))
569
                       || (ts->u.cl->length
570
                           && ts->u.cl->length->expr_type
571
                              != fts->u.cl->length->expr_type)
572
                       || (ts->u.cl->length
573
                           && ts->u.cl->length->expr_type == EXPR_CONSTANT
574
                           && mpz_cmp (ts->u.cl->length->value.integer,
575
                                       fts->u.cl->length->value.integer) != 0)))
576
            gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
577
                            "entries returning variables of different "
578
                            "string lengths", ns->entries->sym->name,
579
                            &ns->entries->sym->declared_at);
580
        }
581
 
582
      if (el == NULL)
583
        {
584
          sym = ns->entries->sym->result;
585
          /* All result types the same.  */
586
          proc->ts = *fts;
587
          if (sym->attr.dimension)
588
            gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
589
          if (sym->attr.pointer)
590
            gfc_add_pointer (&proc->attr, NULL);
591
        }
592
      else
593
        {
594
          /* Otherwise the result will be passed through a union by
595
             reference.  */
596
          proc->attr.mixed_entry_master = 1;
597
          for (el = ns->entries; el; el = el->next)
598
            {
599
              sym = el->sym->result;
600
              if (sym->attr.dimension)
601
                {
602
                  if (el == ns->entries)
603
                    gfc_error ("FUNCTION result %s can't be an array in "
604
                               "FUNCTION %s at %L", sym->name,
605
                               ns->entries->sym->name, &sym->declared_at);
606
                  else
607
                    gfc_error ("ENTRY result %s can't be an array in "
608
                               "FUNCTION %s at %L", sym->name,
609
                               ns->entries->sym->name, &sym->declared_at);
610
                }
611
              else if (sym->attr.pointer)
612
                {
613
                  if (el == ns->entries)
614
                    gfc_error ("FUNCTION result %s can't be a POINTER in "
615
                               "FUNCTION %s at %L", sym->name,
616
                               ns->entries->sym->name, &sym->declared_at);
617
                  else
618
                    gfc_error ("ENTRY result %s can't be a POINTER in "
619
                               "FUNCTION %s at %L", sym->name,
620
                               ns->entries->sym->name, &sym->declared_at);
621
                }
622
              else
623
                {
624
                  ts = &sym->ts;
625
                  if (ts->type == BT_UNKNOWN)
626
                    ts = gfc_get_default_type (sym->name, NULL);
627
                  switch (ts->type)
628
                    {
629
                    case BT_INTEGER:
630
                      if (ts->kind == gfc_default_integer_kind)
631
                        sym = NULL;
632
                      break;
633
                    case BT_REAL:
634
                      if (ts->kind == gfc_default_real_kind
635
                          || ts->kind == gfc_default_double_kind)
636
                        sym = NULL;
637
                      break;
638
                    case BT_COMPLEX:
639
                      if (ts->kind == gfc_default_complex_kind)
640
                        sym = NULL;
641
                      break;
642
                    case BT_LOGICAL:
643
                      if (ts->kind == gfc_default_logical_kind)
644
                        sym = NULL;
645
                      break;
646
                    case BT_UNKNOWN:
647
                      /* We will issue error elsewhere.  */
648
                      sym = NULL;
649
                      break;
650
                    default:
651
                      break;
652
                    }
653
                  if (sym)
654
                    {
655
                      if (el == ns->entries)
656
                        gfc_error ("FUNCTION result %s can't be of type %s "
657
                                   "in FUNCTION %s at %L", sym->name,
658
                                   gfc_typename (ts), ns->entries->sym->name,
659
                                   &sym->declared_at);
660
                      else
661
                        gfc_error ("ENTRY result %s can't be of type %s "
662
                                   "in FUNCTION %s at %L", sym->name,
663
                                   gfc_typename (ts), ns->entries->sym->name,
664
                                   &sym->declared_at);
665
                    }
666
                }
667
            }
668
        }
669
    }
670
  proc->attr.access = ACCESS_PRIVATE;
671
  proc->attr.entry_master = 1;
672
 
673
  /* Merge all the entry point arguments.  */
674
  for (el = ns->entries; el; el = el->next)
675
    merge_argument_lists (proc, el->sym->formal);
676
 
677
  /* Check the master formal arguments for any that are not
678
     present in all entry points.  */
679
  for (el = ns->entries; el; el = el->next)
680
    check_argument_lists (proc, el->sym->formal);
681
 
682
  /* Use the master function for the function body.  */
683
  ns->proc_name = proc;
684
 
685
  /* Finalize the new symbols.  */
686
  gfc_commit_symbols ();
687
 
688
  /* Restore the original namespace.  */
689
  gfc_current_ns = old_ns;
690
}
691
 
692
 
693
static bool
694
has_default_initializer (gfc_symbol *der)
695
{
696
  gfc_component *c;
697
 
698
  gcc_assert (der->attr.flavor == FL_DERIVED);
699
  for (c = der->components; c; c = c->next)
700
    if ((c->ts.type != BT_DERIVED && c->initializer)
701
        || (c->ts.type == BT_DERIVED
702
            && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
703
      break;
704
 
705
  return c != NULL;
706
}
707
 
708
/* Resolve common variables.  */
709
static void
710
resolve_common_vars (gfc_symbol *sym, bool named_common)
711
{
712
  gfc_symbol *csym = sym;
713
 
714
  for (; csym; csym = csym->common_next)
715
    {
716
      if (csym->value || csym->attr.data)
717
        {
718
          if (!csym->ns->is_block_data)
719
            gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
720
                            "but only in BLOCK DATA initialization is "
721
                            "allowed", csym->name, &csym->declared_at);
722
          else if (!named_common)
723
            gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
724
                            "in a blank COMMON but initialization is only "
725
                            "allowed in named common blocks", csym->name,
726
                            &csym->declared_at);
727
        }
728
 
729
      if (csym->ts.type != BT_DERIVED)
730
        continue;
731
 
732
      if (!(csym->ts.u.derived->attr.sequence
733
            || csym->ts.u.derived->attr.is_bind_c))
734
        gfc_error_now ("Derived type variable '%s' in COMMON at %L "
735
                       "has neither the SEQUENCE nor the BIND(C) "
736
                       "attribute", csym->name, &csym->declared_at);
737
      if (csym->ts.u.derived->attr.alloc_comp)
738
        gfc_error_now ("Derived type variable '%s' in COMMON at %L "
739
                       "has an ultimate component that is "
740
                       "allocatable", csym->name, &csym->declared_at);
741
      if (has_default_initializer (csym->ts.u.derived))
742
        gfc_error_now ("Derived type variable '%s' in COMMON at %L "
743
                       "may not have default initializer", csym->name,
744
                       &csym->declared_at);
745
 
746
      if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
747
        gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
748
    }
749
}
750
 
751
/* Resolve common blocks.  */
752
static void
753
resolve_common_blocks (gfc_symtree *common_root)
754
{
755
  gfc_symbol *sym;
756
 
757
  if (common_root == NULL)
758
    return;
759
 
760
  if (common_root->left)
761
    resolve_common_blocks (common_root->left);
762
  if (common_root->right)
763
    resolve_common_blocks (common_root->right);
764
 
765
  resolve_common_vars (common_root->n.common->head, true);
766
 
767
  gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
768
  if (sym == NULL)
769
    return;
770
 
771
  if (sym->attr.flavor == FL_PARAMETER)
772
    gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
773
               sym->name, &common_root->n.common->where, &sym->declared_at);
774
 
775
  if (sym->attr.intrinsic)
776
    gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
777
               sym->name, &common_root->n.common->where);
778
  else if (sym->attr.result
779
           || gfc_is_function_return_value (sym, gfc_current_ns))
780
    gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
781
                    "that is also a function result", sym->name,
782
                    &common_root->n.common->where);
783
  else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
784
           && sym->attr.proc != PROC_ST_FUNCTION)
785
    gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
786
                    "that is also a global procedure", sym->name,
787
                    &common_root->n.common->where);
788
}
789
 
790
 
791
/* Resolve contained function types.  Because contained functions can call one
792
   another, they have to be worked out before any of the contained procedures
793
   can be resolved.
794
 
795
   The good news is that if a function doesn't already have a type, the only
796
   way it can get one is through an IMPLICIT type or a RESULT variable, because
797
   by definition contained functions are contained namespace they're contained
798
   in, not in a sibling or parent namespace.  */
799
 
800
static void
801
resolve_contained_functions (gfc_namespace *ns)
802
{
803
  gfc_namespace *child;
804
  gfc_entry_list *el;
805
 
806
  resolve_formal_arglists (ns);
807
 
808
  for (child = ns->contained; child; child = child->sibling)
809
    {
810
      /* Resolve alternate entry points first.  */
811
      resolve_entries (child);
812
 
813
      /* Then check function return types.  */
814
      resolve_contained_fntype (child->proc_name, child);
815
      for (el = child->entries; el; el = el->next)
816
        resolve_contained_fntype (el->sym, child);
817
    }
818
}
819
 
820
 
821
/* Resolve all of the elements of a structure constructor and make sure that
822
   the types are correct.  */
823
 
824
static gfc_try
825
resolve_structure_cons (gfc_expr *expr)
826
{
827
  gfc_constructor *cons;
828
  gfc_component *comp;
829
  gfc_try t;
830
  symbol_attribute a;
831
 
832
  t = SUCCESS;
833
  cons = expr->value.constructor;
834
  /* A constructor may have references if it is the result of substituting a
835
     parameter variable.  In this case we just pull out the component we
836
     want.  */
837
  if (expr->ref)
838
    comp = expr->ref->u.c.sym->components;
839
  else
840
    comp = expr->ts.u.derived->components;
841
 
842
  /* See if the user is trying to invoke a structure constructor for one of
843
     the iso_c_binding derived types.  */
844
  if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
845
      && expr->ts.u.derived->ts.is_iso_c && cons
846
      && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
847
    {
848
      gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
849
                 expr->ts.u.derived->name, &(expr->where));
850
      return FAILURE;
851
    }
852
 
853
  /* Return if structure constructor is c_null_(fun)prt.  */
854
  if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
855
      && expr->ts.u.derived->ts.is_iso_c && cons
856
      && cons->expr && cons->expr->expr_type == EXPR_NULL)
857
    return SUCCESS;
858
 
859
  for (; comp; comp = comp->next, cons = cons->next)
860
    {
861
      int rank;
862
 
863
      if (!cons->expr)
864
        continue;
865
 
866
      if (gfc_resolve_expr (cons->expr) == FAILURE)
867
        {
868
          t = FAILURE;
869
          continue;
870
        }
871
 
872
      rank = comp->as ? comp->as->rank : 0;
873
      if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
874
          && (comp->attr.allocatable || cons->expr->rank))
875
        {
876
          gfc_error ("The rank of the element in the derived type "
877
                     "constructor at %L does not match that of the "
878
                     "component (%d/%d)", &cons->expr->where,
879
                     cons->expr->rank, rank);
880
          t = FAILURE;
881
        }
882
 
883
      /* If we don't have the right type, try to convert it.  */
884
 
885
      if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
886
        {
887
          t = FAILURE;
888
          if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
889
            gfc_error ("The element in the derived type constructor at %L, "
890
                       "for pointer component '%s', is %s but should be %s",
891
                       &cons->expr->where, comp->name,
892
                       gfc_basic_typename (cons->expr->ts.type),
893
                       gfc_basic_typename (comp->ts.type));
894
          else
895
            t = gfc_convert_type (cons->expr, &comp->ts, 1);
896
        }
897
 
898
      if (cons->expr->expr_type == EXPR_NULL
899
          && !(comp->attr.pointer || comp->attr.allocatable
900
               || comp->attr.proc_pointer
901
               || (comp->ts.type == BT_CLASS
902
                   && (comp->ts.u.derived->components->attr.pointer
903
                       || comp->ts.u.derived->components->attr.allocatable))))
904
        {
905
          t = FAILURE;
906
          gfc_error ("The NULL in the derived type constructor at %L is "
907
                     "being applied to component '%s', which is neither "
908
                     "a POINTER nor ALLOCATABLE", &cons->expr->where,
909
                     comp->name);
910
        }
911
 
912
      if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
913
        continue;
914
 
915
      a = gfc_expr_attr (cons->expr);
916
 
917
      if (!a.pointer && !a.target)
918
        {
919
          t = FAILURE;
920
          gfc_error ("The element in the derived type constructor at %L, "
921
                     "for pointer component '%s' should be a POINTER or "
922
                     "a TARGET", &cons->expr->where, comp->name);
923
        }
924
 
925
      /* F2003, C1272 (3).  */
926
      if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
927
          && gfc_impure_variable (cons->expr->symtree->n.sym))
928
        {
929
          t = FAILURE;
930
          gfc_error ("Invalid expression in the derived type constructor for pointer "
931
                     "component '%s' at %L in PURE procedure", comp->name,
932
                     &cons->expr->where);
933
        }
934
    }
935
 
936
  return t;
937
}
938
 
939
 
940
/****************** Expression name resolution ******************/
941
 
942
/* Returns 0 if a symbol was not declared with a type or
943
   attribute declaration statement, nonzero otherwise.  */
944
 
945
static int
946
was_declared (gfc_symbol *sym)
947
{
948
  symbol_attribute a;
949
 
950
  a = sym->attr;
951
 
952
  if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
953
    return 1;
954
 
955
  if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
956
      || a.optional || a.pointer || a.save || a.target || a.volatile_
957
      || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
958
      || a.asynchronous)
959
    return 1;
960
 
961
  return 0;
962
}
963
 
964
 
965
/* Determine if a symbol is generic or not.  */
966
 
967
static int
968
generic_sym (gfc_symbol *sym)
969
{
970
  gfc_symbol *s;
971
 
972
  if (sym->attr.generic ||
973
      (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
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
  if (s != NULL)
982
    {
983
      if (s == sym)
984
        return 0;
985
      else
986
        return generic_sym (s);
987
    }
988
 
989
  return 0;
990
}
991
 
992
 
993
/* Determine if a symbol is specific or not.  */
994
 
995
static int
996
specific_sym (gfc_symbol *sym)
997
{
998
  gfc_symbol *s;
999
 
1000
  if (sym->attr.if_source == IFSRC_IFBODY
1001
      || sym->attr.proc == PROC_MODULE
1002
      || sym->attr.proc == PROC_INTERNAL
1003
      || sym->attr.proc == PROC_ST_FUNCTION
1004
      || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1005
      || sym->attr.external)
1006
    return 1;
1007
 
1008
  if (was_declared (sym) || sym->ns->parent == NULL)
1009
    return 0;
1010
 
1011
  gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1012
 
1013
  return (s == NULL) ? 0 : specific_sym (s);
1014
}
1015
 
1016
 
1017
/* Figure out if the procedure is specific, generic or unknown.  */
1018
 
1019
typedef enum
1020
{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1021
proc_type;
1022
 
1023
static proc_type
1024
procedure_kind (gfc_symbol *sym)
1025
{
1026
  if (generic_sym (sym))
1027
    return PTYPE_GENERIC;
1028
 
1029
  if (specific_sym (sym))
1030
    return PTYPE_SPECIFIC;
1031
 
1032
  return PTYPE_UNKNOWN;
1033
}
1034
 
1035
/* Check references to assumed size arrays.  The flag need_full_assumed_size
1036
   is nonzero when matching actual arguments.  */
1037
 
1038
static int need_full_assumed_size = 0;
1039
 
1040
static bool
1041
check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1042
{
1043
  if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1044
      return false;
1045
 
1046
  /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1047
     What should it be?  */
1048
  if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1049
          && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1050
               && (e->ref->u.ar.type == AR_FULL))
1051
    {
1052
      gfc_error ("The upper bound in the last dimension must "
1053
                 "appear in the reference to the assumed size "
1054
                 "array '%s' at %L", sym->name, &e->where);
1055
      return true;
1056
    }
1057
  return false;
1058
}
1059
 
1060
 
1061
/* Look for bad assumed size array references in argument expressions
1062
  of elemental and array valued intrinsic procedures.  Since this is
1063
  called from procedure resolution functions, it only recurses at
1064
  operators.  */
1065
 
1066
static bool
1067
resolve_assumed_size_actual (gfc_expr *e)
1068
{
1069
  if (e == NULL)
1070
   return false;
1071
 
1072
  switch (e->expr_type)
1073
    {
1074
    case EXPR_VARIABLE:
1075
      if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1076
        return true;
1077
      break;
1078
 
1079
    case EXPR_OP:
1080
      if (resolve_assumed_size_actual (e->value.op.op1)
1081
          || resolve_assumed_size_actual (e->value.op.op2))
1082
        return true;
1083
      break;
1084
 
1085
    default:
1086
      break;
1087
    }
1088
  return false;
1089
}
1090
 
1091
 
1092
/* Check a generic procedure, passed as an actual argument, to see if
1093
   there is a matching specific name.  If none, it is an error, and if
1094
   more than one, the reference is ambiguous.  */
1095
static int
1096
count_specific_procs (gfc_expr *e)
1097
{
1098
  int n;
1099
  gfc_interface *p;
1100
  gfc_symbol *sym;
1101
 
1102
  n = 0;
1103
  sym = e->symtree->n.sym;
1104
 
1105
  for (p = sym->generic; p; p = p->next)
1106
    if (strcmp (sym->name, p->sym->name) == 0)
1107
      {
1108
        e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1109
                                       sym->name);
1110
        n++;
1111
      }
1112
 
1113
  if (n > 1)
1114
    gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1115
               &e->where);
1116
 
1117
  if (n == 0)
1118
    gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1119
               "argument at %L", sym->name, &e->where);
1120
 
1121
  return n;
1122
}
1123
 
1124
 
1125
/* See if a call to sym could possibly be a not allowed RECURSION because of
1126
   a missing RECURIVE declaration.  This means that either sym is the current
1127
   context itself, or sym is the parent of a contained procedure calling its
1128
   non-RECURSIVE containing procedure.
1129
   This also works if sym is an ENTRY.  */
1130
 
1131
static bool
1132
is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1133
{
1134
  gfc_symbol* proc_sym;
1135
  gfc_symbol* context_proc;
1136
  gfc_namespace* real_context;
1137
 
1138
  if (sym->attr.flavor == FL_PROGRAM)
1139
    return false;
1140
 
1141
  gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1142
 
1143
  /* If we've got an ENTRY, find real procedure.  */
1144
  if (sym->attr.entry && sym->ns->entries)
1145
    proc_sym = sym->ns->entries->sym;
1146
  else
1147
    proc_sym = sym;
1148
 
1149
  /* If sym is RECURSIVE, all is well of course.  */
1150
  if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1151
    return false;
1152
 
1153
  /* Find the context procedure's "real" symbol if it has entries.
1154
     We look for a procedure symbol, so recurse on the parents if we don't
1155
     find one (like in case of a BLOCK construct).  */
1156
  for (real_context = context; ; real_context = real_context->parent)
1157
    {
1158
      /* We should find something, eventually!  */
1159
      gcc_assert (real_context);
1160
 
1161
      context_proc = (real_context->entries ? real_context->entries->sym
1162
                                            : real_context->proc_name);
1163
 
1164
      /* In some special cases, there may not be a proc_name, like for this
1165
         invalid code:
1166
         real(bad_kind()) function foo () ...
1167
         when checking the call to bad_kind ().
1168
         In these cases, we simply return here and assume that the
1169
         call is ok.  */
1170
      if (!context_proc)
1171
        return false;
1172
 
1173
      if (context_proc->attr.flavor != FL_LABEL)
1174
        break;
1175
    }
1176
 
1177
  /* A call from sym's body to itself is recursion, of course.  */
1178
  if (context_proc == proc_sym)
1179
    return true;
1180
 
1181
  /* The same is true if context is a contained procedure and sym the
1182
     containing one.  */
1183
  if (context_proc->attr.contained)
1184
    {
1185
      gfc_symbol* parent_proc;
1186
 
1187
      gcc_assert (context->parent);
1188
      parent_proc = (context->parent->entries ? context->parent->entries->sym
1189
                                              : context->parent->proc_name);
1190
 
1191
      if (parent_proc == proc_sym)
1192
        return true;
1193
    }
1194
 
1195
  return false;
1196
}
1197
 
1198
 
1199
/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1200
   its typespec and formal argument list.  */
1201
 
1202
static gfc_try
1203
resolve_intrinsic (gfc_symbol *sym, locus *loc)
1204
{
1205
  gfc_intrinsic_sym* isym;
1206
  const char* symstd;
1207
 
1208
  if (sym->formal)
1209
    return SUCCESS;
1210
 
1211
  /* We already know this one is an intrinsic, so we don't call
1212
     gfc_is_intrinsic for full checking but rather use gfc_find_function and
1213
     gfc_find_subroutine directly to check whether it is a function or
1214
     subroutine.  */
1215
 
1216
  if ((isym = gfc_find_function (sym->name)))
1217
    {
1218
      if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1219
          && !sym->attr.implicit_type)
1220
        gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1221
                      " ignored", sym->name, &sym->declared_at);
1222
 
1223
      if (!sym->attr.function &&
1224
          gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1225
        return FAILURE;
1226
 
1227
      sym->ts = isym->ts;
1228
    }
1229
  else if ((isym = gfc_find_subroutine (sym->name)))
1230
    {
1231
      if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1232
        {
1233
          gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1234
                      " specifier", sym->name, &sym->declared_at);
1235
          return FAILURE;
1236
        }
1237
 
1238
      if (!sym->attr.subroutine &&
1239
          gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1240
        return FAILURE;
1241
    }
1242
  else
1243
    {
1244
      gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1245
                 &sym->declared_at);
1246
      return FAILURE;
1247
    }
1248
 
1249
  gfc_copy_formal_args_intr (sym, isym);
1250
 
1251
  /* Check it is actually available in the standard settings.  */
1252
  if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1253
      == FAILURE)
1254
    {
1255
      gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1256
                 " available in the current standard settings but %s.  Use"
1257
                 " an appropriate -std=* option or enable -fall-intrinsics"
1258
                 " in order to use it.",
1259
                 sym->name, &sym->declared_at, symstd);
1260
      return FAILURE;
1261
    }
1262
 
1263
  return SUCCESS;
1264
}
1265
 
1266
 
1267
/* Resolve a procedure expression, like passing it to a called procedure or as
1268
   RHS for a procedure pointer assignment.  */
1269
 
1270
static gfc_try
1271
resolve_procedure_expression (gfc_expr* expr)
1272
{
1273
  gfc_symbol* sym;
1274
 
1275
  if (expr->expr_type != EXPR_VARIABLE)
1276
    return SUCCESS;
1277
  gcc_assert (expr->symtree);
1278
 
1279
  sym = expr->symtree->n.sym;
1280
 
1281
  if (sym->attr.intrinsic)
1282
    resolve_intrinsic (sym, &expr->where);
1283
 
1284
  if (sym->attr.flavor != FL_PROCEDURE
1285
      || (sym->attr.function && sym->result == sym))
1286
    return SUCCESS;
1287
 
1288
  /* A non-RECURSIVE procedure that is used as procedure expression within its
1289
     own body is in danger of being called recursively.  */
1290
  if (is_illegal_recursion (sym, gfc_current_ns))
1291
    gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1292
                 " itself recursively.  Declare it RECURSIVE or use"
1293
                 " -frecursive", sym->name, &expr->where);
1294
 
1295
  return SUCCESS;
1296
}
1297
 
1298
 
1299
/* Resolve an actual argument list.  Most of the time, this is just
1300
   resolving the expressions in the list.
1301
   The exception is that we sometimes have to decide whether arguments
1302
   that look like procedure arguments are really simple variable
1303
   references.  */
1304
 
1305
static gfc_try
1306
resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1307
                        bool no_formal_args)
1308
{
1309
  gfc_symbol *sym;
1310
  gfc_symtree *parent_st;
1311
  gfc_expr *e;
1312
  int save_need_full_assumed_size;
1313
  gfc_component *comp;
1314
 
1315
  for (; arg; arg = arg->next)
1316
    {
1317
      e = arg->expr;
1318
      if (e == NULL)
1319
        {
1320
          /* Check the label is a valid branching target.  */
1321
          if (arg->label)
1322
            {
1323
              if (arg->label->defined == ST_LABEL_UNKNOWN)
1324
                {
1325
                  gfc_error ("Label %d referenced at %L is never defined",
1326
                             arg->label->value, &arg->label->where);
1327
                  return FAILURE;
1328
                }
1329
            }
1330
          continue;
1331
        }
1332
 
1333
      if (gfc_is_proc_ptr_comp (e, &comp))
1334
        {
1335
          e->ts = comp->ts;
1336
          if (e->expr_type == EXPR_PPC)
1337
            {
1338
              if (comp->as != NULL)
1339
                e->rank = comp->as->rank;
1340
              e->expr_type = EXPR_FUNCTION;
1341
            }
1342
          if (gfc_resolve_expr (e) == FAILURE)
1343
            return FAILURE;
1344
          goto argument_list;
1345
        }
1346
 
1347
      if (e->expr_type == EXPR_VARIABLE
1348
            && e->symtree->n.sym->attr.generic
1349
            && no_formal_args
1350
            && count_specific_procs (e) != 1)
1351
        return FAILURE;
1352
 
1353
      if (e->ts.type != BT_PROCEDURE)
1354
        {
1355
          save_need_full_assumed_size = need_full_assumed_size;
1356
          if (e->expr_type != EXPR_VARIABLE)
1357
            need_full_assumed_size = 0;
1358
          if (gfc_resolve_expr (e) != SUCCESS)
1359
            return FAILURE;
1360
          need_full_assumed_size = save_need_full_assumed_size;
1361
          goto argument_list;
1362
        }
1363
 
1364
      /* See if the expression node should really be a variable reference.  */
1365
 
1366
      sym = e->symtree->n.sym;
1367
 
1368
      if (sym->attr.flavor == FL_PROCEDURE
1369
          || sym->attr.intrinsic
1370
          || sym->attr.external)
1371
        {
1372
          int actual_ok;
1373
 
1374
          /* If a procedure is not already determined to be something else
1375
             check if it is intrinsic.  */
1376
          if (!sym->attr.intrinsic
1377
              && !(sym->attr.external || sym->attr.use_assoc
1378
                   || sym->attr.if_source == IFSRC_IFBODY)
1379
              && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1380
            sym->attr.intrinsic = 1;
1381
 
1382
          if (sym->attr.proc == PROC_ST_FUNCTION)
1383
            {
1384
              gfc_error ("Statement function '%s' at %L is not allowed as an "
1385
                         "actual argument", sym->name, &e->where);
1386
            }
1387
 
1388
          actual_ok = gfc_intrinsic_actual_ok (sym->name,
1389
                                               sym->attr.subroutine);
1390
          if (sym->attr.intrinsic && actual_ok == 0)
1391
            {
1392
              gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1393
                         "actual argument", sym->name, &e->where);
1394
            }
1395
 
1396
          if (sym->attr.contained && !sym->attr.use_assoc
1397
              && sym->ns->proc_name->attr.flavor != FL_MODULE)
1398
            {
1399
              gfc_error ("Internal procedure '%s' is not allowed as an "
1400
                         "actual argument at %L", sym->name, &e->where);
1401
            }
1402
 
1403
          if (sym->attr.elemental && !sym->attr.intrinsic)
1404
            {
1405
              gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1406
                         "allowed as an actual argument at %L", sym->name,
1407
                         &e->where);
1408
            }
1409
 
1410
          /* Check if a generic interface has a specific procedure
1411
            with the same name before emitting an error.  */
1412
          if (sym->attr.generic && count_specific_procs (e) != 1)
1413
            return FAILURE;
1414
 
1415
          /* Just in case a specific was found for the expression.  */
1416
          sym = e->symtree->n.sym;
1417
 
1418
          /* If the symbol is the function that names the current (or
1419
             parent) scope, then we really have a variable reference.  */
1420
 
1421
          if (gfc_is_function_return_value (sym, sym->ns))
1422
            goto got_variable;
1423
 
1424
          /* If all else fails, see if we have a specific intrinsic.  */
1425
          if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1426
            {
1427
              gfc_intrinsic_sym *isym;
1428
 
1429
              isym = gfc_find_function (sym->name);
1430
              if (isym == NULL || !isym->specific)
1431
                {
1432
                  gfc_error ("Unable to find a specific INTRINSIC procedure "
1433
                             "for the reference '%s' at %L", sym->name,
1434
                             &e->where);
1435
                  return FAILURE;
1436
                }
1437
              sym->ts = isym->ts;
1438
              sym->attr.intrinsic = 1;
1439
              sym->attr.function = 1;
1440
            }
1441
 
1442
          if (gfc_resolve_expr (e) == FAILURE)
1443
            return FAILURE;
1444
          goto argument_list;
1445
        }
1446
 
1447
      /* See if the name is a module procedure in a parent unit.  */
1448
 
1449
      if (was_declared (sym) || sym->ns->parent == NULL)
1450
        goto got_variable;
1451
 
1452
      if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1453
        {
1454
          gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1455
          return FAILURE;
1456
        }
1457
 
1458
      if (parent_st == NULL)
1459
        goto got_variable;
1460
 
1461
      sym = parent_st->n.sym;
1462
      e->symtree = parent_st;           /* Point to the right thing.  */
1463
 
1464
      if (sym->attr.flavor == FL_PROCEDURE
1465
          || sym->attr.intrinsic
1466
          || sym->attr.external)
1467
        {
1468
          if (gfc_resolve_expr (e) == FAILURE)
1469
            return FAILURE;
1470
          goto argument_list;
1471
        }
1472
 
1473
    got_variable:
1474
      e->expr_type = EXPR_VARIABLE;
1475
      e->ts = sym->ts;
1476
      if (sym->as != NULL)
1477
        {
1478
          e->rank = sym->as->rank;
1479
          e->ref = gfc_get_ref ();
1480
          e->ref->type = REF_ARRAY;
1481
          e->ref->u.ar.type = AR_FULL;
1482
          e->ref->u.ar.as = sym->as;
1483
        }
1484
 
1485
      /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1486
         primary.c (match_actual_arg). If above code determines that it
1487
         is a  variable instead, it needs to be resolved as it was not
1488
         done at the beginning of this function.  */
1489
      save_need_full_assumed_size = need_full_assumed_size;
1490
      if (e->expr_type != EXPR_VARIABLE)
1491
        need_full_assumed_size = 0;
1492
      if (gfc_resolve_expr (e) != SUCCESS)
1493
        return FAILURE;
1494
      need_full_assumed_size = save_need_full_assumed_size;
1495
 
1496
    argument_list:
1497
      /* Check argument list functions %VAL, %LOC and %REF.  There is
1498
         nothing to do for %REF.  */
1499
      if (arg->name && arg->name[0] == '%')
1500
        {
1501
          if (strncmp ("%VAL", arg->name, 4) == 0)
1502
            {
1503
              if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1504
                {
1505
                  gfc_error ("By-value argument at %L is not of numeric "
1506
                             "type", &e->where);
1507
                  return FAILURE;
1508
                }
1509
 
1510
              if (e->rank)
1511
                {
1512
                  gfc_error ("By-value argument at %L cannot be an array or "
1513
                             "an array section", &e->where);
1514
                return FAILURE;
1515
                }
1516
 
1517
              /* Intrinsics are still PROC_UNKNOWN here.  However,
1518
                 since same file external procedures are not resolvable
1519
                 in gfortran, it is a good deal easier to leave them to
1520
                 intrinsic.c.  */
1521
              if (ptype != PROC_UNKNOWN
1522
                  && ptype != PROC_DUMMY
1523
                  && ptype != PROC_EXTERNAL
1524
                  && ptype != PROC_MODULE)
1525
                {
1526
                  gfc_error ("By-value argument at %L is not allowed "
1527
                             "in this context", &e->where);
1528
                  return FAILURE;
1529
                }
1530
            }
1531
 
1532
          /* Statement functions have already been excluded above.  */
1533
          else if (strncmp ("%LOC", arg->name, 4) == 0
1534
                   && e->ts.type == BT_PROCEDURE)
1535
            {
1536
              if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1537
                {
1538
                  gfc_error ("Passing internal procedure at %L by location "
1539
                             "not allowed", &e->where);
1540
                  return FAILURE;
1541
                }
1542
            }
1543
        }
1544
    }
1545
 
1546
  return SUCCESS;
1547
}
1548
 
1549
 
1550
/* Do the checks of the actual argument list that are specific to elemental
1551
   procedures.  If called with c == NULL, we have a function, otherwise if
1552
   expr == NULL, we have a subroutine.  */
1553
 
1554
static gfc_try
1555
resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1556
{
1557
  gfc_actual_arglist *arg0;
1558
  gfc_actual_arglist *arg;
1559
  gfc_symbol *esym = NULL;
1560
  gfc_intrinsic_sym *isym = NULL;
1561
  gfc_expr *e = NULL;
1562
  gfc_intrinsic_arg *iformal = NULL;
1563
  gfc_formal_arglist *eformal = NULL;
1564
  bool formal_optional = false;
1565
  bool set_by_optional = false;
1566
  int i;
1567
  int rank = 0;
1568
 
1569
  /* Is this an elemental procedure?  */
1570
  if (expr && expr->value.function.actual != NULL)
1571
    {
1572
      if (expr->value.function.esym != NULL
1573
          && expr->value.function.esym->attr.elemental)
1574
        {
1575
          arg0 = expr->value.function.actual;
1576
          esym = expr->value.function.esym;
1577
        }
1578
      else if (expr->value.function.isym != NULL
1579
               && expr->value.function.isym->elemental)
1580
        {
1581
          arg0 = expr->value.function.actual;
1582
          isym = expr->value.function.isym;
1583
        }
1584
      else
1585
        return SUCCESS;
1586
    }
1587
  else if (c && c->ext.actual != NULL)
1588
    {
1589
      arg0 = c->ext.actual;
1590
 
1591
      if (c->resolved_sym)
1592
        esym = c->resolved_sym;
1593
      else
1594
        esym = c->symtree->n.sym;
1595
      gcc_assert (esym);
1596
 
1597
      if (!esym->attr.elemental)
1598
        return SUCCESS;
1599
    }
1600
  else
1601
    return SUCCESS;
1602
 
1603
  /* The rank of an elemental is the rank of its array argument(s).  */
1604
  for (arg = arg0; arg; arg = arg->next)
1605
    {
1606
      if (arg->expr != NULL && arg->expr->rank > 0)
1607
        {
1608
          rank = arg->expr->rank;
1609
          if (arg->expr->expr_type == EXPR_VARIABLE
1610
              && arg->expr->symtree->n.sym->attr.optional)
1611
            set_by_optional = true;
1612
 
1613
          /* Function specific; set the result rank and shape.  */
1614
          if (expr)
1615
            {
1616
              expr->rank = rank;
1617
              if (!expr->shape && arg->expr->shape)
1618
                {
1619
                  expr->shape = gfc_get_shape (rank);
1620
                  for (i = 0; i < rank; i++)
1621
                    mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1622
                }
1623
            }
1624
          break;
1625
        }
1626
    }
1627
 
1628
  /* If it is an array, it shall not be supplied as an actual argument
1629
     to an elemental procedure unless an array of the same rank is supplied
1630
     as an actual argument corresponding to a nonoptional dummy argument of
1631
     that elemental procedure(12.4.1.5).  */
1632
  formal_optional = false;
1633
  if (isym)
1634
    iformal = isym->formal;
1635
  else
1636
    eformal = esym->formal;
1637
 
1638
  for (arg = arg0; arg; arg = arg->next)
1639
    {
1640
      if (eformal)
1641
        {
1642
          if (eformal->sym && eformal->sym->attr.optional)
1643
            formal_optional = true;
1644
          eformal = eformal->next;
1645
        }
1646
      else if (isym && iformal)
1647
        {
1648
          if (iformal->optional)
1649
            formal_optional = true;
1650
          iformal = iformal->next;
1651
        }
1652
      else if (isym)
1653
        formal_optional = true;
1654
 
1655
      if (pedantic && arg->expr != NULL
1656
          && arg->expr->expr_type == EXPR_VARIABLE
1657
          && arg->expr->symtree->n.sym->attr.optional
1658
          && formal_optional
1659
          && arg->expr->rank
1660
          && (set_by_optional || arg->expr->rank != rank)
1661
          && !(isym && isym->id == GFC_ISYM_CONVERSION))
1662
        {
1663
          gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1664
                       "MISSING, it cannot be the actual argument of an "
1665
                       "ELEMENTAL procedure unless there is a non-optional "
1666
                       "argument with the same rank (12.4.1.5)",
1667
                       arg->expr->symtree->n.sym->name, &arg->expr->where);
1668
          return FAILURE;
1669
        }
1670
    }
1671
 
1672
  for (arg = arg0; arg; arg = arg->next)
1673
    {
1674
      if (arg->expr == NULL || arg->expr->rank == 0)
1675
        continue;
1676
 
1677
      /* Being elemental, the last upper bound of an assumed size array
1678
         argument must be present.  */
1679
      if (resolve_assumed_size_actual (arg->expr))
1680
        return FAILURE;
1681
 
1682
      /* Elemental procedure's array actual arguments must conform.  */
1683
      if (e != NULL)
1684
        {
1685
          if (gfc_check_conformance (arg->expr, e,
1686
                                     "elemental procedure") == FAILURE)
1687
            return FAILURE;
1688
        }
1689
      else
1690
        e = arg->expr;
1691
    }
1692
 
1693
  /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1694
     is an array, the intent inout/out variable needs to be also an array.  */
1695
  if (rank > 0 && esym && expr == NULL)
1696
    for (eformal = esym->formal, arg = arg0; arg && eformal;
1697
         arg = arg->next, eformal = eformal->next)
1698
      if ((eformal->sym->attr.intent == INTENT_OUT
1699
           || eformal->sym->attr.intent == INTENT_INOUT)
1700
          && arg->expr && arg->expr->rank == 0)
1701
        {
1702
          gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1703
                     "ELEMENTAL subroutine '%s' is a scalar, but another "
1704
                     "actual argument is an array", &arg->expr->where,
1705
                     (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1706
                     : "INOUT", eformal->sym->name, esym->name);
1707
          return FAILURE;
1708
        }
1709
  return SUCCESS;
1710
}
1711
 
1712
 
1713
/* Go through each actual argument in ACTUAL and see if it can be
1714
   implemented as an inlined, non-copying intrinsic.  FNSYM is the
1715
   function being called, or NULL if not known.  */
1716
 
1717
static void
1718
find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1719
{
1720
  gfc_actual_arglist *ap;
1721
  gfc_expr *expr;
1722
 
1723
  for (ap = actual; ap; ap = ap->next)
1724
    if (ap->expr
1725
        && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1726
        && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1727
                                         NOT_ELEMENTAL))
1728
      ap->expr->inline_noncopying_intrinsic = 1;
1729
}
1730
 
1731
 
1732
/* This function does the checking of references to global procedures
1733
   as defined in sections 18.1 and 14.1, respectively, of the Fortran
1734
   77 and 95 standards.  It checks for a gsymbol for the name, making
1735
   one if it does not already exist.  If it already exists, then the
1736
   reference being resolved must correspond to the type of gsymbol.
1737
   Otherwise, the new symbol is equipped with the attributes of the
1738
   reference.  The corresponding code that is called in creating
1739
   global entities is parse.c.
1740
 
1741
   In addition, for all but -std=legacy, the gsymbols are used to
1742
   check the interfaces of external procedures from the same file.
1743
   The namespace of the gsymbol is resolved and then, once this is
1744
   done the interface is checked.  */
1745
 
1746
 
1747
static bool
1748
not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1749
{
1750
  if (!gsym_ns->proc_name->attr.recursive)
1751
    return true;
1752
 
1753
  if (sym->ns == gsym_ns)
1754
    return false;
1755
 
1756
  if (sym->ns->parent && sym->ns->parent == gsym_ns)
1757
    return false;
1758
 
1759
  return true;
1760
}
1761
 
1762
static bool
1763
not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
1764
{
1765
  if (gsym_ns->entries)
1766
    {
1767
      gfc_entry_list *entry = gsym_ns->entries;
1768
 
1769
      for (; entry; entry = entry->next)
1770
        {
1771
          if (strcmp (sym->name, entry->sym->name) == 0)
1772
            {
1773
              if (strcmp (gsym_ns->proc_name->name,
1774
                          sym->ns->proc_name->name) == 0)
1775
                return false;
1776
 
1777
              if (sym->ns->parent
1778
                  && strcmp (gsym_ns->proc_name->name,
1779
                             sym->ns->parent->proc_name->name) == 0)
1780
                return false;
1781
            }
1782
        }
1783
    }
1784
  return true;
1785
}
1786
 
1787
static void
1788
resolve_global_procedure (gfc_symbol *sym, locus *where,
1789
                          gfc_actual_arglist **actual, int sub)
1790
{
1791
  gfc_gsymbol * gsym;
1792
  gfc_namespace *ns;
1793
  enum gfc_symbol_type type;
1794
 
1795
  type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1796
 
1797
  gsym = gfc_get_gsymbol (sym->name);
1798
 
1799
  if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1800
    gfc_global_used (gsym, where);
1801
 
1802
  if (gfc_option.flag_whole_file
1803
        && sym->attr.if_source == IFSRC_UNKNOWN
1804
        && gsym->type != GSYM_UNKNOWN
1805
        && gsym->ns
1806
        && gsym->ns->resolved != -1
1807
        && gsym->ns->proc_name
1808
        && not_in_recursive (sym, gsym->ns)
1809
        && not_entry_self_reference (sym, gsym->ns))
1810
    {
1811
      /* Resolve the gsymbol namespace if needed.  */
1812
      if (!gsym->ns->resolved)
1813
        {
1814
          gfc_dt_list *old_dt_list;
1815
 
1816
          /* Stash away derived types so that the backend_decls do not
1817
             get mixed up.  */
1818
          old_dt_list = gfc_derived_types;
1819
          gfc_derived_types = NULL;
1820
 
1821
          gfc_resolve (gsym->ns);
1822
 
1823
          /* Store the new derived types with the global namespace.  */
1824
          if (gfc_derived_types)
1825
            gsym->ns->derived_types = gfc_derived_types;
1826
 
1827
          /* Restore the derived types of this namespace.  */
1828
          gfc_derived_types = old_dt_list;
1829
        }
1830
 
1831
      /* Make sure that translation for the gsymbol occurs before
1832
         the procedure currently being resolved.  */
1833
      ns = gfc_global_ns_list;
1834
      for (; ns && ns != gsym->ns; ns = ns->sibling)
1835
        {
1836
          if (ns->sibling == gsym->ns)
1837
            {
1838
              ns->sibling = gsym->ns->sibling;
1839
              gsym->ns->sibling = gfc_global_ns_list;
1840
              gfc_global_ns_list = gsym->ns;
1841
              break;
1842
            }
1843
        }
1844
 
1845
      /* Differences in constant character lengths.  */
1846
      if (sym->attr.function && sym->ts.type == BT_CHARACTER)
1847
        {
1848
          long int l1 = 0, l2 = 0;
1849
          gfc_charlen *cl1 = sym->ts.u.cl;
1850
          gfc_charlen *cl2 = gsym->ns->proc_name->ts.u.cl;
1851
 
1852
          if (cl1 != NULL
1853
              && cl1->length != NULL
1854
              && cl1->length->expr_type == EXPR_CONSTANT)
1855
            l1 = mpz_get_si (cl1->length->value.integer);
1856
 
1857
          if (cl2 != NULL
1858
              && cl2->length != NULL
1859
              && cl2->length->expr_type == EXPR_CONSTANT)
1860
            l2 = mpz_get_si (cl2->length->value.integer);
1861
 
1862
          if (l1 && l2 && l1 != l2)
1863
            gfc_error ("Character length mismatch in return type of "
1864
                       "function '%s' at %L (%ld/%ld)", sym->name,
1865
                       &sym->declared_at, l1, l2);
1866
        }
1867
 
1868
     /* Type mismatch of function return type and expected type.  */
1869
     if (sym->attr.function
1870
         && !gfc_compare_types (&sym->ts, &gsym->ns->proc_name->ts))
1871
        gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
1872
                   sym->name, &sym->declared_at, gfc_typename (&sym->ts),
1873
                   gfc_typename (&gsym->ns->proc_name->ts));
1874
 
1875
      if (gsym->ns->proc_name->formal)
1876
        {
1877
          gfc_formal_arglist *arg = gsym->ns->proc_name->formal;
1878
          for ( ; arg; arg = arg->next)
1879
            if (!arg->sym)
1880
              continue;
1881
            /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a)  */
1882
            else if (arg->sym->attr.allocatable
1883
                     || arg->sym->attr.asynchronous
1884
                     || arg->sym->attr.optional
1885
                     || arg->sym->attr.pointer
1886
                     || arg->sym->attr.target
1887
                     || arg->sym->attr.value
1888
                     || arg->sym->attr.volatile_)
1889
              {
1890
                gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
1891
                           "has an attribute that requires an explicit "
1892
                           "interface for this procedure", arg->sym->name,
1893
                           sym->name, &sym->declared_at);
1894
                break;
1895
              }
1896
            /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b)  */
1897
            else if (arg->sym && arg->sym->as
1898
                     && arg->sym->as->type == AS_ASSUMED_SHAPE)
1899
              {
1900
                gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
1901
                           "argument '%s' must have an explicit interface",
1902
                           sym->name, &sym->declared_at, arg->sym->name);
1903
                break;
1904
              }
1905
            /* F2008, 12.4.2.2 (2c)  */
1906
            else if (false) /* TODO: is co-array  */
1907
              {
1908
                gfc_error ("Procedure '%s' at %L with coarray dummy argument "
1909
                           "'%s' must have an explicit interface",
1910
                           sym->name, &sym->declared_at, arg->sym->name);
1911
                break;
1912
              }
1913
            /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d)   */
1914
            else if (false) /* TODO: is a parametrized derived type  */
1915
              {
1916
                gfc_error ("Procedure '%s' at %L with parametrized derived "
1917
                           "type argument '%s' must have an explicit "
1918
                           "interface", sym->name, &sym->declared_at,
1919
                           arg->sym->name);
1920
                break;
1921
              }
1922
            /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e)   */
1923
            else if (arg->sym->ts.type == BT_CLASS)
1924
              {
1925
                gfc_error ("Procedure '%s' at %L with polymorphic dummy "
1926
                           "argument '%s' must have an explicit interface",
1927
                           sym->name, &sym->declared_at, arg->sym->name);
1928
                break;
1929
              }
1930
        }
1931
 
1932
      if (gsym->ns->proc_name->attr.function)
1933
        {
1934
          /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
1935
          if (gsym->ns->proc_name->as
1936
              && gsym->ns->proc_name->as->rank
1937
              && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1938
            gfc_error ("The reference to function '%s' at %L either needs an "
1939
                       "explicit INTERFACE or the rank is incorrect", sym->name,
1940
                       where);
1941
 
1942
          /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
1943
          if (gsym->ns->proc_name->result->attr.pointer
1944
              || gsym->ns->proc_name->result->attr.allocatable)
1945
            gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
1946
                       "result must have an explicit interface", sym->name,
1947
                       where);
1948
 
1949
          /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c)  */
1950
          if (sym->ts.type == BT_CHARACTER
1951
              && gsym->ns->proc_name->ts.u.cl->length != NULL)
1952
            {
1953
              gfc_charlen *cl = sym->ts.u.cl;
1954
 
1955
              if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1956
                  && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1957
                {
1958
                  gfc_error ("Nonconstant character-length function '%s' at %L "
1959
                             "must have an explicit interface", sym->name,
1960
                             &sym->declared_at);
1961
                }
1962
            }
1963
        }
1964
 
1965
      /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
1966
      if (gsym->ns->proc_name->attr.elemental)
1967
        {
1968
          gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
1969
                     "interface", sym->name, &sym->declared_at);
1970
        }
1971
 
1972
      /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
1973
      if (gsym->ns->proc_name->attr.is_bind_c)
1974
        {
1975
          gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
1976
                     "an explicit interface", sym->name, &sym->declared_at);
1977
        }
1978
 
1979
      if (gfc_option.flag_whole_file == 1
1980
          || ((gfc_option.warn_std & GFC_STD_LEGACY)
1981
              && !(gfc_option.warn_std & GFC_STD_GNU)))
1982
        gfc_errors_to_warnings (1);
1983
 
1984
      gfc_procedure_use (gsym->ns->proc_name, actual, where);
1985
 
1986
      gfc_errors_to_warnings (0);
1987
    }
1988
 
1989
  if (gsym->type == GSYM_UNKNOWN)
1990
    {
1991
      gsym->type = type;
1992
      gsym->where = *where;
1993
    }
1994
 
1995
  gsym->used = 1;
1996
}
1997
 
1998
 
1999
/************* Function resolution *************/
2000
 
2001
/* Resolve a function call known to be generic.
2002
   Section 14.1.2.4.1.  */
2003
 
2004
static match
2005
resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2006
{
2007
  gfc_symbol *s;
2008
 
2009
  if (sym->attr.generic)
2010
    {
2011
      s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2012
      if (s != NULL)
2013
        {
2014
          expr->value.function.name = s->name;
2015
          expr->value.function.esym = s;
2016
 
2017
          if (s->ts.type != BT_UNKNOWN)
2018
            expr->ts = s->ts;
2019
          else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2020
            expr->ts = s->result->ts;
2021
 
2022
          if (s->as != NULL)
2023
            expr->rank = s->as->rank;
2024
          else if (s->result != NULL && s->result->as != NULL)
2025
            expr->rank = s->result->as->rank;
2026
 
2027
          gfc_set_sym_referenced (expr->value.function.esym);
2028
 
2029
          return MATCH_YES;
2030
        }
2031
 
2032
      /* TODO: Need to search for elemental references in generic
2033
         interface.  */
2034
    }
2035
 
2036
  if (sym->attr.intrinsic)
2037
    return gfc_intrinsic_func_interface (expr, 0);
2038
 
2039
  return MATCH_NO;
2040
}
2041
 
2042
 
2043
static gfc_try
2044
resolve_generic_f (gfc_expr *expr)
2045
{
2046
  gfc_symbol *sym;
2047
  match m;
2048
 
2049
  sym = expr->symtree->n.sym;
2050
 
2051
  for (;;)
2052
    {
2053
      m = resolve_generic_f0 (expr, sym);
2054
      if (m == MATCH_YES)
2055
        return SUCCESS;
2056
      else if (m == MATCH_ERROR)
2057
        return FAILURE;
2058
 
2059
generic:
2060
      if (sym->ns->parent == NULL)
2061
        break;
2062
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2063
 
2064
      if (sym == NULL)
2065
        break;
2066
      if (!generic_sym (sym))
2067
        goto generic;
2068
    }
2069
 
2070
  /* Last ditch attempt.  See if the reference is to an intrinsic
2071
     that possesses a matching interface.  14.1.2.4  */
2072
  if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2073
    {
2074
      gfc_error ("There is no specific function for the generic '%s' at %L",
2075
                 expr->symtree->n.sym->name, &expr->where);
2076
      return FAILURE;
2077
    }
2078
 
2079
  m = gfc_intrinsic_func_interface (expr, 0);
2080
  if (m == MATCH_YES)
2081
    return SUCCESS;
2082
  if (m == MATCH_NO)
2083
    gfc_error ("Generic function '%s' at %L is not consistent with a "
2084
               "specific intrinsic interface", expr->symtree->n.sym->name,
2085
               &expr->where);
2086
 
2087
  return FAILURE;
2088
}
2089
 
2090
 
2091
/* Resolve a function call known to be specific.  */
2092
 
2093
static match
2094
resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2095
{
2096
  match m;
2097
 
2098
  if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2099
    {
2100
      if (sym->attr.dummy)
2101
        {
2102
          sym->attr.proc = PROC_DUMMY;
2103
          goto found;
2104
        }
2105
 
2106
      sym->attr.proc = PROC_EXTERNAL;
2107
      goto found;
2108
    }
2109
 
2110
  if (sym->attr.proc == PROC_MODULE
2111
      || sym->attr.proc == PROC_ST_FUNCTION
2112
      || sym->attr.proc == PROC_INTERNAL)
2113
    goto found;
2114
 
2115
  if (sym->attr.intrinsic)
2116
    {
2117
      m = gfc_intrinsic_func_interface (expr, 1);
2118
      if (m == MATCH_YES)
2119
        return MATCH_YES;
2120
      if (m == MATCH_NO)
2121
        gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2122
                   "with an intrinsic", sym->name, &expr->where);
2123
 
2124
      return MATCH_ERROR;
2125
    }
2126
 
2127
  return MATCH_NO;
2128
 
2129
found:
2130
  gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2131
 
2132
  if (sym->result)
2133
    expr->ts = sym->result->ts;
2134
  else
2135
    expr->ts = sym->ts;
2136
  expr->value.function.name = sym->name;
2137
  expr->value.function.esym = sym;
2138
  if (sym->as != NULL)
2139
    expr->rank = sym->as->rank;
2140
 
2141
  return MATCH_YES;
2142
}
2143
 
2144
 
2145
static gfc_try
2146
resolve_specific_f (gfc_expr *expr)
2147
{
2148
  gfc_symbol *sym;
2149
  match m;
2150
 
2151
  sym = expr->symtree->n.sym;
2152
 
2153
  for (;;)
2154
    {
2155
      m = resolve_specific_f0 (sym, expr);
2156
      if (m == MATCH_YES)
2157
        return SUCCESS;
2158
      if (m == MATCH_ERROR)
2159
        return FAILURE;
2160
 
2161
      if (sym->ns->parent == NULL)
2162
        break;
2163
 
2164
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2165
 
2166
      if (sym == NULL)
2167
        break;
2168
    }
2169
 
2170
  gfc_error ("Unable to resolve the specific function '%s' at %L",
2171
             expr->symtree->n.sym->name, &expr->where);
2172
 
2173
  return SUCCESS;
2174
}
2175
 
2176
 
2177
/* Resolve a procedure call not known to be generic nor specific.  */
2178
 
2179
static gfc_try
2180
resolve_unknown_f (gfc_expr *expr)
2181
{
2182
  gfc_symbol *sym;
2183
  gfc_typespec *ts;
2184
 
2185
  sym = expr->symtree->n.sym;
2186
 
2187
  if (sym->attr.dummy)
2188
    {
2189
      sym->attr.proc = PROC_DUMMY;
2190
      expr->value.function.name = sym->name;
2191
      goto set_type;
2192
    }
2193
 
2194
  /* See if we have an intrinsic function reference.  */
2195
 
2196
  if (gfc_is_intrinsic (sym, 0, expr->where))
2197
    {
2198
      if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2199
        return SUCCESS;
2200
      return FAILURE;
2201
    }
2202
 
2203
  /* The reference is to an external name.  */
2204
 
2205
  sym->attr.proc = PROC_EXTERNAL;
2206
  expr->value.function.name = sym->name;
2207
  expr->value.function.esym = expr->symtree->n.sym;
2208
 
2209
  if (sym->as != NULL)
2210
    expr->rank = sym->as->rank;
2211
 
2212
  /* Type of the expression is either the type of the symbol or the
2213
     default type of the symbol.  */
2214
 
2215
set_type:
2216
  gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2217
 
2218
  if (sym->ts.type != BT_UNKNOWN)
2219
    expr->ts = sym->ts;
2220
  else
2221
    {
2222
      ts = gfc_get_default_type (sym->name, sym->ns);
2223
 
2224
      if (ts->type == BT_UNKNOWN)
2225
        {
2226
          gfc_error ("Function '%s' at %L has no IMPLICIT type",
2227
                     sym->name, &expr->where);
2228
          return FAILURE;
2229
        }
2230
      else
2231
        expr->ts = *ts;
2232
    }
2233
 
2234
  return SUCCESS;
2235
}
2236
 
2237
 
2238
/* Return true, if the symbol is an external procedure.  */
2239
static bool
2240
is_external_proc (gfc_symbol *sym)
2241
{
2242
  if (!sym->attr.dummy && !sym->attr.contained
2243
        && !(sym->attr.intrinsic
2244
              || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2245
        && sym->attr.proc != PROC_ST_FUNCTION
2246
        && !sym->attr.use_assoc
2247
        && sym->name)
2248
    return true;
2249
 
2250
  return false;
2251
}
2252
 
2253
 
2254
/* Figure out if a function reference is pure or not.  Also set the name
2255
   of the function for a potential error message.  Return nonzero if the
2256
   function is PURE, zero if not.  */
2257
static int
2258
pure_stmt_function (gfc_expr *, gfc_symbol *);
2259
 
2260
static int
2261
pure_function (gfc_expr *e, const char **name)
2262
{
2263
  int pure;
2264
 
2265
  *name = NULL;
2266
 
2267
  if (e->symtree != NULL
2268
        && e->symtree->n.sym != NULL
2269
        && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2270
    return pure_stmt_function (e, e->symtree->n.sym);
2271
 
2272
  if (e->value.function.esym)
2273
    {
2274
      pure = gfc_pure (e->value.function.esym);
2275
      *name = e->value.function.esym->name;
2276
    }
2277
  else if (e->value.function.isym)
2278
    {
2279
      pure = e->value.function.isym->pure
2280
             || e->value.function.isym->elemental;
2281
      *name = e->value.function.isym->name;
2282
    }
2283
  else
2284
    {
2285
      /* Implicit functions are not pure.  */
2286
      pure = 0;
2287
      *name = e->value.function.name;
2288
    }
2289
 
2290
  return pure;
2291
}
2292
 
2293
 
2294
static bool
2295
impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2296
                 int *f ATTRIBUTE_UNUSED)
2297
{
2298
  const char *name;
2299
 
2300
  /* Don't bother recursing into other statement functions
2301
     since they will be checked individually for purity.  */
2302
  if (e->expr_type != EXPR_FUNCTION
2303
        || !e->symtree
2304
        || e->symtree->n.sym == sym
2305
        || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2306
    return false;
2307
 
2308
  return pure_function (e, &name) ? false : true;
2309
}
2310
 
2311
 
2312
static int
2313
pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2314
{
2315
  return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2316
}
2317
 
2318
 
2319
static gfc_try
2320
is_scalar_expr_ptr (gfc_expr *expr)
2321
{
2322
  gfc_try retval = SUCCESS;
2323
  gfc_ref *ref;
2324
  int start;
2325
  int end;
2326
 
2327
  /* See if we have a gfc_ref, which means we have a substring, array
2328
     reference, or a component.  */
2329
  if (expr->ref != NULL)
2330
    {
2331
      ref = expr->ref;
2332
      while (ref->next != NULL)
2333
        ref = ref->next;
2334
 
2335
      switch (ref->type)
2336
        {
2337
        case REF_SUBSTRING:
2338
          if (ref->u.ss.length != NULL
2339
              && ref->u.ss.length->length != NULL
2340
              && ref->u.ss.start
2341
              && ref->u.ss.start->expr_type == EXPR_CONSTANT
2342
              && ref->u.ss.end
2343
              && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2344
            {
2345
              start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2346
              end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2347
              if (end - start + 1 != 1)
2348
                retval = FAILURE;
2349
            }
2350
          else
2351
            retval = FAILURE;
2352
          break;
2353
        case REF_ARRAY:
2354
          if (ref->u.ar.type == AR_ELEMENT)
2355
            retval = SUCCESS;
2356
          else if (ref->u.ar.type == AR_FULL)
2357
            {
2358
              /* The user can give a full array if the array is of size 1.  */
2359
              if (ref->u.ar.as != NULL
2360
                  && ref->u.ar.as->rank == 1
2361
                  && ref->u.ar.as->type == AS_EXPLICIT
2362
                  && ref->u.ar.as->lower[0] != NULL
2363
                  && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2364
                  && ref->u.ar.as->upper[0] != NULL
2365
                  && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2366
                {
2367
                  /* If we have a character string, we need to check if
2368
                     its length is one.  */
2369
                  if (expr->ts.type == BT_CHARACTER)
2370
                    {
2371
                      if (expr->ts.u.cl == NULL
2372
                          || expr->ts.u.cl->length == NULL
2373
                          || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2374
                          != 0)
2375
                        retval = FAILURE;
2376
                    }
2377
                  else
2378
                    {
2379
                      /* We have constant lower and upper bounds.  If the
2380
                         difference between is 1, it can be considered a
2381
                         scalar.  */
2382
                      start = (int) mpz_get_si
2383
                                (ref->u.ar.as->lower[0]->value.integer);
2384
                      end = (int) mpz_get_si
2385
                                (ref->u.ar.as->upper[0]->value.integer);
2386
                      if (end - start + 1 != 1)
2387
                        retval = FAILURE;
2388
                   }
2389
                }
2390
              else
2391
                retval = FAILURE;
2392
            }
2393
          else
2394
            retval = FAILURE;
2395
          break;
2396
        default:
2397
          retval = SUCCESS;
2398
          break;
2399
        }
2400
    }
2401
  else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2402
    {
2403
      /* Character string.  Make sure it's of length 1.  */
2404
      if (expr->ts.u.cl == NULL
2405
          || expr->ts.u.cl->length == NULL
2406
          || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2407
        retval = FAILURE;
2408
    }
2409
  else if (expr->rank != 0)
2410
    retval = FAILURE;
2411
 
2412
  return retval;
2413
}
2414
 
2415
 
2416
/* Match one of the iso_c_binding functions (c_associated or c_loc)
2417
   and, in the case of c_associated, set the binding label based on
2418
   the arguments.  */
2419
 
2420
static gfc_try
2421
gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2422
                          gfc_symbol **new_sym)
2423
{
2424
  char name[GFC_MAX_SYMBOL_LEN + 1];
2425
  char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2426
  int optional_arg = 0, is_pointer = 0;
2427
  gfc_try retval = SUCCESS;
2428
  gfc_symbol *args_sym;
2429
  gfc_typespec *arg_ts;
2430
 
2431
  if (args->expr->expr_type == EXPR_CONSTANT
2432
      || args->expr->expr_type == EXPR_OP
2433
      || args->expr->expr_type == EXPR_NULL)
2434
    {
2435
      gfc_error ("Argument to '%s' at %L is not a variable",
2436
                 sym->name, &(args->expr->where));
2437
      return FAILURE;
2438
    }
2439
 
2440
  args_sym = args->expr->symtree->n.sym;
2441
 
2442
  /* The typespec for the actual arg should be that stored in the expr
2443
     and not necessarily that of the expr symbol (args_sym), because
2444
     the actual expression could be a part-ref of the expr symbol.  */
2445
  arg_ts = &(args->expr->ts);
2446
 
2447
  is_pointer = gfc_is_data_pointer (args->expr);
2448
 
2449
  if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2450
    {
2451
      /* If the user gave two args then they are providing something for
2452
         the optional arg (the second cptr).  Therefore, set the name and
2453
         binding label to the c_associated for two cptrs.  Otherwise,
2454
         set c_associated to expect one cptr.  */
2455
      if (args->next)
2456
        {
2457
          /* two args.  */
2458
          sprintf (name, "%s_2", sym->name);
2459
          sprintf (binding_label, "%s_2", sym->binding_label);
2460
          optional_arg = 1;
2461
        }
2462
      else
2463
        {
2464
          /* one arg.  */
2465
          sprintf (name, "%s_1", sym->name);
2466
          sprintf (binding_label, "%s_1", sym->binding_label);
2467
          optional_arg = 0;
2468
        }
2469
 
2470
      /* Get a new symbol for the version of c_associated that
2471
         will get called.  */
2472
      *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2473
    }
2474
  else if (sym->intmod_sym_id == ISOCBINDING_LOC
2475
           || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2476
    {
2477
      sprintf (name, "%s", sym->name);
2478
      sprintf (binding_label, "%s", sym->binding_label);
2479
 
2480
      /* Error check the call.  */
2481
      if (args->next != NULL)
2482
        {
2483
          gfc_error_now ("More actual than formal arguments in '%s' "
2484
                         "call at %L", name, &(args->expr->where));
2485
          retval = FAILURE;
2486
        }
2487
      else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2488
        {
2489
          /* Make sure we have either the target or pointer attribute.  */
2490
          if (!args_sym->attr.target && !is_pointer)
2491
            {
2492
              gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2493
                             "a TARGET or an associated pointer",
2494
                             args_sym->name,
2495
                             sym->name, &(args->expr->where));
2496
              retval = FAILURE;
2497
            }
2498
 
2499
          /* See if we have interoperable type and type param.  */
2500
          if (verify_c_interop (arg_ts) == SUCCESS
2501
              || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2502
            {
2503
              if (args_sym->attr.target == 1)
2504
                {
2505
                  /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2506
                     has the target attribute and is interoperable.  */
2507
                  /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2508
                     allocatable variable that has the TARGET attribute and
2509
                     is not an array of zero size.  */
2510
                  if (args_sym->attr.allocatable == 1)
2511
                    {
2512
                      if (args_sym->attr.dimension != 0
2513
                          && (args_sym->as && args_sym->as->rank == 0))
2514
                        {
2515
                          gfc_error_now ("Allocatable variable '%s' used as a "
2516
                                         "parameter to '%s' at %L must not be "
2517
                                         "an array of zero size",
2518
                                         args_sym->name, sym->name,
2519
                                         &(args->expr->where));
2520
                          retval = FAILURE;
2521
                        }
2522
                    }
2523
                  else
2524
                    {
2525
                      /* A non-allocatable target variable with C
2526
                         interoperable type and type parameters must be
2527
                         interoperable.  */
2528
                      if (args_sym && args_sym->attr.dimension)
2529
                        {
2530
                          if (args_sym->as->type == AS_ASSUMED_SHAPE)
2531
                            {
2532
                              gfc_error ("Assumed-shape array '%s' at %L "
2533
                                         "cannot be an argument to the "
2534
                                         "procedure '%s' because "
2535
                                         "it is not C interoperable",
2536
                                         args_sym->name,
2537
                                         &(args->expr->where), sym->name);
2538
                              retval = FAILURE;
2539
                            }
2540
                          else if (args_sym->as->type == AS_DEFERRED)
2541
                            {
2542
                              gfc_error ("Deferred-shape array '%s' at %L "
2543
                                         "cannot be an argument to the "
2544
                                         "procedure '%s' because "
2545
                                         "it is not C interoperable",
2546
                                         args_sym->name,
2547
                                         &(args->expr->where), sym->name);
2548
                              retval = FAILURE;
2549
                            }
2550
                        }
2551
 
2552
                      /* Make sure it's not a character string.  Arrays of
2553
                         any type should be ok if the variable is of a C
2554
                         interoperable type.  */
2555
                      if (arg_ts->type == BT_CHARACTER)
2556
                        if (arg_ts->u.cl != NULL
2557
                            && (arg_ts->u.cl->length == NULL
2558
                                || arg_ts->u.cl->length->expr_type
2559
                                   != EXPR_CONSTANT
2560
                                || mpz_cmp_si
2561
                                    (arg_ts->u.cl->length->value.integer, 1)
2562
                                   != 0)
2563
                            && is_scalar_expr_ptr (args->expr) != SUCCESS)
2564
                          {
2565
                            gfc_error_now ("CHARACTER argument '%s' to '%s' "
2566
                                           "at %L must have a length of 1",
2567
                                           args_sym->name, sym->name,
2568
                                           &(args->expr->where));
2569
                            retval = FAILURE;
2570
                          }
2571
                    }
2572
                }
2573
              else if (is_pointer
2574
                       && is_scalar_expr_ptr (args->expr) != SUCCESS)
2575
                {
2576
                  /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2577
                     scalar pointer.  */
2578
                  gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2579
                                 "associated scalar POINTER", args_sym->name,
2580
                                 sym->name, &(args->expr->where));
2581
                  retval = FAILURE;
2582
                }
2583
            }
2584
          else
2585
            {
2586
              /* The parameter is not required to be C interoperable.  If it
2587
                 is not C interoperable, it must be a nonpolymorphic scalar
2588
                 with no length type parameters.  It still must have either
2589
                 the pointer or target attribute, and it can be
2590
                 allocatable (but must be allocated when c_loc is called).  */
2591
              if (args->expr->rank != 0
2592
                  && is_scalar_expr_ptr (args->expr) != SUCCESS)
2593
                {
2594
                  gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2595
                                 "scalar", args_sym->name, sym->name,
2596
                                 &(args->expr->where));
2597
                  retval = FAILURE;
2598
                }
2599
              else if (arg_ts->type == BT_CHARACTER
2600
                       && is_scalar_expr_ptr (args->expr) != SUCCESS)
2601
                {
2602
                  gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2603
                                 "%L must have a length of 1",
2604
                                 args_sym->name, sym->name,
2605
                                 &(args->expr->where));
2606
                  retval = FAILURE;
2607
                }
2608
            }
2609
        }
2610
      else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2611
        {
2612
          if (args_sym->attr.flavor != FL_PROCEDURE)
2613
            {
2614
              /* TODO: Update this error message to allow for procedure
2615
                 pointers once they are implemented.  */
2616
              gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2617
                             "procedure",
2618
                             args_sym->name, sym->name,
2619
                             &(args->expr->where));
2620
              retval = FAILURE;
2621
            }
2622
          else if (args_sym->attr.is_bind_c != 1)
2623
            {
2624
              gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2625
                             "BIND(C)",
2626
                             args_sym->name, sym->name,
2627
                             &(args->expr->where));
2628
              retval = FAILURE;
2629
            }
2630
        }
2631
 
2632
      /* for c_loc/c_funloc, the new symbol is the same as the old one */
2633
      *new_sym = sym;
2634
    }
2635
  else
2636
    {
2637
      gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2638
                          "iso_c_binding function: '%s'!\n", sym->name);
2639
    }
2640
 
2641
  return retval;
2642
}
2643
 
2644
 
2645
/* Resolve a function call, which means resolving the arguments, then figuring
2646
   out which entity the name refers to.  */
2647
/* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2648
   to INTENT(OUT) or INTENT(INOUT).  */
2649
 
2650
static gfc_try
2651
resolve_function (gfc_expr *expr)
2652
{
2653
  gfc_actual_arglist *arg;
2654
  gfc_symbol *sym;
2655
  const char *name;
2656
  gfc_try t;
2657
  int temp;
2658
  procedure_type p = PROC_INTRINSIC;
2659
  bool no_formal_args;
2660
 
2661
  sym = NULL;
2662
  if (expr->symtree)
2663
    sym = expr->symtree->n.sym;
2664
 
2665
  /* If this is a procedure pointer component, it has already been resolved.  */
2666
  if (gfc_is_proc_ptr_comp (expr, NULL))
2667
    return SUCCESS;
2668
 
2669
  if (sym && sym->attr.intrinsic
2670
      && resolve_intrinsic (sym, &expr->where) == FAILURE)
2671
    return FAILURE;
2672
 
2673
  if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2674
    {
2675
      gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2676
      return FAILURE;
2677
    }
2678
 
2679
  /* If this ia a deferred TBP with an abstract interface (which may
2680
     of course be referenced), expr->value.function.esym will be set.  */
2681
  if (sym && sym->attr.abstract && !expr->value.function.esym)
2682
    {
2683
      gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2684
                 sym->name, &expr->where);
2685
      return FAILURE;
2686
    }
2687
 
2688
  /* Switch off assumed size checking and do this again for certain kinds
2689
     of procedure, once the procedure itself is resolved.  */
2690
  need_full_assumed_size++;
2691
 
2692
  if (expr->symtree && expr->symtree->n.sym)
2693
    p = expr->symtree->n.sym->attr.proc;
2694
 
2695
  no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2696
  if (resolve_actual_arglist (expr->value.function.actual,
2697
                              p, no_formal_args) == FAILURE)
2698
      return FAILURE;
2699
 
2700
  /* Need to setup the call to the correct c_associated, depending on
2701
     the number of cptrs to user gives to compare.  */
2702
  if (sym && sym->attr.is_iso_c == 1)
2703
    {
2704
      if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2705
          == FAILURE)
2706
        return FAILURE;
2707
 
2708
      /* Get the symtree for the new symbol (resolved func).
2709
         the old one will be freed later, when it's no longer used.  */
2710
      gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2711
    }
2712
 
2713
  /* Resume assumed_size checking.  */
2714
  need_full_assumed_size--;
2715
 
2716
  /* If the procedure is external, check for usage.  */
2717
  if (sym && is_external_proc (sym))
2718
    resolve_global_procedure (sym, &expr->where,
2719
                              &expr->value.function.actual, 0);
2720
 
2721
  if (sym && sym->ts.type == BT_CHARACTER
2722
      && sym->ts.u.cl
2723
      && sym->ts.u.cl->length == NULL
2724
      && !sym->attr.dummy
2725
      && expr->value.function.esym == NULL
2726
      && !sym->attr.contained)
2727
    {
2728
      /* Internal procedures are taken care of in resolve_contained_fntype.  */
2729
      gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2730
                 "be used at %L since it is not a dummy argument",
2731
                 sym->name, &expr->where);
2732
      return FAILURE;
2733
    }
2734
 
2735
  /* See if function is already resolved.  */
2736
 
2737
  if (expr->value.function.name != NULL)
2738
    {
2739
      if (expr->ts.type == BT_UNKNOWN)
2740
        expr->ts = sym->ts;
2741
      t = SUCCESS;
2742
    }
2743
  else
2744
    {
2745
      /* Apply the rules of section 14.1.2.  */
2746
 
2747
      switch (procedure_kind (sym))
2748
        {
2749
        case PTYPE_GENERIC:
2750
          t = resolve_generic_f (expr);
2751
          break;
2752
 
2753
        case PTYPE_SPECIFIC:
2754
          t = resolve_specific_f (expr);
2755
          break;
2756
 
2757
        case PTYPE_UNKNOWN:
2758
          t = resolve_unknown_f (expr);
2759
          break;
2760
 
2761
        default:
2762
          gfc_internal_error ("resolve_function(): bad function type");
2763
        }
2764
    }
2765
 
2766
  /* If the expression is still a function (it might have simplified),
2767
     then we check to see if we are calling an elemental function.  */
2768
 
2769
  if (expr->expr_type != EXPR_FUNCTION)
2770
    return t;
2771
 
2772
  temp = need_full_assumed_size;
2773
  need_full_assumed_size = 0;
2774
 
2775
  if (resolve_elemental_actual (expr, NULL) == FAILURE)
2776
    return FAILURE;
2777
 
2778
  if (omp_workshare_flag
2779
      && expr->value.function.esym
2780
      && ! gfc_elemental (expr->value.function.esym))
2781
    {
2782
      gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2783
                 "in WORKSHARE construct", expr->value.function.esym->name,
2784
                 &expr->where);
2785
      t = FAILURE;
2786
    }
2787
 
2788
#define GENERIC_ID expr->value.function.isym->id
2789
  else if (expr->value.function.actual != NULL
2790
           && expr->value.function.isym != NULL
2791
           && GENERIC_ID != GFC_ISYM_LBOUND
2792
           && GENERIC_ID != GFC_ISYM_LEN
2793
           && GENERIC_ID != GFC_ISYM_LOC
2794
           && GENERIC_ID != GFC_ISYM_PRESENT)
2795
    {
2796
      /* Array intrinsics must also have the last upper bound of an
2797
         assumed size array argument.  UBOUND and SIZE have to be
2798
         excluded from the check if the second argument is anything
2799
         than a constant.  */
2800
 
2801
      for (arg = expr->value.function.actual; arg; arg = arg->next)
2802
        {
2803
          if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2804
              && arg->next != NULL && arg->next->expr)
2805
            {
2806
              if (arg->next->expr->expr_type != EXPR_CONSTANT)
2807
                break;
2808
 
2809
              if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2810
                break;
2811
 
2812
              if ((int)mpz_get_si (arg->next->expr->value.integer)
2813
                        < arg->expr->rank)
2814
                break;
2815
            }
2816
 
2817
          if (arg->expr != NULL
2818
              && arg->expr->rank > 0
2819
              && resolve_assumed_size_actual (arg->expr))
2820
            return FAILURE;
2821
        }
2822
    }
2823
#undef GENERIC_ID
2824
 
2825
  need_full_assumed_size = temp;
2826
  name = NULL;
2827
 
2828
  if (!pure_function (expr, &name) && name)
2829
    {
2830
      if (forall_flag)
2831
        {
2832
          gfc_error ("reference to non-PURE function '%s' at %L inside a "
2833
                     "FORALL %s", name, &expr->where,
2834
                     forall_flag == 2 ? "mask" : "block");
2835
          t = FAILURE;
2836
        }
2837
      else if (gfc_pure (NULL))
2838
        {
2839
          gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2840
                     "procedure within a PURE procedure", name, &expr->where);
2841
          t = FAILURE;
2842
        }
2843
    }
2844
 
2845
  /* Functions without the RECURSIVE attribution are not allowed to
2846
   * call themselves.  */
2847
  if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2848
    {
2849
      gfc_symbol *esym;
2850
      esym = expr->value.function.esym;
2851
 
2852
      if (is_illegal_recursion (esym, gfc_current_ns))
2853
      {
2854
        if (esym->attr.entry && esym->ns->entries)
2855
          gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2856
                     " function '%s' is not RECURSIVE",
2857
                     esym->name, &expr->where, esym->ns->entries->sym->name);
2858
        else
2859
          gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2860
                     " is not RECURSIVE", esym->name, &expr->where);
2861
 
2862
        t = FAILURE;
2863
      }
2864
    }
2865
 
2866
  /* Character lengths of use associated functions may contains references to
2867
     symbols not referenced from the current program unit otherwise.  Make sure
2868
     those symbols are marked as referenced.  */
2869
 
2870
  if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2871
      && expr->value.function.esym->attr.use_assoc)
2872
    {
2873
      gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2874
    }
2875
 
2876
  if (t == SUCCESS
2877
        && !((expr->value.function.esym
2878
                && expr->value.function.esym->attr.elemental)
2879
                        ||
2880
             (expr->value.function.isym
2881
                && expr->value.function.isym->elemental)))
2882
    find_noncopying_intrinsics (expr->value.function.esym,
2883
                                expr->value.function.actual);
2884
 
2885
  /* Make sure that the expression has a typespec that works.  */
2886
  if (expr->ts.type == BT_UNKNOWN)
2887
    {
2888
      if (expr->symtree->n.sym->result
2889
            && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2890
            && !expr->symtree->n.sym->result->attr.proc_pointer)
2891
        expr->ts = expr->symtree->n.sym->result->ts;
2892
    }
2893
 
2894
  return t;
2895
}
2896
 
2897
 
2898
/************* Subroutine resolution *************/
2899
 
2900
static void
2901
pure_subroutine (gfc_code *c, gfc_symbol *sym)
2902
{
2903
  if (gfc_pure (sym))
2904
    return;
2905
 
2906
  if (forall_flag)
2907
    gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2908
               sym->name, &c->loc);
2909
  else if (gfc_pure (NULL))
2910
    gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2911
               &c->loc);
2912
}
2913
 
2914
 
2915
static match
2916
resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2917
{
2918
  gfc_symbol *s;
2919
 
2920
  if (sym->attr.generic)
2921
    {
2922
      s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2923
      if (s != NULL)
2924
        {
2925
          c->resolved_sym = s;
2926
          pure_subroutine (c, s);
2927
          return MATCH_YES;
2928
        }
2929
 
2930
      /* TODO: Need to search for elemental references in generic interface.  */
2931
    }
2932
 
2933
  if (sym->attr.intrinsic)
2934
    return gfc_intrinsic_sub_interface (c, 0);
2935
 
2936
  return MATCH_NO;
2937
}
2938
 
2939
 
2940
static gfc_try
2941
resolve_generic_s (gfc_code *c)
2942
{
2943
  gfc_symbol *sym;
2944
  match m;
2945
 
2946
  sym = c->symtree->n.sym;
2947
 
2948
  for (;;)
2949
    {
2950
      m = resolve_generic_s0 (c, sym);
2951
      if (m == MATCH_YES)
2952
        return SUCCESS;
2953
      else if (m == MATCH_ERROR)
2954
        return FAILURE;
2955
 
2956
generic:
2957
      if (sym->ns->parent == NULL)
2958
        break;
2959
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2960
 
2961
      if (sym == NULL)
2962
        break;
2963
      if (!generic_sym (sym))
2964
        goto generic;
2965
    }
2966
 
2967
  /* Last ditch attempt.  See if the reference is to an intrinsic
2968
     that possesses a matching interface.  14.1.2.4  */
2969
  sym = c->symtree->n.sym;
2970
 
2971
  if (!gfc_is_intrinsic (sym, 1, c->loc))
2972
    {
2973
      gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2974
                 sym->name, &c->loc);
2975
      return FAILURE;
2976
    }
2977
 
2978
  m = gfc_intrinsic_sub_interface (c, 0);
2979
  if (m == MATCH_YES)
2980
    return SUCCESS;
2981
  if (m == MATCH_NO)
2982
    gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2983
               "intrinsic subroutine interface", sym->name, &c->loc);
2984
 
2985
  return FAILURE;
2986
}
2987
 
2988
 
2989
/* Set the name and binding label of the subroutine symbol in the call
2990
   expression represented by 'c' to include the type and kind of the
2991
   second parameter.  This function is for resolving the appropriate
2992
   version of c_f_pointer() and c_f_procpointer().  For example, a
2993
   call to c_f_pointer() for a default integer pointer could have a
2994
   name of c_f_pointer_i4.  If no second arg exists, which is an error
2995
   for these two functions, it defaults to the generic symbol's name
2996
   and binding label.  */
2997
 
2998
static void
2999
set_name_and_label (gfc_code *c, gfc_symbol *sym,
3000
                    char *name, char *binding_label)
3001
{
3002
  gfc_expr *arg = NULL;
3003
  char type;
3004
  int kind;
3005
 
3006
  /* The second arg of c_f_pointer and c_f_procpointer determines
3007
     the type and kind for the procedure name.  */
3008
  arg = c->ext.actual->next->expr;
3009
 
3010
  if (arg != NULL)
3011
    {
3012
      /* Set up the name to have the given symbol's name,
3013
         plus the type and kind.  */
3014
      /* a derived type is marked with the type letter 'u' */
3015
      if (arg->ts.type == BT_DERIVED)
3016
        {
3017
          type = 'd';
3018
          kind = 0; /* set the kind as 0 for now */
3019
        }
3020
      else
3021
        {
3022
          type = gfc_type_letter (arg->ts.type);
3023
          kind = arg->ts.kind;
3024
        }
3025
 
3026
      if (arg->ts.type == BT_CHARACTER)
3027
        /* Kind info for character strings not needed.  */
3028
        kind = 0;
3029
 
3030
      sprintf (name, "%s_%c%d", sym->name, type, kind);
3031
      /* Set up the binding label as the given symbol's label plus
3032
         the type and kind.  */
3033
      sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3034
    }
3035
  else
3036
    {
3037
      /* If the second arg is missing, set the name and label as
3038
         was, cause it should at least be found, and the missing
3039
         arg error will be caught by compare_parameters().  */
3040
      sprintf (name, "%s", sym->name);
3041
      sprintf (binding_label, "%s", sym->binding_label);
3042
    }
3043
 
3044
  return;
3045
}
3046
 
3047
 
3048
/* Resolve a generic version of the iso_c_binding procedure given
3049
   (sym) to the specific one based on the type and kind of the
3050
   argument(s).  Currently, this function resolves c_f_pointer() and
3051
   c_f_procpointer based on the type and kind of the second argument
3052
   (FPTR).  Other iso_c_binding procedures aren't specially handled.
3053
   Upon successfully exiting, c->resolved_sym will hold the resolved
3054
   symbol.  Returns MATCH_ERROR if an error occurred; MATCH_YES
3055
   otherwise.  */
3056
 
3057
match
3058
gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3059
{
3060
  gfc_symbol *new_sym;
3061
  /* this is fine, since we know the names won't use the max */
3062
  char name[GFC_MAX_SYMBOL_LEN + 1];
3063
  char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3064
  /* default to success; will override if find error */
3065
  match m = MATCH_YES;
3066
 
3067
  /* Make sure the actual arguments are in the necessary order (based on the
3068
     formal args) before resolving.  */
3069
  gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3070
 
3071
  if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3072
      (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3073
    {
3074
      set_name_and_label (c, sym, name, binding_label);
3075
 
3076
      if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3077
        {
3078
          if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3079
            {
3080
              /* Make sure we got a third arg if the second arg has non-zero
3081
                 rank.  We must also check that the type and rank are
3082
                 correct since we short-circuit this check in
3083
                 gfc_procedure_use() (called above to sort actual args).  */
3084
              if (c->ext.actual->next->expr->rank != 0)
3085
                {
3086
                  if(c->ext.actual->next->next == NULL
3087
                     || c->ext.actual->next->next->expr == NULL)
3088
                    {
3089
                      m = MATCH_ERROR;
3090
                      gfc_error ("Missing SHAPE parameter for call to %s "
3091
                                 "at %L", sym->name, &(c->loc));
3092
                    }
3093
                  else if (c->ext.actual->next->next->expr->ts.type
3094
                           != BT_INTEGER
3095
                           || c->ext.actual->next->next->expr->rank != 1)
3096
                    {
3097
                      m = MATCH_ERROR;
3098
                      gfc_error ("SHAPE parameter for call to %s at %L must "
3099
                                 "be a rank 1 INTEGER array", sym->name,
3100
                                 &(c->loc));
3101
                    }
3102
                }
3103
            }
3104
        }
3105
 
3106
      if (m != MATCH_ERROR)
3107
        {
3108
          /* the 1 means to add the optional arg to formal list */
3109
          new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3110
 
3111
          /* for error reporting, say it's declared where the original was */
3112
          new_sym->declared_at = sym->declared_at;
3113
        }
3114
    }
3115
  else
3116
    {
3117
      /* no differences for c_loc or c_funloc */
3118
      new_sym = sym;
3119
    }
3120
 
3121
  /* set the resolved symbol */
3122
  if (m != MATCH_ERROR)
3123
    c->resolved_sym = new_sym;
3124
  else
3125
    c->resolved_sym = sym;
3126
 
3127
  return m;
3128
}
3129
 
3130
 
3131
/* Resolve a subroutine call known to be specific.  */
3132
 
3133
static match
3134
resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3135
{
3136
  match m;
3137
 
3138
  if(sym->attr.is_iso_c)
3139
    {
3140
      m = gfc_iso_c_sub_interface (c,sym);
3141
      return m;
3142
    }
3143
 
3144
  if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3145
    {
3146
      if (sym->attr.dummy)
3147
        {
3148
          sym->attr.proc = PROC_DUMMY;
3149
          goto found;
3150
        }
3151
 
3152
      sym->attr.proc = PROC_EXTERNAL;
3153
      goto found;
3154
    }
3155
 
3156
  if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3157
    goto found;
3158
 
3159
  if (sym->attr.intrinsic)
3160
    {
3161
      m = gfc_intrinsic_sub_interface (c, 1);
3162
      if (m == MATCH_YES)
3163
        return MATCH_YES;
3164
      if (m == MATCH_NO)
3165
        gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3166
                   "with an intrinsic", sym->name, &c->loc);
3167
 
3168
      return MATCH_ERROR;
3169
    }
3170
 
3171
  return MATCH_NO;
3172
 
3173
found:
3174
  gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3175
 
3176
  c->resolved_sym = sym;
3177
  pure_subroutine (c, sym);
3178
 
3179
  return MATCH_YES;
3180
}
3181
 
3182
 
3183
static gfc_try
3184
resolve_specific_s (gfc_code *c)
3185
{
3186
  gfc_symbol *sym;
3187
  match m;
3188
 
3189
  sym = c->symtree->n.sym;
3190
 
3191
  for (;;)
3192
    {
3193
      m = resolve_specific_s0 (c, sym);
3194
      if (m == MATCH_YES)
3195
        return SUCCESS;
3196
      if (m == MATCH_ERROR)
3197
        return FAILURE;
3198
 
3199
      if (sym->ns->parent == NULL)
3200
        break;
3201
 
3202
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3203
 
3204
      if (sym == NULL)
3205
        break;
3206
    }
3207
 
3208
  sym = c->symtree->n.sym;
3209
  gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3210
             sym->name, &c->loc);
3211
 
3212
  return FAILURE;
3213
}
3214
 
3215
 
3216
/* Resolve a subroutine call not known to be generic nor specific.  */
3217
 
3218
static gfc_try
3219
resolve_unknown_s (gfc_code *c)
3220
{
3221
  gfc_symbol *sym;
3222
 
3223
  sym = c->symtree->n.sym;
3224
 
3225
  if (sym->attr.dummy)
3226
    {
3227
      sym->attr.proc = PROC_DUMMY;
3228
      goto found;
3229
    }
3230
 
3231
  /* See if we have an intrinsic function reference.  */
3232
 
3233
  if (gfc_is_intrinsic (sym, 1, c->loc))
3234
    {
3235
      if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3236
        return SUCCESS;
3237
      return FAILURE;
3238
    }
3239
 
3240
  /* The reference is to an external name.  */
3241
 
3242
found:
3243
  gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3244
 
3245
  c->resolved_sym = sym;
3246
 
3247
  pure_subroutine (c, sym);
3248
 
3249
  return SUCCESS;
3250
}
3251
 
3252
 
3253
/* Resolve a subroutine call.  Although it was tempting to use the same code
3254
   for functions, subroutines and functions are stored differently and this
3255
   makes things awkward.  */
3256
 
3257
static gfc_try
3258
resolve_call (gfc_code *c)
3259
{
3260
  gfc_try t;
3261
  procedure_type ptype = PROC_INTRINSIC;
3262
  gfc_symbol *csym, *sym;
3263
  bool no_formal_args;
3264
 
3265
  csym = c->symtree ? c->symtree->n.sym : NULL;
3266
 
3267
  if (csym && csym->ts.type != BT_UNKNOWN)
3268
    {
3269
      gfc_error ("'%s' at %L has a type, which is not consistent with "
3270
                 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3271
      return FAILURE;
3272
    }
3273
 
3274
  if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3275
    {
3276
      gfc_symtree *st;
3277
      gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3278
      sym = st ? st->n.sym : NULL;
3279
      if (sym && csym != sym
3280
              && sym->ns == gfc_current_ns
3281
              && sym->attr.flavor == FL_PROCEDURE
3282
              && sym->attr.contained)
3283
        {
3284
          sym->refs++;
3285
          if (csym->attr.generic)
3286
            c->symtree->n.sym = sym;
3287
          else
3288
            c->symtree = st;
3289
          csym = c->symtree->n.sym;
3290
        }
3291
    }
3292
 
3293
  /* If this ia a deferred TBP with an abstract interface
3294
     (which may of course be referenced), c->expr1 will be set.  */
3295
  if (csym && csym->attr.abstract && !c->expr1)
3296
    {
3297
      gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3298
                 csym->name, &c->loc);
3299
      return FAILURE;
3300
    }
3301
 
3302
  /* Subroutines without the RECURSIVE attribution are not allowed to
3303
   * call themselves.  */
3304
  if (csym && is_illegal_recursion (csym, gfc_current_ns))
3305
    {
3306
      if (csym->attr.entry && csym->ns->entries)
3307
        gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3308
                   " subroutine '%s' is not RECURSIVE",
3309
                   csym->name, &c->loc, csym->ns->entries->sym->name);
3310
      else
3311
        gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3312
                   " is not RECURSIVE", csym->name, &c->loc);
3313
 
3314
      t = FAILURE;
3315
    }
3316
 
3317
  /* Switch off assumed size checking and do this again for certain kinds
3318
     of procedure, once the procedure itself is resolved.  */
3319
  need_full_assumed_size++;
3320
 
3321
  if (csym)
3322
    ptype = csym->attr.proc;
3323
 
3324
  no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3325
  if (resolve_actual_arglist (c->ext.actual, ptype,
3326
                              no_formal_args) == FAILURE)
3327
    return FAILURE;
3328
 
3329
  /* Resume assumed_size checking.  */
3330
  need_full_assumed_size--;
3331
 
3332
  /* If external, check for usage.  */
3333
  if (csym && is_external_proc (csym))
3334
    resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3335
 
3336
  t = SUCCESS;
3337
  if (c->resolved_sym == NULL)
3338
    {
3339
      c->resolved_isym = NULL;
3340
      switch (procedure_kind (csym))
3341
        {
3342
        case PTYPE_GENERIC:
3343
          t = resolve_generic_s (c);
3344
          break;
3345
 
3346
        case PTYPE_SPECIFIC:
3347
          t = resolve_specific_s (c);
3348
          break;
3349
 
3350
        case PTYPE_UNKNOWN:
3351
          t = resolve_unknown_s (c);
3352
          break;
3353
 
3354
        default:
3355
          gfc_internal_error ("resolve_subroutine(): bad function type");
3356
        }
3357
    }
3358
 
3359
  /* Some checks of elemental subroutine actual arguments.  */
3360
  if (resolve_elemental_actual (NULL, c) == FAILURE)
3361
    return FAILURE;
3362
 
3363
  if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3364
    find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3365
  return t;
3366
}
3367
 
3368
 
3369
/* Compare the shapes of two arrays that have non-NULL shapes.  If both
3370
   op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3371
   match.  If both op1->shape and op2->shape are non-NULL return FAILURE
3372
   if their shapes do not match.  If either op1->shape or op2->shape is
3373
   NULL, return SUCCESS.  */
3374
 
3375
static gfc_try
3376
compare_shapes (gfc_expr *op1, gfc_expr *op2)
3377
{
3378
  gfc_try t;
3379
  int i;
3380
 
3381
  t = SUCCESS;
3382
 
3383
  if (op1->shape != NULL && op2->shape != NULL)
3384
    {
3385
      for (i = 0; i < op1->rank; i++)
3386
        {
3387
          if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3388
           {
3389
             gfc_error ("Shapes for operands at %L and %L are not conformable",
3390
                         &op1->where, &op2->where);
3391
             t = FAILURE;
3392
             break;
3393
           }
3394
        }
3395
    }
3396
 
3397
  return t;
3398
}
3399
 
3400
 
3401
/* Resolve an operator expression node.  This can involve replacing the
3402
   operation with a user defined function call.  */
3403
 
3404
static gfc_try
3405
resolve_operator (gfc_expr *e)
3406
{
3407
  gfc_expr *op1, *op2;
3408
  char msg[200];
3409
  bool dual_locus_error;
3410
  gfc_try t;
3411
 
3412
  /* Resolve all subnodes-- give them types.  */
3413
 
3414
  switch (e->value.op.op)
3415
    {
3416
    default:
3417
      if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3418
        return FAILURE;
3419
 
3420
    /* Fall through...  */
3421
 
3422
    case INTRINSIC_NOT:
3423
    case INTRINSIC_UPLUS:
3424
    case INTRINSIC_UMINUS:
3425
    case INTRINSIC_PARENTHESES:
3426
      if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3427
        return FAILURE;
3428
      break;
3429
    }
3430
 
3431
  /* Typecheck the new node.  */
3432
 
3433
  op1 = e->value.op.op1;
3434
  op2 = e->value.op.op2;
3435
  dual_locus_error = false;
3436
 
3437
  if ((op1 && op1->expr_type == EXPR_NULL)
3438
      || (op2 && op2->expr_type == EXPR_NULL))
3439
    {
3440
      sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3441
      goto bad_op;
3442
    }
3443
 
3444
  switch (e->value.op.op)
3445
    {
3446
    case INTRINSIC_UPLUS:
3447
    case INTRINSIC_UMINUS:
3448
      if (op1->ts.type == BT_INTEGER
3449
          || op1->ts.type == BT_REAL
3450
          || op1->ts.type == BT_COMPLEX)
3451
        {
3452
          e->ts = op1->ts;
3453
          break;
3454
        }
3455
 
3456
      sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3457
               gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3458
      goto bad_op;
3459
 
3460
    case INTRINSIC_PLUS:
3461
    case INTRINSIC_MINUS:
3462
    case INTRINSIC_TIMES:
3463
    case INTRINSIC_DIVIDE:
3464
    case INTRINSIC_POWER:
3465
      if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3466
        {
3467
          gfc_type_convert_binary (e, 1);
3468
          break;
3469
        }
3470
 
3471
      sprintf (msg,
3472
               _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3473
               gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3474
               gfc_typename (&op2->ts));
3475
      goto bad_op;
3476
 
3477
    case INTRINSIC_CONCAT:
3478
      if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3479
          && op1->ts.kind == op2->ts.kind)
3480
        {
3481
          e->ts.type = BT_CHARACTER;
3482
          e->ts.kind = op1->ts.kind;
3483
          break;
3484
        }
3485
 
3486
      sprintf (msg,
3487
               _("Operands of string concatenation operator at %%L are %s/%s"),
3488
               gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3489
      goto bad_op;
3490
 
3491
    case INTRINSIC_AND:
3492
    case INTRINSIC_OR:
3493
    case INTRINSIC_EQV:
3494
    case INTRINSIC_NEQV:
3495
      if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3496
        {
3497
          e->ts.type = BT_LOGICAL;
3498
          e->ts.kind = gfc_kind_max (op1, op2);
3499
          if (op1->ts.kind < e->ts.kind)
3500
            gfc_convert_type (op1, &e->ts, 2);
3501
          else if (op2->ts.kind < e->ts.kind)
3502
            gfc_convert_type (op2, &e->ts, 2);
3503
          break;
3504
        }
3505
 
3506
      sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3507
               gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3508
               gfc_typename (&op2->ts));
3509
 
3510
      goto bad_op;
3511
 
3512
    case INTRINSIC_NOT:
3513
      if (op1->ts.type == BT_LOGICAL)
3514
        {
3515
          e->ts.type = BT_LOGICAL;
3516
          e->ts.kind = op1->ts.kind;
3517
          break;
3518
        }
3519
 
3520
      sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3521
               gfc_typename (&op1->ts));
3522
      goto bad_op;
3523
 
3524
    case INTRINSIC_GT:
3525
    case INTRINSIC_GT_OS:
3526
    case INTRINSIC_GE:
3527
    case INTRINSIC_GE_OS:
3528
    case INTRINSIC_LT:
3529
    case INTRINSIC_LT_OS:
3530
    case INTRINSIC_LE:
3531
    case INTRINSIC_LE_OS:
3532
      if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3533
        {
3534
          strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3535
          goto bad_op;
3536
        }
3537
 
3538
      /* Fall through...  */
3539
 
3540
    case INTRINSIC_EQ:
3541
    case INTRINSIC_EQ_OS:
3542
    case INTRINSIC_NE:
3543
    case INTRINSIC_NE_OS:
3544
      if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3545
          && op1->ts.kind == op2->ts.kind)
3546
        {
3547
          e->ts.type = BT_LOGICAL;
3548
          e->ts.kind = gfc_default_logical_kind;
3549
          break;
3550
        }
3551
 
3552
      if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3553
        {
3554
          gfc_type_convert_binary (e, 1);
3555
 
3556
          e->ts.type = BT_LOGICAL;
3557
          e->ts.kind = gfc_default_logical_kind;
3558
          break;
3559
        }
3560
 
3561
      if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3562
        sprintf (msg,
3563
                 _("Logicals at %%L must be compared with %s instead of %s"),
3564
                 (e->value.op.op == INTRINSIC_EQ
3565
                  || e->value.op.op == INTRINSIC_EQ_OS)
3566
                 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3567
      else
3568
        sprintf (msg,
3569
                 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3570
                 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3571
                 gfc_typename (&op2->ts));
3572
 
3573
      goto bad_op;
3574
 
3575
    case INTRINSIC_USER:
3576
      if (e->value.op.uop->op == NULL)
3577
        sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3578
      else if (op2 == NULL)
3579
        sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3580
                 e->value.op.uop->name, gfc_typename (&op1->ts));
3581
      else
3582
        sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3583
                 e->value.op.uop->name, gfc_typename (&op1->ts),
3584
                 gfc_typename (&op2->ts));
3585
 
3586
      goto bad_op;
3587
 
3588
    case INTRINSIC_PARENTHESES:
3589
      e->ts = op1->ts;
3590
      if (e->ts.type == BT_CHARACTER)
3591
        e->ts.u.cl = op1->ts.u.cl;
3592
      break;
3593
 
3594
    default:
3595
      gfc_internal_error ("resolve_operator(): Bad intrinsic");
3596
    }
3597
 
3598
  /* Deal with arrayness of an operand through an operator.  */
3599
 
3600
  t = SUCCESS;
3601
 
3602
  switch (e->value.op.op)
3603
    {
3604
    case INTRINSIC_PLUS:
3605
    case INTRINSIC_MINUS:
3606
    case INTRINSIC_TIMES:
3607
    case INTRINSIC_DIVIDE:
3608
    case INTRINSIC_POWER:
3609
    case INTRINSIC_CONCAT:
3610
    case INTRINSIC_AND:
3611
    case INTRINSIC_OR:
3612
    case INTRINSIC_EQV:
3613
    case INTRINSIC_NEQV:
3614
    case INTRINSIC_EQ:
3615
    case INTRINSIC_EQ_OS:
3616
    case INTRINSIC_NE:
3617
    case INTRINSIC_NE_OS:
3618
    case INTRINSIC_GT:
3619
    case INTRINSIC_GT_OS:
3620
    case INTRINSIC_GE:
3621
    case INTRINSIC_GE_OS:
3622
    case INTRINSIC_LT:
3623
    case INTRINSIC_LT_OS:
3624
    case INTRINSIC_LE:
3625
    case INTRINSIC_LE_OS:
3626
 
3627
      if (op1->rank == 0 && op2->rank == 0)
3628
        e->rank = 0;
3629
 
3630
      if (op1->rank == 0 && op2->rank != 0)
3631
        {
3632
          e->rank = op2->rank;
3633
 
3634
          if (e->shape == NULL)
3635
            e->shape = gfc_copy_shape (op2->shape, op2->rank);
3636
        }
3637
 
3638
      if (op1->rank != 0 && op2->rank == 0)
3639
        {
3640
          e->rank = op1->rank;
3641
 
3642
          if (e->shape == NULL)
3643
            e->shape = gfc_copy_shape (op1->shape, op1->rank);
3644
        }
3645
 
3646
      if (op1->rank != 0 && op2->rank != 0)
3647
        {
3648
          if (op1->rank == op2->rank)
3649
            {
3650
              e->rank = op1->rank;
3651
              if (e->shape == NULL)
3652
                {
3653
                  t = compare_shapes(op1, op2);
3654
                  if (t == FAILURE)
3655
                    e->shape = NULL;
3656
                  else
3657
                e->shape = gfc_copy_shape (op1->shape, op1->rank);
3658
                }
3659
            }
3660
          else
3661
            {
3662
              /* Allow higher level expressions to work.  */
3663
              e->rank = 0;
3664
 
3665
              /* Try user-defined operators, and otherwise throw an error.  */
3666
              dual_locus_error = true;
3667
              sprintf (msg,
3668
                       _("Inconsistent ranks for operator at %%L and %%L"));
3669
              goto bad_op;
3670
            }
3671
        }
3672
 
3673
      break;
3674
 
3675
    case INTRINSIC_PARENTHESES:
3676
    case INTRINSIC_NOT:
3677
    case INTRINSIC_UPLUS:
3678
    case INTRINSIC_UMINUS:
3679
      /* Simply copy arrayness attribute */
3680
      e->rank = op1->rank;
3681
 
3682
      if (e->shape == NULL)
3683
        e->shape = gfc_copy_shape (op1->shape, op1->rank);
3684
 
3685
      break;
3686
 
3687
    default:
3688
      break;
3689
    }
3690
 
3691
  /* Attempt to simplify the expression.  */
3692
  if (t == SUCCESS)
3693
    {
3694
      t = gfc_simplify_expr (e, 0);
3695
      /* Some calls do not succeed in simplification and return FAILURE
3696
         even though there is no error; e.g. variable references to
3697
         PARAMETER arrays.  */
3698
      if (!gfc_is_constant_expr (e))
3699
        t = SUCCESS;
3700
    }
3701
  return t;
3702
 
3703
bad_op:
3704
 
3705
  {
3706
    bool real_error;
3707
    if (gfc_extend_expr (e, &real_error) == SUCCESS)
3708
      return SUCCESS;
3709
 
3710
    if (real_error)
3711
      return FAILURE;
3712
  }
3713
 
3714
  if (dual_locus_error)
3715
    gfc_error (msg, &op1->where, &op2->where);
3716
  else
3717
    gfc_error (msg, &e->where);
3718
 
3719
  return FAILURE;
3720
}
3721
 
3722
 
3723
/************** Array resolution subroutines **************/
3724
 
3725
typedef enum
3726
{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3727
comparison;
3728
 
3729
/* Compare two integer expressions.  */
3730
 
3731
static comparison
3732
compare_bound (gfc_expr *a, gfc_expr *b)
3733
{
3734
  int i;
3735
 
3736
  if (a == NULL || a->expr_type != EXPR_CONSTANT
3737
      || b == NULL || b->expr_type != EXPR_CONSTANT)
3738
    return CMP_UNKNOWN;
3739
 
3740
  /* If either of the types isn't INTEGER, we must have
3741
     raised an error earlier.  */
3742
 
3743
  if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3744
    return CMP_UNKNOWN;
3745
 
3746
  i = mpz_cmp (a->value.integer, b->value.integer);
3747
 
3748
  if (i < 0)
3749
    return CMP_LT;
3750
  if (i > 0)
3751
    return CMP_GT;
3752
  return CMP_EQ;
3753
}
3754
 
3755
 
3756
/* Compare an integer expression with an integer.  */
3757
 
3758
static comparison
3759
compare_bound_int (gfc_expr *a, int b)
3760
{
3761
  int i;
3762
 
3763
  if (a == NULL || a->expr_type != EXPR_CONSTANT)
3764
    return CMP_UNKNOWN;
3765
 
3766
  if (a->ts.type != BT_INTEGER)
3767
    gfc_internal_error ("compare_bound_int(): Bad expression");
3768
 
3769
  i = mpz_cmp_si (a->value.integer, b);
3770
 
3771
  if (i < 0)
3772
    return CMP_LT;
3773
  if (i > 0)
3774
    return CMP_GT;
3775
  return CMP_EQ;
3776
}
3777
 
3778
 
3779
/* Compare an integer expression with a mpz_t.  */
3780
 
3781
static comparison
3782
compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3783
{
3784
  int i;
3785
 
3786
  if (a == NULL || a->expr_type != EXPR_CONSTANT)
3787
    return CMP_UNKNOWN;
3788
 
3789
  if (a->ts.type != BT_INTEGER)
3790
    gfc_internal_error ("compare_bound_int(): Bad expression");
3791
 
3792
  i = mpz_cmp (a->value.integer, b);
3793
 
3794
  if (i < 0)
3795
    return CMP_LT;
3796
  if (i > 0)
3797
    return CMP_GT;
3798
  return CMP_EQ;
3799
}
3800
 
3801
 
3802
/* Compute the last value of a sequence given by a triplet.
3803
   Return 0 if it wasn't able to compute the last value, or if the
3804
   sequence if empty, and 1 otherwise.  */
3805
 
3806
static int
3807
compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3808
                                gfc_expr *stride, mpz_t last)
3809
{
3810
  mpz_t rem;
3811
 
3812
  if (start == NULL || start->expr_type != EXPR_CONSTANT
3813
      || end == NULL || end->expr_type != EXPR_CONSTANT
3814
      || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3815
    return 0;
3816
 
3817
  if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3818
      || (stride != NULL && stride->ts.type != BT_INTEGER))
3819
    return 0;
3820
 
3821
  if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3822
    {
3823
      if (compare_bound (start, end) == CMP_GT)
3824
        return 0;
3825
      mpz_set (last, end->value.integer);
3826
      return 1;
3827
    }
3828
 
3829
  if (compare_bound_int (stride, 0) == CMP_GT)
3830
    {
3831
      /* Stride is positive */
3832
      if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3833
        return 0;
3834
    }
3835
  else
3836
    {
3837
      /* Stride is negative */
3838
      if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3839
        return 0;
3840
    }
3841
 
3842
  mpz_init (rem);
3843
  mpz_sub (rem, end->value.integer, start->value.integer);
3844
  mpz_tdiv_r (rem, rem, stride->value.integer);
3845
  mpz_sub (last, end->value.integer, rem);
3846
  mpz_clear (rem);
3847
 
3848
  return 1;
3849
}
3850
 
3851
 
3852
/* Compare a single dimension of an array reference to the array
3853
   specification.  */
3854
 
3855
static gfc_try
3856
check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3857
{
3858
  mpz_t last_value;
3859
 
3860
/* Given start, end and stride values, calculate the minimum and
3861
   maximum referenced indexes.  */
3862
 
3863
  switch (ar->dimen_type[i])
3864
    {
3865
    case DIMEN_VECTOR:
3866
      break;
3867
 
3868
    case DIMEN_ELEMENT:
3869
      if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3870
        {
3871
          gfc_warning ("Array reference at %L is out of bounds "
3872
                       "(%ld < %ld) in dimension %d", &ar->c_where[i],
3873
                       mpz_get_si (ar->start[i]->value.integer),
3874
                       mpz_get_si (as->lower[i]->value.integer), i+1);
3875
          return SUCCESS;
3876
        }
3877
      if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3878
        {
3879
          gfc_warning ("Array reference at %L is out of bounds "
3880
                       "(%ld > %ld) in dimension %d", &ar->c_where[i],
3881
                       mpz_get_si (ar->start[i]->value.integer),
3882
                       mpz_get_si (as->upper[i]->value.integer), i+1);
3883
          return SUCCESS;
3884
        }
3885
 
3886
      break;
3887
 
3888
    case DIMEN_RANGE:
3889
      {
3890
#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3891
#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3892
 
3893
        comparison comp_start_end = compare_bound (AR_START, AR_END);
3894
 
3895
        /* Check for zero stride, which is not allowed.  */
3896
        if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3897
          {
3898
            gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3899
            return FAILURE;
3900
          }
3901
 
3902
        /* if start == len || (stride > 0 && start < len)
3903
                           || (stride < 0 && start > len),
3904
           then the array section contains at least one element.  In this
3905
           case, there is an out-of-bounds access if
3906
           (start < lower || start > upper).  */
3907
        if (compare_bound (AR_START, AR_END) == CMP_EQ
3908
            || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3909
                 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3910
            || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3911
                && comp_start_end == CMP_GT))
3912
          {
3913
            if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3914
              {
3915
                gfc_warning ("Lower array reference at %L is out of bounds "
3916
                       "(%ld < %ld) in dimension %d", &ar->c_where[i],
3917
                       mpz_get_si (AR_START->value.integer),
3918
                       mpz_get_si (as->lower[i]->value.integer), i+1);
3919
                return SUCCESS;
3920
              }
3921
            if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3922
              {
3923
                gfc_warning ("Lower array reference at %L is out of bounds "
3924
                       "(%ld > %ld) in dimension %d", &ar->c_where[i],
3925
                       mpz_get_si (AR_START->value.integer),
3926
                       mpz_get_si (as->upper[i]->value.integer), i+1);
3927
                return SUCCESS;
3928
              }
3929
          }
3930
 
3931
        /* If we can compute the highest index of the array section,
3932
           then it also has to be between lower and upper.  */
3933
        mpz_init (last_value);
3934
        if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3935
                                            last_value))
3936
          {
3937
            if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3938
              {
3939
                gfc_warning ("Upper array reference at %L is out of bounds "
3940
                       "(%ld < %ld) in dimension %d", &ar->c_where[i],
3941
                       mpz_get_si (last_value),
3942
                       mpz_get_si (as->lower[i]->value.integer), i+1);
3943
                mpz_clear (last_value);
3944
                return SUCCESS;
3945
              }
3946
            if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3947
              {
3948
                gfc_warning ("Upper array reference at %L is out of bounds "
3949
                       "(%ld > %ld) in dimension %d", &ar->c_where[i],
3950
                       mpz_get_si (last_value),
3951
                       mpz_get_si (as->upper[i]->value.integer), i+1);
3952
                mpz_clear (last_value);
3953
                return SUCCESS;
3954
              }
3955
          }
3956
        mpz_clear (last_value);
3957
 
3958
#undef AR_START
3959
#undef AR_END
3960
      }
3961
      break;
3962
 
3963
    default:
3964
      gfc_internal_error ("check_dimension(): Bad array reference");
3965
    }
3966
 
3967
  return SUCCESS;
3968
}
3969
 
3970
 
3971
/* Compare an array reference with an array specification.  */
3972
 
3973
static gfc_try
3974
compare_spec_to_ref (gfc_array_ref *ar)
3975
{
3976
  gfc_array_spec *as;
3977
  int i;
3978
 
3979
  as = ar->as;
3980
  i = as->rank - 1;
3981
  /* TODO: Full array sections are only allowed as actual parameters.  */
3982
  if (as->type == AS_ASSUMED_SIZE
3983
      && (/*ar->type == AR_FULL
3984
          ||*/ (ar->type == AR_SECTION
3985
              && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3986
    {
3987
      gfc_error ("Rightmost upper bound of assumed size array section "
3988
                 "not specified at %L", &ar->where);
3989
      return FAILURE;
3990
    }
3991
 
3992
  if (ar->type == AR_FULL)
3993
    return SUCCESS;
3994
 
3995
  if (as->rank != ar->dimen)
3996
    {
3997
      gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3998
                 &ar->where, ar->dimen, as->rank);
3999
      return FAILURE;
4000
    }
4001
 
4002
  for (i = 0; i < as->rank; i++)
4003
    if (check_dimension (i, ar, as) == FAILURE)
4004
      return FAILURE;
4005
 
4006
  return SUCCESS;
4007
}
4008
 
4009
 
4010
/* Resolve one part of an array index.  */
4011
 
4012
gfc_try
4013
gfc_resolve_index (gfc_expr *index, int check_scalar)
4014
{
4015
  gfc_typespec ts;
4016
 
4017
  if (index == NULL)
4018
    return SUCCESS;
4019
 
4020
  if (gfc_resolve_expr (index) == FAILURE)
4021
    return FAILURE;
4022
 
4023
  if (check_scalar && index->rank != 0)
4024
    {
4025
      gfc_error ("Array index at %L must be scalar", &index->where);
4026
      return FAILURE;
4027
    }
4028
 
4029
  if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4030
    {
4031
      gfc_error ("Array index at %L must be of INTEGER type, found %s",
4032
                 &index->where, gfc_basic_typename (index->ts.type));
4033
      return FAILURE;
4034
    }
4035
 
4036
  if (index->ts.type == BT_REAL)
4037
    if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4038
                        &index->where) == FAILURE)
4039
      return FAILURE;
4040
 
4041
  if (index->ts.kind != gfc_index_integer_kind
4042
      || index->ts.type != BT_INTEGER)
4043
    {
4044
      gfc_clear_ts (&ts);
4045
      ts.type = BT_INTEGER;
4046
      ts.kind = gfc_index_integer_kind;
4047
 
4048
      gfc_convert_type_warn (index, &ts, 2, 0);
4049
    }
4050
 
4051
  return SUCCESS;
4052
}
4053
 
4054
/* Resolve a dim argument to an intrinsic function.  */
4055
 
4056
gfc_try
4057
gfc_resolve_dim_arg (gfc_expr *dim)
4058
{
4059
  if (dim == NULL)
4060
    return SUCCESS;
4061
 
4062
  if (gfc_resolve_expr (dim) == FAILURE)
4063
    return FAILURE;
4064
 
4065
  if (dim->rank != 0)
4066
    {
4067
      gfc_error ("Argument dim at %L must be scalar", &dim->where);
4068
      return FAILURE;
4069
 
4070
    }
4071
 
4072
  if (dim->ts.type != BT_INTEGER)
4073
    {
4074
      gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4075
      return FAILURE;
4076
    }
4077
 
4078
  if (dim->ts.kind != gfc_index_integer_kind)
4079
    {
4080
      gfc_typespec ts;
4081
 
4082
      gfc_clear_ts (&ts);
4083
      ts.type = BT_INTEGER;
4084
      ts.kind = gfc_index_integer_kind;
4085
 
4086
      gfc_convert_type_warn (dim, &ts, 2, 0);
4087
    }
4088
 
4089
  return SUCCESS;
4090
}
4091
 
4092
/* Given an expression that contains array references, update those array
4093
   references to point to the right array specifications.  While this is
4094
   filled in during matching, this information is difficult to save and load
4095
   in a module, so we take care of it here.
4096
 
4097
   The idea here is that the original array reference comes from the
4098
   base symbol.  We traverse the list of reference structures, setting
4099
   the stored reference to references.  Component references can
4100
   provide an additional array specification.  */
4101
 
4102
static void
4103
find_array_spec (gfc_expr *e)
4104
{
4105
  gfc_array_spec *as;
4106
  gfc_component *c;
4107
  gfc_symbol *derived;
4108
  gfc_ref *ref;
4109
 
4110
  if (e->symtree->n.sym->ts.type == BT_CLASS)
4111
    as = e->symtree->n.sym->ts.u.derived->components->as;
4112
  else
4113
    as = e->symtree->n.sym->as;
4114
  derived = NULL;
4115
 
4116
  for (ref = e->ref; ref; ref = ref->next)
4117
    switch (ref->type)
4118
      {
4119
      case REF_ARRAY:
4120
        if (as == NULL)
4121
          gfc_internal_error ("find_array_spec(): Missing spec");
4122
 
4123
        ref->u.ar.as = as;
4124
        as = NULL;
4125
        break;
4126
 
4127
      case REF_COMPONENT:
4128
        if (derived == NULL)
4129
          derived = e->symtree->n.sym->ts.u.derived;
4130
 
4131
        if (derived->attr.is_class)
4132
          derived = derived->components->ts.u.derived;
4133
 
4134
        c = derived->components;
4135
 
4136
        for (; c; c = c->next)
4137
          if (c == ref->u.c.component)
4138
            {
4139
              /* Track the sequence of component references.  */
4140
              if (c->ts.type == BT_DERIVED)
4141
                derived = c->ts.u.derived;
4142
              break;
4143
            }
4144
 
4145
        if (c == NULL)
4146
          gfc_internal_error ("find_array_spec(): Component not found");
4147
 
4148
        if (c->attr.dimension)
4149
          {
4150
            if (as != NULL)
4151
              gfc_internal_error ("find_array_spec(): unused as(1)");
4152
            as = c->as;
4153
          }
4154
 
4155
        break;
4156
 
4157
      case REF_SUBSTRING:
4158
        break;
4159
      }
4160
 
4161
  if (as != NULL)
4162
    gfc_internal_error ("find_array_spec(): unused as(2)");
4163
}
4164
 
4165
 
4166
/* Resolve an array reference.  */
4167
 
4168
static gfc_try
4169
resolve_array_ref (gfc_array_ref *ar)
4170
{
4171
  int i, check_scalar;
4172
  gfc_expr *e;
4173
 
4174
  for (i = 0; i < ar->dimen; i++)
4175
    {
4176
      check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4177
 
4178
      if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4179
        return FAILURE;
4180
      if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4181
        return FAILURE;
4182
      if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4183
        return FAILURE;
4184
 
4185
      e = ar->start[i];
4186
 
4187
      if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4188
        switch (e->rank)
4189
          {
4190
          case 0:
4191
            ar->dimen_type[i] = DIMEN_ELEMENT;
4192
            break;
4193
 
4194
          case 1:
4195
            ar->dimen_type[i] = DIMEN_VECTOR;
4196
            if (e->expr_type == EXPR_VARIABLE
4197
                && e->symtree->n.sym->ts.type == BT_DERIVED)
4198
              ar->start[i] = gfc_get_parentheses (e);
4199
            break;
4200
 
4201
          default:
4202
            gfc_error ("Array index at %L is an array of rank %d",
4203
                       &ar->c_where[i], e->rank);
4204
            return FAILURE;
4205
          }
4206
    }
4207
 
4208
  /* If the reference type is unknown, figure out what kind it is.  */
4209
 
4210
  if (ar->type == AR_UNKNOWN)
4211
    {
4212
      ar->type = AR_ELEMENT;
4213
      for (i = 0; i < ar->dimen; i++)
4214
        if (ar->dimen_type[i] == DIMEN_RANGE
4215
            || ar->dimen_type[i] == DIMEN_VECTOR)
4216
          {
4217
            ar->type = AR_SECTION;
4218
            break;
4219
          }
4220
    }
4221
 
4222
  if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4223
    return FAILURE;
4224
 
4225
  return SUCCESS;
4226
}
4227
 
4228
 
4229
static gfc_try
4230
resolve_substring (gfc_ref *ref)
4231
{
4232
  int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4233
 
4234
  if (ref->u.ss.start != NULL)
4235
    {
4236
      if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4237
        return FAILURE;
4238
 
4239
      if (ref->u.ss.start->ts.type != BT_INTEGER)
4240
        {
4241
          gfc_error ("Substring start index at %L must be of type INTEGER",
4242
                     &ref->u.ss.start->where);
4243
          return FAILURE;
4244
        }
4245
 
4246
      if (ref->u.ss.start->rank != 0)
4247
        {
4248
          gfc_error ("Substring start index at %L must be scalar",
4249
                     &ref->u.ss.start->where);
4250
          return FAILURE;
4251
        }
4252
 
4253
      if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4254
          && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4255
              || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4256
        {
4257
          gfc_error ("Substring start index at %L is less than one",
4258
                     &ref->u.ss.start->where);
4259
          return FAILURE;
4260
        }
4261
    }
4262
 
4263
  if (ref->u.ss.end != NULL)
4264
    {
4265
      if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4266
        return FAILURE;
4267
 
4268
      if (ref->u.ss.end->ts.type != BT_INTEGER)
4269
        {
4270
          gfc_error ("Substring end index at %L must be of type INTEGER",
4271
                     &ref->u.ss.end->where);
4272
          return FAILURE;
4273
        }
4274
 
4275
      if (ref->u.ss.end->rank != 0)
4276
        {
4277
          gfc_error ("Substring end index at %L must be scalar",
4278
                     &ref->u.ss.end->where);
4279
          return FAILURE;
4280
        }
4281
 
4282
      if (ref->u.ss.length != NULL
4283
          && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4284
          && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4285
              || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4286
        {
4287
          gfc_error ("Substring end index at %L exceeds the string length",
4288
                     &ref->u.ss.start->where);
4289
          return FAILURE;
4290
        }
4291
 
4292
      if (compare_bound_mpz_t (ref->u.ss.end,
4293
                               gfc_integer_kinds[k].huge) == CMP_GT
4294
          && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4295
              || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4296
        {
4297
          gfc_error ("Substring end index at %L is too large",
4298
                     &ref->u.ss.end->where);
4299
          return FAILURE;
4300
        }
4301
    }
4302
 
4303
  return SUCCESS;
4304
}
4305
 
4306
 
4307
/* This function supplies missing substring charlens.  */
4308
 
4309
void
4310
gfc_resolve_substring_charlen (gfc_expr *e)
4311
{
4312
  gfc_ref *char_ref;
4313
  gfc_expr *start, *end;
4314
 
4315
  for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4316
    if (char_ref->type == REF_SUBSTRING)
4317
      break;
4318
 
4319
  if (!char_ref)
4320
    return;
4321
 
4322
  gcc_assert (char_ref->next == NULL);
4323
 
4324
  if (e->ts.u.cl)
4325
    {
4326
      if (e->ts.u.cl->length)
4327
        gfc_free_expr (e->ts.u.cl->length);
4328
      else if (e->expr_type == EXPR_VARIABLE
4329
                 && e->symtree->n.sym->attr.dummy)
4330
        return;
4331
    }
4332
 
4333
  e->ts.type = BT_CHARACTER;
4334
  e->ts.kind = gfc_default_character_kind;
4335
 
4336
  if (!e->ts.u.cl)
4337
    e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4338
 
4339
  if (char_ref->u.ss.start)
4340
    start = gfc_copy_expr (char_ref->u.ss.start);
4341
  else
4342
    start = gfc_int_expr (1);
4343
 
4344
  if (char_ref->u.ss.end)
4345
    end = gfc_copy_expr (char_ref->u.ss.end);
4346
  else if (e->expr_type == EXPR_VARIABLE)
4347
    end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4348
  else
4349
    end = NULL;
4350
 
4351
  if (!start || !end)
4352
    return;
4353
 
4354
  /* Length = (end - start +1).  */
4355
  e->ts.u.cl->length = gfc_subtract (end, start);
4356
  e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4357
 
4358
  e->ts.u.cl->length->ts.type = BT_INTEGER;
4359
  e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4360
 
4361
  /* Make sure that the length is simplified.  */
4362
  gfc_simplify_expr (e->ts.u.cl->length, 1);
4363
  gfc_resolve_expr (e->ts.u.cl->length);
4364
}
4365
 
4366
 
4367
/* Resolve subtype references.  */
4368
 
4369
static gfc_try
4370
resolve_ref (gfc_expr *expr)
4371
{
4372
  int current_part_dimension, n_components, seen_part_dimension;
4373
  gfc_ref *ref;
4374
 
4375
  for (ref = expr->ref; ref; ref = ref->next)
4376
    if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4377
      {
4378
        find_array_spec (expr);
4379
        break;
4380
      }
4381
 
4382
  for (ref = expr->ref; ref; ref = ref->next)
4383
    switch (ref->type)
4384
      {
4385
      case REF_ARRAY:
4386
        if (resolve_array_ref (&ref->u.ar) == FAILURE)
4387
          return FAILURE;
4388
        break;
4389
 
4390
      case REF_COMPONENT:
4391
        break;
4392
 
4393
      case REF_SUBSTRING:
4394
        resolve_substring (ref);
4395
        break;
4396
      }
4397
 
4398
  /* Check constraints on part references.  */
4399
 
4400
  current_part_dimension = 0;
4401
  seen_part_dimension = 0;
4402
  n_components = 0;
4403
 
4404
  for (ref = expr->ref; ref; ref = ref->next)
4405
    {
4406
      switch (ref->type)
4407
        {
4408
        case REF_ARRAY:
4409
          switch (ref->u.ar.type)
4410
            {
4411
            case AR_FULL:
4412
            case AR_SECTION:
4413
              current_part_dimension = 1;
4414
              break;
4415
 
4416
            case AR_ELEMENT:
4417
              current_part_dimension = 0;
4418
              break;
4419
 
4420
            case AR_UNKNOWN:
4421
              gfc_internal_error ("resolve_ref(): Bad array reference");
4422
            }
4423
 
4424
          break;
4425
 
4426
        case REF_COMPONENT:
4427
          if (current_part_dimension || seen_part_dimension)
4428
            {
4429
              /* F03:C614.  */
4430
              if (ref->u.c.component->attr.pointer
4431
                  || ref->u.c.component->attr.proc_pointer)
4432
                {
4433
                  gfc_error ("Component to the right of a part reference "
4434
                             "with nonzero rank must not have the POINTER "
4435
                             "attribute at %L", &expr->where);
4436
                  return FAILURE;
4437
                }
4438
              else if (ref->u.c.component->attr.allocatable)
4439
                {
4440
                  gfc_error ("Component to the right of a part reference "
4441
                             "with nonzero rank must not have the ALLOCATABLE "
4442
                             "attribute at %L", &expr->where);
4443
                  return FAILURE;
4444
                }
4445
            }
4446
 
4447
          n_components++;
4448
          break;
4449
 
4450
        case REF_SUBSTRING:
4451
          break;
4452
        }
4453
 
4454
      if (((ref->type == REF_COMPONENT && n_components > 1)
4455
           || ref->next == NULL)
4456
          && current_part_dimension
4457
          && seen_part_dimension)
4458
        {
4459
          gfc_error ("Two or more part references with nonzero rank must "
4460
                     "not be specified at %L", &expr->where);
4461
          return FAILURE;
4462
        }
4463
 
4464
      if (ref->type == REF_COMPONENT)
4465
        {
4466
          if (current_part_dimension)
4467
            seen_part_dimension = 1;
4468
 
4469
          /* reset to make sure */
4470
          current_part_dimension = 0;
4471
        }
4472
    }
4473
 
4474
  return SUCCESS;
4475
}
4476
 
4477
 
4478
/* Given an expression, determine its shape.  This is easier than it sounds.
4479
   Leaves the shape array NULL if it is not possible to determine the shape.  */
4480
 
4481
static void
4482
expression_shape (gfc_expr *e)
4483
{
4484
  mpz_t array[GFC_MAX_DIMENSIONS];
4485
  int i;
4486
 
4487
  if (e->rank == 0 || e->shape != NULL)
4488
    return;
4489
 
4490
  for (i = 0; i < e->rank; i++)
4491
    if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4492
      goto fail;
4493
 
4494
  e->shape = gfc_get_shape (e->rank);
4495
 
4496
  memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4497
 
4498
  return;
4499
 
4500
fail:
4501
  for (i--; i >= 0; i--)
4502
    mpz_clear (array[i]);
4503
}
4504
 
4505
 
4506
/* Given a variable expression node, compute the rank of the expression by
4507
   examining the base symbol and any reference structures it may have.  */
4508
 
4509
static void
4510
expression_rank (gfc_expr *e)
4511
{
4512
  gfc_ref *ref;
4513
  int i, rank;
4514
 
4515
  /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4516
     could lead to serious confusion...  */
4517
  gcc_assert (e->expr_type != EXPR_COMPCALL);
4518
 
4519
  if (e->ref == NULL)
4520
    {
4521
      if (e->expr_type == EXPR_ARRAY)
4522
        goto done;
4523
      /* Constructors can have a rank different from one via RESHAPE().  */
4524
 
4525
      if (e->symtree == NULL)
4526
        {
4527
          e->rank = 0;
4528
          goto done;
4529
        }
4530
 
4531
      e->rank = (e->symtree->n.sym->as == NULL)
4532
                ? 0 : e->symtree->n.sym->as->rank;
4533
      goto done;
4534
    }
4535
 
4536
  rank = 0;
4537
 
4538
  for (ref = e->ref; ref; ref = ref->next)
4539
    {
4540
      if (ref->type != REF_ARRAY)
4541
        continue;
4542
 
4543
      if (ref->u.ar.type == AR_FULL)
4544
        {
4545
          rank = ref->u.ar.as->rank;
4546
          break;
4547
        }
4548
 
4549
      if (ref->u.ar.type == AR_SECTION)
4550
        {
4551
          /* Figure out the rank of the section.  */
4552
          if (rank != 0)
4553
            gfc_internal_error ("expression_rank(): Two array specs");
4554
 
4555
          for (i = 0; i < ref->u.ar.dimen; i++)
4556
            if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4557
                || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4558
              rank++;
4559
 
4560
          break;
4561
        }
4562
    }
4563
 
4564
  e->rank = rank;
4565
 
4566
done:
4567
  expression_shape (e);
4568
}
4569
 
4570
 
4571
/* Resolve a variable expression.  */
4572
 
4573
static gfc_try
4574
resolve_variable (gfc_expr *e)
4575
{
4576
  gfc_symbol *sym;
4577
  gfc_try t;
4578
 
4579
  t = SUCCESS;
4580
 
4581
  if (e->symtree == NULL)
4582
    return FAILURE;
4583
 
4584
  if (e->ref && resolve_ref (e) == FAILURE)
4585
    return FAILURE;
4586
 
4587
  sym = e->symtree->n.sym;
4588
  if (sym->attr.flavor == FL_PROCEDURE
4589
      && (!sym->attr.function
4590
          || (sym->attr.function && sym->result
4591
              && sym->result->attr.proc_pointer
4592
              && !sym->result->attr.function)))
4593
    {
4594
      e->ts.type = BT_PROCEDURE;
4595
      goto resolve_procedure;
4596
    }
4597
 
4598
  if (sym->ts.type != BT_UNKNOWN)
4599
    gfc_variable_attr (e, &e->ts);
4600
  else
4601
    {
4602
      /* Must be a simple variable reference.  */
4603
      if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4604
        return FAILURE;
4605
      e->ts = sym->ts;
4606
    }
4607
 
4608
  if (check_assumed_size_reference (sym, e))
4609
    return FAILURE;
4610
 
4611
  /* Deal with forward references to entries during resolve_code, to
4612
     satisfy, at least partially, 12.5.2.5.  */
4613
  if (gfc_current_ns->entries
4614
      && current_entry_id == sym->entry_id
4615
      && cs_base
4616
      && cs_base->current
4617
      && cs_base->current->op != EXEC_ENTRY)
4618
    {
4619
      gfc_entry_list *entry;
4620
      gfc_formal_arglist *formal;
4621
      int n;
4622
      bool seen;
4623
 
4624
      /* If the symbol is a dummy...  */
4625
      if (sym->attr.dummy && sym->ns == gfc_current_ns)
4626
        {
4627
          entry = gfc_current_ns->entries;
4628
          seen = false;
4629
 
4630
          /* ...test if the symbol is a parameter of previous entries.  */
4631
          for (; entry && entry->id <= current_entry_id; entry = entry->next)
4632
            for (formal = entry->sym->formal; formal; formal = formal->next)
4633
              {
4634
                if (formal->sym && sym->name == formal->sym->name)
4635
                  seen = true;
4636
              }
4637
 
4638
          /*  If it has not been seen as a dummy, this is an error.  */
4639
          if (!seen)
4640
            {
4641
              if (specification_expr)
4642
                gfc_error ("Variable '%s', used in a specification expression"
4643
                           ", is referenced at %L before the ENTRY statement "
4644
                           "in which it is a parameter",
4645
                           sym->name, &cs_base->current->loc);
4646
              else
4647
                gfc_error ("Variable '%s' is used at %L before the ENTRY "
4648
                           "statement in which it is a parameter",
4649
                           sym->name, &cs_base->current->loc);
4650
              t = FAILURE;
4651
            }
4652
        }
4653
 
4654
      /* Now do the same check on the specification expressions.  */
4655
      specification_expr = 1;
4656
      if (sym->ts.type == BT_CHARACTER
4657
          && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4658
        t = FAILURE;
4659
 
4660
      if (sym->as)
4661
        for (n = 0; n < sym->as->rank; n++)
4662
          {
4663
             specification_expr = 1;
4664
             if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4665
               t = FAILURE;
4666
             specification_expr = 1;
4667
             if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4668
               t = FAILURE;
4669
          }
4670
      specification_expr = 0;
4671
 
4672
      if (t == SUCCESS)
4673
        /* Update the symbol's entry level.  */
4674
        sym->entry_id = current_entry_id + 1;
4675
    }
4676
 
4677
  /* If a symbol has been host_associated mark it.  This is used latter,
4678
     to identify if aliasing is possible via host association.  */
4679
  if (sym->attr.flavor == FL_VARIABLE
4680
        && gfc_current_ns->parent
4681
        && (gfc_current_ns->parent == sym->ns
4682
              || (gfc_current_ns->parent->parent
4683
                    && gfc_current_ns->parent->parent == sym->ns)))
4684
    sym->attr.host_assoc = 1;
4685
 
4686
resolve_procedure:
4687
  if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4688
    t = FAILURE;
4689
 
4690
  return t;
4691
}
4692
 
4693
 
4694
/* Checks to see that the correct symbol has been host associated.
4695
   The only situation where this arises is that in which a twice
4696
   contained function is parsed after the host association is made.
4697
   Therefore, on detecting this, change the symbol in the expression
4698
   and convert the array reference into an actual arglist if the old
4699
   symbol is a variable.  */
4700
static bool
4701
check_host_association (gfc_expr *e)
4702
{
4703
  gfc_symbol *sym, *old_sym;
4704
  gfc_symtree *st;
4705
  int n;
4706
  gfc_ref *ref;
4707
  gfc_actual_arglist *arg, *tail = NULL;
4708
  bool retval = e->expr_type == EXPR_FUNCTION;
4709
 
4710
  /*  If the expression is the result of substitution in
4711
      interface.c(gfc_extend_expr) because there is no way in
4712
      which the host association can be wrong.  */
4713
  if (e->symtree == NULL
4714
        || e->symtree->n.sym == NULL
4715
        || e->user_operator)
4716
    return retval;
4717
 
4718
  old_sym = e->symtree->n.sym;
4719
 
4720
  if (gfc_current_ns->parent
4721
        && old_sym->ns != gfc_current_ns)
4722
    {
4723
      /* Use the 'USE' name so that renamed module symbols are
4724
         correctly handled.  */
4725
      gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4726
 
4727
      if (sym && old_sym != sym
4728
              && sym->ts.type == old_sym->ts.type
4729
              && sym->attr.flavor == FL_PROCEDURE
4730
              && sym->attr.contained)
4731
        {
4732
          /* Clear the shape, since it might not be valid.  */
4733
          if (e->shape != NULL)
4734
            {
4735
              for (n = 0; n < e->rank; n++)
4736
                mpz_clear (e->shape[n]);
4737
 
4738
              gfc_free (e->shape);
4739
            }
4740
 
4741
          /* Give the expression the right symtree!  */
4742
          gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4743
          gcc_assert (st != NULL);
4744
 
4745
          if (old_sym->attr.flavor == FL_PROCEDURE
4746
                || e->expr_type == EXPR_FUNCTION)
4747
            {
4748
              /* Original was function so point to the new symbol, since
4749
                 the actual argument list is already attached to the
4750
                 expression. */
4751
              e->value.function.esym = NULL;
4752
              e->symtree = st;
4753
            }
4754
          else
4755
            {
4756
              /* Original was variable so convert array references into
4757
                 an actual arglist. This does not need any checking now
4758
                 since gfc_resolve_function will take care of it.  */
4759
              e->value.function.actual = NULL;
4760
              e->expr_type = EXPR_FUNCTION;
4761
              e->symtree = st;
4762
 
4763
              /* Ambiguity will not arise if the array reference is not
4764
                 the last reference.  */
4765
              for (ref = e->ref; ref; ref = ref->next)
4766
                if (ref->type == REF_ARRAY && ref->next == NULL)
4767
                  break;
4768
 
4769
              gcc_assert (ref->type == REF_ARRAY);
4770
 
4771
              /* Grab the start expressions from the array ref and
4772
                 copy them into actual arguments.  */
4773
              for (n = 0; n < ref->u.ar.dimen; n++)
4774
                {
4775
                  arg = gfc_get_actual_arglist ();
4776
                  arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4777
                  if (e->value.function.actual == NULL)
4778
                    tail = e->value.function.actual = arg;
4779
                  else
4780
                    {
4781
                      tail->next = arg;
4782
                      tail = arg;
4783
                    }
4784
                }
4785
 
4786
              /* Dump the reference list and set the rank.  */
4787
              gfc_free_ref_list (e->ref);
4788
              e->ref = NULL;
4789
              e->rank = sym->as ? sym->as->rank : 0;
4790
            }
4791
 
4792
          gfc_resolve_expr (e);
4793
          sym->refs++;
4794
        }
4795
    }
4796
  /* This might have changed!  */
4797
  return e->expr_type == EXPR_FUNCTION;
4798
}
4799
 
4800
 
4801
static void
4802
gfc_resolve_character_operator (gfc_expr *e)
4803
{
4804
  gfc_expr *op1 = e->value.op.op1;
4805
  gfc_expr *op2 = e->value.op.op2;
4806
  gfc_expr *e1 = NULL;
4807
  gfc_expr *e2 = NULL;
4808
 
4809
  gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4810
 
4811
  if (op1->ts.u.cl && op1->ts.u.cl->length)
4812
    e1 = gfc_copy_expr (op1->ts.u.cl->length);
4813
  else if (op1->expr_type == EXPR_CONSTANT)
4814
    e1 = gfc_int_expr (op1->value.character.length);
4815
 
4816
  if (op2->ts.u.cl && op2->ts.u.cl->length)
4817
    e2 = gfc_copy_expr (op2->ts.u.cl->length);
4818
  else if (op2->expr_type == EXPR_CONSTANT)
4819
    e2 = gfc_int_expr (op2->value.character.length);
4820
 
4821
  e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4822
 
4823
  if (!e1 || !e2)
4824
    return;
4825
 
4826
  e->ts.u.cl->length = gfc_add (e1, e2);
4827
  e->ts.u.cl->length->ts.type = BT_INTEGER;
4828
  e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4829
  gfc_simplify_expr (e->ts.u.cl->length, 0);
4830
  gfc_resolve_expr (e->ts.u.cl->length);
4831
 
4832
  return;
4833
}
4834
 
4835
 
4836
/*  Ensure that an character expression has a charlen and, if possible, a
4837
    length expression.  */
4838
 
4839
static void
4840
fixup_charlen (gfc_expr *e)
4841
{
4842
  /* The cases fall through so that changes in expression type and the need
4843
     for multiple fixes are picked up.  In all circumstances, a charlen should
4844
     be available for the middle end to hang a backend_decl on.  */
4845
  switch (e->expr_type)
4846
    {
4847
    case EXPR_OP:
4848
      gfc_resolve_character_operator (e);
4849
 
4850
    case EXPR_ARRAY:
4851
      if (e->expr_type == EXPR_ARRAY)
4852
        gfc_resolve_character_array_constructor (e);
4853
 
4854
    case EXPR_SUBSTRING:
4855
      if (!e->ts.u.cl && e->ref)
4856
        gfc_resolve_substring_charlen (e);
4857
 
4858
    default:
4859
      if (!e->ts.u.cl)
4860
        e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4861
 
4862
      break;
4863
    }
4864
}
4865
 
4866
 
4867
/* Update an actual argument to include the passed-object for type-bound
4868
   procedures at the right position.  */
4869
 
4870
static gfc_actual_arglist*
4871
update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4872
                     const char *name)
4873
{
4874
  gcc_assert (argpos > 0);
4875
 
4876
  if (argpos == 1)
4877
    {
4878
      gfc_actual_arglist* result;
4879
 
4880
      result = gfc_get_actual_arglist ();
4881
      result->expr = po;
4882
      result->next = lst;
4883
      if (name)
4884
        result->name = name;
4885
 
4886
      return result;
4887
    }
4888
 
4889
  if (lst)
4890
    lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4891
  else
4892
    lst = update_arglist_pass (NULL, po, argpos - 1, name);
4893
  return lst;
4894
}
4895
 
4896
 
4897
/* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
4898
 
4899
static gfc_expr*
4900
extract_compcall_passed_object (gfc_expr* e)
4901
{
4902
  gfc_expr* po;
4903
 
4904
  gcc_assert (e->expr_type == EXPR_COMPCALL);
4905
 
4906
  if (e->value.compcall.base_object)
4907
    po = gfc_copy_expr (e->value.compcall.base_object);
4908
  else
4909
    {
4910
      po = gfc_get_expr ();
4911
      po->expr_type = EXPR_VARIABLE;
4912
      po->symtree = e->symtree;
4913
      po->ref = gfc_copy_ref (e->ref);
4914
      po->where = e->where;
4915
    }
4916
 
4917
  if (gfc_resolve_expr (po) == FAILURE)
4918
    return NULL;
4919
 
4920
  return po;
4921
}
4922
 
4923
 
4924
/* Update the arglist of an EXPR_COMPCALL expression to include the
4925
   passed-object.  */
4926
 
4927
static gfc_try
4928
update_compcall_arglist (gfc_expr* e)
4929
{
4930
  gfc_expr* po;
4931
  gfc_typebound_proc* tbp;
4932
 
4933
  tbp = e->value.compcall.tbp;
4934
 
4935
  if (tbp->error)
4936
    return FAILURE;
4937
 
4938
  po = extract_compcall_passed_object (e);
4939
  if (!po)
4940
    return FAILURE;
4941
 
4942
  if (tbp->nopass || e->value.compcall.ignore_pass)
4943
    {
4944
      gfc_free_expr (po);
4945
      return SUCCESS;
4946
    }
4947
 
4948
  gcc_assert (tbp->pass_arg_num > 0);
4949
  e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4950
                                                  tbp->pass_arg_num,
4951
                                                  tbp->pass_arg);
4952
 
4953
  return SUCCESS;
4954
}
4955
 
4956
 
4957
/* Extract the passed object from a PPC call (a copy of it).  */
4958
 
4959
static gfc_expr*
4960
extract_ppc_passed_object (gfc_expr *e)
4961
{
4962
  gfc_expr *po;
4963
  gfc_ref **ref;
4964
 
4965
  po = gfc_get_expr ();
4966
  po->expr_type = EXPR_VARIABLE;
4967
  po->symtree = e->symtree;
4968
  po->ref = gfc_copy_ref (e->ref);
4969
  po->where = e->where;
4970
 
4971
  /* Remove PPC reference.  */
4972
  ref = &po->ref;
4973
  while ((*ref)->next)
4974
    ref = &(*ref)->next;
4975
  gfc_free_ref_list (*ref);
4976
  *ref = NULL;
4977
 
4978
  if (gfc_resolve_expr (po) == FAILURE)
4979
    return NULL;
4980
 
4981
  return po;
4982
}
4983
 
4984
 
4985
/* Update the actual arglist of a procedure pointer component to include the
4986
   passed-object.  */
4987
 
4988
static gfc_try
4989
update_ppc_arglist (gfc_expr* e)
4990
{
4991
  gfc_expr* po;
4992
  gfc_component *ppc;
4993
  gfc_typebound_proc* tb;
4994
 
4995
  if (!gfc_is_proc_ptr_comp (e, &ppc))
4996
    return FAILURE;
4997
 
4998
  tb = ppc->tb;
4999
 
5000
  if (tb->error)
5001
    return FAILURE;
5002
  else if (tb->nopass)
5003
    return SUCCESS;
5004
 
5005
  po = extract_ppc_passed_object (e);
5006
  if (!po)
5007
    return FAILURE;
5008
 
5009
  if (po->rank > 0)
5010
    {
5011
      gfc_error ("Passed-object at %L must be scalar", &e->where);
5012
      return FAILURE;
5013
    }
5014
 
5015
  gcc_assert (tb->pass_arg_num > 0);
5016
  e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5017
                                                  tb->pass_arg_num,
5018
                                                  tb->pass_arg);
5019
 
5020
  return SUCCESS;
5021
}
5022
 
5023
 
5024
/* Check that the object a TBP is called on is valid, i.e. it must not be
5025
   of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
5026
 
5027
static gfc_try
5028
check_typebound_baseobject (gfc_expr* e)
5029
{
5030
  gfc_expr* base;
5031
 
5032
  base = extract_compcall_passed_object (e);
5033
  if (!base)
5034
    return FAILURE;
5035
 
5036
  gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5037
 
5038
  if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5039
    {
5040
      gfc_error ("Base object for type-bound procedure call at %L is of"
5041
                 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5042
      return FAILURE;
5043
    }
5044
 
5045
  /* If the procedure called is NOPASS, the base object must be scalar.  */
5046
  if (e->value.compcall.tbp->nopass && base->rank > 0)
5047
    {
5048
      gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5049
                 " be scalar", &e->where);
5050
      return FAILURE;
5051
    }
5052
 
5053
  /* FIXME: Remove once PR 41177 (this problem) is fixed completely.  */
5054
  if (base->rank > 0)
5055
    {
5056
      gfc_error ("Non-scalar base object at %L currently not implemented",
5057
                 &e->where);
5058
      return FAILURE;
5059
    }
5060
 
5061
  return SUCCESS;
5062
}
5063
 
5064
 
5065
/* Resolve a call to a type-bound procedure, either function or subroutine,
5066
   statically from the data in an EXPR_COMPCALL expression.  The adapted
5067
   arglist and the target-procedure symtree are returned.  */
5068
 
5069
static gfc_try
5070
resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5071
                          gfc_actual_arglist** actual)
5072
{
5073
  gcc_assert (e->expr_type == EXPR_COMPCALL);
5074
  gcc_assert (!e->value.compcall.tbp->is_generic);
5075
 
5076
  /* Update the actual arglist for PASS.  */
5077
  if (update_compcall_arglist (e) == FAILURE)
5078
    return FAILURE;
5079
 
5080
  *actual = e->value.compcall.actual;
5081
  *target = e->value.compcall.tbp->u.specific;
5082
 
5083
  gfc_free_ref_list (e->ref);
5084
  e->ref = NULL;
5085
  e->value.compcall.actual = NULL;
5086
 
5087
  return SUCCESS;
5088
}
5089
 
5090
 
5091
/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5092
   which of the specific bindings (if any) matches the arglist and transform
5093
   the expression into a call of that binding.  */
5094
 
5095
static gfc_try
5096
resolve_typebound_generic_call (gfc_expr* e)
5097
{
5098
  gfc_typebound_proc* genproc;
5099
  const char* genname;
5100
 
5101
  gcc_assert (e->expr_type == EXPR_COMPCALL);
5102
  genname = e->value.compcall.name;
5103
  genproc = e->value.compcall.tbp;
5104
 
5105
  if (!genproc->is_generic)
5106
    return SUCCESS;
5107
 
5108
  /* Try the bindings on this type and in the inheritance hierarchy.  */
5109
  for (; genproc; genproc = genproc->overridden)
5110
    {
5111
      gfc_tbp_generic* g;
5112
 
5113
      gcc_assert (genproc->is_generic);
5114
      for (g = genproc->u.generic; g; g = g->next)
5115
        {
5116
          gfc_symbol* target;
5117
          gfc_actual_arglist* args;
5118
          bool matches;
5119
 
5120
          gcc_assert (g->specific);
5121
 
5122
          if (g->specific->error)
5123
            continue;
5124
 
5125
          target = g->specific->u.specific->n.sym;
5126
 
5127
          /* Get the right arglist by handling PASS/NOPASS.  */
5128
          args = gfc_copy_actual_arglist (e->value.compcall.actual);
5129
          if (!g->specific->nopass)
5130
            {
5131
              gfc_expr* po;
5132
              po = extract_compcall_passed_object (e);
5133
              if (!po)
5134
                return FAILURE;
5135
 
5136
              gcc_assert (g->specific->pass_arg_num > 0);
5137
              gcc_assert (!g->specific->error);
5138
              args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5139
                                          g->specific->pass_arg);
5140
            }
5141
          resolve_actual_arglist (args, target->attr.proc,
5142
                                  is_external_proc (target) && !target->formal);
5143
 
5144
          /* Check if this arglist matches the formal.  */
5145
          matches = gfc_arglist_matches_symbol (&args, target);
5146
 
5147
          /* Clean up and break out of the loop if we've found it.  */
5148
          gfc_free_actual_arglist (args);
5149
          if (matches)
5150
            {
5151
              e->value.compcall.tbp = g->specific;
5152
              goto success;
5153
            }
5154
        }
5155
    }
5156
 
5157
  /* Nothing matching found!  */
5158
  gfc_error ("Found no matching specific binding for the call to the GENERIC"
5159
             " '%s' at %L", genname, &e->where);
5160
  return FAILURE;
5161
 
5162
success:
5163
  return SUCCESS;
5164
}
5165
 
5166
 
5167
/* Resolve a call to a type-bound subroutine.  */
5168
 
5169
static gfc_try
5170
resolve_typebound_call (gfc_code* c)
5171
{
5172
  gfc_actual_arglist* newactual;
5173
  gfc_symtree* target;
5174
 
5175
  /* Check that's really a SUBROUTINE.  */
5176
  if (!c->expr1->value.compcall.tbp->subroutine)
5177
    {
5178
      gfc_error ("'%s' at %L should be a SUBROUTINE",
5179
                 c->expr1->value.compcall.name, &c->loc);
5180
      return FAILURE;
5181
    }
5182
 
5183
  if (check_typebound_baseobject (c->expr1) == FAILURE)
5184
    return FAILURE;
5185
 
5186
  if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5187
    return FAILURE;
5188
 
5189
  /* Transform into an ordinary EXEC_CALL for now.  */
5190
 
5191
  if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5192
    return FAILURE;
5193
 
5194
  c->ext.actual = newactual;
5195
  c->symtree = target;
5196
  c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5197
 
5198
  gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5199
 
5200
  gfc_free_expr (c->expr1);
5201
  c->expr1 = gfc_get_expr ();
5202
  c->expr1->expr_type = EXPR_FUNCTION;
5203
  c->expr1->symtree = target;
5204
  c->expr1->where = c->loc;
5205
 
5206
  return resolve_call (c);
5207
}
5208
 
5209
 
5210
/* Resolve a component-call expression.  This originally was intended
5211
   only to see functions.  However, it is convenient to use it in
5212
   resolving subroutine class methods, since we do not have to add a
5213
   gfc_code each time. */
5214
static gfc_try
5215
resolve_compcall (gfc_expr* e, bool fcn, bool class_members)
5216
{
5217
  gfc_actual_arglist* newactual;
5218
  gfc_symtree* target;
5219
 
5220
  /* Check that's really a FUNCTION.  */
5221
  if (fcn && !e->value.compcall.tbp->function)
5222
    {
5223
      gfc_error ("'%s' at %L should be a FUNCTION",
5224
                 e->value.compcall.name, &e->where);
5225
      return FAILURE;
5226
    }
5227
  else if (!fcn && !e->value.compcall.tbp->subroutine)
5228
    {
5229
      /* To resolve class member calls, we borrow this bit
5230
         of code to select the specific procedures.  */
5231
      gfc_error ("'%s' at %L should be a SUBROUTINE",
5232
                 e->value.compcall.name, &e->where);
5233
      return FAILURE;
5234
    }
5235
 
5236
  /* These must not be assign-calls!  */
5237
  gcc_assert (!e->value.compcall.assign);
5238
 
5239
  if (check_typebound_baseobject (e) == FAILURE)
5240
    return FAILURE;
5241
 
5242
  if (resolve_typebound_generic_call (e) == FAILURE)
5243
    return FAILURE;
5244
  gcc_assert (!e->value.compcall.tbp->is_generic);
5245
 
5246
  /* Take the rank from the function's symbol.  */
5247
  if (e->value.compcall.tbp->u.specific->n.sym->as)
5248
    e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5249
 
5250
  /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5251
     arglist to the TBP's binding target.  */
5252
 
5253
  if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5254
    return FAILURE;
5255
 
5256
  e->value.function.actual = newactual;
5257
  e->value.function.name = NULL;
5258
  e->value.function.esym = target->n.sym;
5259
  e->value.function.class_esym = NULL;
5260
  e->value.function.isym = NULL;
5261
  e->symtree = target;
5262
  e->ts = target->n.sym->ts;
5263
  e->expr_type = EXPR_FUNCTION;
5264
 
5265
  /* Resolution is not necessary when constructing component calls
5266
     for class members, since this must only be done for the
5267
     declared type, which is done afterwards.  */
5268
  return !class_members ? gfc_resolve_expr (e) : SUCCESS;
5269
}
5270
 
5271
 
5272
/* Resolve a typebound call for the members in a class.  This group of
5273
   functions implements dynamic dispatch in the provisional version
5274
   of f03 OOP.  As soon as vtables are in place and contain pointers
5275
   to methods, this will no longer be necessary.  */
5276
static gfc_expr *list_e;
5277
static void check_class_members (gfc_symbol *);
5278
static gfc_try class_try;
5279
static bool fcn_flag;
5280
 
5281
 
5282
static void
5283
check_members (gfc_symbol *derived)
5284
{
5285
  if (derived->attr.flavor == FL_DERIVED)
5286
    check_class_members (derived);
5287
}
5288
 
5289
 
5290
static void
5291
check_class_members (gfc_symbol *derived)
5292
{
5293
  gfc_expr *e;
5294
  gfc_symtree *tbp;
5295
  gfc_class_esym_list *etmp;
5296
 
5297
  e = gfc_copy_expr (list_e);
5298
 
5299
  tbp = gfc_find_typebound_proc (derived, &class_try,
5300
                                 e->value.compcall.name,
5301
                                 false, &e->where);
5302
 
5303
  if (tbp == NULL)
5304
    {
5305
      gfc_error ("no typebound available procedure named '%s' at %L",
5306
                 e->value.compcall.name, &e->where);
5307
      return;
5308
    }
5309
 
5310
  /* If we have to match a passed class member, force the actual
5311
      expression to have the correct type.  */
5312
  if (!tbp->n.tb->nopass)
5313
    {
5314
      if (e->value.compcall.base_object == NULL)
5315
        e->value.compcall.base_object = extract_compcall_passed_object (e);
5316
 
5317
      if (!derived->attr.abstract)
5318
        {
5319
          e->value.compcall.base_object->ts.type = BT_DERIVED;
5320
          e->value.compcall.base_object->ts.u.derived = derived;
5321
        }
5322
    }
5323
 
5324
  e->value.compcall.tbp = tbp->n.tb;
5325
  e->value.compcall.name = tbp->name;
5326
 
5327
  /* Let the original expresssion catch the assertion in
5328
     resolve_compcall, since this flag does not appear to be reset or
5329
     copied in some systems.  */
5330
  e->value.compcall.assign = 0;
5331
 
5332
  /* Do the renaming, PASSing, generic => specific and other
5333
     good things for each class member.  */
5334
  class_try = (resolve_compcall (e, fcn_flag, true) == SUCCESS)
5335
                                ? class_try : FAILURE;
5336
 
5337
  /* Now transfer the found symbol to the esym list.  */
5338
  if (class_try == SUCCESS)
5339
    {
5340
      etmp = list_e->value.function.class_esym;
5341
      list_e->value.function.class_esym
5342
                = gfc_get_class_esym_list();
5343
      list_e->value.function.class_esym->next = etmp;
5344
      list_e->value.function.class_esym->derived = derived;
5345
      list_e->value.function.class_esym->esym
5346
                = e->value.function.esym;
5347
    }
5348
 
5349
  gfc_free_expr (e);
5350
 
5351
  /* Burrow down into grandchildren types.  */
5352
  if (derived->f2k_derived)
5353
    gfc_traverse_ns (derived->f2k_derived, check_members);
5354
}
5355
 
5356
 
5357
/* Eliminate esym_lists where all the members point to the
5358
   typebound procedure of the declared type; ie. one where
5359
   type selection has no effect..  */
5360
static void
5361
resolve_class_esym (gfc_expr *e)
5362
{
5363
  gfc_class_esym_list *p, *q;
5364
  bool empty = true;
5365
 
5366
  gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5367
 
5368
  p = e->value.function.class_esym;
5369
  if (p == NULL)
5370
    return;
5371
 
5372
  for (; p; p = p->next)
5373
    empty = empty && (e->value.function.esym == p->esym);
5374
 
5375
  if (empty)
5376
    {
5377
      p = e->value.function.class_esym;
5378
      for (; p; p = q)
5379
        {
5380
          q = p->next;
5381
          gfc_free (p);
5382
        }
5383
      e->value.function.class_esym = NULL;
5384
   }
5385
}
5386
 
5387
 
5388
/* Generate an expression for the hash value, given the reference to
5389
   the class of the final expression (class_ref), the base of the
5390
   full reference list (new_ref), the declared type and the class
5391
   object (st).  */
5392
static gfc_expr*
5393
hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
5394
{
5395
  gfc_expr *hash_value;
5396
 
5397
  /* Build an expression for the correct hash_value; ie. that of the last
5398
     CLASS reference.  */
5399
  if (class_ref)
5400
    {
5401
      class_ref->next = NULL;
5402
    }
5403
  else
5404
    {
5405
      gfc_free_ref_list (new_ref);
5406
      new_ref = NULL;
5407
    }
5408
  hash_value = gfc_get_expr ();
5409
  hash_value->expr_type = EXPR_VARIABLE;
5410
  hash_value->symtree = st;
5411
  hash_value->symtree->n.sym->refs++;
5412
  hash_value->ref = new_ref;
5413
  gfc_add_component_ref (hash_value, "$vptr");
5414
  gfc_add_component_ref (hash_value, "$hash");
5415
 
5416
  return hash_value;
5417
}
5418
 
5419
 
5420
/* Get the ultimate declared type from an expression.  In addition,
5421
   return the last class/derived type reference and the copy of the
5422
   reference list.  */
5423
static gfc_symbol*
5424
get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5425
                        gfc_expr *e)
5426
{
5427
  gfc_symbol *declared;
5428
  gfc_ref *ref;
5429
 
5430
  declared = NULL;
5431
  *class_ref = NULL;
5432
  *new_ref = gfc_copy_ref (e->ref);
5433
  for (ref = *new_ref; ref; ref = ref->next)
5434
    {
5435
      if (ref->type != REF_COMPONENT)
5436
        continue;
5437
 
5438
      if (ref->u.c.component->ts.type == BT_CLASS
5439
            || ref->u.c.component->ts.type == BT_DERIVED)
5440
        {
5441
          declared = ref->u.c.component->ts.u.derived;
5442
          *class_ref = ref;
5443
        }
5444
    }
5445
 
5446
  if (declared == NULL)
5447
    declared = e->symtree->n.sym->ts.u.derived;
5448
 
5449
  return declared;
5450
}
5451
 
5452
 
5453
/* Resolve the argument expressions so that any arguments expressions
5454
   that include class methods are resolved before the current call.
5455
   This is necessary because of the static variables used in CLASS
5456
   method resolution.  */
5457
static void
5458
resolve_arg_exprs (gfc_actual_arglist *arg)
5459
{
5460
  /* Resolve the actual arglist expressions.  */
5461
  for (; arg; arg = arg->next)
5462
    {
5463
      if (arg->expr)
5464
        gfc_resolve_expr (arg->expr);
5465
    }
5466
}
5467
 
5468
 
5469
/* Resolve a typebound function, or 'method'.  First separate all
5470
   the non-CLASS references by calling resolve_compcall directly.
5471
   Then treat the CLASS references by resolving for each of the class
5472
   members in turn.  */
5473
 
5474
static gfc_try
5475
resolve_typebound_function (gfc_expr* e)
5476
{
5477
  gfc_symbol *derived, *declared;
5478
  gfc_ref *new_ref;
5479
  gfc_ref *class_ref;
5480
  gfc_symtree *st;
5481
 
5482
  st = e->symtree;
5483
  if (st == NULL)
5484
    return resolve_compcall (e, true, false);
5485
 
5486
  /* Get the CLASS declared type.  */
5487
  declared = get_declared_from_expr (&class_ref, &new_ref, e);
5488
 
5489
  /* Weed out cases of the ultimate component being a derived type.  */
5490
  if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5491
        || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5492
    {
5493
      gfc_free_ref_list (new_ref);
5494
      return resolve_compcall (e, true, false);
5495
    }
5496
 
5497
  /* Resolve the argument expressions,  */
5498
  resolve_arg_exprs (e->value.function.actual);
5499
 
5500
  /* Get the data component, which is of the declared type.  */
5501
  derived = declared->components->ts.u.derived;
5502
 
5503
  /* Resolve the function call for each member of the class.  */
5504
  class_try = SUCCESS;
5505
  fcn_flag = true;
5506
  list_e = gfc_copy_expr (e);
5507
  check_class_members (derived);
5508
 
5509
  class_try = (resolve_compcall (e, true, false) == SUCCESS)
5510
                 ? class_try : FAILURE;
5511
 
5512
  /* Transfer the class list to the original expression.  Note that
5513
     the class_esym list is cleaned up in trans-expr.c, as the calls
5514
     are translated.  */
5515
  e->value.function.class_esym = list_e->value.function.class_esym;
5516
  list_e->value.function.class_esym = NULL;
5517
  gfc_free_expr (list_e);
5518
 
5519
  resolve_class_esym (e);
5520
 
5521
  /* More than one typebound procedure so transmit an expression for
5522
     the hash_value as the selector.  */
5523
  if (e->value.function.class_esym != NULL)
5524
    e->value.function.class_esym->hash_value
5525
                = hash_value_expr (class_ref, new_ref, st);
5526
 
5527
  return class_try;
5528
}
5529
 
5530
/* Resolve a typebound subroutine, or 'method'.  First separate all
5531
   the non-CLASS references by calling resolve_typebound_call directly.
5532
   Then treat the CLASS references by resolving for each of the class
5533
   members in turn.  */
5534
 
5535
static gfc_try
5536
resolve_typebound_subroutine (gfc_code *code)
5537
{
5538
  gfc_symbol *derived, *declared;
5539
  gfc_ref *new_ref;
5540
  gfc_ref *class_ref;
5541
  gfc_symtree *st;
5542
 
5543
  st = code->expr1->symtree;
5544
  if (st == NULL)
5545
    return resolve_typebound_call (code);
5546
 
5547
  /* Get the CLASS declared type.  */
5548
  declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5549
 
5550
  /* Weed out cases of the ultimate component being a derived type.  */
5551
  if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5552
        || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5553
    {
5554
      gfc_free_ref_list (new_ref);
5555
      return resolve_typebound_call (code);
5556
    }
5557
 
5558
  /* Resolve the argument expressions,  */
5559
  resolve_arg_exprs (code->expr1->value.compcall.actual);
5560
 
5561
  /* Get the data component, which is of the declared type.  */
5562
  derived = declared->components->ts.u.derived;
5563
 
5564
  class_try = SUCCESS;
5565
  fcn_flag = false;
5566
  list_e = gfc_copy_expr (code->expr1);
5567
  check_class_members (derived);
5568
 
5569
  class_try = (resolve_typebound_call (code) == SUCCESS)
5570
                 ? class_try : FAILURE;
5571
 
5572
  /* Transfer the class list to the original expression.  Note that
5573
     the class_esym list is cleaned up in trans-expr.c, as the calls
5574
     are translated.  */
5575
  code->expr1->value.function.class_esym
5576
                        = list_e->value.function.class_esym;
5577
  list_e->value.function.class_esym = NULL;
5578
  gfc_free_expr (list_e);
5579
 
5580
  resolve_class_esym (code->expr1);
5581
 
5582
  /* More than one typebound procedure so transmit an expression for
5583
     the hash_value as the selector.  */
5584
  if (code->expr1->value.function.class_esym != NULL)
5585
    code->expr1->value.function.class_esym->hash_value
5586
                = hash_value_expr (class_ref, new_ref, st);
5587
 
5588
  return class_try;
5589
}
5590
 
5591
 
5592
/* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
5593
 
5594
static gfc_try
5595
resolve_ppc_call (gfc_code* c)
5596
{
5597
  gfc_component *comp;
5598
  bool b;
5599
 
5600
  b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5601
  gcc_assert (b);
5602
 
5603
  c->resolved_sym = c->expr1->symtree->n.sym;
5604
  c->expr1->expr_type = EXPR_VARIABLE;
5605
 
5606
  if (!comp->attr.subroutine)
5607
    gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5608
 
5609
  if (resolve_ref (c->expr1) == FAILURE)
5610
    return FAILURE;
5611
 
5612
  if (update_ppc_arglist (c->expr1) == FAILURE)
5613
    return FAILURE;
5614
 
5615
  c->ext.actual = c->expr1->value.compcall.actual;
5616
 
5617
  if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5618
                              comp->formal == NULL) == FAILURE)
5619
    return FAILURE;
5620
 
5621
  gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5622
 
5623
  return SUCCESS;
5624
}
5625
 
5626
 
5627
/* Resolve a Function Call to a Procedure Pointer Component (Function).  */
5628
 
5629
static gfc_try
5630
resolve_expr_ppc (gfc_expr* e)
5631
{
5632
  gfc_component *comp;
5633
  bool b;
5634
 
5635
  b = gfc_is_proc_ptr_comp (e, &comp);
5636
  gcc_assert (b);
5637
 
5638
  /* Convert to EXPR_FUNCTION.  */
5639
  e->expr_type = EXPR_FUNCTION;
5640
  e->value.function.isym = NULL;
5641
  e->value.function.actual = e->value.compcall.actual;
5642
  e->ts = comp->ts;
5643
  if (comp->as != NULL)
5644
    e->rank = comp->as->rank;
5645
 
5646
  if (!comp->attr.function)
5647
    gfc_add_function (&comp->attr, comp->name, &e->where);
5648
 
5649
  if (resolve_ref (e) == FAILURE)
5650
    return FAILURE;
5651
 
5652
  if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5653
                              comp->formal == NULL) == FAILURE)
5654
    return FAILURE;
5655
 
5656
  if (update_ppc_arglist (e) == FAILURE)
5657
    return FAILURE;
5658
 
5659
  gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5660
 
5661
  return SUCCESS;
5662
}
5663
 
5664
 
5665
static bool
5666
gfc_is_expandable_expr (gfc_expr *e)
5667
{
5668
  gfc_constructor *con;
5669
 
5670
  if (e->expr_type == EXPR_ARRAY)
5671
    {
5672
      /* Traverse the constructor looking for variables that are flavor
5673
         parameter.  Parameters must be expanded since they are fully used at
5674
         compile time.  */
5675
      for (con = e->value.constructor; con; con = con->next)
5676
        {
5677
          if (con->expr->expr_type == EXPR_VARIABLE
5678
          && con->expr->symtree
5679
          && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5680
              || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5681
            return true;
5682
          if (con->expr->expr_type == EXPR_ARRAY
5683
            && gfc_is_expandable_expr (con->expr))
5684
            return true;
5685
        }
5686
    }
5687
 
5688
  return false;
5689
}
5690
 
5691
/* Resolve an expression.  That is, make sure that types of operands agree
5692
   with their operators, intrinsic operators are converted to function calls
5693
   for overloaded types and unresolved function references are resolved.  */
5694
 
5695
gfc_try
5696
gfc_resolve_expr (gfc_expr *e)
5697
{
5698
  gfc_try t;
5699
 
5700
  if (e == NULL)
5701
    return SUCCESS;
5702
 
5703
  switch (e->expr_type)
5704
    {
5705
    case EXPR_OP:
5706
      t = resolve_operator (e);
5707
      break;
5708
 
5709
    case EXPR_FUNCTION:
5710
    case EXPR_VARIABLE:
5711
 
5712
      if (check_host_association (e))
5713
        t = resolve_function (e);
5714
      else
5715
        {
5716
          t = resolve_variable (e);
5717
          if (t == SUCCESS)
5718
            expression_rank (e);
5719
        }
5720
 
5721
      if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5722
          && e->ref->type != REF_SUBSTRING)
5723
        gfc_resolve_substring_charlen (e);
5724
 
5725
      break;
5726
 
5727
    case EXPR_COMPCALL:
5728
      t = resolve_typebound_function (e);
5729
      break;
5730
 
5731
    case EXPR_SUBSTRING:
5732
      t = resolve_ref (e);
5733
      break;
5734
 
5735
    case EXPR_CONSTANT:
5736
    case EXPR_NULL:
5737
      t = SUCCESS;
5738
      break;
5739
 
5740
    case EXPR_PPC:
5741
      t = resolve_expr_ppc (e);
5742
      break;
5743
 
5744
    case EXPR_ARRAY:
5745
      t = FAILURE;
5746
      if (resolve_ref (e) == FAILURE)
5747
        break;
5748
 
5749
      t = gfc_resolve_array_constructor (e);
5750
      /* Also try to expand a constructor.  */
5751
      if (t == SUCCESS)
5752
        {
5753
          expression_rank (e);
5754
          if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5755
            gfc_expand_constructor (e);
5756
        }
5757
 
5758
      /* This provides the opportunity for the length of constructors with
5759
         character valued function elements to propagate the string length
5760
         to the expression.  */
5761
      if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5762
        {
5763
          /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5764
             here rather then add a duplicate test for it above.  */
5765
          gfc_expand_constructor (e);
5766
          t = gfc_resolve_character_array_constructor (e);
5767
        }
5768
 
5769
      break;
5770
 
5771
    case EXPR_STRUCTURE:
5772
      t = resolve_ref (e);
5773
      if (t == FAILURE)
5774
        break;
5775
 
5776
      t = resolve_structure_cons (e);
5777
      if (t == FAILURE)
5778
        break;
5779
 
5780
      t = gfc_simplify_expr (e, 0);
5781
      break;
5782
 
5783
    default:
5784
      gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5785
    }
5786
 
5787
  if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5788
    fixup_charlen (e);
5789
 
5790
  return t;
5791
}
5792
 
5793
 
5794
/* Resolve an expression from an iterator.  They must be scalar and have
5795
   INTEGER or (optionally) REAL type.  */
5796
 
5797
static gfc_try
5798
gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5799
                           const char *name_msgid)
5800
{
5801
  if (gfc_resolve_expr (expr) == FAILURE)
5802
    return FAILURE;
5803
 
5804
  if (expr->rank != 0)
5805
    {
5806
      gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5807
      return FAILURE;
5808
    }
5809
 
5810
  if (expr->ts.type != BT_INTEGER)
5811
    {
5812
      if (expr->ts.type == BT_REAL)
5813
        {
5814
          if (real_ok)
5815
            return gfc_notify_std (GFC_STD_F95_DEL,
5816
                                   "Deleted feature: %s at %L must be integer",
5817
                                   _(name_msgid), &expr->where);
5818
          else
5819
            {
5820
              gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5821
                         &expr->where);
5822
              return FAILURE;
5823
            }
5824
        }
5825
      else
5826
        {
5827
          gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5828
          return FAILURE;
5829
        }
5830
    }
5831
  return SUCCESS;
5832
}
5833
 
5834
 
5835
/* Resolve the expressions in an iterator structure.  If REAL_OK is
5836
   false allow only INTEGER type iterators, otherwise allow REAL types.  */
5837
 
5838
gfc_try
5839
gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5840
{
5841
  if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5842
      == FAILURE)
5843
    return FAILURE;
5844
 
5845
  if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5846
    {
5847
      gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5848
                 &iter->var->where);
5849
      return FAILURE;
5850
    }
5851
 
5852
  if (gfc_resolve_iterator_expr (iter->start, real_ok,
5853
                                 "Start expression in DO loop") == FAILURE)
5854
    return FAILURE;
5855
 
5856
  if (gfc_resolve_iterator_expr (iter->end, real_ok,
5857
                                 "End expression in DO loop") == FAILURE)
5858
    return FAILURE;
5859
 
5860
  if (gfc_resolve_iterator_expr (iter->step, real_ok,
5861
                                 "Step expression in DO loop") == FAILURE)
5862
    return FAILURE;
5863
 
5864
  if (iter->step->expr_type == EXPR_CONSTANT)
5865
    {
5866
      if ((iter->step->ts.type == BT_INTEGER
5867
           && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5868
          || (iter->step->ts.type == BT_REAL
5869
              && mpfr_sgn (iter->step->value.real) == 0))
5870
        {
5871
          gfc_error ("Step expression in DO loop at %L cannot be zero",
5872
                     &iter->step->where);
5873
          return FAILURE;
5874
        }
5875
    }
5876
 
5877
  /* Convert start, end, and step to the same type as var.  */
5878
  if (iter->start->ts.kind != iter->var->ts.kind
5879
      || iter->start->ts.type != iter->var->ts.type)
5880
    gfc_convert_type (iter->start, &iter->var->ts, 2);
5881
 
5882
  if (iter->end->ts.kind != iter->var->ts.kind
5883
      || iter->end->ts.type != iter->var->ts.type)
5884
    gfc_convert_type (iter->end, &iter->var->ts, 2);
5885
 
5886
  if (iter->step->ts.kind != iter->var->ts.kind
5887
      || iter->step->ts.type != iter->var->ts.type)
5888
    gfc_convert_type (iter->step, &iter->var->ts, 2);
5889
 
5890
  if (iter->start->expr_type == EXPR_CONSTANT
5891
      && iter->end->expr_type == EXPR_CONSTANT
5892
      && iter->step->expr_type == EXPR_CONSTANT)
5893
    {
5894
      int sgn, cmp;
5895
      if (iter->start->ts.type == BT_INTEGER)
5896
        {
5897
          sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5898
          cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5899
        }
5900
      else
5901
        {
5902
          sgn = mpfr_sgn (iter->step->value.real);
5903
          cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5904
        }
5905
      if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5906
        gfc_warning ("DO loop at %L will be executed zero times",
5907
                     &iter->step->where);
5908
    }
5909
 
5910
  return SUCCESS;
5911
}
5912
 
5913
 
5914
/* Traversal function for find_forall_index.  f == 2 signals that
5915
   that variable itself is not to be checked - only the references.  */
5916
 
5917
static bool
5918
forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5919
{
5920
  if (expr->expr_type != EXPR_VARIABLE)
5921
    return false;
5922
 
5923
  /* A scalar assignment  */
5924
  if (!expr->ref || *f == 1)
5925
    {
5926
      if (expr->symtree->n.sym == sym)
5927
        return true;
5928
      else
5929
        return false;
5930
    }
5931
 
5932
  if (*f == 2)
5933
    *f = 1;
5934
  return false;
5935
}
5936
 
5937
 
5938
/* Check whether the FORALL index appears in the expression or not.
5939
   Returns SUCCESS if SYM is found in EXPR.  */
5940
 
5941
gfc_try
5942
find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5943
{
5944
  if (gfc_traverse_expr (expr, sym, forall_index, f))
5945
    return SUCCESS;
5946
  else
5947
    return FAILURE;
5948
}
5949
 
5950
 
5951
/* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
5952
   to be a scalar INTEGER variable.  The subscripts and stride are scalar
5953
   INTEGERs, and if stride is a constant it must be nonzero.
5954
   Furthermore "A subscript or stride in a forall-triplet-spec shall
5955
   not contain a reference to any index-name in the
5956
   forall-triplet-spec-list in which it appears." (7.5.4.1)  */
5957
 
5958
static void
5959
resolve_forall_iterators (gfc_forall_iterator *it)
5960
{
5961
  gfc_forall_iterator *iter, *iter2;
5962
 
5963
  for (iter = it; iter; iter = iter->next)
5964
    {
5965
      if (gfc_resolve_expr (iter->var) == SUCCESS
5966
          && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5967
        gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5968
                   &iter->var->where);
5969
 
5970
      if (gfc_resolve_expr (iter->start) == SUCCESS
5971
          && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5972
        gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5973
                   &iter->start->where);
5974
      if (iter->var->ts.kind != iter->start->ts.kind)
5975
        gfc_convert_type (iter->start, &iter->var->ts, 2);
5976
 
5977
      if (gfc_resolve_expr (iter->end) == SUCCESS
5978
          && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5979
        gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5980
                   &iter->end->where);
5981
      if (iter->var->ts.kind != iter->end->ts.kind)
5982
        gfc_convert_type (iter->end, &iter->var->ts, 2);
5983
 
5984
      if (gfc_resolve_expr (iter->stride) == SUCCESS)
5985
        {
5986
          if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5987
            gfc_error ("FORALL stride expression at %L must be a scalar %s",
5988
                       &iter->stride->where, "INTEGER");
5989
 
5990
          if (iter->stride->expr_type == EXPR_CONSTANT
5991
              && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5992
            gfc_error ("FORALL stride expression at %L cannot be zero",
5993
                       &iter->stride->where);
5994
        }
5995
      if (iter->var->ts.kind != iter->stride->ts.kind)
5996
        gfc_convert_type (iter->stride, &iter->var->ts, 2);
5997
    }
5998
 
5999
  for (iter = it; iter; iter = iter->next)
6000
    for (iter2 = iter; iter2; iter2 = iter2->next)
6001
      {
6002
        if (find_forall_index (iter2->start,
6003
                               iter->var->symtree->n.sym, 0) == SUCCESS
6004
            || find_forall_index (iter2->end,
6005
                                  iter->var->symtree->n.sym, 0) == SUCCESS
6006
            || find_forall_index (iter2->stride,
6007
                                  iter->var->symtree->n.sym, 0) == SUCCESS)
6008
          gfc_error ("FORALL index '%s' may not appear in triplet "
6009
                     "specification at %L", iter->var->symtree->name,
6010
                     &iter2->start->where);
6011
      }
6012
}
6013
 
6014
 
6015
/* Given a pointer to a symbol that is a derived type, see if it's
6016
   inaccessible, i.e. if it's defined in another module and the components are
6017
   PRIVATE.  The search is recursive if necessary.  Returns zero if no
6018
   inaccessible components are found, nonzero otherwise.  */
6019
 
6020
static int
6021
derived_inaccessible (gfc_symbol *sym)
6022
{
6023
  gfc_component *c;
6024
 
6025
  if (sym->attr.use_assoc && sym->attr.private_comp)
6026
    return 1;
6027
 
6028
  for (c = sym->components; c; c = c->next)
6029
    {
6030
        if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6031
          return 1;
6032
    }
6033
 
6034
  return 0;
6035
}
6036
 
6037
 
6038
/* Resolve the argument of a deallocate expression.  The expression must be
6039
   a pointer or a full array.  */
6040
 
6041
static gfc_try
6042
resolve_deallocate_expr (gfc_expr *e)
6043
{
6044
  symbol_attribute attr;
6045
  int allocatable, pointer, check_intent_in;
6046
  gfc_ref *ref;
6047
  gfc_symbol *sym;
6048
  gfc_component *c;
6049
 
6050
  /* Check INTENT(IN), unless the object is a sub-component of a pointer.  */
6051
  check_intent_in = 1;
6052
 
6053
  if (gfc_resolve_expr (e) == FAILURE)
6054
    return FAILURE;
6055
 
6056
  if (e->expr_type != EXPR_VARIABLE)
6057
    goto bad;
6058
 
6059
  sym = e->symtree->n.sym;
6060
 
6061
  if (sym->ts.type == BT_CLASS)
6062
    {
6063
      allocatable = sym->ts.u.derived->components->attr.allocatable;
6064
      pointer = sym->ts.u.derived->components->attr.pointer;
6065
    }
6066
  else
6067
    {
6068
      allocatable = sym->attr.allocatable;
6069
      pointer = sym->attr.pointer;
6070
    }
6071
  for (ref = e->ref; ref; ref = ref->next)
6072
    {
6073
      if (pointer)
6074
        check_intent_in = 0;
6075
 
6076
      switch (ref->type)
6077
        {
6078
        case REF_ARRAY:
6079
          if (ref->u.ar.type != AR_FULL)
6080
            allocatable = 0;
6081
          break;
6082
 
6083
        case REF_COMPONENT:
6084
          c = ref->u.c.component;
6085
          if (c->ts.type == BT_CLASS)
6086
            {
6087
              allocatable = c->ts.u.derived->components->attr.allocatable;
6088
              pointer = c->ts.u.derived->components->attr.pointer;
6089
            }
6090
          else
6091
            {
6092
              allocatable = c->attr.allocatable;
6093
              pointer = c->attr.pointer;
6094
            }
6095
          break;
6096
 
6097
        case REF_SUBSTRING:
6098
          allocatable = 0;
6099
          break;
6100
        }
6101
    }
6102
 
6103
  attr = gfc_expr_attr (e);
6104
 
6105
  if (allocatable == 0 && attr.pointer == 0)
6106
    {
6107
    bad:
6108
      gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6109
                 &e->where);
6110
    }
6111
 
6112
  if (check_intent_in && sym->attr.intent == INTENT_IN)
6113
    {
6114
      gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
6115
                 sym->name, &e->where);
6116
      return FAILURE;
6117
    }
6118
 
6119
  if (e->ts.type == BT_CLASS)
6120
    {
6121
      /* Only deallocate the DATA component.  */
6122
      gfc_add_component_ref (e, "$data");
6123
    }
6124
 
6125
  return SUCCESS;
6126
}
6127
 
6128
 
6129
/* Returns true if the expression e contains a reference to the symbol sym.  */
6130
static bool
6131
sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6132
{
6133
  if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6134
    return true;
6135
 
6136
  return false;
6137
}
6138
 
6139
bool
6140
gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6141
{
6142
  return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6143
}
6144
 
6145
 
6146
/* Given the expression node e for an allocatable/pointer of derived type to be
6147
   allocated, get the expression node to be initialized afterwards (needed for
6148
   derived types with default initializers, and derived types with allocatable
6149
   components that need nullification.)  */
6150
 
6151
gfc_expr *
6152
gfc_expr_to_initialize (gfc_expr *e)
6153
{
6154
  gfc_expr *result;
6155
  gfc_ref *ref;
6156
  int i;
6157
 
6158
  result = gfc_copy_expr (e);
6159
 
6160
  /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
6161
  for (ref = result->ref; ref; ref = ref->next)
6162
    if (ref->type == REF_ARRAY && ref->next == NULL)
6163
      {
6164
        ref->u.ar.type = AR_FULL;
6165
 
6166
        for (i = 0; i < ref->u.ar.dimen; i++)
6167
          ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6168
 
6169
        result->rank = ref->u.ar.dimen;
6170
        break;
6171
      }
6172
 
6173
  return result;
6174
}
6175
 
6176
 
6177
/* Used in resolve_allocate_expr to check that a allocation-object and
6178
   a source-expr are conformable.  This does not catch all possible
6179
   cases; in particular a runtime checking is needed.  */
6180
 
6181
static gfc_try
6182
conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6183
{
6184
  /* First compare rank.  */
6185
  if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6186
    {
6187
      gfc_error ("Source-expr at %L must be scalar or have the "
6188
                 "same rank as the allocate-object at %L",
6189
                 &e1->where, &e2->where);
6190
      return FAILURE;
6191
    }
6192
 
6193
  if (e1->shape)
6194
    {
6195
      int i;
6196
      mpz_t s;
6197
 
6198
      mpz_init (s);
6199
 
6200
      for (i = 0; i < e1->rank; i++)
6201
        {
6202
          if (e2->ref->u.ar.end[i])
6203
            {
6204
              mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6205
              mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6206
              mpz_add_ui (s, s, 1);
6207
            }
6208
          else
6209
            {
6210
              mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6211
            }
6212
 
6213
          if (mpz_cmp (e1->shape[i], s) != 0)
6214
            {
6215
              gfc_error ("Source-expr at %L and allocate-object at %L must "
6216
                         "have the same shape", &e1->where, &e2->where);
6217
              mpz_clear (s);
6218
              return FAILURE;
6219
            }
6220
        }
6221
 
6222
      mpz_clear (s);
6223
    }
6224
 
6225
  return SUCCESS;
6226
}
6227
 
6228
 
6229
/* Resolve the expression in an ALLOCATE statement, doing the additional
6230
   checks to see whether the expression is OK or not.  The expression must
6231
   have a trailing array reference that gives the size of the array.  */
6232
 
6233
static gfc_try
6234
resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6235
{
6236
  int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6237
  symbol_attribute attr;
6238
  gfc_ref *ref, *ref2;
6239
  gfc_array_ref *ar;
6240
  gfc_symbol *sym;
6241
  gfc_alloc *a;
6242
  gfc_component *c;
6243
  gfc_expr *init_e;
6244
 
6245
  /* Check INTENT(IN), unless the object is a sub-component of a pointer.  */
6246
  check_intent_in = 1;
6247
 
6248
  if (gfc_resolve_expr (e) == FAILURE)
6249
    return FAILURE;
6250
 
6251
  /* Make sure the expression is allocatable or a pointer.  If it is
6252
     pointer, the next-to-last reference must be a pointer.  */
6253
 
6254
  ref2 = NULL;
6255
  if (e->symtree)
6256
    sym = e->symtree->n.sym;
6257
 
6258
  /* Check whether ultimate component is abstract and CLASS.  */
6259
  is_abstract = 0;
6260
 
6261
  if (e->expr_type != EXPR_VARIABLE)
6262
    {
6263
      allocatable = 0;
6264
      attr = gfc_expr_attr (e);
6265
      pointer = attr.pointer;
6266
      dimension = attr.dimension;
6267
    }
6268
  else
6269
    {
6270
      if (sym->ts.type == BT_CLASS)
6271
        {
6272
          allocatable = sym->ts.u.derived->components->attr.allocatable;
6273
          pointer = sym->ts.u.derived->components->attr.pointer;
6274
          dimension = sym->ts.u.derived->components->attr.dimension;
6275
          is_abstract = sym->ts.u.derived->components->attr.abstract;
6276
        }
6277
      else
6278
        {
6279
          allocatable = sym->attr.allocatable;
6280
          pointer = sym->attr.pointer;
6281
          dimension = sym->attr.dimension;
6282
        }
6283
 
6284
      for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6285
        {
6286
          if (pointer)
6287
            check_intent_in = 0;
6288
 
6289
          switch (ref->type)
6290
            {
6291
              case REF_ARRAY:
6292
                if (ref->next != NULL)
6293
                  pointer = 0;
6294
                break;
6295
 
6296
              case REF_COMPONENT:
6297
                c = ref->u.c.component;
6298
                if (c->ts.type == BT_CLASS)
6299
                  {
6300
                    allocatable = c->ts.u.derived->components->attr.allocatable;
6301
                    pointer = c->ts.u.derived->components->attr.pointer;
6302
                    dimension = c->ts.u.derived->components->attr.dimension;
6303
                    is_abstract = c->ts.u.derived->components->attr.abstract;
6304
                  }
6305
                else
6306
                  {
6307
                    allocatable = c->attr.allocatable;
6308
                    pointer = c->attr.pointer;
6309
                    dimension = c->attr.dimension;
6310
                    is_abstract = c->attr.abstract;
6311
                  }
6312
                break;
6313
 
6314
              case REF_SUBSTRING:
6315
                allocatable = 0;
6316
                pointer = 0;
6317
                break;
6318
            }
6319
        }
6320
    }
6321
 
6322
  if (allocatable == 0 && pointer == 0)
6323
    {
6324
      gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6325
                 &e->where);
6326
      return FAILURE;
6327
    }
6328
 
6329
  /* Some checks for the SOURCE tag.  */
6330
  if (code->expr3)
6331
    {
6332
      /* Check F03:C631.  */
6333
      if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6334
        {
6335
          gfc_error ("Type of entity at %L is type incompatible with "
6336
                      "source-expr at %L", &e->where, &code->expr3->where);
6337
          return FAILURE;
6338
        }
6339
 
6340
      /* Check F03:C632 and restriction following Note 6.18.  */
6341
      if (code->expr3->rank > 0
6342
          && conformable_arrays (code->expr3, e) == FAILURE)
6343
        return FAILURE;
6344
 
6345
      /* Check F03:C633.  */
6346
      if (code->expr3->ts.kind != e->ts.kind)
6347
        {
6348
          gfc_error ("The allocate-object at %L and the source-expr at %L "
6349
                      "shall have the same kind type parameter",
6350
                      &e->where, &code->expr3->where);
6351
          return FAILURE;
6352
        }
6353
    }
6354
  else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6355
    {
6356
      gcc_assert (e->ts.type == BT_CLASS);
6357
      gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6358
                 "type-spec or SOURCE=", sym->name, &e->where);
6359
      return FAILURE;
6360
    }
6361
 
6362
  if (check_intent_in && sym->attr.intent == INTENT_IN)
6363
    {
6364
      gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6365
                 sym->name, &e->where);
6366
      return FAILURE;
6367
    }
6368
 
6369
  if (!code->expr3)
6370
    {
6371
      /* Add default initializer for those derived types that need them.  */
6372
      if (e->ts.type == BT_DERIVED
6373
          && (init_e = gfc_default_initializer (&e->ts)))
6374
        {
6375
          gfc_code *init_st = gfc_get_code ();
6376
          init_st->loc = code->loc;
6377
          init_st->op = EXEC_INIT_ASSIGN;
6378
          init_st->expr1 = gfc_expr_to_initialize (e);
6379
          init_st->expr2 = init_e;
6380
          init_st->next = code->next;
6381
          code->next = init_st;
6382
        }
6383
      else if (e->ts.type == BT_CLASS
6384
               && ((code->ext.alloc.ts.type == BT_UNKNOWN
6385
                    && (init_e = gfc_default_initializer (&e->ts.u.derived->components->ts)))
6386
                   || (code->ext.alloc.ts.type == BT_DERIVED
6387
                       && (init_e = gfc_default_initializer (&code->ext.alloc.ts)))))
6388
        {
6389
          gfc_code *init_st = gfc_get_code ();
6390
          init_st->loc = code->loc;
6391
          init_st->op = EXEC_INIT_ASSIGN;
6392
          init_st->expr1 = gfc_expr_to_initialize (e);
6393
          init_st->expr2 = init_e;
6394
          init_st->next = code->next;
6395
          code->next = init_st;
6396
        }
6397
    }
6398
 
6399
  if (pointer || dimension == 0)
6400
    return SUCCESS;
6401
 
6402
  /* Make sure the next-to-last reference node is an array specification.  */
6403
 
6404
  if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6405
    {
6406
      gfc_error ("Array specification required in ALLOCATE statement "
6407
                 "at %L", &e->where);
6408
      return FAILURE;
6409
    }
6410
 
6411
  /* Make sure that the array section reference makes sense in the
6412
    context of an ALLOCATE specification.  */
6413
 
6414
  ar = &ref2->u.ar;
6415
 
6416
  for (i = 0; i < ar->dimen; i++)
6417
    {
6418
      if (ref2->u.ar.type == AR_ELEMENT)
6419
        goto check_symbols;
6420
 
6421
      switch (ar->dimen_type[i])
6422
        {
6423
        case DIMEN_ELEMENT:
6424
          break;
6425
 
6426
        case DIMEN_RANGE:
6427
          if (ar->start[i] != NULL
6428
              && ar->end[i] != NULL
6429
              && ar->stride[i] == NULL)
6430
            break;
6431
 
6432
          /* Fall Through...  */
6433
 
6434
        case DIMEN_UNKNOWN:
6435
        case DIMEN_VECTOR:
6436
          gfc_error ("Bad array specification in ALLOCATE statement at %L",
6437
                     &e->where);
6438
          return FAILURE;
6439
        }
6440
 
6441
check_symbols:
6442
 
6443
      for (a = code->ext.alloc.list; a; a = a->next)
6444
        {
6445
          sym = a->expr->symtree->n.sym;
6446
 
6447
          /* TODO - check derived type components.  */
6448
          if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6449
            continue;
6450
 
6451
          if ((ar->start[i] != NULL
6452
               && gfc_find_sym_in_expr (sym, ar->start[i]))
6453
              || (ar->end[i] != NULL
6454
                  && gfc_find_sym_in_expr (sym, ar->end[i])))
6455
            {
6456
              gfc_error ("'%s' must not appear in the array specification at "
6457
                         "%L in the same ALLOCATE statement where it is "
6458
                         "itself allocated", sym->name, &ar->where);
6459
              return FAILURE;
6460
            }
6461
        }
6462
    }
6463
 
6464
  return SUCCESS;
6465
}
6466
 
6467
static void
6468
resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6469
{
6470
  gfc_expr *stat, *errmsg, *pe, *qe;
6471
  gfc_alloc *a, *p, *q;
6472
 
6473
  stat = code->expr1 ? code->expr1 : NULL;
6474
 
6475
  errmsg = code->expr2 ? code->expr2 : NULL;
6476
 
6477
  /* Check the stat variable.  */
6478
  if (stat)
6479
    {
6480
      if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6481
        gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6482
                   stat->symtree->n.sym->name, &stat->where);
6483
 
6484
      if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6485
        gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6486
                   &stat->where);
6487
 
6488
      if ((stat->ts.type != BT_INTEGER
6489
           && !(stat->ref && (stat->ref->type == REF_ARRAY
6490
                              || stat->ref->type == REF_COMPONENT)))
6491
          || stat->rank > 0)
6492
        gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6493
                   "variable", &stat->where);
6494
 
6495
      for (p = code->ext.alloc.list; p; p = p->next)
6496
        if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6497
          {
6498
            gfc_ref *ref1, *ref2;
6499
            bool found = true;
6500
 
6501
            for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6502
                 ref1 = ref1->next, ref2 = ref2->next)
6503
              {
6504
                if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6505
                  continue;
6506
                if (ref1->u.c.component->name != ref2->u.c.component->name)
6507
                  {
6508
                    found = false;
6509
                    break;
6510
                  }
6511
              }
6512
 
6513
            if (found)
6514
              {
6515
                gfc_error ("Stat-variable at %L shall not be %sd within "
6516
                           "the same %s statement", &stat->where, fcn, fcn);
6517
                break;
6518
              }
6519
          }
6520
    }
6521
 
6522
  /* Check the errmsg variable.  */
6523
  if (errmsg)
6524
    {
6525
      if (!stat)
6526
        gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6527
                     &errmsg->where);
6528
 
6529
      if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6530
        gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6531
                   errmsg->symtree->n.sym->name, &errmsg->where);
6532
 
6533
      if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6534
        gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6535
                   &errmsg->where);
6536
 
6537
      if ((errmsg->ts.type != BT_CHARACTER
6538
           && !(errmsg->ref
6539
                && (errmsg->ref->type == REF_ARRAY
6540
                    || errmsg->ref->type == REF_COMPONENT)))
6541
          || errmsg->rank > 0 )
6542
        gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6543
                   "variable", &errmsg->where);
6544
 
6545
      for (p = code->ext.alloc.list; p; p = p->next)
6546
        if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6547
          {
6548
            gfc_ref *ref1, *ref2;
6549
            bool found = true;
6550
 
6551
            for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
6552
                 ref1 = ref1->next, ref2 = ref2->next)
6553
              {
6554
                if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6555
                  continue;
6556
                if (ref1->u.c.component->name != ref2->u.c.component->name)
6557
                  {
6558
                    found = false;
6559
                    break;
6560
                  }
6561
              }
6562
 
6563
            if (found)
6564
              {
6565
                gfc_error ("Errmsg-variable at %L shall not be %sd within "
6566
                           "the same %s statement", &errmsg->where, fcn, fcn);
6567
                break;
6568
              }
6569
          }
6570
    }
6571
 
6572
  /* Check that an allocate-object appears only once in the statement.
6573
     FIXME: Checking derived types is disabled.  */
6574
  for (p = code->ext.alloc.list; p; p = p->next)
6575
    {
6576
      pe = p->expr;
6577
      if ((pe->ref && pe->ref->type != REF_COMPONENT)
6578
           && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6579
        {
6580
          for (q = p->next; q; q = q->next)
6581
            {
6582
              qe = q->expr;
6583
              if ((qe->ref && qe->ref->type != REF_COMPONENT)
6584
                  && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6585
                  && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6586
                gfc_error ("Allocate-object at %L also appears at %L",
6587
                           &pe->where, &qe->where);
6588
            }
6589
        }
6590
    }
6591
 
6592
  if (strcmp (fcn, "ALLOCATE") == 0)
6593
    {
6594
      for (a = code->ext.alloc.list; a; a = a->next)
6595
        resolve_allocate_expr (a->expr, code);
6596
    }
6597
  else
6598
    {
6599
      for (a = code->ext.alloc.list; a; a = a->next)
6600
        resolve_deallocate_expr (a->expr);
6601
    }
6602
}
6603
 
6604
 
6605
/************ SELECT CASE resolution subroutines ************/
6606
 
6607
/* Callback function for our mergesort variant.  Determines interval
6608
   overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6609
   op1 > op2.  Assumes we're not dealing with the default case.
6610
   We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6611
   There are nine situations to check.  */
6612
 
6613
static int
6614
compare_cases (const gfc_case *op1, const gfc_case *op2)
6615
{
6616
  int retval;
6617
 
6618
  if (op1->low == NULL) /* op1 = (:L)  */
6619
    {
6620
      /* op2 = (:N), so overlap.  */
6621
      retval = 0;
6622
      /* op2 = (M:) or (M:N),  L < M  */
6623
      if (op2->low != NULL
6624
          && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6625
        retval = -1;
6626
    }
6627
  else if (op1->high == NULL) /* op1 = (K:)  */
6628
    {
6629
      /* op2 = (M:), so overlap.  */
6630
      retval = 0;
6631
      /* op2 = (:N) or (M:N), K > N  */
6632
      if (op2->high != NULL
6633
          && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6634
        retval = 1;
6635
    }
6636
  else /* op1 = (K:L)  */
6637
    {
6638
      if (op2->low == NULL)       /* op2 = (:N), K > N  */
6639
        retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6640
                 ? 1 : 0;
6641
      else if (op2->high == NULL) /* op2 = (M:), L < M  */
6642
        retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6643
                 ? -1 : 0;
6644
      else                      /* op2 = (M:N)  */
6645
        {
6646
          retval =  0;
6647
          /* L < M  */
6648
          if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6649
            retval =  -1;
6650
          /* K > N  */
6651
          else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6652
            retval =  1;
6653
        }
6654
    }
6655
 
6656
  return retval;
6657
}
6658
 
6659
 
6660
/* Merge-sort a double linked case list, detecting overlap in the
6661
   process.  LIST is the head of the double linked case list before it
6662
   is sorted.  Returns the head of the sorted list if we don't see any
6663
   overlap, or NULL otherwise.  */
6664
 
6665
static gfc_case *
6666
check_case_overlap (gfc_case *list)
6667
{
6668
  gfc_case *p, *q, *e, *tail;
6669
  int insize, nmerges, psize, qsize, cmp, overlap_seen;
6670
 
6671
  /* If the passed list was empty, return immediately.  */
6672
  if (!list)
6673
    return NULL;
6674
 
6675
  overlap_seen = 0;
6676
  insize = 1;
6677
 
6678
  /* Loop unconditionally.  The only exit from this loop is a return
6679
     statement, when we've finished sorting the case list.  */
6680
  for (;;)
6681
    {
6682
      p = list;
6683
      list = NULL;
6684
      tail = NULL;
6685
 
6686
      /* Count the number of merges we do in this pass.  */
6687
      nmerges = 0;
6688
 
6689
      /* Loop while there exists a merge to be done.  */
6690
      while (p)
6691
        {
6692
          int i;
6693
 
6694
          /* Count this merge.  */
6695
          nmerges++;
6696
 
6697
          /* Cut the list in two pieces by stepping INSIZE places
6698
             forward in the list, starting from P.  */
6699
          psize = 0;
6700
          q = p;
6701
          for (i = 0; i < insize; i++)
6702
            {
6703
              psize++;
6704
              q = q->right;
6705
              if (!q)
6706
                break;
6707
            }
6708
          qsize = insize;
6709
 
6710
          /* Now we have two lists.  Merge them!  */
6711
          while (psize > 0 || (qsize > 0 && q != NULL))
6712
            {
6713
              /* See from which the next case to merge comes from.  */
6714
              if (psize == 0)
6715
                {
6716
                  /* P is empty so the next case must come from Q.  */
6717
                  e = q;
6718
                  q = q->right;
6719
                  qsize--;
6720
                }
6721
              else if (qsize == 0 || q == NULL)
6722
                {
6723
                  /* Q is empty.  */
6724
                  e = p;
6725
                  p = p->right;
6726
                  psize--;
6727
                }
6728
              else
6729
                {
6730
                  cmp = compare_cases (p, q);
6731
                  if (cmp < 0)
6732
                    {
6733
                      /* The whole case range for P is less than the
6734
                         one for Q.  */
6735
                      e = p;
6736
                      p = p->right;
6737
                      psize--;
6738
                    }
6739
                  else if (cmp > 0)
6740
                    {
6741
                      /* The whole case range for Q is greater than
6742
                         the case range for P.  */
6743
                      e = q;
6744
                      q = q->right;
6745
                      qsize--;
6746
                    }
6747
                  else
6748
                    {
6749
                      /* The cases overlap, or they are the same
6750
                         element in the list.  Either way, we must
6751
                         issue an error and get the next case from P.  */
6752
                      /* FIXME: Sort P and Q by line number.  */
6753
                      gfc_error ("CASE label at %L overlaps with CASE "
6754
                                 "label at %L", &p->where, &q->where);
6755
                      overlap_seen = 1;
6756
                      e = p;
6757
                      p = p->right;
6758
                      psize--;
6759
                    }
6760
                }
6761
 
6762
                /* Add the next element to the merged list.  */
6763
              if (tail)
6764
                tail->right = e;
6765
              else
6766
                list = e;
6767
              e->left = tail;
6768
              tail = e;
6769
            }
6770
 
6771
          /* P has now stepped INSIZE places along, and so has Q.  So
6772
             they're the same.  */
6773
          p = q;
6774
        }
6775
      tail->right = NULL;
6776
 
6777
      /* If we have done only one merge or none at all, we've
6778
         finished sorting the cases.  */
6779
      if (nmerges <= 1)
6780
        {
6781
          if (!overlap_seen)
6782
            return list;
6783
          else
6784
            return NULL;
6785
        }
6786
 
6787
      /* Otherwise repeat, merging lists twice the size.  */
6788
      insize *= 2;
6789
    }
6790
}
6791
 
6792
 
6793
/* Check to see if an expression is suitable for use in a CASE statement.
6794
   Makes sure that all case expressions are scalar constants of the same
6795
   type.  Return FAILURE if anything is wrong.  */
6796
 
6797
static gfc_try
6798
validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6799
{
6800
  if (e == NULL) return SUCCESS;
6801
 
6802
  if (e->ts.type != case_expr->ts.type)
6803
    {
6804
      gfc_error ("Expression in CASE statement at %L must be of type %s",
6805
                 &e->where, gfc_basic_typename (case_expr->ts.type));
6806
      return FAILURE;
6807
    }
6808
 
6809
  /* C805 (R808) For a given case-construct, each case-value shall be of
6810
     the same type as case-expr.  For character type, length differences
6811
     are allowed, but the kind type parameters shall be the same.  */
6812
 
6813
  if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6814
    {
6815
      gfc_error ("Expression in CASE statement at %L must be of kind %d",
6816
                 &e->where, case_expr->ts.kind);
6817
      return FAILURE;
6818
    }
6819
 
6820
  /* Convert the case value kind to that of case expression kind, if needed.
6821
     FIXME:  Should a warning be issued?  */
6822
  if (e->ts.kind != case_expr->ts.kind)
6823
    gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6824
 
6825
  if (e->rank != 0)
6826
    {
6827
      gfc_error ("Expression in CASE statement at %L must be scalar",
6828
                 &e->where);
6829
      return FAILURE;
6830
    }
6831
 
6832
  return SUCCESS;
6833
}
6834
 
6835
 
6836
/* Given a completely parsed select statement, we:
6837
 
6838
     - Validate all expressions and code within the SELECT.
6839
     - Make sure that the selection expression is not of the wrong type.
6840
     - Make sure that no case ranges overlap.
6841
     - Eliminate unreachable cases and unreachable code resulting from
6842
       removing case labels.
6843
 
6844
   The standard does allow unreachable cases, e.g. CASE (5:3).  But
6845
   they are a hassle for code generation, and to prevent that, we just
6846
   cut them out here.  This is not necessary for overlapping cases
6847
   because they are illegal and we never even try to generate code.
6848
 
6849
   We have the additional caveat that a SELECT construct could have
6850
   been a computed GOTO in the source code. Fortunately we can fairly
6851
   easily work around that here: The case_expr for a "real" SELECT CASE
6852
   is in code->expr1, but for a computed GOTO it is in code->expr2. All
6853
   we have to do is make sure that the case_expr is a scalar integer
6854
   expression.  */
6855
 
6856
static void
6857
resolve_select (gfc_code *code)
6858
{
6859
  gfc_code *body;
6860
  gfc_expr *case_expr;
6861
  gfc_case *cp, *default_case, *tail, *head;
6862
  int seen_unreachable;
6863
  int seen_logical;
6864
  int ncases;
6865
  bt type;
6866
  gfc_try t;
6867
 
6868
  if (code->expr1 == NULL)
6869
    {
6870
      /* This was actually a computed GOTO statement.  */
6871
      case_expr = code->expr2;
6872
      if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6873
        gfc_error ("Selection expression in computed GOTO statement "
6874
                   "at %L must be a scalar integer expression",
6875
                   &case_expr->where);
6876
 
6877
      /* Further checking is not necessary because this SELECT was built
6878
         by the compiler, so it should always be OK.  Just move the
6879
         case_expr from expr2 to expr so that we can handle computed
6880
         GOTOs as normal SELECTs from here on.  */
6881
      code->expr1 = code->expr2;
6882
      code->expr2 = NULL;
6883
      return;
6884
    }
6885
 
6886
  case_expr = code->expr1;
6887
 
6888
  type = case_expr->ts.type;
6889
  if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6890
    {
6891
      gfc_error ("Argument of SELECT statement at %L cannot be %s",
6892
                 &case_expr->where, gfc_typename (&case_expr->ts));
6893
 
6894
      /* Punt. Going on here just produce more garbage error messages.  */
6895
      return;
6896
    }
6897
 
6898
  if (case_expr->rank != 0)
6899
    {
6900
      gfc_error ("Argument of SELECT statement at %L must be a scalar "
6901
                 "expression", &case_expr->where);
6902
 
6903
      /* Punt.  */
6904
      return;
6905
    }
6906
 
6907
  /* PR 19168 has a long discussion concerning a mismatch of the kinds
6908
     of the SELECT CASE expression and its CASE values.  Walk the lists
6909
     of case values, and if we find a mismatch, promote case_expr to
6910
     the appropriate kind.  */
6911
 
6912
  if (type == BT_LOGICAL || type == BT_INTEGER)
6913
    {
6914
      for (body = code->block; body; body = body->block)
6915
        {
6916
          /* Walk the case label list.  */
6917
          for (cp = body->ext.case_list; cp; cp = cp->next)
6918
            {
6919
              /* Intercept the DEFAULT case.  It does not have a kind.  */
6920
              if (cp->low == NULL && cp->high == NULL)
6921
                continue;
6922
 
6923
              /* Unreachable case ranges are discarded, so ignore.  */
6924
              if (cp->low != NULL && cp->high != NULL
6925
                  && cp->low != cp->high
6926
                  && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6927
                continue;
6928
 
6929
              /* FIXME: Should a warning be issued?  */
6930
              if (cp->low != NULL
6931
                  && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6932
                gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6933
 
6934
              if (cp->high != NULL
6935
                  && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6936
                gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6937
            }
6938
         }
6939
    }
6940
 
6941
  /* Assume there is no DEFAULT case.  */
6942
  default_case = NULL;
6943
  head = tail = NULL;
6944
  ncases = 0;
6945
  seen_logical = 0;
6946
 
6947
  for (body = code->block; body; body = body->block)
6948
    {
6949
      /* Assume the CASE list is OK, and all CASE labels can be matched.  */
6950
      t = SUCCESS;
6951
      seen_unreachable = 0;
6952
 
6953
      /* Walk the case label list, making sure that all case labels
6954
         are legal.  */
6955
      for (cp = body->ext.case_list; cp; cp = cp->next)
6956
        {
6957
          /* Count the number of cases in the whole construct.  */
6958
          ncases++;
6959
 
6960
          /* Intercept the DEFAULT case.  */
6961
          if (cp->low == NULL && cp->high == NULL)
6962
            {
6963
              if (default_case != NULL)
6964
                {
6965
                  gfc_error ("The DEFAULT CASE at %L cannot be followed "
6966
                             "by a second DEFAULT CASE at %L",
6967
                             &default_case->where, &cp->where);
6968
                  t = FAILURE;
6969
                  break;
6970
                }
6971
              else
6972
                {
6973
                  default_case = cp;
6974
                  continue;
6975
                }
6976
            }
6977
 
6978
          /* Deal with single value cases and case ranges.  Errors are
6979
             issued from the validation function.  */
6980
          if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6981
             || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6982
            {
6983
              t = FAILURE;
6984
              break;
6985
            }
6986
 
6987
          if (type == BT_LOGICAL
6988
              && ((cp->low == NULL || cp->high == NULL)
6989
                  || cp->low != cp->high))
6990
            {
6991
              gfc_error ("Logical range in CASE statement at %L is not "
6992
                         "allowed", &cp->low->where);
6993
              t = FAILURE;
6994
              break;
6995
            }
6996
 
6997
          if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6998
            {
6999
              int value;
7000
              value = cp->low->value.logical == 0 ? 2 : 1;
7001
              if (value & seen_logical)
7002
                {
7003
                  gfc_error ("constant logical value in CASE statement "
7004
                             "is repeated at %L",
7005
                             &cp->low->where);
7006
                  t = FAILURE;
7007
                  break;
7008
                }
7009
              seen_logical |= value;
7010
            }
7011
 
7012
          if (cp->low != NULL && cp->high != NULL
7013
              && cp->low != cp->high
7014
              && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7015
            {
7016
              if (gfc_option.warn_surprising)
7017
                gfc_warning ("Range specification at %L can never "
7018
                             "be matched", &cp->where);
7019
 
7020
              cp->unreachable = 1;
7021
              seen_unreachable = 1;
7022
            }
7023
          else
7024
            {
7025
              /* If the case range can be matched, it can also overlap with
7026
                 other cases.  To make sure it does not, we put it in a
7027
                 double linked list here.  We sort that with a merge sort
7028
                 later on to detect any overlapping cases.  */
7029
              if (!head)
7030
                {
7031
                  head = tail = cp;
7032
                  head->right = head->left = NULL;
7033
                }
7034
              else
7035
                {
7036
                  tail->right = cp;
7037
                  tail->right->left = tail;
7038
                  tail = tail->right;
7039
                  tail->right = NULL;
7040
                }
7041
            }
7042
        }
7043
 
7044
      /* It there was a failure in the previous case label, give up
7045
         for this case label list.  Continue with the next block.  */
7046
      if (t == FAILURE)
7047
        continue;
7048
 
7049
      /* See if any case labels that are unreachable have been seen.
7050
         If so, we eliminate them.  This is a bit of a kludge because
7051
         the case lists for a single case statement (label) is a
7052
         single forward linked lists.  */
7053
      if (seen_unreachable)
7054
      {
7055
        /* Advance until the first case in the list is reachable.  */
7056
        while (body->ext.case_list != NULL
7057
               && body->ext.case_list->unreachable)
7058
          {
7059
            gfc_case *n = body->ext.case_list;
7060
            body->ext.case_list = body->ext.case_list->next;
7061
            n->next = NULL;
7062
            gfc_free_case_list (n);
7063
          }
7064
 
7065
        /* Strip all other unreachable cases.  */
7066
        if (body->ext.case_list)
7067
          {
7068
            for (cp = body->ext.case_list; cp->next; cp = cp->next)
7069
              {
7070
                if (cp->next->unreachable)
7071
                  {
7072
                    gfc_case *n = cp->next;
7073
                    cp->next = cp->next->next;
7074
                    n->next = NULL;
7075
                    gfc_free_case_list (n);
7076
                  }
7077
              }
7078
          }
7079
      }
7080
    }
7081
 
7082
  /* See if there were overlapping cases.  If the check returns NULL,
7083
     there was overlap.  In that case we don't do anything.  If head
7084
     is non-NULL, we prepend the DEFAULT case.  The sorted list can
7085
     then used during code generation for SELECT CASE constructs with
7086
     a case expression of a CHARACTER type.  */
7087
  if (head)
7088
    {
7089
      head = check_case_overlap (head);
7090
 
7091
      /* Prepend the default_case if it is there.  */
7092
      if (head != NULL && default_case)
7093
        {
7094
          default_case->left = NULL;
7095
          default_case->right = head;
7096
          head->left = default_case;
7097
        }
7098
    }
7099
 
7100
  /* Eliminate dead blocks that may be the result if we've seen
7101
     unreachable case labels for a block.  */
7102
  for (body = code; body && body->block; body = body->block)
7103
    {
7104
      if (body->block->ext.case_list == NULL)
7105
        {
7106
          /* Cut the unreachable block from the code chain.  */
7107
          gfc_code *c = body->block;
7108
          body->block = c->block;
7109
 
7110
          /* Kill the dead block, but not the blocks below it.  */
7111
          c->block = NULL;
7112
          gfc_free_statements (c);
7113
        }
7114
    }
7115
 
7116
  /* More than two cases is legal but insane for logical selects.
7117
     Issue a warning for it.  */
7118
  if (gfc_option.warn_surprising && type == BT_LOGICAL
7119
      && ncases > 2)
7120
    gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7121
                 &code->loc);
7122
}
7123
 
7124
 
7125
/* Check if a derived type is extensible.  */
7126
 
7127
bool
7128
gfc_type_is_extensible (gfc_symbol *sym)
7129
{
7130
  return !(sym->attr.is_bind_c || sym->attr.sequence);
7131
}
7132
 
7133
 
7134
/* Resolve a SELECT TYPE statement.  */
7135
 
7136
static void
7137
resolve_select_type (gfc_code *code)
7138
{
7139
  gfc_symbol *selector_type;
7140
  gfc_code *body, *new_st, *if_st, *tail;
7141
  gfc_code *class_is = NULL, *default_case = NULL;
7142
  gfc_case *c;
7143
  gfc_symtree *st;
7144
  char name[GFC_MAX_SYMBOL_LEN];
7145
  gfc_namespace *ns;
7146
  int error = 0;
7147
 
7148
  ns = code->ext.ns;
7149
  gfc_resolve (ns);
7150
 
7151
  if (code->expr2)
7152
    selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
7153
  else
7154
    selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
7155
 
7156
  /* Loop over TYPE IS / CLASS IS cases.  */
7157
  for (body = code->block; body; body = body->block)
7158
    {
7159
      c = body->ext.case_list;
7160
 
7161
      /* Check F03:C815.  */
7162
      if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7163
          && !gfc_type_is_extensible (c->ts.u.derived))
7164
        {
7165
          gfc_error ("Derived type '%s' at %L must be extensible",
7166
                     c->ts.u.derived->name, &c->where);
7167
          error++;
7168
          continue;
7169
        }
7170
 
7171
      /* Check F03:C816.  */
7172
      if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7173
          && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7174
        {
7175
          gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7176
                     c->ts.u.derived->name, &c->where, selector_type->name);
7177
          error++;
7178
          continue;
7179
        }
7180
 
7181
      /* Intercept the DEFAULT case.  */
7182
      if (c->ts.type == BT_UNKNOWN)
7183
        {
7184
          /* Check F03:C818.  */
7185
          if (default_case)
7186
            {
7187
              gfc_error ("The DEFAULT CASE at %L cannot be followed "
7188
                         "by a second DEFAULT CASE at %L",
7189
                         &default_case->ext.case_list->where, &c->where);
7190
              error++;
7191
              continue;
7192
            }
7193
          else
7194
            default_case = body;
7195
        }
7196
    }
7197
 
7198
  if (error>0)
7199
    return;
7200
 
7201
  if (code->expr2)
7202
    {
7203
      /* Insert assignment for selector variable.  */
7204
      new_st = gfc_get_code ();
7205
      new_st->op = EXEC_ASSIGN;
7206
      new_st->expr1 = gfc_copy_expr (code->expr1);
7207
      new_st->expr2 = gfc_copy_expr (code->expr2);
7208
      ns->code = new_st;
7209
    }
7210
 
7211
  /* Put SELECT TYPE statement inside a BLOCK.  */
7212
  new_st = gfc_get_code ();
7213
  new_st->op = code->op;
7214
  new_st->expr1 = code->expr1;
7215
  new_st->expr2 = code->expr2;
7216
  new_st->block = code->block;
7217
  if (!ns->code)
7218
    ns->code = new_st;
7219
  else
7220
    ns->code->next = new_st;
7221
  code->op = EXEC_BLOCK;
7222
  code->expr1 = code->expr2 =  NULL;
7223
  code->block = NULL;
7224
 
7225
  code = new_st;
7226
 
7227
  /* Transform to EXEC_SELECT.  */
7228
  code->op = EXEC_SELECT;
7229
  gfc_add_component_ref (code->expr1, "$vptr");
7230
  gfc_add_component_ref (code->expr1, "$hash");
7231
 
7232
  /* Loop over TYPE IS / CLASS IS cases.  */
7233
  for (body = code->block; body; body = body->block)
7234
    {
7235
      c = body->ext.case_list;
7236
 
7237
      if (c->ts.type == BT_DERIVED)
7238
        c->low = c->high = gfc_int_expr (c->ts.u.derived->hash_value);
7239
      else if (c->ts.type == BT_UNKNOWN)
7240
        continue;
7241
 
7242
      /* Assign temporary to selector.  */
7243
      if (c->ts.type == BT_CLASS)
7244
        sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7245
      else
7246
        sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7247
      st = gfc_find_symtree (ns->sym_root, name);
7248
      new_st = gfc_get_code ();
7249
      new_st->expr1 = gfc_get_variable_expr (st);
7250
      new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7251
      if (c->ts.type == BT_DERIVED)
7252
        {
7253
          new_st->op = EXEC_POINTER_ASSIGN;
7254
          gfc_add_component_ref (new_st->expr2, "$data");
7255
        }
7256
      else
7257
        new_st->op = EXEC_POINTER_ASSIGN;
7258
      new_st->next = body->next;
7259
      body->next = new_st;
7260
    }
7261
 
7262
  /* Take out CLASS IS cases for separate treatment.  */
7263
  body = code;
7264
  while (body && body->block)
7265
    {
7266
      if (body->block->ext.case_list->ts.type == BT_CLASS)
7267
        {
7268
          /* Add to class_is list.  */
7269
          if (class_is == NULL)
7270
            {
7271
              class_is = body->block;
7272
              tail = class_is;
7273
            }
7274
          else
7275
            {
7276
              for (tail = class_is; tail->block; tail = tail->block) ;
7277
              tail->block = body->block;
7278
              tail = tail->block;
7279
            }
7280
          /* Remove from EXEC_SELECT list.  */
7281
          body->block = body->block->block;
7282
          tail->block = NULL;
7283
        }
7284
      else
7285
        body = body->block;
7286
    }
7287
 
7288
  if (class_is)
7289
    {
7290
      gfc_symbol *vtab;
7291
 
7292
      if (!default_case)
7293
        {
7294
          /* Add a default case to hold the CLASS IS cases.  */
7295
          for (tail = code; tail->block; tail = tail->block) ;
7296
          tail->block = gfc_get_code ();
7297
          tail = tail->block;
7298
          tail->op = EXEC_SELECT_TYPE;
7299
          tail->ext.case_list = gfc_get_case ();
7300
          tail->ext.case_list->ts.type = BT_UNKNOWN;
7301
          tail->next = NULL;
7302
          default_case = tail;
7303
        }
7304
 
7305
      /* More than one CLASS IS block?  */
7306
      if (class_is->block)
7307
        {
7308
          gfc_code **c1,*c2;
7309
          bool swapped;
7310
          /* Sort CLASS IS blocks by extension level.  */
7311
          do
7312
            {
7313
              swapped = false;
7314
              for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7315
                {
7316
                  c2 = (*c1)->block;
7317
                  /* F03:C817 (check for doubles).  */
7318
                  if ((*c1)->ext.case_list->ts.u.derived->hash_value
7319
                      == c2->ext.case_list->ts.u.derived->hash_value)
7320
                    {
7321
                      gfc_error ("Double CLASS IS block in SELECT TYPE "
7322
                                 "statement at %L", &c2->ext.case_list->where);
7323
                      return;
7324
                    }
7325
                  if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7326
                      < c2->ext.case_list->ts.u.derived->attr.extension)
7327
                    {
7328
                      /* Swap.  */
7329
                      (*c1)->block = c2->block;
7330
                      c2->block = *c1;
7331
                      *c1 = c2;
7332
                      swapped = true;
7333
                    }
7334
                }
7335
            }
7336
          while (swapped);
7337
        }
7338
 
7339
      /* Generate IF chain.  */
7340
      if_st = gfc_get_code ();
7341
      if_st->op = EXEC_IF;
7342
      new_st = if_st;
7343
      for (body = class_is; body; body = body->block)
7344
        {
7345
          new_st->block = gfc_get_code ();
7346
          new_st = new_st->block;
7347
          new_st->op = EXEC_IF;
7348
          /* Set up IF condition: Call _gfortran_is_extension_of.  */
7349
          new_st->expr1 = gfc_get_expr ();
7350
          new_st->expr1->expr_type = EXPR_FUNCTION;
7351
          new_st->expr1->ts.type = BT_LOGICAL;
7352
          new_st->expr1->ts.kind = 4;
7353
          new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7354
          new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7355
          new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7356
          /* Set up arguments.  */
7357
          new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7358
          new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7359
          gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7360
          vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7361
          st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7362
          new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7363
          new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7364
          new_st->next = body->next;
7365
        }
7366
        if (default_case->next)
7367
          {
7368
            new_st->block = gfc_get_code ();
7369
            new_st = new_st->block;
7370
            new_st->op = EXEC_IF;
7371
            new_st->next = default_case->next;
7372
          }
7373
 
7374
        /* Replace CLASS DEFAULT code by the IF chain.  */
7375
        default_case->next = if_st;
7376
    }
7377
 
7378
  resolve_select (code);
7379
 
7380
}
7381
 
7382
 
7383
/* Resolve a transfer statement. This is making sure that:
7384
   -- a derived type being transferred has only non-pointer components
7385
   -- a derived type being transferred doesn't have private components, unless
7386
      it's being transferred from the module where the type was defined
7387
   -- we're not trying to transfer a whole assumed size array.  */
7388
 
7389
static void
7390
resolve_transfer (gfc_code *code)
7391
{
7392
  gfc_typespec *ts;
7393
  gfc_symbol *sym;
7394
  gfc_ref *ref;
7395
  gfc_expr *exp;
7396
 
7397
  exp = code->expr1;
7398
 
7399
  if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7400
    return;
7401
 
7402
  sym = exp->symtree->n.sym;
7403
  ts = &sym->ts;
7404
 
7405
  /* Go to actual component transferred.  */
7406
  for (ref = code->expr1->ref; ref; ref = ref->next)
7407
    if (ref->type == REF_COMPONENT)
7408
      ts = &ref->u.c.component->ts;
7409
 
7410
  if (ts->type == BT_DERIVED)
7411
    {
7412
      /* Check that transferred derived type doesn't contain POINTER
7413
         components.  */
7414
      if (ts->u.derived->attr.pointer_comp)
7415
        {
7416
          gfc_error ("Data transfer element at %L cannot have "
7417
                     "POINTER components", &code->loc);
7418
          return;
7419
        }
7420
 
7421
      if (ts->u.derived->attr.alloc_comp)
7422
        {
7423
          gfc_error ("Data transfer element at %L cannot have "
7424
                     "ALLOCATABLE components", &code->loc);
7425
          return;
7426
        }
7427
 
7428
      if (derived_inaccessible (ts->u.derived))
7429
        {
7430
          gfc_error ("Data transfer element at %L cannot have "
7431
                     "PRIVATE components",&code->loc);
7432
          return;
7433
        }
7434
    }
7435
 
7436
  if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7437
      && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7438
    {
7439
      gfc_error ("Data transfer element at %L cannot be a full reference to "
7440
                 "an assumed-size array", &code->loc);
7441
      return;
7442
    }
7443
}
7444
 
7445
 
7446
/*********** Toplevel code resolution subroutines ***********/
7447
 
7448
/* Find the set of labels that are reachable from this block.  We also
7449
   record the last statement in each block.  */
7450
 
7451
static void
7452
find_reachable_labels (gfc_code *block)
7453
{
7454
  gfc_code *c;
7455
 
7456
  if (!block)
7457
    return;
7458
 
7459
  cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7460
 
7461
  /* Collect labels in this block.  We don't keep those corresponding
7462
     to END {IF|SELECT}, these are checked in resolve_branch by going
7463
     up through the code_stack.  */
7464
  for (c = block; c; c = c->next)
7465
    {
7466
      if (c->here && c->op != EXEC_END_BLOCK)
7467
        bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7468
    }
7469
 
7470
  /* Merge with labels from parent block.  */
7471
  if (cs_base->prev)
7472
    {
7473
      gcc_assert (cs_base->prev->reachable_labels);
7474
      bitmap_ior_into (cs_base->reachable_labels,
7475
                       cs_base->prev->reachable_labels);
7476
    }
7477
}
7478
 
7479
/* Given a branch to a label, see if the branch is conforming.
7480
   The code node describes where the branch is located.  */
7481
 
7482
static void
7483
resolve_branch (gfc_st_label *label, gfc_code *code)
7484
{
7485
  code_stack *stack;
7486
 
7487
  if (label == NULL)
7488
    return;
7489
 
7490
  /* Step one: is this a valid branching target?  */
7491
 
7492
  if (label->defined == ST_LABEL_UNKNOWN)
7493
    {
7494
      gfc_error ("Label %d referenced at %L is never defined", label->value,
7495
                 &label->where);
7496
      return;
7497
    }
7498
 
7499
  if (label->defined != ST_LABEL_TARGET)
7500
    {
7501
      gfc_error ("Statement at %L is not a valid branch target statement "
7502
                 "for the branch statement at %L", &label->where, &code->loc);
7503
      return;
7504
    }
7505
 
7506
  /* Step two: make sure this branch is not a branch to itself ;-)  */
7507
 
7508
  if (code->here == label)
7509
    {
7510
      gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7511
      return;
7512
    }
7513
 
7514
  /* Step three:  See if the label is in the same block as the
7515
     branching statement.  The hard work has been done by setting up
7516
     the bitmap reachable_labels.  */
7517
 
7518
  if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7519
    return;
7520
 
7521
  /* Step four:  If we haven't found the label in the bitmap, it may
7522
    still be the label of the END of the enclosing block, in which
7523
    case we find it by going up the code_stack.  */
7524
 
7525
  for (stack = cs_base; stack; stack = stack->prev)
7526
    if (stack->current->next && stack->current->next->here == label)
7527
      break;
7528
 
7529
  if (stack)
7530
    {
7531
      gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7532
      return;
7533
    }
7534
 
7535
  /* The label is not in an enclosing block, so illegal.  This was
7536
     allowed in Fortran 66, so we allow it as extension.  No
7537
     further checks are necessary in this case.  */
7538
  gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7539
                  "as the GOTO statement at %L", &label->where,
7540
                  &code->loc);
7541
  return;
7542
}
7543
 
7544
 
7545
/* Check whether EXPR1 has the same shape as EXPR2.  */
7546
 
7547
static gfc_try
7548
resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7549
{
7550
  mpz_t shape[GFC_MAX_DIMENSIONS];
7551
  mpz_t shape2[GFC_MAX_DIMENSIONS];
7552
  gfc_try result = FAILURE;
7553
  int i;
7554
 
7555
  /* Compare the rank.  */
7556
  if (expr1->rank != expr2->rank)
7557
    return result;
7558
 
7559
  /* Compare the size of each dimension.  */
7560
  for (i=0; i<expr1->rank; i++)
7561
    {
7562
      if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7563
        goto ignore;
7564
 
7565
      if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7566
        goto ignore;
7567
 
7568
      if (mpz_cmp (shape[i], shape2[i]))
7569
        goto over;
7570
    }
7571
 
7572
  /* When either of the two expression is an assumed size array, we
7573
     ignore the comparison of dimension sizes.  */
7574
ignore:
7575
  result = SUCCESS;
7576
 
7577
over:
7578
  for (i--; i >= 0; i--)
7579
    {
7580
      mpz_clear (shape[i]);
7581
      mpz_clear (shape2[i]);
7582
    }
7583
  return result;
7584
}
7585
 
7586
 
7587
/* Check whether a WHERE assignment target or a WHERE mask expression
7588
   has the same shape as the outmost WHERE mask expression.  */
7589
 
7590
static void
7591
resolve_where (gfc_code *code, gfc_expr *mask)
7592
{
7593
  gfc_code *cblock;
7594
  gfc_code *cnext;
7595
  gfc_expr *e = NULL;
7596
 
7597
  cblock = code->block;
7598
 
7599
  /* Store the first WHERE mask-expr of the WHERE statement or construct.
7600
     In case of nested WHERE, only the outmost one is stored.  */
7601
  if (mask == NULL) /* outmost WHERE */
7602
    e = cblock->expr1;
7603
  else /* inner WHERE */
7604
    e = mask;
7605
 
7606
  while (cblock)
7607
    {
7608
      if (cblock->expr1)
7609
        {
7610
          /* Check if the mask-expr has a consistent shape with the
7611
             outmost WHERE mask-expr.  */
7612
          if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7613
            gfc_error ("WHERE mask at %L has inconsistent shape",
7614
                       &cblock->expr1->where);
7615
         }
7616
 
7617
      /* the assignment statement of a WHERE statement, or the first
7618
         statement in where-body-construct of a WHERE construct */
7619
      cnext = cblock->next;
7620
      while (cnext)
7621
        {
7622
          switch (cnext->op)
7623
            {
7624
            /* WHERE assignment statement */
7625
            case EXEC_ASSIGN:
7626
 
7627
              /* Check shape consistent for WHERE assignment target.  */
7628
              if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7629
               gfc_error ("WHERE assignment target at %L has "
7630
                          "inconsistent shape", &cnext->expr1->where);
7631
              break;
7632
 
7633
 
7634
            case EXEC_ASSIGN_CALL:
7635
              resolve_call (cnext);
7636
              if (!cnext->resolved_sym->attr.elemental)
7637
                gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7638
                          &cnext->ext.actual->expr->where);
7639
              break;
7640
 
7641
            /* WHERE or WHERE construct is part of a where-body-construct */
7642
            case EXEC_WHERE:
7643
              resolve_where (cnext, e);
7644
              break;
7645
 
7646
            default:
7647
              gfc_error ("Unsupported statement inside WHERE at %L",
7648
                         &cnext->loc);
7649
            }
7650
         /* the next statement within the same where-body-construct */
7651
         cnext = cnext->next;
7652
       }
7653
    /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7654
    cblock = cblock->block;
7655
  }
7656
}
7657
 
7658
 
7659
/* Resolve assignment in FORALL construct.
7660
   NVAR is the number of FORALL index variables, and VAR_EXPR records the
7661
   FORALL index variables.  */
7662
 
7663
static void
7664
gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7665
{
7666
  int n;
7667
 
7668
  for (n = 0; n < nvar; n++)
7669
    {
7670
      gfc_symbol *forall_index;
7671
 
7672
      forall_index = var_expr[n]->symtree->n.sym;
7673
 
7674
      /* Check whether the assignment target is one of the FORALL index
7675
         variable.  */
7676
      if ((code->expr1->expr_type == EXPR_VARIABLE)
7677
          && (code->expr1->symtree->n.sym == forall_index))
7678
        gfc_error ("Assignment to a FORALL index variable at %L",
7679
                   &code->expr1->where);
7680
      else
7681
        {
7682
          /* If one of the FORALL index variables doesn't appear in the
7683
             assignment variable, then there could be a many-to-one
7684
             assignment.  Emit a warning rather than an error because the
7685
             mask could be resolving this problem.  */
7686
          if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7687
            gfc_warning ("The FORALL with index '%s' is not used on the "
7688
                         "left side of the assignment at %L and so might "
7689
                         "cause multiple assignment to this object",
7690
                         var_expr[n]->symtree->name, &code->expr1->where);
7691
        }
7692
    }
7693
}
7694
 
7695
 
7696
/* Resolve WHERE statement in FORALL construct.  */
7697
 
7698
static void
7699
gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7700
                                  gfc_expr **var_expr)
7701
{
7702
  gfc_code *cblock;
7703
  gfc_code *cnext;
7704
 
7705
  cblock = code->block;
7706
  while (cblock)
7707
    {
7708
      /* the assignment statement of a WHERE statement, or the first
7709
         statement in where-body-construct of a WHERE construct */
7710
      cnext = cblock->next;
7711
      while (cnext)
7712
        {
7713
          switch (cnext->op)
7714
            {
7715
            /* WHERE assignment statement */
7716
            case EXEC_ASSIGN:
7717
              gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7718
              break;
7719
 
7720
            /* WHERE operator assignment statement */
7721
            case EXEC_ASSIGN_CALL:
7722
              resolve_call (cnext);
7723
              if (!cnext->resolved_sym->attr.elemental)
7724
                gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7725
                          &cnext->ext.actual->expr->where);
7726
              break;
7727
 
7728
            /* WHERE or WHERE construct is part of a where-body-construct */
7729
            case EXEC_WHERE:
7730
              gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7731
              break;
7732
 
7733
            default:
7734
              gfc_error ("Unsupported statement inside WHERE at %L",
7735
                         &cnext->loc);
7736
            }
7737
          /* the next statement within the same where-body-construct */
7738
          cnext = cnext->next;
7739
        }
7740
      /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7741
      cblock = cblock->block;
7742
    }
7743
}
7744
 
7745
 
7746
/* Traverse the FORALL body to check whether the following errors exist:
7747
   1. For assignment, check if a many-to-one assignment happens.
7748
   2. For WHERE statement, check the WHERE body to see if there is any
7749
      many-to-one assignment.  */
7750
 
7751
static void
7752
gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7753
{
7754
  gfc_code *c;
7755
 
7756
  c = code->block->next;
7757
  while (c)
7758
    {
7759
      switch (c->op)
7760
        {
7761
        case EXEC_ASSIGN:
7762
        case EXEC_POINTER_ASSIGN:
7763
          gfc_resolve_assign_in_forall (c, nvar, var_expr);
7764
          break;
7765
 
7766
        case EXEC_ASSIGN_CALL:
7767
          resolve_call (c);
7768
          break;
7769
 
7770
        /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7771
           there is no need to handle it here.  */
7772
        case EXEC_FORALL:
7773
          break;
7774
        case EXEC_WHERE:
7775
          gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7776
          break;
7777
        default:
7778
          break;
7779
        }
7780
      /* The next statement in the FORALL body.  */
7781
      c = c->next;
7782
    }
7783
}
7784
 
7785
 
7786
/* Counts the number of iterators needed inside a forall construct, including
7787
   nested forall constructs. This is used to allocate the needed memory
7788
   in gfc_resolve_forall.  */
7789
 
7790
static int
7791
gfc_count_forall_iterators (gfc_code *code)
7792
{
7793
  int max_iters, sub_iters, current_iters;
7794
  gfc_forall_iterator *fa;
7795
 
7796
  gcc_assert(code->op == EXEC_FORALL);
7797
  max_iters = 0;
7798
  current_iters = 0;
7799
 
7800
  for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7801
    current_iters ++;
7802
 
7803
  code = code->block->next;
7804
 
7805
  while (code)
7806
    {
7807
      if (code->op == EXEC_FORALL)
7808
        {
7809
          sub_iters = gfc_count_forall_iterators (code);
7810
          if (sub_iters > max_iters)
7811
            max_iters = sub_iters;
7812
        }
7813
      code = code->next;
7814
    }
7815
 
7816
  return current_iters + max_iters;
7817
}
7818
 
7819
 
7820
/* Given a FORALL construct, first resolve the FORALL iterator, then call
7821
   gfc_resolve_forall_body to resolve the FORALL body.  */
7822
 
7823
static void
7824
gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7825
{
7826
  static gfc_expr **var_expr;
7827
  static int total_var = 0;
7828
  static int nvar = 0;
7829
  int old_nvar, tmp;
7830
  gfc_forall_iterator *fa;
7831
  int i;
7832
 
7833
  old_nvar = nvar;
7834
 
7835
  /* Start to resolve a FORALL construct   */
7836
  if (forall_save == 0)
7837
    {
7838
      /* Count the total number of FORALL index in the nested FORALL
7839
         construct in order to allocate the VAR_EXPR with proper size.  */
7840
      total_var = gfc_count_forall_iterators (code);
7841
 
7842
      /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
7843
      var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7844
    }
7845
 
7846
  /* The information about FORALL iterator, including FORALL index start, end
7847
     and stride. The FORALL index can not appear in start, end or stride.  */
7848
  for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7849
    {
7850
      /* Check if any outer FORALL index name is the same as the current
7851
         one.  */
7852
      for (i = 0; i < nvar; i++)
7853
        {
7854
          if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7855
            {
7856
              gfc_error ("An outer FORALL construct already has an index "
7857
                         "with this name %L", &fa->var->where);
7858
            }
7859
        }
7860
 
7861
      /* Record the current FORALL index.  */
7862
      var_expr[nvar] = gfc_copy_expr (fa->var);
7863
 
7864
      nvar++;
7865
 
7866
      /* No memory leak.  */
7867
      gcc_assert (nvar <= total_var);
7868
    }
7869
 
7870
  /* Resolve the FORALL body.  */
7871
  gfc_resolve_forall_body (code, nvar, var_expr);
7872
 
7873
  /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
7874
  gfc_resolve_blocks (code->block, ns);
7875
 
7876
  tmp = nvar;
7877
  nvar = old_nvar;
7878
  /* Free only the VAR_EXPRs allocated in this frame.  */
7879
  for (i = nvar; i < tmp; i++)
7880
     gfc_free_expr (var_expr[i]);
7881
 
7882
  if (nvar == 0)
7883
    {
7884
      /* We are in the outermost FORALL construct.  */
7885
      gcc_assert (forall_save == 0);
7886
 
7887
      /* VAR_EXPR is not needed any more.  */
7888
      gfc_free (var_expr);
7889
      total_var = 0;
7890
    }
7891
}
7892
 
7893
 
7894
/* Resolve a BLOCK construct statement.  */
7895
 
7896
static void
7897
resolve_block_construct (gfc_code* code)
7898
{
7899
  /* Eventually, we may want to do some checks here or handle special stuff.
7900
     But so far the only thing we can do is resolving the local namespace.  */
7901
 
7902
  gfc_resolve (code->ext.ns);
7903
}
7904
 
7905
 
7906
/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7907
   DO code nodes.  */
7908
 
7909
static void resolve_code (gfc_code *, gfc_namespace *);
7910
 
7911
void
7912
gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7913
{
7914
  gfc_try t;
7915
 
7916
  for (; b; b = b->block)
7917
    {
7918
      t = gfc_resolve_expr (b->expr1);
7919
      if (gfc_resolve_expr (b->expr2) == FAILURE)
7920
        t = FAILURE;
7921
 
7922
      switch (b->op)
7923
        {
7924
        case EXEC_IF:
7925
          if (t == SUCCESS && b->expr1 != NULL
7926
              && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7927
            gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7928
                       &b->expr1->where);
7929
          break;
7930
 
7931
        case EXEC_WHERE:
7932
          if (t == SUCCESS
7933
              && b->expr1 != NULL
7934
              && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
7935
            gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7936
                       &b->expr1->where);
7937
          break;
7938
 
7939
        case EXEC_GOTO:
7940
          resolve_branch (b->label1, b);
7941
          break;
7942
 
7943
        case EXEC_BLOCK:
7944
          resolve_block_construct (b);
7945
          break;
7946
 
7947
        case EXEC_SELECT:
7948
        case EXEC_SELECT_TYPE:
7949
        case EXEC_FORALL:
7950
        case EXEC_DO:
7951
        case EXEC_DO_WHILE:
7952
        case EXEC_READ:
7953
        case EXEC_WRITE:
7954
        case EXEC_IOLENGTH:
7955
        case EXEC_WAIT:
7956
          break;
7957
 
7958
        case EXEC_OMP_ATOMIC:
7959
        case EXEC_OMP_CRITICAL:
7960
        case EXEC_OMP_DO:
7961
        case EXEC_OMP_MASTER:
7962
        case EXEC_OMP_ORDERED:
7963
        case EXEC_OMP_PARALLEL:
7964
        case EXEC_OMP_PARALLEL_DO:
7965
        case EXEC_OMP_PARALLEL_SECTIONS:
7966
        case EXEC_OMP_PARALLEL_WORKSHARE:
7967
        case EXEC_OMP_SECTIONS:
7968
        case EXEC_OMP_SINGLE:
7969
        case EXEC_OMP_TASK:
7970
        case EXEC_OMP_TASKWAIT:
7971
        case EXEC_OMP_WORKSHARE:
7972
          break;
7973
 
7974
        default:
7975
          gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7976
        }
7977
 
7978
      resolve_code (b->next, ns);
7979
    }
7980
}
7981
 
7982
 
7983
/* Does everything to resolve an ordinary assignment.  Returns true
7984
   if this is an interface assignment.  */
7985
static bool
7986
resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7987
{
7988
  bool rval = false;
7989
  gfc_expr *lhs;
7990
  gfc_expr *rhs;
7991
  int llen = 0;
7992
  int rlen = 0;
7993
  int n;
7994
  gfc_ref *ref;
7995
 
7996
  if (gfc_extend_assign (code, ns) == SUCCESS)
7997
    {
7998
      gfc_expr** rhsptr;
7999
 
8000
      if (code->op == EXEC_ASSIGN_CALL)
8001
        {
8002
          lhs = code->ext.actual->expr;
8003
          rhsptr = &code->ext.actual->next->expr;
8004
        }
8005
      else
8006
        {
8007
          gfc_actual_arglist* args;
8008
          gfc_typebound_proc* tbp;
8009
 
8010
          gcc_assert (code->op == EXEC_COMPCALL);
8011
 
8012
          args = code->expr1->value.compcall.actual;
8013
          lhs = args->expr;
8014
          rhsptr = &args->next->expr;
8015
 
8016
          tbp = code->expr1->value.compcall.tbp;
8017
          gcc_assert (!tbp->is_generic);
8018
        }
8019
 
8020
      /* Make a temporary rhs when there is a default initializer
8021
         and rhs is the same symbol as the lhs.  */
8022
      if ((*rhsptr)->expr_type == EXPR_VARIABLE
8023
            && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8024
            && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8025
            && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8026
        *rhsptr = gfc_get_parentheses (*rhsptr);
8027
 
8028
      return true;
8029
    }
8030
 
8031
  lhs = code->expr1;
8032
  rhs = code->expr2;
8033
 
8034
  if (rhs->is_boz
8035
      && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8036
                         "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8037
                         &code->loc) == FAILURE)
8038
    return false;
8039
 
8040
  /* Handle the case of a BOZ literal on the RHS.  */
8041
  if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8042
    {
8043
      int rc;
8044
      if (gfc_option.warn_surprising)
8045
        gfc_warning ("BOZ literal at %L is bitwise transferred "
8046
                     "non-integer symbol '%s'", &code->loc,
8047
                     lhs->symtree->n.sym->name);
8048
 
8049
      if (!gfc_convert_boz (rhs, &lhs->ts))
8050
        return false;
8051
      if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8052
        {
8053
          if (rc == ARITH_UNDERFLOW)
8054
            gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8055
                       ". This check can be disabled with the option "
8056
                       "-fno-range-check", &rhs->where);
8057
          else if (rc == ARITH_OVERFLOW)
8058
            gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8059
                       ". This check can be disabled with the option "
8060
                       "-fno-range-check", &rhs->where);
8061
          else if (rc == ARITH_NAN)
8062
            gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8063
                       ". This check can be disabled with the option "
8064
                       "-fno-range-check", &rhs->where);
8065
          return false;
8066
        }
8067
    }
8068
 
8069
 
8070
  if (lhs->ts.type == BT_CHARACTER
8071
        && gfc_option.warn_character_truncation)
8072
    {
8073
      if (lhs->ts.u.cl != NULL
8074
            && lhs->ts.u.cl->length != NULL
8075
            && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8076
        llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8077
 
8078
      if (rhs->expr_type == EXPR_CONSTANT)
8079
        rlen = rhs->value.character.length;
8080
 
8081
      else if (rhs->ts.u.cl != NULL
8082
                 && rhs->ts.u.cl->length != NULL
8083
                 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8084
        rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8085
 
8086
      if (rlen && llen && rlen > llen)
8087
        gfc_warning_now ("CHARACTER expression will be truncated "
8088
                         "in assignment (%d/%d) at %L",
8089
                         llen, rlen, &code->loc);
8090
    }
8091
 
8092
  /* Ensure that a vector index expression for the lvalue is evaluated
8093
     to a temporary if the lvalue symbol is referenced in it.  */
8094
  if (lhs->rank)
8095
    {
8096
      for (ref = lhs->ref; ref; ref= ref->next)
8097
        if (ref->type == REF_ARRAY)
8098
          {
8099
            for (n = 0; n < ref->u.ar.dimen; n++)
8100
              if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8101
                  && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8102
                                           ref->u.ar.start[n]))
8103
                ref->u.ar.start[n]
8104
                        = gfc_get_parentheses (ref->u.ar.start[n]);
8105
          }
8106
    }
8107
 
8108
  if (gfc_pure (NULL))
8109
    {
8110
      if (gfc_impure_variable (lhs->symtree->n.sym))
8111
        {
8112
          gfc_error ("Cannot assign to variable '%s' in PURE "
8113
                     "procedure at %L",
8114
                      lhs->symtree->n.sym->name,
8115
                      &lhs->where);
8116
          return rval;
8117
        }
8118
 
8119
      if (lhs->ts.type == BT_DERIVED
8120
            && lhs->expr_type == EXPR_VARIABLE
8121
            && lhs->ts.u.derived->attr.pointer_comp
8122
            && rhs->expr_type == EXPR_VARIABLE
8123
            && gfc_impure_variable (rhs->symtree->n.sym))
8124
        {
8125
          gfc_error ("The impure variable at %L is assigned to "
8126
                     "a derived type variable with a POINTER "
8127
                     "component in a PURE procedure (12.6)",
8128
                     &rhs->where);
8129
          return rval;
8130
        }
8131
    }
8132
 
8133
  /* F03:7.4.1.2.  */
8134
  if (lhs->ts.type == BT_CLASS)
8135
    {
8136
      gfc_error ("Variable must not be polymorphic in assignment at %L",
8137
                 &lhs->where);
8138
      return false;
8139
    }
8140
 
8141
  gfc_check_assign (lhs, rhs, 1);
8142
  return false;
8143
}
8144
 
8145
 
8146
/* Given a block of code, recursively resolve everything pointed to by this
8147
   code block.  */
8148
 
8149
static void
8150
resolve_code (gfc_code *code, gfc_namespace *ns)
8151
{
8152
  int omp_workshare_save;
8153
  int forall_save;
8154
  code_stack frame;
8155
  gfc_try t;
8156
 
8157
  frame.prev = cs_base;
8158
  frame.head = code;
8159
  cs_base = &frame;
8160
 
8161
  find_reachable_labels (code);
8162
 
8163
  for (; code; code = code->next)
8164
    {
8165
      frame.current = code;
8166
      forall_save = forall_flag;
8167
 
8168
      if (code->op == EXEC_FORALL)
8169
        {
8170
          forall_flag = 1;
8171
          gfc_resolve_forall (code, ns, forall_save);
8172
          forall_flag = 2;
8173
        }
8174
      else if (code->block)
8175
        {
8176
          omp_workshare_save = -1;
8177
          switch (code->op)
8178
            {
8179
            case EXEC_OMP_PARALLEL_WORKSHARE:
8180
              omp_workshare_save = omp_workshare_flag;
8181
              omp_workshare_flag = 1;
8182
              gfc_resolve_omp_parallel_blocks (code, ns);
8183
              break;
8184
            case EXEC_OMP_PARALLEL:
8185
            case EXEC_OMP_PARALLEL_DO:
8186
            case EXEC_OMP_PARALLEL_SECTIONS:
8187
            case EXEC_OMP_TASK:
8188
              omp_workshare_save = omp_workshare_flag;
8189
              omp_workshare_flag = 0;
8190
              gfc_resolve_omp_parallel_blocks (code, ns);
8191
              break;
8192
            case EXEC_OMP_DO:
8193
              gfc_resolve_omp_do_blocks (code, ns);
8194
              break;
8195
            case EXEC_SELECT_TYPE:
8196
              gfc_current_ns = code->ext.ns;
8197
              gfc_resolve_blocks (code->block, gfc_current_ns);
8198
              gfc_current_ns = ns;
8199
              break;
8200
            case EXEC_OMP_WORKSHARE:
8201
              omp_workshare_save = omp_workshare_flag;
8202
              omp_workshare_flag = 1;
8203
              /* FALLTHROUGH */
8204
            default:
8205
              gfc_resolve_blocks (code->block, ns);
8206
              break;
8207
            }
8208
 
8209
          if (omp_workshare_save != -1)
8210
            omp_workshare_flag = omp_workshare_save;
8211
        }
8212
 
8213
      t = SUCCESS;
8214
      if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8215
        t = gfc_resolve_expr (code->expr1);
8216
      forall_flag = forall_save;
8217
 
8218
      if (gfc_resolve_expr (code->expr2) == FAILURE)
8219
        t = FAILURE;
8220
 
8221
      if (code->op == EXEC_ALLOCATE
8222
          && gfc_resolve_expr (code->expr3) == FAILURE)
8223
        t = FAILURE;
8224
 
8225
      switch (code->op)
8226
        {
8227
        case EXEC_NOP:
8228
        case EXEC_END_BLOCK:
8229
        case EXEC_CYCLE:
8230
        case EXEC_PAUSE:
8231
        case EXEC_STOP:
8232
        case EXEC_EXIT:
8233
        case EXEC_CONTINUE:
8234
        case EXEC_DT_END:
8235
        case EXEC_ASSIGN_CALL:
8236
          break;
8237
 
8238
        case EXEC_ENTRY:
8239
          /* Keep track of which entry we are up to.  */
8240
          current_entry_id = code->ext.entry->id;
8241
          break;
8242
 
8243
        case EXEC_WHERE:
8244
          resolve_where (code, NULL);
8245
          break;
8246
 
8247
        case EXEC_GOTO:
8248
          if (code->expr1 != NULL)
8249
            {
8250
              if (code->expr1->ts.type != BT_INTEGER)
8251
                gfc_error ("ASSIGNED GOTO statement at %L requires an "
8252
                           "INTEGER variable", &code->expr1->where);
8253
              else if (code->expr1->symtree->n.sym->attr.assign != 1)
8254
                gfc_error ("Variable '%s' has not been assigned a target "
8255
                           "label at %L", code->expr1->symtree->n.sym->name,
8256
                           &code->expr1->where);
8257
            }
8258
          else
8259
            resolve_branch (code->label1, code);
8260
          break;
8261
 
8262
        case EXEC_RETURN:
8263
          if (code->expr1 != NULL
8264
                && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8265
            gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8266
                       "INTEGER return specifier", &code->expr1->where);
8267
          break;
8268
 
8269
        case EXEC_INIT_ASSIGN:
8270
        case EXEC_END_PROCEDURE:
8271
          break;
8272
 
8273
        case EXEC_ASSIGN:
8274
          if (t == FAILURE)
8275
            break;
8276
 
8277
          if (resolve_ordinary_assign (code, ns))
8278
            {
8279
              if (code->op == EXEC_COMPCALL)
8280
                goto compcall;
8281
              else
8282
                goto call;
8283
            }
8284
          break;
8285
 
8286
        case EXEC_LABEL_ASSIGN:
8287
          if (code->label1->defined == ST_LABEL_UNKNOWN)
8288
            gfc_error ("Label %d referenced at %L is never defined",
8289
                       code->label1->value, &code->label1->where);
8290
          if (t == SUCCESS
8291
              && (code->expr1->expr_type != EXPR_VARIABLE
8292
                  || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8293
                  || code->expr1->symtree->n.sym->ts.kind
8294
                     != gfc_default_integer_kind
8295
                  || code->expr1->symtree->n.sym->as != NULL))
8296
            gfc_error ("ASSIGN statement at %L requires a scalar "
8297
                       "default INTEGER variable", &code->expr1->where);
8298
          break;
8299
 
8300
        case EXEC_POINTER_ASSIGN:
8301
          if (t == FAILURE)
8302
            break;
8303
 
8304
          gfc_check_pointer_assign (code->expr1, code->expr2);
8305
          break;
8306
 
8307
        case EXEC_ARITHMETIC_IF:
8308
          if (t == SUCCESS
8309
              && code->expr1->ts.type != BT_INTEGER
8310
              && code->expr1->ts.type != BT_REAL)
8311
            gfc_error ("Arithmetic IF statement at %L requires a numeric "
8312
                       "expression", &code->expr1->where);
8313
 
8314
          resolve_branch (code->label1, code);
8315
          resolve_branch (code->label2, code);
8316
          resolve_branch (code->label3, code);
8317
          break;
8318
 
8319
        case EXEC_IF:
8320
          if (t == SUCCESS && code->expr1 != NULL
8321
              && (code->expr1->ts.type != BT_LOGICAL
8322
                  || code->expr1->rank != 0))
8323
            gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8324
                       &code->expr1->where);
8325
          break;
8326
 
8327
        case EXEC_CALL:
8328
        call:
8329
          resolve_call (code);
8330
          break;
8331
 
8332
        case EXEC_COMPCALL:
8333
        compcall:
8334
          resolve_typebound_subroutine (code);
8335
          break;
8336
 
8337
        case EXEC_CALL_PPC:
8338
          resolve_ppc_call (code);
8339
          break;
8340
 
8341
        case EXEC_SELECT:
8342
          /* Select is complicated. Also, a SELECT construct could be
8343
             a transformed computed GOTO.  */
8344
          resolve_select (code);
8345
          break;
8346
 
8347
        case EXEC_SELECT_TYPE:
8348
          resolve_select_type (code);
8349
          break;
8350
 
8351
        case EXEC_BLOCK:
8352
          gfc_resolve (code->ext.ns);
8353
          break;
8354
 
8355
        case EXEC_DO:
8356
          if (code->ext.iterator != NULL)
8357
            {
8358
              gfc_iterator *iter = code->ext.iterator;
8359
              if (gfc_resolve_iterator (iter, true) != FAILURE)
8360
                gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8361
            }
8362
          break;
8363
 
8364
        case EXEC_DO_WHILE:
8365
          if (code->expr1 == NULL)
8366
            gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8367
          if (t == SUCCESS
8368
              && (code->expr1->rank != 0
8369
                  || code->expr1->ts.type != BT_LOGICAL))
8370
            gfc_error ("Exit condition of DO WHILE loop at %L must be "
8371
                       "a scalar LOGICAL expression", &code->expr1->where);
8372
          break;
8373
 
8374
        case EXEC_ALLOCATE:
8375
          if (t == SUCCESS)
8376
            resolve_allocate_deallocate (code, "ALLOCATE");
8377
 
8378
          break;
8379
 
8380
        case EXEC_DEALLOCATE:
8381
          if (t == SUCCESS)
8382
            resolve_allocate_deallocate (code, "DEALLOCATE");
8383
 
8384
          break;
8385
 
8386
        case EXEC_OPEN:
8387
          if (gfc_resolve_open (code->ext.open) == FAILURE)
8388
            break;
8389
 
8390
          resolve_branch (code->ext.open->err, code);
8391
          break;
8392
 
8393
        case EXEC_CLOSE:
8394
          if (gfc_resolve_close (code->ext.close) == FAILURE)
8395
            break;
8396
 
8397
          resolve_branch (code->ext.close->err, code);
8398
          break;
8399
 
8400
        case EXEC_BACKSPACE:
8401
        case EXEC_ENDFILE:
8402
        case EXEC_REWIND:
8403
        case EXEC_FLUSH:
8404
          if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8405
            break;
8406
 
8407
          resolve_branch (code->ext.filepos->err, code);
8408
          break;
8409
 
8410
        case EXEC_INQUIRE:
8411
          if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8412
              break;
8413
 
8414
          resolve_branch (code->ext.inquire->err, code);
8415
          break;
8416
 
8417
        case EXEC_IOLENGTH:
8418
          gcc_assert (code->ext.inquire != NULL);
8419
          if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8420
            break;
8421
 
8422
          resolve_branch (code->ext.inquire->err, code);
8423
          break;
8424
 
8425
        case EXEC_WAIT:
8426
          if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8427
            break;
8428
 
8429
          resolve_branch (code->ext.wait->err, code);
8430
          resolve_branch (code->ext.wait->end, code);
8431
          resolve_branch (code->ext.wait->eor, code);
8432
          break;
8433
 
8434
        case EXEC_READ:
8435
        case EXEC_WRITE:
8436
          if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8437
            break;
8438
 
8439
          resolve_branch (code->ext.dt->err, code);
8440
          resolve_branch (code->ext.dt->end, code);
8441
          resolve_branch (code->ext.dt->eor, code);
8442
          break;
8443
 
8444
        case EXEC_TRANSFER:
8445
          resolve_transfer (code);
8446
          break;
8447
 
8448
        case EXEC_FORALL:
8449
          resolve_forall_iterators (code->ext.forall_iterator);
8450
 
8451
          if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8452
            gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8453
                       "expression", &code->expr1->where);
8454
          break;
8455
 
8456
        case EXEC_OMP_ATOMIC:
8457
        case EXEC_OMP_BARRIER:
8458
        case EXEC_OMP_CRITICAL:
8459
        case EXEC_OMP_FLUSH:
8460
        case EXEC_OMP_DO:
8461
        case EXEC_OMP_MASTER:
8462
        case EXEC_OMP_ORDERED:
8463
        case EXEC_OMP_SECTIONS:
8464
        case EXEC_OMP_SINGLE:
8465
        case EXEC_OMP_TASKWAIT:
8466
        case EXEC_OMP_WORKSHARE:
8467
          gfc_resolve_omp_directive (code, ns);
8468
          break;
8469
 
8470
        case EXEC_OMP_PARALLEL:
8471
        case EXEC_OMP_PARALLEL_DO:
8472
        case EXEC_OMP_PARALLEL_SECTIONS:
8473
        case EXEC_OMP_PARALLEL_WORKSHARE:
8474
        case EXEC_OMP_TASK:
8475
          omp_workshare_save = omp_workshare_flag;
8476
          omp_workshare_flag = 0;
8477
          gfc_resolve_omp_directive (code, ns);
8478
          omp_workshare_flag = omp_workshare_save;
8479
          break;
8480
 
8481
        default:
8482
          gfc_internal_error ("resolve_code(): Bad statement code");
8483
        }
8484
    }
8485
 
8486
  cs_base = frame.prev;
8487
}
8488
 
8489
 
8490
/* Resolve initial values and make sure they are compatible with
8491
   the variable.  */
8492
 
8493
static void
8494
resolve_values (gfc_symbol *sym)
8495
{
8496
  if (sym->value == NULL)
8497
    return;
8498
 
8499
  if (gfc_resolve_expr (sym->value) == FAILURE)
8500
    return;
8501
 
8502
  gfc_check_assign_symbol (sym, sym->value);
8503
}
8504
 
8505
 
8506
/* Verify the binding labels for common blocks that are BIND(C).  The label
8507
   for a BIND(C) common block must be identical in all scoping units in which
8508
   the common block is declared.  Further, the binding label can not collide
8509
   with any other global entity in the program.  */
8510
 
8511
static void
8512
resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8513
{
8514
  if (comm_block_tree->n.common->is_bind_c == 1)
8515
    {
8516
      gfc_gsymbol *binding_label_gsym;
8517
      gfc_gsymbol *comm_name_gsym;
8518
 
8519
      /* See if a global symbol exists by the common block's name.  It may
8520
         be NULL if the common block is use-associated.  */
8521
      comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8522
                                         comm_block_tree->n.common->name);
8523
      if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8524
        gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8525
                   "with the global entity '%s' at %L",
8526
                   comm_block_tree->n.common->binding_label,
8527
                   comm_block_tree->n.common->name,
8528
                   &(comm_block_tree->n.common->where),
8529
                   comm_name_gsym->name, &(comm_name_gsym->where));
8530
      else if (comm_name_gsym != NULL
8531
               && strcmp (comm_name_gsym->name,
8532
                          comm_block_tree->n.common->name) == 0)
8533
        {
8534
          /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8535
             as expected.  */
8536
          if (comm_name_gsym->binding_label == NULL)
8537
            /* No binding label for common block stored yet; save this one.  */
8538
            comm_name_gsym->binding_label =
8539
              comm_block_tree->n.common->binding_label;
8540
          else
8541
            if (strcmp (comm_name_gsym->binding_label,
8542
                        comm_block_tree->n.common->binding_label) != 0)
8543
              {
8544
                /* Common block names match but binding labels do not.  */
8545
                gfc_error ("Binding label '%s' for common block '%s' at %L "
8546
                           "does not match the binding label '%s' for common "
8547
                           "block '%s' at %L",
8548
                           comm_block_tree->n.common->binding_label,
8549
                           comm_block_tree->n.common->name,
8550
                           &(comm_block_tree->n.common->where),
8551
                           comm_name_gsym->binding_label,
8552
                           comm_name_gsym->name,
8553
                           &(comm_name_gsym->where));
8554
                return;
8555
              }
8556
        }
8557
 
8558
      /* There is no binding label (NAME="") so we have nothing further to
8559
         check and nothing to add as a global symbol for the label.  */
8560
      if (comm_block_tree->n.common->binding_label[0] == '\0' )
8561
        return;
8562
 
8563
      binding_label_gsym =
8564
        gfc_find_gsymbol (gfc_gsym_root,
8565
                          comm_block_tree->n.common->binding_label);
8566
      if (binding_label_gsym == NULL)
8567
        {
8568
          /* Need to make a global symbol for the binding label to prevent
8569
             it from colliding with another.  */
8570
          binding_label_gsym =
8571
            gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8572
          binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8573
          binding_label_gsym->type = GSYM_COMMON;
8574
        }
8575
      else
8576
        {
8577
          /* If comm_name_gsym is NULL, the name common block is use
8578
             associated and the name could be colliding.  */
8579
          if (binding_label_gsym->type != GSYM_COMMON)
8580
            gfc_error ("Binding label '%s' for common block '%s' at %L "
8581
                       "collides with the global entity '%s' at %L",
8582
                       comm_block_tree->n.common->binding_label,
8583
                       comm_block_tree->n.common->name,
8584
                       &(comm_block_tree->n.common->where),
8585
                       binding_label_gsym->name,
8586
                       &(binding_label_gsym->where));
8587
          else if (comm_name_gsym != NULL
8588
                   && (strcmp (binding_label_gsym->name,
8589
                               comm_name_gsym->binding_label) != 0)
8590
                   && (strcmp (binding_label_gsym->sym_name,
8591
                               comm_name_gsym->name) != 0))
8592
            gfc_error ("Binding label '%s' for common block '%s' at %L "
8593
                       "collides with global entity '%s' at %L",
8594
                       binding_label_gsym->name, binding_label_gsym->sym_name,
8595
                       &(comm_block_tree->n.common->where),
8596
                       comm_name_gsym->name, &(comm_name_gsym->where));
8597
        }
8598
    }
8599
 
8600
  return;
8601
}
8602
 
8603
 
8604
/* Verify any BIND(C) derived types in the namespace so we can report errors
8605
   for them once, rather than for each variable declared of that type.  */
8606
 
8607
static void
8608
resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8609
{
8610
  if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8611
      && derived_sym->attr.is_bind_c == 1)
8612
    verify_bind_c_derived_type (derived_sym);
8613
 
8614
  return;
8615
}
8616
 
8617
 
8618
/* Verify that any binding labels used in a given namespace do not collide
8619
   with the names or binding labels of any global symbols.  */
8620
 
8621
static void
8622
gfc_verify_binding_labels (gfc_symbol *sym)
8623
{
8624
  int has_error = 0;
8625
 
8626
  if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8627
      && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8628
    {
8629
      gfc_gsymbol *bind_c_sym;
8630
 
8631
      bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8632
      if (bind_c_sym != NULL
8633
          && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8634
        {
8635
          if (sym->attr.if_source == IFSRC_DECL
8636
              && (bind_c_sym->type != GSYM_SUBROUTINE
8637
                  && bind_c_sym->type != GSYM_FUNCTION)
8638
              && ((sym->attr.contained == 1
8639
                   && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8640
                  || (sym->attr.use_assoc == 1
8641
                      && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8642
            {
8643
              /* Make sure global procedures don't collide with anything.  */
8644
              gfc_error ("Binding label '%s' at %L collides with the global "
8645
                         "entity '%s' at %L", sym->binding_label,
8646
                         &(sym->declared_at), bind_c_sym->name,
8647
                         &(bind_c_sym->where));
8648
              has_error = 1;
8649
            }
8650
          else if (sym->attr.contained == 0
8651
                   && (sym->attr.if_source == IFSRC_IFBODY
8652
                       && sym->attr.flavor == FL_PROCEDURE)
8653
                   && (bind_c_sym->sym_name != NULL
8654
                       && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8655
            {
8656
              /* Make sure procedures in interface bodies don't collide.  */
8657
              gfc_error ("Binding label '%s' in interface body at %L collides "
8658
                         "with the global entity '%s' at %L",
8659
                         sym->binding_label,
8660
                         &(sym->declared_at), bind_c_sym->name,
8661
                         &(bind_c_sym->where));
8662
              has_error = 1;
8663
            }
8664
          else if (sym->attr.contained == 0
8665
                   && sym->attr.if_source == IFSRC_UNKNOWN)
8666
            if ((sym->attr.use_assoc && bind_c_sym->mod_name
8667
                 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8668
                || sym->attr.use_assoc == 0)
8669
              {
8670
                gfc_error ("Binding label '%s' at %L collides with global "
8671
                           "entity '%s' at %L", sym->binding_label,
8672
                           &(sym->declared_at), bind_c_sym->name,
8673
                           &(bind_c_sym->where));
8674
                has_error = 1;
8675
              }
8676
 
8677
          if (has_error != 0)
8678
            /* Clear the binding label to prevent checking multiple times.  */
8679
            sym->binding_label[0] = '\0';
8680
        }
8681
      else if (bind_c_sym == NULL)
8682
        {
8683
          bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8684
          bind_c_sym->where = sym->declared_at;
8685
          bind_c_sym->sym_name = sym->name;
8686
 
8687
          if (sym->attr.use_assoc == 1)
8688
            bind_c_sym->mod_name = sym->module;
8689
          else
8690
            if (sym->ns->proc_name != NULL)
8691
              bind_c_sym->mod_name = sym->ns->proc_name->name;
8692
 
8693
          if (sym->attr.contained == 0)
8694
            {
8695
              if (sym->attr.subroutine)
8696
                bind_c_sym->type = GSYM_SUBROUTINE;
8697
              else if (sym->attr.function)
8698
                bind_c_sym->type = GSYM_FUNCTION;
8699
            }
8700
        }
8701
    }
8702
  return;
8703
}
8704
 
8705
 
8706
/* Resolve an index expression.  */
8707
 
8708
static gfc_try
8709
resolve_index_expr (gfc_expr *e)
8710
{
8711
  if (gfc_resolve_expr (e) == FAILURE)
8712
    return FAILURE;
8713
 
8714
  if (gfc_simplify_expr (e, 0) == FAILURE)
8715
    return FAILURE;
8716
 
8717
  if (gfc_specification_expr (e) == FAILURE)
8718
    return FAILURE;
8719
 
8720
  return SUCCESS;
8721
}
8722
 
8723
/* Resolve a charlen structure.  */
8724
 
8725
static gfc_try
8726
resolve_charlen (gfc_charlen *cl)
8727
{
8728
  int i, k;
8729
 
8730
  if (cl->resolved)
8731
    return SUCCESS;
8732
 
8733
  cl->resolved = 1;
8734
 
8735
  specification_expr = 1;
8736
 
8737
  if (resolve_index_expr (cl->length) == FAILURE)
8738
    {
8739
      specification_expr = 0;
8740
      return FAILURE;
8741
    }
8742
 
8743
  /* "If the character length parameter value evaluates to a negative
8744
     value, the length of character entities declared is zero."  */
8745
  if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8746
    {
8747
      if (gfc_option.warn_surprising)
8748
        gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8749
                         " the length has been set to zero",
8750
                         &cl->length->where, i);
8751
      gfc_replace_expr (cl->length, gfc_int_expr (0));
8752
    }
8753
 
8754
  /* Check that the character length is not too large.  */
8755
  k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8756
  if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8757
      && cl->length->ts.type == BT_INTEGER
8758
      && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8759
    {
8760
      gfc_error ("String length at %L is too large", &cl->length->where);
8761
      return FAILURE;
8762
    }
8763
 
8764
  return SUCCESS;
8765
}
8766
 
8767
 
8768
/* Test for non-constant shape arrays.  */
8769
 
8770
static bool
8771
is_non_constant_shape_array (gfc_symbol *sym)
8772
{
8773
  gfc_expr *e;
8774
  int i;
8775
  bool not_constant;
8776
 
8777
  not_constant = false;
8778
  if (sym->as != NULL)
8779
    {
8780
      /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8781
         has not been simplified; parameter array references.  Do the
8782
         simplification now.  */
8783
      for (i = 0; i < sym->as->rank; i++)
8784
        {
8785
          e = sym->as->lower[i];
8786
          if (e && (resolve_index_expr (e) == FAILURE
8787
                    || !gfc_is_constant_expr (e)))
8788
            not_constant = true;
8789
 
8790
          e = sym->as->upper[i];
8791
          if (e && (resolve_index_expr (e) == FAILURE
8792
                    || !gfc_is_constant_expr (e)))
8793
            not_constant = true;
8794
        }
8795
    }
8796
  return not_constant;
8797
}
8798
 
8799
/* Given a symbol and an initialization expression, add code to initialize
8800
   the symbol to the function entry.  */
8801
static void
8802
build_init_assign (gfc_symbol *sym, gfc_expr *init)
8803
{
8804
  gfc_expr *lval;
8805
  gfc_code *init_st;
8806
  gfc_namespace *ns = sym->ns;
8807
 
8808
  /* Search for the function namespace if this is a contained
8809
     function without an explicit result.  */
8810
  if (sym->attr.function && sym == sym->result
8811
      && sym->name != sym->ns->proc_name->name)
8812
    {
8813
      ns = ns->contained;
8814
      for (;ns; ns = ns->sibling)
8815
        if (strcmp (ns->proc_name->name, sym->name) == 0)
8816
          break;
8817
    }
8818
 
8819
  if (ns == NULL)
8820
    {
8821
      gfc_free_expr (init);
8822
      return;
8823
    }
8824
 
8825
  /* Build an l-value expression for the result.  */
8826
  lval = gfc_lval_expr_from_sym (sym);
8827
 
8828
  /* Add the code at scope entry.  */
8829
  init_st = gfc_get_code ();
8830
  init_st->next = ns->code;
8831
  ns->code = init_st;
8832
 
8833
  /* Assign the default initializer to the l-value.  */
8834
  init_st->loc = sym->declared_at;
8835
  init_st->op = EXEC_INIT_ASSIGN;
8836
  init_st->expr1 = lval;
8837
  init_st->expr2 = init;
8838
}
8839
 
8840
/* Assign the default initializer to a derived type variable or result.  */
8841
 
8842
static void
8843
apply_default_init (gfc_symbol *sym)
8844
{
8845
  gfc_expr *init = NULL;
8846
 
8847
  if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8848
    return;
8849
 
8850
  if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8851
    init = gfc_default_initializer (&sym->ts);
8852
 
8853
  if (init == NULL)
8854
    return;
8855
 
8856
  build_init_assign (sym, init);
8857
}
8858
 
8859
/* Build an initializer for a local integer, real, complex, logical, or
8860
   character variable, based on the command line flags finit-local-zero,
8861
   finit-integer=, finit-real=, finit-logical=, and finit-runtime.  Returns
8862
   null if the symbol should not have a default initialization.  */
8863
static gfc_expr *
8864
build_default_init_expr (gfc_symbol *sym)
8865
{
8866
  int char_len;
8867
  gfc_expr *init_expr;
8868
  int i;
8869
 
8870
  /* These symbols should never have a default initialization.  */
8871
  if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8872
      || sym->attr.external
8873
      || sym->attr.dummy
8874
      || sym->attr.pointer
8875
      || sym->attr.in_equivalence
8876
      || sym->attr.in_common
8877
      || sym->attr.data
8878
      || sym->module
8879
      || sym->attr.cray_pointee
8880
      || sym->attr.cray_pointer)
8881
    return NULL;
8882
 
8883
  /* Now we'll try to build an initializer expression.  */
8884
  init_expr = gfc_get_expr ();
8885
  init_expr->expr_type = EXPR_CONSTANT;
8886
  init_expr->ts.type = sym->ts.type;
8887
  init_expr->ts.kind = sym->ts.kind;
8888
  init_expr->where = sym->declared_at;
8889
 
8890
  /* We will only initialize integers, reals, complex, logicals, and
8891
     characters, and only if the corresponding command-line flags
8892
     were set.  Otherwise, we free init_expr and return null.  */
8893
  switch (sym->ts.type)
8894
    {
8895
    case BT_INTEGER:
8896
      if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8897
        mpz_init_set_si (init_expr->value.integer,
8898
                         gfc_option.flag_init_integer_value);
8899
      else
8900
        {
8901
          gfc_free_expr (init_expr);
8902
          init_expr = NULL;
8903
        }
8904
      break;
8905
 
8906
    case BT_REAL:
8907
      mpfr_init (init_expr->value.real);
8908
      switch (gfc_option.flag_init_real)
8909
        {
8910
        case GFC_INIT_REAL_SNAN:
8911
          init_expr->is_snan = 1;
8912
          /* Fall through.  */
8913
        case GFC_INIT_REAL_NAN:
8914
          mpfr_set_nan (init_expr->value.real);
8915
          break;
8916
 
8917
        case GFC_INIT_REAL_INF:
8918
          mpfr_set_inf (init_expr->value.real, 1);
8919
          break;
8920
 
8921
        case GFC_INIT_REAL_NEG_INF:
8922
          mpfr_set_inf (init_expr->value.real, -1);
8923
          break;
8924
 
8925
        case GFC_INIT_REAL_ZERO:
8926
          mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8927
          break;
8928
 
8929
        default:
8930
          gfc_free_expr (init_expr);
8931
          init_expr = NULL;
8932
          break;
8933
        }
8934
      break;
8935
 
8936
    case BT_COMPLEX:
8937
      mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8938
      switch (gfc_option.flag_init_real)
8939
        {
8940
        case GFC_INIT_REAL_SNAN:
8941
          init_expr->is_snan = 1;
8942
          /* Fall through.  */
8943
        case GFC_INIT_REAL_NAN:
8944
          mpfr_set_nan (mpc_realref (init_expr->value.complex));
8945
          mpfr_set_nan (mpc_imagref (init_expr->value.complex));
8946
          break;
8947
 
8948
        case GFC_INIT_REAL_INF:
8949
          mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8950
          mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
8951
          break;
8952
 
8953
        case GFC_INIT_REAL_NEG_INF:
8954
          mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8955
          mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
8956
          break;
8957
 
8958
        case GFC_INIT_REAL_ZERO:
8959
          mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8960
          break;
8961
 
8962
        default:
8963
          gfc_free_expr (init_expr);
8964
          init_expr = NULL;
8965
          break;
8966
        }
8967
      break;
8968
 
8969
    case BT_LOGICAL:
8970
      if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8971
        init_expr->value.logical = 0;
8972
      else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8973
        init_expr->value.logical = 1;
8974
      else
8975
        {
8976
          gfc_free_expr (init_expr);
8977
          init_expr = NULL;
8978
        }
8979
      break;
8980
 
8981
    case BT_CHARACTER:
8982
      /* For characters, the length must be constant in order to
8983
         create a default initializer.  */
8984
      if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
8985
          && sym->ts.u.cl->length
8986
          && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8987
        {
8988
          char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
8989
          init_expr->value.character.length = char_len;
8990
          init_expr->value.character.string = gfc_get_wide_string (char_len+1);
8991
          for (i = 0; i < char_len; i++)
8992
            init_expr->value.character.string[i]
8993
              = (unsigned char) gfc_option.flag_init_character_value;
8994
        }
8995
      else
8996
        {
8997
          gfc_free_expr (init_expr);
8998
          init_expr = NULL;
8999
        }
9000
      break;
9001
 
9002
    default:
9003
     gfc_free_expr (init_expr);
9004
     init_expr = NULL;
9005
    }
9006
  return init_expr;
9007
}
9008
 
9009
/* Add an initialization expression to a local variable.  */
9010
static void
9011
apply_default_init_local (gfc_symbol *sym)
9012
{
9013
  gfc_expr *init = NULL;
9014
 
9015
  /* The symbol should be a variable or a function return value.  */
9016
  if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9017
      || (sym->attr.function && sym->result != sym))
9018
    return;
9019
 
9020
  /* Try to build the initializer expression.  If we can't initialize
9021
     this symbol, then init will be NULL.  */
9022
  init = build_default_init_expr (sym);
9023
  if (init == NULL)
9024
    return;
9025
 
9026
  /* For saved variables, we don't want to add an initializer at
9027
     function entry, so we just add a static initializer.  */
9028
  if (sym->attr.save || sym->ns->save_all
9029
      || gfc_option.flag_max_stack_var_size == 0)
9030
    {
9031
      /* Don't clobber an existing initializer!  */
9032
      gcc_assert (sym->value == NULL);
9033
      sym->value = init;
9034
      return;
9035
    }
9036
 
9037
  build_init_assign (sym, init);
9038
}
9039
 
9040
/* Resolution of common features of flavors variable and procedure.  */
9041
 
9042
static gfc_try
9043
resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9044
{
9045
  /* Constraints on deferred shape variable.  */
9046
  if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9047
    {
9048
      if (sym->attr.allocatable)
9049
        {
9050
          if (sym->attr.dimension)
9051
            {
9052
              gfc_error ("Allocatable array '%s' at %L must have "
9053
                         "a deferred shape", sym->name, &sym->declared_at);
9054
              return FAILURE;
9055
            }
9056
          else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9057
                                   "may not be ALLOCATABLE", sym->name,
9058
                                   &sym->declared_at) == FAILURE)
9059
            return FAILURE;
9060
        }
9061
 
9062
      if (sym->attr.pointer && sym->attr.dimension)
9063
        {
9064
          gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9065
                     sym->name, &sym->declared_at);
9066
          return FAILURE;
9067
        }
9068
 
9069
    }
9070
  else
9071
    {
9072
      if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9073
          && !sym->attr.dummy && sym->ts.type != BT_CLASS)
9074
        {
9075
          gfc_error ("Array '%s' at %L cannot have a deferred shape",
9076
                     sym->name, &sym->declared_at);
9077
          return FAILURE;
9078
         }
9079
    }
9080
  return SUCCESS;
9081
}
9082
 
9083
 
9084
/* Additional checks for symbols with flavor variable and derived
9085
   type.  To be called from resolve_fl_variable.  */
9086
 
9087
static gfc_try
9088
resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9089
{
9090
  gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9091
 
9092
  /* Check to see if a derived type is blocked from being host
9093
     associated by the presence of another class I symbol in the same
9094
     namespace.  14.6.1.3 of the standard and the discussion on
9095
     comp.lang.fortran.  */
9096
  if (sym->ns != sym->ts.u.derived->ns
9097
      && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9098
    {
9099
      gfc_symbol *s;
9100
      gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9101
      if (s && s->attr.flavor != FL_DERIVED)
9102
        {
9103
          gfc_error ("The type '%s' cannot be host associated at %L "
9104
                     "because it is blocked by an incompatible object "
9105
                     "of the same name declared at %L",
9106
                     sym->ts.u.derived->name, &sym->declared_at,
9107
                     &s->declared_at);
9108
          return FAILURE;
9109
        }
9110
    }
9111
 
9112
  /* 4th constraint in section 11.3: "If an object of a type for which
9113
     component-initialization is specified (R429) appears in the
9114
     specification-part of a module and does not have the ALLOCATABLE
9115
     or POINTER attribute, the object shall have the SAVE attribute."
9116
 
9117
     The check for initializers is performed with
9118
     has_default_initializer because gfc_default_initializer generates
9119
     a hidden default for allocatable components.  */
9120
  if (!(sym->value || no_init_flag) && sym->ns->proc_name
9121
      && sym->ns->proc_name->attr.flavor == FL_MODULE
9122
      && !sym->ns->save_all && !sym->attr.save
9123
      && !sym->attr.pointer && !sym->attr.allocatable
9124
      && has_default_initializer (sym->ts.u.derived)
9125
      && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9126
                         "module variable '%s' at %L, needed due to "
9127
                         "the default initialization", sym->name,
9128
                         &sym->declared_at) == FAILURE)
9129
    return FAILURE;
9130
 
9131
  if (sym->ts.type == BT_CLASS)
9132
    {
9133
      /* C502.  */
9134
      if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
9135
        {
9136
          gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9137
                     sym->ts.u.derived->components->ts.u.derived->name,
9138
                     sym->name, &sym->declared_at);
9139
          return FAILURE;
9140
        }
9141
 
9142
      /* C509.  */
9143
      /* Assume that use associated symbols were checked in the module ns.  */
9144
      if (!sym->attr.class_ok && !sym->attr.use_assoc)
9145
        {
9146
          gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9147
                     "or pointer", sym->name, &sym->declared_at);
9148
          return FAILURE;
9149
        }
9150
    }
9151
 
9152
  /* Assign default initializer.  */
9153
  if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9154
      && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9155
    {
9156
      sym->value = gfc_default_initializer (&sym->ts);
9157
    }
9158
 
9159
  return SUCCESS;
9160
}
9161
 
9162
 
9163
/* Resolve symbols with flavor variable.  */
9164
 
9165
static gfc_try
9166
resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9167
{
9168
  int no_init_flag, automatic_flag;
9169
  gfc_expr *e;
9170
  const char *auto_save_msg;
9171
 
9172
  auto_save_msg = "Automatic object '%s' at %L cannot have the "
9173
                  "SAVE attribute";
9174
 
9175
  if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9176
    return FAILURE;
9177
 
9178
  /* Set this flag to check that variables are parameters of all entries.
9179
     This check is effected by the call to gfc_resolve_expr through
9180
     is_non_constant_shape_array.  */
9181
  specification_expr = 1;
9182
 
9183
  if (sym->ns->proc_name
9184
      && (sym->ns->proc_name->attr.flavor == FL_MODULE
9185
          || sym->ns->proc_name->attr.is_main_program)
9186
      && !sym->attr.use_assoc
9187
      && !sym->attr.allocatable
9188
      && !sym->attr.pointer
9189
      && is_non_constant_shape_array (sym))
9190
    {
9191
      /* The shape of a main program or module array needs to be
9192
         constant.  */
9193
      gfc_error ("The module or main program array '%s' at %L must "
9194
                 "have constant shape", sym->name, &sym->declared_at);
9195
      specification_expr = 0;
9196
      return FAILURE;
9197
    }
9198
 
9199
  if (sym->ts.type == BT_CHARACTER)
9200
    {
9201
      /* Make sure that character string variables with assumed length are
9202
         dummy arguments.  */
9203
      e = sym->ts.u.cl->length;
9204
      if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9205
        {
9206
          gfc_error ("Entity with assumed character length at %L must be a "
9207
                     "dummy argument or a PARAMETER", &sym->declared_at);
9208
          return FAILURE;
9209
        }
9210
 
9211
      if (e && sym->attr.save && !gfc_is_constant_expr (e))
9212
        {
9213
          gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9214
          return FAILURE;
9215
        }
9216
 
9217
      if (!gfc_is_constant_expr (e)
9218
          && !(e->expr_type == EXPR_VARIABLE
9219
               && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9220
          && sym->ns->proc_name
9221
          && (sym->ns->proc_name->attr.flavor == FL_MODULE
9222
              || sym->ns->proc_name->attr.is_main_program)
9223
          && !sym->attr.use_assoc)
9224
        {
9225
          gfc_error ("'%s' at %L must have constant character length "
9226
                     "in this context", sym->name, &sym->declared_at);
9227
          return FAILURE;
9228
        }
9229
    }
9230
 
9231
  if (sym->value == NULL && sym->attr.referenced)
9232
    apply_default_init_local (sym); /* Try to apply a default initialization.  */
9233
 
9234
  /* Determine if the symbol may not have an initializer.  */
9235
  no_init_flag = automatic_flag = 0;
9236
  if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9237
      || sym->attr.intrinsic || sym->attr.result)
9238
    no_init_flag = 1;
9239
  else if (sym->attr.dimension && !sym->attr.pointer
9240
           && is_non_constant_shape_array (sym))
9241
    {
9242
      no_init_flag = automatic_flag = 1;
9243
 
9244
      /* Also, they must not have the SAVE attribute.
9245
         SAVE_IMPLICIT is checked below.  */
9246
      if (sym->attr.save == SAVE_EXPLICIT)
9247
        {
9248
          gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9249
          return FAILURE;
9250
        }
9251
    }
9252
 
9253
  /* Ensure that any initializer is simplified.  */
9254
  if (sym->value)
9255
    gfc_simplify_expr (sym->value, 1);
9256
 
9257
  /* Reject illegal initializers.  */
9258
  if (!sym->mark && sym->value)
9259
    {
9260
      if (sym->attr.allocatable)
9261
        gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9262
                   sym->name, &sym->declared_at);
9263
      else if (sym->attr.external)
9264
        gfc_error ("External '%s' at %L cannot have an initializer",
9265
                   sym->name, &sym->declared_at);
9266
      else if (sym->attr.dummy
9267
        && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9268
        gfc_error ("Dummy '%s' at %L cannot have an initializer",
9269
                   sym->name, &sym->declared_at);
9270
      else if (sym->attr.intrinsic)
9271
        gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9272
                   sym->name, &sym->declared_at);
9273
      else if (sym->attr.result)
9274
        gfc_error ("Function result '%s' at %L cannot have an initializer",
9275
                   sym->name, &sym->declared_at);
9276
      else if (automatic_flag)
9277
        gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9278
                   sym->name, &sym->declared_at);
9279
      else
9280
        goto no_init_error;
9281
      return FAILURE;
9282
    }
9283
 
9284
no_init_error:
9285
  if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9286
    return resolve_fl_variable_derived (sym, no_init_flag);
9287
 
9288
  return SUCCESS;
9289
}
9290
 
9291
 
9292
/* Resolve a procedure.  */
9293
 
9294
static gfc_try
9295
resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9296
{
9297
  gfc_formal_arglist *arg;
9298
 
9299
  if (sym->attr.function
9300
      && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9301
    return FAILURE;
9302
 
9303
  if (sym->ts.type == BT_CHARACTER)
9304
    {
9305
      gfc_charlen *cl = sym->ts.u.cl;
9306
 
9307
      if (cl && cl->length && gfc_is_constant_expr (cl->length)
9308
             && resolve_charlen (cl) == FAILURE)
9309
        return FAILURE;
9310
 
9311
      if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9312
          && sym->attr.proc == PROC_ST_FUNCTION)
9313
        {
9314
          gfc_error ("Character-valued statement function '%s' at %L must "
9315
                     "have constant length", sym->name, &sym->declared_at);
9316
          return FAILURE;
9317
        }
9318
    }
9319
 
9320
  /* Ensure that derived type for are not of a private type.  Internal
9321
     module procedures are excluded by 2.2.3.3 - i.e., they are not
9322
     externally accessible and can access all the objects accessible in
9323
     the host.  */
9324
  if (!(sym->ns->parent
9325
        && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9326
      && gfc_check_access(sym->attr.access, sym->ns->default_access))
9327
    {
9328
      gfc_interface *iface;
9329
 
9330
      for (arg = sym->formal; arg; arg = arg->next)
9331
        {
9332
          if (arg->sym
9333
              && arg->sym->ts.type == BT_DERIVED
9334
              && !arg->sym->ts.u.derived->attr.use_assoc
9335
              && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9336
                                    arg->sym->ts.u.derived->ns->default_access)
9337
              && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9338
                                 "PRIVATE type and cannot be a dummy argument"
9339
                                 " of '%s', which is PUBLIC at %L",
9340
                                 arg->sym->name, sym->name, &sym->declared_at)
9341
                 == FAILURE)
9342
            {
9343
              /* Stop this message from recurring.  */
9344
              arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9345
              return FAILURE;
9346
            }
9347
        }
9348
 
9349
      /* PUBLIC interfaces may expose PRIVATE procedures that take types
9350
         PRIVATE to the containing module.  */
9351
      for (iface = sym->generic; iface; iface = iface->next)
9352
        {
9353
          for (arg = iface->sym->formal; arg; arg = arg->next)
9354
            {
9355
              if (arg->sym
9356
                  && arg->sym->ts.type == BT_DERIVED
9357
                  && !arg->sym->ts.u.derived->attr.use_assoc
9358
                  && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9359
                                        arg->sym->ts.u.derived->ns->default_access)
9360
                  && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9361
                                     "'%s' in PUBLIC interface '%s' at %L "
9362
                                     "takes dummy arguments of '%s' which is "
9363
                                     "PRIVATE", iface->sym->name, sym->name,
9364
                                     &iface->sym->declared_at,
9365
                                     gfc_typename (&arg->sym->ts)) == FAILURE)
9366
                {
9367
                  /* Stop this message from recurring.  */
9368
                  arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9369
                  return FAILURE;
9370
                }
9371
             }
9372
        }
9373
 
9374
      /* PUBLIC interfaces may expose PRIVATE procedures that take types
9375
         PRIVATE to the containing module.  */
9376
      for (iface = sym->generic; iface; iface = iface->next)
9377
        {
9378
          for (arg = iface->sym->formal; arg; arg = arg->next)
9379
            {
9380
              if (arg->sym
9381
                  && arg->sym->ts.type == BT_DERIVED
9382
                  && !arg->sym->ts.u.derived->attr.use_assoc
9383
                  && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9384
                                        arg->sym->ts.u.derived->ns->default_access)
9385
                  && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9386
                                     "'%s' in PUBLIC interface '%s' at %L "
9387
                                     "takes dummy arguments of '%s' which is "
9388
                                     "PRIVATE", iface->sym->name, sym->name,
9389
                                     &iface->sym->declared_at,
9390
                                     gfc_typename (&arg->sym->ts)) == FAILURE)
9391
                {
9392
                  /* Stop this message from recurring.  */
9393
                  arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9394
                  return FAILURE;
9395
                }
9396
             }
9397
        }
9398
    }
9399
 
9400
  if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9401
      && !sym->attr.proc_pointer)
9402
    {
9403
      gfc_error ("Function '%s' at %L cannot have an initializer",
9404
                 sym->name, &sym->declared_at);
9405
      return FAILURE;
9406
    }
9407
 
9408
  /* An external symbol may not have an initializer because it is taken to be
9409
     a procedure. Exception: Procedure Pointers.  */
9410
  if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9411
    {
9412
      gfc_error ("External object '%s' at %L may not have an initializer",
9413
                 sym->name, &sym->declared_at);
9414
      return FAILURE;
9415
    }
9416
 
9417
  /* An elemental function is required to return a scalar 12.7.1  */
9418
  if (sym->attr.elemental && sym->attr.function && sym->as)
9419
    {
9420
      gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9421
                 "result", sym->name, &sym->declared_at);
9422
      /* Reset so that the error only occurs once.  */
9423
      sym->attr.elemental = 0;
9424
      return FAILURE;
9425
    }
9426
 
9427
  /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9428
     char-len-param shall not be array-valued, pointer-valued, recursive
9429
     or pure.  ....snip... A character value of * may only be used in the
9430
     following ways: (i) Dummy arg of procedure - dummy associates with
9431
     actual length; (ii) To declare a named constant; or (iii) External
9432
     function - but length must be declared in calling scoping unit.  */
9433
  if (sym->attr.function
9434
      && sym->ts.type == BT_CHARACTER
9435
      && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9436
    {
9437
      if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9438
          || (sym->attr.recursive) || (sym->attr.pure))
9439
        {
9440
          if (sym->as && sym->as->rank)
9441
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9442
                       "array-valued", sym->name, &sym->declared_at);
9443
 
9444
          if (sym->attr.pointer)
9445
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9446
                       "pointer-valued", sym->name, &sym->declared_at);
9447
 
9448
          if (sym->attr.pure)
9449
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9450
                       "pure", sym->name, &sym->declared_at);
9451
 
9452
          if (sym->attr.recursive)
9453
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9454
                       "recursive", sym->name, &sym->declared_at);
9455
 
9456
          return FAILURE;
9457
        }
9458
 
9459
      /* Appendix B.2 of the standard.  Contained functions give an
9460
         error anyway.  Fixed-form is likely to be F77/legacy.  */
9461
      if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9462
        gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9463
                        "CHARACTER(*) function '%s' at %L",
9464
                        sym->name, &sym->declared_at);
9465
    }
9466
 
9467
  if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9468
    {
9469
      gfc_formal_arglist *curr_arg;
9470
      int has_non_interop_arg = 0;
9471
 
9472
      if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9473
                             sym->common_block) == FAILURE)
9474
        {
9475
          /* Clear these to prevent looking at them again if there was an
9476
             error.  */
9477
          sym->attr.is_bind_c = 0;
9478
          sym->attr.is_c_interop = 0;
9479
          sym->ts.is_c_interop = 0;
9480
        }
9481
      else
9482
        {
9483
          /* So far, no errors have been found.  */
9484
          sym->attr.is_c_interop = 1;
9485
          sym->ts.is_c_interop = 1;
9486
        }
9487
 
9488
      curr_arg = sym->formal;
9489
      while (curr_arg != NULL)
9490
        {
9491
          /* Skip implicitly typed dummy args here.  */
9492
          if (curr_arg->sym->attr.implicit_type == 0)
9493
            if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9494
              /* If something is found to fail, record the fact so we
9495
                 can mark the symbol for the procedure as not being
9496
                 BIND(C) to try and prevent multiple errors being
9497
                 reported.  */
9498
              has_non_interop_arg = 1;
9499
 
9500
          curr_arg = curr_arg->next;
9501
        }
9502
 
9503
      /* See if any of the arguments were not interoperable and if so, clear
9504
         the procedure symbol to prevent duplicate error messages.  */
9505
      if (has_non_interop_arg != 0)
9506
        {
9507
          sym->attr.is_c_interop = 0;
9508
          sym->ts.is_c_interop = 0;
9509
          sym->attr.is_bind_c = 0;
9510
        }
9511
    }
9512
 
9513
  if (!sym->attr.proc_pointer)
9514
    {
9515
      if (sym->attr.save == SAVE_EXPLICIT)
9516
        {
9517
          gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9518
                     "in '%s' at %L", sym->name, &sym->declared_at);
9519
          return FAILURE;
9520
        }
9521
      if (sym->attr.intent)
9522
        {
9523
          gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9524
                     "in '%s' at %L", sym->name, &sym->declared_at);
9525
          return FAILURE;
9526
        }
9527
      if (sym->attr.subroutine && sym->attr.result)
9528
        {
9529
          gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9530
                     "in '%s' at %L", sym->name, &sym->declared_at);
9531
          return FAILURE;
9532
        }
9533
      if (sym->attr.external && sym->attr.function
9534
          && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9535
              || sym->attr.contained))
9536
        {
9537
          gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9538
                     "in '%s' at %L", sym->name, &sym->declared_at);
9539
          return FAILURE;
9540
        }
9541
      if (strcmp ("ppr@", sym->name) == 0)
9542
        {
9543
          gfc_error ("Procedure pointer result '%s' at %L "
9544
                     "is missing the pointer attribute",
9545
                     sym->ns->proc_name->name, &sym->declared_at);
9546
          return FAILURE;
9547
        }
9548
    }
9549
 
9550
  return SUCCESS;
9551
}
9552
 
9553
 
9554
/* Resolve a list of finalizer procedures.  That is, after they have hopefully
9555
   been defined and we now know their defined arguments, check that they fulfill
9556
   the requirements of the standard for procedures used as finalizers.  */
9557
 
9558
static gfc_try
9559
gfc_resolve_finalizers (gfc_symbol* derived)
9560
{
9561
  gfc_finalizer* list;
9562
  gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
9563
  gfc_try result = SUCCESS;
9564
  bool seen_scalar = false;
9565
 
9566
  if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9567
    return SUCCESS;
9568
 
9569
  /* Walk over the list of finalizer-procedures, check them, and if any one
9570
     does not fit in with the standard's definition, print an error and remove
9571
     it from the list.  */
9572
  prev_link = &derived->f2k_derived->finalizers;
9573
  for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9574
    {
9575
      gfc_symbol* arg;
9576
      gfc_finalizer* i;
9577
      int my_rank;
9578
 
9579
      /* Skip this finalizer if we already resolved it.  */
9580
      if (list->proc_tree)
9581
        {
9582
          prev_link = &(list->next);
9583
          continue;
9584
        }
9585
 
9586
      /* Check this exists and is a SUBROUTINE.  */
9587
      if (!list->proc_sym->attr.subroutine)
9588
        {
9589
          gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9590
                     list->proc_sym->name, &list->where);
9591
          goto error;
9592
        }
9593
 
9594
      /* We should have exactly one argument.  */
9595
      if (!list->proc_sym->formal || list->proc_sym->formal->next)
9596
        {
9597
          gfc_error ("FINAL procedure at %L must have exactly one argument",
9598
                     &list->where);
9599
          goto error;
9600
        }
9601
      arg = list->proc_sym->formal->sym;
9602
 
9603
      /* This argument must be of our type.  */
9604
      if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9605
        {
9606
          gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9607
                     &arg->declared_at, derived->name);
9608
          goto error;
9609
        }
9610
 
9611
      /* It must neither be a pointer nor allocatable nor optional.  */
9612
      if (arg->attr.pointer)
9613
        {
9614
          gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9615
                     &arg->declared_at);
9616
          goto error;
9617
        }
9618
      if (arg->attr.allocatable)
9619
        {
9620
          gfc_error ("Argument of FINAL procedure at %L must not be"
9621
                     " ALLOCATABLE", &arg->declared_at);
9622
          goto error;
9623
        }
9624
      if (arg->attr.optional)
9625
        {
9626
          gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9627
                     &arg->declared_at);
9628
          goto error;
9629
        }
9630
 
9631
      /* It must not be INTENT(OUT).  */
9632
      if (arg->attr.intent == INTENT_OUT)
9633
        {
9634
          gfc_error ("Argument of FINAL procedure at %L must not be"
9635
                     " INTENT(OUT)", &arg->declared_at);
9636
          goto error;
9637
        }
9638
 
9639
      /* Warn if the procedure is non-scalar and not assumed shape.  */
9640
      if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9641
          && arg->as->type != AS_ASSUMED_SHAPE)
9642
        gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9643
                     " shape argument", &arg->declared_at);
9644
 
9645
      /* Check that it does not match in kind and rank with a FINAL procedure
9646
         defined earlier.  To really loop over the *earlier* declarations,
9647
         we need to walk the tail of the list as new ones were pushed at the
9648
         front.  */
9649
      /* TODO: Handle kind parameters once they are implemented.  */
9650
      my_rank = (arg->as ? arg->as->rank : 0);
9651
      for (i = list->next; i; i = i->next)
9652
        {
9653
          /* Argument list might be empty; that is an error signalled earlier,
9654
             but we nevertheless continued resolving.  */
9655
          if (i->proc_sym->formal)
9656
            {
9657
              gfc_symbol* i_arg = i->proc_sym->formal->sym;
9658
              const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9659
              if (i_rank == my_rank)
9660
                {
9661
                  gfc_error ("FINAL procedure '%s' declared at %L has the same"
9662
                             " rank (%d) as '%s'",
9663
                             list->proc_sym->name, &list->where, my_rank,
9664
                             i->proc_sym->name);
9665
                  goto error;
9666
                }
9667
            }
9668
        }
9669
 
9670
        /* Is this the/a scalar finalizer procedure?  */
9671
        if (!arg->as || arg->as->rank == 0)
9672
          seen_scalar = true;
9673
 
9674
        /* Find the symtree for this procedure.  */
9675
        gcc_assert (!list->proc_tree);
9676
        list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9677
 
9678
        prev_link = &list->next;
9679
        continue;
9680
 
9681
        /* Remove wrong nodes immediately from the list so we don't risk any
9682
           troubles in the future when they might fail later expectations.  */
9683
error:
9684
        result = FAILURE;
9685
        i = list;
9686
        *prev_link = list->next;
9687
        gfc_free_finalizer (i);
9688
    }
9689
 
9690
  /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9691
     were nodes in the list, must have been for arrays.  It is surely a good
9692
     idea to have a scalar version there if there's something to finalize.  */
9693
  if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9694
    gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9695
                 " defined at %L, suggest also scalar one",
9696
                 derived->name, &derived->declared_at);
9697
 
9698
  /* TODO:  Remove this error when finalization is finished.  */
9699
  gfc_error ("Finalization at %L is not yet implemented",
9700
             &derived->declared_at);
9701
 
9702
  return result;
9703
}
9704
 
9705
 
9706
/* Check that it is ok for the typebound procedure proc to override the
9707
   procedure old.  */
9708
 
9709
static gfc_try
9710
check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9711
{
9712
  locus where;
9713
  const gfc_symbol* proc_target;
9714
  const gfc_symbol* old_target;
9715
  unsigned proc_pass_arg, old_pass_arg, argpos;
9716
  gfc_formal_arglist* proc_formal;
9717
  gfc_formal_arglist* old_formal;
9718
 
9719
  /* This procedure should only be called for non-GENERIC proc.  */
9720
  gcc_assert (!proc->n.tb->is_generic);
9721
 
9722
  /* If the overwritten procedure is GENERIC, this is an error.  */
9723
  if (old->n.tb->is_generic)
9724
    {
9725
      gfc_error ("Can't overwrite GENERIC '%s' at %L",
9726
                 old->name, &proc->n.tb->where);
9727
      return FAILURE;
9728
    }
9729
 
9730
  where = proc->n.tb->where;
9731
  proc_target = proc->n.tb->u.specific->n.sym;
9732
  old_target = old->n.tb->u.specific->n.sym;
9733
 
9734
  /* Check that overridden binding is not NON_OVERRIDABLE.  */
9735
  if (old->n.tb->non_overridable)
9736
    {
9737
      gfc_error ("'%s' at %L overrides a procedure binding declared"
9738
                 " NON_OVERRIDABLE", proc->name, &where);
9739
      return FAILURE;
9740
    }
9741
 
9742
  /* It's an error to override a non-DEFERRED procedure with a DEFERRED one.  */
9743
  if (!old->n.tb->deferred && proc->n.tb->deferred)
9744
    {
9745
      gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9746
                 " non-DEFERRED binding", proc->name, &where);
9747
      return FAILURE;
9748
    }
9749
 
9750
  /* If the overridden binding is PURE, the overriding must be, too.  */
9751
  if (old_target->attr.pure && !proc_target->attr.pure)
9752
    {
9753
      gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9754
                 proc->name, &where);
9755
      return FAILURE;
9756
    }
9757
 
9758
  /* If the overridden binding is ELEMENTAL, the overriding must be, too.  If it
9759
     is not, the overriding must not be either.  */
9760
  if (old_target->attr.elemental && !proc_target->attr.elemental)
9761
    {
9762
      gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9763
                 " ELEMENTAL", proc->name, &where);
9764
      return FAILURE;
9765
    }
9766
  if (!old_target->attr.elemental && proc_target->attr.elemental)
9767
    {
9768
      gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9769
                 " be ELEMENTAL, either", proc->name, &where);
9770
      return FAILURE;
9771
    }
9772
 
9773
  /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9774
     SUBROUTINE.  */
9775
  if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9776
    {
9777
      gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9778
                 " SUBROUTINE", proc->name, &where);
9779
      return FAILURE;
9780
    }
9781
 
9782
  /* If the overridden binding is a FUNCTION, the overriding must also be a
9783
     FUNCTION and have the same characteristics.  */
9784
  if (old_target->attr.function)
9785
    {
9786
      if (!proc_target->attr.function)
9787
        {
9788
          gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9789
                     " FUNCTION", proc->name, &where);
9790
          return FAILURE;
9791
        }
9792
 
9793
      /* FIXME:  Do more comprehensive checking (including, for instance, the
9794
         rank and array-shape).  */
9795
      gcc_assert (proc_target->result && old_target->result);
9796
      if (!gfc_compare_types (&proc_target->result->ts,
9797
                              &old_target->result->ts))
9798
        {
9799
          gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9800
                     " matching result types", proc->name, &where);
9801
          return FAILURE;
9802
        }
9803
    }
9804
 
9805
  /* If the overridden binding is PUBLIC, the overriding one must not be
9806
     PRIVATE.  */
9807
  if (old->n.tb->access == ACCESS_PUBLIC
9808
      && proc->n.tb->access == ACCESS_PRIVATE)
9809
    {
9810
      gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9811
                 " PRIVATE", proc->name, &where);
9812
      return FAILURE;
9813
    }
9814
 
9815
  /* Compare the formal argument lists of both procedures.  This is also abused
9816
     to find the position of the passed-object dummy arguments of both
9817
     bindings as at least the overridden one might not yet be resolved and we
9818
     need those positions in the check below.  */
9819
  proc_pass_arg = old_pass_arg = 0;
9820
  if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9821
    proc_pass_arg = 1;
9822
  if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9823
    old_pass_arg = 1;
9824
  argpos = 1;
9825
  for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9826
       proc_formal && old_formal;
9827
       proc_formal = proc_formal->next, old_formal = old_formal->next)
9828
    {
9829
      if (proc->n.tb->pass_arg
9830
          && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9831
        proc_pass_arg = argpos;
9832
      if (old->n.tb->pass_arg
9833
          && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9834
        old_pass_arg = argpos;
9835
 
9836
      /* Check that the names correspond.  */
9837
      if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9838
        {
9839
          gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9840
                     " to match the corresponding argument of the overridden"
9841
                     " procedure", proc_formal->sym->name, proc->name, &where,
9842
                     old_formal->sym->name);
9843
          return FAILURE;
9844
        }
9845
 
9846
      /* Check that the types correspond if neither is the passed-object
9847
         argument.  */
9848
      /* FIXME:  Do more comprehensive testing here.  */
9849
      if (proc_pass_arg != argpos && old_pass_arg != argpos
9850
          && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9851
        {
9852
          gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9853
                     "in respect to the overridden procedure",
9854
                     proc_formal->sym->name, proc->name, &where);
9855
          return FAILURE;
9856
        }
9857
 
9858
      ++argpos;
9859
    }
9860
  if (proc_formal || old_formal)
9861
    {
9862
      gfc_error ("'%s' at %L must have the same number of formal arguments as"
9863
                 " the overridden procedure", proc->name, &where);
9864
      return FAILURE;
9865
    }
9866
 
9867
  /* If the overridden binding is NOPASS, the overriding one must also be
9868
     NOPASS.  */
9869
  if (old->n.tb->nopass && !proc->n.tb->nopass)
9870
    {
9871
      gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9872
                 " NOPASS", proc->name, &where);
9873
      return FAILURE;
9874
    }
9875
 
9876
  /* If the overridden binding is PASS(x), the overriding one must also be
9877
     PASS and the passed-object dummy arguments must correspond.  */
9878
  if (!old->n.tb->nopass)
9879
    {
9880
      if (proc->n.tb->nopass)
9881
        {
9882
          gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9883
                     " PASS", proc->name, &where);
9884
          return FAILURE;
9885
        }
9886
 
9887
      if (proc_pass_arg != old_pass_arg)
9888
        {
9889
          gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9890
                     " the same position as the passed-object dummy argument of"
9891
                     " the overridden procedure", proc->name, &where);
9892
          return FAILURE;
9893
        }
9894
    }
9895
 
9896
  return SUCCESS;
9897
}
9898
 
9899
 
9900
/* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
9901
 
9902
static gfc_try
9903
check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9904
                             const char* generic_name, locus where)
9905
{
9906
  gfc_symbol* sym1;
9907
  gfc_symbol* sym2;
9908
 
9909
  gcc_assert (t1->specific && t2->specific);
9910
  gcc_assert (!t1->specific->is_generic);
9911
  gcc_assert (!t2->specific->is_generic);
9912
 
9913
  sym1 = t1->specific->u.specific->n.sym;
9914
  sym2 = t2->specific->u.specific->n.sym;
9915
 
9916
  if (sym1 == sym2)
9917
    return SUCCESS;
9918
 
9919
  /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
9920
  if (sym1->attr.subroutine != sym2->attr.subroutine
9921
      || sym1->attr.function != sym2->attr.function)
9922
    {
9923
      gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9924
                 " GENERIC '%s' at %L",
9925
                 sym1->name, sym2->name, generic_name, &where);
9926
      return FAILURE;
9927
    }
9928
 
9929
  /* Compare the interfaces.  */
9930
  if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
9931
    {
9932
      gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9933
                 sym1->name, sym2->name, generic_name, &where);
9934
      return FAILURE;
9935
    }
9936
 
9937
  return SUCCESS;
9938
}
9939
 
9940
 
9941
/* Worker function for resolving a generic procedure binding; this is used to
9942
   resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9943
 
9944
   The difference between those cases is finding possible inherited bindings
9945
   that are overridden, as one has to look for them in tb_sym_root,
9946
   tb_uop_root or tb_op, respectively.  Thus the caller must already find
9947
   the super-type and set p->overridden correctly.  */
9948
 
9949
static gfc_try
9950
resolve_tb_generic_targets (gfc_symbol* super_type,
9951
                            gfc_typebound_proc* p, const char* name)
9952
{
9953
  gfc_tbp_generic* target;
9954
  gfc_symtree* first_target;
9955
  gfc_symtree* inherited;
9956
 
9957
  gcc_assert (p && p->is_generic);
9958
 
9959
  /* Try to find the specific bindings for the symtrees in our target-list.  */
9960
  gcc_assert (p->u.generic);
9961
  for (target = p->u.generic; target; target = target->next)
9962
    if (!target->specific)
9963
      {
9964
        gfc_typebound_proc* overridden_tbp;
9965
        gfc_tbp_generic* g;
9966
        const char* target_name;
9967
 
9968
        target_name = target->specific_st->name;
9969
 
9970
        /* Defined for this type directly.  */
9971
        if (target->specific_st->n.tb)
9972
          {
9973
            target->specific = target->specific_st->n.tb;
9974
            goto specific_found;
9975
          }
9976
 
9977
        /* Look for an inherited specific binding.  */
9978
        if (super_type)
9979
          {
9980
            inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9981
                                                 true, NULL);
9982
 
9983
            if (inherited)
9984
              {
9985
                gcc_assert (inherited->n.tb);
9986
                target->specific = inherited->n.tb;
9987
                goto specific_found;
9988
              }
9989
          }
9990
 
9991
        gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9992
                   " at %L", target_name, name, &p->where);
9993
        return FAILURE;
9994
 
9995
        /* Once we've found the specific binding, check it is not ambiguous with
9996
           other specifics already found or inherited for the same GENERIC.  */
9997
specific_found:
9998
        gcc_assert (target->specific);
9999
 
10000
        /* This must really be a specific binding!  */
10001
        if (target->specific->is_generic)
10002
          {
10003
            gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10004
                       " '%s' is GENERIC, too", name, &p->where, target_name);
10005
            return FAILURE;
10006
          }
10007
 
10008
        /* Check those already resolved on this type directly.  */
10009
        for (g = p->u.generic; g; g = g->next)
10010
          if (g != target && g->specific
10011
              && check_generic_tbp_ambiguity (target, g, name, p->where)
10012
                  == FAILURE)
10013
            return FAILURE;
10014
 
10015
        /* Check for ambiguity with inherited specific targets.  */
10016
        for (overridden_tbp = p->overridden; overridden_tbp;
10017
             overridden_tbp = overridden_tbp->overridden)
10018
          if (overridden_tbp->is_generic)
10019
            {
10020
              for (g = overridden_tbp->u.generic; g; g = g->next)
10021
                {
10022
                  gcc_assert (g->specific);
10023
                  if (check_generic_tbp_ambiguity (target, g,
10024
                                                   name, p->where) == FAILURE)
10025
                    return FAILURE;
10026
                }
10027
            }
10028
      }
10029
 
10030
  /* If we attempt to "overwrite" a specific binding, this is an error.  */
10031
  if (p->overridden && !p->overridden->is_generic)
10032
    {
10033
      gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10034
                 " the same name", name, &p->where);
10035
      return FAILURE;
10036
    }
10037
 
10038
  /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10039
     all must have the same attributes here.  */
10040
  first_target = p->u.generic->specific->u.specific;
10041
  gcc_assert (first_target);
10042
  p->subroutine = first_target->n.sym->attr.subroutine;
10043
  p->function = first_target->n.sym->attr.function;
10044
 
10045
  return SUCCESS;
10046
}
10047
 
10048
 
10049
/* Resolve a GENERIC procedure binding for a derived type.  */
10050
 
10051
static gfc_try
10052
resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10053
{
10054
  gfc_symbol* super_type;
10055
 
10056
  /* Find the overridden binding if any.  */
10057
  st->n.tb->overridden = NULL;
10058
  super_type = gfc_get_derived_super_type (derived);
10059
  if (super_type)
10060
    {
10061
      gfc_symtree* overridden;
10062
      overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10063
                                            true, NULL);
10064
 
10065
      if (overridden && overridden->n.tb)
10066
        st->n.tb->overridden = overridden->n.tb;
10067
    }
10068
 
10069
  /* Resolve using worker function.  */
10070
  return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10071
}
10072
 
10073
 
10074
/* Retrieve the target-procedure of an operator binding and do some checks in
10075
   common for intrinsic and user-defined type-bound operators.  */
10076
 
10077
static gfc_symbol*
10078
get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10079
{
10080
  gfc_symbol* target_proc;
10081
 
10082
  gcc_assert (target->specific && !target->specific->is_generic);
10083
  target_proc = target->specific->u.specific->n.sym;
10084
  gcc_assert (target_proc);
10085
 
10086
  /* All operator bindings must have a passed-object dummy argument.  */
10087
  if (target->specific->nopass)
10088
    {
10089
      gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10090
      return NULL;
10091
    }
10092
 
10093
  return target_proc;
10094
}
10095
 
10096
 
10097
/* Resolve a type-bound intrinsic operator.  */
10098
 
10099
static gfc_try
10100
resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10101
                                gfc_typebound_proc* p)
10102
{
10103
  gfc_symbol* super_type;
10104
  gfc_tbp_generic* target;
10105
 
10106
  /* If there's already an error here, do nothing (but don't fail again).  */
10107
  if (p->error)
10108
    return SUCCESS;
10109
 
10110
  /* Operators should always be GENERIC bindings.  */
10111
  gcc_assert (p->is_generic);
10112
 
10113
  /* Look for an overridden binding.  */
10114
  super_type = gfc_get_derived_super_type (derived);
10115
  if (super_type && super_type->f2k_derived)
10116
    p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10117
                                                     op, true, NULL);
10118
  else
10119
    p->overridden = NULL;
10120
 
10121
  /* Resolve general GENERIC properties using worker function.  */
10122
  if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10123
    goto error;
10124
 
10125
  /* Check the targets to be procedures of correct interface.  */
10126
  for (target = p->u.generic; target; target = target->next)
10127
    {
10128
      gfc_symbol* target_proc;
10129
 
10130
      target_proc = get_checked_tb_operator_target (target, p->where);
10131
      if (!target_proc)
10132
        goto error;
10133
 
10134
      if (!gfc_check_operator_interface (target_proc, op, p->where))
10135
        goto error;
10136
    }
10137
 
10138
  return SUCCESS;
10139
 
10140
error:
10141
  p->error = 1;
10142
  return FAILURE;
10143
}
10144
 
10145
 
10146
/* Resolve a type-bound user operator (tree-walker callback).  */
10147
 
10148
static gfc_symbol* resolve_bindings_derived;
10149
static gfc_try resolve_bindings_result;
10150
 
10151
static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10152
 
10153
static void
10154
resolve_typebound_user_op (gfc_symtree* stree)
10155
{
10156
  gfc_symbol* super_type;
10157
  gfc_tbp_generic* target;
10158
 
10159
  gcc_assert (stree && stree->n.tb);
10160
 
10161
  if (stree->n.tb->error)
10162
    return;
10163
 
10164
  /* Operators should always be GENERIC bindings.  */
10165
  gcc_assert (stree->n.tb->is_generic);
10166
 
10167
  /* Find overridden procedure, if any.  */
10168
  super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10169
  if (super_type && super_type->f2k_derived)
10170
    {
10171
      gfc_symtree* overridden;
10172
      overridden = gfc_find_typebound_user_op (super_type, NULL,
10173
                                               stree->name, true, NULL);
10174
 
10175
      if (overridden && overridden->n.tb)
10176
        stree->n.tb->overridden = overridden->n.tb;
10177
    }
10178
  else
10179
    stree->n.tb->overridden = NULL;
10180
 
10181
  /* Resolve basically using worker function.  */
10182
  if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10183
        == FAILURE)
10184
    goto error;
10185
 
10186
  /* Check the targets to be functions of correct interface.  */
10187
  for (target = stree->n.tb->u.generic; target; target = target->next)
10188
    {
10189
      gfc_symbol* target_proc;
10190
 
10191
      target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10192
      if (!target_proc)
10193
        goto error;
10194
 
10195
      if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10196
        goto error;
10197
    }
10198
 
10199
  return;
10200
 
10201
error:
10202
  resolve_bindings_result = FAILURE;
10203
  stree->n.tb->error = 1;
10204
}
10205
 
10206
 
10207
/* Resolve the type-bound procedures for a derived type.  */
10208
 
10209
static void
10210
resolve_typebound_procedure (gfc_symtree* stree)
10211
{
10212
  gfc_symbol* proc;
10213
  locus where;
10214
  gfc_symbol* me_arg;
10215
  gfc_symbol* super_type;
10216
  gfc_component* comp;
10217
 
10218
  gcc_assert (stree);
10219
 
10220
  /* Undefined specific symbol from GENERIC target definition.  */
10221
  if (!stree->n.tb)
10222
    return;
10223
 
10224
  if (stree->n.tb->error)
10225
    return;
10226
 
10227
  /* If this is a GENERIC binding, use that routine.  */
10228
  if (stree->n.tb->is_generic)
10229
    {
10230
      if (resolve_typebound_generic (resolve_bindings_derived, stree)
10231
            == FAILURE)
10232
        goto error;
10233
      return;
10234
    }
10235
 
10236
  /* Get the target-procedure to check it.  */
10237
  gcc_assert (!stree->n.tb->is_generic);
10238
  gcc_assert (stree->n.tb->u.specific);
10239
  proc = stree->n.tb->u.specific->n.sym;
10240
  where = stree->n.tb->where;
10241
 
10242
  /* Default access should already be resolved from the parser.  */
10243
  gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10244
 
10245
  /* It should be a module procedure or an external procedure with explicit
10246
     interface.  For DEFERRED bindings, abstract interfaces are ok as well.  */
10247
  if ((!proc->attr.subroutine && !proc->attr.function)
10248
      || (proc->attr.proc != PROC_MODULE
10249
          && proc->attr.if_source != IFSRC_IFBODY)
10250
      || (proc->attr.abstract && !stree->n.tb->deferred))
10251
    {
10252
      gfc_error ("'%s' must be a module procedure or an external procedure with"
10253
                 " an explicit interface at %L", proc->name, &where);
10254
      goto error;
10255
    }
10256
  stree->n.tb->subroutine = proc->attr.subroutine;
10257
  stree->n.tb->function = proc->attr.function;
10258
 
10259
  /* Find the super-type of the current derived type.  We could do this once and
10260
     store in a global if speed is needed, but as long as not I believe this is
10261
     more readable and clearer.  */
10262
  super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10263
 
10264
  /* If PASS, resolve and check arguments if not already resolved / loaded
10265
     from a .mod file.  */
10266
  if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10267
    {
10268
      if (stree->n.tb->pass_arg)
10269
        {
10270
          gfc_formal_arglist* i;
10271
 
10272
          /* If an explicit passing argument name is given, walk the arg-list
10273
             and look for it.  */
10274
 
10275
          me_arg = NULL;
10276
          stree->n.tb->pass_arg_num = 1;
10277
          for (i = proc->formal; i; i = i->next)
10278
            {
10279
              if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10280
                {
10281
                  me_arg = i->sym;
10282
                  break;
10283
                }
10284
              ++stree->n.tb->pass_arg_num;
10285
            }
10286
 
10287
          if (!me_arg)
10288
            {
10289
              gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10290
                         " argument '%s'",
10291
                         proc->name, stree->n.tb->pass_arg, &where,
10292
                         stree->n.tb->pass_arg);
10293
              goto error;
10294
            }
10295
        }
10296
      else
10297
        {
10298
          /* Otherwise, take the first one; there should in fact be at least
10299
             one.  */
10300
          stree->n.tb->pass_arg_num = 1;
10301
          if (!proc->formal)
10302
            {
10303
              gfc_error ("Procedure '%s' with PASS at %L must have at"
10304
                         " least one argument", proc->name, &where);
10305
              goto error;
10306
            }
10307
          me_arg = proc->formal->sym;
10308
        }
10309
 
10310
      /* Now check that the argument-type matches and the passed-object
10311
         dummy argument is generally fine.  */
10312
 
10313
      gcc_assert (me_arg);
10314
 
10315
      if (me_arg->ts.type != BT_CLASS)
10316
        {
10317
          gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10318
                     " at %L", proc->name, &where);
10319
          goto error;
10320
        }
10321
 
10322
      if (me_arg->ts.u.derived->components->ts.u.derived
10323
          != resolve_bindings_derived)
10324
        {
10325
          gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10326
                     " the derived-type '%s'", me_arg->name, proc->name,
10327
                     me_arg->name, &where, resolve_bindings_derived->name);
10328
          goto error;
10329
        }
10330
 
10331
      gcc_assert (me_arg->ts.type == BT_CLASS);
10332
      if (me_arg->ts.u.derived->components->as
10333
          && me_arg->ts.u.derived->components->as->rank > 0)
10334
        {
10335
          gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10336
                     " scalar", proc->name, &where);
10337
          goto error;
10338
        }
10339
      if (me_arg->ts.u.derived->components->attr.allocatable)
10340
        {
10341
          gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10342
                     " be ALLOCATABLE", proc->name, &where);
10343
          goto error;
10344
        }
10345
      if (me_arg->ts.u.derived->components->attr.class_pointer)
10346
        {
10347
          gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10348
                     " be POINTER", proc->name, &where);
10349
          goto error;
10350
        }
10351
    }
10352
 
10353
  /* If we are extending some type, check that we don't override a procedure
10354
     flagged NON_OVERRIDABLE.  */
10355
  stree->n.tb->overridden = NULL;
10356
  if (super_type)
10357
    {
10358
      gfc_symtree* overridden;
10359
      overridden = gfc_find_typebound_proc (super_type, NULL,
10360
                                            stree->name, true, NULL);
10361
 
10362
      if (overridden && overridden->n.tb)
10363
        stree->n.tb->overridden = overridden->n.tb;
10364
 
10365
      if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10366
        goto error;
10367
    }
10368
 
10369
  /* See if there's a name collision with a component directly in this type.  */
10370
  for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10371
    if (!strcmp (comp->name, stree->name))
10372
      {
10373
        gfc_error ("Procedure '%s' at %L has the same name as a component of"
10374
                   " '%s'",
10375
                   stree->name, &where, resolve_bindings_derived->name);
10376
        goto error;
10377
      }
10378
 
10379
  /* Try to find a name collision with an inherited component.  */
10380
  if (super_type && gfc_find_component (super_type, stree->name, true, true))
10381
    {
10382
      gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10383
                 " component of '%s'",
10384
                 stree->name, &where, resolve_bindings_derived->name);
10385
      goto error;
10386
    }
10387
 
10388
  stree->n.tb->error = 0;
10389
  return;
10390
 
10391
error:
10392
  resolve_bindings_result = FAILURE;
10393
  stree->n.tb->error = 1;
10394
}
10395
 
10396
static gfc_try
10397
resolve_typebound_procedures (gfc_symbol* derived)
10398
{
10399
  int op;
10400
 
10401
  if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10402
    return SUCCESS;
10403
 
10404
  resolve_bindings_derived = derived;
10405
  resolve_bindings_result = SUCCESS;
10406
 
10407
  if (derived->f2k_derived->tb_sym_root)
10408
    gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10409
                          &resolve_typebound_procedure);
10410
 
10411
  if (derived->f2k_derived->tb_uop_root)
10412
    gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10413
                          &resolve_typebound_user_op);
10414
 
10415
  for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10416
    {
10417
      gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10418
      if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10419
                                               p) == FAILURE)
10420
        resolve_bindings_result = FAILURE;
10421
    }
10422
 
10423
  return resolve_bindings_result;
10424
}
10425
 
10426
 
10427
/* Add a derived type to the dt_list.  The dt_list is used in trans-types.c
10428
   to give all identical derived types the same backend_decl.  */
10429
static void
10430
add_dt_to_dt_list (gfc_symbol *derived)
10431
{
10432
  gfc_dt_list *dt_list;
10433
 
10434
  for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10435
    if (derived == dt_list->derived)
10436
      break;
10437
 
10438
  if (dt_list == NULL)
10439
    {
10440
      dt_list = gfc_get_dt_list ();
10441
      dt_list->next = gfc_derived_types;
10442
      dt_list->derived = derived;
10443
      gfc_derived_types = dt_list;
10444
    }
10445
}
10446
 
10447
 
10448
/* Ensure that a derived-type is really not abstract, meaning that every
10449
   inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
10450
 
10451
static gfc_try
10452
ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10453
{
10454
  if (!st)
10455
    return SUCCESS;
10456
 
10457
  if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10458
    return FAILURE;
10459
  if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10460
    return FAILURE;
10461
 
10462
  if (st->n.tb && st->n.tb->deferred)
10463
    {
10464
      gfc_symtree* overriding;
10465
      overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10466
      if (!overriding)
10467
        return FAILURE;
10468
      gcc_assert (overriding->n.tb);
10469
      if (overriding->n.tb->deferred)
10470
        {
10471
          gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10472
                     " '%s' is DEFERRED and not overridden",
10473
                     sub->name, &sub->declared_at, st->name);
10474
          return FAILURE;
10475
        }
10476
    }
10477
 
10478
  return SUCCESS;
10479
}
10480
 
10481
static gfc_try
10482
ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10483
{
10484
  /* The algorithm used here is to recursively travel up the ancestry of sub
10485
     and for each ancestor-type, check all bindings.  If any of them is
10486
     DEFERRED, look it up starting from sub and see if the found (overriding)
10487
     binding is not DEFERRED.
10488
     This is not the most efficient way to do this, but it should be ok and is
10489
     clearer than something sophisticated.  */
10490
 
10491
  gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10492
 
10493
  /* Walk bindings of this ancestor.  */
10494
  if (ancestor->f2k_derived)
10495
    {
10496
      gfc_try t;
10497
      t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10498
      if (t == FAILURE)
10499
        return FAILURE;
10500
    }
10501
 
10502
  /* Find next ancestor type and recurse on it.  */
10503
  ancestor = gfc_get_derived_super_type (ancestor);
10504
  if (ancestor)
10505
    return ensure_not_abstract (sub, ancestor);
10506
 
10507
  return SUCCESS;
10508
}
10509
 
10510
 
10511
static void resolve_symbol (gfc_symbol *sym);
10512
 
10513
 
10514
/* Resolve the components of a derived type.  */
10515
 
10516
static gfc_try
10517
resolve_fl_derived (gfc_symbol *sym)
10518
{
10519
  gfc_symbol* super_type;
10520
  gfc_component *c;
10521
  int i;
10522
 
10523
  super_type = gfc_get_derived_super_type (sym);
10524
 
10525
  /* Ensure the extended type gets resolved before we do.  */
10526
  if (super_type && resolve_fl_derived (super_type) == FAILURE)
10527
    return FAILURE;
10528
 
10529
  /* An ABSTRACT type must be extensible.  */
10530
  if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10531
    {
10532
      gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10533
                 sym->name, &sym->declared_at);
10534
      return FAILURE;
10535
    }
10536
 
10537
  for (c = sym->components; c != NULL; c = c->next)
10538
    {
10539
      if (c->attr.proc_pointer && c->ts.interface)
10540
        {
10541
          if (c->ts.interface->attr.procedure)
10542
            gfc_error ("Interface '%s', used by procedure pointer component "
10543
                       "'%s' at %L, is declared in a later PROCEDURE statement",
10544
                       c->ts.interface->name, c->name, &c->loc);
10545
 
10546
          /* Get the attributes from the interface (now resolved).  */
10547
          if (c->ts.interface->attr.if_source
10548
              || c->ts.interface->attr.intrinsic)
10549
            {
10550
              gfc_symbol *ifc = c->ts.interface;
10551
 
10552
              if (ifc->formal && !ifc->formal_ns)
10553
                resolve_symbol (ifc);
10554
 
10555
              if (ifc->attr.intrinsic)
10556
                resolve_intrinsic (ifc, &ifc->declared_at);
10557
 
10558
              if (ifc->result)
10559
                {
10560
                  c->ts = ifc->result->ts;
10561
                  c->attr.allocatable = ifc->result->attr.allocatable;
10562
                  c->attr.pointer = ifc->result->attr.pointer;
10563
                  c->attr.dimension = ifc->result->attr.dimension;
10564
                  c->as = gfc_copy_array_spec (ifc->result->as);
10565
                }
10566
              else
10567
                {
10568
                  c->ts = ifc->ts;
10569
                  c->attr.allocatable = ifc->attr.allocatable;
10570
                  c->attr.pointer = ifc->attr.pointer;
10571
                  c->attr.dimension = ifc->attr.dimension;
10572
                  c->as = gfc_copy_array_spec (ifc->as);
10573
                }
10574
              c->ts.interface = ifc;
10575
              c->attr.function = ifc->attr.function;
10576
              c->attr.subroutine = ifc->attr.subroutine;
10577
              gfc_copy_formal_args_ppc (c, ifc);
10578
 
10579
              c->attr.pure = ifc->attr.pure;
10580
              c->attr.elemental = ifc->attr.elemental;
10581
              c->attr.recursive = ifc->attr.recursive;
10582
              c->attr.always_explicit = ifc->attr.always_explicit;
10583
              c->attr.ext_attr |= ifc->attr.ext_attr;
10584
              /* Replace symbols in array spec.  */
10585
              if (c->as)
10586
                {
10587
                  int i;
10588
                  for (i = 0; i < c->as->rank; i++)
10589
                    {
10590
                      gfc_expr_replace_comp (c->as->lower[i], c);
10591
                      gfc_expr_replace_comp (c->as->upper[i], c);
10592
                    }
10593
                }
10594
              /* Copy char length.  */
10595
              if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10596
                {
10597
                  gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10598
                  gfc_expr_replace_comp (cl->length, c);
10599
                  if (cl->length && !cl->resolved
10600
                        && gfc_resolve_expr (cl->length) == FAILURE)
10601
                    return FAILURE;
10602
                  c->ts.u.cl = cl;
10603
                }
10604
            }
10605
          else if (c->ts.interface->name[0] != '\0')
10606
            {
10607
              gfc_error ("Interface '%s' of procedure pointer component "
10608
                         "'%s' at %L must be explicit", c->ts.interface->name,
10609
                         c->name, &c->loc);
10610
              return FAILURE;
10611
            }
10612
        }
10613
      else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10614
        {
10615
          /* Since PPCs are not implicitly typed, a PPC without an explicit
10616
             interface must be a subroutine.  */
10617
          gfc_add_subroutine (&c->attr, c->name, &c->loc);
10618
        }
10619
 
10620
      /* Procedure pointer components: Check PASS arg.  */
10621
      if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10622
        {
10623
          gfc_symbol* me_arg;
10624
 
10625
          if (c->tb->pass_arg)
10626
            {
10627
              gfc_formal_arglist* i;
10628
 
10629
              /* If an explicit passing argument name is given, walk the arg-list
10630
                and look for it.  */
10631
 
10632
              me_arg = NULL;
10633
              c->tb->pass_arg_num = 1;
10634
              for (i = c->formal; i; i = i->next)
10635
                {
10636
                  if (!strcmp (i->sym->name, c->tb->pass_arg))
10637
                    {
10638
                      me_arg = i->sym;
10639
                      break;
10640
                    }
10641
                  c->tb->pass_arg_num++;
10642
                }
10643
 
10644
              if (!me_arg)
10645
                {
10646
                  gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10647
                             "at %L has no argument '%s'", c->name,
10648
                             c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10649
                  c->tb->error = 1;
10650
                  return FAILURE;
10651
                }
10652
            }
10653
          else
10654
            {
10655
              /* Otherwise, take the first one; there should in fact be at least
10656
                one.  */
10657
              c->tb->pass_arg_num = 1;
10658
              if (!c->formal)
10659
                {
10660
                  gfc_error ("Procedure pointer component '%s' with PASS at %L "
10661
                             "must have at least one argument",
10662
                             c->name, &c->loc);
10663
                  c->tb->error = 1;
10664
                  return FAILURE;
10665
                }
10666
              me_arg = c->formal->sym;
10667
            }
10668
 
10669
          /* Now check that the argument-type matches.  */
10670
          gcc_assert (me_arg);
10671
          if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10672
              || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10673
              || (me_arg->ts.type == BT_CLASS
10674
                  && me_arg->ts.u.derived->components->ts.u.derived != sym))
10675
            {
10676
              gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10677
                         " the derived type '%s'", me_arg->name, c->name,
10678
                         me_arg->name, &c->loc, sym->name);
10679
              c->tb->error = 1;
10680
              return FAILURE;
10681
            }
10682
 
10683
          /* Check for C453.  */
10684
          if (me_arg->attr.dimension)
10685
            {
10686
              gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10687
                         "must be scalar", me_arg->name, c->name, me_arg->name,
10688
                         &c->loc);
10689
              c->tb->error = 1;
10690
              return FAILURE;
10691
            }
10692
 
10693
          if (me_arg->attr.pointer)
10694
            {
10695
              gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10696
                         "may not have the POINTER attribute", me_arg->name,
10697
                         c->name, me_arg->name, &c->loc);
10698
              c->tb->error = 1;
10699
              return FAILURE;
10700
            }
10701
 
10702
          if (me_arg->attr.allocatable)
10703
            {
10704
              gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10705
                         "may not be ALLOCATABLE", me_arg->name, c->name,
10706
                         me_arg->name, &c->loc);
10707
              c->tb->error = 1;
10708
              return FAILURE;
10709
            }
10710
 
10711
          if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10712
            gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10713
                       " at %L", c->name, &c->loc);
10714
 
10715
        }
10716
 
10717
      /* Check type-spec if this is not the parent-type component.  */
10718
      if ((!sym->attr.extension || c != sym->components)
10719
          && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10720
        return FAILURE;
10721
 
10722
      /* If this type is an extension, set the accessibility of the parent
10723
         component.  */
10724
      if (super_type && c == sym->components
10725
          && strcmp (super_type->name, c->name) == 0)
10726
        c->attr.access = super_type->attr.access;
10727
 
10728
      /* If this type is an extension, see if this component has the same name
10729
         as an inherited type-bound procedure.  */
10730
      if (super_type
10731
          && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10732
        {
10733
          gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10734
                     " inherited type-bound procedure",
10735
                     c->name, sym->name, &c->loc);
10736
          return FAILURE;
10737
        }
10738
 
10739
      if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10740
        {
10741
         if (c->ts.u.cl->length == NULL
10742
             || (resolve_charlen (c->ts.u.cl) == FAILURE)
10743
             || !gfc_is_constant_expr (c->ts.u.cl->length))
10744
           {
10745
             gfc_error ("Character length of component '%s' needs to "
10746
                        "be a constant specification expression at %L",
10747
                        c->name,
10748
                        c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10749
             return FAILURE;
10750
           }
10751
        }
10752
 
10753
      if (c->ts.type == BT_DERIVED
10754
          && sym->component_access != ACCESS_PRIVATE
10755
          && gfc_check_access (sym->attr.access, sym->ns->default_access)
10756
          && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10757
          && !c->ts.u.derived->attr.use_assoc
10758
          && !gfc_check_access (c->ts.u.derived->attr.access,
10759
                                c->ts.u.derived->ns->default_access)
10760
          && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10761
                             "is a PRIVATE type and cannot be a component of "
10762
                             "'%s', which is PUBLIC at %L", c->name,
10763
                             sym->name, &sym->declared_at) == FAILURE)
10764
        return FAILURE;
10765
 
10766
      if (sym->attr.sequence)
10767
        {
10768
          if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10769
            {
10770
              gfc_error ("Component %s of SEQUENCE type declared at %L does "
10771
                         "not have the SEQUENCE attribute",
10772
                         c->ts.u.derived->name, &sym->declared_at);
10773
              return FAILURE;
10774
            }
10775
        }
10776
 
10777
      if (c->ts.type == BT_DERIVED && c->attr.pointer
10778
          && c->ts.u.derived->components == NULL
10779
          && !c->ts.u.derived->attr.zero_comp)
10780
        {
10781
          gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10782
                     "that has not been declared", c->name, sym->name,
10783
                     &c->loc);
10784
          return FAILURE;
10785
        }
10786
 
10787
      /* C437.  */
10788
      if (c->ts.type == BT_CLASS
10789
          && !(c->ts.u.derived->components->attr.pointer
10790
               || c->ts.u.derived->components->attr.allocatable))
10791
        {
10792
          gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10793
                     "or pointer", c->name, &c->loc);
10794
          return FAILURE;
10795
        }
10796
 
10797
      /* Ensure that all the derived type components are put on the
10798
         derived type list; even in formal namespaces, where derived type
10799
         pointer components might not have been declared.  */
10800
      if (c->ts.type == BT_DERIVED
10801
            && c->ts.u.derived
10802
            && c->ts.u.derived->components
10803
            && c->attr.pointer
10804
            && sym != c->ts.u.derived)
10805
        add_dt_to_dt_list (c->ts.u.derived);
10806
 
10807
      if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10808
          || c->as == NULL)
10809
        continue;
10810
 
10811
      for (i = 0; i < c->as->rank; i++)
10812
        {
10813
          if (c->as->lower[i] == NULL
10814
              || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10815
              || !gfc_is_constant_expr (c->as->lower[i])
10816
              || c->as->upper[i] == NULL
10817
              || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10818
              || !gfc_is_constant_expr (c->as->upper[i]))
10819
            {
10820
              gfc_error ("Component '%s' of '%s' at %L must have "
10821
                         "constant array bounds",
10822
                         c->name, sym->name, &c->loc);
10823
              return FAILURE;
10824
            }
10825
        }
10826
    }
10827
 
10828
  /* Resolve the type-bound procedures.  */
10829
  if (resolve_typebound_procedures (sym) == FAILURE)
10830
    return FAILURE;
10831
 
10832
  /* Resolve the finalizer procedures.  */
10833
  if (gfc_resolve_finalizers (sym) == FAILURE)
10834
    return FAILURE;
10835
 
10836
  /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10837
     all DEFERRED bindings are overridden.  */
10838
  if (super_type && super_type->attr.abstract && !sym->attr.abstract
10839
      && ensure_not_abstract (sym, super_type) == FAILURE)
10840
    return FAILURE;
10841
 
10842
  /* Add derived type to the derived type list.  */
10843
  add_dt_to_dt_list (sym);
10844
 
10845
  return SUCCESS;
10846
}
10847
 
10848
 
10849
static gfc_try
10850
resolve_fl_namelist (gfc_symbol *sym)
10851
{
10852
  gfc_namelist *nl;
10853
  gfc_symbol *nlsym;
10854
 
10855
  /* Reject PRIVATE objects in a PUBLIC namelist.  */
10856
  if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10857
    {
10858
      for (nl = sym->namelist; nl; nl = nl->next)
10859
        {
10860
          if (!nl->sym->attr.use_assoc
10861
              && !is_sym_host_assoc (nl->sym, sym->ns)
10862
              && !gfc_check_access(nl->sym->attr.access,
10863
                                nl->sym->ns->default_access))
10864
            {
10865
              gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10866
                         "cannot be member of PUBLIC namelist '%s' at %L",
10867
                         nl->sym->name, sym->name, &sym->declared_at);
10868
              return FAILURE;
10869
            }
10870
 
10871
          /* Types with private components that came here by USE-association.  */
10872
          if (nl->sym->ts.type == BT_DERIVED
10873
              && derived_inaccessible (nl->sym->ts.u.derived))
10874
            {
10875
              gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10876
                         "components and cannot be member of namelist '%s' at %L",
10877
                         nl->sym->name, sym->name, &sym->declared_at);
10878
              return FAILURE;
10879
            }
10880
 
10881
          /* Types with private components that are defined in the same module.  */
10882
          if (nl->sym->ts.type == BT_DERIVED
10883
              && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10884
              && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
10885
                                        ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10886
                                        nl->sym->ns->default_access))
10887
            {
10888
              gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10889
                         "cannot be a member of PUBLIC namelist '%s' at %L",
10890
                         nl->sym->name, sym->name, &sym->declared_at);
10891
              return FAILURE;
10892
            }
10893
        }
10894
    }
10895
 
10896
  for (nl = sym->namelist; nl; nl = nl->next)
10897
    {
10898
      /* Reject namelist arrays of assumed shape.  */
10899
      if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10900
          && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10901
                             "must not have assumed shape in namelist "
10902
                             "'%s' at %L", nl->sym->name, sym->name,
10903
                             &sym->declared_at) == FAILURE)
10904
            return FAILURE;
10905
 
10906
      /* Reject namelist arrays that are not constant shape.  */
10907
      if (is_non_constant_shape_array (nl->sym))
10908
        {
10909
          gfc_error ("NAMELIST array object '%s' must have constant "
10910
                     "shape in namelist '%s' at %L", nl->sym->name,
10911
                     sym->name, &sym->declared_at);
10912
          return FAILURE;
10913
        }
10914
 
10915
      /* Namelist objects cannot have allocatable or pointer components.  */
10916
      if (nl->sym->ts.type != BT_DERIVED)
10917
        continue;
10918
 
10919
      if (nl->sym->ts.u.derived->attr.alloc_comp)
10920
        {
10921
          gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10922
                     "have ALLOCATABLE components",
10923
                     nl->sym->name, sym->name, &sym->declared_at);
10924
          return FAILURE;
10925
        }
10926
 
10927
      if (nl->sym->ts.u.derived->attr.pointer_comp)
10928
        {
10929
          gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10930
                     "have POINTER components",
10931
                     nl->sym->name, sym->name, &sym->declared_at);
10932
          return FAILURE;
10933
        }
10934
    }
10935
 
10936
 
10937
  /* 14.1.2 A module or internal procedure represent local entities
10938
     of the same type as a namelist member and so are not allowed.  */
10939
  for (nl = sym->namelist; nl; nl = nl->next)
10940
    {
10941
      if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10942
        continue;
10943
 
10944
      if (nl->sym->attr.function && nl->sym == nl->sym->result)
10945
        if ((nl->sym == sym->ns->proc_name)
10946
               ||
10947
            (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10948
          continue;
10949
 
10950
      nlsym = NULL;
10951
      if (nl->sym && nl->sym->name)
10952
        gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
10953
      if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10954
        {
10955
          gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10956
                     "attribute in '%s' at %L", nlsym->name,
10957
                     &sym->declared_at);
10958
          return FAILURE;
10959
        }
10960
    }
10961
 
10962
  return SUCCESS;
10963
}
10964
 
10965
 
10966
static gfc_try
10967
resolve_fl_parameter (gfc_symbol *sym)
10968
{
10969
  /* A parameter array's shape needs to be constant.  */
10970
  if (sym->as != NULL
10971
      && (sym->as->type == AS_DEFERRED
10972
          || is_non_constant_shape_array (sym)))
10973
    {
10974
      gfc_error ("Parameter array '%s' at %L cannot be automatic "
10975
                 "or of deferred shape", sym->name, &sym->declared_at);
10976
      return FAILURE;
10977
    }
10978
 
10979
  /* Make sure a parameter that has been implicitly typed still
10980
     matches the implicit type, since PARAMETER statements can precede
10981
     IMPLICIT statements.  */
10982
  if (sym->attr.implicit_type
10983
      && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10984
                                                             sym->ns)))
10985
    {
10986
      gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10987
                 "later IMPLICIT type", sym->name, &sym->declared_at);
10988
      return FAILURE;
10989
    }
10990
 
10991
  /* Make sure the types of derived parameters are consistent.  This
10992
     type checking is deferred until resolution because the type may
10993
     refer to a derived type from the host.  */
10994
  if (sym->ts.type == BT_DERIVED
10995
      && !gfc_compare_types (&sym->ts, &sym->value->ts))
10996
    {
10997
      gfc_error ("Incompatible derived type in PARAMETER at %L",
10998
                 &sym->value->where);
10999
      return FAILURE;
11000
    }
11001
  return SUCCESS;
11002
}
11003
 
11004
 
11005
/* Do anything necessary to resolve a symbol.  Right now, we just
11006
   assume that an otherwise unknown symbol is a variable.  This sort
11007
   of thing commonly happens for symbols in module.  */
11008
 
11009
static void
11010
resolve_symbol (gfc_symbol *sym)
11011
{
11012
  int check_constant, mp_flag;
11013
  gfc_symtree *symtree;
11014
  gfc_symtree *this_symtree;
11015
  gfc_namespace *ns;
11016
  gfc_component *c;
11017
 
11018
  if (sym->attr.flavor == FL_UNKNOWN)
11019
    {
11020
 
11021
    /* If we find that a flavorless symbol is an interface in one of the
11022
       parent namespaces, find its symtree in this namespace, free the
11023
       symbol and set the symtree to point to the interface symbol.  */
11024
      for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11025
        {
11026
          symtree = gfc_find_symtree (ns->sym_root, sym->name);
11027
          if (symtree && symtree->n.sym->generic)
11028
            {
11029
              this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11030
                                               sym->name);
11031
              sym->refs--;
11032
              if (!sym->refs)
11033
                gfc_free_symbol (sym);
11034
              symtree->n.sym->refs++;
11035
              this_symtree->n.sym = symtree->n.sym;
11036
              return;
11037
            }
11038
        }
11039
 
11040
      /* Otherwise give it a flavor according to such attributes as
11041
         it has.  */
11042
      if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11043
        sym->attr.flavor = FL_VARIABLE;
11044
      else
11045
        {
11046
          sym->attr.flavor = FL_PROCEDURE;
11047
          if (sym->attr.dimension)
11048
            sym->attr.function = 1;
11049
        }
11050
    }
11051
 
11052
  if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11053
    gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11054
 
11055
  if (sym->attr.procedure && sym->ts.interface
11056
      && sym->attr.if_source != IFSRC_DECL)
11057
    {
11058
      if (sym->ts.interface == sym)
11059
        {
11060
          gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
11061
                     "interface", sym->name, &sym->declared_at);
11062
          return;
11063
        }
11064
      if (sym->ts.interface->attr.procedure)
11065
        {
11066
          gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
11067
                     " in a later PROCEDURE statement", sym->ts.interface->name,
11068
                     sym->name,&sym->declared_at);
11069
          return;
11070
        }
11071
 
11072
      /* Get the attributes from the interface (now resolved).  */
11073
      if (sym->ts.interface->attr.if_source
11074
          || sym->ts.interface->attr.intrinsic)
11075
        {
11076
          gfc_symbol *ifc = sym->ts.interface;
11077
          resolve_symbol (ifc);
11078
 
11079
          if (ifc->attr.intrinsic)
11080
            resolve_intrinsic (ifc, &ifc->declared_at);
11081
 
11082
          if (ifc->result)
11083
            sym->ts = ifc->result->ts;
11084
          else
11085
            sym->ts = ifc->ts;
11086
          sym->ts.interface = ifc;
11087
          sym->attr.function = ifc->attr.function;
11088
          sym->attr.subroutine = ifc->attr.subroutine;
11089
          gfc_copy_formal_args (sym, ifc);
11090
 
11091
          sym->attr.allocatable = ifc->attr.allocatable;
11092
          sym->attr.pointer = ifc->attr.pointer;
11093
          sym->attr.pure = ifc->attr.pure;
11094
          sym->attr.elemental = ifc->attr.elemental;
11095
          sym->attr.dimension = ifc->attr.dimension;
11096
          sym->attr.recursive = ifc->attr.recursive;
11097
          sym->attr.always_explicit = ifc->attr.always_explicit;
11098
          sym->attr.ext_attr |= ifc->attr.ext_attr;
11099
          /* Copy array spec.  */
11100
          sym->as = gfc_copy_array_spec (ifc->as);
11101
          if (sym->as)
11102
            {
11103
              int i;
11104
              for (i = 0; i < sym->as->rank; i++)
11105
                {
11106
                  gfc_expr_replace_symbols (sym->as->lower[i], sym);
11107
                  gfc_expr_replace_symbols (sym->as->upper[i], sym);
11108
                }
11109
            }
11110
          /* Copy char length.  */
11111
          if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11112
            {
11113
              sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11114
              gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
11115
              if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
11116
                    && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
11117
                return;
11118
            }
11119
        }
11120
      else if (sym->ts.interface->name[0] != '\0')
11121
        {
11122
          gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11123
                    sym->ts.interface->name, sym->name, &sym->declared_at);
11124
          return;
11125
        }
11126
    }
11127
 
11128
  if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11129
    return;
11130
 
11131
  /* Symbols that are module procedures with results (functions) have
11132
     the types and array specification copied for type checking in
11133
     procedures that call them, as well as for saving to a module
11134
     file.  These symbols can't stand the scrutiny that their results
11135
     can.  */
11136
  mp_flag = (sym->result != NULL && sym->result != sym);
11137
 
11138
 
11139
  /* Make sure that the intrinsic is consistent with its internal
11140
     representation. This needs to be done before assigning a default
11141
     type to avoid spurious warnings.  */
11142
  if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11143
      && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11144
    return;
11145
 
11146
  /* Assign default type to symbols that need one and don't have one.  */
11147
  if (sym->ts.type == BT_UNKNOWN)
11148
    {
11149
      if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11150
        gfc_set_default_type (sym, 1, NULL);
11151
 
11152
      if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11153
          && !sym->attr.function && !sym->attr.subroutine
11154
          && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11155
        gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11156
 
11157
      if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11158
        {
11159
          /* The specific case of an external procedure should emit an error
11160
             in the case that there is no implicit type.  */
11161
          if (!mp_flag)
11162
            gfc_set_default_type (sym, sym->attr.external, NULL);
11163
          else
11164
            {
11165
              /* Result may be in another namespace.  */
11166
              resolve_symbol (sym->result);
11167
 
11168
              if (!sym->result->attr.proc_pointer)
11169
                {
11170
                  sym->ts = sym->result->ts;
11171
                  sym->as = gfc_copy_array_spec (sym->result->as);
11172
                  sym->attr.dimension = sym->result->attr.dimension;
11173
                  sym->attr.pointer = sym->result->attr.pointer;
11174
                  sym->attr.allocatable = sym->result->attr.allocatable;
11175
                }
11176
            }
11177
        }
11178
    }
11179
 
11180
  /* Assumed size arrays and assumed shape arrays must be dummy
11181
     arguments.  */
11182
 
11183
  if (sym->as != NULL
11184
      && ((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11185
          || sym->as->type == AS_ASSUMED_SHAPE)
11186
      && sym->attr.dummy == 0)
11187
    {
11188
      if (sym->as->type == AS_ASSUMED_SIZE)
11189
        gfc_error ("Assumed size array at %L must be a dummy argument",
11190
                   &sym->declared_at);
11191
      else
11192
        gfc_error ("Assumed shape array at %L must be a dummy argument",
11193
                   &sym->declared_at);
11194
      return;
11195
    }
11196
 
11197
  /* Make sure symbols with known intent or optional are really dummy
11198
     variable.  Because of ENTRY statement, this has to be deferred
11199
     until resolution time.  */
11200
 
11201
  if (!sym->attr.dummy
11202
      && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11203
    {
11204
      gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11205
      return;
11206
    }
11207
 
11208
  if (sym->attr.value && !sym->attr.dummy)
11209
    {
11210
      gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11211
                 "it is not a dummy argument", sym->name, &sym->declared_at);
11212
      return;
11213
    }
11214
 
11215
  if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11216
    {
11217
      gfc_charlen *cl = sym->ts.u.cl;
11218
      if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11219
        {
11220
          gfc_error ("Character dummy variable '%s' at %L with VALUE "
11221
                     "attribute must have constant length",
11222
                     sym->name, &sym->declared_at);
11223
          return;
11224
        }
11225
 
11226
      if (sym->ts.is_c_interop
11227
          && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11228
        {
11229
          gfc_error ("C interoperable character dummy variable '%s' at %L "
11230
                     "with VALUE attribute must have length one",
11231
                     sym->name, &sym->declared_at);
11232
          return;
11233
        }
11234
    }
11235
 
11236
  /* If the symbol is marked as bind(c), verify it's type and kind.  Do not
11237
     do this for something that was implicitly typed because that is handled
11238
     in gfc_set_default_type.  Handle dummy arguments and procedure
11239
     definitions separately.  Also, anything that is use associated is not
11240
     handled here but instead is handled in the module it is declared in.
11241
     Finally, derived type definitions are allowed to be BIND(C) since that
11242
     only implies that they're interoperable, and they are checked fully for
11243
     interoperability when a variable is declared of that type.  */
11244
  if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11245
      sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11246
      sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11247
    {
11248
      gfc_try t = SUCCESS;
11249
 
11250
      /* First, make sure the variable is declared at the
11251
         module-level scope (J3/04-007, Section 15.3).  */
11252
      if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11253
          sym->attr.in_common == 0)
11254
        {
11255
          gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11256
                     "is neither a COMMON block nor declared at the "
11257
                     "module level scope", sym->name, &(sym->declared_at));
11258
          t = FAILURE;
11259
        }
11260
      else if (sym->common_head != NULL)
11261
        {
11262
          t = verify_com_block_vars_c_interop (sym->common_head);
11263
        }
11264
      else
11265
        {
11266
          /* If type() declaration, we need to verify that the components
11267
             of the given type are all C interoperable, etc.  */
11268
          if (sym->ts.type == BT_DERIVED &&
11269
              sym->ts.u.derived->attr.is_c_interop != 1)
11270
            {
11271
              /* Make sure the user marked the derived type as BIND(C).  If
11272
                 not, call the verify routine.  This could print an error
11273
                 for the derived type more than once if multiple variables
11274
                 of that type are declared.  */
11275
              if (sym->ts.u.derived->attr.is_bind_c != 1)
11276
                verify_bind_c_derived_type (sym->ts.u.derived);
11277
              t = FAILURE;
11278
            }
11279
 
11280
          /* Verify the variable itself as C interoperable if it
11281
             is BIND(C).  It is not possible for this to succeed if
11282
             the verify_bind_c_derived_type failed, so don't have to handle
11283
             any error returned by verify_bind_c_derived_type.  */
11284
          t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11285
                                 sym->common_block);
11286
        }
11287
 
11288
      if (t == FAILURE)
11289
        {
11290
          /* clear the is_bind_c flag to prevent reporting errors more than
11291
             once if something failed.  */
11292
          sym->attr.is_bind_c = 0;
11293
          return;
11294
        }
11295
    }
11296
 
11297
  /* If a derived type symbol has reached this point, without its
11298
     type being declared, we have an error.  Notice that most
11299
     conditions that produce undefined derived types have already
11300
     been dealt with.  However, the likes of:
11301
     implicit type(t) (t) ..... call foo (t) will get us here if
11302
     the type is not declared in the scope of the implicit
11303
     statement. Change the type to BT_UNKNOWN, both because it is so
11304
     and to prevent an ICE.  */
11305
  if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11306
      && !sym->ts.u.derived->attr.zero_comp)
11307
    {
11308
      gfc_error ("The derived type '%s' at %L is of type '%s', "
11309
                 "which has not been defined", sym->name,
11310
                  &sym->declared_at, sym->ts.u.derived->name);
11311
      sym->ts.type = BT_UNKNOWN;
11312
      return;
11313
    }
11314
 
11315
  /* Make sure that the derived type has been resolved and that the
11316
     derived type is visible in the symbol's namespace, if it is a
11317
     module function and is not PRIVATE.  */
11318
  if (sym->ts.type == BT_DERIVED
11319
        && sym->ts.u.derived->attr.use_assoc
11320
        && sym->ns->proc_name
11321
        && sym->ns->proc_name->attr.flavor == FL_MODULE)
11322
    {
11323
      gfc_symbol *ds;
11324
 
11325
      if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11326
        return;
11327
 
11328
      gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11329
      if (!ds && sym->attr.function
11330
            && gfc_check_access (sym->attr.access, sym->ns->default_access))
11331
        {
11332
          symtree = gfc_new_symtree (&sym->ns->sym_root,
11333
                                     sym->ts.u.derived->name);
11334
          symtree->n.sym = sym->ts.u.derived;
11335
          sym->ts.u.derived->refs++;
11336
        }
11337
    }
11338
 
11339
  /* Unless the derived-type declaration is use associated, Fortran 95
11340
     does not allow public entries of private derived types.
11341
     See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11342
     161 in 95-006r3.  */
11343
  if (sym->ts.type == BT_DERIVED
11344
      && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11345
      && !sym->ts.u.derived->attr.use_assoc
11346
      && gfc_check_access (sym->attr.access, sym->ns->default_access)
11347
      && !gfc_check_access (sym->ts.u.derived->attr.access,
11348
                            sym->ts.u.derived->ns->default_access)
11349
      && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11350
                         "of PRIVATE derived type '%s'",
11351
                         (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11352
                         : "variable", sym->name, &sym->declared_at,
11353
                         sym->ts.u.derived->name) == FAILURE)
11354
    return;
11355
 
11356
  /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11357
     default initialization is defined (5.1.2.4.4).  */
11358
  if (sym->ts.type == BT_DERIVED
11359
      && sym->attr.dummy
11360
      && sym->attr.intent == INTENT_OUT
11361
      && sym->as
11362
      && sym->as->type == AS_ASSUMED_SIZE)
11363
    {
11364
      for (c = sym->ts.u.derived->components; c; c = c->next)
11365
        {
11366
          if (c->initializer)
11367
            {
11368
              gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11369
                         "ASSUMED SIZE and so cannot have a default initializer",
11370
                         sym->name, &sym->declared_at);
11371
              return;
11372
            }
11373
        }
11374
    }
11375
 
11376
  switch (sym->attr.flavor)
11377
    {
11378
    case FL_VARIABLE:
11379
      if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11380
        return;
11381
      break;
11382
 
11383
    case FL_PROCEDURE:
11384
      if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11385
        return;
11386
      break;
11387
 
11388
    case FL_NAMELIST:
11389
      if (resolve_fl_namelist (sym) == FAILURE)
11390
        return;
11391
      break;
11392
 
11393
    case FL_PARAMETER:
11394
      if (resolve_fl_parameter (sym) == FAILURE)
11395
        return;
11396
      break;
11397
 
11398
    default:
11399
      break;
11400
    }
11401
 
11402
  /* Resolve array specifier. Check as well some constraints
11403
     on COMMON blocks.  */
11404
 
11405
  check_constant = sym->attr.in_common && !sym->attr.pointer;
11406
 
11407
  /* Set the formal_arg_flag so that check_conflict will not throw
11408
     an error for host associated variables in the specification
11409
     expression for an array_valued function.  */
11410
  if (sym->attr.function && sym->as)
11411
    formal_arg_flag = 1;
11412
 
11413
  gfc_resolve_array_spec (sym->as, check_constant);
11414
 
11415
  formal_arg_flag = 0;
11416
 
11417
  /* Resolve formal namespaces.  */
11418
  if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11419
      && !sym->attr.contained && !sym->attr.intrinsic)
11420
    gfc_resolve (sym->formal_ns);
11421
 
11422
  /* Make sure the formal namespace is present.  */
11423
  if (sym->formal && !sym->formal_ns)
11424
    {
11425
      gfc_formal_arglist *formal = sym->formal;
11426
      while (formal && !formal->sym)
11427
        formal = formal->next;
11428
 
11429
      if (formal)
11430
        {
11431
          sym->formal_ns = formal->sym->ns;
11432
          sym->formal_ns->refs++;
11433
        }
11434
    }
11435
 
11436
  /* Check threadprivate restrictions.  */
11437
  if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11438
      && (!sym->attr.in_common
11439
          && sym->module == NULL
11440
          && (sym->ns->proc_name == NULL
11441
              || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11442
    gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11443
 
11444
  /* If we have come this far we can apply default-initializers, as
11445
     described in 14.7.5, to those variables that have not already
11446
     been assigned one.  */
11447
  if (sym->ts.type == BT_DERIVED
11448
      && sym->attr.referenced
11449
      && sym->ns == gfc_current_ns
11450
      && !sym->value
11451
      && !sym->attr.allocatable
11452
      && !sym->attr.alloc_comp)
11453
    {
11454
      symbol_attribute *a = &sym->attr;
11455
 
11456
      if ((!a->save && !a->dummy && !a->pointer
11457
           && !a->in_common && !a->use_assoc
11458
           && !(a->function && sym != sym->result))
11459
          || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11460
        apply_default_init (sym);
11461
    }
11462
 
11463
  /* If this symbol has a type-spec, check it.  */
11464
  if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11465
      || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11466
    if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11467
          == FAILURE)
11468
      return;
11469
}
11470
 
11471
 
11472
/************* Resolve DATA statements *************/
11473
 
11474
static struct
11475
{
11476
  gfc_data_value *vnode;
11477
  mpz_t left;
11478
}
11479
values;
11480
 
11481
 
11482
/* Advance the values structure to point to the next value in the data list.  */
11483
 
11484
static gfc_try
11485
next_data_value (void)
11486
{
11487
  while (mpz_cmp_ui (values.left, 0) == 0)
11488
    {
11489
 
11490
      if (values.vnode->next == NULL)
11491
        return FAILURE;
11492
 
11493
      values.vnode = values.vnode->next;
11494
      mpz_set (values.left, values.vnode->repeat);
11495
    }
11496
 
11497
  return SUCCESS;
11498
}
11499
 
11500
 
11501
static gfc_try
11502
check_data_variable (gfc_data_variable *var, locus *where)
11503
{
11504
  gfc_expr *e;
11505
  mpz_t size;
11506
  mpz_t offset;
11507
  gfc_try t;
11508
  ar_type mark = AR_UNKNOWN;
11509
  int i;
11510
  mpz_t section_index[GFC_MAX_DIMENSIONS];
11511
  gfc_ref *ref;
11512
  gfc_array_ref *ar;
11513
  gfc_symbol *sym;
11514
  int has_pointer;
11515
 
11516
  if (gfc_resolve_expr (var->expr) == FAILURE)
11517
    return FAILURE;
11518
 
11519
  ar = NULL;
11520
  mpz_init_set_si (offset, 0);
11521
  e = var->expr;
11522
 
11523
  if (e->expr_type != EXPR_VARIABLE)
11524
    gfc_internal_error ("check_data_variable(): Bad expression");
11525
 
11526
  sym = e->symtree->n.sym;
11527
 
11528
  if (sym->ns->is_block_data && !sym->attr.in_common)
11529
    {
11530
      gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11531
                 sym->name, &sym->declared_at);
11532
    }
11533
 
11534
  if (e->ref == NULL && sym->as)
11535
    {
11536
      gfc_error ("DATA array '%s' at %L must be specified in a previous"
11537
                 " declaration", sym->name, where);
11538
      return FAILURE;
11539
    }
11540
 
11541
  has_pointer = sym->attr.pointer;
11542
 
11543
  for (ref = e->ref; ref; ref = ref->next)
11544
    {
11545
      if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11546
        has_pointer = 1;
11547
 
11548
      if (has_pointer
11549
            && ref->type == REF_ARRAY
11550
            && ref->u.ar.type != AR_FULL)
11551
          {
11552
            gfc_error ("DATA element '%s' at %L is a pointer and so must "
11553
                        "be a full array", sym->name, where);
11554
            return FAILURE;
11555
          }
11556
    }
11557
 
11558
  if (e->rank == 0 || has_pointer)
11559
    {
11560
      mpz_init_set_ui (size, 1);
11561
      ref = NULL;
11562
    }
11563
  else
11564
    {
11565
      ref = e->ref;
11566
 
11567
      /* Find the array section reference.  */
11568
      for (ref = e->ref; ref; ref = ref->next)
11569
        {
11570
          if (ref->type != REF_ARRAY)
11571
            continue;
11572
          if (ref->u.ar.type == AR_ELEMENT)
11573
            continue;
11574
          break;
11575
        }
11576
      gcc_assert (ref);
11577
 
11578
      /* Set marks according to the reference pattern.  */
11579
      switch (ref->u.ar.type)
11580
        {
11581
        case AR_FULL:
11582
          mark = AR_FULL;
11583
          break;
11584
 
11585
        case AR_SECTION:
11586
          ar = &ref->u.ar;
11587
          /* Get the start position of array section.  */
11588
          gfc_get_section_index (ar, section_index, &offset);
11589
          mark = AR_SECTION;
11590
          break;
11591
 
11592
        default:
11593
          gcc_unreachable ();
11594
        }
11595
 
11596
      if (gfc_array_size (e, &size) == FAILURE)
11597
        {
11598
          gfc_error ("Nonconstant array section at %L in DATA statement",
11599
                     &e->where);
11600
          mpz_clear (offset);
11601
          return FAILURE;
11602
        }
11603
    }
11604
 
11605
  t = SUCCESS;
11606
 
11607
  while (mpz_cmp_ui (size, 0) > 0)
11608
    {
11609
      if (next_data_value () == FAILURE)
11610
        {
11611
          gfc_error ("DATA statement at %L has more variables than values",
11612
                     where);
11613
          t = FAILURE;
11614
          break;
11615
        }
11616
 
11617
      t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11618
      if (t == FAILURE)
11619
        break;
11620
 
11621
      /* If we have more than one element left in the repeat count,
11622
         and we have more than one element left in the target variable,
11623
         then create a range assignment.  */
11624
      /* FIXME: Only done for full arrays for now, since array sections
11625
         seem tricky.  */
11626
      if (mark == AR_FULL && ref && ref->next == NULL
11627
          && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11628
        {
11629
          mpz_t range;
11630
 
11631
          if (mpz_cmp (size, values.left) >= 0)
11632
            {
11633
              mpz_init_set (range, values.left);
11634
              mpz_sub (size, size, values.left);
11635
              mpz_set_ui (values.left, 0);
11636
            }
11637
          else
11638
            {
11639
              mpz_init_set (range, size);
11640
              mpz_sub (values.left, values.left, size);
11641
              mpz_set_ui (size, 0);
11642
            }
11643
 
11644
          gfc_assign_data_value_range (var->expr, values.vnode->expr,
11645
                                       offset, range);
11646
 
11647
          mpz_add (offset, offset, range);
11648
          mpz_clear (range);
11649
        }
11650
 
11651
      /* Assign initial value to symbol.  */
11652
      else
11653
        {
11654
          mpz_sub_ui (values.left, values.left, 1);
11655
          mpz_sub_ui (size, size, 1);
11656
 
11657
          t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11658
          if (t == FAILURE)
11659
            break;
11660
 
11661
          if (mark == AR_FULL)
11662
            mpz_add_ui (offset, offset, 1);
11663
 
11664
          /* Modify the array section indexes and recalculate the offset
11665
             for next element.  */
11666
          else if (mark == AR_SECTION)
11667
            gfc_advance_section (section_index, ar, &offset);
11668
        }
11669
    }
11670
 
11671
  if (mark == AR_SECTION)
11672
    {
11673
      for (i = 0; i < ar->dimen; i++)
11674
        mpz_clear (section_index[i]);
11675
    }
11676
 
11677
  mpz_clear (size);
11678
  mpz_clear (offset);
11679
 
11680
  return t;
11681
}
11682
 
11683
 
11684
static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11685
 
11686
/* Iterate over a list of elements in a DATA statement.  */
11687
 
11688
static gfc_try
11689
traverse_data_list (gfc_data_variable *var, locus *where)
11690
{
11691
  mpz_t trip;
11692
  iterator_stack frame;
11693
  gfc_expr *e, *start, *end, *step;
11694
  gfc_try retval = SUCCESS;
11695
 
11696
  mpz_init (frame.value);
11697
 
11698
  start = gfc_copy_expr (var->iter.start);
11699
  end = gfc_copy_expr (var->iter.end);
11700
  step = gfc_copy_expr (var->iter.step);
11701
 
11702
  if (gfc_simplify_expr (start, 1) == FAILURE
11703
      || start->expr_type != EXPR_CONSTANT)
11704
    {
11705
      gfc_error ("iterator start at %L does not simplify", &start->where);
11706
      retval = FAILURE;
11707
      goto cleanup;
11708
    }
11709
  if (gfc_simplify_expr (end, 1) == FAILURE
11710
      || end->expr_type != EXPR_CONSTANT)
11711
    {
11712
      gfc_error ("iterator end at %L does not simplify", &end->where);
11713
      retval = FAILURE;
11714
      goto cleanup;
11715
    }
11716
  if (gfc_simplify_expr (step, 1) == FAILURE
11717
      || step->expr_type != EXPR_CONSTANT)
11718
    {
11719
      gfc_error ("iterator step at %L does not simplify", &step->where);
11720
      retval = FAILURE;
11721
      goto cleanup;
11722
    }
11723
 
11724
  mpz_init_set (trip, end->value.integer);
11725
  mpz_sub (trip, trip, start->value.integer);
11726
  mpz_add (trip, trip, step->value.integer);
11727
 
11728
  mpz_div (trip, trip, step->value.integer);
11729
 
11730
  mpz_set (frame.value, start->value.integer);
11731
 
11732
  frame.prev = iter_stack;
11733
  frame.variable = var->iter.var->symtree;
11734
  iter_stack = &frame;
11735
 
11736
  while (mpz_cmp_ui (trip, 0) > 0)
11737
    {
11738
      if (traverse_data_var (var->list, where) == FAILURE)
11739
        {
11740
          mpz_clear (trip);
11741
          retval = FAILURE;
11742
          goto cleanup;
11743
        }
11744
 
11745
      e = gfc_copy_expr (var->expr);
11746
      if (gfc_simplify_expr (e, 1) == FAILURE)
11747
        {
11748
          gfc_free_expr (e);
11749
          mpz_clear (trip);
11750
          retval = FAILURE;
11751
          goto cleanup;
11752
        }
11753
 
11754
      mpz_add (frame.value, frame.value, step->value.integer);
11755
 
11756
      mpz_sub_ui (trip, trip, 1);
11757
    }
11758
 
11759
  mpz_clear (trip);
11760
cleanup:
11761
  mpz_clear (frame.value);
11762
 
11763
  gfc_free_expr (start);
11764
  gfc_free_expr (end);
11765
  gfc_free_expr (step);
11766
 
11767
  iter_stack = frame.prev;
11768
  return retval;
11769
}
11770
 
11771
 
11772
/* Type resolve variables in the variable list of a DATA statement.  */
11773
 
11774
static gfc_try
11775
traverse_data_var (gfc_data_variable *var, locus *where)
11776
{
11777
  gfc_try t;
11778
 
11779
  for (; var; var = var->next)
11780
    {
11781
      if (var->expr == NULL)
11782
        t = traverse_data_list (var, where);
11783
      else
11784
        t = check_data_variable (var, where);
11785
 
11786
      if (t == FAILURE)
11787
        return FAILURE;
11788
    }
11789
 
11790
  return SUCCESS;
11791
}
11792
 
11793
 
11794
/* Resolve the expressions and iterators associated with a data statement.
11795
   This is separate from the assignment checking because data lists should
11796
   only be resolved once.  */
11797
 
11798
static gfc_try
11799
resolve_data_variables (gfc_data_variable *d)
11800
{
11801
  for (; d; d = d->next)
11802
    {
11803
      if (d->list == NULL)
11804
        {
11805
          if (gfc_resolve_expr (d->expr) == FAILURE)
11806
            return FAILURE;
11807
        }
11808
      else
11809
        {
11810
          if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
11811
            return FAILURE;
11812
 
11813
          if (resolve_data_variables (d->list) == FAILURE)
11814
            return FAILURE;
11815
        }
11816
    }
11817
 
11818
  return SUCCESS;
11819
}
11820
 
11821
 
11822
/* Resolve a single DATA statement.  We implement this by storing a pointer to
11823
   the value list into static variables, and then recursively traversing the
11824
   variables list, expanding iterators and such.  */
11825
 
11826
static void
11827
resolve_data (gfc_data *d)
11828
{
11829
 
11830
  if (resolve_data_variables (d->var) == FAILURE)
11831
    return;
11832
 
11833
  values.vnode = d->value;
11834
  if (d->value == NULL)
11835
    mpz_set_ui (values.left, 0);
11836
  else
11837
    mpz_set (values.left, d->value->repeat);
11838
 
11839
  if (traverse_data_var (d->var, &d->where) == FAILURE)
11840
    return;
11841
 
11842
  /* At this point, we better not have any values left.  */
11843
 
11844
  if (next_data_value () == SUCCESS)
11845
    gfc_error ("DATA statement at %L has more values than variables",
11846
               &d->where);
11847
}
11848
 
11849
 
11850
/* 12.6 Constraint: In a pure subprogram any variable which is in common or
11851
   accessed by host or use association, is a dummy argument to a pure function,
11852
   is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11853
   is storage associated with any such variable, shall not be used in the
11854
   following contexts: (clients of this function).  */
11855
 
11856
/* Determines if a variable is not 'pure', i.e., not assignable within a pure
11857
   procedure.  Returns zero if assignment is OK, nonzero if there is a
11858
   problem.  */
11859
int
11860
gfc_impure_variable (gfc_symbol *sym)
11861
{
11862
  gfc_symbol *proc;
11863
  gfc_namespace *ns;
11864
 
11865
  if (sym->attr.use_assoc || sym->attr.in_common)
11866
    return 1;
11867
 
11868
  /* Check if the symbol's ns is inside the pure procedure.  */
11869
  for (ns = gfc_current_ns; ns; ns = ns->parent)
11870
    {
11871
      if (ns == sym->ns)
11872
        break;
11873
      if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
11874
        return 1;
11875
    }
11876
 
11877
  proc = sym->ns->proc_name;
11878
  if (sym->attr.dummy && gfc_pure (proc)
11879
        && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11880
                ||
11881
             proc->attr.function))
11882
    return 1;
11883
 
11884
  /* TODO: Sort out what can be storage associated, if anything, and include
11885
     it here.  In principle equivalences should be scanned but it does not
11886
     seem to be possible to storage associate an impure variable this way.  */
11887
  return 0;
11888
}
11889
 
11890
 
11891
/* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
11892
   current namespace is inside a pure procedure.  */
11893
 
11894
int
11895
gfc_pure (gfc_symbol *sym)
11896
{
11897
  symbol_attribute attr;
11898
  gfc_namespace *ns;
11899
 
11900
  if (sym == NULL)
11901
    {
11902
      /* Check if the current namespace or one of its parents
11903
        belongs to a pure procedure.  */
11904
      for (ns = gfc_current_ns; ns; ns = ns->parent)
11905
        {
11906
          sym = ns->proc_name;
11907
          if (sym == NULL)
11908
            return 0;
11909
          attr = sym->attr;
11910
          if (attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental))
11911
            return 1;
11912
        }
11913
      return 0;
11914
    }
11915
 
11916
  attr = sym->attr;
11917
 
11918
  return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11919
}
11920
 
11921
 
11922
/* Test whether the current procedure is elemental or not.  */
11923
 
11924
int
11925
gfc_elemental (gfc_symbol *sym)
11926
{
11927
  symbol_attribute attr;
11928
 
11929
  if (sym == NULL)
11930
    sym = gfc_current_ns->proc_name;
11931
  if (sym == NULL)
11932
    return 0;
11933
  attr = sym->attr;
11934
 
11935
  return attr.flavor == FL_PROCEDURE && attr.elemental;
11936
}
11937
 
11938
 
11939
/* Warn about unused labels.  */
11940
 
11941
static void
11942
warn_unused_fortran_label (gfc_st_label *label)
11943
{
11944
  if (label == NULL)
11945
    return;
11946
 
11947
  warn_unused_fortran_label (label->left);
11948
 
11949
  if (label->defined == ST_LABEL_UNKNOWN)
11950
    return;
11951
 
11952
  switch (label->referenced)
11953
    {
11954
    case ST_LABEL_UNKNOWN:
11955
      gfc_warning ("Label %d at %L defined but not used", label->value,
11956
                   &label->where);
11957
      break;
11958
 
11959
    case ST_LABEL_BAD_TARGET:
11960
      gfc_warning ("Label %d at %L defined but cannot be used",
11961
                   label->value, &label->where);
11962
      break;
11963
 
11964
    default:
11965
      break;
11966
    }
11967
 
11968
  warn_unused_fortran_label (label->right);
11969
}
11970
 
11971
 
11972
/* Returns the sequence type of a symbol or sequence.  */
11973
 
11974
static seq_type
11975
sequence_type (gfc_typespec ts)
11976
{
11977
  seq_type result;
11978
  gfc_component *c;
11979
 
11980
  switch (ts.type)
11981
  {
11982
    case BT_DERIVED:
11983
 
11984
      if (ts.u.derived->components == NULL)
11985
        return SEQ_NONDEFAULT;
11986
 
11987
      result = sequence_type (ts.u.derived->components->ts);
11988
      for (c = ts.u.derived->components->next; c; c = c->next)
11989
        if (sequence_type (c->ts) != result)
11990
          return SEQ_MIXED;
11991
 
11992
      return result;
11993
 
11994
    case BT_CHARACTER:
11995
      if (ts.kind != gfc_default_character_kind)
11996
          return SEQ_NONDEFAULT;
11997
 
11998
      return SEQ_CHARACTER;
11999
 
12000
    case BT_INTEGER:
12001
      if (ts.kind != gfc_default_integer_kind)
12002
          return SEQ_NONDEFAULT;
12003
 
12004
      return SEQ_NUMERIC;
12005
 
12006
    case BT_REAL:
12007
      if (!(ts.kind == gfc_default_real_kind
12008
            || ts.kind == gfc_default_double_kind))
12009
          return SEQ_NONDEFAULT;
12010
 
12011
      return SEQ_NUMERIC;
12012
 
12013
    case BT_COMPLEX:
12014
      if (ts.kind != gfc_default_complex_kind)
12015
          return SEQ_NONDEFAULT;
12016
 
12017
      return SEQ_NUMERIC;
12018
 
12019
    case BT_LOGICAL:
12020
      if (ts.kind != gfc_default_logical_kind)
12021
          return SEQ_NONDEFAULT;
12022
 
12023
      return SEQ_NUMERIC;
12024
 
12025
    default:
12026
      return SEQ_NONDEFAULT;
12027
  }
12028
}
12029
 
12030
 
12031
/* Resolve derived type EQUIVALENCE object.  */
12032
 
12033
static gfc_try
12034
resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12035
{
12036
  gfc_component *c = derived->components;
12037
 
12038
  if (!derived)
12039
    return SUCCESS;
12040
 
12041
  /* Shall not be an object of nonsequence derived type.  */
12042
  if (!derived->attr.sequence)
12043
    {
12044
      gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12045
                 "attribute to be an EQUIVALENCE object", sym->name,
12046
                 &e->where);
12047
      return FAILURE;
12048
    }
12049
 
12050
  /* Shall not have allocatable components.  */
12051
  if (derived->attr.alloc_comp)
12052
    {
12053
      gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12054
                 "components to be an EQUIVALENCE object",sym->name,
12055
                 &e->where);
12056
      return FAILURE;
12057
    }
12058
 
12059
  if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
12060
    {
12061
      gfc_error ("Derived type variable '%s' at %L with default "
12062
                 "initialization cannot be in EQUIVALENCE with a variable "
12063
                 "in COMMON", sym->name, &e->where);
12064
      return FAILURE;
12065
    }
12066
 
12067
  for (; c ; c = c->next)
12068
    {
12069
      if (c->ts.type == BT_DERIVED
12070
          && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12071
        return FAILURE;
12072
 
12073
      /* Shall not be an object of sequence derived type containing a pointer
12074
         in the structure.  */
12075
      if (c->attr.pointer)
12076
        {
12077
          gfc_error ("Derived type variable '%s' at %L with pointer "
12078
                     "component(s) cannot be an EQUIVALENCE object",
12079
                     sym->name, &e->where);
12080
          return FAILURE;
12081
        }
12082
    }
12083
  return SUCCESS;
12084
}
12085
 
12086
 
12087
/* Resolve equivalence object.
12088
   An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12089
   an allocatable array, an object of nonsequence derived type, an object of
12090
   sequence derived type containing a pointer at any level of component
12091
   selection, an automatic object, a function name, an entry name, a result
12092
   name, a named constant, a structure component, or a subobject of any of
12093
   the preceding objects.  A substring shall not have length zero.  A
12094
   derived type shall not have components with default initialization nor
12095
   shall two objects of an equivalence group be initialized.
12096
   Either all or none of the objects shall have an protected attribute.
12097
   The simple constraints are done in symbol.c(check_conflict) and the rest
12098
   are implemented here.  */
12099
 
12100
static void
12101
resolve_equivalence (gfc_equiv *eq)
12102
{
12103
  gfc_symbol *sym;
12104
  gfc_symbol *first_sym;
12105
  gfc_expr *e;
12106
  gfc_ref *r;
12107
  locus *last_where = NULL;
12108
  seq_type eq_type, last_eq_type;
12109
  gfc_typespec *last_ts;
12110
  int object, cnt_protected;
12111
  const char *msg;
12112
 
12113
  last_ts = &eq->expr->symtree->n.sym->ts;
12114
 
12115
  first_sym = eq->expr->symtree->n.sym;
12116
 
12117
  cnt_protected = 0;
12118
 
12119
  for (object = 1; eq; eq = eq->eq, object++)
12120
    {
12121
      e = eq->expr;
12122
 
12123
      e->ts = e->symtree->n.sym->ts;
12124
      /* match_varspec might not know yet if it is seeing
12125
         array reference or substring reference, as it doesn't
12126
         know the types.  */
12127
      if (e->ref && e->ref->type == REF_ARRAY)
12128
        {
12129
          gfc_ref *ref = e->ref;
12130
          sym = e->symtree->n.sym;
12131
 
12132
          if (sym->attr.dimension)
12133
            {
12134
              ref->u.ar.as = sym->as;
12135
              ref = ref->next;
12136
            }
12137
 
12138
          /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
12139
          if (e->ts.type == BT_CHARACTER
12140
              && ref
12141
              && ref->type == REF_ARRAY
12142
              && ref->u.ar.dimen == 1
12143
              && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12144
              && ref->u.ar.stride[0] == NULL)
12145
            {
12146
              gfc_expr *start = ref->u.ar.start[0];
12147
              gfc_expr *end = ref->u.ar.end[0];
12148
              void *mem = NULL;
12149
 
12150
              /* Optimize away the (:) reference.  */
12151
              if (start == NULL && end == NULL)
12152
                {
12153
                  if (e->ref == ref)
12154
                    e->ref = ref->next;
12155
                  else
12156
                    e->ref->next = ref->next;
12157
                  mem = ref;
12158
                }
12159
              else
12160
                {
12161
                  ref->type = REF_SUBSTRING;
12162
                  if (start == NULL)
12163
                    start = gfc_int_expr (1);
12164
                  ref->u.ss.start = start;
12165
                  if (end == NULL && e->ts.u.cl)
12166
                    end = gfc_copy_expr (e->ts.u.cl->length);
12167
                  ref->u.ss.end = end;
12168
                  ref->u.ss.length = e->ts.u.cl;
12169
                  e->ts.u.cl = NULL;
12170
                }
12171
              ref = ref->next;
12172
              gfc_free (mem);
12173
            }
12174
 
12175
          /* Any further ref is an error.  */
12176
          if (ref)
12177
            {
12178
              gcc_assert (ref->type == REF_ARRAY);
12179
              gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12180
                         &ref->u.ar.where);
12181
              continue;
12182
            }
12183
        }
12184
 
12185
      if (gfc_resolve_expr (e) == FAILURE)
12186
        continue;
12187
 
12188
      sym = e->symtree->n.sym;
12189
 
12190
      if (sym->attr.is_protected)
12191
        cnt_protected++;
12192
      if (cnt_protected > 0 && cnt_protected != object)
12193
        {
12194
              gfc_error ("Either all or none of the objects in the "
12195
                         "EQUIVALENCE set at %L shall have the "
12196
                         "PROTECTED attribute",
12197
                         &e->where);
12198
              break;
12199
        }
12200
 
12201
      /* Shall not equivalence common block variables in a PURE procedure.  */
12202
      if (sym->ns->proc_name
12203
          && sym->ns->proc_name->attr.pure
12204
          && sym->attr.in_common)
12205
        {
12206
          gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12207
                     "object in the pure procedure '%s'",
12208
                     sym->name, &e->where, sym->ns->proc_name->name);
12209
          break;
12210
        }
12211
 
12212
      /* Shall not be a named constant.  */
12213
      if (e->expr_type == EXPR_CONSTANT)
12214
        {
12215
          gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12216
                     "object", sym->name, &e->where);
12217
          continue;
12218
        }
12219
 
12220
      if (e->ts.type == BT_DERIVED
12221
          && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12222
        continue;
12223
 
12224
      /* Check that the types correspond correctly:
12225
         Note 5.28:
12226
         A numeric sequence structure may be equivalenced to another sequence
12227
         structure, an object of default integer type, default real type, double
12228
         precision real type, default logical type such that components of the
12229
         structure ultimately only become associated to objects of the same
12230
         kind. A character sequence structure may be equivalenced to an object
12231
         of default character kind or another character sequence structure.
12232
         Other objects may be equivalenced only to objects of the same type and
12233
         kind parameters.  */
12234
 
12235
      /* Identical types are unconditionally OK.  */
12236
      if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12237
        goto identical_types;
12238
 
12239
      last_eq_type = sequence_type (*last_ts);
12240
      eq_type = sequence_type (sym->ts);
12241
 
12242
      /* Since the pair of objects is not of the same type, mixed or
12243
         non-default sequences can be rejected.  */
12244
 
12245
      msg = "Sequence %s with mixed components in EQUIVALENCE "
12246
            "statement at %L with different type objects";
12247
      if ((object ==2
12248
           && last_eq_type == SEQ_MIXED
12249
           && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12250
              == FAILURE)
12251
          || (eq_type == SEQ_MIXED
12252
              && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12253
                                 &e->where) == FAILURE))
12254
        continue;
12255
 
12256
      msg = "Non-default type object or sequence %s in EQUIVALENCE "
12257
            "statement at %L with objects of different type";
12258
      if ((object ==2
12259
           && last_eq_type == SEQ_NONDEFAULT
12260
           && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12261
                              last_where) == FAILURE)
12262
          || (eq_type == SEQ_NONDEFAULT
12263
              && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12264
                                 &e->where) == FAILURE))
12265
        continue;
12266
 
12267
      msg ="Non-CHARACTER object '%s' in default CHARACTER "
12268
           "EQUIVALENCE statement at %L";
12269
      if (last_eq_type == SEQ_CHARACTER
12270
          && eq_type != SEQ_CHARACTER
12271
          && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12272
                             &e->where) == FAILURE)
12273
                continue;
12274
 
12275
      msg ="Non-NUMERIC object '%s' in default NUMERIC "
12276
           "EQUIVALENCE statement at %L";
12277
      if (last_eq_type == SEQ_NUMERIC
12278
          && eq_type != SEQ_NUMERIC
12279
          && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12280
                             &e->where) == FAILURE)
12281
                continue;
12282
 
12283
  identical_types:
12284
      last_ts =&sym->ts;
12285
      last_where = &e->where;
12286
 
12287
      if (!e->ref)
12288
        continue;
12289
 
12290
      /* Shall not be an automatic array.  */
12291
      if (e->ref->type == REF_ARRAY
12292
          && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12293
        {
12294
          gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12295
                     "an EQUIVALENCE object", sym->name, &e->where);
12296
          continue;
12297
        }
12298
 
12299
      r = e->ref;
12300
      while (r)
12301
        {
12302
          /* Shall not be a structure component.  */
12303
          if (r->type == REF_COMPONENT)
12304
            {
12305
              gfc_error ("Structure component '%s' at %L cannot be an "
12306
                         "EQUIVALENCE object",
12307
                         r->u.c.component->name, &e->where);
12308
              break;
12309
            }
12310
 
12311
          /* A substring shall not have length zero.  */
12312
          if (r->type == REF_SUBSTRING)
12313
            {
12314
              if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12315
                {
12316
                  gfc_error ("Substring at %L has length zero",
12317
                             &r->u.ss.start->where);
12318
                  break;
12319
                }
12320
            }
12321
          r = r->next;
12322
        }
12323
    }
12324
}
12325
 
12326
 
12327
/* Resolve function and ENTRY types, issue diagnostics if needed.  */
12328
 
12329
static void
12330
resolve_fntype (gfc_namespace *ns)
12331
{
12332
  gfc_entry_list *el;
12333
  gfc_symbol *sym;
12334
 
12335
  if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12336
    return;
12337
 
12338
  /* If there are any entries, ns->proc_name is the entry master
12339
     synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
12340
  if (ns->entries)
12341
    sym = ns->entries->sym;
12342
  else
12343
    sym = ns->proc_name;
12344
  if (sym->result == sym
12345
      && sym->ts.type == BT_UNKNOWN
12346
      && gfc_set_default_type (sym, 0, NULL) == FAILURE
12347
      && !sym->attr.untyped)
12348
    {
12349
      gfc_error ("Function '%s' at %L has no IMPLICIT type",
12350
                 sym->name, &sym->declared_at);
12351
      sym->attr.untyped = 1;
12352
    }
12353
 
12354
  if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12355
      && !sym->attr.contained
12356
      && !gfc_check_access (sym->ts.u.derived->attr.access,
12357
                            sym->ts.u.derived->ns->default_access)
12358
      && gfc_check_access (sym->attr.access, sym->ns->default_access))
12359
    {
12360
      gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12361
                      "%L of PRIVATE type '%s'", sym->name,
12362
                      &sym->declared_at, sym->ts.u.derived->name);
12363
    }
12364
 
12365
    if (ns->entries)
12366
    for (el = ns->entries->next; el; el = el->next)
12367
      {
12368
        if (el->sym->result == el->sym
12369
            && el->sym->ts.type == BT_UNKNOWN
12370
            && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12371
            && !el->sym->attr.untyped)
12372
          {
12373
            gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12374
                       el->sym->name, &el->sym->declared_at);
12375
            el->sym->attr.untyped = 1;
12376
          }
12377
      }
12378
}
12379
 
12380
 
12381
/* 12.3.2.1.1 Defined operators.  */
12382
 
12383
static gfc_try
12384
check_uop_procedure (gfc_symbol *sym, locus where)
12385
{
12386
  gfc_formal_arglist *formal;
12387
 
12388
  if (!sym->attr.function)
12389
    {
12390
      gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12391
                 sym->name, &where);
12392
      return FAILURE;
12393
    }
12394
 
12395
  if (sym->ts.type == BT_CHARACTER
12396
      && !(sym->ts.u.cl && sym->ts.u.cl->length)
12397
      && !(sym->result && sym->result->ts.u.cl
12398
           && sym->result->ts.u.cl->length))
12399
    {
12400
      gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12401
                 "character length", sym->name, &where);
12402
      return FAILURE;
12403
    }
12404
 
12405
  formal = sym->formal;
12406
  if (!formal || !formal->sym)
12407
    {
12408
      gfc_error ("User operator procedure '%s' at %L must have at least "
12409
                 "one argument", sym->name, &where);
12410
      return FAILURE;
12411
    }
12412
 
12413
  if (formal->sym->attr.intent != INTENT_IN)
12414
    {
12415
      gfc_error ("First argument of operator interface at %L must be "
12416
                 "INTENT(IN)", &where);
12417
      return FAILURE;
12418
    }
12419
 
12420
  if (formal->sym->attr.optional)
12421
    {
12422
      gfc_error ("First argument of operator interface at %L cannot be "
12423
                 "optional", &where);
12424
      return FAILURE;
12425
    }
12426
 
12427
  formal = formal->next;
12428
  if (!formal || !formal->sym)
12429
    return SUCCESS;
12430
 
12431
  if (formal->sym->attr.intent != INTENT_IN)
12432
    {
12433
      gfc_error ("Second argument of operator interface at %L must be "
12434
                 "INTENT(IN)", &where);
12435
      return FAILURE;
12436
    }
12437
 
12438
  if (formal->sym->attr.optional)
12439
    {
12440
      gfc_error ("Second argument of operator interface at %L cannot be "
12441
                 "optional", &where);
12442
      return FAILURE;
12443
    }
12444
 
12445
  if (formal->next)
12446
    {
12447
      gfc_error ("Operator interface at %L must have, at most, two "
12448
                 "arguments", &where);
12449
      return FAILURE;
12450
    }
12451
 
12452
  return SUCCESS;
12453
}
12454
 
12455
static void
12456
gfc_resolve_uops (gfc_symtree *symtree)
12457
{
12458
  gfc_interface *itr;
12459
 
12460
  if (symtree == NULL)
12461
    return;
12462
 
12463
  gfc_resolve_uops (symtree->left);
12464
  gfc_resolve_uops (symtree->right);
12465
 
12466
  for (itr = symtree->n.uop->op; itr; itr = itr->next)
12467
    check_uop_procedure (itr->sym, itr->sym->declared_at);
12468
}
12469
 
12470
 
12471
/* Examine all of the expressions associated with a program unit,
12472
   assign types to all intermediate expressions, make sure that all
12473
   assignments are to compatible types and figure out which names
12474
   refer to which functions or subroutines.  It doesn't check code
12475
   block, which is handled by resolve_code.  */
12476
 
12477
static void
12478
resolve_types (gfc_namespace *ns)
12479
{
12480
  gfc_namespace *n;
12481
  gfc_charlen *cl;
12482
  gfc_data *d;
12483
  gfc_equiv *eq;
12484
  gfc_namespace* old_ns = gfc_current_ns;
12485
 
12486
  /* Check that all IMPLICIT types are ok.  */
12487
  if (!ns->seen_implicit_none)
12488
    {
12489
      unsigned letter;
12490
      for (letter = 0; letter != GFC_LETTERS; ++letter)
12491
        if (ns->set_flag[letter]
12492
            && resolve_typespec_used (&ns->default_type[letter],
12493
                                      &ns->implicit_loc[letter],
12494
                                      NULL) == FAILURE)
12495
          return;
12496
    }
12497
 
12498
  gfc_current_ns = ns;
12499
 
12500
  resolve_entries (ns);
12501
 
12502
  resolve_common_vars (ns->blank_common.head, false);
12503
  resolve_common_blocks (ns->common_root);
12504
 
12505
  resolve_contained_functions (ns);
12506
 
12507
  gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12508
 
12509
  for (cl = ns->cl_list; cl; cl = cl->next)
12510
    resolve_charlen (cl);
12511
 
12512
  gfc_traverse_ns (ns, resolve_symbol);
12513
 
12514
  resolve_fntype (ns);
12515
 
12516
  for (n = ns->contained; n; n = n->sibling)
12517
    {
12518
      if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12519
        gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12520
                   "also be PURE", n->proc_name->name,
12521
                   &n->proc_name->declared_at);
12522
 
12523
      resolve_types (n);
12524
    }
12525
 
12526
  forall_flag = 0;
12527
  gfc_check_interfaces (ns);
12528
 
12529
  gfc_traverse_ns (ns, resolve_values);
12530
 
12531
  if (ns->save_all)
12532
    gfc_save_all (ns);
12533
 
12534
  iter_stack = NULL;
12535
  for (d = ns->data; d; d = d->next)
12536
    resolve_data (d);
12537
 
12538
  iter_stack = NULL;
12539
  gfc_traverse_ns (ns, gfc_formalize_init_value);
12540
 
12541
  gfc_traverse_ns (ns, gfc_verify_binding_labels);
12542
 
12543
  if (ns->common_root != NULL)
12544
    gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12545
 
12546
  for (eq = ns->equiv; eq; eq = eq->next)
12547
    resolve_equivalence (eq);
12548
 
12549
  /* Warn about unused labels.  */
12550
  if (warn_unused_label)
12551
    warn_unused_fortran_label (ns->st_labels);
12552
 
12553
  gfc_resolve_uops (ns->uop_root);
12554
 
12555
  gfc_current_ns = old_ns;
12556
}
12557
 
12558
 
12559
/* Call resolve_code recursively.  */
12560
 
12561
static void
12562
resolve_codes (gfc_namespace *ns)
12563
{
12564
  gfc_namespace *n;
12565
  bitmap_obstack old_obstack;
12566
 
12567
  for (n = ns->contained; n; n = n->sibling)
12568
    resolve_codes (n);
12569
 
12570
  gfc_current_ns = ns;
12571
 
12572
  /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
12573
  if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12574
    cs_base = NULL;
12575
 
12576
  /* Set to an out of range value.  */
12577
  current_entry_id = -1;
12578
 
12579
  old_obstack = labels_obstack;
12580
  bitmap_obstack_initialize (&labels_obstack);
12581
 
12582
  resolve_code (ns->code, ns);
12583
 
12584
  bitmap_obstack_release (&labels_obstack);
12585
  labels_obstack = old_obstack;
12586
}
12587
 
12588
 
12589
/* This function is called after a complete program unit has been compiled.
12590
   Its purpose is to examine all of the expressions associated with a program
12591
   unit, assign types to all intermediate expressions, make sure that all
12592
   assignments are to compatible types and figure out which names refer to
12593
   which functions or subroutines.  */
12594
 
12595
void
12596
gfc_resolve (gfc_namespace *ns)
12597
{
12598
  gfc_namespace *old_ns;
12599
  code_stack *old_cs_base;
12600
 
12601
  if (ns->resolved)
12602
    return;
12603
 
12604
  ns->resolved = -1;
12605
  old_ns = gfc_current_ns;
12606
  old_cs_base = cs_base;
12607
 
12608
  resolve_types (ns);
12609
  resolve_codes (ns);
12610
 
12611
  gfc_current_ns = old_ns;
12612
  cs_base = old_cs_base;
12613
  ns->resolved = 1;
12614
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.