OSDN Git Service

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