re PR fortran/28788 (ICE on valid code)
[gcc.git] / gcc / fortran / symbol.c
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
3 Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 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 "flags.h"
27 #include "gfortran.h"
28 #include "parse.h"
29
30 /* Strings for all symbol attributes. We use these for dumping the
31 parse tree, in error messages, and also when reading and writing
32 modules. */
33
34 const mstring flavors[] =
35 {
36 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
37 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
38 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
39 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
40 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
41 minit (NULL, -1)
42 };
43
44 const mstring procedures[] =
45 {
46 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
47 minit ("MODULE-PROC", PROC_MODULE),
48 minit ("INTERNAL-PROC", PROC_INTERNAL),
49 minit ("DUMMY-PROC", PROC_DUMMY),
50 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
51 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
52 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
53 minit (NULL, -1)
54 };
55
56 const mstring intents[] =
57 {
58 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
59 minit ("IN", INTENT_IN),
60 minit ("OUT", INTENT_OUT),
61 minit ("INOUT", INTENT_INOUT),
62 minit (NULL, -1)
63 };
64
65 const mstring access_types[] =
66 {
67 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
68 minit ("PUBLIC", ACCESS_PUBLIC),
69 minit ("PRIVATE", ACCESS_PRIVATE),
70 minit (NULL, -1)
71 };
72
73 const mstring ifsrc_types[] =
74 {
75 minit ("UNKNOWN", IFSRC_UNKNOWN),
76 minit ("DECL", IFSRC_DECL),
77 minit ("BODY", IFSRC_IFBODY),
78 minit ("USAGE", IFSRC_USAGE)
79 };
80
81
82 /* This is to make sure the backend generates setup code in the correct
83 order. */
84
85 static int next_dummy_order = 1;
86
87
88 gfc_namespace *gfc_current_ns;
89
90 gfc_gsymbol *gfc_gsym_root = NULL;
91
92 static gfc_symbol *changed_syms = NULL;
93
94
95 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
96
97 /* The following static variable indicates whether a particular element has
98 been explicitly set or not. */
99
100 static int new_flag[GFC_LETTERS];
101
102
103 /* Handle a correctly parsed IMPLICIT NONE. */
104
105 void
106 gfc_set_implicit_none (void)
107 {
108 int i;
109
110 if (gfc_current_ns->seen_implicit_none)
111 {
112 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
113 return;
114 }
115
116 gfc_current_ns->seen_implicit_none = 1;
117
118 for (i = 0; i < GFC_LETTERS; i++)
119 {
120 gfc_clear_ts (&gfc_current_ns->default_type[i]);
121 gfc_current_ns->set_flag[i] = 1;
122 }
123 }
124
125
126 /* Reset the implicit range flags. */
127
128 void
129 gfc_clear_new_implicit (void)
130 {
131 int i;
132
133 for (i = 0; i < GFC_LETTERS; i++)
134 new_flag[i] = 0;
135 }
136
137
138 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
139
140 try
141 gfc_add_new_implicit_range (int c1, int c2)
142 {
143 int i;
144
145 c1 -= 'a';
146 c2 -= 'a';
147
148 for (i = c1; i <= c2; i++)
149 {
150 if (new_flag[i])
151 {
152 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
153 i + 'A');
154 return FAILURE;
155 }
156
157 new_flag[i] = 1;
158 }
159
160 return SUCCESS;
161 }
162
163
164 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
165 the new implicit types back into the existing types will work. */
166
167 try
168 gfc_merge_new_implicit (gfc_typespec * ts)
169 {
170 int i;
171
172 if (gfc_current_ns->seen_implicit_none)
173 {
174 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
175 return FAILURE;
176 }
177
178 for (i = 0; i < GFC_LETTERS; i++)
179 {
180 if (new_flag[i])
181 {
182
183 if (gfc_current_ns->set_flag[i])
184 {
185 gfc_error ("Letter %c already has an IMPLICIT type at %C",
186 i + 'A');
187 return FAILURE;
188 }
189 gfc_current_ns->default_type[i] = *ts;
190 gfc_current_ns->set_flag[i] = 1;
191 }
192 }
193 return SUCCESS;
194 }
195
196
197 /* Given a symbol, return a pointer to the typespec for its default type. */
198
199 gfc_typespec *
200 gfc_get_default_type (gfc_symbol * sym, gfc_namespace * ns)
201 {
202 char letter;
203
204 letter = sym->name[0];
205 if (letter < 'a' || letter > 'z')
206 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
207
208 if (ns == NULL)
209 ns = gfc_current_ns;
210
211 return &ns->default_type[letter - 'a'];
212 }
213
214
215 /* Given a pointer to a symbol, set its type according to the first
216 letter of its name. Fails if the letter in question has no default
217 type. */
218
219 try
220 gfc_set_default_type (gfc_symbol * sym, int error_flag, gfc_namespace * ns)
221 {
222 gfc_typespec *ts;
223
224 if (sym->ts.type != BT_UNKNOWN)
225 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
226
227 ts = gfc_get_default_type (sym, ns);
228
229 if (ts->type == BT_UNKNOWN)
230 {
231 if (error_flag && !sym->attr.untyped)
232 {
233 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
234 sym->name, &sym->declared_at);
235 sym->attr.untyped = 1; /* Ensure we only give an error once. */
236 }
237
238 return FAILURE;
239 }
240
241 sym->ts = *ts;
242 sym->attr.implicit_type = 1;
243
244 return SUCCESS;
245 }
246
247
248 /******************** Symbol attribute stuff *********************/
249
250 /* This is a generic conflict-checker. We do this to avoid having a
251 single conflict in two places. */
252
253 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
254 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
255 #define conf_std(a, b, std) if (attr->a && attr->b)\
256 {\
257 a1 = a;\
258 a2 = b;\
259 standard = std;\
260 goto conflict_std;\
261 }
262
263 static try
264 check_conflict (symbol_attribute * attr, const char * name, locus * where)
265 {
266 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
267 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
268 *intrinsic = "INTRINSIC", *allocatable = "ALLOCATABLE",
269 *elemental = "ELEMENTAL", *private = "PRIVATE", *recursive = "RECURSIVE",
270 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
271 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
272 *function = "FUNCTION", *subroutine = "SUBROUTINE",
273 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
274 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
275 *cray_pointee = "CRAY POINTEE", *data = "DATA";
276 static const char *threadprivate = "THREADPRIVATE";
277
278 const char *a1, *a2;
279 int standard;
280
281 if (where == NULL)
282 where = &gfc_current_locus;
283
284 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
285 {
286 a1 = pointer;
287 a2 = intent;
288 goto conflict;
289 }
290
291 /* Check for attributes not allowed in a BLOCK DATA. */
292 if (gfc_current_state () == COMP_BLOCK_DATA)
293 {
294 a1 = NULL;
295
296 if (attr->in_namelist)
297 a1 = in_namelist;
298 if (attr->allocatable)
299 a1 = allocatable;
300 if (attr->external)
301 a1 = external;
302 if (attr->optional)
303 a1 = optional;
304 if (attr->access == ACCESS_PRIVATE)
305 a1 = private;
306 if (attr->access == ACCESS_PUBLIC)
307 a1 = public;
308 if (attr->intent != INTENT_UNKNOWN)
309 a1 = intent;
310
311 if (a1 != NULL)
312 {
313 gfc_error
314 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
315 where);
316 return FAILURE;
317 }
318 }
319
320 conf (dummy, save);
321 conf (dummy, threadprivate);
322 conf (pointer, target);
323 conf (pointer, external);
324 conf (pointer, intrinsic);
325 conf (pointer, elemental);
326 conf (allocatable, elemental);
327
328 conf (target, external);
329 conf (target, intrinsic);
330 conf (external, dimension); /* See Fortran 95's R504. */
331
332 conf (external, intrinsic);
333
334 if (attr->if_source || attr->contained)
335 {
336 conf (external, subroutine);
337 conf (external, function);
338 }
339
340 conf (allocatable, pointer);
341 conf_std (allocatable, dummy, GFC_STD_F2003);
342 conf_std (allocatable, function, GFC_STD_F2003);
343 conf_std (allocatable, result, GFC_STD_F2003);
344 conf (elemental, recursive);
345
346 conf (in_common, dummy);
347 conf (in_common, allocatable);
348 conf (in_common, result);
349 conf (in_common, save);
350 conf (result, save);
351
352 conf (dummy, result);
353
354 conf (in_equivalence, use_assoc);
355 conf (in_equivalence, dummy);
356 conf (in_equivalence, target);
357 conf (in_equivalence, pointer);
358 conf (in_equivalence, function);
359 conf (in_equivalence, result);
360 conf (in_equivalence, entry);
361 conf (in_equivalence, allocatable);
362 conf (in_equivalence, threadprivate);
363
364 conf (in_namelist, pointer);
365 conf (in_namelist, allocatable);
366
367 conf (entry, result);
368
369 conf (function, subroutine);
370
371 /* Cray pointer/pointee conflicts. */
372 conf (cray_pointer, cray_pointee);
373 conf (cray_pointer, dimension);
374 conf (cray_pointer, pointer);
375 conf (cray_pointer, target);
376 conf (cray_pointer, allocatable);
377 conf (cray_pointer, external);
378 conf (cray_pointer, intrinsic);
379 conf (cray_pointer, in_namelist);
380 conf (cray_pointer, function);
381 conf (cray_pointer, subroutine);
382 conf (cray_pointer, entry);
383
384 conf (cray_pointee, allocatable);
385 conf (cray_pointee, intent);
386 conf (cray_pointee, optional);
387 conf (cray_pointee, dummy);
388 conf (cray_pointee, target);
389 conf (cray_pointee, intrinsic);
390 conf (cray_pointee, pointer);
391 conf (cray_pointee, entry);
392 conf (cray_pointee, in_common);
393 conf (cray_pointee, in_equivalence);
394 conf (cray_pointee, threadprivate);
395
396 conf (data, dummy);
397 conf (data, function);
398 conf (data, result);
399 conf (data, allocatable);
400 conf (data, use_assoc);
401
402 a1 = gfc_code2string (flavors, attr->flavor);
403
404 if (attr->in_namelist
405 && attr->flavor != FL_VARIABLE
406 && attr->flavor != FL_UNKNOWN)
407 {
408
409 a2 = in_namelist;
410 goto conflict;
411 }
412
413 switch (attr->flavor)
414 {
415 case FL_PROGRAM:
416 case FL_BLOCK_DATA:
417 case FL_MODULE:
418 case FL_LABEL:
419 conf2 (dummy);
420 conf2 (save);
421 conf2 (pointer);
422 conf2 (target);
423 conf2 (external);
424 conf2 (intrinsic);
425 conf2 (allocatable);
426 conf2 (result);
427 conf2 (in_namelist);
428 conf2 (optional);
429 conf2 (function);
430 conf2 (subroutine);
431 conf2 (threadprivate);
432 break;
433
434 case FL_VARIABLE:
435 case FL_NAMELIST:
436 break;
437
438 case FL_PROCEDURE:
439 conf2 (intent);
440
441 if (attr->subroutine)
442 {
443 conf2(save);
444 conf2(pointer);
445 conf2(target);
446 conf2(allocatable);
447 conf2(result);
448 conf2(in_namelist);
449 conf2(function);
450 conf2(threadprivate);
451 }
452
453 switch (attr->proc)
454 {
455 case PROC_ST_FUNCTION:
456 conf2 (in_common);
457 conf2 (dummy);
458 break;
459
460 case PROC_MODULE:
461 conf2 (dummy);
462 break;
463
464 case PROC_DUMMY:
465 conf2 (result);
466 conf2 (in_common);
467 conf2 (save);
468 conf2 (threadprivate);
469 break;
470
471 default:
472 break;
473 }
474
475 break;
476
477 case FL_DERIVED:
478 conf2 (dummy);
479 conf2 (save);
480 conf2 (pointer);
481 conf2 (target);
482 conf2 (external);
483 conf2 (intrinsic);
484 conf2 (allocatable);
485 conf2 (optional);
486 conf2 (entry);
487 conf2 (function);
488 conf2 (subroutine);
489 conf2 (threadprivate);
490
491 if (attr->intent != INTENT_UNKNOWN)
492 {
493 a2 = intent;
494 goto conflict;
495 }
496 break;
497
498 case FL_PARAMETER:
499 conf2 (external);
500 conf2 (intrinsic);
501 conf2 (optional);
502 conf2 (allocatable);
503 conf2 (function);
504 conf2 (subroutine);
505 conf2 (entry);
506 conf2 (pointer);
507 conf2 (target);
508 conf2 (dummy);
509 conf2 (in_common);
510 conf2 (save);
511 conf2 (threadprivate);
512 break;
513
514 default:
515 break;
516 }
517
518 return SUCCESS;
519
520 conflict:
521 if (name == NULL)
522 gfc_error ("%s attribute conflicts with %s attribute at %L",
523 a1, a2, where);
524 else
525 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
526 a1, a2, name, where);
527
528 return FAILURE;
529
530 conflict_std:
531 if (name == NULL)
532 {
533 return gfc_notify_std (standard, "In the selected standard, %s attribute "
534 "conflicts with %s attribute at %L", a1, a2,
535 where);
536 }
537 else
538 {
539 return gfc_notify_std (standard, "In the selected standard, %s attribute "
540 "conflicts with %s attribute in '%s' at %L",
541 a1, a2, name, where);
542 }
543 }
544
545 #undef conf
546 #undef conf2
547 #undef conf_std
548
549
550 /* Mark a symbol as referenced. */
551
552 void
553 gfc_set_sym_referenced (gfc_symbol * sym)
554 {
555 if (sym->attr.referenced)
556 return;
557
558 sym->attr.referenced = 1;
559
560 /* Remember which order dummy variables are accessed in. */
561 if (sym->attr.dummy)
562 sym->dummy_order = next_dummy_order++;
563 }
564
565
566 /* Common subroutine called by attribute changing subroutines in order
567 to prevent them from changing a symbol that has been
568 use-associated. Returns zero if it is OK to change the symbol,
569 nonzero if not. */
570
571 static int
572 check_used (symbol_attribute * attr, const char * name, locus * where)
573 {
574
575 if (attr->use_assoc == 0)
576 return 0;
577
578 if (where == NULL)
579 where = &gfc_current_locus;
580
581 if (name == NULL)
582 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
583 where);
584 else
585 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
586 name, where);
587
588 return 1;
589 }
590
591
592 /* Used to prevent changing the attributes of a symbol after it has been
593 used. This check is only done for dummy variables as only these can be
594 used in specification expressions. Applying this to all symbols causes
595 an error when we reach the body of a contained function. */
596
597 static int
598 check_done (symbol_attribute * attr, locus * where)
599 {
600
601 if (!(attr->dummy && attr->referenced))
602 return 0;
603
604 if (where == NULL)
605 where = &gfc_current_locus;
606
607 gfc_error ("Cannot change attributes of symbol at %L"
608 " after it has been used", where);
609
610 return 1;
611 }
612
613
614 /* Generate an error because of a duplicate attribute. */
615
616 static void
617 duplicate_attr (const char *attr, locus * where)
618 {
619
620 if (where == NULL)
621 where = &gfc_current_locus;
622
623 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
624 }
625
626 /* Called from decl.c (attr_decl1) to check attributes, when declared separately. */
627
628 try
629 gfc_add_attribute (symbol_attribute * attr, locus * where,
630 unsigned int attr_intent)
631 {
632
633 if (check_used (attr, NULL, where)
634 || (attr_intent == 0 && check_done (attr, where)))
635 return FAILURE;
636
637 return check_conflict (attr, NULL, where);
638 }
639
640 try
641 gfc_add_allocatable (symbol_attribute * attr, locus * where)
642 {
643
644 if (check_used (attr, NULL, where) || check_done (attr, where))
645 return FAILURE;
646
647 if (attr->allocatable)
648 {
649 duplicate_attr ("ALLOCATABLE", where);
650 return FAILURE;
651 }
652
653 attr->allocatable = 1;
654 return check_conflict (attr, NULL, where);
655 }
656
657
658 try
659 gfc_add_dimension (symbol_attribute * attr, const char *name, locus * where)
660 {
661
662 if (check_used (attr, name, where) || check_done (attr, where))
663 return FAILURE;
664
665 if (attr->dimension)
666 {
667 duplicate_attr ("DIMENSION", where);
668 return FAILURE;
669 }
670
671 attr->dimension = 1;
672 return check_conflict (attr, name, where);
673 }
674
675
676 try
677 gfc_add_external (symbol_attribute * attr, locus * where)
678 {
679
680 if (check_used (attr, NULL, where) || check_done (attr, where))
681 return FAILURE;
682
683 if (attr->external)
684 {
685 duplicate_attr ("EXTERNAL", where);
686 return FAILURE;
687 }
688
689 attr->external = 1;
690
691 return check_conflict (attr, NULL, where);
692 }
693
694
695 try
696 gfc_add_intrinsic (symbol_attribute * attr, locus * where)
697 {
698
699 if (check_used (attr, NULL, where) || check_done (attr, where))
700 return FAILURE;
701
702 if (attr->intrinsic)
703 {
704 duplicate_attr ("INTRINSIC", where);
705 return FAILURE;
706 }
707
708 attr->intrinsic = 1;
709
710 return check_conflict (attr, NULL, where);
711 }
712
713
714 try
715 gfc_add_optional (symbol_attribute * attr, locus * where)
716 {
717
718 if (check_used (attr, NULL, where) || check_done (attr, where))
719 return FAILURE;
720
721 if (attr->optional)
722 {
723 duplicate_attr ("OPTIONAL", where);
724 return FAILURE;
725 }
726
727 attr->optional = 1;
728 return check_conflict (attr, NULL, where);
729 }
730
731
732 try
733 gfc_add_pointer (symbol_attribute * attr, locus * where)
734 {
735
736 if (check_used (attr, NULL, where) || check_done (attr, where))
737 return FAILURE;
738
739 attr->pointer = 1;
740 return check_conflict (attr, NULL, where);
741 }
742
743
744 try
745 gfc_add_cray_pointer (symbol_attribute * attr, locus * where)
746 {
747
748 if (check_used (attr, NULL, where) || check_done (attr, where))
749 return FAILURE;
750
751 attr->cray_pointer = 1;
752 return check_conflict (attr, NULL, where);
753 }
754
755
756 try
757 gfc_add_cray_pointee (symbol_attribute * attr, locus * where)
758 {
759
760 if (check_used (attr, NULL, where) || check_done (attr, where))
761 return FAILURE;
762
763 if (attr->cray_pointee)
764 {
765 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
766 " statements.", where);
767 return FAILURE;
768 }
769
770 attr->cray_pointee = 1;
771 return check_conflict (attr, NULL, where);
772 }
773
774
775 try
776 gfc_add_result (symbol_attribute * attr, const char *name, locus * where)
777 {
778
779 if (check_used (attr, name, where) || check_done (attr, where))
780 return FAILURE;
781
782 attr->result = 1;
783 return check_conflict (attr, name, where);
784 }
785
786
787 try
788 gfc_add_save (symbol_attribute * attr, const char *name, locus * where)
789 {
790
791 if (check_used (attr, name, where))
792 return FAILURE;
793
794 if (gfc_pure (NULL))
795 {
796 gfc_error
797 ("SAVE attribute at %L cannot be specified in a PURE procedure",
798 where);
799 return FAILURE;
800 }
801
802 if (attr->save)
803 {
804 if (gfc_notify_std (GFC_STD_LEGACY,
805 "Duplicate SAVE attribute specified at %L",
806 where)
807 == FAILURE)
808 return FAILURE;
809 }
810
811 attr->save = 1;
812 return check_conflict (attr, name, where);
813 }
814
815
816 try
817 gfc_add_threadprivate (symbol_attribute * attr, const char *name, locus * where)
818 {
819 if (check_used (attr, name, where))
820 return FAILURE;
821
822 if (attr->threadprivate)
823 {
824 duplicate_attr ("THREADPRIVATE", where);
825 return FAILURE;
826 }
827
828 attr->threadprivate = 1;
829 return check_conflict (attr, name, where);
830 }
831
832
833 try
834 gfc_add_target (symbol_attribute * attr, locus * where)
835 {
836
837 if (check_used (attr, NULL, where) || check_done (attr, where))
838 return FAILURE;
839
840 if (attr->target)
841 {
842 duplicate_attr ("TARGET", where);
843 return FAILURE;
844 }
845
846 attr->target = 1;
847 return check_conflict (attr, NULL, where);
848 }
849
850
851 try
852 gfc_add_dummy (symbol_attribute * attr, const char *name, locus * where)
853 {
854
855 if (check_used (attr, name, where))
856 return FAILURE;
857
858 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
859 attr->dummy = 1;
860 return check_conflict (attr, name, where);
861 }
862
863
864 try
865 gfc_add_in_common (symbol_attribute * attr, const char *name, locus * where)
866 {
867
868 if (check_used (attr, name, where) || check_done (attr, where))
869 return FAILURE;
870
871 /* Duplicate attribute already checked for. */
872 attr->in_common = 1;
873 if (check_conflict (attr, name, where) == FAILURE)
874 return FAILURE;
875
876 if (attr->flavor == FL_VARIABLE)
877 return SUCCESS;
878
879 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
880 }
881
882 try
883 gfc_add_in_equivalence (symbol_attribute * attr, const char *name, locus * where)
884 {
885
886 /* Duplicate attribute already checked for. */
887 attr->in_equivalence = 1;
888 if (check_conflict (attr, name, where) == FAILURE)
889 return FAILURE;
890
891 if (attr->flavor == FL_VARIABLE)
892 return SUCCESS;
893
894 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
895 }
896
897
898 try
899 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
900 {
901
902 if (check_used (attr, name, where))
903 return FAILURE;
904
905 attr->data = 1;
906 return check_conflict (attr, name, where);
907 }
908
909
910 try
911 gfc_add_in_namelist (symbol_attribute * attr, const char *name,
912 locus * where)
913 {
914
915 attr->in_namelist = 1;
916 return check_conflict (attr, name, where);
917 }
918
919
920 try
921 gfc_add_sequence (symbol_attribute * attr, const char *name, locus * where)
922 {
923
924 if (check_used (attr, name, where))
925 return FAILURE;
926
927 attr->sequence = 1;
928 return check_conflict (attr, name, where);
929 }
930
931
932 try
933 gfc_add_elemental (symbol_attribute * attr, locus * where)
934 {
935
936 if (check_used (attr, NULL, where) || check_done (attr, where))
937 return FAILURE;
938
939 attr->elemental = 1;
940 return check_conflict (attr, NULL, where);
941 }
942
943
944 try
945 gfc_add_pure (symbol_attribute * attr, locus * where)
946 {
947
948 if (check_used (attr, NULL, where) || check_done (attr, where))
949 return FAILURE;
950
951 attr->pure = 1;
952 return check_conflict (attr, NULL, where);
953 }
954
955
956 try
957 gfc_add_recursive (symbol_attribute * attr, locus * where)
958 {
959
960 if (check_used (attr, NULL, where) || check_done (attr, where))
961 return FAILURE;
962
963 attr->recursive = 1;
964 return check_conflict (attr, NULL, where);
965 }
966
967
968 try
969 gfc_add_entry (symbol_attribute * attr, const char *name, locus * where)
970 {
971
972 if (check_used (attr, name, where))
973 return FAILURE;
974
975 if (attr->entry)
976 {
977 duplicate_attr ("ENTRY", where);
978 return FAILURE;
979 }
980
981 attr->entry = 1;
982 return check_conflict (attr, name, where);
983 }
984
985
986 try
987 gfc_add_function (symbol_attribute * attr, const char *name, locus * where)
988 {
989
990 if (attr->flavor != FL_PROCEDURE
991 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
992 return FAILURE;
993
994 attr->function = 1;
995 return check_conflict (attr, name, where);
996 }
997
998
999 try
1000 gfc_add_subroutine (symbol_attribute * attr, const char *name, locus * where)
1001 {
1002
1003 if (attr->flavor != FL_PROCEDURE
1004 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1005 return FAILURE;
1006
1007 attr->subroutine = 1;
1008 return check_conflict (attr, name, where);
1009 }
1010
1011
1012 try
1013 gfc_add_generic (symbol_attribute * attr, const char *name, locus * where)
1014 {
1015
1016 if (attr->flavor != FL_PROCEDURE
1017 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1018 return FAILURE;
1019
1020 attr->generic = 1;
1021 return check_conflict (attr, name, where);
1022 }
1023
1024
1025 /* Flavors are special because some flavors are not what Fortran
1026 considers attributes and can be reaffirmed multiple times. */
1027
1028 try
1029 gfc_add_flavor (symbol_attribute * attr, sym_flavor f, const char *name,
1030 locus * where)
1031 {
1032
1033 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1034 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1035 || f == FL_NAMELIST) && check_used (attr, name, where))
1036 return FAILURE;
1037
1038 if (attr->flavor == f && f == FL_VARIABLE)
1039 return SUCCESS;
1040
1041 if (attr->flavor != FL_UNKNOWN)
1042 {
1043 if (where == NULL)
1044 where = &gfc_current_locus;
1045
1046 gfc_error ("%s attribute conflicts with %s attribute at %L",
1047 gfc_code2string (flavors, attr->flavor),
1048 gfc_code2string (flavors, f), where);
1049
1050 return FAILURE;
1051 }
1052
1053 attr->flavor = f;
1054
1055 return check_conflict (attr, name, where);
1056 }
1057
1058
1059 try
1060 gfc_add_procedure (symbol_attribute * attr, procedure_type t,
1061 const char *name, locus * where)
1062 {
1063
1064 if (check_used (attr, name, where) || check_done (attr, where))
1065 return FAILURE;
1066
1067 if (attr->flavor != FL_PROCEDURE
1068 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1069 return FAILURE;
1070
1071 if (where == NULL)
1072 where = &gfc_current_locus;
1073
1074 if (attr->proc != PROC_UNKNOWN)
1075 {
1076 gfc_error ("%s procedure at %L is already declared as %s procedure",
1077 gfc_code2string (procedures, t), where,
1078 gfc_code2string (procedures, attr->proc));
1079
1080 return FAILURE;
1081 }
1082
1083 attr->proc = t;
1084
1085 /* Statement functions are always scalar and functions. */
1086 if (t == PROC_ST_FUNCTION
1087 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1088 || attr->dimension))
1089 return FAILURE;
1090
1091 return check_conflict (attr, name, where);
1092 }
1093
1094
1095 try
1096 gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
1097 {
1098
1099 if (check_used (attr, NULL, where))
1100 return FAILURE;
1101
1102 if (attr->intent == INTENT_UNKNOWN)
1103 {
1104 attr->intent = intent;
1105 return check_conflict (attr, NULL, where);
1106 }
1107
1108 if (where == NULL)
1109 where = &gfc_current_locus;
1110
1111 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1112 gfc_intent_string (attr->intent),
1113 gfc_intent_string (intent), where);
1114
1115 return FAILURE;
1116 }
1117
1118
1119 /* No checks for use-association in public and private statements. */
1120
1121 try
1122 gfc_add_access (symbol_attribute * attr, gfc_access access,
1123 const char *name, locus * where)
1124 {
1125
1126 if (attr->access == ACCESS_UNKNOWN)
1127 {
1128 attr->access = access;
1129 return check_conflict (attr, name, where);
1130 }
1131
1132 if (where == NULL)
1133 where = &gfc_current_locus;
1134 gfc_error ("ACCESS specification at %L was already specified", where);
1135
1136 return FAILURE;
1137 }
1138
1139
1140 try
1141 gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
1142 gfc_formal_arglist * formal, locus * where)
1143 {
1144
1145 if (check_used (&sym->attr, sym->name, where))
1146 return FAILURE;
1147
1148 if (where == NULL)
1149 where = &gfc_current_locus;
1150
1151 if (sym->attr.if_source != IFSRC_UNKNOWN
1152 && sym->attr.if_source != IFSRC_DECL)
1153 {
1154 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1155 sym->name, where);
1156 return FAILURE;
1157 }
1158
1159 sym->formal = formal;
1160 sym->attr.if_source = source;
1161
1162 return SUCCESS;
1163 }
1164
1165
1166 /* Add a type to a symbol. */
1167
1168 try
1169 gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
1170 {
1171 sym_flavor flavor;
1172
1173 /* TODO: This is legal if it is reaffirming an implicit type.
1174 if (check_done (&sym->attr, where))
1175 return FAILURE;*/
1176
1177 if (where == NULL)
1178 where = &gfc_current_locus;
1179
1180 if (sym->ts.type != BT_UNKNOWN)
1181 {
1182 const char *msg = "Symbol '%s' at %L already has basic type of %s";
1183 if (!(sym->ts.type == ts->type
1184 && (sym->attr.flavor == FL_PROCEDURE || sym->attr.result))
1185 || gfc_notification_std (GFC_STD_GNU) == ERROR
1186 || pedantic)
1187 {
1188 gfc_error (msg, sym->name, where, gfc_basic_typename (sym->ts.type));
1189 return FAILURE;
1190 }
1191 else if (gfc_notify_std (GFC_STD_GNU, msg, sym->name, where,
1192 gfc_basic_typename (sym->ts.type)) == FAILURE)
1193 return FAILURE;
1194 }
1195
1196 flavor = sym->attr.flavor;
1197
1198 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1199 || flavor == FL_LABEL || (flavor == FL_PROCEDURE
1200 && sym->attr.subroutine)
1201 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1202 {
1203 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1204 return FAILURE;
1205 }
1206
1207 sym->ts = *ts;
1208 return SUCCESS;
1209 }
1210
1211
1212 /* Clears all attributes. */
1213
1214 void
1215 gfc_clear_attr (symbol_attribute * attr)
1216 {
1217 memset (attr, 0, sizeof(symbol_attribute));
1218 }
1219
1220
1221 /* Check for missing attributes in the new symbol. Currently does
1222 nothing, but it's not clear that it is unnecessary yet. */
1223
1224 try
1225 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1226 locus * where ATTRIBUTE_UNUSED)
1227 {
1228
1229 return SUCCESS;
1230 }
1231
1232
1233 /* Copy an attribute to a symbol attribute, bit by bit. Some
1234 attributes have a lot of side-effects but cannot be present given
1235 where we are called from, so we ignore some bits. */
1236
1237 try
1238 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1239 {
1240
1241 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1242 goto fail;
1243
1244 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1245 goto fail;
1246 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1247 goto fail;
1248 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1249 goto fail;
1250 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1251 goto fail;
1252 if (src->threadprivate && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
1253 goto fail;
1254 if (src->target && gfc_add_target (dest, where) == FAILURE)
1255 goto fail;
1256 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1257 goto fail;
1258 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1259 goto fail;
1260 if (src->entry)
1261 dest->entry = 1;
1262
1263 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1264 goto fail;
1265
1266 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1267 goto fail;
1268
1269 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1270 goto fail;
1271 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1272 goto fail;
1273 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1274 goto fail;
1275
1276 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1277 goto fail;
1278 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1279 goto fail;
1280 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1281 goto fail;
1282 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1283 goto fail;
1284
1285 if (src->flavor != FL_UNKNOWN
1286 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1287 goto fail;
1288
1289 if (src->intent != INTENT_UNKNOWN
1290 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1291 goto fail;
1292
1293 if (src->access != ACCESS_UNKNOWN
1294 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1295 goto fail;
1296
1297 if (gfc_missing_attr (dest, where) == FAILURE)
1298 goto fail;
1299
1300 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1301 goto fail;
1302 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1303 goto fail;
1304
1305 /* The subroutines that set these bits also cause flavors to be set,
1306 and that has already happened in the original, so don't let it
1307 happen again. */
1308 if (src->external)
1309 dest->external = 1;
1310 if (src->intrinsic)
1311 dest->intrinsic = 1;
1312
1313 return SUCCESS;
1314
1315 fail:
1316 return FAILURE;
1317 }
1318
1319
1320 /************** Component name management ************/
1321
1322 /* Component names of a derived type form their own little namespaces
1323 that are separate from all other spaces. The space is composed of
1324 a singly linked list of gfc_component structures whose head is
1325 located in the parent symbol. */
1326
1327
1328 /* Add a component name to a symbol. The call fails if the name is
1329 already present. On success, the component pointer is modified to
1330 point to the additional component structure. */
1331
1332 try
1333 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1334 {
1335 gfc_component *p, *tail;
1336
1337 tail = NULL;
1338
1339 for (p = sym->components; p; p = p->next)
1340 {
1341 if (strcmp (p->name, name) == 0)
1342 {
1343 gfc_error ("Component '%s' at %C already declared at %L",
1344 name, &p->loc);
1345 return FAILURE;
1346 }
1347
1348 tail = p;
1349 }
1350
1351 /* Allocate a new component. */
1352 p = gfc_get_component ();
1353
1354 if (tail == NULL)
1355 sym->components = p;
1356 else
1357 tail->next = p;
1358
1359 p->name = gfc_get_string (name);
1360 p->loc = gfc_current_locus;
1361
1362 *component = p;
1363 return SUCCESS;
1364 }
1365
1366
1367 /* Recursive search for a renamed derived type. */
1368
1369 static gfc_symbol *
1370 find_renamed_type (gfc_symbol * der, gfc_symtree * st)
1371 {
1372 gfc_symbol *sym = NULL;
1373
1374 if (st == NULL)
1375 return NULL;
1376
1377 sym = find_renamed_type (der, st->left);
1378 if (sym != NULL)
1379 return sym;
1380
1381 sym = find_renamed_type (der, st->right);
1382 if (sym != NULL)
1383 return sym;
1384
1385 if (strcmp (der->name, st->n.sym->name) == 0
1386 && st->n.sym->attr.use_assoc
1387 && st->n.sym->attr.flavor == FL_DERIVED
1388 && gfc_compare_derived_types (der, st->n.sym))
1389 sym = st->n.sym;
1390
1391 return sym;
1392 }
1393
1394 /* Recursive function to switch derived types of all symbols in a
1395 namespace. The formal namespaces contain references to derived
1396 types that can be left hanging by gfc_use_derived, so these must
1397 be switched too. */
1398
1399 static void
1400 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1401 {
1402 gfc_symbol *sym;
1403
1404 if (st == NULL)
1405 return;
1406
1407 sym = st->n.sym;
1408 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1409 sym->ts.derived = to;
1410
1411 if (sym->formal_ns && sym->formal_ns->sym_root)
1412 switch_types (sym->formal_ns->sym_root, from, to);
1413
1414 switch_types (st->left, from, to);
1415 switch_types (st->right, from, to);
1416 }
1417
1418
1419 /* This subroutine is called when a derived type is used in order to
1420 make the final determination about which version to use. The
1421 standard requires that a type be defined before it is 'used', but
1422 such types can appear in IMPLICIT statements before the actual
1423 definition. 'Using' in this context means declaring a variable to
1424 be that type or using the type constructor.
1425
1426 If a type is used and the components haven't been defined, then we
1427 have to have a derived type in a parent unit. We find the node in
1428 the other namespace and point the symtree node in this namespace to
1429 that node. Further reference to this name point to the correct
1430 node. If we can't find the node in a parent namespace, then we have
1431 an error.
1432
1433 This subroutine takes a pointer to a symbol node and returns a
1434 pointer to the translated node or NULL for an error. Usually there
1435 is no translation and we return the node we were passed. */
1436
1437 gfc_symbol *
1438 gfc_use_derived (gfc_symbol * sym)
1439 {
1440 gfc_symbol *s;
1441 gfc_typespec *t;
1442 gfc_symtree *st;
1443 gfc_component *c;
1444 gfc_namespace *ns;
1445 int i;
1446
1447 if (sym->ns->parent == NULL || sym->ns != gfc_current_ns)
1448 {
1449 /* Already defined in highest possible or sibling namespace. */
1450 if (sym->components != NULL)
1451 return sym;
1452
1453 /* There is no scope for finding a definition elsewhere. */
1454 else
1455 goto bad;
1456 }
1457 else
1458 {
1459 /* This type can only be locally associated. */
1460 if (!(sym->attr.use_assoc || sym->attr.sequence))
1461 return sym;
1462
1463 /* Derived types must be defined within an interface. */
1464 if (gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
1465 return sym;
1466 }
1467
1468 /* Look in parent namespace for a derived type of the same name. */
1469 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1470 {
1471 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1472 return NULL;
1473 }
1474
1475 /* Look in sibling namespaces for a derived type of the same name. */
1476 if (s == NULL && sym->attr.use_assoc && sym->ns->sibling)
1477 {
1478 ns = sym->ns->sibling;
1479 for (; ns; ns = ns->sibling)
1480 {
1481 s = NULL;
1482 if (sym->ns == ns)
1483 break;
1484
1485 if (gfc_find_symbol (sym->name, ns, 1, &s))
1486 {
1487 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1488 return NULL;
1489 }
1490
1491 if (s != NULL && s->attr.flavor == FL_DERIVED)
1492 break;
1493 }
1494 }
1495
1496 if (s == NULL || s->attr.flavor != FL_DERIVED)
1497 {
1498 /* Check to see if type has been renamed in parent namespace. */
1499 s = find_renamed_type (sym, sym->ns->parent->sym_root);
1500 if (s != NULL)
1501 goto return_use_assoc;
1502
1503 /* See if sym is identical to renamed, use-associated derived
1504 types in sibling namespaces. */
1505 if (sym->attr.use_assoc
1506 && sym->ns->parent
1507 && sym->ns->parent->contained)
1508 {
1509 ns = sym->ns->parent->contained;
1510 for (; ns; ns = ns->sibling)
1511 {
1512 if (sym->ns == ns)
1513 break;
1514
1515 s = find_renamed_type (sym, ns->sym_root);
1516
1517 if (s != NULL)
1518 goto return_use_assoc;
1519 }
1520 }
1521
1522 /* The local definition is all that there is. */
1523 if (sym->components != NULL)
1524 {
1525 /* Non-pointer derived type components have already been checked
1526 but pointer types need to be correctly associated. */
1527 for (c = sym->components; c; c = c->next)
1528 if (c->ts.type == BT_DERIVED && c->pointer)
1529 c->ts.derived = gfc_use_derived (c->ts.derived);
1530
1531 return sym;
1532 }
1533 }
1534
1535 /* Although the parent namespace has a derived type of the same name, it is
1536 not an identical derived type and so cannot be used. */
1537 if (s != NULL && sym->components != NULL && !gfc_compare_derived_types (s, sym))
1538 return sym;
1539
1540 if (s == NULL || s->attr.flavor != FL_DERIVED)
1541 goto bad;
1542
1543 /* Get rid of symbol sym, translating all references to s. */
1544 for (i = 0; i < GFC_LETTERS; i++)
1545 {
1546 t = &sym->ns->default_type[i];
1547 if (t->derived == sym)
1548 t->derived = s;
1549 }
1550
1551 if (sym->attr.use_assoc)
1552 goto return_use_assoc;
1553
1554 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1555 st->n.sym = s;
1556
1557 s->refs++;
1558
1559 /* Unlink from list of modified symbols. */
1560 gfc_commit_symbol (sym);
1561
1562 switch_types (sym->ns->sym_root, sym, s);
1563
1564 /* TODO: Also have to replace sym -> s in other lists like
1565 namelists, common lists and interface lists. */
1566 gfc_free_symbol (sym);
1567
1568 return s;
1569
1570 return_use_assoc:
1571 /* Use associated types are not freed at this stage because some
1572 references remain to 'sym'. We retain the symbol and leave it
1573 to be cleaned up by gfc_free_namespace, at the end of the
1574 compilation. */
1575 switch_types (sym->ns->sym_root, sym, s);
1576 return s;
1577
1578 bad:
1579 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1580 sym->name);
1581 return NULL;
1582 }
1583
1584
1585 /* Given a derived type node and a component name, try to locate the
1586 component structure. Returns the NULL pointer if the component is
1587 not found or the components are private. */
1588
1589 gfc_component *
1590 gfc_find_component (gfc_symbol * sym, const char *name)
1591 {
1592 gfc_component *p;
1593
1594 if (name == NULL)
1595 return NULL;
1596
1597 sym = gfc_use_derived (sym);
1598
1599 if (sym == NULL)
1600 return NULL;
1601
1602 for (p = sym->components; p; p = p->next)
1603 if (strcmp (p->name, name) == 0)
1604 break;
1605
1606 if (p == NULL)
1607 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1608 name, sym->name);
1609 else
1610 {
1611 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1612 {
1613 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1614 name, sym->name);
1615 p = NULL;
1616 }
1617 }
1618
1619 return p;
1620 }
1621
1622
1623 /* Given a symbol, free all of the component structures and everything
1624 they point to. */
1625
1626 static void
1627 free_components (gfc_component * p)
1628 {
1629 gfc_component *q;
1630
1631 for (; p; p = q)
1632 {
1633 q = p->next;
1634
1635 gfc_free_array_spec (p->as);
1636 gfc_free_expr (p->initializer);
1637
1638 gfc_free (p);
1639 }
1640 }
1641
1642
1643 /* Set component attributes from a standard symbol attribute
1644 structure. */
1645
1646 void
1647 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1648 {
1649
1650 c->dimension = attr->dimension;
1651 c->pointer = attr->pointer;
1652 }
1653
1654
1655 /* Get a standard symbol attribute structure given the component
1656 structure. */
1657
1658 void
1659 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1660 {
1661
1662 gfc_clear_attr (attr);
1663 attr->dimension = c->dimension;
1664 attr->pointer = c->pointer;
1665 }
1666
1667
1668 /******************** Statement label management ********************/
1669
1670 /* Comparison function for statement labels, used for managing the
1671 binary tree. */
1672
1673 static int
1674 compare_st_labels (void * a1, void * b1)
1675 {
1676 int a = ((gfc_st_label *)a1)->value;
1677 int b = ((gfc_st_label *)b1)->value;
1678
1679 return (b - a);
1680 }
1681
1682
1683 /* Free a single gfc_st_label structure, making sure the tree is not
1684 messed up. This function is called only when some parse error
1685 occurs. */
1686
1687 void
1688 gfc_free_st_label (gfc_st_label * label)
1689 {
1690 if (label == NULL)
1691 return;
1692
1693 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
1694
1695 if (label->format != NULL)
1696 gfc_free_expr (label->format);
1697
1698 gfc_free (label);
1699 }
1700
1701 /* Free a whole tree of gfc_st_label structures. */
1702
1703 static void
1704 free_st_labels (gfc_st_label * label)
1705 {
1706 if (label == NULL)
1707 return;
1708
1709 free_st_labels (label->left);
1710 free_st_labels (label->right);
1711
1712 if (label->format != NULL)
1713 gfc_free_expr (label->format);
1714 gfc_free (label);
1715 }
1716
1717
1718 /* Given a label number, search for and return a pointer to the label
1719 structure, creating it if it does not exist. */
1720
1721 gfc_st_label *
1722 gfc_get_st_label (int labelno)
1723 {
1724 gfc_st_label *lp;
1725
1726 /* First see if the label is already in this namespace. */
1727 lp = gfc_current_ns->st_labels;
1728 while (lp)
1729 {
1730 if (lp->value == labelno)
1731 return lp;
1732
1733 if (lp->value < labelno)
1734 lp = lp->left;
1735 else
1736 lp = lp->right;
1737 }
1738
1739 lp = gfc_getmem (sizeof (gfc_st_label));
1740
1741 lp->value = labelno;
1742 lp->defined = ST_LABEL_UNKNOWN;
1743 lp->referenced = ST_LABEL_UNKNOWN;
1744
1745 gfc_insert_bbt (&gfc_current_ns->st_labels, lp, compare_st_labels);
1746
1747 return lp;
1748 }
1749
1750
1751 /* Called when a statement with a statement label is about to be
1752 accepted. We add the label to the list of the current namespace,
1753 making sure it hasn't been defined previously and referenced
1754 correctly. */
1755
1756 void
1757 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1758 {
1759 int labelno;
1760
1761 labelno = lp->value;
1762
1763 if (lp->defined != ST_LABEL_UNKNOWN)
1764 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1765 &lp->where, label_locus);
1766 else
1767 {
1768 lp->where = *label_locus;
1769
1770 switch (type)
1771 {
1772 case ST_LABEL_FORMAT:
1773 if (lp->referenced == ST_LABEL_TARGET)
1774 gfc_error ("Label %d at %C already referenced as branch target",
1775 labelno);
1776 else
1777 lp->defined = ST_LABEL_FORMAT;
1778
1779 break;
1780
1781 case ST_LABEL_TARGET:
1782 if (lp->referenced == ST_LABEL_FORMAT)
1783 gfc_error ("Label %d at %C already referenced as a format label",
1784 labelno);
1785 else
1786 lp->defined = ST_LABEL_TARGET;
1787
1788 break;
1789
1790 default:
1791 lp->defined = ST_LABEL_BAD_TARGET;
1792 lp->referenced = ST_LABEL_BAD_TARGET;
1793 }
1794 }
1795 }
1796
1797
1798 /* Reference a label. Given a label and its type, see if that
1799 reference is consistent with what is known about that label,
1800 updating the unknown state. Returns FAILURE if something goes
1801 wrong. */
1802
1803 try
1804 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1805 {
1806 gfc_sl_type label_type;
1807 int labelno;
1808 try rc;
1809
1810 if (lp == NULL)
1811 return SUCCESS;
1812
1813 labelno = lp->value;
1814
1815 if (lp->defined != ST_LABEL_UNKNOWN)
1816 label_type = lp->defined;
1817 else
1818 {
1819 label_type = lp->referenced;
1820 lp->where = gfc_current_locus;
1821 }
1822
1823 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1824 {
1825 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1826 rc = FAILURE;
1827 goto done;
1828 }
1829
1830 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1831 && type == ST_LABEL_FORMAT)
1832 {
1833 gfc_error ("Label %d at %C previously used as branch target", labelno);
1834 rc = FAILURE;
1835 goto done;
1836 }
1837
1838 lp->referenced = type;
1839 rc = SUCCESS;
1840
1841 done:
1842 return rc;
1843 }
1844
1845
1846 /************** Symbol table management subroutines ****************/
1847
1848 /* Basic details: Fortran 95 requires a potentially unlimited number
1849 of distinct namespaces when compiling a program unit. This case
1850 occurs during a compilation of internal subprograms because all of
1851 the internal subprograms must be read before we can start
1852 generating code for the host.
1853
1854 Given the tricky nature of the Fortran grammar, we must be able to
1855 undo changes made to a symbol table if the current interpretation
1856 of a statement is found to be incorrect. Whenever a symbol is
1857 looked up, we make a copy of it and link to it. All of these
1858 symbols are kept in a singly linked list so that we can commit or
1859 undo the changes at a later time.
1860
1861 A symtree may point to a symbol node outside of its namespace. In
1862 this case, that symbol has been used as a host associated variable
1863 at some previous time. */
1864
1865 /* Allocate a new namespace structure. Copies the implicit types from
1866 PARENT if PARENT_TYPES is set. */
1867
1868 gfc_namespace *
1869 gfc_get_namespace (gfc_namespace * parent, int parent_types)
1870 {
1871 gfc_namespace *ns;
1872 gfc_typespec *ts;
1873 gfc_intrinsic_op in;
1874 int i;
1875
1876 ns = gfc_getmem (sizeof (gfc_namespace));
1877 ns->sym_root = NULL;
1878 ns->uop_root = NULL;
1879 ns->default_access = ACCESS_UNKNOWN;
1880 ns->parent = parent;
1881
1882 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1883 ns->operator_access[in] = ACCESS_UNKNOWN;
1884
1885 /* Initialize default implicit types. */
1886 for (i = 'a'; i <= 'z'; i++)
1887 {
1888 ns->set_flag[i - 'a'] = 0;
1889 ts = &ns->default_type[i - 'a'];
1890
1891 if (parent_types && ns->parent != NULL)
1892 {
1893 /* Copy parent settings */
1894 *ts = ns->parent->default_type[i - 'a'];
1895 continue;
1896 }
1897
1898 if (gfc_option.flag_implicit_none != 0)
1899 {
1900 gfc_clear_ts (ts);
1901 continue;
1902 }
1903
1904 if ('i' <= i && i <= 'n')
1905 {
1906 ts->type = BT_INTEGER;
1907 ts->kind = gfc_default_integer_kind;
1908 }
1909 else
1910 {
1911 ts->type = BT_REAL;
1912 ts->kind = gfc_default_real_kind;
1913 }
1914 }
1915
1916 ns->refs = 1;
1917
1918 return ns;
1919 }
1920
1921
1922 /* Comparison function for symtree nodes. */
1923
1924 static int
1925 compare_symtree (void * _st1, void * _st2)
1926 {
1927 gfc_symtree *st1, *st2;
1928
1929 st1 = (gfc_symtree *) _st1;
1930 st2 = (gfc_symtree *) _st2;
1931
1932 return strcmp (st1->name, st2->name);
1933 }
1934
1935
1936 /* Allocate a new symtree node and associate it with the new symbol. */
1937
1938 gfc_symtree *
1939 gfc_new_symtree (gfc_symtree ** root, const char *name)
1940 {
1941 gfc_symtree *st;
1942
1943 st = gfc_getmem (sizeof (gfc_symtree));
1944 st->name = gfc_get_string (name);
1945
1946 gfc_insert_bbt (root, st, compare_symtree);
1947 return st;
1948 }
1949
1950
1951 /* Delete a symbol from the tree. Does not free the symbol itself! */
1952
1953 static void
1954 delete_symtree (gfc_symtree ** root, const char *name)
1955 {
1956 gfc_symtree st, *st0;
1957
1958 st0 = gfc_find_symtree (*root, name);
1959
1960 st.name = gfc_get_string (name);
1961 gfc_delete_bbt (root, &st, compare_symtree);
1962
1963 gfc_free (st0);
1964 }
1965
1966
1967 /* Given a root symtree node and a name, try to find the symbol within
1968 the namespace. Returns NULL if the symbol is not found. */
1969
1970 gfc_symtree *
1971 gfc_find_symtree (gfc_symtree * st, const char *name)
1972 {
1973 int c;
1974
1975 while (st != NULL)
1976 {
1977 c = strcmp (name, st->name);
1978 if (c == 0)
1979 return st;
1980
1981 st = (c < 0) ? st->left : st->right;
1982 }
1983
1984 return NULL;
1985 }
1986
1987
1988 /* Given a name find a user operator node, creating it if it doesn't
1989 exist. These are much simpler than symbols because they can't be
1990 ambiguous with one another. */
1991
1992 gfc_user_op *
1993 gfc_get_uop (const char *name)
1994 {
1995 gfc_user_op *uop;
1996 gfc_symtree *st;
1997
1998 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1999 if (st != NULL)
2000 return st->n.uop;
2001
2002 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
2003
2004 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
2005 uop->name = gfc_get_string (name);
2006 uop->access = ACCESS_UNKNOWN;
2007 uop->ns = gfc_current_ns;
2008
2009 return uop;
2010 }
2011
2012
2013 /* Given a name find the user operator node. Returns NULL if it does
2014 not exist. */
2015
2016 gfc_user_op *
2017 gfc_find_uop (const char *name, gfc_namespace * ns)
2018 {
2019 gfc_symtree *st;
2020
2021 if (ns == NULL)
2022 ns = gfc_current_ns;
2023
2024 st = gfc_find_symtree (ns->uop_root, name);
2025 return (st == NULL) ? NULL : st->n.uop;
2026 }
2027
2028
2029 /* Remove a gfc_symbol structure and everything it points to. */
2030
2031 void
2032 gfc_free_symbol (gfc_symbol * sym)
2033 {
2034
2035 if (sym == NULL)
2036 return;
2037
2038 gfc_free_array_spec (sym->as);
2039
2040 free_components (sym->components);
2041
2042 gfc_free_expr (sym->value);
2043
2044 gfc_free_namelist (sym->namelist);
2045
2046 gfc_free_namespace (sym->formal_ns);
2047
2048 gfc_free_interface (sym->generic);
2049
2050 gfc_free_formal_arglist (sym->formal);
2051
2052 gfc_free (sym);
2053 }
2054
2055
2056 /* Allocate and initialize a new symbol node. */
2057
2058 gfc_symbol *
2059 gfc_new_symbol (const char *name, gfc_namespace * ns)
2060 {
2061 gfc_symbol *p;
2062
2063 p = gfc_getmem (sizeof (gfc_symbol));
2064
2065 gfc_clear_ts (&p->ts);
2066 gfc_clear_attr (&p->attr);
2067 p->ns = ns;
2068
2069 p->declared_at = gfc_current_locus;
2070
2071 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2072 gfc_internal_error ("new_symbol(): Symbol name too long");
2073
2074 p->name = gfc_get_string (name);
2075 return p;
2076 }
2077
2078
2079 /* Generate an error if a symbol is ambiguous. */
2080
2081 static void
2082 ambiguous_symbol (const char *name, gfc_symtree * st)
2083 {
2084
2085 if (st->n.sym->module)
2086 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2087 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
2088 else
2089 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2090 "from current program unit", name, st->n.sym->name);
2091 }
2092
2093
2094 /* Search for a symtree starting in the current namespace, resorting to
2095 any parent namespaces if requested by a nonzero parent_flag.
2096 Returns nonzero if the name is ambiguous. */
2097
2098 int
2099 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
2100 gfc_symtree ** result)
2101 {
2102 gfc_symtree *st;
2103
2104 if (ns == NULL)
2105 ns = gfc_current_ns;
2106
2107 do
2108 {
2109 st = gfc_find_symtree (ns->sym_root, name);
2110 if (st != NULL)
2111 {
2112 *result = st;
2113 if (st->ambiguous)
2114 {
2115 ambiguous_symbol (name, st);
2116 return 1;
2117 }
2118
2119 return 0;
2120 }
2121
2122 if (!parent_flag)
2123 break;
2124
2125 ns = ns->parent;
2126 }
2127 while (ns != NULL);
2128
2129 *result = NULL;
2130 return 0;
2131 }
2132
2133
2134 /* Same, but returns the symbol instead. */
2135
2136 int
2137 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
2138 gfc_symbol ** result)
2139 {
2140 gfc_symtree *st;
2141 int i;
2142
2143 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2144
2145 if (st == NULL)
2146 *result = NULL;
2147 else
2148 *result = st->n.sym;
2149
2150 return i;
2151 }
2152
2153
2154 /* Save symbol with the information necessary to back it out. */
2155
2156 static void
2157 save_symbol_data (gfc_symbol * sym)
2158 {
2159
2160 if (sym->new || sym->old_symbol != NULL)
2161 return;
2162
2163 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
2164 *(sym->old_symbol) = *sym;
2165
2166 sym->tlink = changed_syms;
2167 changed_syms = sym;
2168 }
2169
2170
2171 /* Given a name, find a symbol, or create it if it does not exist yet
2172 in the current namespace. If the symbol is found we make sure that
2173 it's OK.
2174
2175 The integer return code indicates
2176 0 All OK
2177 1 The symbol name was ambiguous
2178 2 The name meant to be established was already host associated.
2179
2180 So if the return value is nonzero, then an error was issued. */
2181
2182 int
2183 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
2184 {
2185 gfc_symtree *st;
2186 gfc_symbol *p;
2187
2188 /* This doesn't usually happen during resolution. */
2189 if (ns == NULL)
2190 ns = gfc_current_ns;
2191
2192 /* Try to find the symbol in ns. */
2193 st = gfc_find_symtree (ns->sym_root, name);
2194
2195 if (st == NULL)
2196 {
2197 /* If not there, create a new symbol. */
2198 p = gfc_new_symbol (name, ns);
2199
2200 /* Add to the list of tentative symbols. */
2201 p->old_symbol = NULL;
2202 p->tlink = changed_syms;
2203 p->mark = 1;
2204 p->new = 1;
2205 changed_syms = p;
2206
2207 st = gfc_new_symtree (&ns->sym_root, name);
2208 st->n.sym = p;
2209 p->refs++;
2210
2211 }
2212 else
2213 {
2214 /* Make sure the existing symbol is OK. */
2215 if (st->ambiguous)
2216 {
2217 ambiguous_symbol (name, st);
2218 return 1;
2219 }
2220
2221 p = st->n.sym;
2222
2223 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2224 {
2225 /* Symbol is from another namespace. */
2226 gfc_error ("Symbol '%s' at %C has already been host associated",
2227 name);
2228 return 2;
2229 }
2230
2231 p->mark = 1;
2232
2233 /* Copy in case this symbol is changed. */
2234 save_symbol_data (p);
2235 }
2236
2237 *result = st;
2238 return 0;
2239 }
2240
2241
2242 int
2243 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
2244 {
2245 gfc_symtree *st;
2246 int i;
2247
2248
2249 i = gfc_get_sym_tree (name, ns, &st);
2250 if (i != 0)
2251 return i;
2252
2253 if (st)
2254 *result = st->n.sym;
2255 else
2256 *result = NULL;
2257 return i;
2258 }
2259
2260
2261 /* Subroutine that searches for a symbol, creating it if it doesn't
2262 exist, but tries to host-associate the symbol if possible. */
2263
2264 int
2265 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
2266 {
2267 gfc_symtree *st;
2268 int i;
2269
2270 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2271 if (st != NULL)
2272 {
2273 save_symbol_data (st->n.sym);
2274
2275 *result = st;
2276 return i;
2277 }
2278
2279 if (gfc_current_ns->parent != NULL)
2280 {
2281 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2282 if (i)
2283 return i;
2284
2285 if (st != NULL)
2286 {
2287 *result = st;
2288 return 0;
2289 }
2290 }
2291
2292 return gfc_get_sym_tree (name, gfc_current_ns, result);
2293 }
2294
2295
2296 int
2297 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
2298 {
2299 int i;
2300 gfc_symtree *st;
2301
2302 i = gfc_get_ha_sym_tree (name, &st);
2303
2304 if (st)
2305 *result = st->n.sym;
2306 else
2307 *result = NULL;
2308
2309 return i;
2310 }
2311
2312 /* Return true if both symbols could refer to the same data object. Does
2313 not take account of aliasing due to equivalence statements. */
2314
2315 int
2316 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
2317 {
2318 /* Aliasing isn't possible if the symbols have different base types. */
2319 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2320 return 0;
2321
2322 /* Pointers can point to other pointers, target objects and allocatable
2323 objects. Two allocatable objects cannot share the same storage. */
2324 if (lsym->attr.pointer
2325 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2326 return 1;
2327 if (lsym->attr.target && rsym->attr.pointer)
2328 return 1;
2329 if (lsym->attr.allocatable && rsym->attr.pointer)
2330 return 1;
2331
2332 return 0;
2333 }
2334
2335
2336 /* Undoes all the changes made to symbols in the current statement.
2337 This subroutine is made simpler due to the fact that attributes are
2338 never removed once added. */
2339
2340 void
2341 gfc_undo_symbols (void)
2342 {
2343 gfc_symbol *p, *q, *old;
2344
2345 for (p = changed_syms; p; p = q)
2346 {
2347 q = p->tlink;
2348
2349 if (p->new)
2350 {
2351 /* Symbol was new. */
2352 delete_symtree (&p->ns->sym_root, p->name);
2353
2354 p->refs--;
2355 if (p->refs < 0)
2356 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2357 if (p->refs == 0)
2358 gfc_free_symbol (p);
2359 continue;
2360 }
2361
2362 /* Restore previous state of symbol. Just copy simple stuff. */
2363 p->mark = 0;
2364 old = p->old_symbol;
2365
2366 p->ts.type = old->ts.type;
2367 p->ts.kind = old->ts.kind;
2368
2369 p->attr = old->attr;
2370
2371 if (p->value != old->value)
2372 {
2373 gfc_free_expr (old->value);
2374 p->value = NULL;
2375 }
2376
2377 if (p->as != old->as)
2378 {
2379 if (p->as)
2380 gfc_free_array_spec (p->as);
2381 p->as = old->as;
2382 }
2383
2384 p->generic = old->generic;
2385 p->component_access = old->component_access;
2386
2387 if (p->namelist != NULL && old->namelist == NULL)
2388 {
2389 gfc_free_namelist (p->namelist);
2390 p->namelist = NULL;
2391 }
2392 else
2393 {
2394
2395 if (p->namelist_tail != old->namelist_tail)
2396 {
2397 gfc_free_namelist (old->namelist_tail);
2398 old->namelist_tail->next = NULL;
2399 }
2400 }
2401
2402 p->namelist_tail = old->namelist_tail;
2403
2404 if (p->formal != old->formal)
2405 {
2406 gfc_free_formal_arglist (p->formal);
2407 p->formal = old->formal;
2408 }
2409
2410 gfc_free (p->old_symbol);
2411 p->old_symbol = NULL;
2412 p->tlink = NULL;
2413 }
2414
2415 changed_syms = NULL;
2416 }
2417
2418
2419 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2420 components of old_symbol that might need deallocation are the "allocatables"
2421 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2422 namelist_tail. In case these differ between old_symbol and sym, it's just
2423 because sym->namelist has gotten a few more items. */
2424
2425 static void
2426 free_old_symbol (gfc_symbol * sym)
2427 {
2428 if (sym->old_symbol == NULL)
2429 return;
2430
2431 if (sym->old_symbol->as != sym->as)
2432 gfc_free_array_spec (sym->old_symbol->as);
2433
2434 if (sym->old_symbol->value != sym->value)
2435 gfc_free_expr (sym->old_symbol->value);
2436
2437 if (sym->old_symbol->formal != sym->formal)
2438 gfc_free_formal_arglist (sym->old_symbol->formal);
2439
2440 gfc_free (sym->old_symbol);
2441 sym->old_symbol = NULL;
2442 }
2443
2444
2445 /* Makes the changes made in the current statement permanent-- gets
2446 rid of undo information. */
2447
2448 void
2449 gfc_commit_symbols (void)
2450 {
2451 gfc_symbol *p, *q;
2452
2453 for (p = changed_syms; p; p = q)
2454 {
2455 q = p->tlink;
2456 p->tlink = NULL;
2457 p->mark = 0;
2458 p->new = 0;
2459
2460 free_old_symbol (p);
2461 }
2462 changed_syms = NULL;
2463 }
2464
2465
2466 /* Makes the changes made in one symbol permanent -- gets rid of undo
2467 information. */
2468
2469 void
2470 gfc_commit_symbol (gfc_symbol * sym)
2471 {
2472 gfc_symbol *p;
2473
2474 if (changed_syms == sym)
2475 changed_syms = sym->tlink;
2476 else
2477 {
2478 for (p = changed_syms; p; p = p->tlink)
2479 if (p->tlink == sym)
2480 {
2481 p->tlink = sym->tlink;
2482 break;
2483 }
2484 }
2485
2486 sym->tlink = NULL;
2487 sym->mark = 0;
2488 sym->new = 0;
2489
2490 free_old_symbol (sym);
2491 }
2492
2493
2494 /* Recursive function that deletes an entire tree and all the common
2495 head structures it points to. */
2496
2497 static void
2498 free_common_tree (gfc_symtree * common_tree)
2499 {
2500 if (common_tree == NULL)
2501 return;
2502
2503 free_common_tree (common_tree->left);
2504 free_common_tree (common_tree->right);
2505
2506 gfc_free (common_tree);
2507 }
2508
2509
2510 /* Recursive function that deletes an entire tree and all the user
2511 operator nodes that it contains. */
2512
2513 static void
2514 free_uop_tree (gfc_symtree * uop_tree)
2515 {
2516
2517 if (uop_tree == NULL)
2518 return;
2519
2520 free_uop_tree (uop_tree->left);
2521 free_uop_tree (uop_tree->right);
2522
2523 gfc_free_interface (uop_tree->n.uop->operator);
2524
2525 gfc_free (uop_tree->n.uop);
2526 gfc_free (uop_tree);
2527 }
2528
2529
2530 /* Recursive function that deletes an entire tree and all the symbols
2531 that it contains. */
2532
2533 static void
2534 free_sym_tree (gfc_symtree * sym_tree)
2535 {
2536 gfc_namespace *ns;
2537 gfc_symbol *sym;
2538
2539 if (sym_tree == NULL)
2540 return;
2541
2542 free_sym_tree (sym_tree->left);
2543 free_sym_tree (sym_tree->right);
2544
2545 sym = sym_tree->n.sym;
2546
2547 sym->refs--;
2548 if (sym->refs < 0)
2549 gfc_internal_error ("free_sym_tree(): Negative refs");
2550
2551 if (sym->formal_ns != NULL && sym->refs == 1)
2552 {
2553 /* As formal_ns contains a reference to sym, delete formal_ns just
2554 before the deletion of sym. */
2555 ns = sym->formal_ns;
2556 sym->formal_ns = NULL;
2557 gfc_free_namespace (ns);
2558 }
2559 else if (sym->refs == 0)
2560 {
2561 /* Go ahead and delete the symbol. */
2562 gfc_free_symbol (sym);
2563 }
2564
2565 gfc_free (sym_tree);
2566 }
2567
2568
2569 /* Free the gfc_equiv_info's. */
2570
2571 static void
2572 gfc_free_equiv_infos (gfc_equiv_info * s)
2573 {
2574 if (s == NULL)
2575 return;
2576 gfc_free_equiv_infos (s->next);
2577 gfc_free (s);
2578 }
2579
2580
2581 /* Free the gfc_equiv_lists. */
2582
2583 static void
2584 gfc_free_equiv_lists (gfc_equiv_list * l)
2585 {
2586 if (l == NULL)
2587 return;
2588 gfc_free_equiv_lists (l->next);
2589 gfc_free_equiv_infos (l->equiv);
2590 gfc_free (l);
2591 }
2592
2593
2594 /* Free a namespace structure and everything below it. Interface
2595 lists associated with intrinsic operators are not freed. These are
2596 taken care of when a specific name is freed. */
2597
2598 void
2599 gfc_free_namespace (gfc_namespace * ns)
2600 {
2601 gfc_charlen *cl, *cl2;
2602 gfc_namespace *p, *q;
2603 gfc_intrinsic_op i;
2604
2605 if (ns == NULL)
2606 return;
2607
2608 ns->refs--;
2609 if (ns->refs > 0)
2610 return;
2611 gcc_assert (ns->refs == 0);
2612
2613 gfc_free_statements (ns->code);
2614
2615 free_sym_tree (ns->sym_root);
2616 free_uop_tree (ns->uop_root);
2617 free_common_tree (ns->common_root);
2618
2619 for (cl = ns->cl_list; cl; cl = cl2)
2620 {
2621 cl2 = cl->next;
2622 gfc_free_expr (cl->length);
2623 gfc_free (cl);
2624 }
2625
2626 free_st_labels (ns->st_labels);
2627
2628 gfc_free_equiv (ns->equiv);
2629 gfc_free_equiv_lists (ns->equiv_lists);
2630
2631 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2632 gfc_free_interface (ns->operator[i]);
2633
2634 gfc_free_data (ns->data);
2635 p = ns->contained;
2636 gfc_free (ns);
2637
2638 /* Recursively free any contained namespaces. */
2639 while (p != NULL)
2640 {
2641 q = p;
2642 p = p->sibling;
2643
2644 gfc_free_namespace (q);
2645 }
2646 }
2647
2648
2649 void
2650 gfc_symbol_init_2 (void)
2651 {
2652
2653 gfc_current_ns = gfc_get_namespace (NULL, 0);
2654 }
2655
2656
2657 void
2658 gfc_symbol_done_2 (void)
2659 {
2660
2661 gfc_free_namespace (gfc_current_ns);
2662 gfc_current_ns = NULL;
2663 }
2664
2665
2666 /* Clear mark bits from symbol nodes associated with a symtree node. */
2667
2668 static void
2669 clear_sym_mark (gfc_symtree * st)
2670 {
2671
2672 st->n.sym->mark = 0;
2673 }
2674
2675
2676 /* Recursively traverse the symtree nodes. */
2677
2678 void
2679 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2680 {
2681 if (st != NULL)
2682 {
2683 (*func) (st);
2684
2685 gfc_traverse_symtree (st->left, func);
2686 gfc_traverse_symtree (st->right, func);
2687 }
2688 }
2689
2690
2691 /* Recursive namespace traversal function. */
2692
2693 static void
2694 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2695 {
2696
2697 if (st == NULL)
2698 return;
2699
2700 if (st->n.sym->mark == 0)
2701 (*func) (st->n.sym);
2702 st->n.sym->mark = 1;
2703
2704 traverse_ns (st->left, func);
2705 traverse_ns (st->right, func);
2706 }
2707
2708
2709 /* Call a given function for all symbols in the namespace. We take
2710 care that each gfc_symbol node is called exactly once. */
2711
2712 void
2713 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2714 {
2715
2716 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2717
2718 traverse_ns (ns->sym_root, func);
2719 }
2720
2721
2722 /* Return TRUE if the symbol is an automatic variable. */
2723 static bool
2724 gfc_is_var_automatic (gfc_symbol * sym)
2725 {
2726 /* Pointer and allocatable variables are never automatic. */
2727 if (sym->attr.pointer || sym->attr.allocatable)
2728 return false;
2729 /* Check for arrays with non-constant size. */
2730 if (sym->attr.dimension && sym->as
2731 && !gfc_is_compile_time_shape (sym->as))
2732 return true;
2733 /* Check for non-constant length character variables. */
2734 if (sym->ts.type == BT_CHARACTER
2735 && sym->ts.cl
2736 && !gfc_is_constant_expr (sym->ts.cl->length))
2737 return true;
2738 return false;
2739 }
2740
2741 /* Given a symbol, mark it as SAVEd if it is allowed. */
2742
2743 static void
2744 save_symbol (gfc_symbol * sym)
2745 {
2746
2747 if (sym->attr.use_assoc)
2748 return;
2749
2750 if (sym->attr.in_common
2751 || sym->attr.dummy
2752 || sym->attr.flavor != FL_VARIABLE)
2753 return;
2754 /* Automatic objects are not saved. */
2755 if (gfc_is_var_automatic (sym))
2756 return;
2757 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2758 }
2759
2760
2761 /* Mark those symbols which can be SAVEd as such. */
2762
2763 void
2764 gfc_save_all (gfc_namespace * ns)
2765 {
2766
2767 gfc_traverse_ns (ns, save_symbol);
2768 }
2769
2770
2771 #ifdef GFC_DEBUG
2772 /* Make sure that no changes to symbols are pending. */
2773
2774 void
2775 gfc_symbol_state(void) {
2776
2777 if (changed_syms != NULL)
2778 gfc_internal_error("Symbol changes still pending!");
2779 }
2780 #endif
2781
2782
2783 /************** Global symbol handling ************/
2784
2785
2786 /* Search a tree for the global symbol. */
2787
2788 gfc_gsymbol *
2789 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2790 {
2791 gfc_gsymbol *s;
2792
2793 if (symbol == NULL)
2794 return NULL;
2795 if (strcmp (symbol->name, name) == 0)
2796 return symbol;
2797
2798 s = gfc_find_gsymbol (symbol->left, name);
2799 if (s != NULL)
2800 return s;
2801
2802 s = gfc_find_gsymbol (symbol->right, name);
2803 if (s != NULL)
2804 return s;
2805
2806 return NULL;
2807 }
2808
2809
2810 /* Compare two global symbols. Used for managing the BB tree. */
2811
2812 static int
2813 gsym_compare (void * _s1, void * _s2)
2814 {
2815 gfc_gsymbol *s1, *s2;
2816
2817 s1 = (gfc_gsymbol *)_s1;
2818 s2 = (gfc_gsymbol *)_s2;
2819 return strcmp(s1->name, s2->name);
2820 }
2821
2822
2823 /* Get a global symbol, creating it if it doesn't exist. */
2824
2825 gfc_gsymbol *
2826 gfc_get_gsymbol (const char *name)
2827 {
2828 gfc_gsymbol *s;
2829
2830 s = gfc_find_gsymbol (gfc_gsym_root, name);
2831 if (s != NULL)
2832 return s;
2833
2834 s = gfc_getmem (sizeof (gfc_gsymbol));
2835 s->type = GSYM_UNKNOWN;
2836 s->name = gfc_get_string (name);
2837
2838 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2839
2840 return s;
2841 }