OSDN Git Service

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