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