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