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

Subversion Repositories scarts

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Deal with interfaces.
2
   Copyright (C) 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
3
   Contributed by Andy Vaught
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 2, or (at your option) any later
10
version.
11
 
12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15
for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING.  If not, write to the Free
19
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.  */
21
 
22
 
23
/* Deal with interfaces.  An explicit interface is represented as a
24
   singly linked list of formal argument structures attached to the
25
   relevant symbols.  For an implicit interface, the arguments don't
26
   point to symbols.  Explicit interfaces point to namespaces that
27
   contain the symbols within that interface.
28
 
29
   Implicit interfaces are linked together in a singly linked list
30
   along the next_if member of symbol nodes.  Since a particular
31
   symbol can only have a single explicit interface, the symbol cannot
32
   be part of multiple lists and a single next-member suffices.
33
 
34
   This is not the case for general classes, though.  An operator
35
   definition is independent of just about all other uses and has it's
36
   own head pointer.
37
 
38
   Nameless interfaces:
39
     Nameless interfaces create symbols with explicit interfaces within
40
     the current namespace.  They are otherwise unlinked.
41
 
42
   Generic interfaces:
43
     The generic name points to a linked list of symbols.  Each symbol
44
     has an explicit interface.  Each explicit interface has its own
45
     namespace containing the arguments.  Module procedures are symbols in
46
     which the interface is added later when the module procedure is parsed.
47
 
48
   User operators:
49
     User-defined operators are stored in a their own set of symtrees
50
     separate from regular symbols.  The symtrees point to gfc_user_op
51
     structures which in turn head up a list of relevant interfaces.
52
 
53
   Extended intrinsics and assignment:
54
     The head of these interface lists are stored in the containing namespace.
55
 
56
   Implicit interfaces:
57
     An implicit interface is represented as a singly linked list of
58
     formal argument list structures that don't point to any symbol
59
     nodes -- they just contain types.
60
 
61
 
62
   When a subprogram is defined, the program unit's name points to an
63
   interface as usual, but the link to the namespace is NULL and the
64
   formal argument list points to symbols within the same namespace as
65
   the program unit name.  */
66
 
67
#include "config.h"
68
#include "system.h"
69
#include "gfortran.h"
70
#include "match.h"
71
 
72
 
73
/* The current_interface structure holds information about the
74
   interface currently being parsed.  This structure is saved and
75
   restored during recursive interfaces.  */
76
 
77
gfc_interface_info current_interface;
78
 
79
 
80
/* Free a singly linked list of gfc_interface structures.  */
81
 
82
void
83
gfc_free_interface (gfc_interface * intr)
84
{
85
  gfc_interface *next;
86
 
87
  for (; intr; intr = next)
88
    {
89
      next = intr->next;
90
      gfc_free (intr);
91
    }
92
}
93
 
94
 
95
/* Change the operators unary plus and minus into binary plus and
96
   minus respectively, leaving the rest unchanged.  */
97
 
98
static gfc_intrinsic_op
99
fold_unary (gfc_intrinsic_op operator)
100
{
101
 
102
  switch (operator)
103
    {
104
    case INTRINSIC_UPLUS:
105
      operator = INTRINSIC_PLUS;
106
      break;
107
    case INTRINSIC_UMINUS:
108
      operator = INTRINSIC_MINUS;
109
      break;
110
    default:
111
      break;
112
    }
113
 
114
  return operator;
115
}
116
 
117
 
118
/* Match a generic specification.  Depending on which type of
119
   interface is found, the 'name' or 'operator' pointers may be set.
120
   This subroutine doesn't return MATCH_NO.  */
121
 
122
match
123
gfc_match_generic_spec (interface_type * type,
124
                        char *name,
125
                        gfc_intrinsic_op *operator)
126
{
127
  char buffer[GFC_MAX_SYMBOL_LEN + 1];
128
  match m;
129
  gfc_intrinsic_op i;
130
 
131
  if (gfc_match (" assignment ( = )") == MATCH_YES)
132
    {
133
      *type = INTERFACE_INTRINSIC_OP;
134
      *operator = INTRINSIC_ASSIGN;
135
      return MATCH_YES;
136
    }
137
 
138
  if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
139
    {                           /* Operator i/f */
140
      *type = INTERFACE_INTRINSIC_OP;
141
      *operator = fold_unary (i);
142
      return MATCH_YES;
143
    }
144
 
145
  if (gfc_match (" operator ( ") == MATCH_YES)
146
    {
147
      m = gfc_match_defined_op_name (buffer, 1);
148
      if (m == MATCH_NO)
149
        goto syntax;
150
      if (m != MATCH_YES)
151
        return MATCH_ERROR;
152
 
153
      m = gfc_match_char (')');
154
      if (m == MATCH_NO)
155
        goto syntax;
156
      if (m != MATCH_YES)
157
        return MATCH_ERROR;
158
 
159
      strcpy (name, buffer);
160
      *type = INTERFACE_USER_OP;
161
      return MATCH_YES;
162
    }
163
 
164
  if (gfc_match_name (buffer) == MATCH_YES)
165
    {
166
      strcpy (name, buffer);
167
      *type = INTERFACE_GENERIC;
168
      return MATCH_YES;
169
    }
170
 
171
  *type = INTERFACE_NAMELESS;
172
  return MATCH_YES;
173
 
174
syntax:
175
  gfc_error ("Syntax error in generic specification at %C");
176
  return MATCH_ERROR;
177
}
178
 
179
 
180
/* Match one of the five forms of an interface statement.  */
181
 
182
match
183
gfc_match_interface (void)
184
{
185
  char name[GFC_MAX_SYMBOL_LEN + 1];
186
  interface_type type;
187
  gfc_symbol *sym;
188
  gfc_intrinsic_op operator;
189
  match m;
190
 
191
  m = gfc_match_space ();
192
 
193
  if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR)
194
    return MATCH_ERROR;
195
 
196
 
197
  /* If we're not looking at the end of the statement now, or if this
198
     is not a nameless interface but we did not see a space, punt.  */
199
  if (gfc_match_eos () != MATCH_YES
200
      || (type != INTERFACE_NAMELESS
201
          && m != MATCH_YES))
202
    {
203
      gfc_error
204
        ("Syntax error: Trailing garbage in INTERFACE statement at %C");
205
      return MATCH_ERROR;
206
    }
207
 
208
  current_interface.type = type;
209
 
210
  switch (type)
211
    {
212
    case INTERFACE_GENERIC:
213
      if (gfc_get_symbol (name, NULL, &sym))
214
        return MATCH_ERROR;
215
 
216
      if (!sym->attr.generic
217
          && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
218
        return MATCH_ERROR;
219
 
220
      current_interface.sym = gfc_new_block = sym;
221
      break;
222
 
223
    case INTERFACE_USER_OP:
224
      current_interface.uop = gfc_get_uop (name);
225
      break;
226
 
227
    case INTERFACE_INTRINSIC_OP:
228
      current_interface.op = operator;
229
      break;
230
 
231
    case INTERFACE_NAMELESS:
232
      break;
233
    }
234
 
235
  return MATCH_YES;
236
}
237
 
238
 
239
/* Match the different sort of generic-specs that can be present after
240
   the END INTERFACE itself.  */
241
 
242
match
243
gfc_match_end_interface (void)
244
{
245
  char name[GFC_MAX_SYMBOL_LEN + 1];
246
  interface_type type;
247
  gfc_intrinsic_op operator;
248
  match m;
249
 
250
  m = gfc_match_space ();
251
 
252
  if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR)
253
    return MATCH_ERROR;
254
 
255
  /* If we're not looking at the end of the statement now, or if this
256
     is not a nameless interface but we did not see a space, punt.  */
