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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [resolve.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Perform type resolution on the various stuctures.
2
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
3
   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 2, 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 COPYING.  If not, write to the Free
20
Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
21
02110-1301, USA.  */
22
 
23
 
24
#include "config.h"
25
#include "system.h"
26
#include "gfortran.h"
27
#include "arith.h"  /* For gfc_compare_expr().  */
28
 
29
/* Types used in equivalence statements.  */
30
 
31
typedef enum seq_type
32
{
33
  SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
34
}
35
seq_type;
36
 
37
/* Stack to push the current if we descend into a block during
38
   resolution.  See resolve_branch() and resolve_code().  */
39
 
40
typedef struct code_stack
41
{
42
  struct gfc_code *head, *current;
43
  struct code_stack *prev;
44
}
45
code_stack;
46
 
47
static code_stack *cs_base = NULL;
48
 
49
 
50
/* Nonzero if we're inside a FORALL block */
51
 
52
static int forall_flag;
53
 
54
/* Nonzero if we are processing a formal arglist. The corresponding function
55
   resets the flag each time that it is read.  */
56
static int formal_arg_flag = 0;
57
 
58
int
59
gfc_is_formal_arg (void)
60
{
61
  return formal_arg_flag;
62
}
63
 
64
/* Resolve types of formal argument lists.  These have to be done early so that
65
   the formal argument lists of module procedures can be copied to the
66
   containing module before the individual procedures are resolved
67
   individually.  We also resolve argument lists of procedures in interface
68
   blocks because they are self-contained scoping units.
69
 
70
   Since a dummy argument cannot be a non-dummy procedure, the only
71
   resort left for untyped names are the IMPLICIT types.  */
72
 
73
static void
74
resolve_formal_arglist (gfc_symbol * proc)
75
{
76
  gfc_formal_arglist *f;
77
  gfc_symbol *sym;
78
  int i;
79
 
80
  /* TODO: Procedures whose return character length parameter is not constant
81
     or assumed must also have explicit interfaces.  */
82
  if (proc->result != NULL)
83
    sym = proc->result;
84
  else
85
    sym = proc;
86
 
87
  if (gfc_elemental (proc)
88
      || sym->attr.pointer || sym->attr.allocatable
89
      || (sym->as && sym->as->rank > 0))
90
    proc->attr.always_explicit = 1;
91
 
92
  formal_arg_flag = 1;
93
 
94
  for (f = proc->formal; f; f = f->next)
95
    {
96
      sym = f->sym;
97
 
98
      if (sym == NULL)
99
        {
100
          /* Alternate return placeholder.  */
101
          if (gfc_elemental (proc))
102
            gfc_error ("Alternate return specifier in elemental subroutine "
103
                       "'%s' at %L is not allowed", proc->name,
104
                       &proc->declared_at);
105
          if (proc->attr.function)
106
            gfc_error ("Alternate return specifier in function "
107
                       "'%s' at %L is not allowed", proc->name,
108
                       &proc->declared_at);
109
          continue;
110
        }
111
 
112
      if (sym->attr.if_source != IFSRC_UNKNOWN)
113
        resolve_formal_arglist (sym);
114
 
115
      if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
116
        {
117
          if (gfc_pure (proc) && !gfc_pure (sym))
118
            {
119
              gfc_error
120
                ("Dummy procedure '%s' of PURE procedure at %L must also "
121
                 "be PURE", sym->name, &sym->declared_at);
122
              continue;
123
            }
124
 
125
          if (gfc_elemental (proc))
126
            {
127
              gfc_error
128
                ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
129
                 &sym->declared_at);
130
              continue;
131
            }
132
 
133
          continue;
134
        }
135
 
136
      if (sym->ts.type == BT_UNKNOWN)
137
        {
138
          if (!sym->attr.function || sym->result == sym)
139
            gfc_set_default_type (sym, 1, sym->ns);
140
        }
141
 
142
      gfc_resolve_array_spec (sym->as, 0);
143
 
144
      /* We can't tell if an array with dimension (:) is assumed or deferred
145
         shape until we know if it has the pointer or allocatable attributes.
146
      */
147
      if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
148
          && !(sym->attr.pointer || sym->attr.allocatable))
149
        {
150
          sym->as->type = AS_ASSUMED_SHAPE;
151
          for (i = 0; i < sym->as->rank; i++)
152
            sym->as->lower[i] = gfc_int_expr (1);
153
        }
154
 
155
      if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
156
          || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
157
          || sym->attr.optional)
158
        proc->attr.always_explicit = 1;
159
 
160
      /* If the flavor is unknown at this point, it has to be a variable.
161
         A procedure specification would have already set the type.  */
162
 
163
      if (sym->attr.flavor == FL_UNKNOWN)
164
        gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
165
 
166
      if (gfc_pure (proc))
167
        {
168
          if (proc->attr.function && !sym->attr.pointer
169
              && sym->attr.flavor != FL_PROCEDURE
170
              && sym->attr.intent != INTENT_IN)
171
 
172
            gfc_error ("Argument '%s' of pure function '%s' at %L must be "
173
                       "INTENT(IN)", sym->name, proc->name,
174
                       &sym->declared_at);
175
 
176
          if (proc->attr.subroutine && !sym->attr.pointer
177
              && sym->attr.intent == INTENT_UNKNOWN)
178
 
179
            gfc_error
180
              ("Argument '%s' of pure subroutine '%s' at %L must have "
181
               "its INTENT specified", sym->name, proc->name,
182
               &sym->declared_at);
183
        }
184
 
185
 
186
      if (gfc_elemental (proc))
187
        {
188
          if (sym->as != NULL)
189
            {
190
              gfc_error
191
                ("Argument '%s' of elemental procedure at %L must be scalar",
192
                 sym->name, &sym->declared_at);
193
              continue;
194
            }
195
 
196
          if (sym->attr.pointer)
197
            {
198
              gfc_error
199
                ("Argument '%s' of elemental procedure at %L cannot have "
200
                 "the POINTER attribute", sym->name, &sym->declared_at);
201
              continue;
202
            }
203
        }
204
 
205
      /* Each dummy shall be specified to be scalar.  */
206
      if (proc->attr.proc == PROC_ST_FUNCTION)
207
        {
208
          if (sym->as != NULL)
209
            {
210
              gfc_error
211
                ("Argument '%s' of statement function at %L must be scalar",
212
                 sym->name, &sym->declared_at);
213
              continue;
214
            }
215
 
216
          if (sym->ts.type == BT_CHARACTER)
217
            {
218
              gfc_charlen *cl = sym->ts.cl;
219
              if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
220
                {
221
                  gfc_error
222
                    ("Character-valued argument '%s' of statement function at "
223
                     "%L must has constant length",
224
                     sym->name, &sym->declared_at);
225
                  continue;
226
                }
227
            }
228
        }
229
    }
230
  formal_arg_flag = 0;
231
}
232
 
233
 
234
/* Work function called when searching for symbols that have argument lists
235
   associated with them.  */
236
 
237
static void
238
find_arglists (gfc_symbol * sym)
239
{
240
 
241
  if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
242
    return;
243
 
244
  resolve_formal_arglist (sym);
245
}
246
 
247
 
248
/* Given a namespace, resolve all formal argument lists within the namespace.
249
 */
250
 
251
static void
252
resolve_formal_arglists (gfc_namespace * ns)
253
{
254
 
255
  if (ns == NULL)
256
    return;
257
 
258
  gfc_traverse_ns (ns, find_arglists);
259
}
260
 
261
 
262
static void
263
resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
264
{
265
  try t;
266
 
267
  /* If this namespace is not a function, ignore it.  */
268
  if (! sym
269
      || !(sym->attr.function
270
           || sym->attr.flavor == FL_VARIABLE))
271
    return;
272
 
273
  /* Try to find out of what the return type is.  */
274
  if (sym->result != NULL)
275
    sym = sym->result;
276
 
277
  if (sym->ts.type == BT_UNKNOWN)
278
    {
279
      t = gfc_set_default_type (sym, 0, ns);
280
 
281
      if (t == FAILURE && !sym->attr.untyped)
282
        {
283
          gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
284
                     sym->name, &sym->declared_at); /* FIXME */
285
          sym->attr.untyped = 1;
286
        }
287
    }
288
 
289
  if (sym->ts.type == BT_CHARACTER)
290
    {
291
      gfc_charlen *cl = sym->ts.cl;
292
      if (!cl || !cl->length)
293
        gfc_error ("Character-valued internal function '%s' at %L must "
294
                   "not be assumed length", sym->name, &sym->declared_at);
295
    }
296
}
297
 
298
 
299
/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
300
   introduce duplicates.  */
301
 
302
static void
303
merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
304
{
305
  gfc_formal_arglist *f, *new_arglist;
306
  gfc_symbol *new_sym;
307
 
308
  for (; new_args != NULL; new_args = new_args->next)
309
    {
310
      new_sym = new_args->sym;
311
      /* See if ths arg is already in the formal argument list.  */
312
      for (f = proc->formal; f; f = f->next)
313
        {
314
          if (new_sym == f->sym)
315
            break;
316
        }
317
 
318
      if (f)
319
        continue;
320
 
321
      /* Add a new argument.  Argument order is not important.  */
322
      new_arglist = gfc_get_formal_arglist ();
323
      new_arglist->sym = new_sym;
324
      new_arglist->next = proc->formal;
325
      proc->formal  = new_arglist;
326
    }
327
}
328
 
329
 
330
/* Resolve alternate entry points.  If a symbol has multiple entry points we
331
   create a new master symbol for the main routine, and turn the existing
332
   symbol into an entry point.  */
333
 
334
static void
335
resolve_entries (gfc_namespace * ns)
336
{
337
  gfc_namespace *old_ns;
338
  gfc_code *c;
339
  gfc_symbol *proc;
340
  gfc_entry_list *el;
341
  char name[GFC_MAX_SYMBOL_LEN + 1];
342
  static int master_count = 0;
343
 
344
  if (ns->proc_name == NULL)
345
    return;
346
 
347
  /* No need to do anything if this procedure doesn't have alternate entry
348
     points.  */
349
  if (!ns->entries)
350
    return;
351
 
352
  /* We may already have resolved alternate entry points.  */
353
  if (ns->proc_name->attr.entry_master)
354
    return;
355
 
356
  /* If this isn't a procedure something has gone horribly wrong.  */
357
  gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
358
 
359
  /* Remember the current namespace.  */
360
  old_ns = gfc_current_ns;
361
 
362
  gfc_current_ns = ns;
363
 
364
  /* Add the main entry point to the list of entry points.  */
365
  el = gfc_get_entry_list ();
366
  el->sym = ns->proc_name;
367
  el->id = 0;
368
  el->next = ns->entries;
369
  ns->entries = el;
370
  ns->proc_name->attr.entry = 1;
371
 
372
  /* Add an entry statement for it.  */
373
  c = gfc_get_code ();
374
  c->op = EXEC_ENTRY;
375
  c->ext.entry = el;
376
  c->next = ns->code;
377
  ns->code = c;
378
 
379
  /* Create a new symbol for the master function.  */
380
  /* Give the internal function a unique name (within this file).
381
     Also include the function name so the user has some hope of figuring
382
     out what is going on.  */
383
  snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
384
            master_count++, ns->proc_name->name);
385
  gfc_get_ha_symbol (name, &proc);
386
  gcc_assert (proc != NULL);
387
 
388
  gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
389
  if (ns->proc_name->attr.subroutine)
390
    gfc_add_subroutine (&proc->attr, proc->name, NULL);
391
  else
392
    {
393
      gfc_symbol *sym;
394
      gfc_typespec *ts, *fts;
395
 
396
      gfc_add_function (&proc->attr, proc->name, NULL);
397
      proc->result = proc;
398
      fts = &ns->entries->sym->result->ts;
399
      if (fts->type == BT_UNKNOWN)
400
        fts = gfc_get_default_type (ns->entries->sym->result, NULL);
401
      for (el = ns->entries->next; el; el = el->next)
402
        {
403
          ts = &el->sym->result->ts;
404
          if (ts->type == BT_UNKNOWN)
405
            ts = gfc_get_default_type (el->sym->result, NULL);
406
          if (! gfc_compare_types (ts, fts)
407
              || (el->sym->result->attr.dimension
408
                  != ns->entries->sym->result->attr.dimension)
409
              || (el->sym->result->attr.pointer
410
                  != ns->entries->sym->result->attr.pointer))
411
            break;
412
        }
413
 
414
      if (el == NULL)
415
        {
416
          sym = ns->entries->sym->result;
417
          /* All result types the same.  */
418
          proc->ts = *fts;
419
          if (sym->attr.dimension)
420
            gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
421
          if (sym->attr.pointer)
422
            gfc_add_pointer (&proc->attr, NULL);
423
        }
424
      else
425
        {
426
          /* Otherwise the result will be passed through a union by
427
             reference.  */
428
          proc->attr.mixed_entry_master = 1;
429
          for (el = ns->entries; el; el = el->next)
430
            {
431
              sym = el->sym->result;
432
              if (sym->attr.dimension)
433
              {
434
                if (el == ns->entries)
435
                  gfc_error
436
                  ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
437
                   sym->name, ns->entries->sym->name, &sym->declared_at);
438
                else
439
                  gfc_error
440
                    ("ENTRY result %s can't be an array in FUNCTION %s at %L",
441
                     sym->name, ns->entries->sym->name, &sym->declared_at);
442
              }
443
              else if (sym->attr.pointer)
444
              {
445
                if (el == ns->entries)
446
                  gfc_error
447
                  ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
448
                   sym->name, ns->entries->sym->name, &sym->declared_at);
449
                else
450
                  gfc_error
451
                    ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
452
                     sym->name, ns->entries->sym->name, &sym->declared_at);
453
              }
454
              else
455
                {
456
                  ts = &sym->ts;
457
                  if (ts->type == BT_UNKNOWN)
458
                    ts = gfc_get_default_type (sym, NULL);
459
                  switch (ts->type)
460
                    {
461
                    case BT_INTEGER:
462
                      if (ts->kind == gfc_default_integer_kind)
463
                        sym = NULL;
464
                      break;
465
                    case BT_REAL:
466
                      if (ts->kind == gfc_default_real_kind
467
                          || ts->kind == gfc_default_double_kind)
468
                        sym = NULL;
469
                      break;
470
                    case BT_COMPLEX:
471
                      if (ts->kind == gfc_default_complex_kind)
472
                        sym = NULL;
473
                      break;
474
                    case BT_LOGICAL:
475
                      if (ts->kind == gfc_default_logical_kind)
476
                        sym = NULL;
477
                      break;
478
                    case BT_UNKNOWN:
479
                      /* We will issue error elsewhere.  */
480
                      sym = NULL;
481
                      break;
482
                    default:
483
                      break;
484
                    }
485
                  if (sym)
486
                  {
487
                    if (el == ns->entries)
488
                      gfc_error
489
                        ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
490
                         sym->name, gfc_typename (ts), ns->entries->sym->name,
491
                         &sym->declared_at);
492
                    else
493
                      gfc_error
494
                        ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
495
                         sym->name, gfc_typename (ts), ns->entries->sym->name,
496
                         &sym->declared_at);
497
                  }
498
                }
499
            }
500
        }
501
    }
502
  proc->attr.access = ACCESS_PRIVATE;
503
  proc->attr.entry_master = 1;
504
 
505
  /* Merge all the entry point arguments.  */
506
  for (el = ns->entries; el; el = el->next)
507
    merge_argument_lists (proc, el->sym->formal);
508
 
509
  /* Use the master function for the function body.  */
510
  ns->proc_name = proc;
511
 
512
  /* Finalize the new symbols.  */
513
  gfc_commit_symbols ();
514
 
515
  /* Restore the original namespace.  */
516
  gfc_current_ns = old_ns;
517
}
518
 
519
 
520
/* Resolve contained function types.  Because contained functions can call one
521
   another, they have to be worked out before any of the contained procedures
522
   can be resolved.
523
 
524
   The good news is that if a function doesn't already have a type, the only
525
   way it can get one is through an IMPLICIT type or a RESULT variable, because
526
   by definition contained functions are contained namespace they're contained
527
   in, not in a sibling or parent namespace.  */
528
 
529
static void
530
resolve_contained_functions (gfc_namespace * ns)
531
{
532
  gfc_namespace *child;
533
  gfc_entry_list *el;
534
 
535
  resolve_formal_arglists (ns);
536
 
537
  for (child = ns->contained; child; child = child->sibling)
538
    {
539
      /* Resolve alternate entry points first.  */
540
      resolve_entries (child);
541
 
542
      /* Then check function return types.  */
543
      resolve_contained_fntype (child->proc_name, child);
544
      for (el = child->entries; el; el = el->next)
545
        resolve_contained_fntype (el->sym, child);
546
    }
547
}
548
 
549
 
550
/* Resolve all of the elements of a structure constructor and make sure that
551
   the types are correct.  */
552
 
553
static try
554
resolve_structure_cons (gfc_expr * expr)
555
{
556
  gfc_constructor *cons;
557
  gfc_component *comp;
558
  try t;
559
 
560
  t = SUCCESS;
561
  cons = expr->value.constructor;
562
  /* A constructor may have references if it is the result of substituting a
563
     parameter variable.  In this case we just pull out the component we
564
     want.  */
565
  if (expr->ref)
566
    comp = expr->ref->u.c.sym->components;
567
  else
568
    comp = expr->ts.derived->components;
569
 
570
  for (; comp; comp = comp->next, cons = cons->next)
571
    {
572
      if (! cons->expr)
573
        {
574
          t = FAILURE;
575
          continue;
576
        }
577
 
578
      if (gfc_resolve_expr (cons->expr) == FAILURE)
579
        {
580
          t = FAILURE;
581
          continue;
582
        }
583
 
584
      /* If we don't have the right type, try to convert it.  */
585
 
586
      if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
587
        {
588
          t = FAILURE;
589
          if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
590
            gfc_error ("The element in the derived type constructor at %L, "
591
                       "for pointer component '%s', is %s but should be %s",
592
                       &cons->expr->where, comp->name,
593
                       gfc_basic_typename (cons->expr->ts.type),
594
                       gfc_basic_typename (comp->ts.type));
595
          else
596
            t = gfc_convert_type (cons->expr, &comp->ts, 1);
597
        }
598
    }
599
 
600
  return t;
601
}
602
 
603
 
604
 
605
/****************** Expression name resolution ******************/
606
 
607
/* Returns 0 if a symbol was not declared with a type or
608
   attribute declaration statement, nonzero otherwise.  */
609
 
610
static int
611
was_declared (gfc_symbol * sym)
612
{
613
  symbol_attribute a;
614
 
615
  a = sym->attr;
616
 
617
  if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
618
    return 1;
619
 
620
  if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
621
      || a.optional || a.pointer || a.save || a.target
622
      || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
623
    return 1;
624
 
625
  return 0;
626
}
627
 
628
 
629
/* Determine if a symbol is generic or not.  */
630
 
