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