OSDN Git Service

2004-08-20 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   memset (attr, 0, sizeof(symbol_attribute));
1014 }
1015
1016
1017 /* Check for missing attributes in the new symbol.  Currently does
1018    nothing, but it's not clear that it is unnecessary yet.  */
1019
1020 try
1021 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1022                   locus * where ATTRIBUTE_UNUSED)
1023 {
1024
1025   return SUCCESS;
1026 }
1027
1028
1029 /* Copy an attribute to a symbol attribute, bit by bit.  Some
1030    attributes have a lot of side-effects but cannot be present given
1031    where we are called from, so we ignore some bits.  */
1032
1033 try
1034 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1035 {
1036
1037   if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1038     goto fail;
1039
1040   if (src->dimension && gfc_add_dimension (dest, where) == FAILURE)
1041     goto fail;
1042   if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1043     goto fail;
1044   if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1045     goto fail;
1046   if (src->save && gfc_add_save (dest, where) == FAILURE)
1047     goto fail;
1048   if (src->target && gfc_add_target (dest, where) == FAILURE)
1049     goto fail;
1050   if (src->dummy && gfc_add_dummy (dest, where) == FAILURE)
1051     goto fail;
1052   if (src->result && gfc_add_result (dest, where) == FAILURE)
1053     goto fail;
1054   if (src->entry)
1055     dest->entry = 1;
1056
1057   if (src->in_namelist && gfc_add_in_namelist (dest, where) == FAILURE)
1058     goto fail;
1059
1060   if (src->in_common && gfc_add_in_common (dest, where) == FAILURE)
1061     goto fail;
1062
1063   if (src->generic && gfc_add_generic (dest, where) == FAILURE)
1064     goto fail;
1065   if (src->function && gfc_add_function (dest, where) == FAILURE)
1066     goto fail;
1067   if (src->subroutine && gfc_add_subroutine (dest, where) == FAILURE)
1068     goto fail;
1069
1070   if (src->sequence && gfc_add_sequence (dest, where) == FAILURE)
1071     goto fail;
1072   if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1073     goto fail;
1074   if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1075     goto fail;
1076   if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1077     goto fail;
1078
1079   if (src->flavor != FL_UNKNOWN
1080       && gfc_add_flavor (dest, src->flavor, where) == FAILURE)
1081     goto fail;
1082
1083   if (src->intent != INTENT_UNKNOWN
1084       && gfc_add_intent (dest, src->intent, where) == FAILURE)
1085     goto fail;
1086
1087   if (src->access != ACCESS_UNKNOWN
1088       && gfc_add_access (dest, src->access, where) == FAILURE)
1089     goto fail;
1090
1091   if (gfc_missing_attr (dest, where) == FAILURE)
1092     goto fail;
1093
1094   /* The subroutines that set these bits also cause flavors to be set,
1095      and that has already happened in the original, so don't let to
1096      happen again.  */
1097   if (src->external)
1098     dest->external = 1;
1099   if (src->intrinsic)
1100     dest->intrinsic = 1;
1101
1102   return SUCCESS;
1103
1104 fail:
1105   return FAILURE;
1106 }
1107
1108
1109 /************** Component name management ************/
1110
1111 /* Component names of a derived type form their own little namespaces
1112    that are separate from all other spaces.  The space is composed of
1113    a singly linked list of gfc_component structures whose head is
1114    located in the parent symbol.  */
1115
1116
1117 /* Add a component name to a symbol.  The call fails if the name is
1118    already present.  On success, the component pointer is modified to
1119    point to the additional component structure.  */
1120
1121 try
1122 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1123 {
1124   gfc_component *p, *tail;
1125
1126   tail = NULL;
1127
1128   for (p = sym->components; p; p = p->next)
1129     {
1130       if (strcmp (p->name, name) == 0)
1131         {
1132           gfc_error ("Component '%s' at %C already declared at %L",
1133                      name, &p->loc);
1134           return FAILURE;
1135         }
1136
1137       tail = p;
1138     }
1139
1140   /* Allocate new component */
1141   p = gfc_get_component ();
1142
1143   if (tail == NULL)
1144     sym->components = p;
1145   else
1146     tail->next = p;
1147
1148   strcpy (p->name, name);
1149   p->loc = gfc_current_locus;
1150
1151   *component = p;
1152   return SUCCESS;
1153 }
1154
1155
1156 /* Recursive function to switch derived types of all symbol in a
1157    namespace.  */
1158
1159 static void
1160 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1161 {
1162   gfc_symbol *sym;
1163
1164   if (st == NULL)
1165     return;
1166
1167   sym = st->n.sym;
1168   if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1169     sym->ts.derived = to;
1170
1171   switch_types (st->left, from, to);
1172   switch_types (st->right, from, to);
1173 }
1174
1175
1176 /* This subroutine is called when a derived type is used in order to
1177    make the final determination about which version to use.  The
1178    standard requires that a type be defined before it is 'used', but
1179    such types can appear in IMPLICIT statements before the actual
1180    definition.  'Using' in this context means declaring a variable to
1181    be that type or using the type constructor.
1182
1183    If a type is used and the components haven't been defined, then we
1184    have to have a derived type in a parent unit.  We find the node in
1185    the other namespace and point the symtree node in this namespace to
1186    that node.  Further reference to this name point to the correct
1187    node.  If we can't find the node in a parent namespace, then have
1188    an error.
1189
1190    This subroutine takes a pointer to a symbol node and returns a
1191    pointer to the translated node or NULL for an error.  Usually there
1192    is no translation and we return the node we were passed.  */
1193
1194 static gfc_symtree *
1195 gfc_use_ha_derived (gfc_symbol * sym)
1196 {
1197   gfc_symbol *s, *p;
1198   gfc_typespec *t;
1199   gfc_symtree *st;
1200   int i;
1201
1202   if (sym->ns->parent == NULL)
1203     goto bad;
1204
1205   if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1206     {
1207       gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1208       return NULL;
1209     }
1210
1211   if (s == NULL || s->attr.flavor != FL_DERIVED)
1212     goto bad;
1213
1214   /* Get rid of symbol sym, translating all references to s.  */
1215   for (i = 0; i < GFC_LETTERS; i++)
1216     {
1217       t = &sym->ns->default_type[i];
1218       if (t->derived == sym)
1219         t->derived = s;
1220     }
1221
1222   st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1223   st->n.sym = s;
1224
1225   s->refs++;
1226
1227   /* Unlink from list of modified symbols.  */
1228   if (changed_syms == sym)
1229     changed_syms = sym->tlink;
1230   else
1231     for (p = changed_syms; p; p = p->tlink)
1232       if (p->tlink == sym)
1233         {
1234           p->tlink = sym->tlink;
1235           break;
1236         }
1237
1238   switch_types (sym->ns->sym_root, sym, s);
1239
1240   /* TODO: Also have to replace sym -> s in other lists like
1241      namelists, common lists and interface lists.  */
1242   gfc_free_symbol (sym);
1243
1244   return st;
1245
1246 bad:
1247   gfc_error ("Derived type '%s' at %C is being used before it is defined",
1248              sym->name);
1249   return NULL;
1250 }
1251
1252
1253 gfc_symbol *
1254 gfc_use_derived (gfc_symbol * sym)
1255 {
1256   gfc_symtree *st;
1257
1258   if (sym->components != NULL)
1259     return sym;                 /* Already defined */
1260
1261   st = gfc_use_ha_derived (sym);
1262   if (st)
1263     return st->n.sym;
1264   else
1265     return NULL;
1266 }
1267
1268
1269 /* Given a derived type node and a component name, try to locate the
1270    component structure.  Returns the NULL pointer if the component is
1271    not found or the components are private.  */
1272
1273 gfc_component *
1274 gfc_find_component (gfc_symbol * sym, const char *name)
1275 {
1276   gfc_component *p;
1277
1278   if (name == NULL)
1279     return NULL;
1280
1281   sym = gfc_use_derived (sym);
1282
1283   if (sym == NULL)
1284     return NULL;
1285
1286   for (p = sym->components; p; p = p->next)
1287     if (strcmp (p->name, name) == 0)
1288       break;
1289
1290   if (p == NULL)
1291     gfc_error ("'%s' at %C is not a member of the '%s' structure",
1292                name, sym->name);
1293   else
1294     {
1295       if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1296         {
1297           gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1298                      name, sym->name);
1299           p = NULL;
1300         }
1301     }
1302
1303   return p;
1304 }
1305
1306
1307 /* Given a symbol, free all of the component structures and everything
1308    they point to.  */
1309
1310 static void
1311 free_components (gfc_component * p)
1312 {
1313   gfc_component *q;
1314
1315   for (; p; p = q)
1316     {
1317       q = p->next;
1318
1319       gfc_free_array_spec (p->as);
1320       gfc_free_expr (p->initializer);
1321
1322       gfc_free (p);
1323     }
1324 }
1325
1326
1327 /* Set component attributes from a standard symbol attribute
1328    structure.  */
1329
1330 void
1331 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1332 {
1333
1334   c->dimension = attr->dimension;
1335   c->pointer = attr->pointer;
1336 }
1337
1338
1339 /* Get a standard symbol attribute structure given the component
1340    structure.  */
1341
1342 void
1343 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1344 {
1345
1346   gfc_clear_attr (attr);
1347   attr->dimension = c->dimension;
1348   attr->pointer = c->pointer;
1349 }
1350
1351
1352 /******************** Statement label management ********************/
1353
1354 /* Free a single gfc_st_label structure, making sure the list is not
1355    messed up.  This function is called only when some parse error
1356    occurs.  */
1357
1358 void
1359 gfc_free_st_label (gfc_st_label * l)
1360 {
1361
1362   if (l == NULL)
1363     return;
1364
1365   if (l->prev)
1366     (l->prev->next = l->next);
1367
1368   if (l->next)
1369     (l->next->prev = l->prev);
1370
1371   if (l->format != NULL)
1372     gfc_free_expr (l->format);
1373   gfc_free (l);
1374 }
1375
1376 /* Free a whole list of gfc_st_label structures.  */
1377
1378 static void
1379 free_st_labels (gfc_st_label * l1)
1380 {
1381   gfc_st_label *l2;
1382
1383   for (; l1; l1 = l2)
1384     {
1385       l2 = l1->next;
1386       if (l1->format != NULL)
1387         gfc_free_expr (l1->format);
1388       gfc_free (l1);
1389     }
1390 }
1391
1392
1393 /* Given a label number, search for and return a pointer to the label
1394    structure, creating it if it does not exist.  */
1395
1396 gfc_st_label *
1397 gfc_get_st_label (int labelno)
1398 {
1399   gfc_st_label *lp;
1400
1401   /* First see if the label is already in this namespace.  */
1402   for (lp = gfc_current_ns->st_labels; lp; lp = lp->next)
1403     if (lp->value == labelno)
1404       break;
1405   if (lp != NULL)
1406     return lp;
1407
1408   lp = gfc_getmem (sizeof (gfc_st_label));
1409
1410   lp->value = labelno;
1411   lp->defined = ST_LABEL_UNKNOWN;
1412   lp->referenced = ST_LABEL_UNKNOWN;
1413
1414   lp->prev = NULL;
1415   lp->next = gfc_current_ns->st_labels;
1416   if (gfc_current_ns->st_labels)
1417     gfc_current_ns->st_labels->prev = lp;
1418   gfc_current_ns->st_labels = lp;
1419
1420   return lp;
1421 }
1422
1423
1424 /* Called when a statement with a statement label is about to be
1425    accepted.  We add the label to the list of the current namespace,
1426    making sure it hasn't been defined previously and referenced
1427    correctly.  */
1428
1429 void
1430 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1431 {
1432   int labelno;
1433
1434   labelno = lp->value;
1435
1436   if (lp->defined != ST_LABEL_UNKNOWN)
1437     gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1438                &lp->where, label_locus);
1439   else
1440     {
1441       lp->where = *label_locus;
1442
1443       switch (type)
1444         {
1445         case ST_LABEL_FORMAT:
1446           if (lp->referenced == ST_LABEL_TARGET)
1447             gfc_error ("Label %d at %C already referenced as branch target",
1448                        labelno);
1449           else
1450             lp->defined = ST_LABEL_FORMAT;
1451
1452           break;
1453
1454         case ST_LABEL_TARGET:
1455           if (lp->referenced == ST_LABEL_FORMAT)
1456             gfc_error ("Label %d at %C already referenced as a format label",
1457                        labelno);
1458           else
1459             lp->defined = ST_LABEL_TARGET;
1460
1461           break;
1462
1463         default:
1464           lp->defined = ST_LABEL_BAD_TARGET;
1465           lp->referenced = ST_LABEL_BAD_TARGET;
1466         }
1467     }
1468 }
1469
1470
1471 /* Reference a label.  Given a label and its type, see if that
1472    reference is consistent with what is known about that label,
1473    updating the unknown state.  Returns FAILURE if something goes
1474    wrong.  */
1475
1476 try
1477 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1478 {
1479   gfc_sl_type label_type;
1480   int labelno;
1481   try rc;
1482
1483   if (lp == NULL)
1484     return SUCCESS;
1485
1486   labelno = lp->value;
1487
1488   if (lp->defined != ST_LABEL_UNKNOWN)
1489     label_type = lp->defined;
1490   else
1491     {
1492       label_type = lp->referenced;
1493       lp->where = gfc_current_locus;
1494     }
1495
1496   if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1497     {
1498       gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1499       rc = FAILURE;
1500       goto done;
1501     }
1502
1503   if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1504       && type == ST_LABEL_FORMAT)
1505     {
1506       gfc_error ("Label %d at %C previously used as branch target", labelno);
1507       rc = FAILURE;
1508       goto done;
1509     }
1510
1511   lp->referenced = type;
1512   rc = SUCCESS;
1513
1514 done:
1515   return rc;
1516 }
1517
1518
1519 /************** Symbol table management subroutines ****************/
1520
1521 /* Basic details: Fortran 95 requires a potentially unlimited number
1522    of distinct namespaces when compiling a program unit.  This case
1523    occurs during a compilation of internal subprograms because all of
1524    the internal subprograms must be read before we can start
1525    generating code for the host.
1526
1527    Given the tricky nature of the fortran grammar, we must be able to
1528    undo changes made to a symbol table if the current interpretation
1529    of a statement is found to be incorrect.  Whenever a symbol is
1530    looked up, we make a copy of it and link to it.  All of these
1531    symbols are kept in a singly linked list so that we can commit or
1532    undo the changes at a later time.
1533
1534    A symtree may point to a symbol node outside of its namespace.  In
1535    this case, that symbol has been used as a host associated variable
1536    at some previous time.  */
1537
1538 /* Allocate a new namespace structure.  */
1539
1540 gfc_namespace *
1541 gfc_get_namespace (gfc_namespace * parent)
1542 {
1543   gfc_namespace *ns;
1544   gfc_typespec *ts;
1545   gfc_intrinsic_op in;
1546   int i;
1547
1548   ns = gfc_getmem (sizeof (gfc_namespace));
1549   ns->sym_root = NULL;
1550   ns->uop_root = NULL;
1551   ns->default_access = ACCESS_UNKNOWN;
1552   ns->parent = parent;
1553
1554   for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1555     ns->operator_access[in] = ACCESS_UNKNOWN;
1556
1557   /* Initialize default implicit types.  */
1558   for (i = 'a'; i <= 'z'; i++)
1559     {
1560       ns->set_flag[i - 'a'] = 0;
1561       ts = &ns->default_type[i - 'a'];
1562
1563       if (ns->parent != NULL)
1564         {
1565           /* Copy parent settings */
1566           *ts = ns->parent->default_type[i - 'a'];
1567           continue;
1568         }
1569
1570       if (gfc_option.flag_implicit_none != 0)
1571         {
1572           gfc_clear_ts (ts);
1573           continue;
1574         }
1575
1576       if ('i' <= i && i <= 'n')
1577         {
1578           ts->type = BT_INTEGER;
1579           ts->kind = gfc_default_integer_kind ();
1580         }
1581       else
1582         {
1583           ts->type = BT_REAL;
1584           ts->kind = gfc_default_real_kind ();
1585         }
1586     }
1587
1588   ns->refs = 1;
1589
1590   return ns;
1591 }
1592
1593
1594 /* Comparison function for symtree nodes.  */
1595
1596 static int
1597 compare_symtree (void * _st1, void * _st2)
1598 {
1599   gfc_symtree *st1, *st2;
1600
1601   st1 = (gfc_symtree *) _st1;
1602   st2 = (gfc_symtree *) _st2;
1603
1604   return strcmp (st1->name, st2->name);
1605 }
1606
1607
1608 /* Allocate a new symtree node and associate it with the new symbol.  */
1609
1610 gfc_symtree *
1611 gfc_new_symtree (gfc_symtree ** root, const char *name)
1612 {
1613   gfc_symtree *st;
1614
1615   st = gfc_getmem (sizeof (gfc_symtree));
1616   strcpy (st->name, name);
1617
1618   gfc_insert_bbt (root, st, compare_symtree);
1619   return st;
1620 }
1621
1622
1623 /* Delete a symbol from the tree.  Does not free the symbol itself!  */
1624
1625 static void
1626 delete_symtree (gfc_symtree ** root, const char *name)
1627 {
1628   gfc_symtree st, *st0;
1629
1630   st0 = gfc_find_symtree (*root, name);
1631
1632   strcpy (st.name, name);
1633   gfc_delete_bbt (root, &st, compare_symtree);
1634
1635   gfc_free (st0);
1636 }
1637
1638
1639 /* Given a root symtree node and a name, try to find the symbol within
1640    the namespace.  Returns NULL if the symbol is not found.  */
1641
1642 gfc_symtree *
1643 gfc_find_symtree (gfc_symtree * st, const char *name)
1644 {
1645   int c;
1646
1647   while (st != NULL)
1648     {
1649       c = strcmp (name, st->name);
1650       if (c == 0)
1651         return st;
1652
1653       st = (c < 0) ? st->left : st->right;
1654     }
1655
1656   return NULL;
1657 }
1658
1659
1660 /* Given a name find a user operator node, creating it if it doesn't
1661    exist.  These are much simpler than symbols because they can't be
1662    ambiguous with one another.  */
1663
1664 gfc_user_op *
1665 gfc_get_uop (const char *name)
1666 {
1667   gfc_user_op *uop;
1668   gfc_symtree *st;
1669
1670   st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1671   if (st != NULL)
1672     return st->n.uop;
1673
1674   st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1675
1676   uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1677   strcpy (uop->name, name);
1678   uop->access = ACCESS_UNKNOWN;
1679   uop->ns = gfc_current_ns;
1680
1681   return uop;
1682 }
1683
1684
1685 /* Given a name find the user operator node.  Returns NULL if it does
1686    not exist.  */
1687
1688 gfc_user_op *
1689 gfc_find_uop (const char *name, gfc_namespace * ns)
1690 {
1691   gfc_symtree *st;
1692
1693   if (ns == NULL)
1694     ns = gfc_current_ns;
1695
1696   st = gfc_find_symtree (ns->uop_root, name);
1697   return (st == NULL) ? NULL : st->n.uop;
1698 }
1699
1700
1701 /* Remove a gfc_symbol structure and everything it points to.  */
1702
1703 void
1704 gfc_free_symbol (gfc_symbol * sym)
1705 {
1706
1707   if (sym == NULL)
1708     return;
1709
1710   gfc_free_array_spec (sym->as);
1711
1712   free_components (sym->components);
1713
1714   gfc_free_expr (sym->value);
1715
1716   gfc_free_namelist (sym->namelist);
1717
1718   gfc_free_namespace (sym->formal_ns);
1719
1720   gfc_free_interface (sym->generic);
1721
1722   gfc_free_formal_arglist (sym->formal);
1723
1724   gfc_free (sym);
1725 }
1726
1727
1728 /* Allocate and initialize a new symbol node.  */
1729
1730 gfc_symbol *
1731 gfc_new_symbol (const char *name, gfc_namespace * ns)
1732 {
1733   gfc_symbol *p;
1734
1735   p = gfc_getmem (sizeof (gfc_symbol));
1736
1737   gfc_clear_ts (&p->ts);
1738   gfc_clear_attr (&p->attr);
1739   p->ns = ns;
1740
1741   p->declared_at = gfc_current_locus;
1742
1743   if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1744     gfc_internal_error ("new_symbol(): Symbol name too long");
1745
1746   strcpy (p->name, name);
1747   return p;
1748 }
1749
1750
1751 /* Generate an error if a symbol is ambiguous.  */
1752
1753 static void
1754 ambiguous_symbol (const char *name, gfc_symtree * st)
1755 {
1756
1757   if (st->n.sym->module[0])
1758     gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1759                "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1760   else
1761     gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1762                "from current program unit", name, st->n.sym->name);
1763 }
1764
1765
1766 /* Search for a symbol starting in the current namespace, resorting to
1767    any parent namespaces if requested by a nonzero parent_flag.
1768    Returns nonzero if the symbol is ambiguous.  */
1769
1770 int
1771 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1772                  gfc_symtree ** result)
1773 {
1774   gfc_symtree *st;
1775
1776   if (ns == NULL)
1777     ns = gfc_current_ns;
1778
1779   do
1780     {
1781       st = gfc_find_symtree (ns->sym_root, name);
1782       if (st != NULL)
1783         {
1784           *result = st;
1785           if (st->ambiguous)
1786             {
1787               ambiguous_symbol (name, st);
1788               return 1;
1789             }
1790
1791           return 0;
1792         }
1793
1794       if (!parent_flag)
1795         break;
1796
1797       ns = ns->parent;
1798     }
1799   while (ns != NULL);
1800
1801   *result = NULL;
1802   return 0;
1803 }
1804
1805
1806 int
1807 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
1808                  gfc_symbol ** result)
1809 {
1810   gfc_symtree *st;
1811   int i;
1812
1813   i = gfc_find_sym_tree (name, ns, parent_flag, &st);
1814
1815   if (st == NULL)
1816     *result = NULL;
1817   else
1818     *result = st->n.sym;
1819
1820   return i;
1821 }
1822
1823
1824 /* Save symbol with the information necessary to back it out.  */
1825
1826 static void
1827 save_symbol_data (gfc_symbol * sym)
1828 {
1829
1830   if (sym->new || sym->old_symbol != NULL)
1831     return;
1832
1833   sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
1834   *(sym->old_symbol) = *sym;
1835
1836   sym->tlink = changed_syms;
1837   changed_syms = sym;
1838 }
1839
1840
1841 /* Given a name, find a symbol, or create it if it does not exist yet
1842    in the current namespace.  If the symbol is found we make sure that
1843    it's OK.
1844
1845    The integer return code indicates
1846      0   All OK
1847      1   The symbol name was ambiguous
1848      2   The name meant to be established was already host associated.
1849
1850    So if the return value is nonzero, then an error was issued.  */
1851
1852 int
1853 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
1854 {
1855   gfc_symtree *st;
1856   gfc_symbol *p;
1857
1858   /* This doesn't usually happen during resolution.  */
1859   if (ns == NULL)
1860     ns = gfc_current_ns;
1861
1862   /* Try to find the symbol in ns.  */
1863   st = gfc_find_symtree (ns->sym_root, name);
1864
1865   if (st == NULL)
1866     {
1867       /* If not there, create a new symbol.  */
1868       p = gfc_new_symbol (name, ns);
1869
1870       /* Add to the list of tentative symbols.  */
1871       p->old_symbol = NULL;
1872       p->tlink = changed_syms;
1873       p->mark = 1;
1874       p->new = 1;
1875       changed_syms = p;
1876
1877       st = gfc_new_symtree (&ns->sym_root, name);
1878       st->n.sym = p;
1879       p->refs++;
1880
1881     }
1882   else
1883     {
1884       /* Make sure the existing symbol is OK.  */
1885       if (st->ambiguous)
1886         {
1887           ambiguous_symbol (name, st);
1888           return 1;
1889         }
1890
1891       p = st->n.sym;
1892
1893       if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
1894         {
1895           /* Symbol is from another namespace.  */
1896           gfc_error ("Symbol '%s' at %C has already been host associated",
1897                      name);
1898           return 2;
1899         }
1900
1901       p->mark = 1;
1902
1903       /* Copy in case this symbol is changed.  */
1904       save_symbol_data (p);
1905     }
1906
1907   *result = st;
1908   return 0;
1909 }
1910
1911
1912 int
1913 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
1914 {
1915   gfc_symtree *st;
1916   int i;
1917
1918
1919   i = gfc_get_sym_tree (name, ns, &st);
1920   if (i != 0)
1921     return i;
1922
1923   if (st)
1924     *result = st->n.sym;
1925   else
1926     *result = NULL;
1927   return i;
1928 }
1929
1930
1931 /* Subroutine that searches for a symbol, creating it if it doesn't
1932    exist, but tries to host-associate the symbol if possible.  */
1933
1934 int
1935 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
1936 {
1937   gfc_symtree *st;
1938   int i;
1939
1940   i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1941   if (st != NULL)
1942     {
1943       save_symbol_data (st->n.sym);
1944
1945       *result = st;
1946       return i;
1947     }
1948
1949   if (gfc_current_ns->parent != NULL)
1950     {
1951       i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
1952       if (i)
1953         return i;
1954
1955       if (st != NULL)
1956         {
1957           *result = st;
1958           return 0;
1959         }
1960     }
1961
1962   return gfc_get_sym_tree (name, gfc_current_ns, result);
1963 }
1964
1965
1966 int
1967 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
1968 {
1969   int i;
1970   gfc_symtree *st;
1971
1972   i = gfc_get_ha_sym_tree (name, &st);
1973
1974   if (st)
1975     *result = st->n.sym;
1976   else
1977     *result = NULL;
1978
1979   return i;
1980 }
1981
1982 /* Return true if both symbols could refer to the same data object.  Does
1983    not take account of aliasing due to equivalence statements.  */
1984
1985 int
1986 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
1987 {
1988   /* Aliasing isn't possible if the symbols have different base types.  */
1989   if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
1990     return 0;
1991
1992   /* Pointers can point to other pointers, target objects and allocatable
1993      objects.  Two allocatable objects cannot share the same storage.  */
1994   if (lsym->attr.pointer
1995       && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
1996     return 1;
1997   if (lsym->attr.target && rsym->attr.pointer)
1998     return 1;
1999   if (lsym->attr.allocatable && rsym->attr.pointer)
2000     return 1;
2001
2002   return 0;
2003 }
2004
2005
2006 /* Undoes all the changes made to symbols in the current statement.
2007    This subroutine is made simpler due to the fact that attributes are
2008    never removed once added.  */
2009
2010 void
2011 gfc_undo_symbols (void)
2012 {
2013   gfc_symbol *p, *q, *old;
2014
2015   for (p = changed_syms; p; p = q)
2016     {
2017       q = p->tlink;
2018
2019       if (p->new)
2020         {
2021           /* Symbol was new.  */
2022           delete_symtree (&p->ns->sym_root, p->name);
2023
2024           p->refs--;
2025           if (p->refs < 0)
2026             gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2027           if (p->refs == 0)
2028             gfc_free_symbol (p);
2029           continue;
2030         }
2031
2032       /* Restore previous state of symbol.  Just copy simple stuff.  */
2033       p->mark = 0;
2034       old = p->old_symbol;
2035
2036       p->ts.type = old->ts.type;
2037       p->ts.kind = old->ts.kind;
2038
2039       p->attr = old->attr;
2040
2041       if (p->value != old->value)
2042         {
2043           gfc_free_expr (old->value);
2044           p->value = NULL;
2045         }
2046
2047       if (p->as != old->as)
2048         {
2049           if (p->as)
2050             gfc_free_array_spec (p->as);
2051           p->as = old->as;
2052         }
2053
2054       p->generic = old->generic;
2055       p->component_access = old->component_access;
2056
2057       if (p->namelist != NULL && old->namelist == NULL)
2058         {
2059           gfc_free_namelist (p->namelist);
2060           p->namelist = NULL;
2061         }
2062       else
2063         {
2064
2065           if (p->namelist_tail != old->namelist_tail)
2066             {
2067               gfc_free_namelist (old->namelist_tail);
2068               old->namelist_tail->next = NULL;
2069             }
2070         }
2071
2072       p->namelist_tail = old->namelist_tail;
2073
2074       if (p->formal != old->formal)
2075         {
2076           gfc_free_formal_arglist (p->formal);
2077           p->formal = old->formal;
2078         }
2079
2080       gfc_free (p->old_symbol);
2081       p->old_symbol = NULL;
2082       p->tlink = NULL;
2083     }
2084
2085   changed_syms = NULL;
2086 }
2087
2088
2089 /* Makes the changes made in the current statement permanent-- gets
2090    rid of undo information.  */
2091
2092 void
2093 gfc_commit_symbols (void)
2094 {
2095   gfc_symbol *p, *q;
2096
2097   for (p = changed_syms; p; p = q)
2098     {
2099       q = p->tlink;
2100       p->tlink = NULL;
2101       p->mark = 0;
2102       p->new = 0;
2103
2104       if (p->old_symbol != NULL)
2105         {
2106           gfc_free (p->old_symbol);
2107           p->old_symbol = NULL;
2108         }
2109     }
2110
2111   changed_syms = NULL;
2112 }
2113
2114
2115 /* Recursive function that deletes an entire tree and all the common
2116    head structures it points to.  */
2117
2118 static void
2119 free_common_tree (gfc_symtree * common_tree)
2120 {
2121   if (common_tree == NULL)
2122     return;
2123
2124   free_common_tree (common_tree->left);
2125   free_common_tree (common_tree->right);
2126
2127   gfc_free (common_tree);
2128 }  
2129
2130
2131 /* Recursive function that deletes an entire tree and all the user
2132    operator nodes that it contains.  */
2133
2134 static void
2135 free_uop_tree (gfc_symtree * uop_tree)
2136 {
2137
2138   if (uop_tree == NULL)
2139     return;
2140
2141   free_uop_tree (uop_tree->left);
2142   free_uop_tree (uop_tree->right);
2143
2144   gfc_free_interface (uop_tree->n.uop->operator);
2145
2146   gfc_free (uop_tree->n.uop);
2147   gfc_free (uop_tree);
2148 }
2149
2150
2151 /* Recursive function that deletes an entire tree and all the symbols
2152    that it contains.  */
2153
2154 static void
2155 free_sym_tree (gfc_symtree * sym_tree)
2156 {
2157   gfc_namespace *ns;
2158   gfc_symbol *sym;
2159
2160   if (sym_tree == NULL)
2161     return;
2162
2163   free_sym_tree (sym_tree->left);
2164   free_sym_tree (sym_tree->right);
2165
2166   sym = sym_tree->n.sym;
2167
2168   sym->refs--;
2169   if (sym->refs < 0)
2170     gfc_internal_error ("free_sym_tree(): Negative refs");
2171
2172   if (sym->formal_ns != NULL && sym->refs == 1)
2173     {
2174       /* As formal_ns contains a reference to sym, delete formal_ns just
2175          before the deletion of sym.  */
2176       ns = sym->formal_ns;
2177       sym->formal_ns = NULL;
2178       gfc_free_namespace (ns);
2179     }
2180   else if (sym->refs == 0)
2181     {
2182       /* Go ahead and delete the symbol.  */
2183       gfc_free_symbol (sym);
2184     }
2185
2186   gfc_free (sym_tree);
2187 }
2188
2189
2190 /* Free a namespace structure and everything below it.  Interface
2191    lists associated with intrinsic operators are not freed.  These are
2192    taken care of when a specific name is freed.  */
2193
2194 void
2195 gfc_free_namespace (gfc_namespace * ns)
2196 {
2197   gfc_charlen *cl, *cl2;
2198   gfc_namespace *p, *q;
2199   gfc_intrinsic_op i;
2200
2201   if (ns == NULL)
2202     return;
2203
2204   ns->refs--;
2205   if (ns->refs > 0)
2206     return;
2207   assert (ns->refs == 0);
2208
2209   gfc_free_statements (ns->code);
2210
2211   free_sym_tree (ns->sym_root);
2212   free_uop_tree (ns->uop_root);
2213   free_common_tree (ns->common_root);
2214
2215   for (cl = ns->cl_list; cl; cl = cl2)
2216     {
2217       cl2 = cl->next;
2218       gfc_free_expr (cl->length);
2219       gfc_free (cl);
2220     }
2221
2222   free_st_labels (ns->st_labels);
2223
2224   gfc_free_equiv (ns->equiv);
2225
2226   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2227     gfc_free_interface (ns->operator[i]);
2228
2229   gfc_free_data (ns->data);
2230   p = ns->contained;
2231   gfc_free (ns);
2232
2233   /* Recursively free any contained namespaces.  */
2234   while (p != NULL)
2235     {
2236       q = p;
2237       p = p->sibling;
2238
2239       gfc_free_namespace (q);
2240     }
2241 }
2242
2243
2244 void
2245 gfc_symbol_init_2 (void)
2246 {
2247
2248   gfc_current_ns = gfc_get_namespace (NULL);
2249 }
2250
2251
2252 void
2253 gfc_symbol_done_2 (void)
2254 {
2255
2256   gfc_free_namespace (gfc_current_ns);
2257   gfc_current_ns = NULL;
2258 }
2259
2260
2261 /* Clear mark bits from symbol nodes associated with a symtree node.  */
2262
2263 static void
2264 clear_sym_mark (gfc_symtree * st)
2265 {
2266
2267   st->n.sym->mark = 0;
2268 }
2269
2270
2271 /* Recursively traverse the symtree nodes.  */
2272
2273 void
2274 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2275 {
2276   if (st != NULL)
2277     {
2278       (*func) (st);
2279
2280       gfc_traverse_symtree (st->left, func);
2281       gfc_traverse_symtree (st->right, func);
2282     }
2283 }
2284
2285
2286 /* Recursive namespace traversal function.  */
2287
2288 static void
2289 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2290 {
2291
2292   if (st == NULL)
2293     return;
2294
2295   if (st->n.sym->mark == 0)
2296     (*func) (st->n.sym);
2297   st->n.sym->mark = 1;
2298
2299   traverse_ns (st->left, func);
2300   traverse_ns (st->right, func);
2301 }
2302
2303
2304 /* Call a given function for all symbols in the namespace.  We take
2305    care that each gfc_symbol node is called exactly once.  */
2306
2307 void
2308 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2309 {
2310
2311   gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2312
2313   traverse_ns (ns->sym_root, func);
2314 }
2315
2316
2317 /* Given a symbol, mark it as SAVEd if it is allowed.  */
2318
2319 static void
2320 save_symbol (gfc_symbol * sym)
2321 {
2322
2323   if (sym->attr.use_assoc)
2324     return;
2325
2326   if (sym->attr.in_common
2327       || sym->attr.dummy
2328       || sym->attr.flavor != FL_VARIABLE)
2329     return;
2330
2331   gfc_add_save (&sym->attr, &sym->declared_at);
2332 }
2333
2334
2335 /* Mark those symbols which can be SAVEd as such.  */
2336
2337 void
2338 gfc_save_all (gfc_namespace * ns)
2339 {
2340
2341   gfc_traverse_ns (ns, save_symbol);
2342 }
2343
2344
2345 #ifdef GFC_DEBUG
2346 /* Make sure that no changes to symbols are pending.  */
2347
2348 void
2349 gfc_symbol_state(void) {
2350
2351   if (changed_syms != NULL)
2352     gfc_internal_error("Symbol changes still pending!");
2353 }
2354 #endif
2355
2356
2357 /************** Global symbol handling ************/
2358
2359
2360 /* Search a tree for the global symbol.  */
2361
2362 gfc_gsymbol *
2363 gfc_find_gsymbol (gfc_gsymbol *symbol, char *name)
2364 {
2365   gfc_gsymbol *s;
2366
2367   if (symbol == NULL)
2368     return NULL;
2369   if (strcmp (symbol->name, name) == 0)
2370     return symbol;
2371
2372   s = gfc_find_gsymbol (symbol->left, name);
2373   if (s != NULL)
2374     return s;
2375
2376   s = gfc_find_gsymbol (symbol->right, name);
2377   if (s != NULL)
2378     return s;
2379
2380   return NULL;
2381 }
2382
2383
2384 /* Compare two global symbols. Used for managing the BB tree.  */
2385
2386 static int
2387 gsym_compare (void * _s1, void * _s2)
2388 {
2389   gfc_gsymbol *s1, *s2;
2390
2391   s1 = (gfc_gsymbol *)_s1;
2392   s2 = (gfc_gsymbol *)_s2;
2393   return strcmp(s1->name, s2->name);
2394 }
2395
2396
2397 /* Get a global symbol, creating it if it doesn't exist.  */
2398
2399 gfc_gsymbol *
2400 gfc_get_gsymbol (char *name)
2401 {
2402   gfc_gsymbol *s;
2403
2404   s = gfc_find_gsymbol (gfc_gsym_root, name);
2405   if (s != NULL)
2406     return s;
2407
2408   s = gfc_getmem (sizeof (gfc_gsymbol));
2409   s->type = GSYM_UNKNOWN;
2410   strcpy (s->name, name);
2411
2412   gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2413
2414   return s;
2415 }