631
static int
632
generic_sym (gfc_symbol * sym)
633
{
634
  gfc_symbol *s;
635
 
636
  if (sym->attr.generic ||
637
      (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
638
    return 1;
639
 
640
  if (was_declared (sym) || sym->ns->parent == NULL)
641
    return 0;
642
 
643
  gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
644
 
645
  return (s == NULL) ? 0 : generic_sym (s);
646
}
647
 
648
 
649
/* Determine if a symbol is specific or not.  */
650
 
651
static int
652
specific_sym (gfc_symbol * sym)
653
{
654
  gfc_symbol *s;
655
 
656
  if (sym->attr.if_source == IFSRC_IFBODY
657
      || sym->attr.proc == PROC_MODULE
658
      || sym->attr.proc == PROC_INTERNAL
659
      || sym->attr.proc == PROC_ST_FUNCTION
660
      || (sym->attr.intrinsic &&
661
          gfc_specific_intrinsic (sym->name))
662
      || sym->attr.external)
663
    return 1;
664
 
665
  if (was_declared (sym) || sym->ns->parent == NULL)
666
    return 0;
667
 
668
  gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
669
 
670
  return (s == NULL) ? 0 : specific_sym (s);
671
}
672
 
673
 
674
/* Figure out if the procedure is specific, generic or unknown.  */
675
 
676
typedef enum
677
{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
678
proc_type;
679
 
680
static proc_type
681
procedure_kind (gfc_symbol * sym)
682
{
683
 
684
  if (generic_sym (sym))
685
    return PTYPE_GENERIC;
686
 
687
  if (specific_sym (sym))
688
    return PTYPE_SPECIFIC;
689
 
690
  return PTYPE_UNKNOWN;
691
}
692
 
693
/* Check references to assumed size arrays.  The flag need_full_assumed_size
694
   is non-zero when matching actual arguments.  */
695
 
696
static int need_full_assumed_size = 0;
697
 
698
static bool
699
check_assumed_size_reference (gfc_symbol * sym, gfc_expr * e)
700
{
701
  gfc_ref * ref;
702
  int dim;
703
  int last = 1;
704
 
705
  if (need_full_assumed_size
706
        || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
707
      return false;
708
 
709
  for (ref = e->ref; ref; ref = ref->next)
710
    if (ref->type == REF_ARRAY)
711
      for (dim = 0; dim < ref->u.ar.as->rank; dim++)
712
        last = (ref->u.ar.end[dim] == NULL) && (ref->u.ar.type == DIMEN_ELEMENT);
713
 
714
  if (last)
715
    {
716
      gfc_error ("The upper bound in the last dimension must "
717
                 "appear in the reference to the assumed size "
718
                 "array '%s' at %L.", sym->name, &e->where);
719
      return true;
720
    }
721
  return false;
722
}
723
 
724
 
725
/* Look for bad assumed size array references in argument expressions
726
  of elemental and array valued intrinsic procedures.  Since this is
727
  called from procedure resolution functions, it only recurses at
728
  operators.  */
729
 
730
static bool
731
resolve_assumed_size_actual (gfc_expr *e)
732
{
733
  if (e == NULL)
734
   return false;
735
 
736
  switch (e->expr_type)
737
    {
738
    case EXPR_VARIABLE:
739
      if (e->symtree
740
            && check_assumed_size_reference (e->symtree->n.sym, e))
741
        return true;
742
      break;
743
 
744
    case EXPR_OP:
745
      if (resolve_assumed_size_actual (e->value.op.op1)
746
            || resolve_assumed_size_actual (e->value.op.op2))
747
        return true;
748
      break;
749
 
750
    default:
751
      break;
752
    }
753
  return false;
754
}
755
 
756
 
757
/* Resolve an actual argument list.  Most of the time, this is just
758
   resolving the expressions in the list.
759
   The exception is that we sometimes have to decide whether arguments
760
   that look like procedure arguments are really simple variable
761
   references.  */
762
 
763
static try
764
resolve_actual_arglist (gfc_actual_arglist * arg)
765
{
766
  gfc_symbol *sym;
767
  gfc_symtree *parent_st;
768
  gfc_expr *e;
769
 
770
  for (; arg; arg = arg->next)
771
    {
772
 
773
      e = arg->expr;
774
      if (e == NULL)
775
        {
776
          /* Check the label is a valid branching target.  */
777
          if (arg->label)
778
            {
779
              if (arg->label->defined == ST_LABEL_UNKNOWN)
780
                {
781
                  gfc_error ("Label %d referenced at %L is never defined",
782
                             arg->label->value, &arg->label->where);
783
                  return FAILURE;
784
                }
785
            }
786
          continue;
787
        }
788
 
789
      if (e->ts.type != BT_PROCEDURE)
790
        {
791
          if (gfc_resolve_expr (e) != SUCCESS)
792
            return FAILURE;
793
          continue;
794
        }
795
 
796
      /* See if the expression node should really be a variable
797
         reference.  */
798
 
799
      sym = e->symtree->n.sym;
800
 
801
      if (sym->attr.flavor == FL_PROCEDURE
802
          || sym->attr.intrinsic
803
          || sym->attr.external)
804
        {
805
 
806
          if (sym->attr.proc == PROC_ST_FUNCTION)
807
            {
808
              gfc_error ("Statement function '%s' at %L is not allowed as an "
809
                         "actual argument", sym->name, &e->where);
810
            }
811
 
812
          if (sym->attr.contained && !sym->attr.use_assoc
813
              && sym->ns->proc_name->attr.flavor != FL_MODULE)
814
            {
815
              gfc_error ("Internal procedure '%s' is not allowed as an "
816
                         "actual argument at %L", sym->name, &e->where);
817
            }
818
 
819
          if (sym->attr.elemental && !sym->attr.intrinsic)
820
            {
821
              gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
822
                         "allowed as an actual argument at %L", sym->name,
823
                         &e->where);
824
            }
825
 
826
          /* If the symbol is the function that names the current (or
827
             parent) scope, then we really have a variable reference.  */
828
 
829
          if (sym->attr.function && sym->result == sym
830
              && (sym->ns->proc_name == sym
831
                  || (sym->ns->parent != NULL
832
                      && sym->ns->parent->proc_name == sym)))
833
            goto got_variable;
834
 
835
          continue;
836
        }
837
 
838
      /* See if the name is a module procedure in a parent unit.  */
839
 
840
      if (was_declared (sym) || sym->ns->parent == NULL)
841
        goto got_variable;
842
 
843
      if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
844
        {
845
          gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
846
          return FAILURE;
847
        }
848
 
849
      if (parent_st == NULL)
850
        goto got_variable;
851
 
852
      sym = parent_st->n.sym;
853
      e->symtree = parent_st;           /* Point to the right thing.  */
854
 
855
      if (sym->attr.flavor == FL_PROCEDURE
856
          || sym->attr.intrinsic
857
          || sym->attr.external)
858
        {
859
          continue;
860
        }
861
 
862
    got_variable:
863
      e->expr_type = EXPR_VARIABLE;
864
      e->ts = sym->ts;
865
      if (sym->as != NULL)
866
        {
867
          e->rank = sym->as->rank;
868
          e->ref = gfc_get_ref ();
869
          e->ref->type = REF_ARRAY;
870
          e->ref->u.ar.type = AR_FULL;
871
          e->ref->u.ar.as = sym->as;
872
        }
873
    }
874
 
875
  return SUCCESS;
876
}
877
 
878
/* This function does the checking of references to global procedures
879
   as defined in sections 18.1 and 14.1, respectively, of the Fortran
880
   77 and 95 standards.  It checks for a gsymbol for the name, making
881
   one if it does not already exist.  If it already exists, then the
882
   reference being resolved must correspond to the type of gsymbol.
883
   Otherwise, the new symbol is equipped with the attributes of the
884
   reference.  The corresponding code that is called in creating
885
   global entities is parse.c.  */
886
 
887
static void
888
resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
889
{
890
  gfc_gsymbol * gsym;
891
  uint type;
892
 
893
  type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
894
 
895
  gsym = gfc_get_gsymbol (sym->name);
896
 
897
  if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
898
    global_used (gsym, where);
899
 
900
  if (gsym->type == GSYM_UNKNOWN)
901
    {
902
      gsym->type = type;
903
      gsym->where = *where;
904
    }
905
 
906
  gsym->used = 1;
907
}
908
 
909
/************* Function resolution *************/
910
 
911
/* Resolve a function call known to be generic.
912
   Section 14.1.2.4.1.  */
913
 
914
static match
915
resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
916
{
917
  gfc_symbol *s;
918
 
919
  if (sym->attr.generic)
920
    {
921
      s =
922
        gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
923
      if (s != NULL)
924
        {
925
          expr->value.function.name = s->name;
926
          expr->value.function.esym = s;
927
 
928
          if (s->ts.type != BT_UNKNOWN)
929
            expr->ts = s->ts;
930
          else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
931
            expr->ts = s->result->ts;
932
 
933
          if (s->as != NULL)
934
            expr->rank = s->as->rank;
935
          else if (s->result != NULL && s->result->as != NULL)
936
            expr->rank = s->result->as->rank;
937
 
938
          return MATCH_YES;
939
        }
940
 
941
      /* TODO: Need to search for elemental references in generic interface */
942
    }
943
 
944
  if (sym->attr.intrinsic)
945
    return gfc_intrinsic_func_interface (expr, 0);
946
 
947
  return MATCH_NO;
948
}
949
 
950
 
951
static try
952
resolve_generic_f (gfc_expr * expr)
953
{
954
  gfc_symbol *sym;
955
  match m;
956
 
957
  sym = expr->symtree->n.sym;
958
 
959
  for (;;)
960
    {
961
      m = resolve_generic_f0 (expr, sym);
962
      if (m == MATCH_YES)
963
        return SUCCESS;
964
      else if (m == MATCH_ERROR)
965
        return FAILURE;
966
 
967
generic:
968
      if (sym->ns->parent == NULL)
969
        break;
970
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
971
 
972
      if (sym == NULL)
973
        break;
974
      if (!generic_sym (sym))
975
        goto generic;
976
    }
977
 
978
  /* Last ditch attempt.  */
979
 
980
  if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
981
    {
982
      gfc_error ("Generic function '%s' at %L is not an intrinsic function",
983
                 expr->symtree->n.sym->name, &expr->where);
984
      return FAILURE;
985
    }
986
 
987
  m = gfc_intrinsic_func_interface (expr, 0);
988
  if (m == MATCH_YES)
989
    return SUCCESS;
990
  if (m == MATCH_NO)
991
    gfc_error
992
      ("Generic function '%s' at %L is not consistent with a specific "
993
       "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
994
 
995
  return FAILURE;
996
}
997
 
998
 
999
/* Resolve a function call known to be specific.  */
1000
 
1001
static match
1002
resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
1003
{
1004
  match m;
1005
 
1006
  if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1007
    {
1008
      if (sym->attr.dummy)
1009
        {
1010
          sym->attr.proc = PROC_DUMMY;
1011
          goto found;
1012
        }
1013
 
1014
      sym->attr.proc = PROC_EXTERNAL;
1015
      goto found;
1016
    }
1017
 
1018
  if (sym->attr.proc == PROC_MODULE
1019
      || sym->attr.proc == PROC_ST_FUNCTION
1020
      || sym->attr.proc == PROC_INTERNAL)
1021
    goto found;
1022
 
1023
  if (sym->attr.intrinsic)
1024
    {
1025
      m = gfc_intrinsic_func_interface (expr, 1);
1026
      if (m == MATCH_YES)
1027
        return MATCH_YES;
1028
      if (m == MATCH_NO)
1029
        gfc_error
1030
          ("Function '%s' at %L is INTRINSIC but is not compatible with "
1031
           "an intrinsic", sym->name, &expr->where);
1032
 
1033
      return MATCH_ERROR;
1034
    }
1035
 
1036
  return MATCH_NO;
1037
 
1038
found:
1039
  gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1040
 
1041
  expr->ts = sym->ts;
1042
  expr->value.function.name = sym->name;
1043
  expr->value.function.esym = sym;
1044
  if (sym->as != NULL)
1045
    expr->rank = sym->as->rank;
1046
 
1047
  return MATCH_YES;
1048
}
1049
 
1050
 
1051
static try
1052
resolve_specific_f (gfc_expr * expr)
1053
{
1054
  gfc_symbol *sym;
1055
  match m;
1056
 
1057
  sym = expr->symtree->n.sym;
1058
 
1059
  for (;;)
1060
    {
1061
      m = resolve_specific_f0 (sym, expr);
1062
      if (m == MATCH_YES)
1063
        return SUCCESS;
1064
      if (m == MATCH_ERROR)
1065
        return FAILURE;
1066
 
1067
      if (sym->ns->parent == NULL)
1068
        break;
1069
 
1070
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1071
 
1072
      if (sym == NULL)
1073
        break;
1074
    }
1075
 
1076
  gfc_error ("Unable to resolve the specific function '%s' at %L",
1077
             expr->symtree->n.sym->name, &expr->where);
1078
 
1079
  return SUCCESS;
1080
}
1081
 
1082
 
1083
/* Resolve a procedure call not known to be generic nor specific.  */
1084
 
1085
static try
1086
resolve_unknown_f (gfc_expr * expr)
1087
{
1088
  gfc_symbol *sym;
1089
  gfc_typespec *ts;
1090
 
1091
  sym = expr->symtree->n.sym;
1092
 
1093
  if (sym->attr.dummy)
1094
    {
1095
      sym->attr.proc = PROC_DUMMY;
1096
      expr->value.function.name = sym->name;
1097
      goto set_type;
1098
    }
1099
 
1100
  /* See if we have an intrinsic function reference.  */
1101
 
1102
  if (gfc_intrinsic_name (sym->name, 0))
1103
    {
1104
      if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1105
        return SUCCESS;
1106
      return FAILURE;
1107
    }
1108
 
1109
  /* The reference is to an external name.  */
1110
 
1111
  sym->attr.proc = PROC_EXTERNAL;
1112
  expr->value.function.name = sym->name;
1113
  expr->value.function.esym = expr->symtree->n.sym;
1114
 
1115
  if (sym->as != NULL)
1116
    expr->rank = sym->as->rank;
1117
 
1118
  /* Type of the expression is either the type of the symbol or the
1119
     default type of the symbol.  */
1120
 
1121
set_type:
1122
  gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1123
 
1124
  if (sym->ts.type != BT_UNKNOWN)
1125
    expr->ts = sym->ts;
1126
  else
1127
    {
1128
      ts = gfc_get_default_type (sym, sym->ns);
1129
 
1130
      if (ts->type == BT_UNKNOWN)
1131
        {
1132
          gfc_error ("Function '%s' at %L has no IMPLICIT type",
1133
                     sym->name, &expr->where);
1134
          return FAILURE;
1135
        }
1136
      else
1137
        expr->ts = *ts;
1138
    }
1139
 
1140
  return SUCCESS;
1141
}
1142
 
1143
 
1144
/* Figure out if a function reference is pure or not.  Also set the name
1145
   of the function for a potential error message.  Return nonzero if the
1146
   function is PURE, zero if not.  */
1147
 
1148
static int
1149
pure_function (gfc_expr * e, const char **name)
1150
{
1151
  int pure;
1152
 
1153
  if (e->value.function.esym)
1154
    {
1155
      pure = gfc_pure (e->value.function.esym);
1156
      *name = e->value.function.esym->name;
1157
    }
1158
  else if (e->value.function.isym)
1159
    {
1160
      pure = e->value.function.isym->pure
1161
        || e->value.function.isym->elemental;
1162
      *name = e->value.function.isym->name;
1163
    }
1164
  else
1165
    {
1166
      /* Implicit functions are not pure.  */
1167
      pure = 0;
1168
      *name = e->value.function.name;
1169
    }
1170
 
1171
  return pure;
1172
}
1173
 
1174
 
1175
/* Resolve a function call, which means resolving the arguments, then figuring
1176
   out which entity the name refers to.  */
1177
/* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1178
   to INTENT(OUT) or INTENT(INOUT).  */
1179
 
1180
static try
1181
resolve_function (gfc_expr * expr)
1182
{
1183
  gfc_actual_arglist *arg;
1184
  gfc_symbol * sym;
1185
  const char *name;
1186
  try t;
1187
  int temp;
1188
  int i;
1189
 
1190
  sym = NULL;
1191
  if (expr->symtree)
1192
    sym = expr->symtree->n.sym;
1193
 
1194
  /* If the procedure is not internal, a statement function or a module
1195
     procedure,it must be external and should be checked for usage.  */
1196
  if (sym && !sym->attr.dummy && !sym->attr.contained
1197
        && sym->attr.proc != PROC_ST_FUNCTION
1198
        && !sym->attr.use_assoc)
1199
    resolve_global_procedure (sym, &expr->where, 0);
1200
 
1201
  /* Switch off assumed size checking and do this again for certain kinds
1202
     of procedure, once the procedure itself is resolved.  */
1203
  need_full_assumed_size++;
1204
 
1205
  if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1206
    return FAILURE;
1207
 
1208
  /* Resume assumed_size checking. */
1209
  need_full_assumed_size--;
1210
 
1211
  if (sym && sym->ts.type == BT_CHARACTER
1212
        && sym->ts.cl
1213
        && sym->ts.cl->length == NULL
1214
        && !sym->attr.dummy
1215
        && !sym->attr.contained)
1216
    {
1217
      /* Internal procedures are taken care of in resolve_contained_fntype.  */
1218
      gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1219
                 "be used at %L since it is not a dummy argument",
1220
                 sym->name, &expr->where);
1221
      return FAILURE;
1222
    }
1223
 
1224
/* See if function is already resolved.  */
1225
 
1226
  if (expr->value.function.name != NULL)
1227
    {
1228
      if (expr->ts.type == BT_UNKNOWN)
1229
        expr->ts = sym->ts;
1230
      t = SUCCESS;
1231
    }
1232
  else
1233
    {
1234
      /* Apply the rules of section 14.1.2.  */
1235
 
1236
      switch (procedure_kind (sym))
1237
        {
1238
        case PTYPE_GENERIC:
1239
          t = resolve_generic_f (expr);
1240
          break;
1241
 
1242
        case PTYPE_SPECIFIC:
1243
          t = resolve_specific_f (expr);
1244
          break;
1245
 
1246
        case PTYPE_UNKNOWN:
1247
          t = resolve_unknown_f (expr);
1248
          break;
1249
 
1250
        default:
1251
          gfc_internal_error ("resolve_function(): bad function type");
1252
        }
1253
    }
1254
 
1255
  /* If the expression is still a function (it might have simplified),
1256
     then we check to see if we are calling an elemental function.  */
1257
 
1258
  if (expr->expr_type != EXPR_FUNCTION)
1259
    return t;
1260
 
1261
  temp = need_full_assumed_size;
1262
  need_full_assumed_size = 0;
1263
 
1264
  if (expr->value.function.actual != NULL
1265
      && ((expr->value.function.esym != NULL
1266
           && expr->value.function.esym->attr.elemental)
1267
          || (expr->value.function.isym != NULL
1268
              && expr->value.function.isym->elemental)))
1269
    {
1270
 
1271
      /* The rank of an elemental is the rank of its array argument(s).  */
1272
      for (arg = expr->value.function.actual; arg; arg = arg->next)
1273
        {
1274
          if (arg->expr != NULL && arg->expr->rank > 0)
1275
            {
1276
              expr->rank = arg->expr->rank;
1277
              if (!expr->shape && arg->expr->shape)
1278
                {
1279
                  expr->shape = gfc_get_shape (expr->rank);
1280
                  for (i = 0; i < expr->rank; i++)
1281
                    mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1282
                }
1283
              break;
1284
            }
1285
        }
1286
 
1287
      /* Being elemental, the last upper bound of an assumed size array
1288
         argument must be present.  */
1289
      for (arg = expr->value.function.actual; arg; arg = arg->next)
1290
        {
1291
          if (arg->expr != NULL
1292
                && arg->expr->rank > 0
1293
                && resolve_assumed_size_actual (arg->expr))
1294
            return FAILURE;
1295
        }
1296
    }
1297
 
1298
  else if (expr->value.function.actual != NULL
1299
             && expr->value.function.isym != NULL
1300
             && expr->value.function.isym->generic_id != GFC_ISYM_LBOUND
1301
             && expr->value.function.isym->generic_id != GFC_ISYM_LOC
1302
             && expr->value.function.isym->generic_id != GFC_ISYM_PRESENT)
1303
    {
1304
      /* Array instrinsics must also have the last upper bound of an
1305
         asumed size array argument.  UBOUND and SIZE have to be
1306
         excluded from the check if the second argument is anything
1307
         than a constant.  */
1308
      int inquiry;
1309
      inquiry = expr->value.function.isym->generic_id == GFC_ISYM_UBOUND
1310
                  || expr->value.function.isym->generic_id == GFC_ISYM_SIZE;
1311
 
1312
      for (arg = expr->value.function.actual; arg; arg = arg->next)
1313
        {
1314
          if (inquiry && arg->next != NULL && arg->next->expr
1315
                && arg->next->expr->expr_type != EXPR_CONSTANT)
1316
            break;
1317
 
1318
          if (arg->expr != NULL
1319
                && arg->expr->rank > 0
1320
                && resolve_assumed_size_actual (arg->expr))
1321
            return FAILURE;
1322
        }
1323
    }
1324
 
1325
  need_full_assumed_size = temp;
1326
 
1327
  if (!pure_function (expr, &name) && name)
1328
    {
1329
      if (forall_flag)
1330
        {
1331
          gfc_error
1332
            ("Function reference to '%s' at %L is inside a FORALL block",
1333
             name, &expr->where);
1334
          t = FAILURE;
1335
        }
1336
      else if (gfc_pure (NULL))
1337
        {
1338
          gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1339
                     "procedure within a PURE procedure", name, &expr->where);
1340
          t = FAILURE;
1341
        }
1342
    }
1343
 
1344
  /* Character lengths of use associated functions may contains references to
1345
     symbols not referenced from the current program unit otherwise.  Make sure
1346
     those symbols are marked as referenced.  */
1347
 
1348
  if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
1349
      && expr->value.function.esym->attr.use_assoc)
1350
    {
1351
      gfc_expr_set_symbols_referenced (expr->ts.cl->length);
1352
    }
1353
 
1354
  return t;
1355
}
1356
 
1357
 
1358
/************* Subroutine resolution *************/
1359
 
1360
static void
1361
pure_subroutine (gfc_code * c, gfc_symbol * sym)
1362
{
1363
 
1364
  if (gfc_pure (sym))
1365
    return;
1366
 
1367
  if (forall_flag)
1368
    gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1369
               sym->name, &c->loc);
1370
  else if (gfc_pure (NULL))
1371
    gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1372
               &c->loc);
1373
}
1374
 
1375
 
1376
static match
1377
resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1378
{
1379
  gfc_symbol *s;
1380
 
1381
  if (sym->attr.generic)
1382
    {
1383
      s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1384
      if (s != NULL)
1385
        {
1386
          c->resolved_sym = s;
1387
          pure_subroutine (c, s);
1388
          return MATCH_YES;
1389
        }
1390
 
1391
      /* TODO: Need to search for elemental references in generic interface.  */
1392
    }
1393
 
1394
  if (sym->attr.intrinsic)
1395
    return gfc_intrinsic_sub_interface (c, 0);
1396
 
1397
  return MATCH_NO;
1398
}
1399
 
1400
 
1401
static try
1402
resolve_generic_s (gfc_code * c)
1403
{
1404
  gfc_symbol *sym;
1405
  match m;
1406
 
1407
  sym = c->symtree->n.sym;
1408
 
1409
  m = resolve_generic_s0 (c, sym);
1410
  if (m == MATCH_YES)
1411
    return SUCCESS;
1412
  if (m == MATCH_ERROR)
1413
    return FAILURE;
1414
 
1415
  if (sym->ns->parent != NULL)
1416
    {
1417
      gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1418
      if (sym != NULL)
1419
        {
1420
          m = resolve_generic_s0 (c, sym);
1421
          if (m == MATCH_YES)
1422
            return SUCCESS;
1423
          if (m == MATCH_ERROR)
1424
            return FAILURE;
1425
        }
1426
    }
1427
 
1428
  /* Last ditch attempt.  */
1429
 
1430
  if (!gfc_generic_intrinsic (sym->name))
1431
    {
1432
      gfc_error
1433
        ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
1434
         sym->name, &c->loc);
1435
      return FAILURE;
1436
    }
1437
 
1438
  m = gfc_intrinsic_sub_interface (c, 0);
1439
  if (m == MATCH_YES)
1440
    return SUCCESS;
1441
  if (m == MATCH_NO)
1442
    gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1443
               "intrinsic subroutine interface", sym->name, &c->loc);
1444
 
1445
  return FAILURE;
1446
}
1447
 
1448
 
1449
/* Resolve a subroutine call known to be specific.  */
1450
 
1451
static match
1452
resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1453
{
1454
  match m;
1455
 
1456
  if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1457
    {
1458
      if (sym->attr.dummy)
1459
        {
1460
          sym->attr.proc = PROC_DUMMY;
1461
          goto found;
1462
        }
1463
 
1464
      sym->attr.proc = PROC_EXTERNAL;
1465
      goto found;
1466
    }
1467
 
1468
  if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1469
    goto found;
1470
 
1471
  if (sym->attr.intrinsic)
1472
    {
1473
      m = gfc_intrinsic_sub_interface (c, 1);
1474
      if (m == MATCH_YES)
1475
        return MATCH_YES;
1476
      if (m == MATCH_NO)
1477
        gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1478
                   "with an intrinsic", sym->name, &c->loc);
1479
 
1480
      return MATCH_ERROR;
1481
    }
1482
 
1483
  return MATCH_NO;
1484
 
1485
found:
1486
  gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1487
 
1488
  c->resolved_sym = sym;
1489
  pure_subroutine (c, sym);
1490
 
1491
  return MATCH_YES;
1492
}
1493
 
1494
 
1495
static try
1496
resolve_specific_s (gfc_code * c)
1497
{
1498
  gfc_symbol *sym;
1499
  match m;
1500
 
1501
  sym = c->symtree->n.sym;
1502
 
1503
  m = resolve_specific_s0 (c, sym);
1504
  if (m == MATCH_YES)
1505
    return SUCCESS;
1506
  if (m == MATCH_ERROR)
1507
    return FAILURE;
1508
 
1509
  gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1510
 
1511
  if (sym != NULL)
1512
    {
1513
      m = resolve_specific_s0 (c, sym);
1514
      if (m == MATCH_YES)
1515
        return SUCCESS;
1516
      if (m == MATCH_ERROR)
1517
        return FAILURE;
1518
    }
1519
 
1520
  gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1521
             sym->name, &c->loc);
1522
 
1523
  return FAILURE;
1524
}
1525
 
1526
 
1527
/* Resolve a subroutine call not known to be generic nor specific.  */
1528
 
1529
static try
1530
resolve_unknown_s (gfc_code * c)
1531
{
1532
  gfc_symbol *sym;
1533
 
1534
  sym = c->symtree->n.sym;
1535
 
1536
  if (sym->attr.dummy)
1537
    {
1538
      sym->attr.proc = PROC_DUMMY;
1539
      goto found;
1540
    }
1541
 
1542
  /* See if we have an intrinsic function reference.  */
1543
 
1544
  if (gfc_intrinsic_name (sym->name, 1))
1545
    {
1546
      if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1547
        return SUCCESS;
1548
      return FAILURE;
1549
    }
1550
 
1551
  /* The reference is to an external name.  */
1552
 
1553
found:
1554
  gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1555
 
1556
  c->resolved_sym = sym;
1557
 
1558
  pure_subroutine (c, sym);
1559
 
1560
  return SUCCESS;
1561
}
1562
 
1563
 
1564
/* Resolve a subroutine call.  Although it was tempting to use the same code
1565
   for functions, subroutines and functions are stored differently and this
1566
   makes things awkward.  */
1567
 
1568
static try
1569
resolve_call (gfc_code * c)
1570
{
1571
  try t;
1572
 
1573
  if (c->symtree && c->symtree->n.sym
1574
        && c->symtree->n.sym->ts.type != BT_UNKNOWN)
1575
    {
1576
      gfc_error ("'%s' at %L has a type, which is not consistent with "
1577
                 "the CALL at %L", c->symtree->n.sym->name,
1578
                 &c->symtree->n.sym->declared_at, &c->loc);
1579
      return FAILURE;
1580
    }
1581
 
1582
  /* If the procedure is not internal or module, it must be external and
1583
     should be checked for usage.  */
1584
  if (c->symtree && c->symtree->n.sym
1585
        && !c->symtree->n.sym->attr.dummy
1586
        && !c->symtree->n.sym->attr.contained
1587
        && !c->symtree->n.sym->attr.use_assoc)
1588
    resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
1589
 
1590
  /* Switch off assumed size checking and do this again for certain kinds
1591
     of procedure, once the procedure itself is resolved.  */
1592
  need_full_assumed_size++;
1593
 
1594
  if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1595
    return FAILURE;
1596
 
1597
  /* Resume assumed_size checking. */
1598
  need_full_assumed_size--;
1599
 
1600
  if (c->resolved_sym != NULL)
1601
    return SUCCESS;
1602
 
1603
  switch (procedure_kind (c->symtree->n.sym))
1604
    {
1605
    case PTYPE_GENERIC:
1606
      t = resolve_generic_s (c);
1607
      break;
1608
 
1609
    case PTYPE_SPECIFIC:
1610
      t = resolve_specific_s (c);
1611
      break;
1612
 
1613
    case PTYPE_UNKNOWN:
1614
      t = resolve_unknown_s (c);
1615
      break;
1616
 
1617
    default:
1618
      gfc_internal_error ("resolve_subroutine(): bad function type");
1619
    }
1620
 
1621
  /* Some checks of elemental subroutines.  */
1622
  if (c->ext.actual != NULL
1623
      && c->symtree->n.sym->attr.elemental)
1624
    {
1625
      gfc_actual_arglist * a;
1626
      gfc_expr * e;
1627
      e = NULL;
1628
 
1629
      for (a = c->ext.actual; a; a = a->next)
1630
        {
1631
          if (a->expr == NULL || a->expr->rank == 0)
1632
            continue;
1633
 
1634
         /* The last upper bound of an assumed size array argument must
1635
            be present.  */
1636
          if (resolve_assumed_size_actual (a->expr))
1637
            return FAILURE;
1638
 
1639
          /* Array actual arguments must conform.  */
1640
          if (e != NULL)
1641
            {
1642
              if (gfc_check_conformance ("elemental subroutine", a->expr, e)
1643
                        == FAILURE)
1644
                return FAILURE;
1645
            }
1646
          else
1647
            e = a->expr;
1648
        }
1649
    }
1650
 
1651
  return t;
1652
}
1653
 