257
  if (gfc_match_eos () != MATCH_YES
258
      || (type != INTERFACE_NAMELESS
259
          && m != MATCH_YES))
260
    {
261
      gfc_error
262
        ("Syntax error: Trailing garbage in END INTERFACE statement at %C");
263
      return MATCH_ERROR;
264
    }
265
 
266
  m = MATCH_YES;
267
 
268
  switch (current_interface.type)
269
    {
270
    case INTERFACE_NAMELESS:
271
      if (type != current_interface.type)
272
        {
273
          gfc_error ("Expected a nameless interface at %C");
274
          m = MATCH_ERROR;
275
        }
276
 
277
      break;
278
 
279
    case INTERFACE_INTRINSIC_OP:
280
      if (type != current_interface.type || operator != current_interface.op)
281
        {
282
 
283
          if (current_interface.op == INTRINSIC_ASSIGN)
284
            gfc_error ("Expected 'END INTERFACE ASSIGNMENT (=)' at %C");
285
          else
286
            gfc_error ("Expecting 'END INTERFACE OPERATOR (%s)' at %C",
287
                       gfc_op2string (current_interface.op));
288
 
289
          m = MATCH_ERROR;
290
        }
291
 
292
      break;
293
 
294
    case INTERFACE_USER_OP:
295
      /* Comparing the symbol node names is OK because only use-associated
296
         symbols can be renamed.  */
297
      if (type != current_interface.type
298
          || strcmp (current_interface.uop->name, name) != 0)
299
        {
300
          gfc_error ("Expecting 'END INTERFACE OPERATOR (.%s.)' at %C",
301
                     current_interface.uop->name);
302
          m = MATCH_ERROR;
303
        }
304
 
305
      break;
306
 
307
    case INTERFACE_GENERIC:
308
      if (type != current_interface.type
309
          || strcmp (current_interface.sym->name, name) != 0)
310
        {
311
          gfc_error ("Expecting 'END INTERFACE %s' at %C",
312
                     current_interface.sym->name);
313
          m = MATCH_ERROR;
314
        }
315
 
316
      break;
317
    }
318
 
319
  return m;
320
}
321
 
322
 
323
/* Compare two derived types using the criteria in 4.4.2 of the standard,
324
   recursing through gfc_compare_types for the components.  */
325
 
326
int
327
gfc_compare_derived_types (gfc_symbol * derived1, gfc_symbol * derived2)
328
{
329
  gfc_component *dt1, *dt2;
330
 
331
  /* Special case for comparing derived types across namespaces.  If the
332
     true names and module names are the same and the module name is
333
     nonnull, then they are equal.  */
334
  if (strcmp (derived1->name, derived2->name) == 0
335
        && derived1 != NULL && derived2 != NULL
336
        && derived1->module != NULL && derived2->module != NULL
337
        && strcmp (derived1->module, derived2->module) == 0)
338
    return 1;
339
 
340
  /* Compare type via the rules of the standard.  Both types must have
341
     the SEQUENCE attribute to be equal.  */
342
 
343
  if (strcmp (derived1->name, derived2->name))
344
    return 0;
345
 
346
  if (derived1->component_access == ACCESS_PRIVATE
347
        || derived2->component_access == ACCESS_PRIVATE)
348
    return 0;
349
 
350
  if (derived1->attr.sequence == 0 || derived2->attr.sequence == 0)
351
    return 0;
352
 
353
  dt1 = derived1->components;
354
  dt2 = derived2->components;
355
 
356
  /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
357
     simple test can speed things up.  Otherwise, lots of things have to
358
     match.  */
359
  for (;;)
360
    {
361
      if (strcmp (dt1->name, dt2->name) != 0)
362
        return 0;
363
 
364
      if (dt1->pointer != dt2->pointer)
365
        return 0;
366
 
367
      if (dt1->dimension != dt2->dimension)
368
        return 0;
369
 
370
      if (dt1->dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0)
371
        return 0;
372
 
373
      if (gfc_compare_types (&dt1->ts, &dt2->ts) == 0)
374
        return 0;
375
 
376
      dt1 = dt1->next;
377
      dt2 = dt2->next;
378
 
379
      if (dt1 == NULL && dt2 == NULL)
380
        break;
381
      if (dt1 == NULL || dt2 == NULL)
382
        return 0;
383
    }
384
 
385
  return 1;
386
}
387
 
388
/* Compare two typespecs, recursively if necessary.  */
389
 
390
int
391
gfc_compare_types (gfc_typespec * ts1, gfc_typespec * ts2)
392
{
393
 
394
  if (ts1->type != ts2->type)
395
    return 0;
396
  if (ts1->type != BT_DERIVED)
397
    return (ts1->kind == ts2->kind);
398
 
399
  /* Compare derived types.  */
400
  if (ts1->derived == ts2->derived)
401
    return 1;
402
 
403
  return gfc_compare_derived_types (ts1->derived ,ts2->derived);
404
}
405
 
406
 
407
/* Given two symbols that are formal arguments, compare their ranks
408
   and types.  Returns nonzero if they have the same rank and type,
409
   zero otherwise.  */
410
 
411
static int
412
compare_type_rank (gfc_symbol * s1, gfc_symbol * s2)
413
{
414
  int r1, r2;
415
 
416
  r1 = (s1->as != NULL) ? s1->as->rank : 0;
417
  r2 = (s2->as != NULL) ? s2->as->rank : 0;
418
 
419
  if (r1 != r2)
420
    return 0;                    /* Ranks differ */
421
 
422
  return gfc_compare_types (&s1->ts, &s2->ts);
423
}
424
 
425
 
426
static int compare_interfaces (gfc_symbol *, gfc_symbol *, int);
427
 
428
/* Given two symbols that are formal arguments, compare their types
429
   and rank and their formal interfaces if they are both dummy
430
   procedures.  Returns nonzero if the same, zero if different.  */
431
 
432
static int
433
compare_type_rank_if (gfc_symbol * s1, gfc_symbol * s2)
434
{
435
 
436
  if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
437
    return compare_type_rank (s1, s2);
438
 
439
  if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
440
    return 0;
441
 
442
  /* At this point, both symbols are procedures.  */
443
  if ((s1->attr.function == 0 && s1->attr.subroutine == 0)
444
      || (s2->attr.function == 0 && s2->attr.subroutine == 0))
445
    return 0;
446
 
447
  if (s1->attr.function != s2->attr.function
448
      || s1->attr.subroutine != s2->attr.subroutine)
449
    return 0;
450
 
451
  if (s1->attr.function && compare_type_rank (s1, s2) == 0)
452
    return 0;
453
 
454
  return compare_interfaces (s1, s2, 0); /* Recurse! */
455
}
456
 
457
 
458
/* Given a formal argument list and a keyword name, search the list
459
   for that keyword.  Returns the correct symbol node if found, NULL
460
   if not found.  */
461
 
462
static gfc_symbol *
463
find_keyword_arg (const char *name, gfc_formal_arglist * f)
464
{
465
 
466
  for (; f; f = f->next)
467
    if (strcmp (f->sym->name, name) == 0)
468
      return f->sym;
469
 
470
  return NULL;
471
}
472
 
473
 
474
/******** Interface checking subroutines **********/
475
 
476
 
477
/* Given an operator interface and the operator, make sure that all
478
   interfaces for that operator are legal.  */
479
 
