OSDN Git Service

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