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