480
static void
481
check_operator_interface (gfc_interface * intr, gfc_intrinsic_op operator)
482
{
483
  gfc_formal_arglist *formal;
484
  sym_intent i1, i2;
485
  gfc_symbol *sym;
486
  bt t1, t2;
487
  int args;
488
 
489
  if (intr == NULL)
490
    return;
491
 
492
  args = 0;
493
  t1 = t2 = BT_UNKNOWN;
494
  i1 = i2 = INTENT_UNKNOWN;
495
 
496
  for (formal = intr->sym->formal; formal; formal = formal->next)
497
    {
498
      sym = formal->sym;
499
 
500
      if (args == 0)
501
        {
502
          t1 = sym->ts.type;
503
          i1 = sym->attr.intent;
504
        }
505
      if (args == 1)
506
        {
507
          t2 = sym->ts.type;
508
          i2 = sym->attr.intent;
509
        }
510
      args++;
511
    }
512
 
513
  if (args == 0 || args > 2)
514
    goto num_args;
515
 
516
  sym = intr->sym;
517
 
518
  if (operator == INTRINSIC_ASSIGN)
519
    {
520
      if (!sym->attr.subroutine)
521
        {
522
          gfc_error
523
            ("Assignment operator interface at %L must be a SUBROUTINE",
524
             &intr->where);
525
          return;
526
        }
527
    }
528
  else
529
    {
530
      if (!sym->attr.function)
531
        {
532
          gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
533
                     &intr->where);
534
          return;
535
        }
536
    }
537
 
538
  switch (operator)
539
    {
540
    case INTRINSIC_PLUS:        /* Numeric unary or binary */
541
    case INTRINSIC_MINUS:
542
      if ((args == 1)
543
          && (t1 == BT_INTEGER
544
              || t1 == BT_REAL
545
              || t1 == BT_COMPLEX))
546
        goto bad_repl;
547
 
548
      if ((args == 2)
549
          && (t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX)
550
          && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX))
551
        goto bad_repl;
552
 
553
      break;
554
 
555
    case INTRINSIC_POWER:       /* Binary numeric */
556
    case INTRINSIC_TIMES:
557
    case INTRINSIC_DIVIDE:
558
 
559
    case INTRINSIC_EQ:
560
    case INTRINSIC_NE:
561
      if (args == 1)
562
        goto num_args;
563
 
564
      if ((t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX)
565
          && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX))
566
        goto bad_repl;
567
 
568
      break;
569
 
570
    case INTRINSIC_GE:          /* Binary numeric operators that do not support */
571
    case INTRINSIC_LE:          /* complex numbers */
572
    case INTRINSIC_LT:
573
    case INTRINSIC_GT:
574
      if (args == 1)
575
        goto num_args;
576
 
577
      if ((t1 == BT_INTEGER || t1 == BT_REAL)
578
          && (t2 == BT_INTEGER || t2 == BT_REAL))
579
        goto bad_repl;
580
 
581
      break;
582
 
583
    case INTRINSIC_OR:          /* Binary logical */
584
    case INTRINSIC_AND:
585
    case INTRINSIC_EQV:
586
    case INTRINSIC_NEQV:
587
      if (args == 1)
588
        goto num_args;
589
      if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
590
        goto bad_repl;
591
      break;
592
 
593
    case INTRINSIC_NOT: /* Unary logical */
594
      if (args != 1)
595
        goto num_args;
596
      if (t1 == BT_LOGICAL)
597
        goto bad_repl;
598
      break;
599
 
600
    case INTRINSIC_CONCAT:      /* Binary string */
601
      if (args != 2)
602
        goto num_args;
603
      if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
604
        goto bad_repl;
605
      break;
606
 
607
    case INTRINSIC_ASSIGN:      /* Class by itself */
608
      if (args != 2)
609
        goto num_args;
610
      break;
611
    default:
612
      gfc_internal_error ("check_operator_interface(): Bad operator");
613
    }
614
 
615
  /* Check intents on operator interfaces.  */
616
  if (operator == INTRINSIC_ASSIGN)
617
    {
618
      if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
619
        gfc_error ("First argument of defined assignment at %L must be "
620
                   "INTENT(IN) or INTENT(INOUT)", &intr->where);
621
 
622
      if (i2 != INTENT_IN)
623
        gfc_error ("Second argument of defined assignment at %L must be "
624
                   "INTENT(IN)", &intr->where);
625
    }
626
  else
627
    {
628
      if (i1 != INTENT_IN)
629
        gfc_error ("First argument of operator interface at %L must be "
630
                   "INTENT(IN)", &intr->where);
631
 
632
      if (args == 2 && i2 != INTENT_IN)
633
        gfc_error ("Second argument of operator interface at %L must be "
634
                   "INTENT(IN)", &intr->where);
635
    }
636
 
637
  return;
638
 
639
bad_repl:
640
  gfc_error ("Operator interface at %L conflicts with intrinsic interface",
641
             &intr->where);
642
  return;
643
 
644
num_args:
645
  gfc_error ("Operator interface at %L has the wrong number of arguments",
646
             &intr->where);
647
  return;
648
}
649
 
650
 
651
/* Given a pair of formal argument lists, we see if the two lists can
652
   be distinguished by counting the number of nonoptional arguments of
653
   a given type/rank in f1 and seeing if there are less then that
654
   number of those arguments in f2 (including optional arguments).
655
   Since this test is asymmetric, it has to be called twice to make it
656
   symmetric.  Returns nonzero if the argument lists are incompatible
657
   by this test.  This subroutine implements rule 1 of section
658
   14.1.2.3.  */
659
 
