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