1654
/* Compare the shapes of two arrays that have non-NULL shapes.  If both
1655
   op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1656
   match.  If both op1->shape and op2->shape are non-NULL return FAILURE
1657
   if their shapes do not match.  If either op1->shape or op2->shape is
1658
   NULL, return SUCCESS.  */
1659
 
1660
static try
1661
compare_shapes (gfc_expr * op1, gfc_expr * op2)
1662
{
1663
  try t;
1664
  int i;
1665
 
1666
  t = SUCCESS;
1667
 
1668
  if (op1->shape != NULL && op2->shape != NULL)
1669
    {
1670
      for (i = 0; i < op1->rank; i++)
1671
        {
1672
          if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1673
           {
1674
             gfc_error ("Shapes for operands at %L and %L are not conformable",
1675
                         &op1->where, &op2->where);
1676
             t = FAILURE;
1677
             break;
1678
           }
1679
        }
1680
    }
1681
 
1682
  return t;
1683
}
1684
 
1685
/* Resolve an operator expression node.  This can involve replacing the
1686
   operation with a user defined function call.  */
1687
 
1688
static try
1689
resolve_operator (gfc_expr * e)
1690
{
1691
  gfc_expr *op1, *op2;
1692
  char msg[200];
1693
  try t;
1694
 
1695
  /* Resolve all subnodes-- give them types.  */
1696
 
1697
  switch (e->value.op.operator)
1698
    {
1699
    default:
1700
      if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1701
        return FAILURE;
1702
 
1703
    /* Fall through...  */
1704
 
1705
    case INTRINSIC_NOT:
1706
    case INTRINSIC_UPLUS:
1707
    case INTRINSIC_UMINUS:
1708
    case INTRINSIC_PARENTHESES:
1709
      if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1710
        return FAILURE;
1711
      break;
1712
    }
1713
 
1714
  /* Typecheck the new node.  */
1715
 
1716
  op1 = e->value.op.op1;
1717
  op2 = e->value.op.op2;
1718
 
1719
  switch (e->value.op.operator)
1720
    {
1721
    case INTRINSIC_UPLUS:
1722
    case INTRINSIC_UMINUS:
1723
      if (op1->ts.type == BT_INTEGER
1724
          || op1->ts.type == BT_REAL
1725
          || op1->ts.type == BT_COMPLEX)
1726
        {
1727
          e->ts = op1->ts;
1728
          break;
1729
        }
1730
 
1731
      sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1732
               gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1733
      goto bad_op;
1734
 
1735
    case INTRINSIC_PLUS:
1736
    case INTRINSIC_MINUS:
1737
    case INTRINSIC_TIMES:
1738
    case INTRINSIC_DIVIDE:
1739
    case INTRINSIC_POWER:
1740
      if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1741
        {
1742
          gfc_type_convert_binary (e);
1743
          break;
1744
        }
1745
 
1746
      sprintf (msg,
1747
               _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1748
               gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1749
               gfc_typename (&op2->ts));
1750
      goto bad_op;
1751
 
1752
    case INTRINSIC_CONCAT:
1753
      if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1754
        {
1755
          e->ts.type = BT_CHARACTER;
1756
          e->ts.kind = op1->ts.kind;
1757
          break;
1758
        }
1759
 
1760
      sprintf (msg,
1761
               _("Operands of string concatenation operator at %%L are %s/%s"),
1762
               gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1763
      goto bad_op;
1764
 
1765
    case INTRINSIC_AND:
1766
    case INTRINSIC_OR:
1767
    case INTRINSIC_EQV:
1768
    case INTRINSIC_NEQV:
1769
      if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1770
        {
1771
          e->ts.type = BT_LOGICAL;
1772
          e->ts.kind = gfc_kind_max (op1, op2);
1773
          if (op1->ts.kind < e->ts.kind)
1774
            gfc_convert_type (op1, &e->ts, 2);
1775
          else if (op2->ts.kind < e->ts.kind)
1776
            gfc_convert_type (op2, &e->ts, 2);
1777
          break;
1778
        }
1779
 
1780
      sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
1781
               gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1782
               gfc_typename (&op2->ts));
1783
 
1784
      goto bad_op;
1785
 
1786
    case INTRINSIC_NOT:
1787
      if (op1->ts.type == BT_LOGICAL)
1788
        {
1789
          e->ts.type = BT_LOGICAL;
1790
          e->ts.kind = op1->ts.kind;
1791
          break;
1792
        }
1793
 
1794
      sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
1795
               gfc_typename (&op1->ts));
1796
      goto bad_op;
1797
 
1798
    case INTRINSIC_GT:
1799
    case INTRINSIC_GE:
1800
    case INTRINSIC_LT:
1801
    case INTRINSIC_LE:
1802
      if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1803
        {
1804
          strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
1805
          goto bad_op;
1806
        }
1807
 
1808
      /* Fall through...  */
1809
 
1810
    case INTRINSIC_EQ:
1811
    case INTRINSIC_NE:
1812
      if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1813
        {
1814
          e->ts.type = BT_LOGICAL;
1815
          e->ts.kind = gfc_default_logical_kind;
1816
          break;
1817
        }
1818
 
1819
      if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1820
        {
1821
          gfc_type_convert_binary (e);
1822
 
1823
          e->ts.type = BT_LOGICAL;
1824
          e->ts.kind = gfc_default_logical_kind;
1825
          break;
1826
        }
1827
 
1828
      if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1829
        sprintf (msg,
1830
                 _("Logicals at %%L must be compared with %s instead of %s"),
1831
                 e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
1832
                 gfc_op2string (e->value.op.operator));
1833
      else
1834
        sprintf (msg,
1835
                 _("Operands of comparison operator '%s' at %%L are %s/%s"),
1836
                 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1837
                 gfc_typename (&op2->ts));
1838
 
1839
      goto bad_op;
1840
 
1841
    case INTRINSIC_USER:
1842
      if (op2 == NULL)
1843
        sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
1844
                 e->value.op.uop->name, gfc_typename (&op1->ts));
1845
      else
1846
        sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
1847
                 e->value.op.uop->name, gfc_typename (&op1->ts),
1848
                 gfc_typename (&op2->ts));
1849
 
1850
      goto bad_op;
1851
 
1852
    case INTRINSIC_PARENTHESES:
1853
      break;
1854
 
1855
    default:
1856
      gfc_internal_error ("resolve_operator(): Bad intrinsic");
1857
    }
1858
 
1859
  /* Deal with arrayness of an operand through an operator.  */
1860
 
1861
  t = SUCCESS;
1862
 
1863
  switch (e->value.op.operator)
1864
    {
1865
    case INTRINSIC_PLUS:
1866
    case INTRINSIC_MINUS:
1867
    case INTRINSIC_TIMES:
1868
    case INTRINSIC_DIVIDE:
1869
    case INTRINSIC_POWER:
1870
    case INTRINSIC_CONCAT:
1871
    case INTRINSIC_AND:
1872
    case INTRINSIC_OR:
1873
    case INTRINSIC_EQV:
1874
    case INTRINSIC_NEQV:
1875
    case INTRINSIC_EQ:
1876
    case INTRINSIC_NE:
1877
    case INTRINSIC_GT:
1878
    case INTRINSIC_GE:
1879
    case INTRINSIC_LT:
1880
    case INTRINSIC_LE:
1881
 
1882
      if (op1->rank == 0 && op2->rank == 0)
1883
        e->rank = 0;
1884
 
1885
      if (op1->rank == 0 && op2->rank != 0)
1886
        {
1887
          e->rank = op2->rank;
1888
 
1889
          if (e->shape == NULL)
1890
            e->shape = gfc_copy_shape (op2->shape, op2->rank);
1891
        }
1892
 
1893
      if (op1->rank != 0 && op2->rank == 0)
1894
        {
1895
          e->rank = op1->rank;
1896
 
1897
          if (e->shape == NULL)
1898
            e->shape = gfc_copy_shape (op1->shape, op1->rank);
1899
        }
1900
 
1901
      if (op1->rank != 0 && op2->rank != 0)
1902
        {
1903
          if (op1->rank == op2->rank)
1904
            {
1905
              e->rank = op1->rank;
1906
              if (e->shape == NULL)
1907
                {
1908
                  t = compare_shapes(op1, op2);
1909
                  if (t == FAILURE)
1910
                    e->shape = NULL;
1911
                  else
1912
                e->shape = gfc_copy_shape (op1->shape, op1->rank);
1913
                }
1914
            }
1915
          else
1916
            {
1917
              gfc_error ("Inconsistent ranks for operator at %L and %L",
1918
                         &op1->where, &op2->where);
1919
              t = FAILURE;
1920
 
1921
              /* Allow higher level expressions to work.  */
1922
              e->rank = 0;
1923
            }
1924
        }
1925
 
1926
      break;
1927
 
1928
    case INTRINSIC_NOT:
1929
    case INTRINSIC_UPLUS:
1930
    case INTRINSIC_UMINUS:
1931
    case INTRINSIC_PARENTHESES:
1932
      e->rank = op1->rank;
1933
 
1934
      if (e->shape == NULL)
1935
        e->shape = gfc_copy_shape (op1->shape, op1->rank);
1936
 
1937
      /* Simply copy arrayness attribute */
1938
      break;
1939
 
1940
    default:
1941
      break;
1942
    }
1943
 
1944
  /* Attempt to simplify the expression.  */
1945
  if (t == SUCCESS)
1946
    t = gfc_simplify_expr (e, 0);
1947
  return t;
1948
 
1949
bad_op:
1950
 
1951
  if (gfc_extend_expr (e) == SUCCESS)
1952
    return SUCCESS;
1953
 
1954
  gfc_error (msg, &e->where);
1955
 
1956
  return FAILURE;
1957
}
1958
 
1959
 
1960
/************** Array resolution subroutines **************/
1961
 
1962
 
1963
typedef enum
1964
{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
1965
comparison;
1966
 
1967
/* Compare two integer expressions.  */
1968
 
1969
static comparison
1970
compare_bound (gfc_expr * a, gfc_expr * b)
1971
{
1972
  int i;
1973
 
1974
  if (a == NULL || a->expr_type != EXPR_CONSTANT
1975
      || b == NULL || b->expr_type != EXPR_CONSTANT)
1976
    return CMP_UNKNOWN;
1977
 
1978
  if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
1979
    gfc_internal_error ("compare_bound(): Bad expression");
1980
 
1981
  i = mpz_cmp (a->value.integer, b->value.integer);
1982
 
1983
  if (i < 0)
1984
    return CMP_LT;
1985
  if (i > 0)
1986
    return CMP_GT;
1987
  return CMP_EQ;
1988
}
1989
 
1990
 
1991
/* Compare an integer expression with an integer.  */
1992
 
1993
static comparison
1994
compare_bound_int (gfc_expr * a, int b)
1995
{
1996
  int i;
1997
 
1998
  if (a == NULL || a->expr_type != EXPR_CONSTANT)
1999
    return CMP_UNKNOWN;
2000
 
2001
  if (a->ts.type != BT_INTEGER)
2002
    gfc_internal_error ("compare_bound_int(): Bad expression");
2003
 
2004
  i = mpz_cmp_si (a->value.integer, b);
2005
 
2006
  if (i < 0)
2007
    return CMP_LT;
2008
  if (i > 0)
2009
    return CMP_GT;
2010
  return CMP_EQ;
2011
}
2012
 
2013
 
2014
/* Compare a single dimension of an array reference to the array
2015
   specification.  */
2016
 
2017
static try
2018
check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
2019
{
2020
 
2021
/* Given start, end and stride values, calculate the minimum and
2022
   maximum referenced indexes.  */
2023
 
2024
  switch (ar->type)
2025
    {
2026
    case AR_FULL:
2027
      break;
2028
 
2029
    case AR_ELEMENT:
2030
      if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2031
        goto bound;
2032
      if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2033
        goto bound;
2034
 
2035
      break;
2036
 
2037
    case AR_SECTION:
2038
      if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
2039
        {
2040
          gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
2041
          return FAILURE;
2042
        }
2043
 
2044
      if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2045
        goto bound;
2046
      if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2047
        goto bound;
2048
 
2049
      /* TODO: Possibly, we could warn about end[i] being out-of-bound although
2050
         it is legal (see 6.2.2.3.1).  */
2051
 
2052
      break;
2053
 
2054
    default:
2055
      gfc_internal_error ("check_dimension(): Bad array reference");
2056
    }
2057
 
2058
  return SUCCESS;
2059
 
2060
bound:
2061
  gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
2062
  return SUCCESS;
2063
}
2064
 
2065
 
2066
/* Compare an array reference with an array specification.  */
2067
 
2068
static try
2069
compare_spec_to_ref (gfc_array_ref * ar)
2070
{
2071
  gfc_array_spec *as;
2072
  int i;
2073
 
2074
  as = ar->as;
2075
  i = as->rank - 1;
2076
  /* TODO: Full array sections are only allowed as actual parameters.  */
2077
  if (as->type == AS_ASSUMED_SIZE
2078
      && (/*ar->type == AR_FULL
2079
          ||*/ (ar->type == AR_SECTION
2080
              && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
2081
    {
2082
      gfc_error ("Rightmost upper bound of assumed size array section"
2083
                 " not specified at %L", &ar->where);
2084
      return FAILURE;
2085
    }
2086
 
2087
  if (ar->type == AR_FULL)
2088
    return SUCCESS;
2089
 
2090
  if (as->rank != ar->dimen)
2091
    {
2092
      gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2093
                 &ar->where, ar->dimen, as->rank);
2094
      return FAILURE;
2095
    }
2096
 
2097
  for (i = 0; i < as->rank; i++)
2098
    if (check_dimension (i, ar, as) == FAILURE)
2099
      return FAILURE;
2100
 
2101
  return SUCCESS;
2102
}
2103
 
2104
 
2105
/* Resolve one part of an array index.  */
2106
 
2107
try
2108
gfc_resolve_index (gfc_expr * index, int check_scalar)
2109
{
2110
  gfc_typespec ts;
2111
 
2112
  if (index == NULL)
2113
    return SUCCESS;
2114
 
2115
  if (gfc_resolve_expr (index) == FAILURE)
2116
    return FAILURE;
2117
 
2118
  if (check_scalar && index->rank != 0)
2119
    {
2120
      gfc_error ("Array index at %L must be scalar", &index->where);
2121
      return FAILURE;
2122
    }
2123
 
2124
  if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
2125
    {
2126
      gfc_error ("Array index at %L must be of INTEGER type",
2127
                 &index->where);
2128
      return FAILURE;
2129
    }
2130
 
2131
  if (index->ts.type == BT_REAL)
2132
    if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
2133
                        &index->where) == FAILURE)
2134
      return FAILURE;
2135
 
2136
  if (index->ts.kind != gfc_index_integer_kind
2137
      || index->ts.type != BT_INTEGER)
2138
    {
2139
      gfc_clear_ts (&ts);
2140
      ts.type = BT_INTEGER;
2141
      ts.kind = gfc_index_integer_kind;
2142
 
2143
      gfc_convert_type_warn (index, &ts, 2, 0);
2144
    }
2145
 
2146
  return SUCCESS;
2147
}
2148
 
2149
/* Resolve a dim argument to an intrinsic function.  */
2150
 
2151
try
2152
gfc_resolve_dim_arg (gfc_expr *dim)
2153
{
2154
  if (dim == NULL)
2155
    return SUCCESS;
2156
 
2157
  if (gfc_resolve_expr (dim) == FAILURE)
2158
    return FAILURE;
2159
 
2160
  if (dim->rank != 0)
2161
    {
2162
      gfc_error ("Argument dim at %L must be scalar", &dim->where);
2163
      return FAILURE;
2164
 
2165
    }
2166
  if (dim->ts.type != BT_INTEGER)
2167
    {
2168
      gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
2169
      return FAILURE;
2170
    }
2171
  if (dim->ts.kind != gfc_index_integer_kind)
2172
    {
2173
      gfc_typespec ts;
2174
 
2175
      ts.type = BT_INTEGER;
2176
      ts.kind = gfc_index_integer_kind;
2177
 
2178
      gfc_convert_type_warn (dim, &ts, 2, 0);
2179
    }
2180
 
2181
  return SUCCESS;
2182
}
2183
 
2184
/* Given an expression that contains array references, update those array
2185
   references to point to the right array specifications.  While this is
2186
   filled in during matching, this information is difficult to save and load
2187
   in a module, so we take care of it here.
2188
 
2189
   The idea here is that the original array reference comes from the
2190
   base symbol.  We traverse the list of reference structures, setting
2191
   the stored reference to references.  Component references can
2192
   provide an additional array specification.  */
2193
 
2194
static void
2195
find_array_spec (gfc_expr * e)
2196
{
2197
  gfc_array_spec *as;
2198
  gfc_component *c;
2199
  gfc_ref *ref;
2200
 
2201
  as = e->symtree->n.sym->as;
2202
 
2203
  for (ref = e->ref; ref; ref = ref->next)
2204
    switch (ref->type)
2205
      {
2206
      case REF_ARRAY:
2207
        if (as == NULL)
2208
          gfc_internal_error ("find_array_spec(): Missing spec");
2209
 
2210
        ref->u.ar.as = as;
2211
        as = NULL;
2212
        break;
2213
 
2214
      case REF_COMPONENT:
2215
        for (c = e->symtree->n.sym->ts.derived->components; c; c = c->next)
2216
          if (c == ref->u.c.component)
2217
            break;
2218
 
2219
        if (c == NULL)
2220
          gfc_internal_error ("find_array_spec(): Component not found");
2221
 
2222
        if (c->dimension)
2223
          {
2224
            if (as != NULL)
2225
              gfc_internal_error ("find_array_spec(): unused as(1)");
2226
            as = c->as;
2227
          }
2228
 
2229
        break;
2230
 
2231
      case REF_SUBSTRING:
2232
        break;
2233
      }
2234
 
2235
  if (as != NULL)
2236
    gfc_internal_error ("find_array_spec(): unused as(2)");
2237
}
2238
 
2239
 
2240
/* Resolve an array reference.  */
2241
 
2242
static try
2243
resolve_array_ref (gfc_array_ref * ar)
2244
{
2245
  int i, check_scalar;
2246
 
2247
  for (i = 0; i < ar->dimen; i++)
2248
    {
2249
      check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2250
 
2251
      if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2252
        return FAILURE;
2253
      if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2254
        return FAILURE;
2255
      if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2256
        return FAILURE;
2257
 
2258
      if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2259
        switch (ar->start[i]->rank)
2260
          {
2261
          case 0:
2262
            ar->dimen_type[i] = DIMEN_ELEMENT;
2263
            break;
2264
 
2265
          case 1:
2266
            ar->dimen_type[i] = DIMEN_VECTOR;
2267
            break;
2268
 
2269
          default:
2270
            gfc_error ("Array index at %L is an array of rank %d",
2271
                       &ar->c_where[i], ar->start[i]->rank);
2272
            return FAILURE;
2273
          }
2274
    }
2275
 
2276
  /* If the reference type is unknown, figure out what kind it is.  */
2277
 
2278
  if (ar->type == AR_UNKNOWN)
2279
    {
2280
      ar->type = AR_ELEMENT;
2281
      for (i = 0; i < ar->dimen; i++)
2282
        if (ar->dimen_type[i] == DIMEN_RANGE
2283
            || ar->dimen_type[i] == DIMEN_VECTOR)
2284
          {
2285
            ar->type = AR_SECTION;
2286
            break;
2287
          }
2288
    }
2289
 
2290
  if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2291
    return FAILURE;
2292
 
2293
  return SUCCESS;
2294
}
2295
 
2296
 
2297
static try
2298
resolve_substring (gfc_ref * ref)
2299
{
2300
 
2301
  if (ref->u.ss.start != NULL)
2302
    {
2303
      if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2304
        return FAILURE;
2305
 
2306
      if (ref->u.ss.start->ts.type != BT_INTEGER)
2307
        {
2308
          gfc_error ("Substring start index at %L must be of type INTEGER",
2309
                     &ref->u.ss.start->where);
2310
          return FAILURE;
2311
        }
2312
 
2313
      if (ref->u.ss.start->rank != 0)
2314
        {
2315
          gfc_error ("Substring start index at %L must be scalar",
2316
                     &ref->u.ss.start->where);
2317
          return FAILURE;
2318
        }
2319
 
2320
      if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
2321
        {
2322
          gfc_error ("Substring start index at %L is less than one",
2323
                     &ref->u.ss.start->where);
2324
          return FAILURE;
2325
        }
2326
    }
2327
 
2328
  if (ref->u.ss.end != NULL)
2329
    {
2330
      if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2331
        return FAILURE;
2332
 
2333
      if (ref->u.ss.end->ts.type != BT_INTEGER)
2334
        {
2335
          gfc_error ("Substring end index at %L must be of type INTEGER",
2336
                     &ref->u.ss.end->where);
2337
          return FAILURE;
2338
        }
2339
 
2340
      if (ref->u.ss.end->rank != 0)
2341
        {
2342
          gfc_error ("Substring end index at %L must be scalar",
2343
                     &ref->u.ss.end->where);
2344
          return FAILURE;
2345
        }
2346
 
2347
      if (ref->u.ss.length != NULL
2348
          && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
2349
        {
2350
          gfc_error ("Substring end index at %L is out of bounds",
2351
                     &ref->u.ss.start->where);
2352
          return FAILURE;
2353
        }
2354
    }
2355
 
2356
  return SUCCESS;
2357
}
2358
 
2359
 
2360
/* Resolve subtype references.  */
2361
 
2362
static try
2363
resolve_ref (gfc_expr * expr)
2364
{
2365
  int current_part_dimension, n_components, seen_part_dimension;
2366
  gfc_ref *ref;
2367
 
2368
  for (ref = expr->ref; ref; ref = ref->next)
2369
    if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2370
      {
2371
        find_array_spec (expr);
2372
        break;
2373
      }
2374
 
2375
  for (ref = expr->ref; ref; ref = ref->next)
2376
    switch (ref->type)
2377
      {
2378
      case REF_ARRAY:
2379
        if (resolve_array_ref (&ref->u.ar) == FAILURE)
2380
          return FAILURE;
2381
        break;
2382
 
2383
      case REF_COMPONENT:
2384
        break;
2385
 
2386
      case REF_SUBSTRING:
2387
        resolve_substring (ref);
2388
        break;
2389
      }
2390
 
2391
  /* Check constraints on part references.  */
2392
 
2393
  current_part_dimension = 0;
2394
  seen_part_dimension = 0;
2395
  n_components = 0;
2396
 
2397
  for (ref = expr->ref; ref; ref = ref->next)
2398
    {
2399
      switch (ref->type)
2400
        {
2401
        case REF_ARRAY:
2402
          switch (ref->u.ar.type)
2403
            {
2404
            case AR_FULL:
2405
            case AR_SECTION:
2406
              current_part_dimension = 1;
2407
              break;
2408
 
2409
            case AR_ELEMENT:
2410
              current_part_dimension = 0;
2411
              break;
2412
 
2413
            case AR_UNKNOWN:
2414
              gfc_internal_error ("resolve_ref(): Bad array reference");
2415
            }
2416
 
2417
          break;
2418
 
2419
        case REF_COMPONENT:
2420
          if ((current_part_dimension || seen_part_dimension)
2421
              && ref->u.c.component->pointer)
2422
            {
2423
              gfc_error
2424
                ("Component to the right of a part reference with nonzero "
2425
                 "rank must not have the POINTER attribute at %L",
2426
                 &expr->where);
2427
              return FAILURE;
2428
            }
2429
 
2430
          n_components++;
2431
          break;
2432
 
2433
        case REF_SUBSTRING:
2434
          break;
2435
        }
2436
 
2437
      if (((ref->type == REF_COMPONENT && n_components > 1)
2438
           || ref->next == NULL)
2439
          && current_part_dimension
2440
          && seen_part_dimension)
2441
        {
2442
 
2443
          gfc_error ("Two or more part references with nonzero rank must "
2444
                     "not be specified at %L", &expr->where);
2445
          return FAILURE;
2446
        }
2447
 
2448
      if (ref->type == REF_COMPONENT)
2449
        {
2450
          if (current_part_dimension)
2451
            seen_part_dimension = 1;
2452
 
2453
          /* reset to make sure */
2454
          current_part_dimension = 0;
2455
        }
2456
    }
2457
 
2458
  return SUCCESS;
2459
}
2460
 
2461
 
2462
/* Given an expression, determine its shape.  This is easier than it sounds.
2463
   Leaves the shape array NULL if it is not possible to determine the shape.  */
2464
 
2465
static void
2466
expression_shape (gfc_expr * e)
2467
{
2468
  mpz_t array[GFC_MAX_DIMENSIONS];
2469
  int i;
2470
 
2471
  if (e->rank == 0 || e->shape != NULL)
2472
    return;
2473
 
2474
  for (i = 0; i < e->rank; i++)
2475
    if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2476
      goto fail;
2477
 
2478
  e->shape = gfc_get_shape (e->rank);
2479
 
2480
  memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2481
 
2482
  return;
2483
 
2484
fail:
2485
  for (i--; i >= 0; i--)
2486
    mpz_clear (array[i]);
2487
}
2488
 
2489
 
2490
/* Given a variable expression node, compute the rank of the expression by
2491
   examining the base symbol and any reference structures it may have.  */
2492
 
2493
static void
2494
expression_rank (gfc_expr * e)
2495
{
2496
  gfc_ref *ref;
2497
  int i, rank;
2498
 
2499
  if (e->ref == NULL)
2500
    {
2501
      if (e->expr_type == EXPR_ARRAY)
2502
        goto done;
2503
      /* Constructors can have a rank different from one via RESHAPE().  */
2504
 
2505
      if (e->symtree == NULL)
2506
        {
2507
          e->rank = 0;
2508
          goto done;
2509
        }
2510
 
2511
      e->rank = (e->symtree->n.sym->as == NULL)
2512
                  ? 0 : e->symtree->n.sym->as->rank;
2513
      goto done;
2514
    }
2515
 
2516
  rank = 0;
2517
 
2518
  for (ref = e->ref; ref; ref = ref->next)
2519
    {
2520
      if (ref->type != REF_ARRAY)
2521
        continue;
2522
 
2523
      if (ref->u.ar.type == AR_FULL)
2524
        {
2525
          rank = ref->u.ar.as->rank;
2526
          break;
2527
        }
2528
 
2529
      if (ref->u.ar.type == AR_SECTION)
2530
        {
2531
          /* Figure out the rank of the section.  */
2532
          if (rank != 0)
2533
            gfc_internal_error ("expression_rank(): Two array specs");
2534
 
2535
          for (i = 0; i < ref->u.ar.dimen; i++)
2536
            if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2537
                || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2538
              rank++;
2539
 
2540
          break;
2541
        }
2542
    }
2543
 
2544
  e->rank = rank;
2545
 
2546
done:
2547
  expression_shape (e);
2548
}
2549
 
2550
 
2551
/* Resolve a variable expression.  */
2552
 
2553
static try
2554
resolve_variable (gfc_expr * e)
2555
{
2556
  gfc_symbol *sym;
2557
 
2558
  if (e->ref && resolve_ref (e) == FAILURE)
2559
    return FAILURE;
2560
 
2561
  if (e->symtree == NULL)
2562
    return FAILURE;
2563
 
2564
  sym = e->symtree->n.sym;
2565
  if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2566
    {
2567
      e->ts.type = BT_PROCEDURE;
2568
      return SUCCESS;
2569
    }
2570
 
2571
  if (sym->ts.type != BT_UNKNOWN)
2572
    gfc_variable_attr (e, &e->ts);
2573
  else
2574
    {
2575
      /* Must be a simple variable reference.  */
2576
      if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
2577
        return FAILURE;
2578
      e->ts = sym->ts;
2579
    }
2580
 
2581
  if (check_assumed_size_reference (sym, e))
2582
    return FAILURE;
2583
 
2584
  return SUCCESS;
2585
}
2586
 
2587
 
2588
/* Resolve an expression.  That is, make sure that types of operands agree
2589
   with their operators, intrinsic operators are converted to function calls
2590
   for overloaded types and unresolved function references are resolved.  */
2591
 
2592
try
2593
gfc_resolve_expr (gfc_expr * e)
2594
{
2595
  try t;
2596
 
2597
  if (e == NULL)
2598
    return SUCCESS;
2599
 
2600
  switch (e->expr_type)
2601
    {
2602
    case EXPR_OP:
2603
      t = resolve_operator (e);
2604
      break;
2605
 
2606
    case EXPR_FUNCTION:
2607
      t = resolve_function (e);
2608
      break;
2609
 
2610
    case EXPR_VARIABLE:
2611
      t = resolve_variable (e);
2612
      if (t == SUCCESS)
2613
        expression_rank (e);
2614
      break;
2615
 
2616
    case EXPR_SUBSTRING:
2617
      t = resolve_ref (e);
2618
      break;
2619
 
2620
    case EXPR_CONSTANT:
2621
    case EXPR_NULL:
2622
      t = SUCCESS;
2623
      break;
2624
 
2625
    case EXPR_ARRAY:
2626
      t = FAILURE;
2627
      if (resolve_ref (e) == FAILURE)
2628
        break;
2629
 
2630
      t = gfc_resolve_array_constructor (e);
2631
      /* Also try to expand a constructor.  */
2632
      if (t == SUCCESS)
2633
        {
2634
          expression_rank (e);
2635
          gfc_expand_constructor (e);
2636
        }
2637
 
2638
      break;
2639
 
2640
    case EXPR_STRUCTURE:
2641
      t = resolve_ref (e);
2642
      if (t == FAILURE)
2643
        break;
2644
 
2645
      t = resolve_structure_cons (e);
2646
      if (t == FAILURE)
2647
        break;
2648
 
2649
      t = gfc_simplify_expr (e, 0);
2650
      break;
2651
 
2652
    default:
2653
      gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2654
    }
2655
 
2656
  return t;
2657
}
2658
 
2659
 
2660
/* Resolve an expression from an iterator.  They must be scalar and have
2661
   INTEGER or (optionally) REAL type.  */
2662
 
2663
static try
2664
gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
2665
                           const char * name_msgid)
2666
{
2667
  if (gfc_resolve_expr (expr) == FAILURE)
2668
    return FAILURE;
2669
 
2670
  if (expr->rank != 0)
2671
    {
2672
      gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
2673
      return FAILURE;
2674
    }
2675
 
2676
  if (!(expr->ts.type == BT_INTEGER
2677
        || (expr->ts.type == BT_REAL && real_ok)))
2678
    {
2679
      if (real_ok)
2680
        gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
2681
                   &expr->where);
2682
      else
2683
        gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
2684
      return FAILURE;
2685
    }
2686
  return SUCCESS;
2687
}
2688
 