660
static int
661
count_types_test (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
662
{
663
  int rc, ac1, ac2, i, j, k, n1;
664
  gfc_formal_arglist *f;
665
 
666
  typedef struct
667
  {
668
    int flag;
669
    gfc_symbol *sym;
670
  }
671
  arginfo;
672
 
673
  arginfo *arg;
674
 
675
  n1 = 0;
676
 
677
  for (f = f1; f; f = f->next)
678
    n1++;
679
 
680
  /* Build an array of integers that gives the same integer to
681
     arguments of the same type/rank.  */
682
  arg = gfc_getmem (n1 * sizeof (arginfo));
683
 
684
  f = f1;
685
  for (i = 0; i < n1; i++, f = f->next)
686
    {
687
      arg[i].flag = -1;
688
      arg[i].sym = f->sym;
689
    }
690
 
691
  k = 0;
692
 
693
  for (i = 0; i < n1; i++)
694
    {
695
      if (arg[i].flag != -1)
696
        continue;
697
 
698
      if (arg[i].sym->attr.optional)
699
        continue;               /* Skip optional arguments */
700
 
701
      arg[i].flag = k;
702
 
703
      /* Find other nonoptional arguments of the same type/rank.  */
704
      for (j = i + 1; j < n1; j++)
705
        if (!arg[j].sym->attr.optional
706
            && compare_type_rank_if (arg[i].sym, arg[j].sym))
707
          arg[j].flag = k;
708
 
709
      k++;
710
    }
711
 
712
  /* Now loop over each distinct type found in f1.  */
713
  k = 0;
714
  rc = 0;
715
 
716
  for (i = 0; i < n1; i++)
717
    {
718
      if (arg[i].flag != k)
719
        continue;
720
 
721
      ac1 = 1;
722
      for (j = i + 1; j < n1; j++)
723
        if (arg[j].flag == k)
724
          ac1++;
725
 
726
      /* Count the number of arguments in f2 with that type, including
727
         those that are optional.  */
728
      ac2 = 0;
729
 
730
      for (f = f2; f; f = f->next)
731
        if (compare_type_rank_if (arg[i].sym, f->sym))
732
          ac2++;
733
 
734
      if (ac1 > ac2)
735
        {
736
          rc = 1;
737
          break;
738
        }
739
 
740
      k++;
741
    }
742
 
743
  gfc_free (arg);
744
 
745
  return rc;
746
}
747
 
748
 
749
/* Perform the abbreviated correspondence test for operators.  The
750
   arguments cannot be optional and are always ordered correctly,
751
   which makes this test much easier than that for generic tests.
752
 
753
   This subroutine is also used when comparing a formal and actual
754
   argument list when an actual parameter is a dummy procedure.  At
755
   that point, two formal interfaces must be compared for equality
756
   which is what happens here.  */
757
 
758
static int
759
operator_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
760
{
761
  for (;;)
762
    {
763
      if (f1 == NULL && f2 == NULL)
764
        break;
765
      if (f1 == NULL || f2 == NULL)
766
        return 1;
767
 
768
      if (!compare_type_rank (f1->sym, f2->sym))
769
        return 1;
770
 
771
      f1 = f1->next;
772
      f2 = f2->next;
773
    }
774
 
775
  return 0;
776
}
777
 
778
 
779
/* Perform the correspondence test in rule 2 of section 14.1.2.3.
780
   Returns zero if no argument is found that satisfies rule 2, nonzero
781
   otherwise.
782
 
783
   This test is also not symmetric in f1 and f2 and must be called
784
   twice.  This test finds problems caused by sorting the actual
785
   argument list with keywords.  For example:
786
 
787
   INTERFACE FOO
788
       SUBROUTINE F1(A, B)
789
           INTEGER :: A ; REAL :: B
790
       END SUBROUTINE F1
791
 
792
       SUBROUTINE F2(B, A)
793
           INTEGER :: A ; REAL :: B
794
       END SUBROUTINE F1
795
   END INTERFACE FOO
796
 
797
   At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */
798
 
799
static int
800
generic_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
801
{
802
 
803
  gfc_formal_arglist *f2_save, *g;
804
  gfc_symbol *sym;
805
 
806
  f2_save = f2;
807
 
808
  while (f1)
809
    {
810
      if (f1->sym->attr.optional)
811
        goto next;
812
 
813
      if (f2 != NULL && compare_type_rank (f1->sym, f2->sym))
814
        goto next;
815
 
816
      /* Now search for a disambiguating keyword argument starting at
817
         the current non-match.  */
818
      for (g = f1; g; g = g->next)
819
        {
820
          if (g->sym->attr.optional)
821
            continue;
822
 
823
          sym = find_keyword_arg (g->sym->name, f2_save);
824
          if (sym == NULL || !compare_type_rank (g->sym, sym))
825
            return 1;
826
        }
827
 
828
    next:
829
      f1 = f1->next;
830
      if (f2 != NULL)
831
        f2 = f2->next;
832
    }
833
 
834
  return 0;
835
}
836
 
837
 
838
/* 'Compare' two formal interfaces associated with a pair of symbols.
839
   We return nonzero if there exists an actual argument list that
840
   would be ambiguous between the two interfaces, zero otherwise.  */
841
 
842
static int
843
compare_interfaces (gfc_symbol * s1, gfc_symbol * s2, int generic_flag)
844
{
845
  gfc_formal_arglist *f1, *f2;
846
 
847
  if (s1->attr.function != s2->attr.function
848
      && s1->attr.subroutine != s2->attr.subroutine)
849
    return 0;                    /* disagreement between function/subroutine */
850
 
851
  f1 = s1->formal;
852
  f2 = s2->formal;
853
 
854
  if (f1 == NULL && f2 == NULL)
855
    return 1;                   /* Special case */
856
 
857
  if (count_types_test (f1, f2))
858
    return 0;
859
  if (count_types_test (f2, f1))
860
    return 0;
861
 
862
  if (generic_flag)
863
    {
864
      if (generic_correspondence (f1, f2))
865
        return 0;
866
      if (generic_correspondence (f2, f1))
867
        return 0;
868
    }
869
  else
870
    {
871
      if (operator_correspondence (f1, f2))
872
        return 0;
873
    }
874
 
875
  return 1;
876
}
877
 
878
 
879
/* Given a pointer to an interface pointer, remove duplicate
880
   interfaces and make sure that all symbols are either functions or
881
   subroutines.  Returns nonzero if something goes wrong.  */
882
 
883
static int
884
check_interface0 (gfc_interface * p, const char *interface_name)
885
{
886
  gfc_interface *psave, *q, *qlast;
887
 
888
  psave = p;
889
  /* Make sure all symbols in the interface have been defined as
890
     functions or subroutines.  */
891
  for (; p; p = p->next)
892
    if (!p->sym->attr.function && !p->sym->attr.subroutine)
893
      {
894
        gfc_error ("Procedure '%s' in %s at %L is neither function nor "
895
                   "subroutine", p->sym->name, interface_name,
896
                   &p->sym->declared_at);
897
        return 1;
898
      }
899
  p = psave;
900
 
901
  /* Remove duplicate interfaces in this interface list.  */
902
  for (; p; p = p->next)
903
    {
904
      qlast = p;
905
 
906
      for (q = p->next; q;)
907
        {
908
          if (p->sym != q->sym)
909
            {
910
              qlast = q;
911
              q = q->next;
912
 
913
            }
914
          else
915
            {
916
              /* Duplicate interface */
917
              qlast->next = q->next;
918
              gfc_free (q);
919
              q = qlast->next;
920
            }
921
        }
922
    }
923
 
924
  return 0;
925
}
926
 
927
 
928
/* Check lists of interfaces to make sure that no two interfaces are
929
   ambiguous.  Duplicate interfaces (from the same symbol) are OK
930
   here.  */
931
 
932
static int
933
check_interface1 (gfc_interface * p, gfc_interface * q,
934
                  int generic_flag, const char *interface_name)
935
{
936
 
937
  for (; p; p = p->next)
938
    for (; q; q = q->next)
939
      {
940
        if (p->sym == q->sym)
941
          continue;             /* Duplicates OK here */
942
 
943
        if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
944
          continue;
945
 
946
        if (compare_interfaces (p->sym, q->sym, generic_flag))
947
          {
948
            gfc_error ("Ambiguous interfaces '%s' and '%s' in %s at %L",
949
                       p->sym->name, q->sym->name, interface_name, &p->where);
950
            return 1;
951
          }
952
      }
953
 
954
  return 0;
955
}
956
 
957
 
958
/* Check the generic and operator interfaces of symbols to make sure
959
   that none of the interfaces conflict.  The check has to be done
960
   after all of the symbols are actually loaded.  */
961
 
962
static void
963
check_sym_interfaces (gfc_symbol * sym)
964
{
965
  char interface_name[100];
966
  gfc_symbol *s2;
967
 
968
  if (sym->ns != gfc_current_ns)
969
    return;
970
 
971
  if (sym->generic != NULL)
972
    {
973
      sprintf (interface_name, "generic interface '%s'", sym->name);
974
      if (check_interface0 (sym->generic, interface_name))
975
        return;
976
 
977
      s2 = sym;
978
      while (s2 != NULL)
979
        {
980
          if (check_interface1 (sym->generic, s2->generic, 1, interface_name))
981
            return;
982
 
983
          if (s2->ns->parent == NULL)
984
            break;
985
          if (gfc_find_symbol (sym->name, s2->ns->parent, 1, &s2))
986
            break;
987
        }
988
    }
989
}
990
 
991
 
992
static void
993
check_uop_interfaces (gfc_user_op * uop)
994
{
995
  char interface_name[100];
996
  gfc_user_op *uop2;
997
  gfc_namespace *ns;
998
 
999
  sprintf (interface_name, "operator interface '%s'", uop->name);
1000
  if (check_interface0 (uop->operator, interface_name))
1001
    return;
1002
 
1003
  for (ns = gfc_current_ns; ns; ns = ns->parent)
1004
    {
1005
      uop2 = gfc_find_uop (uop->name, ns);
1006
      if (uop2 == NULL)
1007
        continue;
1008
 
1009
      check_interface1 (uop->operator, uop2->operator, 0, interface_name);
1010
    }
1011
}
1012
 
1013
 
1014
/* For the namespace, check generic, user operator and intrinsic
1015
   operator interfaces for consistency and to remove duplicate
1016
   interfaces.  We traverse the whole namespace, counting on the fact
1017
   that most symbols will not have generic or operator interfaces.  */
1018
 
1019
void
1020
gfc_check_interfaces (gfc_namespace * ns)
1021
{
1022
  gfc_namespace *old_ns, *ns2;
1023
  char interface_name[100];
1024
  gfc_intrinsic_op i;
1025
 
1026
  old_ns = gfc_current_ns;
1027
  gfc_current_ns = ns;
1028
 
1029
  gfc_traverse_ns (ns, check_sym_interfaces);
1030
 
1031
  gfc_traverse_user_op (ns, check_uop_interfaces);
1032
 
1033
  for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
1034
    {
1035
      if (i == INTRINSIC_USER)
1036
        continue;
1037
 
1038
      if (i == INTRINSIC_ASSIGN)
1039
        strcpy (interface_name, "intrinsic assignment operator");
1040
      else
1041
        sprintf (interface_name, "intrinsic '%s' operator",
1042
                 gfc_op2string (i));
1043
 
1044
      if (check_interface0 (ns->operator[i], interface_name))
1045
        continue;
1046
 
1047
      check_operator_interface (ns->operator[i], i);
1048
 
1049
      for (ns2 = ns->parent; ns2; ns2 = ns2->parent)
1050
        if (check_interface1 (ns->operator[i], ns2->operator[i], 0,
1051
                              interface_name))
1052
          break;
1053
    }
1054
 
1055
  gfc_current_ns = old_ns;
1056
}
1057
 
1058
 
1059
static int
1060
symbol_rank (gfc_symbol * sym)
1061
{
1062
 
1063
  return (sym->as == NULL) ? 0 : sym->as->rank;
1064
}
1065
 
1066
 
1067
/* Given a symbol of a formal argument list and an expression, if the
1068
   formal argument is a pointer, see if the actual argument is a
1069
   pointer. Returns nonzero if compatible, zero if not compatible.  */
1070
 
1071
static int
1072
compare_pointer (gfc_symbol * formal, gfc_expr * actual)
1073
{
1074
  symbol_attribute attr;
1075
 
1076
  if (formal->attr.pointer)
1077
    {
1078
      attr = gfc_expr_attr (actual);
1079
      if (!attr.pointer)
1080
        return 0;
1081
    }
1082
 
1083
  return 1;
1084
}
1085
 
1086
 
1087
/* Given a symbol of a formal argument list and an expression, see if
1088
   the two are compatible as arguments.  Returns nonzero if
1089
   compatible, zero if not compatible.  */
1090
 
1091
static int
1092
compare_parameter (gfc_symbol * formal, gfc_expr * actual,
1093
                   int ranks_must_agree, int is_elemental)
1094
{
1095
  gfc_ref *ref;
1096
 
1097
  if (actual->ts.type == BT_PROCEDURE)
1098
    {
1099
      if (formal->attr.flavor != FL_PROCEDURE)
1100
        return 0;
1101
 
1102
      if (formal->attr.function
1103
          && !compare_type_rank (formal, actual->symtree->n.sym))
1104
        return 0;
1105
 
1106
      if (formal->attr.if_source == IFSRC_UNKNOWN)
1107
        return 1;               /* Assume match */
1108
 
1109
      return compare_interfaces (formal, actual->symtree->n.sym, 0);
1110
    }
1111
 
1112
  if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
1113
      && !gfc_compare_types (&formal->ts, &actual->ts))
1114
    return 0;
1115
 
1116
  if (symbol_rank (formal) == actual->rank)
1117
    return 1;
1118
 
1119
  /* At this point the ranks didn't agree.  */
1120
  if (ranks_must_agree || formal->attr.pointer)
1121
    return 0;
1122
 
1123
  if (actual->rank != 0)
1124
    return is_elemental || formal->attr.dimension;
1125
 
1126
  /* At this point, we are considering a scalar passed to an array.
1127
     This is legal if the scalar is an array element of the right sort.  */
1128
  if (formal->as->type == AS_ASSUMED_SHAPE)
1129
    return 0;
1130
 
1131
  for (ref = actual->ref; ref; ref = ref->next)
1132
    if (ref->type == REF_SUBSTRING)
1133
      return 0;
1134
 
1135
  for (ref = actual->ref; ref; ref = ref->next)
1136
    if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT)
