OSDN Git Service

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