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