OSDN Git Service

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