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