OSDN Git Service

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