1137
      break;
1138
 
1139
  if (ref == NULL)
1140
    return 0;                    /* Not an array element */
1141
 
1142
  return 1;
1143
}
1144
 
1145
 
1146
/* Given formal and actual argument lists, see if they are compatible.
1147
   If they are compatible, the actual argument list is sorted to
1148
   correspond with the formal list, and elements for missing optional
1149
   arguments are inserted. If WHERE pointer is nonnull, then we issue
1150
   errors when things don't match instead of just returning the status
1151
   code.  */
1152
 
1153
static int
1154
compare_actual_formal (gfc_actual_arglist ** ap,
1155
                       gfc_formal_arglist * formal,
1156
                       int ranks_must_agree, int is_elemental, locus * where)
1157
{
1158
  gfc_actual_arglist **new, *a, *actual, temp;
1159
  gfc_formal_arglist *f;
1160
  int i, n, na;
1161
  bool rank_check;
1162
 
1163
  actual = *ap;
1164
 
1165
  if (actual == NULL && formal == NULL)
1166
    return 1;
1167
 
1168
  n = 0;
1169
  for (f = formal; f; f = f->next)
1170
    n++;
1171
 
1172
  new = (gfc_actual_arglist **) alloca (n * sizeof (gfc_actual_arglist *));
1173
 
1174
  for (i = 0; i < n; i++)
1175
    new[i] = NULL;
1176
 
1177
  na = 0;
1178
  f = formal;
1179
  i = 0;
1180
 
1181
  for (a = actual; a; a = a->next, f = f->next)
1182
    {
1183
      if (a->name != NULL)
1184
        {
1185
          i = 0;
1186
          for (f = formal; f; f = f->next, i++)
1187
            {
1188
              if (f->sym == NULL)
1189
                continue;
1190
              if (strcmp (f->sym->name, a->name) == 0)
1191
                break;
1192
            }
1193
 
1194
          if (f == NULL)
1195
            {
1196
              if (where)
1197
                gfc_error
1198
                  ("Keyword argument '%s' at %L is not in the procedure",
1199
                   a->name, &a->expr->where);
1200
              return 0;
1201
            }
1202
 
1203
          if (new[i] != NULL)
1204
            {
1205
              if (where)
1206
                gfc_error
1207
                  ("Keyword argument '%s' at %L is already associated "
1208
                   "with another actual argument", a->name, &a->expr->where);
1209
              return 0;
1210
            }
1211
        }
1212
 
1213
      if (f == NULL)
1214
        {
1215
          if (where)
1216
            gfc_error
1217
              ("More actual than formal arguments in procedure call at %L",
1218
               where);
1219
 
1220
          return 0;
1221
        }
1222
 
1223
      if (f->sym == NULL && a->expr == NULL)
1224
        goto match;
1225
 
1226
      if (f->sym == NULL)
1227
        {
1228
          if (where)
1229
            gfc_error
1230
              ("Missing alternate return spec in subroutine call at %L",
1231
               where);
1232
          return 0;
1233
        }
1234
 
1235
      if (a->expr == NULL)
1236
        {
1237
          if (where)
1238
            gfc_error
1239
              ("Unexpected alternate return spec in subroutine call at %L",
1240
               where);
1241
          return 0;
1242
        }
1243
 
1244
      rank_check = where != NULL
1245
                     && !is_elemental
1246
                     && f->sym->as
1247
                     && (f->sym->as->type == AS_ASSUMED_SHAPE
1248
                           || f->sym->as->type == AS_DEFERRED);
1249
 
1250
      if (!compare_parameter
1251
          (f->sym, a->expr, ranks_must_agree || rank_check, is_elemental))
1252
        {
1253
          if (where)
1254
            gfc_error ("Type/rank mismatch in argument '%s' at %L",
1255
                       f->sym->name, &a->expr->where);
1256
          return 0;
1257
        }
1258
 
1259
      if (f->sym->as
1260
          && f->sym->as->type == AS_ASSUMED_SHAPE
1261
          && a->expr->expr_type == EXPR_VARIABLE
1262
          && a->expr->symtree->n.sym->as
1263
          && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
1264
          && (a->expr->ref == NULL
1265
              || (a->expr->ref->type == REF_ARRAY
1266
                  && a->expr->ref->u.ar.type == AR_FULL)))
1267
        {
1268
          if (where)
1269
            gfc_error ("Actual argument for '%s' cannot be an assumed-size"
1270
                       " array at %L", f->sym->name, where);
1271
          return 0;
1272
        }
1273
 
1274
      if (a->expr->expr_type != EXPR_NULL
1275
          && compare_pointer (f->sym, a->expr) == 0)
1276
        {
1277
          if (where)
1278
            gfc_error ("Actual argument for '%s' must be a pointer at %L",
1279
                       f->sym->name, &a->expr->where);
1280
          return 0;
1281
        }
1282
 
1283
      /* Check intent = OUT/INOUT for definable actual argument.  */
1284
      if (a->expr->expr_type != EXPR_VARIABLE
1285
             && (f->sym->attr.intent == INTENT_OUT
1286
                   || f->sym->attr.intent == INTENT_INOUT))
1287
        {
1288
          gfc_error ("Actual argument at %L must be definable to "
1289
                     "match dummy INTENT = OUT/INOUT", &a->expr->where);
1290
          return 0;
1291
        }
1292
 
1293
    match:
1294
      if (a == actual)
1295
        na = i;
1296
 
1297
      new[i++] = a;
1298
    }
1299
 
1300
  /* Make sure missing actual arguments are optional.  */
1301
  i = 0;
1302
  for (f = formal; f; f = f->next, i++)
1303
    {
1304
      if (new[i] != NULL)
1305
        continue;
1306
      if (!f->sym->attr.optional)
1307
        {
1308
          if (where)
1309
            gfc_error ("Missing actual argument for argument '%s' at %L",
1310
                       f->sym->name, where);
1311
          return 0;
1312
        }
1313
    }
1314
 
1315
  /* The argument lists are compatible.  We now relink a new actual
1316
     argument list with null arguments in the right places.  The head
1317
     of the list remains the head.  */
1318
  for (i = 0; i < n; i++)
1319
    if (new[i] == NULL)
1320
      new[i] = gfc_get_actual_arglist ();
1321
 
1322
  if (na != 0)
1323
    {
1324
      temp = *new[0];
1325
      *new[0] = *actual;
1326
      *actual = temp;
1327
 
1328
      a = new[0];
1329
      new[0] = new[na];
1330
      new[na] = a;
1331
    }
1332
 
1333
  for (i = 0; i < n - 1; i++)
1334
    new[i]->next = new[i + 1];
1335
 
1336
  new[i]->next = NULL;
1337
 
1338
  if (*ap == NULL && n > 0)
1339
    *ap = new[0];
1340
 
1341
  /* Note the types of omitted optional arguments.  */
1342
  for (a = actual, f = formal; a; a = a->next, f = f->next)
1343
    if (a->expr == NULL && a->label == NULL)
1344
      a->missing_arg_type = f->sym->ts.type;
1345
 
1346
  return 1;
1347
}
1348
 
