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