OSDN Git Service

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