mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
interface.cc
5803 lines (4942 loc) · 160 KB
/
interface.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Deal with interfaces.
Copyright (C) 2000-2024 Free Software Foundation, Inc.
Contributed by Andy Vaught
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* Deal with interfaces. An explicit interface is represented as a
singly linked list of formal argument structures attached to the
relevant symbols. For an implicit interface, the arguments don't
point to symbols. Explicit interfaces point to namespaces that
contain the symbols within that interface.
Implicit interfaces are linked together in a singly linked list
along the next_if member of symbol nodes. Since a particular
symbol can only have a single explicit interface, the symbol cannot
be part of multiple lists and a single next-member suffices.
This is not the case for general classes, though. An operator
definition is independent of just about all other uses and has it's
own head pointer.
Nameless interfaces:
Nameless interfaces create symbols with explicit interfaces within
the current namespace. They are otherwise unlinked.
Generic interfaces:
The generic name points to a linked list of symbols. Each symbol
has an explicit interface. Each explicit interface has its own
namespace containing the arguments. Module procedures are symbols in
which the interface is added later when the module procedure is parsed.
User operators:
User-defined operators are stored in a their own set of symtrees
separate from regular symbols. The symtrees point to gfc_user_op
structures which in turn head up a list of relevant interfaces.
Extended intrinsics and assignment:
The head of these interface lists are stored in the containing namespace.
Implicit interfaces:
An implicit interface is represented as a singly linked list of
formal argument list structures that don't point to any symbol
nodes -- they just contain types.
When a subprogram is defined, the program unit's name points to an
interface as usual, but the link to the namespace is NULL and the
formal argument list points to symbols within the same namespace as
the program unit name. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"
#include "gfortran.h"
#include "match.h"
#include "arith.h"
/* The current_interface structure holds information about the
interface currently being parsed. This structure is saved and
restored during recursive interfaces. */
gfc_interface_info current_interface;
/* Free the leading members of the gfc_interface linked list given in INTR
up to the END element (exclusive: the END element is not freed).
If END is not nullptr, it is assumed that END is in the linked list starting
with INTR. */
static void
free_interface_elements_until (gfc_interface *intr, gfc_interface *end)
{
gfc_interface *next;
for (; intr != end; intr = next)
{
next = intr->next;
free (intr);
}
}
/* Free a singly linked list of gfc_interface structures. */
void
gfc_free_interface (gfc_interface *intr)
{
free_interface_elements_until (intr, nullptr);
}
/* Update the interface pointer given by IFC_PTR to make it point to TAIL.
It is expected that TAIL (if non-null) is in the list pointed to by
IFC_PTR, hence the tail of it. The members of the list before TAIL are
freed before the pointer reassignment. */
void
gfc_drop_interface_elements_before (gfc_interface **ifc_ptr,
gfc_interface *tail)
{
if (ifc_ptr == nullptr)
return;
free_interface_elements_until (*ifc_ptr, tail);
*ifc_ptr = tail;
}
/* Change the operators unary plus and minus into binary plus and
minus respectively, leaving the rest unchanged. */
static gfc_intrinsic_op
fold_unary_intrinsic (gfc_intrinsic_op op)
{
switch (op)
{
case INTRINSIC_UPLUS:
op = INTRINSIC_PLUS;
break;
case INTRINSIC_UMINUS:
op = INTRINSIC_MINUS;
break;
default:
break;
}
return op;
}
/* Return the operator depending on the DTIO moded string. Note that
these are not operators in the normal sense and so have been placed
beyond GFC_INTRINSIC_END in gfortran.h:enum gfc_intrinsic_op. */
static gfc_intrinsic_op
dtio_op (char* mode)
{
if (strcmp (mode, "formatted") == 0)
return INTRINSIC_FORMATTED;
if (strcmp (mode, "unformatted") == 0)
return INTRINSIC_UNFORMATTED;
return INTRINSIC_NONE;
}
/* Match a generic specification. Depending on which type of
interface is found, the 'name' or 'op' pointers may be set.
This subroutine doesn't return MATCH_NO. */
match
gfc_match_generic_spec (interface_type *type,
char *name,
gfc_intrinsic_op *op)
{
char buffer[GFC_MAX_SYMBOL_LEN + 1];
match m;
gfc_intrinsic_op i;
if (gfc_match (" assignment ( = )") == MATCH_YES)
{
*type = INTERFACE_INTRINSIC_OP;
*op = INTRINSIC_ASSIGN;
return MATCH_YES;
}
if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
{ /* Operator i/f */
*type = INTERFACE_INTRINSIC_OP;
*op = fold_unary_intrinsic (i);
return MATCH_YES;
}
*op = INTRINSIC_NONE;
if (gfc_match (" operator ( ") == MATCH_YES)
{
m = gfc_match_defined_op_name (buffer, 1);
if (m == MATCH_NO)
goto syntax;
if (m != MATCH_YES)
return MATCH_ERROR;
m = gfc_match_char (')');
if (m == MATCH_NO)
goto syntax;
if (m != MATCH_YES)
return MATCH_ERROR;
strcpy (name, buffer);
*type = INTERFACE_USER_OP;
return MATCH_YES;
}
if (gfc_match (" read ( %n )", buffer) == MATCH_YES)
{
*op = dtio_op (buffer);
if (*op == INTRINSIC_FORMATTED)
{
strcpy (name, gfc_code2string (dtio_procs, DTIO_RF));
*type = INTERFACE_DTIO;
}
if (*op == INTRINSIC_UNFORMATTED)
{
strcpy (name, gfc_code2string (dtio_procs, DTIO_RUF));
*type = INTERFACE_DTIO;
}
if (*op != INTRINSIC_NONE)
return MATCH_YES;
}
if (gfc_match (" write ( %n )", buffer) == MATCH_YES)
{
*op = dtio_op (buffer);
if (*op == INTRINSIC_FORMATTED)
{
strcpy (name, gfc_code2string (dtio_procs, DTIO_WF));
*type = INTERFACE_DTIO;
}
if (*op == INTRINSIC_UNFORMATTED)
{
strcpy (name, gfc_code2string (dtio_procs, DTIO_WUF));
*type = INTERFACE_DTIO;
}
if (*op != INTRINSIC_NONE)
return MATCH_YES;
}
if (gfc_match_name (buffer) == MATCH_YES)
{
strcpy (name, buffer);
*type = INTERFACE_GENERIC;
return MATCH_YES;
}
*type = INTERFACE_NAMELESS;
return MATCH_YES;
syntax:
gfc_error ("Syntax error in generic specification at %C");
return MATCH_ERROR;
}
/* Match one of the five F95 forms of an interface statement. The
matcher for the abstract interface follows. */
match
gfc_match_interface (void)
{
char name[GFC_MAX_SYMBOL_LEN + 1];
interface_type type;
gfc_symbol *sym;
gfc_intrinsic_op op;
match m;
m = gfc_match_space ();
if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
return MATCH_ERROR;
/* If we're not looking at the end of the statement now, or if this
is not a nameless interface but we did not see a space, punt. */
if (gfc_match_eos () != MATCH_YES
|| (type != INTERFACE_NAMELESS && m != MATCH_YES))
{
gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
"at %C");
return MATCH_ERROR;
}
current_interface.type = type;
switch (type)
{
case INTERFACE_DTIO:
case INTERFACE_GENERIC:
if (gfc_get_symbol (name, NULL, &sym))
return MATCH_ERROR;
if (!sym->attr.generic
&& !gfc_add_generic (&sym->attr, sym->name, NULL))
return MATCH_ERROR;
if (sym->attr.dummy)
{
gfc_error ("Dummy procedure %qs at %C cannot have a "
"generic interface", sym->name);
return MATCH_ERROR;
}
current_interface.sym = gfc_new_block = sym;
break;
case INTERFACE_USER_OP:
current_interface.uop = gfc_get_uop (name);
break;
case INTERFACE_INTRINSIC_OP:
current_interface.op = op;
break;
case INTERFACE_NAMELESS:
case INTERFACE_ABSTRACT:
break;
}
return MATCH_YES;
}
/* Match a F2003 abstract interface. */
match
gfc_match_abstract_interface (void)
{
match m;
if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT INTERFACE at %C"))
return MATCH_ERROR;
m = gfc_match_eos ();
if (m != MATCH_YES)
{
gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
return MATCH_ERROR;
}
current_interface.type = INTERFACE_ABSTRACT;
return m;
}
/* Match the different sort of generic-specs that can be present after
the END INTERFACE itself. */
match
gfc_match_end_interface (void)
{
char name[GFC_MAX_SYMBOL_LEN + 1];
interface_type type;
gfc_intrinsic_op op;
match m;
m = gfc_match_space ();
if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
return MATCH_ERROR;
/* If we're not looking at the end of the statement now, or if this
is not a nameless interface but we did not see a space, punt. */
if (gfc_match_eos () != MATCH_YES
|| (type != INTERFACE_NAMELESS && m != MATCH_YES))
{
gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
"statement at %C");
return MATCH_ERROR;
}
m = MATCH_YES;
switch (current_interface.type)
{
case INTERFACE_NAMELESS:
case INTERFACE_ABSTRACT:
if (type != INTERFACE_NAMELESS)
{
gfc_error ("Expected a nameless interface at %C");
m = MATCH_ERROR;
}
break;
case INTERFACE_INTRINSIC_OP:
if (type != current_interface.type || op != current_interface.op)
{
if (current_interface.op == INTRINSIC_ASSIGN)
{
m = MATCH_ERROR;
gfc_error ("Expected %<END INTERFACE ASSIGNMENT (=)%> at %C");
}
else
{
const char *s1, *s2;
s1 = gfc_op2string (current_interface.op);
s2 = gfc_op2string (op);
/* The following if-statements are used to enforce C1202
from F2003. */
if ((strcmp(s1, "==") == 0 && strcmp (s2, ".eq.") == 0)
|| (strcmp(s1, ".eq.") == 0 && strcmp (s2, "==") == 0))
break;
if ((strcmp(s1, "/=") == 0 && strcmp (s2, ".ne.") == 0)
|| (strcmp(s1, ".ne.") == 0 && strcmp (s2, "/=") == 0))
break;
if ((strcmp(s1, "<=") == 0 && strcmp (s2, ".le.") == 0)
|| (strcmp(s1, ".le.") == 0 && strcmp (s2, "<=") == 0))
break;
if ((strcmp(s1, "<") == 0 && strcmp (s2, ".lt.") == 0)
|| (strcmp(s1, ".lt.") == 0 && strcmp (s2, "<") == 0))
break;
if ((strcmp(s1, ">=") == 0 && strcmp (s2, ".ge.") == 0)
|| (strcmp(s1, ".ge.") == 0 && strcmp (s2, ">=") == 0))
break;
if ((strcmp(s1, ">") == 0 && strcmp (s2, ".gt.") == 0)
|| (strcmp(s1, ".gt.") == 0 && strcmp (s2, ">") == 0))
break;
m = MATCH_ERROR;
if (strcmp(s2, "none") == 0)
gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> "
"at %C", s1);
else
gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> at %C, "
"but got %qs", s1, s2);
}
}
break;
case INTERFACE_USER_OP:
/* Comparing the symbol node names is OK because only use-associated
symbols can be renamed. */
if (type != current_interface.type
|| strcmp (current_interface.uop->name, name) != 0)
{
gfc_error ("Expecting %<END INTERFACE OPERATOR (.%s.)%> at %C",
current_interface.uop->name);
m = MATCH_ERROR;
}
break;
case INTERFACE_DTIO:
case INTERFACE_GENERIC:
if (type != current_interface.type
|| strcmp (current_interface.sym->name, name) != 0)
{
gfc_error ("Expecting %<END INTERFACE %s%> at %C",
current_interface.sym->name);
m = MATCH_ERROR;
}
break;
}
return m;
}
/* Return whether the component was defined anonymously. */
static bool
is_anonymous_component (gfc_component *cmp)
{
/* Only UNION and MAP components are anonymous. In the case of a MAP,
the derived type symbol is FL_STRUCT and the component name looks like mM*.
This is the only case in which the second character of a component name is
uppercase. */
return cmp->ts.type == BT_UNION
|| (cmp->ts.type == BT_DERIVED
&& cmp->ts.u.derived->attr.flavor == FL_STRUCT
&& cmp->name[0] && cmp->name[1] && ISUPPER (cmp->name[1]));
}
/* Return whether the derived type was defined anonymously. */
static bool
is_anonymous_dt (gfc_symbol *derived)
{
/* UNION and MAP types are always anonymous. Otherwise, only nested STRUCTURE
types can be anonymous. For anonymous MAP/STRUCTURE, we have FL_STRUCT
and the type name looks like XX*. This is the only case in which the
second character of a type name is uppercase. */
return derived->attr.flavor == FL_UNION
|| (derived->attr.flavor == FL_STRUCT
&& derived->name[0] && derived->name[1] && ISUPPER (derived->name[1]));
}
/* Compare components according to 4.4.2 of the Fortran standard. */
static bool
compare_components (gfc_component *cmp1, gfc_component *cmp2,
gfc_symbol *derived1, gfc_symbol *derived2)
{
/* Compare names, but not for anonymous components such as UNION or MAP. */
if (!is_anonymous_component (cmp1) && !is_anonymous_component (cmp2)
&& strcmp (cmp1->name, cmp2->name) != 0)
return false;
if (cmp1->attr.access != cmp2->attr.access)
return false;
if (cmp1->attr.pointer != cmp2->attr.pointer)
return false;
if (cmp1->attr.dimension != cmp2->attr.dimension)
return false;
if (cmp1->attr.allocatable != cmp2->attr.allocatable)
return false;
if (cmp1->attr.dimension && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
return false;
if (cmp1->ts.type == BT_CHARACTER && cmp2->ts.type == BT_CHARACTER)
{
gfc_charlen *l1 = cmp1->ts.u.cl;
gfc_charlen *l2 = cmp2->ts.u.cl;
if (l1 && l2 && l1->length && l2->length
&& l1->length->expr_type == EXPR_CONSTANT
&& l2->length->expr_type == EXPR_CONSTANT
&& gfc_dep_compare_expr (l1->length, l2->length) != 0)
return false;
}
/* Make sure that link lists do not put this function into an
endless recursive loop! */
if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
&& !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived)
&& !gfc_compare_types (&cmp1->ts, &cmp2->ts))
return false;
else if ( (cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
&& !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
return false;
else if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
&& (cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
return false;
return true;
}
/* Compare two union types by comparing the components of their maps.
Because unions and maps are anonymous their types get special internal
names; therefore the usual derived type comparison will fail on them.
Returns nonzero if equal, as with gfc_compare_derived_types. Also as with
gfc_compare_derived_types, 'equal' is closer to meaning 'duplicate
definitions' than 'equivalent structure'. */
static bool
compare_union_types (gfc_symbol *un1, gfc_symbol *un2)
{
gfc_component *map1, *map2, *cmp1, *cmp2;
gfc_symbol *map1_t, *map2_t;
if (un1->attr.flavor != FL_UNION || un2->attr.flavor != FL_UNION)
return false;
if (un1->attr.zero_comp != un2->attr.zero_comp)
return false;
if (un1->attr.zero_comp)
return true;
map1 = un1->components;
map2 = un2->components;
/* In terms of 'equality' here we are worried about types which are
declared the same in two places, not types that represent equivalent
structures. (This is common because of FORTRAN's weird scoping rules.)
Though two unions with their maps in different orders could be equivalent,
we will say they are not equal for the purposes of this test; therefore
we compare the maps sequentially. */
for (;;)
{
map1_t = map1->ts.u.derived;
map2_t = map2->ts.u.derived;
cmp1 = map1_t->components;
cmp2 = map2_t->components;
/* Protect against null components. */
if (map1_t->attr.zero_comp != map2_t->attr.zero_comp)
return false;
if (map1_t->attr.zero_comp)
return true;
for (;;)
{
/* No two fields will ever point to the same map type unless they are
the same component, because one map field is created with its type
declaration. Therefore don't worry about recursion here. */
/* TODO: worry about recursion into parent types of the unions? */
if (!compare_components (cmp1, cmp2, map1_t, map2_t))
return false;
cmp1 = cmp1->next;
cmp2 = cmp2->next;
if (cmp1 == NULL && cmp2 == NULL)
break;
if (cmp1 == NULL || cmp2 == NULL)
return false;
}
map1 = map1->next;
map2 = map2->next;
if (map1 == NULL && map2 == NULL)
break;
if (map1 == NULL || map2 == NULL)
return false;
}
return true;
}
/* Compare two derived types using the criteria in 4.4.2 of the standard,
recursing through gfc_compare_types for the components. */
bool
gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
{
gfc_component *cmp1, *cmp2;
if (derived1 == derived2)
return true;
if (!derived1 || !derived2)
gfc_internal_error ("gfc_compare_derived_types: invalid derived type");
if (derived1->attr.unlimited_polymorphic
&& derived2->attr.unlimited_polymorphic)
return true;
if (derived1->attr.unlimited_polymorphic
!= derived2->attr.unlimited_polymorphic)
return false;
/* Compare UNION types specially. */
if (derived1->attr.flavor == FL_UNION || derived2->attr.flavor == FL_UNION)
return compare_union_types (derived1, derived2);
/* Special case for comparing derived types across namespaces. If the
true names and module names are the same and the module name is
nonnull, then they are equal. */
if (strcmp (derived1->name, derived2->name) == 0
&& derived1->module != NULL && derived2->module != NULL
&& strcmp (derived1->module, derived2->module) == 0)
return true;
/* Compare type via the rules of the standard. Both types must have the
SEQUENCE or BIND(C) attribute to be equal. We also compare types
recursively if they are class descriptors types or virtual tables types.
STRUCTUREs are special because they can be anonymous; therefore two
structures with different names may be equal. */
/* Compare names, but not for anonymous types such as UNION or MAP. */
if (!is_anonymous_dt (derived1) && !is_anonymous_dt (derived2)
&& strcmp (derived1->name, derived2->name) != 0)
return false;
if (derived1->component_access == ACCESS_PRIVATE
|| derived2->component_access == ACCESS_PRIVATE)
return false;
if (!(derived1->attr.sequence && derived2->attr.sequence)
&& !(derived1->attr.is_bind_c && derived2->attr.is_bind_c)
&& !(derived1->attr.is_class && derived2->attr.is_class)
&& !(derived1->attr.vtype && derived2->attr.vtype)
&& !(derived1->attr.pdt_type && derived2->attr.pdt_type))
return false;
/* Protect against null components. */
if (derived1->attr.zero_comp != derived2->attr.zero_comp)
return false;
if (derived1->attr.zero_comp)
return true;
cmp1 = derived1->components;
cmp2 = derived2->components;
/* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
simple test can speed things up. Otherwise, lots of things have to
match. */
for (;;)
{
if (!compare_components (cmp1, cmp2, derived1, derived2))
return false;
cmp1 = cmp1->next;
cmp2 = cmp2->next;
if (cmp1 == NULL && cmp2 == NULL)
break;
if (cmp1 == NULL || cmp2 == NULL)
return false;
}
return true;
}
/* Compare two typespecs, recursively if necessary. */
bool
gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
{
/* See if one of the typespecs is a BT_VOID, which is what is being used
to allow the funcs like c_f_pointer to accept any pointer type.
TODO: Possibly should narrow this to just the one typespec coming in
that is for the formal arg, but oh well. */
if (ts1->type == BT_VOID || ts2->type == BT_VOID)
return true;
/* Special case for our C interop types. FIXME: There should be a
better way of doing this. When ISO C binding is cleared up,
this can probably be removed. See PR 57048. */
if ((ts1->type == BT_INTEGER
&& ts2->type == BT_DERIVED
&& ts1->f90_type == BT_VOID
&& ts2->u.derived->from_intmod == INTMOD_ISO_C_BINDING
&& ts1->u.derived
&& strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0)
|| (ts2->type == BT_INTEGER
&& ts1->type == BT_DERIVED
&& ts2->f90_type == BT_VOID
&& ts1->u.derived->from_intmod == INTMOD_ISO_C_BINDING
&& ts2->u.derived
&& strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0))
return true;
/* The _data component is not always present, therefore check for its
presence before assuming, that its derived->attr is available.
When the _data component is not present, then nevertheless the
unlimited_polymorphic flag may be set in the derived type's attr. */
if (ts1->type == BT_CLASS && ts1->u.derived->components
&& ((ts1->u.derived->attr.is_class
&& ts1->u.derived->components->ts.u.derived->attr
.unlimited_polymorphic)
|| ts1->u.derived->attr.unlimited_polymorphic))
return true;
/* F2003: C717 */
if (ts2->type == BT_CLASS && ts1->type == BT_DERIVED
&& ts2->u.derived->components
&& ((ts2->u.derived->attr.is_class
&& ts2->u.derived->components->ts.u.derived->attr
.unlimited_polymorphic)
|| ts2->u.derived->attr.unlimited_polymorphic)
&& (ts1->u.derived->attr.sequence || ts1->u.derived->attr.is_bind_c))
return true;
if (ts1->type != ts2->type
&& ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
|| (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
return false;
if (ts1->type == BT_UNION)
return compare_union_types (ts1->u.derived, ts2->u.derived);
if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
return (ts1->kind == ts2->kind);
/* Compare derived types. */
return gfc_type_compatible (ts1, ts2);
}
static bool
compare_type (gfc_symbol *s1, gfc_symbol *s2)
{
if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
return true;
return gfc_compare_types (&s1->ts, &s2->ts) || s2->ts.type == BT_ASSUMED;
}
static bool
compare_type_characteristics (gfc_symbol *s1, gfc_symbol *s2)
{
/* TYPE and CLASS of the same declared type are type compatible,
but have different characteristics. */
if ((s1->ts.type == BT_CLASS && s2->ts.type == BT_DERIVED)
|| (s1->ts.type == BT_DERIVED && s2->ts.type == BT_CLASS))
return false;
return compare_type (s1, s2);
}
static bool
compare_rank (gfc_symbol *s1, gfc_symbol *s2)
{
gfc_array_spec *as1, *as2;
int r1, r2;
if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
return true;
as1 = (s1->ts.type == BT_CLASS
&& !s1->ts.u.derived->attr.unlimited_polymorphic)
? CLASS_DATA (s1)->as : s1->as;
as2 = (s2->ts.type == BT_CLASS
&& !s2->ts.u.derived->attr.unlimited_polymorphic)
? CLASS_DATA (s2)->as : s2->as;
r1 = as1 ? as1->rank : 0;
r2 = as2 ? as2->rank : 0;
if (r1 != r2 && (!as2 || as2->type != AS_ASSUMED_RANK))
return false; /* Ranks differ. */
return true;
}
/* Given two symbols that are formal arguments, compare their ranks
and types. Returns true if they have the same rank and type,
false otherwise. */
static bool
compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
{
return compare_type (s1, s2) && compare_rank (s1, s2);
}
/* Given two symbols that are formal arguments, compare their types
and rank and their formal interfaces if they are both dummy
procedures. Returns true if the same, false if different. */
static bool
compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
{
if (s1 == NULL || s2 == NULL)
return (s1 == s2);
if (s1 == s2)
return true;
if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
return compare_type_rank (s1, s2);
if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
return false;
/* At this point, both symbols are procedures. It can happen that
external procedures are compared, where one is identified by usage
to be a function or subroutine but the other is not. Check TKR
nonetheless for these cases. */
if (s1->attr.function == 0 && s1->attr.subroutine == 0)
return s1->attr.external ? compare_type_rank (s1, s2) : false;
if (s2->attr.function == 0 && s2->attr.subroutine == 0)
return s2->attr.external ? compare_type_rank (s1, s2) : false;
/* Now the type of procedure has been identified. */
if (s1->attr.function != s2->attr.function
|| s1->attr.subroutine != s2->attr.subroutine)
return false;
if (s1->attr.function && !compare_type_rank (s1, s2))
return false;
/* Originally, gfortran recursed here to check the interfaces of passed
procedures. This is explicitly not required by the standard. */
return true;
}
/* Given a formal argument list and a keyword name, search the list
for that keyword. Returns the correct symbol node if found, NULL
if not found. */
static gfc_symbol *
find_keyword_arg (const char *name, gfc_formal_arglist *f)
{
for (; f; f = f->next)
if (strcmp (f->sym->name, name) == 0)
return f->sym;
return NULL;
}
/******** Interface checking subroutines **********/
/* Given an operator interface and the operator, make sure that all
interfaces for that operator are legal. */
bool
gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
locus opwhere)
{
gfc_formal_arglist *formal;
sym_intent i1, i2;
bt t1, t2;
int args, r1, r2, k1, k2;
gcc_assert (sym);
args = 0;
t1 = t2 = BT_UNKNOWN;
i1 = i2 = INTENT_UNKNOWN;
r1 = r2 = -1;
k1 = k2 = -1;
for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
{
gfc_symbol *fsym = formal->sym;
if (fsym == NULL)
{
gfc_error ("Alternate return cannot appear in operator "
"interface at %L", &sym->declared_at);
return false;
}
if (args == 0)
{
t1 = fsym->ts.type;
i1 = fsym->attr.intent;
r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
k1 = fsym->ts.kind;
}
if (args == 1)
{
t2 = fsym->ts.type;
i2 = fsym->attr.intent;
r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
k2 = fsym->ts.kind;
}
args++;
}
/* Only +, - and .not. can be unary operators.
.not. cannot be a binary operator. */
if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
&& op != INTRINSIC_MINUS
&& op != INTRINSIC_NOT)
|| (args == 2 && op == INTRINSIC_NOT))
{
if (op == INTRINSIC_ASSIGN)
gfc_error ("Assignment operator interface at %L must have "
"two arguments", &sym->declared_at);
else
gfc_error ("Operator interface at %L has the wrong number of arguments",
&sym->declared_at);
return false;
}
/* Check that intrinsics are mapped to functions, except
INTRINSIC_ASSIGN which should map to a subroutine. */
if (op == INTRINSIC_ASSIGN)
{
gfc_formal_arglist *dummy_args;
if (!sym->attr.subroutine)
{
gfc_error ("Assignment operator interface at %L must be "
"a SUBROUTINE", &sym->declared_at);
return false;
}
/* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
- First argument an array with different rank than second,
- First argument is a scalar and second an array,
- Types and kinds do not conform, or
- First argument is of derived type. */
dummy_args = gfc_sym_get_dummy_args (sym);
if (dummy_args->sym->ts.type != BT_DERIVED
&& dummy_args->sym->ts.type != BT_CLASS
&& (r2 == 0 || r1 == r2)
&& (dummy_args->sym->ts.type == dummy_args->next->sym->ts.type
|| (gfc_numeric_ts (&dummy_args->sym->ts)
&& gfc_numeric_ts (&dummy_args->next->sym->ts))))
{
gfc_error ("Assignment operator interface at %L must not redefine "
"an INTRINSIC type assignment", &sym->declared_at);