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