2689
 
2690
/* Resolve the expressions in an iterator structure.  If REAL_OK is
2691
   false allow only INTEGER type iterators, otherwise allow REAL types.  */
2692
 
2693
try
2694
gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
2695
{
2696
 
2697
  if (iter->var->ts.type == BT_REAL)
2698
    gfc_notify_std (GFC_STD_F95_DEL,
2699
                    "Obsolete: REAL DO loop iterator at %L",
2700
                    &iter->var->where);
2701
 
2702
  if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
2703
      == FAILURE)
2704
    return FAILURE;
2705
 
2706
  if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2707
    {
2708
      gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2709
                 &iter->var->where);
2710
      return FAILURE;
2711
    }
2712
 
2713
  if (gfc_resolve_iterator_expr (iter->start, real_ok,
2714
                                 "Start expression in DO loop") == FAILURE)
2715
    return FAILURE;
2716
 
2717
  if (gfc_resolve_iterator_expr (iter->end, real_ok,
2718
                                 "End expression in DO loop") == FAILURE)
2719
    return FAILURE;
2720
 
2721
  if (gfc_resolve_iterator_expr (iter->step, real_ok,
2722
                                 "Step expression in DO loop") == FAILURE)
2723
    return FAILURE;
2724
 
2725
  if (iter->step->expr_type == EXPR_CONSTANT)
2726
    {
2727
      if ((iter->step->ts.type == BT_INTEGER
2728
           && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2729
          || (iter->step->ts.type == BT_REAL
2730
              && mpfr_sgn (iter->step->value.real) == 0))
2731
        {
2732
          gfc_error ("Step expression in DO loop at %L cannot be zero",
2733
                     &iter->step->where);
2734
          return FAILURE;
2735
        }
2736
    }
2737
 
2738
  /* Convert start, end, and step to the same type as var.  */
2739
  if (iter->start->ts.kind != iter->var->ts.kind
2740
      || iter->start->ts.type != iter->var->ts.type)
2741
    gfc_convert_type (iter->start, &iter->var->ts, 2);
2742
 
2743
  if (iter->end->ts.kind != iter->var->ts.kind
2744
      || iter->end->ts.type != iter->var->ts.type)
2745
    gfc_convert_type (iter->end, &iter->var->ts, 2);
2746
 
2747
  if (iter->step->ts.kind != iter->var->ts.kind
2748
      || iter->step->ts.type != iter->var->ts.type)
2749
    gfc_convert_type (iter->step, &iter->var->ts, 2);
2750
 
2751
  return SUCCESS;
2752
}
2753
 
2754
 
2755
/* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
2756
   to be a scalar INTEGER variable.  The subscripts and stride are scalar
2757
   INTEGERs, and if stride is a constant it must be nonzero.  */
2758
 