1349
 
1350
typedef struct
1351
{
1352
  gfc_formal_arglist *f;
1353
  gfc_actual_arglist *a;
1354
}
1355
argpair;
1356
 
1357
/* qsort comparison function for argument pairs, with the following
1358
   order:
1359
    - p->a->expr == NULL
1360
    - p->a->expr->expr_type != EXPR_VARIABLE
1361
    - growing p->a->expr->symbol.  */
1362
 
1363
static int
1364
pair_cmp (const void *p1, const void *p2)
1365
{
1366
  const gfc_actual_arglist *a1, *a2;
1367
 
1368
  /* *p1 and *p2 are elements of the to-be-sorted array.  */
1369
  a1 = ((const argpair *) p1)->a;
1370
  a2 = ((const argpair *) p2)->a;
1371
  if (!a1->expr)
1372
    {
1373
      if (!a2->expr)
1374
        return 0;
1375
      return -1;
1376
    }
1377
  if (!a2->expr)
1378
    return 1;
1379
  if (a1->expr->expr_type != EXPR_VARIABLE)
1380
    {
1381
      if (a2->expr->expr_type != EXPR_VARIABLE)
1382
        return 0;
1383
      return -1;
1384
    }
1385
  if (a2->expr->expr_type != EXPR_VARIABLE)
1386
    return 1;
1387
  return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
1388
}
1389
 
1390
 
1391
/* Given two expressions from some actual arguments, test whether they
1392
   refer to the same expression. The analysis is conservative.
1393
   Returning FAILURE will produce no warning.  */
1394
 
1395
static try
1396
compare_actual_expr (gfc_expr * e1, gfc_expr * e2)
1397
{
1398
  const gfc_ref *r1, *r2;
1399
 
1400
  if (!e1 || !e2
1401
      || e1->expr_type != EXPR_VARIABLE
1402
      || e2->expr_type != EXPR_VARIABLE
1403
      || e1->symtree->n.sym != e2->symtree->n.sym)
1404
    return FAILURE;
1405
 
1406
  /* TODO: improve comparison, see expr.c:show_ref().  */
1407
  for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
1408
    {
1409
      if (r1->type != r2->type)
1410
        return FAILURE;
1411
      switch (r1->type)
1412
        {
1413
        case REF_ARRAY:
1414
          if (r1->u.ar.type != r2->u.ar.type)
1415
            return FAILURE;
1416
          /* TODO: At the moment, consider only full arrays;
1417
             we could do better.  */
1418
          if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
1419
            return FAILURE;
1420
          break;
1421
 
1422
        case REF_COMPONENT:
1423
          if (r1->u.c.component != r2->u.c.component)
1424
            return FAILURE;
1425
          break;
1426
 
1427
        case REF_SUBSTRING:
1428
          return FAILURE;
1429
 
1430
        default:
1431
          gfc_internal_error ("compare_actual_expr(): Bad component code");
1432
        }
1433
    }
1434
  if (!r1 && !r2)
1435
    return SUCCESS;
1436
  return FAILURE;
1437
}
1438
 
1439
/* Given formal and actual argument lists that correspond to one
1440
   another, check that identical actual arguments aren't not
1441
   associated with some incompatible INTENTs.  */
1442
 
