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