2759
static void
2760
resolve_forall_iterators (gfc_forall_iterator * iter)
2761
{
2762
 
2763
  while (iter)
2764
    {
2765
      if (gfc_resolve_expr (iter->var) == SUCCESS
2766
          && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
2767
        gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
2768
                   &iter->var->where);
2769
 
2770
      if (gfc_resolve_expr (iter->start) == SUCCESS
2771
          && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
2772
        gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
2773
                   &iter->start->where);
2774
      if (iter->var->ts.kind != iter->start->ts.kind)
2775
        gfc_convert_type (iter->start, &iter->var->ts, 2);
2776
 
2777
      if (gfc_resolve_expr (iter->end) == SUCCESS
2778
          && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
2779
        gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
2780
                   &iter->end->where);
2781
      if (iter->var->ts.kind != iter->end->ts.kind)
2782
        gfc_convert_type (iter->end, &iter->var->ts, 2);
2783
 
2784
      if (gfc_resolve_expr (iter->stride) == SUCCESS)
2785
        {
2786
          if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
2787
            gfc_error ("FORALL stride expression at %L must be a scalar %s",
2788
                        &iter->stride->where, "INTEGER");
2789
 
2790
          if (iter->stride->expr_type == EXPR_CONSTANT
2791
              && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
2792
            gfc_error ("FORALL stride expression at %L cannot be zero",
2793
                       &iter->stride->where);
2794
        }
2795
      if (iter->var->ts.kind != iter->stride->ts.kind)
2796
        gfc_convert_type (iter->stride, &iter->var->ts, 2);
2797
 
2798
      iter = iter->next;
2799
    }
2800
}
2801
 
2802
 
2803
/* Given a pointer to a symbol that is a derived type, see if any components
2804
   have the POINTER attribute.  The search is recursive if necessary.
2805
   Returns zero if no pointer components are found, nonzero otherwise.  */
2806
 
2807
static int
2808
derived_pointer (gfc_symbol * sym)
2809
{
2810
  gfc_component *c;
2811
 
2812
  for (c = sym->components; c; c = c->next)
2813
    {
2814
      if (c->pointer)
2815
        return 1;
2816
 
2817
      if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2818
        return 1;
2819
    }
2820
 
2821
  return 0;
2822
}
2823
 
2824
 
2825
/* Given a pointer to a symbol that is a derived type, see if it's
2826
   inaccessible, i.e. if it's defined in another module and the components are
2827
   PRIVATE.  The search is recursive if necessary.  Returns zero if no
2828
   inaccessible components are found, nonzero otherwise.  */
2829
 
2830
static int
2831
derived_inaccessible (gfc_symbol *sym)
2832
{
2833
  gfc_component *c;
2834
 
2835
  if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
2836
    return 1;
2837
 
2838
  for (c = sym->components; c; c = c->next)
2839
    {
2840
        if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
2841
          return 1;
2842
    }
2843
 
2844
  return 0;
2845
}
2846
 
2847
 
2848
/* Resolve the argument of a deallocate expression.  The expression must be
2849
   a pointer or a full array.  */
2850
 
2851
static try
2852
resolve_deallocate_expr (gfc_expr * e)
2853
{
2854
  symbol_attribute attr;
2855
  int allocatable;
2856
  gfc_ref *ref;
2857
 
2858
  if (gfc_resolve_expr (e) == FAILURE)
2859
    return FAILURE;
2860
 
2861
  attr = gfc_expr_attr (e);
2862
  if (attr.pointer)
2863
    return SUCCESS;
2864
 
2865
  if (e->expr_type != EXPR_VARIABLE)
2866
    goto bad;
2867
 
2868
  allocatable = e->symtree->n.sym->attr.allocatable;
2869
  for (ref = e->ref; ref; ref = ref->next)
2870
    switch (ref->type)
2871
      {
2872
      case REF_ARRAY:
2873
        if (ref->u.ar.type != AR_FULL)
2874
          allocatable = 0;
2875
        break;
2876
 
2877
      case REF_COMPONENT:
2878
        allocatable = (ref->u.c.component->as != NULL
2879
                       && ref->u.c.component->as->type == AS_DEFERRED);
2880
        break;
2881
 
2882
      case REF_SUBSTRING:
2883
        allocatable = 0;
2884
        break;
2885
      }
2886
 
2887
  if (allocatable == 0)
2888
    {
2889
    bad:
2890
      gfc_error ("Expression in DEALLOCATE statement at %L must be "
2891
                 "ALLOCATABLE or a POINTER", &e->where);
2892
    }
2893
 
2894
  return SUCCESS;
2895
}
2896
 
2897
 
2898
/* Given the expression node e for an allocatable/pointer of derived type to be
2899
   allocated, get the expression node to be initialized afterwards (needed for
2900
   derived types with default initializers).  */
2901
 
2902
static gfc_expr *
2903
expr_to_initialize (gfc_expr * e)
2904
{
2905
  gfc_expr *result;
2906
  gfc_ref *ref;
2907
  int i;
2908
 
2909
  result = gfc_copy_expr (e);
2910
 
2911
  /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
2912
  for (ref = result->ref; ref; ref = ref->next)
2913
    if (ref->type == REF_ARRAY && ref->next == NULL)
2914
      {
2915
        ref->u.ar.type = AR_FULL;
2916
 
2917
        for (i = 0; i < ref->u.ar.dimen; i++)
2918
          ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
2919
 
2920
        result->rank = ref->u.ar.dimen;
2921
        break;
2922
      }
2923
 
2924
  return result;
2925
}
2926
 
2927
 
2928
/* Resolve the expression in an ALLOCATE statement, doing the additional
2929
   checks to see whether the expression is OK or not.  The expression must
2930
   have a trailing array reference that gives the size of the array.  */
2931
 
2932
static try
2933
resolve_allocate_expr (gfc_expr * e, gfc_code * code)
2934
{
2935
  int i, pointer, allocatable, dimension;
2936
  symbol_attribute attr;
2937
  gfc_ref *ref, *ref2;
2938
  gfc_array_ref *ar;
2939
  gfc_code *init_st;
2940
  gfc_expr *init_e;
2941
 
2942
  if (gfc_resolve_expr (e) == FAILURE)
2943
    return FAILURE;
2944
 
2945
  /* Make sure the expression is allocatable or a pointer.  If it is
2946
     pointer, the next-to-last reference must be a pointer.  */
2947
 
2948
  ref2 = NULL;
2949
 
2950
  if (e->expr_type != EXPR_VARIABLE)
2951
    {
2952
      allocatable = 0;
2953
 
2954
      attr = gfc_expr_attr (e);
2955
      pointer = attr.pointer;
2956
      dimension = attr.dimension;
2957
 
2958
    }
2959
  else
2960
    {
2961
      allocatable = e->symtree->n.sym->attr.allocatable;
2962
      pointer = e->symtree->n.sym->attr.pointer;
2963
      dimension = e->symtree->n.sym->attr.dimension;
2964
 
2965
      for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
2966
        switch (ref->type)
2967
          {
2968
          case REF_ARRAY:
2969
            if (ref->next != NULL)
2970
              pointer = 0;
2971
            break;
2972
 
2973
          case REF_COMPONENT:
2974
            allocatable = (ref->u.c.component->as != NULL
2975
                           && ref->u.c.component->as->type == AS_DEFERRED);
2976
 
2977
            pointer = ref->u.c.component->pointer;
2978
            dimension = ref->u.c.component->dimension;
2979
            break;
2980
 
2981
          case REF_SUBSTRING:
2982
            allocatable = 0;
2983
            pointer = 0;
2984
            break;
2985
          }
2986
    }
2987
 
2988
  if (allocatable == 0 && pointer == 0)
2989
    {
2990
      gfc_error ("Expression in ALLOCATE statement at %L must be "
2991
                 "ALLOCATABLE or a POINTER", &e->where);
2992
      return FAILURE;
2993
    }
2994
 
2995
  /* Add default initializer for those derived types that need them.  */
2996
  if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
2997
    {
2998
        init_st = gfc_get_code ();
2999
        init_st->loc = code->loc;
3000
        init_st->op = EXEC_ASSIGN;
3001
        init_st->expr = expr_to_initialize (e);
3002
        init_st->expr2 = init_e;
3003
 
3004
        init_st->next = code->next;
3005
        code->next = init_st;
3006
    }
3007
 
3008
  if (pointer && dimension == 0)
3009
    return SUCCESS;
3010
 
3011
  /* Make sure the next-to-last reference node is an array specification.  */
3012
 
3013
  if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
3014
    {
3015
      gfc_error ("Array specification required in ALLOCATE statement "
3016
                 "at %L", &e->where);
3017
      return FAILURE;
3018
    }
3019
 
3020
  if (ref2->u.ar.type == AR_ELEMENT)
3021
    return SUCCESS;
3022
 
3023
  /* Make sure that the array section reference makes sense in the
3024
    context of an ALLOCATE specification.  */
3025
 
3026
  ar = &ref2->u.ar;
3027
 
3028
  for (i = 0; i < ar->dimen; i++)
3029
    switch (ar->dimen_type[i])
3030
      {
3031
      case DIMEN_ELEMENT:
3032
        break;
3033
 
3034
      case DIMEN_RANGE:
3035
        if (ar->start[i] != NULL
3036
            && ar->end[i] != NULL
3037
            && ar->stride[i] == NULL)
3038
          break;
3039
 
3040
        /* Fall Through...  */
3041
 
3042
      case DIMEN_UNKNOWN:
3043
      case DIMEN_VECTOR:
3044
        gfc_error ("Bad array specification in ALLOCATE statement at %L",
3045
                   &e->where);
3046
        return FAILURE;
3047
      }
3048
 
3049
  return SUCCESS;
3050
}
3051
 
3052
 
3053
/************ SELECT CASE resolution subroutines ************/
3054
 
3055
/* Callback function for our mergesort variant.  Determines interval
3056
   overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
3057
   op1 > op2.  Assumes we're not dealing with the default case.
3058
   We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
3059
   There are nine situations to check.  */
3060
 
3061
static int
3062
compare_cases (const gfc_case * op1, const gfc_case * op2)
3063
{
3064
  int retval;
3065
 
3066
  if (op1->low == NULL) /* op1 = (:L)  */
3067
    {
3068
      /* op2 = (:N), so overlap.  */
3069
      retval = 0;
3070
      /* op2 = (M:) or (M:N),  L < M  */
3071
      if (op2->low != NULL
3072
          && gfc_compare_expr (op1->high, op2->low) < 0)
3073
        retval = -1;
3074
    }
3075
  else if (op1->high == NULL) /* op1 = (K:)  */
3076
    {
3077
      /* op2 = (M:), so overlap.  */
3078
      retval = 0;
3079
      /* op2 = (:N) or (M:N), K > N  */
3080
      if (op2->high != NULL
3081
          && gfc_compare_expr (op1->low, op2->high) > 0)
3082
        retval = 1;
3083
    }
3084
  else /* op1 = (K:L)  */
3085
    {
3086
      if (op2->low == NULL)       /* op2 = (:N), K > N  */
3087
        retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
3088
      else if (op2->high == NULL) /* op2 = (M:), L < M  */
3089
        retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
3090
      else                        /* op2 = (M:N)  */
3091
        {
3092
          retval =  0;
3093
          /* L < M  */
3094
          if (gfc_compare_expr (op1->high, op2->low) < 0)
3095
            retval =  -1;
3096
          /* K > N  */
3097
          else if (gfc_compare_expr (op1->low, op2->high) > 0)
3098
            retval =  1;
3099
        }
3100
    }
3101
 
3102
  return retval;
3103
}
3104
 
3105
 
3106
/* Merge-sort a double linked case list, detecting overlap in the
3107
   process.  LIST is the head of the double linked case list before it
3108
   is sorted.  Returns the head of the sorted list if we don't see any
3109
   overlap, or NULL otherwise.  */
3110
 
3111
static gfc_case *
3112
check_case_overlap (gfc_case * list)
3113
{
3114
  gfc_case *p, *q, *e, *tail;
3115
  int insize, nmerges, psize, qsize, cmp, overlap_seen;
3116
 
3117
  /* If the passed list was empty, return immediately.  */
3118
  if (!list)
3119
    return NULL;
3120
 
3121
  overlap_seen = 0;
3122
  insize = 1;
3123
 
3124
  /* Loop unconditionally.  The only exit from this loop is a return
3125
     statement, when we've finished sorting the case list.  */
3126
  for (;;)
3127
    {
3128
      p = list;
3129
      list = NULL;
3130
      tail = NULL;
3131
 
3132
      /* Count the number of merges we do in this pass.  */
3133
      nmerges = 0;
3134
 
3135
      /* Loop while there exists a merge to be done.  */
3136
      while (p)
3137
        {
3138
          int i;
3139
 
3140
          /* Count this merge.  */
3141
          nmerges++;
3142
 
3143
          /* Cut the list in two pieces by stepping INSIZE places
3144
             forward in the list, starting from P.  */
3145
          psize = 0;
3146
          q = p;
3147
          for (i = 0; i < insize; i++)
3148
            {
3149
              psize++;
3150
              q = q->right;
3151
              if (!q)
3152
                break;
3153
            }
3154
          qsize = insize;
3155
 
3156
          /* Now we have two lists.  Merge them!  */
3157
          while (psize > 0 || (qsize > 0 && q != NULL))
3158
            {
3159
 
3160
              /* See from which the next case to merge comes from.  */
3161
              if (psize == 0)
3162
                {
3163
                  /* P is empty so the next case must come from Q.  */
3164
                  e = q;
3165
                  q = q->right;
3166
                  qsize--;
3167
                }
3168
              else if (qsize == 0 || q == NULL)
3169
                {
3170
                  /* Q is empty.  */
3171
                  e = p;
3172
                  p = p->right;
3173
                  psize--;
3174
                }
3175
              else
3176
                {
3177
                  cmp = compare_cases (p, q);
3178
                  if (cmp < 0)
3179
                    {
3180
                      /* The whole case range for P is less than the
3181
                         one for Q.  */
3182
                      e = p;
3183
                      p = p->right;
3184
                      psize--;
3185
                    }
3186
                  else if (cmp > 0)
3187
                    {
3188
                      /* The whole case range for Q is greater than
3189
                         the case range for P.  */
3190
                      e = q;
3191
                      q = q->right;
3192
                      qsize--;
3193
                    }
3194
                  else
3195
                    {
3196
                      /* The cases overlap, or they are the same
3197
                         element in the list.  Either way, we must
3198
                         issue an error and get the next case from P.  */
3199
                      /* FIXME: Sort P and Q by line number.  */
3200
                      gfc_error ("CASE label at %L overlaps with CASE "
3201
                                 "label at %L", &p->where, &q->where);
3202
                      overlap_seen = 1;
3203
                      e = p;
3204
                      p = p->right;
3205
                      psize--;
3206
                    }
3207
                }
3208
 
3209
                /* Add the next element to the merged list.  */
3210
              if (tail)
3211
                tail->right = e;
3212
              else
3213
                list = e;
3214
              e->left = tail;
3215
              tail = e;
3216
            }
3217
 
3218
          /* P has now stepped INSIZE places along, and so has Q.  So
3219
             they're the same.  */
3220
          p = q;
3221
        }
3222
      tail->right = NULL;
3223
 
3224
      /* If we have done only one merge or none at all, we've
3225
         finished sorting the cases.  */
3226
      if (nmerges <= 1)
3227
        {
3228
          if (!overlap_seen)
3229
            return list;
3230
          else
3231
            return NULL;
3232
        }
3233
 
3234
      /* Otherwise repeat, merging lists twice the size.  */
3235
      insize *= 2;
3236
    }
3237
}
3238
 
3239
 
3240
/* Check to see if an expression is suitable for use in a CASE statement.
3241
   Makes sure that all case expressions are scalar constants of the same
3242
   type.  Return FAILURE if anything is wrong.  */
3243
 
3244
static try
3245
validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
3246
{
3247
  if (e == NULL) return SUCCESS;
3248
 
3249
  if (e->ts.type != case_expr->ts.type)
3250
    {
3251
      gfc_error ("Expression in CASE statement at %L must be of type %s",
3252
                 &e->where, gfc_basic_typename (case_expr->ts.type));
3253
      return FAILURE;
3254
    }
3255
 
3256
  /* C805 (R808) For a given case-construct, each case-value shall be of
3257
     the same type as case-expr.  For character type, length differences
3258
     are allowed, but the kind type parameters shall be the same.  */
3259
 
3260
  if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
3261
    {
3262
      gfc_error("Expression in CASE statement at %L must be kind %d",
3263
                &e->where, case_expr->ts.kind);
3264
      return FAILURE;
3265
    }
3266
 
3267
  /* Convert the case value kind to that of case expression kind, if needed.
3268
     FIXME:  Should a warning be issued?  */
3269
  if (e->ts.kind != case_expr->ts.kind)
3270
    gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
3271
 
3272
  if (e->rank != 0)
3273
    {
3274
      gfc_error ("Expression in CASE statement at %L must be scalar",
3275
                 &e->where);
3276
      return FAILURE;
3277
    }
3278
 
3279
  return SUCCESS;
3280
}
3281
 
3282
 
3283
/* Given a completely parsed select statement, we:
3284
 
3285
     - Validate all expressions and code within the SELECT.
3286
     - Make sure that the selection expression is not of the wrong type.
3287
     - Make sure that no case ranges overlap.
3288
     - Eliminate unreachable cases and unreachable code resulting from
3289
       removing case labels.
3290
 
3291
   The standard does allow unreachable cases, e.g. CASE (5:3).  But
3292
   they are a hassle for code generation, and to prevent that, we just
3293
   cut them out here.  This is not necessary for overlapping cases
3294
   because they are illegal and we never even try to generate code.
3295
 
3296
   We have the additional caveat that a SELECT construct could have
3297
   been a computed GOTO in the source code. Fortunately we can fairly
3298
   easily work around that here: The case_expr for a "real" SELECT CASE
3299
   is in code->expr1, but for a computed GOTO it is in code->expr2. All
3300
   we have to do is make sure that the case_expr is a scalar integer
3301
   expression.  */
3302
 
3303
static void
3304
resolve_select (gfc_code * code)
3305
{
3306
  gfc_code *body;
3307
  gfc_expr *case_expr;
3308
  gfc_case *cp, *default_case, *tail, *head;
3309
  int seen_unreachable;
3310
  int ncases;
3311
  bt type;
3312
  try t;
3313
 
3314
  if (code->expr == NULL)
3315
    {
3316
      /* This was actually a computed GOTO statement.  */
3317
      case_expr = code->expr2;
3318
      if (case_expr->ts.type != BT_INTEGER
3319
          || case_expr->rank != 0)
3320
        gfc_error ("Selection expression in computed GOTO statement "
3321
                   "at %L must be a scalar integer expression",
3322
                   &case_expr->where);
3323
 
3324
      /* Further checking is not necessary because this SELECT was built
3325
         by the compiler, so it should always be OK.  Just move the
3326
         case_expr from expr2 to expr so that we can handle computed
3327
         GOTOs as normal SELECTs from here on.  */
3328
      code->expr = code->expr2;
3329
      code->expr2 = NULL;
3330
      return;
3331
    }
3332
 
3333
  case_expr = code->expr;
3334
 
3335
  type = case_expr->ts.type;
3336
  if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
3337
    {
3338
      gfc_error ("Argument of SELECT statement at %L cannot be %s",
3339
                 &case_expr->where, gfc_typename (&case_expr->ts));
3340
 
3341
      /* Punt. Going on here just produce more garbage error messages.  */
3342
      return;
3343
    }
3344
 
3345
  if (case_expr->rank != 0)
3346
    {
3347
      gfc_error ("Argument of SELECT statement at %L must be a scalar "
3348
                 "expression", &case_expr->where);
3349
 
3350
      /* Punt.  */
3351
      return;
3352
    }
3353
 
3354
  /* PR 19168 has a long discussion concerning a mismatch of the kinds
3355
     of the SELECT CASE expression and its CASE values.  Walk the lists
3356
     of case values, and if we find a mismatch, promote case_expr to
3357
     the appropriate kind.  */
3358
 
3359
  if (type == BT_LOGICAL || type == BT_INTEGER)
3360
    {
3361
      for (body = code->block; body; body = body->block)
3362
        {
3363
          /* Walk the case label list.  */
3364
          for (cp = body->ext.case_list; cp; cp = cp->next)
3365
            {
3366
              /* Intercept the DEFAULT case.  It does not have a kind.  */
3367
              if (cp->low == NULL && cp->high == NULL)
3368
                continue;
3369
 
3370
              /* Unreachable case ranges are discarded, so ignore.  */
3371
              if (cp->low != NULL && cp->high != NULL
3372
                  && cp->low != cp->high
3373
                  && gfc_compare_expr (cp->low, cp->high) > 0)
3374
                continue;
3375
 
3376
              /* FIXME: Should a warning be issued?  */
3377
              if (cp->low != NULL
3378
                  && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
3379
                gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
3380
 
3381
              if (cp->high != NULL
3382
                  && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
3383
                gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
3384
            }
3385
         }
3386
    }
3387
 
3388
  /* Assume there is no DEFAULT case.  */
3389
  default_case = NULL;
3390
  head = tail = NULL;
3391
  ncases = 0;
3392
 
3393
  for (body = code->block; body; body = body->block)
3394
    {
3395
      /* Assume the CASE list is OK, and all CASE labels can be matched.  */
3396
      t = SUCCESS;
3397
      seen_unreachable = 0;
3398
 
3399
      /* Walk the case label list, making sure that all case labels
3400
         are legal.  */
3401
      for (cp = body->ext.case_list; cp; cp = cp->next)
3402
        {
3403
          /* Count the number of cases in the whole construct.  */
3404
          ncases++;
3405
 
3406
          /* Intercept the DEFAULT case.  */
3407
          if (cp->low == NULL && cp->high == NULL)
3408
            {
3409
              if (default_case != NULL)
3410
                {
3411
                  gfc_error ("The DEFAULT CASE at %L cannot be followed "
3412
                             "by a second DEFAULT CASE at %L",
3413
                             &default_case->where, &cp->where);
3414
                  t = FAILURE;
3415
                  break;
3416
                }
3417
              else
3418
                {
3419
                  default_case = cp;
3420
                  continue;
3421
                }
3422
            }
3423
 
3424
          /* Deal with single value cases and case ranges.  Errors are
3425
             issued from the validation function.  */
3426
          if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
3427
             || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
3428
            {
3429
              t = FAILURE;
3430
              break;
3431
            }
3432
 
3433
          if (type == BT_LOGICAL
3434
              && ((cp->low == NULL || cp->high == NULL)
3435
                  || cp->low != cp->high))
3436
            {
3437
              gfc_error
3438
                ("Logical range in CASE statement at %L is not allowed",
3439
                 &cp->low->where);
3440
              t = FAILURE;
3441
              break;
3442
            }
3443
 
3444
          if (cp->low != NULL && cp->high != NULL
3445
              && cp->low != cp->high
3446
              && gfc_compare_expr (cp->low, cp->high) > 0)
3447
            {
3448
              if (gfc_option.warn_surprising)
3449
                gfc_warning ("Range specification at %L can never "
3450
                             "be matched", &cp->where);
3451
 
3452
              cp->unreachable = 1;
3453
              seen_unreachable = 1;
3454
            }
3455
          else
3456
            {
3457
              /* If the case range can be matched, it can also overlap with
3458
                 other cases.  To make sure it does not, we put it in a
3459
                 double linked list here.  We sort that with a merge sort
3460
                 later on to detect any overlapping cases.  */
3461
              if (!head)
3462
                {
3463
                  head = tail = cp;
3464
                  head->right = head->left = NULL;
3465
                }
3466
              else
3467
                {
3468
                  tail->right = cp;
3469
                  tail->right->left = tail;
3470
                  tail = tail->right;
3471
                  tail->right = NULL;
3472
                }
3473
            }
3474
        }
3475
 
3476
      /* It there was a failure in the previous case label, give up
3477
         for this case label list.  Continue with the next block.  */
3478
      if (t == FAILURE)
3479
        continue;
3480
 
3481
      /* See if any case labels that are unreachable have been seen.
3482
         If so, we eliminate them.  This is a bit of a kludge because
3483
         the case lists for a single case statement (label) is a
3484
         single forward linked lists.  */
3485
      if (seen_unreachable)
3486
      {
3487
        /* Advance until the first case in the list is reachable.  */
3488
        while (body->ext.case_list != NULL
3489
               && body->ext.case_list->unreachable)
3490
          {
3491
            gfc_case *n = body->ext.case_list;
3492
            body->ext.case_list = body->ext.case_list->next;
3493
            n->next = NULL;
3494
            gfc_free_case_list (n);
3495
          }
3496
 
3497
        /* Strip all other unreachable cases.  */
3498
        if (body->ext.case_list)
3499
          {
3500
            for (cp = body->ext.case_list; cp->next; cp = cp->next)
3501
              {
3502
                if (cp->next->unreachable)
3503
                  {
3504
                    gfc_case *n = cp->next;
3505
                    cp->next = cp->next->next;
3506
                    n->next = NULL;
3507
                    gfc_free_case_list (n);
3508
                  }
3509
              }
3510
          }
3511
      }
3512
    }
3513
 
3514
  /* See if there were overlapping cases.  If the check returns NULL,
3515
     there was overlap.  In that case we don't do anything.  If head
3516
     is non-NULL, we prepend the DEFAULT case.  The sorted list can
3517
     then used during code generation for SELECT CASE constructs with
3518
     a case expression of a CHARACTER type.  */
3519
  if (head)
3520
    {
3521
      head = check_case_overlap (head);
3522
 
3523
      /* Prepend the default_case if it is there.  */
3524
      if (head != NULL && default_case)
3525
        {
3526
          default_case->left = NULL;
3527
          default_case->right = head;
3528
          head->left = default_case;
3529
        }
3530
    }
3531
 
3532
  /* Eliminate dead blocks that may be the result if we've seen
3533
     unreachable case labels for a block.  */
3534
  for (body = code; body && body->block; body = body->block)
3535
    {
3536
      if (body->block->ext.case_list == NULL)
3537
        {
3538
          /* Cut the unreachable block from the code chain.  */
3539
          gfc_code *c = body->block;
3540
          body->block = c->block;
3541
 
3542
          /* Kill the dead block, but not the blocks below it.  */
3543
          c->block = NULL;
3544
          gfc_free_statements (c);
3545
        }
3546
    }
3547
 
3548
  /* More than two cases is legal but insane for logical selects.
3549
     Issue a warning for it.  */
3550
  if (gfc_option.warn_surprising && type == BT_LOGICAL
3551
      && ncases > 2)
3552
    gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
3553
                 &code->loc);
3554
}
3555
 
3556
 
3557
/* Resolve a transfer statement. This is making sure that:
3558
   -- a derived type being transferred has only non-pointer components
3559
   -- a derived type being transferred doesn't have private components, unless
3560
      it's being transferred from the module where the type was defined
3561
   -- we're not trying to transfer a whole assumed size array.  */
3562
 
3563
static void
3564
resolve_transfer (gfc_code * code)
3565
{
3566
  gfc_typespec *ts;
3567
  gfc_symbol *sym;
3568
  gfc_ref *ref;
3569
  gfc_expr *exp;
3570
 
3571
  exp = code->expr;
3572
 
3573
  if (exp->expr_type != EXPR_VARIABLE)
3574
    return;
3575
 
3576
  sym = exp->symtree->n.sym;
3577
  ts = &sym->ts;
3578
 
3579
  /* Go to actual component transferred.  */
3580
  for (ref = code->expr->ref; ref; ref = ref->next)
3581
    if (ref->type == REF_COMPONENT)
3582
      ts = &ref->u.c.component->ts;
3583
 
3584
  if (ts->type == BT_DERIVED)
3585
    {
3586
      /* Check that transferred derived type doesn't contain POINTER
3587
         components.  */
3588
      if (derived_pointer (ts->derived))
3589
        {
3590
          gfc_error ("Data transfer element at %L cannot have "
3591
                     "POINTER components", &code->loc);
3592
          return;
3593
        }
3594
 
3595
      if (derived_inaccessible (ts->derived))
3596
        {
3597
          gfc_error ("Data transfer element at %L cannot have "
3598
                     "PRIVATE components",&code->loc);
3599
          return;
3600
        }
3601
    }
3602
 
3603
  if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
3604
      && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
3605
    {
3606
      gfc_error ("Data transfer element at %L cannot be a full reference to "
3607
                 "an assumed-size array", &code->loc);
3608
      return;
3609
    }
3610
}
3611
 
3612
 
3613
/*********** Toplevel code resolution subroutines ***********/
3614
 
3615
/* Given a branch to a label and a namespace, if the branch is conforming.
3616
   The code node described where the branch is located.  */
3617
 
3618
static void
3619
resolve_branch (gfc_st_label * label, gfc_code * code)
3620
{
3621
  gfc_code *block, *found;
3622
  code_stack *stack;
3623
  gfc_st_label *lp;
3624
 
3625
  if (label == NULL)
3626
    return;
3627
  lp = label;
3628
 
3629
  /* Step one: is this a valid branching target?  */
3630
 
3631
  if (lp->defined == ST_LABEL_UNKNOWN)
3632
    {
3633
      gfc_error ("Label %d referenced at %L is never defined", lp->value,
3634
                 &lp->where);
3635
      return;
3636
    }
3637
 
3638
  if (lp->defined != ST_LABEL_TARGET)
3639
    {
3640
      gfc_error ("Statement at %L is not a valid branch target statement "
3641
                 "for the branch statement at %L", &lp->where, &code->loc);
3642
      return;
3643
    }
3644
 
3645
  /* Step two: make sure this branch is not a branch to itself ;-)  */
3646
 
3647
  if (code->here == label)
3648
    {
3649
      gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
3650
      return;
3651
    }
3652
 
3653
  /* Step three: Try to find the label in the parse tree. To do this,
3654
     we traverse the tree block-by-block: first the block that
3655
     contains this GOTO, then the block that it is nested in, etc.  We
3656
     can ignore other blocks because branching into another block is
3657
     not allowed.  */
3658
 
3659
  found = NULL;
3660
 
3661
  for (stack = cs_base; stack; stack = stack->prev)
3662
    {
3663
      for (block = stack->head; block; block = block->next)
3664
        {
3665
          if (block->here == label)
3666
            {
3667
              found = block;
3668
              break;
3669
            }
3670
        }
3671
 
3672
      if (found)
3673
        break;
3674
    }
3675
 
3676
  if (found == NULL)
3677
    {
3678
      /* The label is not in an enclosing block, so illegal.  This was
3679
         allowed in Fortran 66, so we allow it as extension.  We also
3680
         forego further checks if we run into this.  */
3681
      gfc_notify_std (GFC_STD_LEGACY,
3682
                      "Label at %L is not in the same block as the "
3683
                      "GOTO statement at %L", &lp->where, &code->loc);
3684
      return;
3685
    }
3686
 
3687
  /* Step four: Make sure that the branching target is legal if
3688
     the statement is an END {SELECT,DO,IF}.  */
3689
 
3690
  if (found->op == EXEC_NOP)
3691
    {
3692
      for (stack = cs_base; stack; stack = stack->prev)
3693
        if (stack->current->next == found)
3694
          break;
3695
 
3696
      if (stack == NULL)
3697
        gfc_notify_std (GFC_STD_F95_DEL,
3698
                        "Obsolete: GOTO at %L jumps to END of construct at %L",
3699
                        &code->loc, &found->loc);
3700
    }
3701
}
3702
 
3703
 
3704
/* Check whether EXPR1 has the same shape as EXPR2.  */
3705
 
3706
static try
3707
resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
3708
{
3709
  mpz_t shape[GFC_MAX_DIMENSIONS];
3710
  mpz_t shape2[GFC_MAX_DIMENSIONS];
3711
  try result = FAILURE;
3712
  int i;
3713
 
3714
  /* Compare the rank.  */
3715
  if (expr1->rank != expr2->rank)
3716
    return result;
3717
 
3718
  /* Compare the size of each dimension.  */
3719
  for (i=0; i<expr1->rank; i++)
3720
    {
3721
      if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
3722
        goto ignore;
3723
 
3724
      if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
3725
        goto ignore;
3726
 
3727
      if (mpz_cmp (shape[i], shape2[i]))
3728
        goto over;
3729
    }
3730
 
3731
  /* When either of the two expression is an assumed size array, we
3732
     ignore the comparison of dimension sizes.  */
3733
ignore:
3734
  result = SUCCESS;
3735
 
3736
over:
3737
  for (i--; i>=0; i--)
3738
    {
3739
      mpz_clear (shape[i]);
3740
      mpz_clear (shape2[i]);
3741
    }
3742
  return result;
3743
}
3744
 
3745
 
3746
/* Check whether a WHERE assignment target or a WHERE mask expression
3747
   has the same shape as the outmost WHERE mask expression.  */
3748
 
3749
static void
3750
resolve_where (gfc_code *code, gfc_expr *mask)
3751
{
3752
  gfc_code *cblock;
3753
  gfc_code *cnext;
3754
  gfc_expr *e = NULL;
3755
 
3756
  cblock = code->block;
3757
 
3758
  /* Store the first WHERE mask-expr of the WHERE statement or construct.
3759
     In case of nested WHERE, only the outmost one is stored.  */
3760
  if (mask == NULL) /* outmost WHERE */
3761
    e = cblock->expr;
3762
  else /* inner WHERE */
3763
    e = mask;
3764
 
3765
  while (cblock)
3766
    {
3767
      if (cblock->expr)
3768
        {
3769
          /* Check if the mask-expr has a consistent shape with the
3770
             outmost WHERE mask-expr.  */
3771
          if (resolve_where_shape (cblock->expr, e) == FAILURE)
3772
            gfc_error ("WHERE mask at %L has inconsistent shape",
3773
                       &cblock->expr->where);
3774
         }
3775
 
3776
      /* the assignment statement of a WHERE statement, or the first
3777
         statement in where-body-construct of a WHERE construct */
3778
      cnext = cblock->next;
3779
      while (cnext)
3780
        {
3781
          switch (cnext->op)
3782
            {
3783
            /* WHERE assignment statement */
3784
            case EXEC_ASSIGN:
3785
 
3786
              /* Check shape consistent for WHERE assignment target.  */
3787
              if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3788
               gfc_error ("WHERE assignment target at %L has "
3789
                          "inconsistent shape", &cnext->expr->where);
3790
              break;
3791
 
3792
            /* WHERE or WHERE construct is part of a where-body-construct */
3793
            case EXEC_WHERE:
3794
              resolve_where (cnext, e);
3795
              break;
3796
 
3797
            default:
3798
              gfc_error ("Unsupported statement inside WHERE at %L",
3799
                         &cnext->loc);
3800
            }
3801
         /* the next statement within the same where-body-construct */
3802
         cnext = cnext->next;
3803
       }
3804
    /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3805
    cblock = cblock->block;
3806
  }
3807
}
3808
 
3809
 
3810
/* Check whether the FORALL index appears in the expression or not.  */
3811
 
3812
static try
3813
gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3814
{
3815
  gfc_array_ref ar;
3816
  gfc_ref *tmp;
3817
  gfc_actual_arglist *args;
3818
  int i;
3819
 
3820
  switch (expr->expr_type)
3821
    {
3822
    case EXPR_VARIABLE:
3823
      gcc_assert (expr->symtree->n.sym);
3824
 
3825
      /* A scalar assignment  */
3826
      if (!expr->ref)
3827
        {
3828
          if (expr->symtree->n.sym == symbol)
3829
            return SUCCESS;
3830
          else
3831
            return FAILURE;
3832
        }
3833
 
3834
      /* the expr is array ref, substring or struct component.  */
3835
      tmp = expr->ref;
3836
      while (tmp != NULL)
3837
        {
3838
          switch (tmp->type)
3839
            {
3840
            case  REF_ARRAY:
3841
              /* Check if the symbol appears in the array subscript.  */
3842
              ar = tmp->u.ar;
3843
              for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3844
                {
3845
                  if (ar.start[i])
3846
                    if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3847
                      return SUCCESS;
3848
 
3849
                  if (ar.end[i])
3850
                    if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3851
                      return SUCCESS;
3852
 
3853
                  if (ar.stride[i])
3854
                    if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3855
                      return SUCCESS;
3856
                }  /* end for  */
3857
              break;
3858
 
3859
            case REF_SUBSTRING:
3860
              if (expr->symtree->n.sym == symbol)
3861
                return SUCCESS;
3862
              tmp = expr->ref;
3863
              /* Check if the symbol appears in the substring section.  */
3864
              if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3865
                return SUCCESS;
3866
              if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3867
                return SUCCESS;
3868
              break;
3869
 
3870
            case REF_COMPONENT:
3871
              break;
3872
 
3873
            default:
3874
              gfc_error("expresion reference type error at %L", &expr->where);
3875
            }
3876
          tmp = tmp->next;
3877
        }
3878
      break;
3879
 
3880
    /* If the expression is a function call, then check if the symbol
3881
       appears in the actual arglist of the function.  */
3882
    case EXPR_FUNCTION:
3883
      for (args = expr->value.function.actual; args; args = args->next)
3884
        {
3885
          if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
3886
            return SUCCESS;
3887
        }
3888
      break;
3889
 
3890
    /* It seems not to happen.  */
3891
    case EXPR_SUBSTRING:
3892
      if (expr->ref)
3893
        {
3894
          tmp = expr->ref;
3895
          gcc_assert (expr->ref->type == REF_SUBSTRING);
3896
          if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3897
            return SUCCESS;
3898
          if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3899
            return SUCCESS;
3900
        }
3901
      break;
3902
 
3903
    /* It seems not to happen.  */
3904
    case EXPR_STRUCTURE:
3905
    case EXPR_ARRAY:
3906
      gfc_error ("Unsupported statement while finding forall index in "
3907
                 "expression");
3908
      break;
3909
 
3910
    case EXPR_OP:
3911
      /* Find the FORALL index in the first operand.  */
3912
      if (expr->value.op.op1)
3913
        {
3914
          if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
3915
            return SUCCESS;
3916
        }
3917
 
3918
      /* Find the FORALL index in the second operand.  */
3919
      if (expr->value.op.op2)
3920
        {
3921
          if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
3922
            return SUCCESS;
3923
        }
3924
      break;
3925
 
3926
    default:
3927
      break;
3928
    }
3929
 
3930
  return FAILURE;
3931
}
3932
 
3933
 
3934
/* Resolve assignment in FORALL construct.
3935
   NVAR is the number of FORALL index variables, and VAR_EXPR records the
3936
   FORALL index variables.  */
3937
 
3938
static void
3939
gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
3940
{
3941
  int n;
3942
 
3943
  for (n = 0; n < nvar; n++)
3944
    {
3945
      gfc_symbol *forall_index;
3946
 
3947
      forall_index = var_expr[n]->symtree->n.sym;
3948
 
3949
      /* Check whether the assignment target is one of the FORALL index
3950
         variable.  */
3951
      if ((code->expr->expr_type == EXPR_VARIABLE)
3952
          && (code->expr->symtree->n.sym == forall_index))
3953
        gfc_error ("Assignment to a FORALL index variable at %L",
3954
                   &code->expr->where);
3955
      else
3956
        {
3957
          /* If one of the FORALL index variables doesn't appear in the
3958
             assignment target, then there will be a many-to-one
3959
             assignment.  */
3960
          if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
3961
            gfc_error ("The FORALL with index '%s' cause more than one "
3962
                       "assignment to this object at %L",
3963
                       var_expr[n]->symtree->name, &code->expr->where);
3964
        }
3965
    }
3966
}
3967
 
3968
 
3969
/* Resolve WHERE statement in FORALL construct.  */
3970
 
3971
static void
3972
gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
3973
  gfc_code *cblock;
3974
  gfc_code *cnext;
3975
 
3976
  cblock = code->block;
3977
  while (cblock)
3978
    {
3979
      /* the assignment statement of a WHERE statement, or the first
3980
         statement in where-body-construct of a WHERE construct */
3981
      cnext = cblock->next;
3982
      while (cnext)
3983
        {
3984
          switch (cnext->op)
3985
            {
3986
            /* WHERE assignment statement */
3987
            case EXEC_ASSIGN:
3988
              gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
3989
              break;
3990
 
3991
            /* WHERE or WHERE construct is part of a where-body-construct */
3992
            case EXEC_WHERE:
3993
              gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
3994
              break;
3995
 
3996
            default:
3997
              gfc_error ("Unsupported statement inside WHERE at %L",
3998
                         &cnext->loc);
3999
            }
4000
          /* the next statement within the same where-body-construct */
4001
          cnext = cnext->next;
4002
        }
4003
      /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
4004
      cblock = cblock->block;
4005
    }
4006
}
4007
 
4008
 
4009
/* Traverse the FORALL body to check whether the following errors exist:
4010
   1. For assignment, check if a many-to-one assignment happens.
4011
   2. For WHERE statement, check the WHERE body to see if there is any
4012
      many-to-one assignment.  */
4013
 
4014
static void
4015
gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
4016
{
4017
  gfc_code *c;
4018
 
4019
  c = code->block->next;
4020
  while (c)
4021
    {
4022
      switch (c->op)
4023
        {
4024
        case EXEC_ASSIGN:
4025
        case EXEC_POINTER_ASSIGN:
4026
          gfc_resolve_assign_in_forall (c, nvar, var_expr);
4027
          break;
4028
 
4029
        /* Because the resolve_blocks() will handle the nested FORALL,
4030
           there is no need to handle it here.  */
4031
        case EXEC_FORALL:
4032
          break;
4033
        case EXEC_WHERE:
4034
          gfc_resolve_where_code_in_forall(c, nvar, var_expr);
4035
          break;
4036
        default:
4037
          break;
4038
        }
4039
      /* The next statement in the FORALL body.  */
4040
      c = c->next;
4041
    }
4042
}
4043
 
4044
 
4045
/* Given a FORALL construct, first resolve the FORALL iterator, then call
4046
   gfc_resolve_forall_body to resolve the FORALL body.  */
4047
 
4048
static void resolve_blocks (gfc_code *, gfc_namespace *);
4049
 
4050
static void
4051
gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
4052
{
4053
  static gfc_expr **var_expr;
4054
  static int total_var = 0;
4055
  static int nvar = 0;
4056
  gfc_forall_iterator *fa;
4057
  gfc_symbol *forall_index;
4058
  gfc_code *next;
4059
  int i;
4060
 
4061
  /* Start to resolve a FORALL construct   */
4062
  if (forall_save == 0)
4063
    {
4064
      /* Count the total number of FORALL index in the nested FORALL
4065
         construct in order to allocate the VAR_EXPR with proper size.  */
4066
      next = code;
4067
      while ((next != NULL) && (next->op == EXEC_FORALL))
4068
        {
4069
          for (fa = next->ext.forall_iterator; fa; fa = fa->next)
4070
            total_var ++;
4071
          next = next->block->next;
4072
        }
4073
 
4074
      /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
4075
      var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
4076
    }
4077
 
4078
  /* The information about FORALL iterator, including FORALL index start, end
4079
     and stride. The FORALL index can not appear in start, end or stride.  */
4080
  for (fa = code->ext.forall_iterator; fa; fa = fa->next)
4081
    {
4082
      /* Check if any outer FORALL index name is the same as the current
4083
         one.  */
4084
      for (i = 0; i < nvar; i++)
4085
        {
4086
          if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
4087
            {
4088
              gfc_error ("An outer FORALL construct already has an index "
4089
                         "with this name %L", &fa->var->where);
4090
            }
4091
        }
4092
 
4093
      /* Record the current FORALL index.  */
4094
      var_expr[nvar] = gfc_copy_expr (fa->var);
4095
 
4096
      forall_index = fa->var->symtree->n.sym;
4097
 
4098
      /* Check if the FORALL index appears in start, end or stride.  */
4099
      if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
4100
        gfc_error ("A FORALL index must not appear in a limit or stride "
4101
                   "expression in the same FORALL at %L", &fa->start->where);
4102
      if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
4103
        gfc_error ("A FORALL index must not appear in a limit or stride "
4104
                   "expression in the same FORALL at %L", &fa->end->where);
4105
      if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
4106
        gfc_error ("A FORALL index must not appear in a limit or stride "
4107
                   "expression in the same FORALL at %L", &fa->stride->where);
4108
      nvar++;
4109
    }
4110
 
4111
  /* Resolve the FORALL body.  */
4112
  gfc_resolve_forall_body (code, nvar, var_expr);
4113
 
4114
  /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
4115
  resolve_blocks (code->block, ns);
4116
 
4117
  /* Free VAR_EXPR after the whole FORALL construct resolved.  */
4118
  for (i = 0; i < total_var; i++)
4119
    gfc_free_expr (var_expr[i]);
4120
 
4121
  /* Reset the counters.  */
4122
  total_var = 0;
4123
  nvar = 0;
4124
}
4125
 
4126
 
4127
/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
4128
   DO code nodes.  */
4129
 
4130
static void resolve_code (gfc_code *, gfc_namespace *);
4131
 
4132
static void
4133
resolve_blocks (gfc_code * b, gfc_namespace * ns)
4134
{
4135
  try t;
4136
 
4137
  for (; b; b = b->block)
4138
    {
4139
      t = gfc_resolve_expr (b->expr);
4140
      if (gfc_resolve_expr (b->expr2) == FAILURE)
4141
        t = FAILURE;
4142
 
4143
      switch (b->op)
4144
        {
4145
        case EXEC_IF:
4146
          if (t == SUCCESS && b->expr != NULL
4147
              && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
4148
            gfc_error
4149
              ("ELSE IF clause at %L requires a scalar LOGICAL expression",
4150
               &b->expr->where);
4151
          break;
4152
 
4153
        case EXEC_WHERE:
4154
          if (t == SUCCESS
4155
              && b->expr != NULL
4156
              && (b->expr->ts.type != BT_LOGICAL
4157
                  || b->expr->rank == 0))
4158
            gfc_error
4159
              ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
4160
               &b->expr->where);
4161
          break;
4162
 
4163
        case EXEC_GOTO:
4164
          resolve_branch (b->label, b);
4165
          break;
4166
 
4167
        case EXEC_SELECT:
4168
        case EXEC_FORALL:
4169
        case EXEC_DO:
4170
        case EXEC_DO_WHILE:
4171
        case EXEC_READ:
4172
        case EXEC_WRITE:
4173
        case EXEC_IOLENGTH:
4174
          break;
4175
 
4176
        default:
4177
          gfc_internal_error ("resolve_block(): Bad block type");
4178
        }
4179
 
4180
      resolve_code (b->next, ns);
4181
    }
4182
}
4183
 
4184
 
4185
/* Given a block of code, recursively resolve everything pointed to by this
4186
   code block.  */
4187
 
4188
static void
4189
resolve_code (gfc_code * code, gfc_namespace * ns)
4190
{
4191
  int forall_save = 0;
4192
  code_stack frame;
4193
  gfc_alloc *a;
4194
  try t;
4195
 
4196
  frame.prev = cs_base;
4197
  frame.head = code;
4198
  cs_base = &frame;
4199
 
4200
  for (; code; code = code->next)
4201
    {
4202
      frame.current = code;
4203
 
4204
      if (code->op == EXEC_FORALL)
4205
        {
4206
          forall_save = forall_flag;
4207
          forall_flag = 1;
4208
          gfc_resolve_forall (code, ns, forall_save);
4209
        }
4210
      else
4211
        resolve_blocks (code->block, ns);
4212
 
4213
      if (code->op == EXEC_FORALL)
4214
        forall_flag = forall_save;
4215
 
4216
      t = gfc_resolve_expr (code->expr);
4217
      if (gfc_resolve_expr (code->expr2) == FAILURE)
4218
        t = FAILURE;
4219
 
4220
      switch (code->op)
4221
        {
4222
        case EXEC_NOP:
4223
        case EXEC_CYCLE:
4224
        case EXEC_PAUSE:
4225
        case EXEC_STOP:
4226
        case EXEC_EXIT:
4227
        case EXEC_CONTINUE:
4228
        case EXEC_DT_END:
4229
        case EXEC_ENTRY:
4230
          break;
4231
 
4232
        case EXEC_WHERE:
4233
          resolve_where (code, NULL);
4234
          break;
4235
 
4236
        case EXEC_GOTO:
4237
          if (code->expr != NULL)
4238
            {
4239
              if (code->expr->ts.type != BT_INTEGER)
4240
                gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
4241
                       "variable", &code->expr->where);
4242
              else if (code->expr->symtree->n.sym->attr.assign != 1)
4243
                gfc_error ("Variable '%s' has not been assigned a target label "
4244
                        "at %L", code->expr->symtree->n.sym->name,
4245
                        &code->expr->where);
4246
            }
4247
          else
4248
            resolve_branch (code->label, code);
4249
          break;
4250
 
4251
        case EXEC_RETURN:
4252
          if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
4253
            gfc_error ("Alternate RETURN statement at %L requires an INTEGER "
4254
                       "return specifier", &code->expr->where);
4255
          break;
4256
 
4257
        case EXEC_ASSIGN:
4258
          if (t == FAILURE)
4259
            break;
4260
 
4261
          if (gfc_extend_assign (code, ns) == SUCCESS)
4262
            {
4263
              if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
4264
                {
4265
                  gfc_error ("Subroutine '%s' called instead of assignment at "
4266
                             "%L must be PURE", code->symtree->n.sym->name,
4267
                             &code->loc);
4268
                  break;
4269
                }
4270
              goto call;
4271
            }
4272
 
4273
          if (gfc_pure (NULL))
4274
            {
4275
              if (gfc_impure_variable (code->expr->symtree->n.sym))
4276
                {
4277
                  gfc_error
4278
                    ("Cannot assign to variable '%s' in PURE procedure at %L",
4279
                     code->expr->symtree->n.sym->name, &code->expr->where);
4280
                  break;
4281
                }
4282
 
4283
              if (code->expr2->ts.type == BT_DERIVED
4284
                  && derived_pointer (code->expr2->ts.derived))
4285
                {
4286
                  gfc_error
4287
                    ("Right side of assignment at %L is a derived type "
4288
                     "containing a POINTER in a PURE procedure",
4289
                     &code->expr2->where);
4290
                  break;
4291
                }
4292
            }
4293
 
4294
          gfc_check_assign (code->expr, code->expr2, 1);
4295
          break;
4296
 
4297
        case EXEC_LABEL_ASSIGN:
4298
          if (code->label->defined == ST_LABEL_UNKNOWN)
4299
            gfc_error ("Label %d referenced at %L is never defined",
4300
                       code->label->value, &code->label->where);
4301
          if (t == SUCCESS
4302
              && (code->expr->expr_type != EXPR_VARIABLE
4303
                  || code->expr->symtree->n.sym->ts.type != BT_INTEGER
4304
                  || code->expr->symtree->n.sym->ts.kind
4305
                        != gfc_default_integer_kind
4306
                  || code->expr->symtree->n.sym->as != NULL))
4307
            gfc_error ("ASSIGN statement at %L requires a scalar "
4308
                       "default INTEGER variable", &code->expr->where);
4309
          break;
4310
 
4311
        case EXEC_POINTER_ASSIGN:
4312
          if (t == FAILURE)
4313
            break;
4314
 
4315
          gfc_check_pointer_assign (code->expr, code->expr2);
4316
          break;
4317
 
4318
        case EXEC_ARITHMETIC_IF:
4319
          if (t == SUCCESS
4320
              && code->expr->ts.type != BT_INTEGER
4321
              && code->expr->ts.type != BT_REAL)
4322
            gfc_error ("Arithmetic IF statement at %L requires a numeric "
4323
                       "expression", &code->expr->where);
4324
 
4325
          resolve_branch (code->label, code);
4326
          resolve_branch (code->label2, code);
4327
          resolve_branch (code->label3, code);
4328
          break;
4329
 
4330
        case EXEC_IF:
4331
          if (t == SUCCESS && code->expr != NULL
4332
              && (code->expr->ts.type != BT_LOGICAL
4333
                  || code->expr->rank != 0))
4334
            gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
4335
                       &code->expr->where);
4336
          break;
4337
 
4338
        case EXEC_CALL:
4339
        call:
4340
          resolve_call (code);
4341
          break;
4342
 
4343
        case EXEC_SELECT:
4344
          /* Select is complicated. Also, a SELECT construct could be
4345
             a transformed computed GOTO.  */
4346
          resolve_select (code);
4347
          break;
4348
 
4349
        case EXEC_DO:
4350
          if (code->ext.iterator != NULL)
4351
            gfc_resolve_iterator (code->ext.iterator, true);
4352
          break;
4353
 
4354
        case EXEC_DO_WHILE:
4355
          if (code->expr == NULL)
4356
            gfc_internal_error ("resolve_code(): No expression on DO WHILE");
4357
          if (t == SUCCESS
4358
              && (code->expr->rank != 0
4359
                  || code->expr->ts.type != BT_LOGICAL))
4360
            gfc_error ("Exit condition of DO WHILE loop at %L must be "
4361
                       "a scalar LOGICAL expression", &code->expr->where);
4362
          break;
4363
 
4364
        case EXEC_ALLOCATE:
4365
          if (t == SUCCESS && code->expr != NULL
4366
              && code->expr->ts.type != BT_INTEGER)
4367
            gfc_error ("STAT tag in ALLOCATE statement at %L must be "
4368
                       "of type INTEGER", &code->expr->where);
4369
 
4370
          for (a = code->ext.alloc_list; a; a = a->next)
4371
            resolve_allocate_expr (a->expr, code);
4372
 
4373
          break;
4374
 
4375
        case EXEC_DEALLOCATE:
4376
          if (t == SUCCESS && code->expr != NULL
4377
              && code->expr->ts.type != BT_INTEGER)
4378
            gfc_error
4379
              ("STAT tag in DEALLOCATE statement at %L must be of type "
4380
               "INTEGER", &code->expr->where);
4381
 
4382
          for (a = code->ext.alloc_list; a; a = a->next)
4383
            resolve_deallocate_expr (a->expr);
4384
 
4385
          break;
4386
 
4387
        case EXEC_OPEN:
4388
          if (gfc_resolve_open (code->ext.open) == FAILURE)
4389
            break;
4390
 
4391
          resolve_branch (code->ext.open->err, code);
4392
          break;
4393
 
4394
        case EXEC_CLOSE:
4395
          if (gfc_resolve_close (code->ext.close) == FAILURE)
4396
            break;
4397
 
4398
          resolve_branch (code->ext.close->err, code);
4399
          break;
4400
 
4401
        case EXEC_BACKSPACE:
4402
        case EXEC_ENDFILE:
4403
        case EXEC_REWIND:
4404
        case EXEC_FLUSH:
4405
          if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
4406
            break;
4407
 
4408
          resolve_branch (code->ext.filepos->err, code);
4409
          break;
4410
 
4411
        case EXEC_INQUIRE:
4412
          if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4413
              break;
4414
 
4415
          resolve_branch (code->ext.inquire->err, code);
4416
          break;
4417
 
4418
        case EXEC_IOLENGTH:
4419
          gcc_assert (code->ext.inquire != NULL);
4420
          if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4421
            break;
4422
 
4423
          resolve_branch (code->ext.inquire->err, code);
4424
          break;
4425
 
4426
        case EXEC_READ:
4427
        case EXEC_WRITE:
4428
          if (gfc_resolve_dt (code->ext.dt) == FAILURE)
4429
            break;
4430
 
4431
          resolve_branch (code->ext.dt->err, code);
4432
          resolve_branch (code->ext.dt->end, code);
4433
          resolve_branch (code->ext.dt->eor, code);
4434
          break;
4435
 
4436
        case EXEC_TRANSFER:
4437
          resolve_transfer (code);
4438
          break;
4439
 
4440
        case EXEC_FORALL:
4441
          resolve_forall_iterators (code->ext.forall_iterator);
4442
 
4443
          if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
4444
            gfc_error
4445
              ("FORALL mask clause at %L requires a LOGICAL expression",
4446
               &code->expr->where);
4447
          break;
4448
 
4449
        default:
4450
          gfc_internal_error ("resolve_code(): Bad statement code");
4451
        }
4452
    }