1443
static try
1444
check_some_aliasing (gfc_formal_arglist * f, gfc_actual_arglist * a)
1445
{
1446
  sym_intent f1_intent, f2_intent;
1447
  gfc_formal_arglist *f1;
1448
  gfc_actual_arglist *a1;
1449
  size_t n, i, j;
1450
  argpair *p;
1451
  try t = SUCCESS;
1452
 
1453
  n = 0;
1454
  for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
1455
    {
1456
      if (f1 == NULL && a1 == NULL)
1457
        break;
1458
      if (f1 == NULL || a1 == NULL)
1459
        gfc_internal_error ("check_some_aliasing(): List mismatch");
1460
      n++;
1461
    }
1462
  if (n == 0)
1463
    return t;
1464
  p = (argpair *) alloca (n * sizeof (argpair));
1465
 
1466
  for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
1467
    {
1468
      p[i].f = f1;
1469
      p[i].a = a1;
1470
    }
1471
 
1472
  qsort (p, n, sizeof (argpair), pair_cmp);
1473
 
1474
  for (i = 0; i < n; i++)
1475
    {
1476
      if (!p[i].a->expr
1477
          || p[i].a->expr->expr_type != EXPR_VARIABLE
1478
          || p[i].a->expr->ts.type == BT_PROCEDURE)
1479
        continue;
1480
      f1_intent = p[i].f->sym->attr.intent;
1481
      for (j = i + 1; j < n; j++)
1482
        {
1483
          /* Expected order after the sort.  */
1484
          if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
1485
            gfc_internal_error ("check_some_aliasing(): corrupted data");
1486
 
1487
          /* Are the expression the same?  */
1488
          if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE)
1489
            break;
1490
          f2_intent = p[j].f->sym->attr.intent;
1491
          if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
1492
              || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN))
1493
            {
1494
              gfc_warning ("Same actual argument associated with INTENT(%s) "
1495
                           "argument '%s' and INTENT(%s) argument '%s' at %L",
1496
                           gfc_intent_string (f1_intent), p[i].f->sym->name,
1497
                           gfc_intent_string (f2_intent), p[j].f->sym->name,
1498
                           &p[i].a->expr->where);
1499
              t = FAILURE;
1500
            }
1501
        }
1502
    }
1503
 
1504
  return t;
1505
}
1506
 
1507
 
1508
/* Given formal and actual argument lists that correspond to one
1509
   another, check that they are compatible in the sense that intents
1510
   are not mismatched.  */
1511
 
1512
static try
1513
check_intents (gfc_formal_arglist * f, gfc_actual_arglist * a)
1514
{
1515
  sym_intent a_intent, f_intent;
1516
 
1517
  for (;; f = f->next, a = a->next)
1518
    {
1519
      if (f == NULL && a == NULL)
1520
        break;
1521
      if (f == NULL || a == NULL)
1522
        gfc_internal_error ("check_intents(): List mismatch");
1523
 
1524
      if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE)
1525
        continue;
1526
 
1527
      a_intent = a->expr->symtree->n.sym->attr.intent;
1528
      f_intent = f->sym->attr.intent;
1529
 
1530
      if (a_intent == INTENT_IN
1531
          && (f_intent == INTENT_INOUT
1532
              || f_intent == INTENT_OUT))
1533
        {
1534
 
1535
          gfc_error ("Procedure argument at %L is INTENT(IN) while interface "
1536
                     "specifies INTENT(%s)", &a->expr->where,
1537
                     gfc_intent_string (f_intent));
1538
          return FAILURE;
1539
        }
1540
 
1541
      if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym))
1542
        {
1543
          if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
1544
            {
1545
              gfc_error
1546
                ("Procedure argument at %L is local to a PURE procedure and "
1547
                 "is passed to an INTENT(%s) argument", &a->expr->where,
1548
                 gfc_intent_string (f_intent));
1549
              return FAILURE;
1550
            }
1551
 
1552
          if (a->expr->symtree->n.sym->attr.pointer)
1553
            {
1554
              gfc_error
1555
                ("Procedure argument at %L is local to a PURE procedure and "
1556
                 "has the POINTER attribute", &a->expr->where);
1557
              return FAILURE;
1558
            }
1559
        }
1560
    }
1561
 
1562
  return SUCCESS;
1563
}
1564
 
1565
 
1566
/* Check how a procedure is used against its interface.  If all goes
1567
   well, the actual argument list will also end up being properly
1568
   sorted.  */
1569
 
1570
void
1571
gfc_procedure_use (gfc_symbol * sym, gfc_actual_arglist ** ap, locus * where)
1572
{
1573
 
1574
  /* Warn about calls with an implicit interface.  */
1575
  if (gfc_option.warn_implicit_interface
1576
      && sym->attr.if_source == IFSRC_UNKNOWN)
1577
    gfc_warning ("Procedure '%s' called with an implicit interface at %L",
1578
                 sym->name, where);
1579
 
1580
  if (sym->attr.if_source == IFSRC_UNKNOWN
1581
      || !compare_actual_formal (ap, sym->formal, 0,
1582
                                 sym->attr.elemental, where))
1583
    return;
1584
 
1585
  check_intents (sym->formal, *ap);
1586
  if (gfc_option.warn_aliasing)
1587
    check_some_aliasing (sym->formal, *ap);
1588
}
1589
 
1590
 
1591
/* Given an interface pointer and an actual argument list, search for
1592
   a formal argument list that matches the actual.  If found, returns
1593
   a pointer to the symbol of the correct interface.  Returns NULL if
1594
   not found.  */
1595
 
1596
gfc_symbol *
1597
gfc_search_interface (gfc_interface * intr, int sub_flag,
1598
                      gfc_actual_arglist ** ap)
1599
{
1600
  int r;
1601
 
1602
  for (; intr; intr = intr->next)
1603
    {
1604
      if (sub_flag && intr->sym->attr.function)
1605
        continue;
1606
      if (!sub_flag && intr->sym->attr.subroutine)
1607
        continue;
1608
 
1609
      r = !intr->sym->attr.elemental;
1610
 
1611
      if (compare_actual_formal (ap, intr->sym->formal, r, !r, NULL))
1612
        {
1613
          check_intents (intr->sym->formal, *ap);
1614
          if (gfc_option.warn_aliasing)
1615
            check_some_aliasing (intr->sym->formal, *ap);
1616
          return intr->sym;
1617
        }
1618
    }
1619
 
1620
  return NULL;
1621
}
1622
 
1623
 
1624
/* Do a brute force recursive search for a symbol.  */
1625
 
1626
static gfc_symtree *
1627
find_symtree0 (gfc_symtree * root, gfc_symbol * sym)
1628
{
1629
  gfc_symtree * st;
1630
 
1631
  if (root->n.sym == sym)
1632
    return root;
1633
 
1634
  st = NULL;
1635
  if (root->left)
1636
    st = find_symtree0 (root->left, sym);
1637
  if (root->right && ! st)
1638
    st = find_symtree0 (root->right, sym);
1639
  return st;
1640
}
1641
 
1642
 
1643
/* Find a symtree for a symbol.  */
1644
 
1645
static gfc_symtree *
1646
find_sym_in_symtree (gfc_symbol * sym)
1647
{
1648
  gfc_symtree *st;
1649
  gfc_namespace *ns;
1650
 
1651
  /* First try to find it by name.  */
1652
  gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
1653
  if (st && st->n.sym == sym)
1654
    return st;
1655
 
1656
  /* if it's been renamed, resort to a brute-force search.  */
1657
  /* TODO: avoid having to do this search.  If the symbol doesn't exist
1658
     in the symtree for the current namespace, it should probably be added.  */
1659
  for (ns = gfc_current_ns; ns; ns = ns->parent)
1660
    {
1661
      st = find_symtree0 (ns->sym_root, sym);
1662
      if (st)
1663
        return st;
1664
    }
1665
  gfc_internal_error ("Unable to find symbol %s", sym->name);
1666
  /* Not reached */
1667
}
1668
 
1669
 
1670
/* This subroutine is called when an expression is being resolved.
1671
   The expression node in question is either a user defined operator
1672
   or an intrinsic operator with arguments that aren't compatible
1673
   with the operator.  This subroutine builds an actual argument list
1674
   corresponding to the operands, then searches for a compatible
1675
   interface.  If one is found, the expression node is replaced with
1676
   the appropriate function call.  */
1677
 
