OSDN Git Service

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