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