1678
try
1679
gfc_extend_expr (gfc_expr * e)
1680
{
1681
  gfc_actual_arglist *actual;
1682
  gfc_symbol *sym;
1683
  gfc_namespace *ns;
1684
  gfc_user_op *uop;
1685
  gfc_intrinsic_op i;
1686
 
1687
  sym = NULL;
1688
 
1689
  actual = gfc_get_actual_arglist ();
1690
  actual->expr = e->value.op.op1;
1691
 
1692
  if (e->value.op.op2 != NULL)
1693
    {
1694
      actual->next = gfc_get_actual_arglist ();
1695
      actual->next->expr = e->value.op.op2;
1696
    }
1697
 
1698
  i = fold_unary (e->value.op.operator);
1699
 
1700
  if (i == INTRINSIC_USER)
1701
    {
1702
      for (ns = gfc_current_ns; ns; ns = ns->parent)
1703
        {
1704
          uop = gfc_find_uop (e->value.op.uop->name, ns);
1705
          if (uop == NULL)
1706
            continue;
1707
 
1708
          sym = gfc_search_interface (uop->operator, 0, &actual);
1709
          if (sym != NULL)
1710
            break;
1711
        }
1712
    }
1713
  else
1714
    {
1715
      for (ns = gfc_current_ns; ns; ns = ns->parent)
1716
        {
1717
          sym = gfc_search_interface (ns->operator[i], 0, &actual);
1718
          if (sym != NULL)
1719
            break;
1720
        }
1721
    }
1722
 
1723
  if (sym == NULL)
1724
    {
1725
      /* Don't use gfc_free_actual_arglist() */
1726
      if (actual->next != NULL)
1727
        gfc_free (actual->next);
1728
      gfc_free (actual);
1729
 
1730
      return FAILURE;
1731
    }
1732
 
1733
  /* Change the expression node to a function call.  */
1734
  e->expr_type = EXPR_FUNCTION;
1735
  e->symtree = find_sym_in_symtree (sym);
1736
  e->value.function.actual = actual;
1737
  e->value.function.esym = NULL;
1738
  e->value.function.isym = NULL;
1739
  e->value.function.name = NULL;
1740
 
1741
  if (gfc_pure (NULL) && !gfc_pure (sym))
1742
    {
1743
      gfc_error
1744
        ("Function '%s' called in lieu of an operator at %L must be PURE",
1745
         sym->name, &e->where);
1746
      return FAILURE;
1747
    }
1748
 
1749
  if (gfc_resolve_expr (e) == FAILURE)
1750
    return FAILURE;
1751
 
1752
  return SUCCESS;
1753
}
1754
 
1755
 
1756
/* Tries to replace an assignment code node with a subroutine call to
1757
   the subroutine associated with the assignment operator.  Return
1758
   SUCCESS if the node was replaced.  On FAILURE, no error is
1759
   generated.  */
1760
 
1761
try
1762
gfc_extend_assign (gfc_code * c, gfc_namespace * ns)
1763
{
1764
  gfc_actual_arglist *actual;
1765
  gfc_expr *lhs, *rhs;
1766
  gfc_symbol *sym;
1767
 
1768
  lhs = c->expr;
1769
  rhs = c->expr2;
1770
 
1771
  /* Don't allow an intrinsic assignment to be replaced.  */
1772
  if (lhs->ts.type != BT_DERIVED && rhs->ts.type != BT_DERIVED
1773
      && (lhs->ts.type == rhs->ts.type
1774
          || (gfc_numeric_ts (&lhs->ts)
1775
              && gfc_numeric_ts (&rhs->ts))))
1776
    return FAILURE;
1777
 
1778
  actual = gfc_get_actual_arglist ();
1779
  actual->expr = lhs;
1780
 
1781
  actual->next = gfc_get_actual_arglist ();
1782
  actual->next->expr = rhs;
1783
 
1784
  sym = NULL;
1785
 
1786
  for (; ns; ns = ns->parent)
1787
    {
1788
      sym = gfc_search_interface (ns->operator[INTRINSIC_ASSIGN], 1, &actual);
1789
      if (sym != NULL)
1790
        break;
1791
    }
1792
 
1793
  if (sym == NULL)
1794
    {
1795
      gfc_free (actual->next);
1796
      gfc_free (actual);
1797
      return FAILURE;
1798
    }
1799
 
1800
  /* Replace the assignment with the call.  */
1801
  c->op = EXEC_CALL;
1802
  c->symtree = find_sym_in_symtree (sym);
1803
  c->expr = NULL;
1804
  c->expr2 = NULL;
1805
  c->ext.actual = actual;
1806
 
1807
  return SUCCESS;
1808
}
1809
 
1810
 
1811
/* Make sure that the interface just parsed is not already present in
1812
   the given interface list.  Ambiguity isn't checked yet since module
1813
   procedures can be present without interfaces.  */
1814
 
1815
static try
1816
check_new_interface (gfc_interface * base, gfc_symbol * new)
1817
{
1818
  gfc_interface *ip;
1819
 
1820
  for (ip = base; ip; ip = ip->next)
1821
    {
1822
      if (ip->sym == new)
1823
        {
1824
          gfc_error ("Entity '%s' at %C is already present in the interface",
1825
                     new->name);
1826
          return FAILURE;
1827
        }
1828
    }
1829
 
1830
  return SUCCESS;
1831
}
1832
 
1833
 
1834
/* Add a symbol to the current interface.  */
1835
 
1836
try
1837
gfc_add_interface (gfc_symbol * new)
1838
{
1839
  gfc_interface **head, *intr;
1840
  gfc_namespace *ns;
1841
  gfc_symbol *sym;
1842
 
1843
  switch (current_interface.type)
1844
    {
1845
    case INTERFACE_NAMELESS:
1846
      return SUCCESS;
1847
 
1848
    case INTERFACE_INTRINSIC_OP:
1849
      for (ns = current_interface.ns; ns; ns = ns->parent)
1850
        if (check_new_interface (ns->operator[current_interface.op], new)
1851
            == FAILURE)
1852
          return FAILURE;
1853
 
1854
      head = &current_interface.ns->operator[current_interface.op];
1855
      break;
1856
 
1857
    case INTERFACE_GENERIC:
1858
      for (ns = current_interface.ns; ns; ns = ns->parent)
1859
        {
1860
          gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
1861
          if (sym == NULL)
1862
            continue;
1863
 
1864
          if (check_new_interface (sym->generic, new) == FAILURE)
1865
            return FAILURE;
1866
        }
1867
 
1868
      head = &current_interface.sym->generic;
1869
      break;
1870
 
1871
    case INTERFACE_USER_OP:
1872
      if (check_new_interface (current_interface.uop->operator, new) ==
1873
          FAILURE)
1874
        return FAILURE;
1875
 
1876
      head = &current_interface.uop->operator;
1877
      break;
1878
 
1879
    default:
1880
      gfc_internal_error ("gfc_add_interface(): Bad interface type");
1881
    }
1882
 
1883
  intr = gfc_get_interface ();
1884
  intr->sym = new;
1885
  intr->where = gfc_current_locus;
1886
 
1887
  intr->next = *head;
1888
  *head = intr;
1889
 
1890
  return SUCCESS;
1891
}
1892
 
1893
 
1894
/* Gets rid of a formal argument list.  We do not free symbols.
1895
   Symbols are freed when a namespace is freed.  */
1896
 
1897
void
1898
gfc_free_formal_arglist (gfc_formal_arglist * p)
1899
{
1900
  gfc_formal_arglist *q;
1901
 
1902
  for (; p; p = q)
1903
    {
1904
      q = p->next;
1905
      gfc_free (p);
1906
    }
1907
}

powered by: WebSVN 2.1.0

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