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