4453
 
4454
  cs_base = frame.prev;
4455
}
4456
 
4457
 
4458
/* Resolve initial values and make sure they are compatible with
4459
   the variable.  */
4460
 
4461
static void
4462
resolve_values (gfc_symbol * sym)
4463
{
4464
 
4465
  if (sym->value == NULL)
4466
    return;
4467
 
4468
  if (gfc_resolve_expr (sym->value) == FAILURE)
4469
    return;
4470
 
4471
  gfc_check_assign_symbol (sym, sym->value);
4472
}
4473
 
4474
 
4475
/* Resolve an index expression.  */
4476
 
4477
static try
4478
resolve_index_expr (gfc_expr * e)
4479
{
4480
 
4481
  if (gfc_resolve_expr (e) == FAILURE)
4482
    return FAILURE;
4483
 
4484
  if (gfc_simplify_expr (e, 0) == FAILURE)
4485
    return FAILURE;
4486
 
4487
  if (gfc_specification_expr (e) == FAILURE)
4488
    return FAILURE;
4489
 
4490
  return SUCCESS;
4491
}
4492
 
4493
/* Resolve a charlen structure.  */
4494
 
4495
static try
4496
resolve_charlen (gfc_charlen *cl)
4497
{
4498
  if (cl->resolved)
4499
    return SUCCESS;
4500
 
4501
  cl->resolved = 1;
4502
 
4503
  if (resolve_index_expr (cl->length) == FAILURE)
4504
    return FAILURE;
4505
 
4506
  return SUCCESS;
4507
}
4508
 
4509
 
4510
/* Test for non-constant shape arrays. */
4511
 
4512
static bool
4513
is_non_constant_shape_array (gfc_symbol *sym)
4514
{
4515
  gfc_expr *e;
4516
  int i;
4517
 
4518
  if (sym->as != NULL)
4519
    {
4520
      /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
4521
         has not been simplified; parameter array references.  Do the
4522
         simplification now.  */
4523
      for (i = 0; i < sym->as->rank; i++)
4524
        {
4525
          e = sym->as->lower[i];
4526
          if (e && (resolve_index_expr (e) == FAILURE
4527
                || !gfc_is_constant_expr (e)))
4528
            return true;
4529
 
4530
          e = sym->as->upper[i];
4531
          if (e && (resolve_index_expr (e) == FAILURE
4532
                || !gfc_is_constant_expr (e)))
4533
            return true;
4534
        }
4535
    }
4536
  return false;
4537
}
4538
 
4539
/* Resolution of common features of flavors variable and procedure. */
4540
 
4541
static try
4542
resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
4543
{
4544
  /* Constraints on deferred shape variable.  */
4545
  if (sym->as == NULL || sym->as->type != AS_DEFERRED)
4546
    {
4547
      if (sym->attr.allocatable)
4548
        {
4549
          if (sym->attr.dimension)
4550
            gfc_error ("Allocatable array '%s' at %L must have "
4551
                       "a deferred shape", sym->name, &sym->declared_at);
4552
          else
4553
            gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
4554
                       sym->name, &sym->declared_at);
4555
            return FAILURE;
4556
        }
4557
 
4558
      if (sym->attr.pointer && sym->attr.dimension)
4559
        {
4560
          gfc_error ("Array pointer '%s' at %L must have a deferred shape",
4561
                     sym->name, &sym->declared_at);
4562
          return FAILURE;
4563
        }
4564
 
4565
    }
4566
  else
4567
    {
4568
      if (!mp_flag && !sym->attr.allocatable
4569
             && !sym->attr.pointer && !sym->attr.dummy)
4570
        {
4571
          gfc_error ("Array '%s' at %L cannot have a deferred shape",
4572
                     sym->name, &sym->declared_at);
4573
          return FAILURE;
4574
         }
4575
    }
4576
  return SUCCESS;
4577
}
4578
 
4579
/* Resolve symbols with flavor variable.  */
4580
 
4581
static try
4582
resolve_fl_variable (gfc_symbol *sym, int mp_flag)
4583
{
4584
  int flag;
4585
  int i;
4586
  gfc_expr *e;
4587
  gfc_expr *constructor_expr;
4588
 
4589
  if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
4590
    return FAILURE;
4591
 
4592
  /* The shape of a main program or module array needs to be constant.  */
4593
  if (sym->ns->proc_name
4594
        && (sym->ns->proc_name->attr.flavor == FL_MODULE
4595
             || sym->ns->proc_name->attr.is_main_program)
4596
        && !sym->attr.use_assoc
4597
        && !sym->attr.allocatable
4598
        && !sym->attr.pointer
4599
        && is_non_constant_shape_array (sym))
4600
    {
4601
       gfc_error ("The module or main program array '%s' at %L must "
4602
                     "have constant shape", sym->name, &sym->declared_at);
4603
          return FAILURE;
4604
    }
4605
 
4606
  if (sym->ts.type == BT_CHARACTER)
4607
    {
4608
      /* Make sure that character string variables with assumed length are
4609
         dummy arguments.  */
4610
      e = sym->ts.cl->length;
4611
      if (e == NULL && !sym->attr.dummy && !sym->attr.result)
4612
        {
4613
          gfc_error ("Entity with assumed character length at %L must be a "
4614
                     "dummy argument or a PARAMETER", &sym->declared_at);
4615
          return FAILURE;
4616
        }
4617
 
4618
      if (!gfc_is_constant_expr (e)
4619
            && !(e->expr_type == EXPR_VARIABLE
4620
            && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
4621
            && sym->ns->proc_name
4622
            && (sym->ns->proc_name->attr.flavor == FL_MODULE
4623
                  || sym->ns->proc_name->attr.is_main_program)
4624
            && !sym->attr.use_assoc)
4625
        {
4626
          gfc_error ("'%s' at %L must have constant character length "
4627
                     "in this context", sym->name, &sym->declared_at);
4628
          return FAILURE;
4629
        }
4630
    }
4631
 
4632
  /* Can the symbol have an initializer?  */
4633
  flag = 0;
4634
  if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
4635
        || sym->attr.intrinsic || sym->attr.result)
4636
    flag = 1;
4637
  else if (sym->attr.dimension && !sym->attr.pointer)
4638
    {
4639
      /* Don't allow initialization of automatic arrays.  */
4640
      for (i = 0; i < sym->as->rank; i++)
4641
        {
4642
          if (sym->as->lower[i] == NULL
4643
                || sym->as->lower[i]->expr_type != EXPR_CONSTANT
4644
                || sym->as->upper[i] == NULL
4645
                || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
4646
            {
4647
              flag = 1;
4648
              break;
4649
            }
4650
        }
4651
  }
4652
 
4653
  /* Reject illegal initializers.  */
4654
  if (sym->value && flag)
4655
    {
4656
      if (sym->attr.allocatable)
4657
        gfc_error ("Allocatable '%s' at %L cannot have an initializer",
4658
                   sym->name, &sym->declared_at);
4659
      else if (sym->attr.external)
4660
        gfc_error ("External '%s' at %L cannot have an initializer",
4661
                   sym->name, &sym->declared_at);
4662
      else if (sym->attr.dummy)
4663
        gfc_error ("Dummy '%s' at %L cannot have an initializer",
4664
                   sym->name, &sym->declared_at);
4665
      else if (sym->attr.intrinsic)
4666
        gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
4667
                   sym->name, &sym->declared_at);
4668
      else if (sym->attr.result)
4669
        gfc_error ("Function result '%s' at %L cannot have an initializer",
4670
                   sym->name, &sym->declared_at);
4671
      else
4672
        gfc_error ("Automatic array '%s' at %L cannot have an initializer",
4673
                   sym->name, &sym->declared_at);
4674
      return FAILURE;
4675
    }
4676
 
4677
  /* 4th constraint in section 11.3:  "If an object of a type for which
4678
     component-initialization is specified (R429) appears in the
4679
     specification-part of a module and does not have the ALLOCATABLE
4680
     or POINTER attribute, the object shall have the SAVE attribute."  */
4681
 
4682
  constructor_expr = NULL;
4683
  if (sym->ts.type == BT_DERIVED && !(sym->value || flag))
4684
        constructor_expr = gfc_default_initializer (&sym->ts);
4685
 
4686
  if (sym->ns->proc_name
4687
        && sym->ns->proc_name->attr.flavor == FL_MODULE
4688
        && constructor_expr
4689
        && !sym->ns->save_all && !sym->attr.save
4690
        && !sym->attr.pointer && !sym->attr.allocatable)
4691
    {
4692
      gfc_error("Object '%s' at %L must have the SAVE attribute %s",
4693
                sym->name, &sym->declared_at,
4694
                "for default initialization of a component");
4695
      return FAILURE;
4696
    }
4697
 
4698
  /* Assign default initializer.  */
4699
  if (sym->ts.type == BT_DERIVED && !(sym->value || flag)
4700
        && !sym->attr.pointer)
4701
    sym->value = gfc_default_initializer (&sym->ts);
4702
 
4703
  return SUCCESS;
4704
}
4705
 
4706
 
4707
/* Resolve a procedure.  */
4708
 
