OSDN Git Service

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