OSDN Git Service

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