4709
static try
4710
resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
4711
{
4712
  gfc_formal_arglist *arg;
4713
 
4714
  if (sym->attr.function
4715
        && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
4716
    return FAILURE;
4717
 
4718
  if (sym->attr.proc == PROC_ST_FUNCTION)
4719
    {
4720
      if (sym->ts.type == BT_CHARACTER)
4721
        {
4722
          gfc_charlen *cl = sym->ts.cl;
4723
          if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
4724
            {
4725
              gfc_error ("Character-valued statement function '%s' at %L must "
4726
                         "have constant length", sym->name, &sym->declared_at);
4727
              return FAILURE;
4728
            }
4729
        }
4730
    }
4731
 
4732
  /* Ensure that derived type for are not of a private type.  Internal
4733
     module procedures are excluded by 2.2.3.3 - ie. they are not
4734
     externally accessible and can access all the objects accesible in
4735
     the host. */
4736
  if (!(sym->ns->parent
4737
            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
4738
        && gfc_check_access(sym->attr.access, sym->ns->default_access))
4739
    {
4740
      for (arg = sym->formal; arg; arg = arg->next)
4741
        {
4742
          if (arg->sym
4743
                && arg->sym->ts.type == BT_DERIVED
4744
                && !arg->sym->ts.derived->attr.use_assoc
4745
                && !gfc_check_access(arg->sym->ts.derived->attr.access,
4746
                        arg->sym->ts.derived->ns->default_access))
4747
            {
4748
              gfc_error_now ("'%s' is of a PRIVATE type and cannot be "
4749
                             "a dummy argument of '%s', which is "
4750
                             "PUBLIC at %L", arg->sym->name, sym->name,
4751
                             &sym->declared_at);
4752
              /* Stop this message from recurring.  */
4753
              arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
4754
              return FAILURE;
4755
            }
4756
        }
4757
    }
4758
 
4759
  /* An external symbol may not have an intializer because it is taken to be
4760
     a procedure.  */
4761
  if (sym->attr.external && sym->value)
4762
    {
4763
      gfc_error ("External object '%s' at %L may not have an initializer",
4764
                 sym->name, &sym->declared_at);
4765
      return FAILURE;
4766
    }
4767
 
4768
  /* 5.1.1.5 of the Standard: A function name declared with an asterisk
4769
     char-len-param shall not be array-valued, pointer-valued, recursive
4770
     or pure.  ....snip... A character value of * may only be used in the
4771
     following ways: (i) Dummy arg of procedure - dummy associates with
4772
     actual length; (ii) To declare a named constant; or (iii) External
4773
     function - but length must be declared in calling scoping unit.  */
4774
  if (sym->attr.function
4775
        && sym->ts.type == BT_CHARACTER
4776
        && sym->ts.cl && sym->ts.cl->length == NULL)
4777
    {
4778
      if ((sym->as && sym->as->rank) || (sym->attr.pointer)
4779
             || (sym->attr.recursive) || (sym->attr.pure))
4780
        {
4781
          if (sym->as && sym->as->rank)
4782
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4783
                       "array-valued", sym->name, &sym->declared_at);
4784
 
4785
          if (sym->attr.pointer)
4786
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4787
                       "pointer-valued", sym->name, &sym->declared_at);
4788
 
4789
          if (sym->attr.pure)
4790
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4791
                       "pure", sym->name, &sym->declared_at);
4792
 
4793
          if (sym->attr.recursive)
4794
            gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4795
                       "recursive", sym->name, &sym->declared_at);
4796
 
4797
          return FAILURE;
4798
        }
4799
 
4800
      /* Appendix B.2 of the standard.  Contained functions give an
4801
         error anyway.  Fixed-form is likely to be F77/legacy.  */
4802
      if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
4803
        gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
4804
                        "'%s' at %L is obsolescent in fortran 95",
4805
                        sym->name, &sym->declared_at);
4806
    }
4807
  return SUCCESS;
4808
}
4809
 
4810
 
4811
/* Resolve the components of a derived type.  */
4812
 
4813
static try
4814
resolve_fl_derived (gfc_symbol *sym)
4815
{
4816
  gfc_component *c;
4817
  gfc_dt_list * dt_list;
4818
  int i;
4819
 
4820
  for (c = sym->components; c != NULL; c = c->next)
4821
    {
4822
      if (c->ts.type == BT_CHARACTER)
4823
        {
4824
         if (c->ts.cl->length == NULL
4825
             || (resolve_charlen (c->ts.cl) == FAILURE)
4826
             || !gfc_is_constant_expr (c->ts.cl->length))
4827
           {
4828
             gfc_error ("Character length of component '%s' needs to "
4829
                        "be a constant specification expression at %L.",
4830
                        c->name,
4831
                        c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
4832
             return FAILURE;
4833
           }
4834
        }
4835
 
4836
      if (c->ts.type == BT_DERIVED
4837
            && sym->component_access != ACCESS_PRIVATE
4838
            && gfc_check_access(sym->attr.access, sym->ns->default_access)
4839
            && !c->ts.derived->attr.use_assoc
4840
            && !gfc_check_access(c->ts.derived->attr.access,
4841
                                 c->ts.derived->ns->default_access))
4842
        {
4843
          gfc_error ("The component '%s' is a PRIVATE type and cannot be "
4844
                     "a component of '%s', which is PUBLIC at %L",
4845
                      c->name, sym->name, &sym->declared_at);
4846
          return FAILURE;
4847
        }
4848
 
4849
      if (c->pointer || c->as == NULL)
4850
        continue;
4851
 
4852
      for (i = 0; i < c->as->rank; i++)
4853
        {
4854
          if (c->as->lower[i] == NULL
4855
                || !gfc_is_constant_expr (c->as->lower[i])
4856
                || (resolve_index_expr (c->as->lower[i]) == FAILURE)
4857
                || c->as->upper[i] == NULL
4858
                || (resolve_index_expr (c->as->upper[i]) == FAILURE)
4859
                || !gfc_is_constant_expr (c->as->upper[i]))
4860
            {
4861
              gfc_error ("Component '%s' of '%s' at %L must have "
4862
                         "constant array bounds.",
4863
                         c->name, sym->name, &c->loc);
4864
              return FAILURE;
4865
            }
4866
        }
4867
    }
4868
 
4869
  /* Add derived type to the derived type list.  */
4870
  dt_list = gfc_get_dt_list ();
4871
  dt_list->next = sym->ns->derived_types;
4872
  dt_list->derived = sym;
4873
  sym->ns->derived_types = dt_list;
4874
 
4875
  return SUCCESS;
4876
}
4877
 
4878
 
4879
static try
4880
resolve_fl_namelist (gfc_symbol *sym)
4881
{
4882
  gfc_namelist *nl;
4883
  gfc_symbol *nlsym;
4884
 
4885
  /* Reject PRIVATE objects in a PUBLIC namelist.  */
4886
  if (gfc_check_access(sym->attr.access, sym->ns->default_access))
4887
    {
4888
      for (nl = sym->namelist; nl; nl = nl->next)
4889
        {
4890
          if (!nl->sym->attr.use_assoc
4891
                && !(sym->ns->parent == nl->sym->ns)
4892
                       && !gfc_check_access(nl->sym->attr.access,
4893
                                            nl->sym->ns->default_access))
4894
            {
4895
              gfc_error ("PRIVATE symbol '%s' cannot be member of "
4896
                         "PUBLIC namelist at %L", nl->sym->name,
4897
                         &sym->declared_at);
4898
              return FAILURE;
4899
            }
4900
        }
4901
    }
4902
 
4903
    /* Reject namelist arrays that are not constant shape.  */
4904
    for (nl = sym->namelist; nl; nl = nl->next)
4905
      {
4906
        if (is_non_constant_shape_array (nl->sym))
4907
          {
4908
            gfc_error ("The array '%s' must have constant shape to be "
4909
                       "a NAMELIST object at %L", nl->sym->name,
4910
                       &sym->declared_at);
4911
            return FAILURE;
4912
          }
4913
    }
4914
 
4915
  /* 14.1.2 A module or internal procedure represent local entities
4916
     of the same type as a namelist member and so are not allowed.
4917
     Note that this is sometimes caught by check_conflict so the
4918
     same message has been used.  */
4919
  for (nl = sym->namelist; nl; nl = nl->next)
4920
    {
4921
      nlsym = NULL;
4922
        if (sym->ns->parent && nl->sym && nl->sym->name)
4923
          gfc_find_symbol (nl->sym->name, sym->ns->parent, 0, &nlsym);
4924
        if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
4925
          {
4926
            gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
4927
                       "attribute in '%s' at %L", nlsym->name,
4928
                       &sym->declared_at);
4929
            return FAILURE;
4930
          }
4931
    }
4932
 
4933
  return SUCCESS;
4934
}
4935
 
4936
 
4937
static try
4938
resolve_fl_parameter (gfc_symbol *sym)
4939
{
4940
  /* A parameter array's shape needs to be constant.  */
4941
  if (sym->as != NULL && !gfc_is_compile_time_shape (sym->as))
4942
    {
4943
      gfc_error ("Parameter array '%s' at %L cannot be automatic "
4944
                 "or assumed shape", sym->name, &sym->declared_at);
4945
      return FAILURE;
4946
    }
4947
 
4948
  /* Make sure a parameter that has been implicitly typed still
4949
     matches the implicit type, since PARAMETER statements can precede
4950
     IMPLICIT statements.  */
4951
  if (sym->attr.implicit_type
4952
        && !gfc_compare_types (&sym->ts,
4953
                               gfc_get_default_type (sym, sym->ns)))
4954
    {
4955
      gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
4956
                 "later IMPLICIT type", sym->name, &sym->declared_at);
4957
      return FAILURE;
4958
    }
4959
 
4960
  /* Make sure the types of derived parameters are consistent.  This
4961
     type checking is deferred until resolution because the type may
4962
     refer to a derived type from the host.  */
4963
  if (sym->ts.type == BT_DERIVED
4964
        && !gfc_compare_types (&sym->ts, &sym->value->ts))
4965
    {
4966
      gfc_error ("Incompatible derived type in PARAMETER at %L",
4967
                 &sym->value->where);
4968
      return FAILURE;
4969
    }
4970
  return SUCCESS;
4971
}
4972
 
4973
 
4974
/* Do anything necessary to resolve a symbol.  Right now, we just
4975
   assume that an otherwise unknown symbol is a variable.  This sort
4976
   of thing commonly happens for symbols in module.  */
4977
 
4978
static void
4979
resolve_symbol (gfc_symbol * sym)
4980
{
4981
  /* Zero if we are checking a formal namespace.  */
4982
  static int formal_ns_flag = 1;
4983
  int formal_ns_save, check_constant, mp_flag;
4984
  gfc_symtree *symtree;
4985
  gfc_symtree *this_symtree;
4986
  gfc_namespace *ns;
4987
  gfc_component *c;
4988
 
4989
  if (sym->attr.flavor == FL_UNKNOWN)
4990
    {
4991
 
4992
    /* If we find that a flavorless symbol is an interface in one of the
4993
       parent namespaces, find its symtree in this namespace, free the
4994
       symbol and set the symtree to point to the interface symbol.  */
4995
      for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
4996
        {
4997
          symtree = gfc_find_symtree (ns->sym_root, sym->name);
4998
          if (symtree && symtree->n.sym->generic)
4999
            {
5000
              this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
5001
                                               sym->name);
5002
              sym->refs--;
5003
              if (!sym->refs)
5004
                gfc_free_symbol (sym);
5005
              symtree->n.sym->refs++;
5006
              this_symtree->n.sym = symtree->n.sym;
5007
              return;
5008
            }
5009
        }
5010
 
5011
      /* Otherwise give it a flavor according to such attributes as
5012
         it has.  */
5013
      if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
5014
        sym->attr.flavor = FL_VARIABLE;
5015
      else
5016
        {
5017
          sym->attr.flavor = FL_PROCEDURE;
5018
          if (sym->attr.dimension)
5019
            sym->attr.function = 1;
5020
        }
5021
    }
5022
 
5023
  if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
5024
    return;
5025
 
5026
  /* Symbols that are module procedures with results (functions) have
5027
     the types and array specification copied for type checking in
5028
     procedures that call them, as well as for saving to a module
5029
     file.  These symbols can't stand the scrutiny that their results
5030
     can.  */
5031
  mp_flag = (sym->result != NULL && sym->result != sym);
5032
 
5033
  /* Assign default type to symbols that need one and don't have one.  */
5034
  if (sym->ts.type == BT_UNKNOWN)
5035
    {
5036
      if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
5037
        gfc_set_default_type (sym, 1, NULL);
5038
 
5039
      if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
5040
        {
5041
          /* The specific case of an external procedure should emit an error
5042
             in the case that there is no implicit type.  */
5043
          if (!mp_flag)
5044
            gfc_set_default_type (sym, sym->attr.external, NULL);
5045
          else
5046
            {
5047
              /* Result may be in another namespace.  */
5048
              resolve_symbol (sym->result);
5049
 
5050
              sym->ts = sym->result->ts;
5051
              sym->as = gfc_copy_array_spec (sym->result->as);
5052
              sym->attr.dimension = sym->result->attr.dimension;
5053
              sym->attr.pointer = sym->result->attr.pointer;
5054
            }
5055
        }
5056
    }
5057
 
5058
  /* Assumed size arrays and assumed shape arrays must be dummy
5059
     arguments.  */
5060
 
5061
  if (sym->as != NULL
5062
      && (sym->as->type == AS_ASSUMED_SIZE
5063
          || sym->as->type == AS_ASSUMED_SHAPE)
5064
      && sym->attr.dummy == 0)
5065
    {
5066
      if (sym->as->type == AS_ASSUMED_SIZE)
5067
        gfc_error ("Assumed size array at %L must be a dummy argument",
5068
                   &sym->declared_at);
5069
      else
5070
        gfc_error ("Assumed shape array at %L must be a dummy argument",
5071
                   &sym->declared_at);
5072
      return;
5073
    }
5074
 
5075
  /* Make sure symbols with known intent or optional are really dummy
5076
     variable.  Because of ENTRY statement, this has to be deferred
5077
     until resolution time.  */
5078
 
5079
  if (!sym->attr.dummy
5080
      && (sym->attr.optional
5081
          || sym->attr.intent != INTENT_UNKNOWN))
5082
    {
5083
      gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
5084
      return;
5085
    }
5086
 
5087
  /* If a derived type symbol has reached this point, without its
5088
     type being declared, we have an error.  Notice that most
5089
     conditions that produce undefined derived types have already
5090
     been dealt with.  However, the likes of:
5091
     implicit type(t) (t) ..... call foo (t) will get us here if
5092
     the type is not declared in the scope of the implicit
5093
     statement. Change the type to BT_UNKNOWN, both because it is so
5094
     and to prevent an ICE.  */
5095
  if (sym->ts.type == BT_DERIVED
5096
        && sym->ts.derived->components == NULL)
5097
    {
5098
      gfc_error ("The derived type '%s' at %L is of type '%s', "
5099
                 "which has not been defined.", sym->name,
5100
                  &sym->declared_at, sym->ts.derived->name);
5101
      sym->ts.type = BT_UNKNOWN;
5102
      return;
5103
    }
5104
 
5105
  /* An assumed-size array with INTENT(OUT) shall not be of a type for which
5106
     default initialization is defined (5.1.2.4.4).  */
5107
  if (sym->ts.type == BT_DERIVED
5108
        && sym->attr.dummy
5109
        && sym->attr.intent == INTENT_OUT
5110
        && sym->as
5111
        && sym->as->type == AS_ASSUMED_SIZE)
5112
    {
5113
      for (c = sym->ts.derived->components; c; c = c->next)
5114
        {
5115
          if (c->initializer)
5116
            {
5117
              gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
5118
                         "ASSUMED SIZE and so cannot have a default initializer",
5119
                         sym->name, &sym->declared_at);
5120
              return;
5121
            }
5122
        }
5123
    }
5124
 
5125
  switch (sym->attr.flavor)
5126
    {
5127
    case FL_VARIABLE:
5128
      if (resolve_fl_variable (sym, mp_flag) == FAILURE)
5129
        return;
5130
      break;
5131
 
5132
    case FL_PROCEDURE:
5133
      if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
5134
        return;
5135
      break;
5136
 
5137
    case FL_NAMELIST:
5138
      if (resolve_fl_namelist (sym) == FAILURE)
5139
        return;
5140
      break;
5141
 
5142
    case FL_PARAMETER:
5143
      if (resolve_fl_parameter (sym) == FAILURE)
5144
        return;
5145
 
5146
      break;
5147
 
5148
    default:
5149
 
5150
      break;
5151
    }
5152
 
5153
  /* Make sure that intrinsic exist */
5154
  if (sym->attr.intrinsic
5155
      && ! gfc_intrinsic_name(sym->name, 0)
5156
      && ! gfc_intrinsic_name(sym->name, 1))
5157
    gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
5158
 
5159
  /* Resolve array specifier. Check as well some constraints
5160
     on COMMON blocks.  */
5161
 
5162
  check_constant = sym->attr.in_common && !sym->attr.pointer;
5163
  gfc_resolve_array_spec (sym->as, check_constant);
5164
 
5165
  /* Resolve formal namespaces.  */
5166
 
5167
  if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
5168
    {
5169
      formal_ns_save = formal_ns_flag;
5170
      formal_ns_flag = 0;
5171
      gfc_resolve (sym->formal_ns);
5172
      formal_ns_flag = formal_ns_save;
5173
    }
5174
}
5175
 
5176
 
5177
 
5178
/************* Resolve DATA statements *************/
5179
 
5180
static struct
5181
{
5182
  gfc_data_value *vnode;
5183
  unsigned int left;
5184
}
5185
values;
5186
 
5187
 
5188
/* Advance the values structure to point to the next value in the data list.  */
5189
 
5190
static try
5191
next_data_value (void)
5192
{
5193
  while (values.left == 0)
5194
    {
5195
      if (values.vnode->next == NULL)
5196
        return FAILURE;
5197
 
5198
      values.vnode = values.vnode->next;
5199
      values.left = values.vnode->repeat;
5200
    }
5201
 
5202
  return SUCCESS;
5203
}
5204
 
5205
 
5206
static try
5207
check_data_variable (gfc_data_variable * var, locus * where)
5208
{
5209
  gfc_expr *e;
5210
  mpz_t size;
5211
  mpz_t offset;
5212
  try t;
5213
  ar_type mark = AR_UNKNOWN;
5214
  int i;
5215
  mpz_t section_index[GFC_MAX_DIMENSIONS];
5216
  gfc_ref *ref;
5217
  gfc_array_ref *ar;
5218
 
5219
  if (gfc_resolve_expr (var->expr) == FAILURE)
5220
    return FAILURE;
5221
 
5222
  ar = NULL;
5223
  mpz_init_set_si (offset, 0);
5224
  e = var->expr;
5225
 
5226
  if (e->expr_type != EXPR_VARIABLE)
5227
    gfc_internal_error ("check_data_variable(): Bad expression");
5228
 
5229
  if (e->symtree->n.sym->ns->is_block_data
5230
        && !e->symtree->n.sym->attr.in_common)
5231
    {
5232
      gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
5233
                 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
5234
    }
5235
 
5236
  if (e->rank == 0)
5237
    {
5238
      mpz_init_set_ui (size, 1);
5239
      ref = NULL;
5240
    }
5241
  else
5242
    {
5243
      ref = e->ref;
5244
 
5245
      /* Find the array section reference.  */
5246
      for (ref = e->ref; ref; ref = ref->next)
5247
        {
5248
          if (ref->type != REF_ARRAY)
5249
            continue;
5250
          if (ref->u.ar.type == AR_ELEMENT)
5251
            continue;
5252
          break;
5253
        }
5254
      gcc_assert (ref);
5255
 
5256
      /* Set marks according to the reference pattern.  */
5257
      switch (ref->u.ar.type)
5258
        {
5259
        case AR_FULL:
5260
          mark = AR_FULL;
5261
          break;
5262
 
5263
        case AR_SECTION:
5264
          ar = &ref->u.ar;
5265
          /* Get the start position of array section.  */
5266
          gfc_get_section_index (ar, section_index, &offset);
5267
          mark = AR_SECTION;
5268
          break;
5269
 
5270
        default:
5271
          gcc_unreachable ();
5272
        }
5273
 
5274
      if (gfc_array_size (e, &size) == FAILURE)
5275
        {
5276
          gfc_error ("Nonconstant array section at %L in DATA statement",
5277
                     &e->where);
5278
          mpz_clear (offset);
5279
          return FAILURE;
5280
        }
5281
    }
5282
 
5283
  t = SUCCESS;
5284
 
5285
  while (mpz_cmp_ui (size, 0) > 0)
5286
    {
5287
      if (next_data_value () == FAILURE)
5288
        {
5289
          gfc_error ("DATA statement at %L has more variables than values",
5290
                     where);
5291
          t = FAILURE;
5292
          break;
5293
        }
5294
 
5295
      t = gfc_check_assign (var->expr, values.vnode->expr, 0);
5296
      if (t == FAILURE)
5297
        break;
5298
 
5299
      /* If we have more than one element left in the repeat count,
5300
         and we have more than one element left in the target variable,
5301
         then create a range assignment.  */
5302
      /* ??? Only done for full arrays for now, since array sections
5303
         seem tricky.  */
5304
      if (mark == AR_FULL && ref && ref->next == NULL
5305
          && values.left > 1 && mpz_cmp_ui (size, 1) > 0)
5306
        {
5307
          mpz_t range;
5308
 
5309
          if (mpz_cmp_ui (size, values.left) >= 0)
5310
            {
5311
              mpz_init_set_ui (range, values.left);
5312
              mpz_sub_ui (size, size, values.left);
5313
              values.left = 0;
5314
            }
5315
          else
5316
            {
5317
              mpz_init_set (range, size);
5318
              values.left -= mpz_get_ui (size);
5319
              mpz_set_ui (size, 0);
5320
            }
5321
 
5322
          gfc_assign_data_value_range (var->expr, values.vnode->expr,
5323
                                       offset, range);
5324
 
5325
          mpz_add (offset, offset, range);
5326
          mpz_clear (range);
5327
        }
5328
 
5329
      /* Assign initial value to symbol.  */
5330
      else
5331
        {
5332
          values.left -= 1;
5333
          mpz_sub_ui (size, size, 1);
5334
 
5335
          gfc_assign_data_value (var->expr, values.vnode->expr, offset);
5336
 
5337
          if (mark == AR_FULL)
5338
            mpz_add_ui (offset, offset, 1);
5339
 
5340
          /* Modify the array section indexes and recalculate the offset
5341
             for next element.  */
5342
          else if (mark == AR_SECTION)
5343
            gfc_advance_section (section_index, ar, &offset);
5344
        }
5345
    }
5346
 
5347
  if (mark == AR_SECTION)
5348
    {
5349
      for (i = 0; i < ar->dimen; i++)
5350
        mpz_clear (section_index[i]);
5351
    }
5352
 
5353
  mpz_clear (size);
5354
  mpz_clear (offset);
5355
 
5356
  return t;
5357
}
5358
 
5359
 
5360
static try traverse_data_var (gfc_data_variable *, locus *);
5361
 
5362
/* Iterate over a list of elements in a DATA statement.  */
5363
 
5364
static try
5365
traverse_data_list (gfc_data_variable * var, locus * where)
5366
{
5367
  mpz_t trip;
5368
  iterator_stack frame;
5369
  gfc_expr *e;
5370
 
5371
  mpz_init (frame.value);
5372
 
5373
  mpz_init_set (trip, var->iter.end->value.integer);
5374
  mpz_sub (trip, trip, var->iter.start->value.integer);
5375
  mpz_add (trip, trip, var->iter.step->value.integer);
5376
 
5377
  mpz_div (trip, trip, var->iter.step->value.integer);
5378
 
5379
  mpz_set (frame.value, var->iter.start->value.integer);
5380
 
5381
  frame.prev = iter_stack;
5382
  frame.variable = var->iter.var->symtree;
5383
  iter_stack = &frame;
5384
 
5385
  while (mpz_cmp_ui (trip, 0) > 0)
5386
    {
5387
      if (traverse_data_var (var->list, where) == FAILURE)
5388
        {
5389
          mpz_clear (trip);
5390
          return FAILURE;
5391
        }
5392
 
5393
      e = gfc_copy_expr (var->expr);
5394
      if (gfc_simplify_expr (e, 1) == FAILURE)
5395
        {
5396
          gfc_free_expr (e);
5397
          return FAILURE;
5398
        }
5399
 
5400
      mpz_add (frame.value, frame.value, var->iter.step->value.integer);
5401
 
5402
      mpz_sub_ui (trip, trip, 1);
5403
    }
5404
 
5405
  mpz_clear (trip);
5406
  mpz_clear (frame.value);
5407
 
5408
  iter_stack = frame.prev;
5409
  return SUCCESS;
5410
}
5411
 
