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