5412
 
5413
/* Type resolve variables in the variable list of a DATA statement.  */
5414
 
5415
static try
5416
traverse_data_var (gfc_data_variable * var, locus * where)
5417
{
5418
  try t;
5419
 
5420
  for (; var; var = var->next)
5421
    {
5422
      if (var->expr == NULL)
5423
        t = traverse_data_list (var, where);
5424
      else
5425
        t = check_data_variable (var, where);
5426
 
5427
      if (t == FAILURE)
5428
        return FAILURE;
5429
    }
5430
 
5431
  return SUCCESS;
5432
}
5433
 
5434
 
5435
/* Resolve the expressions and iterators associated with a data statement.
5436
   This is separate from the assignment checking because data lists should
5437
   only be resolved once.  */
5438
 
5439
static try
5440
resolve_data_variables (gfc_data_variable * d)
5441
{
5442
  for (; d; d = d->next)
5443
    {
5444
      if (d->list == NULL)
5445
        {
5446
          if (gfc_resolve_expr (d->expr) == FAILURE)
5447
            return FAILURE;
5448
        }
5449
      else
5450
        {
5451
          if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
5452
            return FAILURE;
5453
 
5454
          if (d->iter.start->expr_type != EXPR_CONSTANT
5455
              || d->iter.end->expr_type != EXPR_CONSTANT
5456
              || d->iter.step->expr_type != EXPR_CONSTANT)
5457
            gfc_internal_error ("resolve_data_variables(): Bad iterator");
5458
 
5459
          if (resolve_data_variables (d->list) == FAILURE)
5460
            return FAILURE;
5461
        }
5462
    }
5463
 
5464
  return SUCCESS;
5465
}
5466
 
5467
 
5468
/* Resolve a single DATA statement.  We implement this by storing a pointer to
5469
   the value list into static variables, and then recursively traversing the
5470
   variables list, expanding iterators and such.  */
5471
 
5472
static void
5473
resolve_data (gfc_data * d)
5474
{
5475
  if (resolve_data_variables (d->var) == FAILURE)
5476
    return;
5477
 
5478
  values.vnode = d->value;
5479
  values.left = (d->value == NULL) ? 0 : d->value->repeat;
5480
 
5481
  if (traverse_data_var (d->var, &d->where) == FAILURE)
5482
    return;
5483
 
5484
  /* At this point, we better not have any values left.  */
5485
 
5486
  if (next_data_value () == SUCCESS)
5487
    gfc_error ("DATA statement at %L has more values than variables",
5488
               &d->where);
5489
}
5490
 
5491
 
5492
/* Determines if a variable is not 'pure', ie not assignable within a pure
5493
   procedure.  Returns zero if assignment is OK, nonzero if there is a problem.
5494
 */
5495
 
5496
int
5497
gfc_impure_variable (gfc_symbol * sym)
5498
{
5499
  if (sym->attr.use_assoc || sym->attr.in_common)
5500
    return 1;
5501
 
5502
  if (sym->ns != gfc_current_ns)
5503
    return !sym->attr.function;
5504
 
5505
  /* TODO: Check storage association through EQUIVALENCE statements */
5506
 
5507
  return 0;
5508
}
5509
 
5510
 
5511
/* Test whether a symbol is pure or not.  For a NULL pointer, checks the
5512
   symbol of the current procedure.  */
5513
 
5514
int
5515
gfc_pure (gfc_symbol * sym)
5516
{
5517
  symbol_attribute attr;
5518
 
5519
  if (sym == NULL)
5520
    sym = gfc_current_ns->proc_name;
5521
  if (sym == NULL)
5522
    return 0;
5523
 
5524
  attr = sym->attr;
5525
 
5526
  return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
5527
}
5528
 
5529
 
5530
/* Test whether the current procedure is elemental or not.  */
5531
 
5532
int
5533
gfc_elemental (gfc_symbol * sym)
5534
{
5535
  symbol_attribute attr;
5536
 
5537
  if (sym == NULL)
5538
    sym = gfc_current_ns->proc_name;
5539
  if (sym == NULL)
5540
    return 0;
5541
  attr = sym->attr;
5542
 
5543
  return attr.flavor == FL_PROCEDURE && attr.elemental;
5544
}
5545
 
5546
 
5547
/* Warn about unused labels.  */
5548
 
5549
static void
5550
warn_unused_label (gfc_namespace * ns)
5551
{
5552
  gfc_st_label *l;
5553
 
5554
  l = ns->st_labels;
5555
  if (l == NULL)
5556
    return;
5557
 
5558
  while (l->next)
5559
    l = l->next;
5560
 
5561
  for (; l; l = l->prev)
5562
    {
5563
      if (l->defined == ST_LABEL_UNKNOWN)
5564
        continue;
5565
 
5566
      switch (l->referenced)
5567
        {
5568
        case ST_LABEL_UNKNOWN:
5569
          gfc_warning ("Label %d at %L defined but not used", l->value,
5570
                       &l->where);
5571
          break;
5572
 
5573
        case ST_LABEL_BAD_TARGET:
5574
          gfc_warning ("Label %d at %L defined but cannot be used", l->value,
5575
                       &l->where);
5576
          break;
5577
 
5578
        default:
5579
          break;
5580
        }
5581
    }
5582
}
5583
 
5584
 
5585
/* Returns the sequence type of a symbol or sequence.  */
5586
 
5587
static seq_type
5588
sequence_type (gfc_typespec ts)
5589
{
5590
  seq_type result;
5591
  gfc_component *c;
5592
 
5593
  switch (ts.type)
5594
  {
5595
    case BT_DERIVED:
5596
 
5597
      if (ts.derived->components == NULL)
5598
        return SEQ_NONDEFAULT;
5599
 
5600
      result = sequence_type (ts.derived->components->ts);
5601
      for (c = ts.derived->components->next; c; c = c->next)
5602
        if (sequence_type (c->ts) != result)
5603
          return SEQ_MIXED;
5604
 
5605
      return result;
5606
 
5607
    case BT_CHARACTER:
5608
      if (ts.kind != gfc_default_character_kind)
5609
          return SEQ_NONDEFAULT;
5610
 
5611
      return SEQ_CHARACTER;
5612
 
5613
    case BT_INTEGER:
5614
      if (ts.kind != gfc_default_integer_kind)
5615
          return SEQ_NONDEFAULT;
5616
 
5617
      return SEQ_NUMERIC;
5618
 
5619
    case BT_REAL:
5620
      if (!(ts.kind == gfc_default_real_kind
5621
             || ts.kind == gfc_default_double_kind))
5622
          return SEQ_NONDEFAULT;
5623
 
5624
      return SEQ_NUMERIC;
5625
 
5626
    case BT_COMPLEX:
5627
      if (ts.kind != gfc_default_complex_kind)
5628
          return SEQ_NONDEFAULT;
5629
 
5630
      return SEQ_NUMERIC;
5631
 
5632
    case BT_LOGICAL:
5633
      if (ts.kind != gfc_default_logical_kind)
5634
          return SEQ_NONDEFAULT;
5635
 
5636
      return SEQ_NUMERIC;
5637
 
5638
    default:
5639
      return SEQ_NONDEFAULT;
5640
  }
5641
}
5642
 
5643
 
5644
/* Resolve derived type EQUIVALENCE object.  */
5645
 
5646
static try
5647
resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
5648
{
5649
  gfc_symbol *d;
5650
  gfc_component *c = derived->components;
5651
 
5652
  if (!derived)
5653
    return SUCCESS;
5654
 
5655
  /* Shall not be an object of nonsequence derived type.  */
5656
  if (!derived->attr.sequence)
5657
    {
5658
      gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
5659
                 "attribute to be an EQUIVALENCE object", sym->name, &e->where);
5660
      return FAILURE;
5661
    }
5662
 
5663
  for (; c ; c = c->next)
5664
    {
5665
      d = c->ts.derived;
5666
      if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
5667
        return FAILURE;
5668
 
5669
      /* Shall not be an object of sequence derived type containing a pointer
5670
         in the structure.  */
5671
      if (c->pointer)
5672
        {
5673
          gfc_error ("Derived type variable '%s' at %L with pointer component(s) "
5674
                     "cannot be an EQUIVALENCE object", sym->name, &e->where);
5675
          return FAILURE;
5676
        }
5677
 
5678
      if (c->initializer)
5679
        {
5680
          gfc_error ("Derived type variable '%s' at %L with default initializer "
5681
                     "cannot be an EQUIVALENCE object", sym->name, &e->where);
5682
          return FAILURE;
5683
        }
5684
    }
5685
  return SUCCESS;
5686
}
5687
 
5688
 
5689
/* Resolve equivalence object.
5690
   An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
5691
   an allocatable array, an object of nonsequence derived type, an object of
5692
   sequence derived type containing a pointer at any level of component
5693
   selection, an automatic object, a function name, an entry name, a result
5694
   name, a named constant, a structure component, or a subobject of any of
5695
   the preceding objects.  A substring shall not have length zero.  A
5696
   derived type shall not have components with default initialization nor
5697
   shall two objects of an equivalence group be initialized.
5698
   The simple constraints are done in symbol.c(check_conflict) and the rest
5699
   are implemented here.  */
5700
 
5701
static void
5702
resolve_equivalence (gfc_equiv *eq)
5703
{
5704
  gfc_symbol *sym;
5705
  gfc_symbol *derived;
5706
  gfc_symbol *first_sym;
5707
  gfc_expr *e;
5708
  gfc_ref *r;
5709
  locus *last_where = NULL;
5710
  seq_type eq_type, last_eq_type;
5711
  gfc_typespec *last_ts;
5712
  int object;
5713
  const char *value_name;
5714
  const char *msg;
5715
 
5716
  value_name = NULL;
5717
  last_ts = &eq->expr->symtree->n.sym->ts;
5718
 
5719
  first_sym = eq->expr->symtree->n.sym;
5720
 
5721
  for (object = 1; eq; eq = eq->eq, object++)
5722
    {
5723
      e = eq->expr;
5724
 
5725
      e->ts = e->symtree->n.sym->ts;
5726
      /* match_varspec might not know yet if it is seeing
5727
         array reference or substring reference, as it doesn't
5728
         know the types.  */
5729
      if (e->ref && e->ref->type == REF_ARRAY)
5730
        {
5731
          gfc_ref *ref = e->ref;
5732
          sym = e->symtree->n.sym;
5733
 
5734
          if (sym->attr.dimension)
5735
            {
5736
              ref->u.ar.as = sym->as;
5737
              ref = ref->next;
5738
            }
5739
 
5740
          /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
5741
          if (e->ts.type == BT_CHARACTER
5742
              && ref
5743
              && ref->type == REF_ARRAY
5744
              && ref->u.ar.dimen == 1
5745
              && ref->u.ar.dimen_type[0] == DIMEN_RANGE
5746
              && ref->u.ar.stride[0] == NULL)
5747
            {
5748
              gfc_expr *start = ref->u.ar.start[0];
5749
              gfc_expr *end = ref->u.ar.end[0];
5750
              void *mem = NULL;
5751
 
5752
              /* Optimize away the (:) reference.  */
5753
              if (start == NULL && end == NULL)
5754
                {
5755
                  if (e->ref == ref)
5756
                    e->ref = ref->next;
5757
                  else
5758
                    e->ref->next = ref->next;
5759
                  mem = ref;
5760
                }
5761
              else
5762
                {
5763
                  ref->type = REF_SUBSTRING;
5764
                  if (start == NULL)
5765
                    start = gfc_int_expr (1);
5766
                  ref->u.ss.start = start;
5767
                  if (end == NULL && e->ts.cl)
5768
                    end = gfc_copy_expr (e->ts.cl->length);
5769
                  ref->u.ss.end = end;
5770
                  ref->u.ss.length = e->ts.cl;
5771
                  e->ts.cl = NULL;
5772
                }
5773
              ref = ref->next;
5774
              gfc_free (mem);
5775
            }
5776
 
5777
          /* Any further ref is an error.  */
5778
          if (ref)
5779
            {
5780
              gcc_assert (ref->type == REF_ARRAY);
5781
              gfc_error ("Syntax error in EQUIVALENCE statement at %L",
5782
                         &ref->u.ar.where);
5783
              continue;
5784
            }
5785
        }
5786
 
5787
      if (gfc_resolve_expr (e) == FAILURE)
5788
        continue;
5789
 
5790
      sym = e->symtree->n.sym;
5791
 
5792
      /* An equivalence statement cannot have more than one initialized
5793
         object.  */
5794
      if (sym->value)
5795
        {
5796
          if (value_name != NULL)
5797
            {
5798
              gfc_error ("Initialized objects '%s' and '%s'  cannot both "
5799
                         "be in the EQUIVALENCE statement at %L",
5800
                         value_name, sym->name, &e->where);
5801
              continue;
5802
            }
5803
          else
5804
            value_name = sym->name;
5805
        }
5806
 
5807
      /* Shall not equivalence common block variables in a PURE procedure.  */
5808
      if (sym->ns->proc_name
5809
            && sym->ns->proc_name->attr.pure
5810
            && sym->attr.in_common)
5811
        {
5812
          gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
5813
                     "object in the pure procedure '%s'",
5814
                     sym->name, &e->where, sym->ns->proc_name->name);
5815
          break;
5816
        }
5817
 
5818
      /* Shall not be a named constant.  */
5819
      if (e->expr_type == EXPR_CONSTANT)
5820
        {
5821
          gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
5822
                     "object", sym->name, &e->where);
5823
          continue;
5824
        }
5825
 
5826
      derived = e->ts.derived;
5827
      if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
5828
        continue;
5829
 
5830
      /* Check that the types correspond correctly:
5831
         Note 5.28:
5832
         A numeric sequence structure may be equivalenced to another sequence
5833
         structure, an object of default integer type, default real type, double
5834
         precision real type, default logical type such that components of the
5835
         structure ultimately only become associated to objects of the same
5836
         kind. A character sequence structure may be equivalenced to an object
5837
         of default character kind or another character sequence structure.
5838
         Other objects may be equivalenced only to objects of the same type and
5839
         kind parameters.  */
5840
 
5841
      /* Identical types are unconditionally OK.  */
5842
      if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
5843
        goto identical_types;
5844
 
5845
      last_eq_type = sequence_type (*last_ts);
5846
      eq_type = sequence_type (sym->ts);
5847
 
5848
      /* Since the pair of objects is not of the same type, mixed or
5849
         non-default sequences can be rejected.  */
5850
 
5851
      msg = "Sequence %s with mixed components in EQUIVALENCE "
5852
            "statement at %L with different type objects";
5853
      if ((object ==2
5854
               && last_eq_type == SEQ_MIXED
5855
               && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
5856
                                  last_where) == FAILURE)
5857
           ||  (eq_type == SEQ_MIXED
5858
               && gfc_notify_std (GFC_STD_GNU, msg,sym->name,
5859
                                  &e->where) == FAILURE))
5860
        continue;
5861
 
5862
      msg = "Non-default type object or sequence %s in EQUIVALENCE "
5863
            "statement at %L with objects of different type";
5864
      if ((object ==2
5865
               && last_eq_type == SEQ_NONDEFAULT
5866
               && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
5867
                                  last_where) == FAILURE)
5868
           ||  (eq_type == SEQ_NONDEFAULT
5869
               && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5870
                                  &e->where) == FAILURE))
5871
        continue;
5872
 
5873
      msg ="Non-CHARACTER object '%s' in default CHARACTER "
5874
           "EQUIVALENCE statement at %L";
5875
      if (last_eq_type == SEQ_CHARACTER
5876
            && eq_type != SEQ_CHARACTER
5877
            && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5878
                                  &e->where) == FAILURE)
5879
                continue;
5880
 
5881
      msg ="Non-NUMERIC object '%s' in default NUMERIC "
5882
           "EQUIVALENCE statement at %L";
5883
      if (last_eq_type == SEQ_NUMERIC
5884
            && eq_type != SEQ_NUMERIC
5885
            && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5886
                                  &e->where) == FAILURE)
5887
                continue;
5888
 
5889
  identical_types:
5890
      last_ts =&sym->ts;
5891
      last_where = &e->where;
5892
 
5893
      if (!e->ref)
5894
        continue;
5895
 
5896
      /* Shall not be an automatic array.  */
5897
      if (e->ref->type == REF_ARRAY
5898
          && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
5899
        {
5900
          gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
5901
                     "an EQUIVALENCE object", sym->name, &e->where);
5902
          continue;
5903
        }
5904
 
5905
      r = e->ref;
5906
      while (r)
5907
        {
5908
          /* Shall not be a structure component.  */
5909
          if (r->type == REF_COMPONENT)
5910
            {
5911
              gfc_error ("Structure component '%s' at %L cannot be an "
5912
                         "EQUIVALENCE object",
5913
                         r->u.c.component->name, &e->where);
5914
              break;
5915
            }
5916
 
5917
          /* A substring shall not have length zero.  */
5918
          if (r->type == REF_SUBSTRING)
5919
            {
5920
              if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
5921
                {
5922
                  gfc_error ("Substring at %L has length zero",
5923
                             &r->u.ss.start->where);
5924
                  break;
5925
                }
5926
            }
5927
          r = r->next;
5928
        }
5929
    }
5930
}
5931
 
5932
 
5933
/* Resolve function and ENTRY types, issue diagnostics if needed. */
5934
 
5935
static void
5936
resolve_fntype (gfc_namespace * ns)
5937
{
5938
  gfc_entry_list *el;
5939
  gfc_symbol *sym;
5940
 
5941
  if (ns->proc_name == NULL || !ns->proc_name->attr.function)
5942
    return;
5943
 
5944
  /* If there are any entries, ns->proc_name is the entry master
5945
     synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
5946
  if (ns->entries)
5947
    sym = ns->entries->sym;
5948
  else
5949
    sym = ns->proc_name;
5950
  if (sym->result == sym
5951
      && sym->ts.type == BT_UNKNOWN
5952
      && gfc_set_default_type (sym, 0, NULL) == FAILURE
5953
      && !sym->attr.untyped)
5954
    {
5955
      gfc_error ("Function '%s' at %L has no IMPLICIT type",
5956
                 sym->name, &sym->declared_at);
5957
      sym->attr.untyped = 1;
5958
    }
5959
 
5960
  if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
5961
      && !gfc_check_access (sym->ts.derived->attr.access,
5962
                            sym->ts.derived->ns->default_access)
5963
      && gfc_check_access (sym->attr.access, sym->ns->default_access))
5964
    {
5965
      gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
5966
                 sym->name, &sym->declared_at, sym->ts.derived->name);
5967
    }
5968
 
5969
  if (ns->entries)
5970
    for (el = ns->entries->next; el; el = el->next)
5971
      {
5972
        if (el->sym->result == el->sym
5973
            && el->sym->ts.type == BT_UNKNOWN
5974
            && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
5975
            && !el->sym->attr.untyped)
5976
          {
5977
            gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
5978
                       el->sym->name, &el->sym->declared_at);
5979
            el->sym->attr.untyped = 1;
5980
          }
5981
      }
5982
}
5983
 
5984
/* 12.3.2.1.1 Defined operators.  */
5985
 
5986
static void
5987
gfc_resolve_uops(gfc_symtree *symtree)
5988
{
5989
  gfc_interface *itr;
5990
  gfc_symbol *sym;
5991
  gfc_formal_arglist *formal;
5992
 
5993
  if (symtree == NULL)
5994
    return;
5995
 
5996
  gfc_resolve_uops (symtree->left);
5997
  gfc_resolve_uops (symtree->right);
5998
 
5999
  for (itr = symtree->n.uop->operator; itr; itr = itr->next)
6000
    {
6001
      sym = itr->sym;
6002
      if (!sym->attr.function)
6003
        gfc_error("User operator procedure '%s' at %L must be a FUNCTION",
6004
                  sym->name, &sym->declared_at);
6005
 
6006
      if (sym->ts.type == BT_CHARACTER
6007
            && !(sym->ts.cl && sym->ts.cl->length)
6008
            && !(sym->result && sym->result->ts.cl && sym->result->ts.cl->length))
6009
        gfc_error("User operator procedure '%s' at %L cannot be assumed character "
6010
                  "length", sym->name, &sym->declared_at);
6011
 
6012
      formal = sym->formal;
6013
      if (!formal || !formal->sym)
6014
        {
6015
          gfc_error("User operator procedure '%s' at %L must have at least "
6016
                    "one argument", sym->name, &sym->declared_at);
6017
          continue;
6018
        }
6019
 
6020
      if (formal->sym->attr.intent != INTENT_IN)
6021
        gfc_error ("First argument of operator interface at %L must be "
6022
                   "INTENT(IN)", &sym->declared_at);
6023
 
6024
      if (formal->sym->attr.optional)
6025
        gfc_error ("First argument of operator interface at %L cannot be "
6026
                   "optional", &sym->declared_at);
6027
 
6028
      formal = formal->next;
6029
      if (!formal || !formal->sym)
6030
        continue;
6031
 
6032
      if (formal->sym->attr.intent != INTENT_IN)
6033
        gfc_error ("Second argument of operator interface at %L must be "
6034
                   "INTENT(IN)", &sym->declared_at);
6035
 
6036
      if (formal->sym->attr.optional)
6037
        gfc_error ("Second argument of operator interface at %L cannot be "
6038
                   "optional", &sym->declared_at);
6039
 
6040
      if (formal->next)
6041
        gfc_error ("Operator interface at %L must have, at most, two "
6042
                   "arguments", &sym->declared_at);
6043
    }
6044
}
6045
 
6046
 
6047
/* Examine all of the expressions associated with a program unit,
6048
   assign types to all intermediate expressions, make sure that all
6049
   assignments are to compatible types and figure out which names
6050
   refer to which functions or subroutines.  It doesn't check code
6051
   block, which is handled by resolve_code.  */
6052
 
6053
static void
6054
resolve_types (gfc_namespace * ns)
6055
{
6056
  gfc_namespace *n;
6057
  gfc_charlen *cl;
6058
  gfc_data *d;
6059
  gfc_equiv *eq;
6060
 
6061
  gfc_current_ns = ns;
6062
 
6063
  resolve_entries (ns);
6064
 
6065
  resolve_contained_functions (ns);
6066
 
6067
  gfc_traverse_ns (ns, resolve_symbol);
6068
 
6069
  resolve_fntype (ns);
6070
 
6071
  for (n = ns->contained; n; n = n->sibling)
6072
    {
6073
      if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
6074
        gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
6075
                   "also be PURE", n->proc_name->name,
6076
                   &n->proc_name->declared_at);
6077
 
6078
      resolve_types (n);
6079
    }
6080
 
6081
  forall_flag = 0;
6082
  gfc_check_interfaces (ns);
6083
 
6084
  for (cl = ns->cl_list; cl; cl = cl->next)
6085
    resolve_charlen (cl);
6086
 
6087
  gfc_traverse_ns (ns, resolve_values);
6088
 
6089
  if (ns->save_all)
6090
    gfc_save_all (ns);
6091
 
6092
  iter_stack = NULL;
6093
  for (d = ns->data; d; d = d->next)
6094
    resolve_data (d);
6095
 
6096
  iter_stack = NULL;
6097
  gfc_traverse_ns (ns, gfc_formalize_init_value);
6098
 
6099
  for (eq = ns->equiv; eq; eq = eq->next)
6100
    resolve_equivalence (eq);
6101
 
6102
  /* Warn about unused labels.  */
6103
  if (gfc_option.warn_unused_labels)
6104
    warn_unused_label (ns);
6105
 
6106
  gfc_resolve_uops (ns->uop_root);
6107
}
6108
 
6109
 
6110
/* Call resolve_code recursively.  */
6111
 
6112
static void
6113
resolve_codes (gfc_namespace * ns)
6114
{
6115
  gfc_namespace *n;
6116
 
6117
  for (n = ns->contained; n; n = n->sibling)
6118
    resolve_codes (n);
6119
 
6120
  gfc_current_ns = ns;
6121
  cs_base = NULL;
6122
  resolve_code (ns->code, ns);
6123
}
6124
 
6125
 
6126
/* This function is called after a complete program unit has been compiled.
6127
   Its purpose is to examine all of the expressions associated with a program
6128
   unit, assign types to all intermediate expressions, make sure that all
6129
   assignments are to compatible types and figure out which names refer to
6130
   which functions or subroutines.  */
6131
 
6132
void
6133
gfc_resolve (gfc_namespace * ns)
6134
{
6135
  gfc_namespace *old_ns;
6136
 
6137
  old_ns = gfc_current_ns;
6138
 
6139
  resolve_types (ns);
6140
  resolve_codes (ns);
6141
 
6142
  gfc_current_ns = old_ns;
6143
}

powered by: WebSVN 2.1.0

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