2004-08-23 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / fortran / symbol.c
blob7e24d4e1c8ebdd0262be3a0412fe88ee94c0b4ee
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
3 Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
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.
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.
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. */
24 #include "config.h"
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
30 #include "gfortran.h"
31 #include "parse.h"
33 /* Strings for all symbol attributes. We use these for dumping the
34 parse tree, in error messages, and also when reading and writing
35 modules. */
37 const mstring flavors[] =
39 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
40 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
41 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
42 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
43 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
44 minit (NULL, -1)
47 const mstring procedures[] =
49 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
50 minit ("MODULE-PROC", PROC_MODULE),
51 minit ("INTERNAL-PROC", PROC_INTERNAL),
52 minit ("DUMMY-PROC", PROC_DUMMY),
53 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
54 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
55 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
56 minit (NULL, -1)
59 const mstring intents[] =
61 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
62 minit ("IN", INTENT_IN),
63 minit ("OUT", INTENT_OUT),
64 minit ("INOUT", INTENT_INOUT),
65 minit (NULL, -1)
68 const mstring access_types[] =
70 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
71 minit ("PUBLIC", ACCESS_PUBLIC),
72 minit ("PRIVATE", ACCESS_PRIVATE),
73 minit (NULL, -1)
76 const mstring ifsrc_types[] =
78 minit ("UNKNOWN", IFSRC_UNKNOWN),
79 minit ("DECL", IFSRC_DECL),
80 minit ("BODY", IFSRC_IFBODY),
81 minit ("USAGE", IFSRC_USAGE)
85 /* This is to make sure the backend generates setup code in the correct
86 order. */
88 static int next_dummy_order = 1;
91 gfc_namespace *gfc_current_ns;
93 gfc_gsymbol *gfc_gsym_root = NULL;
95 static gfc_symbol *changed_syms = NULL;
98 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
100 /* The following static variable indicates whether a particular element has
101 been explicitly set or not. */
103 static int new_flag[GFC_LETTERS];
106 /* Handle a correctly parsed IMPLICIT NONE. */
108 void
109 gfc_set_implicit_none (void)
111 int i;
113 for (i = 0; i < GFC_LETTERS; i++)
115 gfc_clear_ts (&gfc_current_ns->default_type[i]);
116 gfc_current_ns->set_flag[i] = 1;
121 /* Reset the implicit range flags. */
123 void
124 gfc_clear_new_implicit (void)
126 int i;
128 for (i = 0; i < GFC_LETTERS; i++)
129 new_flag[i] = 0;
133 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
136 gfc_add_new_implicit_range (int c1, int c2)
138 int i;
140 c1 -= 'a';
141 c2 -= 'a';
143 for (i = c1; i <= c2; i++)
145 if (new_flag[i])
147 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
148 i + 'A');
149 return FAILURE;
152 new_flag[i] = 1;
155 return SUCCESS;
159 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
160 the new implicit types back into the existing types will work. */
163 gfc_merge_new_implicit (gfc_typespec * ts)
165 int i;
167 for (i = 0; i < GFC_LETTERS; i++)
169 if (new_flag[i])
172 if (gfc_current_ns->set_flag[i])
174 gfc_error ("Letter %c already has an IMPLICIT type at %C",
175 i + 'A');
176 return FAILURE;
178 gfc_current_ns->default_type[i] = *ts;
179 gfc_current_ns->set_flag[i] = 1;
182 return SUCCESS;
186 /* Given a symbol, return a pointer to the typespec for it's default
187 type. */
189 gfc_typespec *
190 gfc_get_default_type (gfc_symbol * sym, gfc_namespace * ns)
192 char letter;
194 letter = sym->name[0];
195 if (letter < 'a' || letter > 'z')
196 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
198 if (ns == NULL)
199 ns = gfc_current_ns;
201 return &ns->default_type[letter - 'a'];
205 /* Given a pointer to a symbol, set its type according to the first
206 letter of its name. Fails if the letter in question has no default
207 type. */
210 gfc_set_default_type (gfc_symbol * sym, int error_flag, gfc_namespace * ns)
212 gfc_typespec *ts;
214 if (sym->ts.type != BT_UNKNOWN)
215 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
217 ts = gfc_get_default_type (sym, ns);
219 if (ts->type == BT_UNKNOWN)
221 if (error_flag)
222 gfc_error ("Symbol '%s' at %L has no IMPLICIT type", sym->name,
223 &sym->declared_at);
225 return FAILURE;
228 sym->ts = *ts;
229 sym->attr.implicit_type = 1;
231 return SUCCESS;
235 /******************** Symbol attribute stuff *********************/
237 /* This is a generic conflict-checker. We do this to avoid having a
238 single conflict in two places. */
240 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
241 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
243 static try
244 check_conflict (symbol_attribute * attr, locus * where)
246 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
247 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
248 *intrinsic = "INTRINSIC", *allocatable = "ALLOCATABLE",
249 *elemental = "ELEMENTAL", *private = "PRIVATE", *recursive = "RECURSIVE",
250 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
251 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
252 *function = "FUNCTION", *subroutine = "SUBROUTINE",
253 *dimension = "DIMENSION";
255 const char *a1, *a2;
257 if (where == NULL)
258 where = &gfc_current_locus;
260 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
262 a1 = pointer;
263 a2 = intent;
264 goto conflict;
267 /* Check for attributes not allowed in a BLOCK DATA. */
268 if (gfc_current_state () == COMP_BLOCK_DATA)
270 a1 = NULL;
272 if (attr->allocatable)
273 a1 = allocatable;
274 if (attr->external)
275 a1 = external;
276 if (attr->optional)
277 a1 = optional;
278 if (attr->access == ACCESS_PRIVATE)
279 a1 = private;
280 if (attr->access == ACCESS_PUBLIC)
281 a1 = public;
282 if (attr->intent != INTENT_UNKNOWN)
283 a1 = intent;
285 if (a1 != NULL)
287 gfc_error
288 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
289 where);
290 return FAILURE;
294 conf (dummy, save);
295 conf (pointer, target);
296 conf (pointer, external);
297 conf (pointer, intrinsic);
298 conf (target, external);
299 conf (target, intrinsic);
300 conf (external, dimension); /* See Fortran 95's R504. */
302 conf (external, intrinsic);
303 conf (allocatable, pointer);
304 conf (allocatable, dummy); /* TODO: Allowed in Fortran 200x. */
305 conf (allocatable, function); /* TODO: Allowed in Fortran 200x. */
306 conf (allocatable, result); /* TODO: Allowed in Fortran 200x. */
307 conf (elemental, recursive);
309 conf (in_common, dummy);
310 conf (in_common, allocatable);
311 conf (in_common, result);
312 conf (dummy, result);
314 conf (in_namelist, pointer);
315 conf (in_namelist, allocatable);
317 conf (entry, result);
319 conf (function, subroutine);
321 a1 = gfc_code2string (flavors, attr->flavor);
323 if (attr->in_namelist
324 && attr->flavor != FL_VARIABLE
325 && attr->flavor != FL_UNKNOWN)
328 a2 = in_namelist;
329 goto conflict;
332 switch (attr->flavor)
334 case FL_PROGRAM:
335 case FL_BLOCK_DATA:
336 case FL_MODULE:
337 case FL_LABEL:
338 conf2 (dummy);
339 conf2 (save);
340 conf2 (pointer);
341 conf2 (target);
342 conf2 (external);
343 conf2 (intrinsic);
344 conf2 (allocatable);
345 conf2 (result);
346 conf2 (in_namelist);
347 conf2 (optional);
348 conf2 (function);
349 conf2 (subroutine);
350 break;
352 case FL_VARIABLE:
353 case FL_NAMELIST:
354 break;
356 case FL_PROCEDURE:
357 conf2 (intent);
359 if (attr->subroutine)
361 conf2(save);
362 conf2(pointer);
363 conf2(target);
364 conf2(allocatable);
365 conf2(result);
366 conf2(in_namelist);
367 conf2(function);
370 switch (attr->proc)
372 case PROC_ST_FUNCTION:
373 conf2 (in_common);
374 break;
376 case PROC_MODULE:
377 conf2 (dummy);
378 break;
380 case PROC_DUMMY:
381 conf2 (result);
382 conf2 (in_common);
383 conf2 (save);
384 break;
386 default:
387 break;
390 break;
392 case FL_DERIVED:
393 conf2 (dummy);
394 conf2 (save);
395 conf2 (pointer);
396 conf2 (target);
397 conf2 (external);
398 conf2 (intrinsic);
399 conf2 (allocatable);
400 conf2 (optional);
401 conf2 (entry);
402 conf2 (function);
403 conf2 (subroutine);
405 if (attr->intent != INTENT_UNKNOWN)
407 a2 = intent;
408 goto conflict;
410 break;
412 case FL_PARAMETER:
413 conf2 (external);
414 conf2 (intrinsic);
415 conf2 (optional);
416 conf2 (allocatable);
417 conf2 (function);
418 conf2 (subroutine);
419 conf2 (entry);
420 conf2 (pointer);
421 conf2 (target);
422 conf2 (dummy);
423 conf2 (in_common);
424 break;
426 default:
427 break;
430 return SUCCESS;
432 conflict:
433 gfc_error ("%s attribute conflicts with %s attribute at %L", a1, a2, where);
434 return FAILURE;
437 #undef conf
438 #undef conf2
441 /* Mark a symbol as referenced. */
443 void
444 gfc_set_sym_referenced (gfc_symbol * sym)
446 if (sym->attr.referenced)
447 return;
449 sym->attr.referenced = 1;
451 /* Remember which order dummy variables are accessed in. */
452 if (sym->attr.dummy)
453 sym->dummy_order = next_dummy_order++;
457 /* Common subroutine called by attribute changing subroutines in order
458 to prevent them from changing a symbol that has been
459 use-associated. Returns zero if it is OK to change the symbol,
460 nonzero if not. */
462 static int
463 check_used (symbol_attribute * attr, locus * where)
466 if (attr->use_assoc == 0)
467 return 0;
469 if (where == NULL)
470 where = &gfc_current_locus;
472 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
473 where);
475 return 1;
479 /* Used to prevent changing the attributes of a symbol after it has been
480 used. This check is only done from dummy variable as only these can be
481 used in specification expressions. Applying this to all symbols causes
482 error when we reach the body of a contained function. */
484 static int
485 check_done (symbol_attribute * attr, locus * where)
488 if (!(attr->dummy && attr->referenced))
489 return 0;
491 if (where == NULL)
492 where = &gfc_current_locus;
494 gfc_error ("Cannot change attributes of symbol at %L"
495 " after it has been used", where);
497 return 1;
501 /* Generate an error because of a duplicate attribute. */
503 static void
504 duplicate_attr (const char *attr, locus * where)
507 if (where == NULL)
508 where = &gfc_current_locus;
510 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
515 gfc_add_allocatable (symbol_attribute * attr, locus * where)
518 if (check_used (attr, where) || check_done (attr, where))
519 return FAILURE;
521 if (attr->allocatable)
523 duplicate_attr ("ALLOCATABLE", where);
524 return FAILURE;
527 attr->allocatable = 1;
528 return check_conflict (attr, where);
533 gfc_add_dimension (symbol_attribute * attr, locus * where)
536 if (check_used (attr, where) || check_done (attr, where))
537 return FAILURE;
539 if (attr->dimension)
541 duplicate_attr ("DIMENSION", where);
542 return FAILURE;
545 attr->dimension = 1;
546 return check_conflict (attr, where);
551 gfc_add_external (symbol_attribute * attr, locus * where)
554 if (check_used (attr, where) || check_done (attr, where))
555 return FAILURE;
557 if (attr->external)
559 duplicate_attr ("EXTERNAL", where);
560 return FAILURE;
563 attr->external = 1;
565 return check_conflict (attr, where);
570 gfc_add_intrinsic (symbol_attribute * attr, locus * where)
573 if (check_used (attr, where) || check_done (attr, where))
574 return FAILURE;
576 if (attr->intrinsic)
578 duplicate_attr ("INTRINSIC", where);
579 return FAILURE;
582 attr->intrinsic = 1;
584 return check_conflict (attr, where);
589 gfc_add_optional (symbol_attribute * attr, locus * where)
592 if (check_used (attr, where) || check_done (attr, where))
593 return FAILURE;
595 if (attr->optional)
597 duplicate_attr ("OPTIONAL", where);
598 return FAILURE;
601 attr->optional = 1;
602 return check_conflict (attr, where);
607 gfc_add_pointer (symbol_attribute * attr, locus * where)
610 if (check_used (attr, where) || check_done (attr, where))
611 return FAILURE;
613 attr->pointer = 1;
614 return check_conflict (attr, where);
619 gfc_add_result (symbol_attribute * attr, locus * where)
622 if (check_used (attr, where) || check_done (attr, where))
623 return FAILURE;
625 attr->result = 1;
626 return check_conflict (attr, where);
631 gfc_add_save (symbol_attribute * attr, locus * where)
634 if (check_used (attr, where))
635 return FAILURE;
637 if (gfc_pure (NULL))
639 gfc_error
640 ("SAVE attribute at %L cannot be specified in a PURE procedure",
641 where);
642 return FAILURE;
645 if (attr->save)
647 duplicate_attr ("SAVE", where);
648 return FAILURE;
651 attr->save = 1;
652 return check_conflict (attr, where);
657 gfc_add_target (symbol_attribute * attr, locus * where)
660 if (check_used (attr, where) || check_done (attr, where))
661 return FAILURE;
663 if (attr->target)
665 duplicate_attr ("TARGET", where);
666 return FAILURE;
669 attr->target = 1;
670 return check_conflict (attr, where);
675 gfc_add_dummy (symbol_attribute * attr, locus * where)
678 if (check_used (attr, where))
679 return FAILURE;
681 /* Duplicate dummy arguments are allow due to ENTRY statements. */
682 attr->dummy = 1;
683 return check_conflict (attr, where);
688 gfc_add_in_common (symbol_attribute * attr, locus * where)
691 if (check_used (attr, where) || check_done (attr, where))
692 return FAILURE;
694 /* Duplicate attribute already checked for. */
695 attr->in_common = 1;
696 if (check_conflict (attr, where) == FAILURE)
697 return FAILURE;
699 if (attr->flavor == FL_VARIABLE)
700 return SUCCESS;
702 return gfc_add_flavor (attr, FL_VARIABLE, where);
707 gfc_add_data (symbol_attribute *attr, locus *where)
710 if (check_used (attr, where))
711 return FAILURE;
713 attr->data = 1;
714 return check_conflict (attr, where);
719 gfc_add_in_namelist (symbol_attribute * attr, locus * where)
722 attr->in_namelist = 1;
723 return check_conflict (attr, where);
728 gfc_add_sequence (symbol_attribute * attr, locus * where)
731 if (check_used (attr, where))
732 return FAILURE;
734 attr->sequence = 1;
735 return check_conflict (attr, where);
740 gfc_add_elemental (symbol_attribute * attr, locus * where)
743 if (check_used (attr, where) || check_done (attr, where))
744 return FAILURE;
746 attr->elemental = 1;
747 return check_conflict (attr, where);
752 gfc_add_pure (symbol_attribute * attr, locus * where)
755 if (check_used (attr, where) || check_done (attr, where))
756 return FAILURE;
758 attr->pure = 1;
759 return check_conflict (attr, where);
764 gfc_add_recursive (symbol_attribute * attr, locus * where)
767 if (check_used (attr, where) || check_done (attr, where))
768 return FAILURE;
770 attr->recursive = 1;
771 return check_conflict (attr, where);
776 gfc_add_entry (symbol_attribute * attr, locus * where)
779 if (check_used (attr, where))
780 return FAILURE;
782 if (attr->entry)
784 duplicate_attr ("ENTRY", where);
785 return FAILURE;
788 attr->entry = 1;
789 return check_conflict (attr, where);
794 gfc_add_function (symbol_attribute * attr, locus * where)
797 if (attr->flavor != FL_PROCEDURE
798 && gfc_add_flavor (attr, FL_PROCEDURE, where) == FAILURE)
799 return FAILURE;
801 attr->function = 1;
802 return check_conflict (attr, where);
807 gfc_add_subroutine (symbol_attribute * attr, locus * where)
810 if (attr->flavor != FL_PROCEDURE
811 && gfc_add_flavor (attr, FL_PROCEDURE, where) == FAILURE)
812 return FAILURE;
814 attr->subroutine = 1;
815 return check_conflict (attr, where);
820 gfc_add_generic (symbol_attribute * attr, locus * where)
823 if (attr->flavor != FL_PROCEDURE
824 && gfc_add_flavor (attr, FL_PROCEDURE, where) == FAILURE)
825 return FAILURE;
827 attr->generic = 1;
828 return check_conflict (attr, where);
832 /* Flavors are special because some flavors are not what fortran
833 considers attributes and can be reaffirmed multiple times. */
836 gfc_add_flavor (symbol_attribute * attr, sym_flavor f, locus * where)
839 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
840 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
841 || f == FL_NAMELIST) && check_used (attr, where))
842 return FAILURE;
844 if (attr->flavor == f && f == FL_VARIABLE)
845 return SUCCESS;
847 if (attr->flavor != FL_UNKNOWN)
849 if (where == NULL)
850 where = &gfc_current_locus;
852 gfc_error ("%s attribute conflicts with %s attribute at %L",
853 gfc_code2string (flavors, attr->flavor),
854 gfc_code2string (flavors, f), where);
856 return FAILURE;
859 attr->flavor = f;
861 return check_conflict (attr, where);
866 gfc_add_procedure (symbol_attribute * attr, procedure_type t, locus * where)
869 if (check_used (attr, where) || check_done (attr, where))
870 return FAILURE;
872 if (attr->flavor != FL_PROCEDURE
873 && gfc_add_flavor (attr, FL_PROCEDURE, where) == FAILURE)
874 return FAILURE;
876 if (where == NULL)
877 where = &gfc_current_locus;
879 if (attr->proc != PROC_UNKNOWN)
881 gfc_error ("%s procedure at %L is already %s %s procedure",
882 gfc_code2string (procedures, t), where,
883 gfc_article (gfc_code2string (procedures, attr->proc)),
884 gfc_code2string (procedures, attr->proc));
886 return FAILURE;
889 attr->proc = t;
891 /* Statement functions are always scalar and functions. */
892 if (t == PROC_ST_FUNCTION
893 && ((!attr->function && gfc_add_function (attr, where) == FAILURE)
894 || attr->dimension))
895 return FAILURE;
897 return check_conflict (attr, where);
902 gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
905 if (check_used (attr, where))
906 return FAILURE;
908 if (attr->intent == INTENT_UNKNOWN)
910 attr->intent = intent;
911 return check_conflict (attr, where);
914 if (where == NULL)
915 where = &gfc_current_locus;
917 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
918 gfc_intent_string (attr->intent),
919 gfc_intent_string (intent), where);
921 return FAILURE;
925 /* No checks for use-association in public and private statements. */
928 gfc_add_access (symbol_attribute * attr, gfc_access access, locus * where)
931 if (attr->access == ACCESS_UNKNOWN)
933 attr->access = access;
934 return check_conflict (attr, where);
937 if (where == NULL)
938 where = &gfc_current_locus;
939 gfc_error ("ACCESS specification at %L was already specified", where);
941 return FAILURE;
946 gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
947 gfc_formal_arglist * formal, locus * where)
950 if (check_used (&sym->attr, where))
951 return FAILURE;
953 if (where == NULL)
954 where = &gfc_current_locus;
956 if (sym->attr.if_source != IFSRC_UNKNOWN
957 && sym->attr.if_source != IFSRC_DECL)
959 gfc_error ("Symbol '%s' at %L already has an explicit interface",
960 sym->name, where);
961 return FAILURE;
964 sym->formal = formal;
965 sym->attr.if_source = source;
967 return SUCCESS;
971 /* Add a type to a symbol. */
974 gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
976 sym_flavor flavor;
978 /* TODO: This is legal if it is reaffirming an implicit type.
979 if (check_done (&sym->attr, where))
980 return FAILURE;*/
982 if (where == NULL)
983 where = &gfc_current_locus;
985 if (sym->ts.type != BT_UNKNOWN)
987 gfc_error ("Symbol '%s' at %L already has basic type of %s", sym->name,
988 where, gfc_basic_typename (sym->ts.type));
989 return FAILURE;
992 flavor = sym->attr.flavor;
994 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
995 || flavor == FL_LABEL || (flavor == FL_PROCEDURE
996 && sym->attr.subroutine)
997 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
999 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1000 return FAILURE;
1003 sym->ts = *ts;
1004 return SUCCESS;
1008 /* Clears all attributes. */
1010 void
1011 gfc_clear_attr (symbol_attribute * attr)
1013 memset (attr, 0, sizeof(symbol_attribute));
1017 /* Check for missing attributes in the new symbol. Currently does
1018 nothing, but it's not clear that it is unnecessary yet. */
1021 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1022 locus * where ATTRIBUTE_UNUSED)
1025 return SUCCESS;
1029 /* Copy an attribute to a symbol attribute, bit by bit. Some
1030 attributes have a lot of side-effects but cannot be present given
1031 where we are called from, so we ignore some bits. */
1034 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1037 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1038 goto fail;
1040 if (src->dimension && gfc_add_dimension (dest, where) == FAILURE)
1041 goto fail;
1042 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1043 goto fail;
1044 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1045 goto fail;
1046 if (src->save && gfc_add_save (dest, where) == FAILURE)
1047 goto fail;
1048 if (src->target && gfc_add_target (dest, where) == FAILURE)
1049 goto fail;
1050 if (src->dummy && gfc_add_dummy (dest, where) == FAILURE)
1051 goto fail;
1052 if (src->result && gfc_add_result (dest, where) == FAILURE)
1053 goto fail;
1054 if (src->entry)
1055 dest->entry = 1;
1057 if (src->in_namelist && gfc_add_in_namelist (dest, where) == FAILURE)
1058 goto fail;
1060 if (src->in_common && gfc_add_in_common (dest, where) == FAILURE)
1061 goto fail;
1063 if (src->generic && gfc_add_generic (dest, where) == FAILURE)
1064 goto fail;
1065 if (src->function && gfc_add_function (dest, where) == FAILURE)
1066 goto fail;
1067 if (src->subroutine && gfc_add_subroutine (dest, where) == FAILURE)
1068 goto fail;
1070 if (src->sequence && gfc_add_sequence (dest, where) == FAILURE)
1071 goto fail;
1072 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1073 goto fail;
1074 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1075 goto fail;
1076 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1077 goto fail;
1079 if (src->flavor != FL_UNKNOWN
1080 && gfc_add_flavor (dest, src->flavor, where) == FAILURE)
1081 goto fail;
1083 if (src->intent != INTENT_UNKNOWN
1084 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1085 goto fail;
1087 if (src->access != ACCESS_UNKNOWN
1088 && gfc_add_access (dest, src->access, where) == FAILURE)
1089 goto fail;
1091 if (gfc_missing_attr (dest, where) == FAILURE)
1092 goto fail;
1094 /* The subroutines that set these bits also cause flavors to be set,
1095 and that has already happened in the original, so don't let to
1096 happen again. */
1097 if (src->external)
1098 dest->external = 1;
1099 if (src->intrinsic)
1100 dest->intrinsic = 1;
1102 return SUCCESS;
1104 fail:
1105 return FAILURE;
1109 /************** Component name management ************/
1111 /* Component names of a derived type form their own little namespaces
1112 that are separate from all other spaces. The space is composed of
1113 a singly linked list of gfc_component structures whose head is
1114 located in the parent symbol. */
1117 /* Add a component name to a symbol. The call fails if the name is
1118 already present. On success, the component pointer is modified to
1119 point to the additional component structure. */
1122 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1124 gfc_component *p, *tail;
1126 tail = NULL;
1128 for (p = sym->components; p; p = p->next)
1130 if (strcmp (p->name, name) == 0)
1132 gfc_error ("Component '%s' at %C already declared at %L",
1133 name, &p->loc);
1134 return FAILURE;
1137 tail = p;
1140 /* Allocate new component */
1141 p = gfc_get_component ();
1143 if (tail == NULL)
1144 sym->components = p;
1145 else
1146 tail->next = p;
1148 strcpy (p->name, name);
1149 p->loc = gfc_current_locus;
1151 *component = p;
1152 return SUCCESS;
1156 /* Recursive function to switch derived types of all symbol in a
1157 namespace. */
1159 static void
1160 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1162 gfc_symbol *sym;
1164 if (st == NULL)
1165 return;
1167 sym = st->n.sym;
1168 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1169 sym->ts.derived = to;
1171 switch_types (st->left, from, to);
1172 switch_types (st->right, from, to);
1176 /* This subroutine is called when a derived type is used in order to
1177 make the final determination about which version to use. The
1178 standard requires that a type be defined before it is 'used', but
1179 such types can appear in IMPLICIT statements before the actual
1180 definition. 'Using' in this context means declaring a variable to
1181 be that type or using the type constructor.
1183 If a type is used and the components haven't been defined, then we
1184 have to have a derived type in a parent unit. We find the node in
1185 the other namespace and point the symtree node in this namespace to
1186 that node. Further reference to this name point to the correct
1187 node. If we can't find the node in a parent namespace, then have
1188 an error.
1190 This subroutine takes a pointer to a symbol node and returns a
1191 pointer to the translated node or NULL for an error. Usually there
1192 is no translation and we return the node we were passed. */
1194 static gfc_symtree *
1195 gfc_use_ha_derived (gfc_symbol * sym)
1197 gfc_symbol *s, *p;
1198 gfc_typespec *t;
1199 gfc_symtree *st;
1200 int i;
1202 if (sym->ns->parent == NULL)
1203 goto bad;
1205 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1207 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1208 return NULL;
1211 if (s == NULL || s->attr.flavor != FL_DERIVED)
1212 goto bad;
1214 /* Get rid of symbol sym, translating all references to s. */
1215 for (i = 0; i < GFC_LETTERS; i++)
1217 t = &sym->ns->default_type[i];
1218 if (t->derived == sym)
1219 t->derived = s;
1222 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1223 st->n.sym = s;
1225 s->refs++;
1227 /* Unlink from list of modified symbols. */
1228 if (changed_syms == sym)
1229 changed_syms = sym->tlink;
1230 else
1231 for (p = changed_syms; p; p = p->tlink)
1232 if (p->tlink == sym)
1234 p->tlink = sym->tlink;
1235 break;
1238 switch_types (sym->ns->sym_root, sym, s);
1240 /* TODO: Also have to replace sym -> s in other lists like
1241 namelists, common lists and interface lists. */
1242 gfc_free_symbol (sym);
1244 return st;
1246 bad:
1247 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1248 sym->name);
1249 return NULL;
1253 gfc_symbol *
1254 gfc_use_derived (gfc_symbol * sym)
1256 gfc_symtree *st;
1258 if (sym->components != NULL)
1259 return sym; /* Already defined */
1261 st = gfc_use_ha_derived (sym);
1262 if (st)
1263 return st->n.sym;
1264 else
1265 return NULL;
1269 /* Given a derived type node and a component name, try to locate the
1270 component structure. Returns the NULL pointer if the component is
1271 not found or the components are private. */
1273 gfc_component *
1274 gfc_find_component (gfc_symbol * sym, const char *name)
1276 gfc_component *p;
1278 if (name == NULL)
1279 return NULL;
1281 sym = gfc_use_derived (sym);
1283 if (sym == NULL)
1284 return NULL;
1286 for (p = sym->components; p; p = p->next)
1287 if (strcmp (p->name, name) == 0)
1288 break;
1290 if (p == NULL)
1291 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1292 name, sym->name);
1293 else
1295 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1297 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1298 name, sym->name);
1299 p = NULL;
1303 return p;
1307 /* Given a symbol, free all of the component structures and everything
1308 they point to. */
1310 static void
1311 free_components (gfc_component * p)
1313 gfc_component *q;
1315 for (; p; p = q)
1317 q = p->next;
1319 gfc_free_array_spec (p->as);
1320 gfc_free_expr (p->initializer);
1322 gfc_free (p);
1327 /* Set component attributes from a standard symbol attribute
1328 structure. */
1330 void
1331 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1334 c->dimension = attr->dimension;
1335 c->pointer = attr->pointer;
1339 /* Get a standard symbol attribute structure given the component
1340 structure. */
1342 void
1343 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1346 gfc_clear_attr (attr);
1347 attr->dimension = c->dimension;
1348 attr->pointer = c->pointer;
1352 /******************** Statement label management ********************/
1354 /* Free a single gfc_st_label structure, making sure the list is not
1355 messed up. This function is called only when some parse error
1356 occurs. */
1358 void
1359 gfc_free_st_label (gfc_st_label * l)
1362 if (l == NULL)
1363 return;
1365 if (l->prev)
1366 (l->prev->next = l->next);
1368 if (l->next)
1369 (l->next->prev = l->prev);
1371 if (l->format != NULL)
1372 gfc_free_expr (l->format);
1373 gfc_free (l);
1376 /* Free a whole list of gfc_st_label structures. */
1378 static void
1379 free_st_labels (gfc_st_label * l1)
1381 gfc_st_label *l2;
1383 for (; l1; l1 = l2)
1385 l2 = l1->next;
1386 if (l1->format != NULL)
1387 gfc_free_expr (l1->format);
1388 gfc_free (l1);
1393 /* Given a label number, search for and return a pointer to the label
1394 structure, creating it if it does not exist. */
1396 gfc_st_label *
1397 gfc_get_st_label (int labelno)
1399 gfc_st_label *lp;
1401 /* First see if the label is already in this namespace. */
1402 for (lp = gfc_current_ns->st_labels; lp; lp = lp->next)
1403 if (lp->value == labelno)
1404 break;
1405 if (lp != NULL)
1406 return lp;
1408 lp = gfc_getmem (sizeof (gfc_st_label));
1410 lp->value = labelno;
1411 lp->defined = ST_LABEL_UNKNOWN;
1412 lp->referenced = ST_LABEL_UNKNOWN;
1414 lp->prev = NULL;
1415 lp->next = gfc_current_ns->st_labels;
1416 if (gfc_current_ns->st_labels)
1417 gfc_current_ns->st_labels->prev = lp;
1418 gfc_current_ns->st_labels = lp;
1420 return lp;
1424 /* Called when a statement with a statement label is about to be
1425 accepted. We add the label to the list of the current namespace,
1426 making sure it hasn't been defined previously and referenced
1427 correctly. */
1429 void
1430 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1432 int labelno;
1434 labelno = lp->value;
1436 if (lp->defined != ST_LABEL_UNKNOWN)
1437 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1438 &lp->where, label_locus);
1439 else
1441 lp->where = *label_locus;
1443 switch (type)
1445 case ST_LABEL_FORMAT:
1446 if (lp->referenced == ST_LABEL_TARGET)
1447 gfc_error ("Label %d at %C already referenced as branch target",
1448 labelno);
1449 else
1450 lp->defined = ST_LABEL_FORMAT;
1452 break;
1454 case ST_LABEL_TARGET:
1455 if (lp->referenced == ST_LABEL_FORMAT)
1456 gfc_error ("Label %d at %C already referenced as a format label",
1457 labelno);
1458 else
1459 lp->defined = ST_LABEL_TARGET;
1461 break;
1463 default:
1464 lp->defined = ST_LABEL_BAD_TARGET;
1465 lp->referenced = ST_LABEL_BAD_TARGET;
1471 /* Reference a label. Given a label and its type, see if that
1472 reference is consistent with what is known about that label,
1473 updating the unknown state. Returns FAILURE if something goes
1474 wrong. */
1477 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1479 gfc_sl_type label_type;
1480 int labelno;
1481 try rc;
1483 if (lp == NULL)
1484 return SUCCESS;
1486 labelno = lp->value;
1488 if (lp->defined != ST_LABEL_UNKNOWN)
1489 label_type = lp->defined;
1490 else
1492 label_type = lp->referenced;
1493 lp->where = gfc_current_locus;
1496 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1498 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1499 rc = FAILURE;
1500 goto done;
1503 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1504 && type == ST_LABEL_FORMAT)
1506 gfc_error ("Label %d at %C previously used as branch target", labelno);
1507 rc = FAILURE;
1508 goto done;
1511 lp->referenced = type;
1512 rc = SUCCESS;
1514 done:
1515 return rc;
1519 /************** Symbol table management subroutines ****************/
1521 /* Basic details: Fortran 95 requires a potentially unlimited number
1522 of distinct namespaces when compiling a program unit. This case
1523 occurs during a compilation of internal subprograms because all of
1524 the internal subprograms must be read before we can start
1525 generating code for the host.
1527 Given the tricky nature of the fortran grammar, we must be able to
1528 undo changes made to a symbol table if the current interpretation
1529 of a statement is found to be incorrect. Whenever a symbol is
1530 looked up, we make a copy of it and link to it. All of these
1531 symbols are kept in a singly linked list so that we can commit or
1532 undo the changes at a later time.
1534 A symtree may point to a symbol node outside of its namespace. In
1535 this case, that symbol has been used as a host associated variable
1536 at some previous time. */
1538 /* Allocate a new namespace structure. */
1540 gfc_namespace *
1541 gfc_get_namespace (gfc_namespace * parent)
1543 gfc_namespace *ns;
1544 gfc_typespec *ts;
1545 gfc_intrinsic_op in;
1546 int i;
1548 ns = gfc_getmem (sizeof (gfc_namespace));
1549 ns->sym_root = NULL;
1550 ns->uop_root = NULL;
1551 ns->default_access = ACCESS_UNKNOWN;
1552 ns->parent = parent;
1554 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1555 ns->operator_access[in] = ACCESS_UNKNOWN;
1557 /* Initialize default implicit types. */
1558 for (i = 'a'; i <= 'z'; i++)
1560 ns->set_flag[i - 'a'] = 0;
1561 ts = &ns->default_type[i - 'a'];
1563 if (ns->parent != NULL)
1565 /* Copy parent settings */
1566 *ts = ns->parent->default_type[i - 'a'];
1567 continue;
1570 if (gfc_option.flag_implicit_none != 0)
1572 gfc_clear_ts (ts);
1573 continue;
1576 if ('i' <= i && i <= 'n')
1578 ts->type = BT_INTEGER;
1579 ts->kind = gfc_default_integer_kind ();
1581 else
1583 ts->type = BT_REAL;
1584 ts->kind = gfc_default_real_kind ();
1588 ns->refs = 1;
1590 return ns;
1594 /* Comparison function for symtree nodes. */
1596 static int
1597 compare_symtree (void * _st1, void * _st2)
1599 gfc_symtree *st1, *st2;
1601 st1 = (gfc_symtree *) _st1;
1602 st2 = (gfc_symtree *) _st2;
1604 return strcmp (st1->name, st2->name);
1608 /* Allocate a new symtree node and associate it with the new symbol. */
1610 gfc_symtree *
1611 gfc_new_symtree (gfc_symtree ** root, const char *name)
1613 gfc_symtree *st;
1615 st = gfc_getmem (sizeof (gfc_symtree));
1616 strcpy (st->name, name);
1618 gfc_insert_bbt (root, st, compare_symtree);
1619 return st;
1623 /* Delete a symbol from the tree. Does not free the symbol itself! */
1625 static void
1626 delete_symtree (gfc_symtree ** root, const char *name)
1628 gfc_symtree st, *st0;
1630 st0 = gfc_find_symtree (*root, name);
1632 strcpy (st.name, name);
1633 gfc_delete_bbt (root, &st, compare_symtree);
1635 gfc_free (st0);
1639 /* Given a root symtree node and a name, try to find the symbol within
1640 the namespace. Returns NULL if the symbol is not found. */
1642 gfc_symtree *
1643 gfc_find_symtree (gfc_symtree * st, const char *name)
1645 int c;
1647 while (st != NULL)
1649 c = strcmp (name, st->name);
1650 if (c == 0)
1651 return st;
1653 st = (c < 0) ? st->left : st->right;
1656 return NULL;
1660 /* Given a name find a user operator node, creating it if it doesn't
1661 exist. These are much simpler than symbols because they can't be
1662 ambiguous with one another. */
1664 gfc_user_op *
1665 gfc_get_uop (const char *name)
1667 gfc_user_op *uop;
1668 gfc_symtree *st;
1670 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1671 if (st != NULL)
1672 return st->n.uop;
1674 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1676 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1677 strcpy (uop->name, name);
1678 uop->access = ACCESS_UNKNOWN;
1679 uop->ns = gfc_current_ns;
1681 return uop;
1685 /* Given a name find the user operator node. Returns NULL if it does
1686 not exist. */
1688 gfc_user_op *
1689 gfc_find_uop (const char *name, gfc_namespace * ns)
1691 gfc_symtree *st;
1693 if (ns == NULL)
1694 ns = gfc_current_ns;
1696 st = gfc_find_symtree (ns->uop_root, name);
1697 return (st == NULL) ? NULL : st->n.uop;
1701 /* Remove a gfc_symbol structure and everything it points to. */
1703 void
1704 gfc_free_symbol (gfc_symbol * sym)
1707 if (sym == NULL)
1708 return;
1710 gfc_free_array_spec (sym->as);
1712 free_components (sym->components);
1714 gfc_free_expr (sym->value);
1716 gfc_free_namelist (sym->namelist);
1718 gfc_free_namespace (sym->formal_ns);
1720 gfc_free_interface (sym->generic);
1722 gfc_free_formal_arglist (sym->formal);
1724 gfc_free (sym);
1728 /* Allocate and initialize a new symbol node. */
1730 gfc_symbol *
1731 gfc_new_symbol (const char *name, gfc_namespace * ns)
1733 gfc_symbol *p;
1735 p = gfc_getmem (sizeof (gfc_symbol));
1737 gfc_clear_ts (&p->ts);
1738 gfc_clear_attr (&p->attr);
1739 p->ns = ns;
1741 p->declared_at = gfc_current_locus;
1743 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1744 gfc_internal_error ("new_symbol(): Symbol name too long");
1746 strcpy (p->name, name);
1747 return p;
1751 /* Generate an error if a symbol is ambiguous. */
1753 static void
1754 ambiguous_symbol (const char *name, gfc_symtree * st)
1757 if (st->n.sym->module[0])
1758 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1759 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1760 else
1761 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1762 "from current program unit", name, st->n.sym->name);
1766 /* Search for a symbol starting in the current namespace, resorting to
1767 any parent namespaces if requested by a nonzero parent_flag.
1768 Returns nonzero if the symbol is ambiguous. */
1771 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1772 gfc_symtree ** result)
1774 gfc_symtree *st;
1776 if (ns == NULL)
1777 ns = gfc_current_ns;
1781 st = gfc_find_symtree (ns->sym_root, name);
1782 if (st != NULL)
1784 *result = st;
1785 if (st->ambiguous)
1787 ambiguous_symbol (name, st);
1788 return 1;
1791 return 0;
1794 if (!parent_flag)
1795 break;
1797 ns = ns->parent;
1799 while (ns != NULL);
1801 *result = NULL;
1802 return 0;
1807 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
1808 gfc_symbol ** result)
1810 gfc_symtree *st;
1811 int i;
1813 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
1815 if (st == NULL)
1816 *result = NULL;
1817 else
1818 *result = st->n.sym;
1820 return i;
1824 /* Save symbol with the information necessary to back it out. */
1826 static void
1827 save_symbol_data (gfc_symbol * sym)
1830 if (sym->new || sym->old_symbol != NULL)
1831 return;
1833 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
1834 *(sym->old_symbol) = *sym;
1836 sym->tlink = changed_syms;
1837 changed_syms = sym;
1841 /* Given a name, find a symbol, or create it if it does not exist yet
1842 in the current namespace. If the symbol is found we make sure that
1843 it's OK.
1845 The integer return code indicates
1846 0 All OK
1847 1 The symbol name was ambiguous
1848 2 The name meant to be established was already host associated.
1850 So if the return value is nonzero, then an error was issued. */
1853 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
1855 gfc_symtree *st;
1856 gfc_symbol *p;
1858 /* This doesn't usually happen during resolution. */
1859 if (ns == NULL)
1860 ns = gfc_current_ns;
1862 /* Try to find the symbol in ns. */
1863 st = gfc_find_symtree (ns->sym_root, name);
1865 if (st == NULL)
1867 /* If not there, create a new symbol. */
1868 p = gfc_new_symbol (name, ns);
1870 /* Add to the list of tentative symbols. */
1871 p->old_symbol = NULL;
1872 p->tlink = changed_syms;
1873 p->mark = 1;
1874 p->new = 1;
1875 changed_syms = p;
1877 st = gfc_new_symtree (&ns->sym_root, name);
1878 st->n.sym = p;
1879 p->refs++;
1882 else
1884 /* Make sure the existing symbol is OK. */
1885 if (st->ambiguous)
1887 ambiguous_symbol (name, st);
1888 return 1;
1891 p = st->n.sym;
1893 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
1895 /* Symbol is from another namespace. */
1896 gfc_error ("Symbol '%s' at %C has already been host associated",
1897 name);
1898 return 2;
1901 p->mark = 1;
1903 /* Copy in case this symbol is changed. */
1904 save_symbol_data (p);
1907 *result = st;
1908 return 0;
1913 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
1915 gfc_symtree *st;
1916 int i;
1919 i = gfc_get_sym_tree (name, ns, &st);
1920 if (i != 0)
1921 return i;
1923 if (st)
1924 *result = st->n.sym;
1925 else
1926 *result = NULL;
1927 return i;
1931 /* Subroutine that searches for a symbol, creating it if it doesn't
1932 exist, but tries to host-associate the symbol if possible. */
1935 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
1937 gfc_symtree *st;
1938 int i;
1940 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1941 if (st != NULL)
1943 save_symbol_data (st->n.sym);
1945 *result = st;
1946 return i;
1949 if (gfc_current_ns->parent != NULL)
1951 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
1952 if (i)
1953 return i;
1955 if (st != NULL)
1957 *result = st;
1958 return 0;
1962 return gfc_get_sym_tree (name, gfc_current_ns, result);
1967 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
1969 int i;
1970 gfc_symtree *st;
1972 i = gfc_get_ha_sym_tree (name, &st);
1974 if (st)
1975 *result = st->n.sym;
1976 else
1977 *result = NULL;
1979 return i;
1982 /* Return true if both symbols could refer to the same data object. Does
1983 not take account of aliasing due to equivalence statements. */
1986 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
1988 /* Aliasing isn't possible if the symbols have different base types. */
1989 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
1990 return 0;
1992 /* Pointers can point to other pointers, target objects and allocatable
1993 objects. Two allocatable objects cannot share the same storage. */
1994 if (lsym->attr.pointer
1995 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
1996 return 1;
1997 if (lsym->attr.target && rsym->attr.pointer)
1998 return 1;
1999 if (lsym->attr.allocatable && rsym->attr.pointer)
2000 return 1;
2002 return 0;
2006 /* Undoes all the changes made to symbols in the current statement.
2007 This subroutine is made simpler due to the fact that attributes are
2008 never removed once added. */
2010 void
2011 gfc_undo_symbols (void)
2013 gfc_symbol *p, *q, *old;
2015 for (p = changed_syms; p; p = q)
2017 q = p->tlink;
2019 if (p->new)
2021 /* Symbol was new. */
2022 delete_symtree (&p->ns->sym_root, p->name);
2024 p->refs--;
2025 if (p->refs < 0)
2026 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2027 if (p->refs == 0)
2028 gfc_free_symbol (p);
2029 continue;
2032 /* Restore previous state of symbol. Just copy simple stuff. */
2033 p->mark = 0;
2034 old = p->old_symbol;
2036 p->ts.type = old->ts.type;
2037 p->ts.kind = old->ts.kind;
2039 p->attr = old->attr;
2041 if (p->value != old->value)
2043 gfc_free_expr (old->value);
2044 p->value = NULL;
2047 if (p->as != old->as)
2049 if (p->as)
2050 gfc_free_array_spec (p->as);
2051 p->as = old->as;
2054 p->generic = old->generic;
2055 p->component_access = old->component_access;
2057 if (p->namelist != NULL && old->namelist == NULL)
2059 gfc_free_namelist (p->namelist);
2060 p->namelist = NULL;
2062 else
2065 if (p->namelist_tail != old->namelist_tail)
2067 gfc_free_namelist (old->namelist_tail);
2068 old->namelist_tail->next = NULL;
2072 p->namelist_tail = old->namelist_tail;
2074 if (p->formal != old->formal)
2076 gfc_free_formal_arglist (p->formal);
2077 p->formal = old->formal;
2080 gfc_free (p->old_symbol);
2081 p->old_symbol = NULL;
2082 p->tlink = NULL;
2085 changed_syms = NULL;
2089 /* Makes the changes made in the current statement permanent-- gets
2090 rid of undo information. */
2092 void
2093 gfc_commit_symbols (void)
2095 gfc_symbol *p, *q;
2097 for (p = changed_syms; p; p = q)
2099 q = p->tlink;
2100 p->tlink = NULL;
2101 p->mark = 0;
2102 p->new = 0;
2104 if (p->old_symbol != NULL)
2106 gfc_free (p->old_symbol);
2107 p->old_symbol = NULL;
2111 changed_syms = NULL;
2115 /* Recursive function that deletes an entire tree and all the common
2116 head structures it points to. */
2118 static void
2119 free_common_tree (gfc_symtree * common_tree)
2121 if (common_tree == NULL)
2122 return;
2124 free_common_tree (common_tree->left);
2125 free_common_tree (common_tree->right);
2127 gfc_free (common_tree);
2131 /* Recursive function that deletes an entire tree and all the user
2132 operator nodes that it contains. */
2134 static void
2135 free_uop_tree (gfc_symtree * uop_tree)
2138 if (uop_tree == NULL)
2139 return;
2141 free_uop_tree (uop_tree->left);
2142 free_uop_tree (uop_tree->right);
2144 gfc_free_interface (uop_tree->n.uop->operator);
2146 gfc_free (uop_tree->n.uop);
2147 gfc_free (uop_tree);
2151 /* Recursive function that deletes an entire tree and all the symbols
2152 that it contains. */
2154 static void
2155 free_sym_tree (gfc_symtree * sym_tree)
2157 gfc_namespace *ns;
2158 gfc_symbol *sym;
2160 if (sym_tree == NULL)
2161 return;
2163 free_sym_tree (sym_tree->left);
2164 free_sym_tree (sym_tree->right);
2166 sym = sym_tree->n.sym;
2168 sym->refs--;
2169 if (sym->refs < 0)
2170 gfc_internal_error ("free_sym_tree(): Negative refs");
2172 if (sym->formal_ns != NULL && sym->refs == 1)
2174 /* As formal_ns contains a reference to sym, delete formal_ns just
2175 before the deletion of sym. */
2176 ns = sym->formal_ns;
2177 sym->formal_ns = NULL;
2178 gfc_free_namespace (ns);
2180 else if (sym->refs == 0)
2182 /* Go ahead and delete the symbol. */
2183 gfc_free_symbol (sym);
2186 gfc_free (sym_tree);
2190 /* Free a namespace structure and everything below it. Interface
2191 lists associated with intrinsic operators are not freed. These are
2192 taken care of when a specific name is freed. */
2194 void
2195 gfc_free_namespace (gfc_namespace * ns)
2197 gfc_charlen *cl, *cl2;
2198 gfc_namespace *p, *q;
2199 gfc_intrinsic_op i;
2201 if (ns == NULL)
2202 return;
2204 ns->refs--;
2205 if (ns->refs > 0)
2206 return;
2207 assert (ns->refs == 0);
2209 gfc_free_statements (ns->code);
2211 free_sym_tree (ns->sym_root);
2212 free_uop_tree (ns->uop_root);
2213 free_common_tree (ns->common_root);
2215 for (cl = ns->cl_list; cl; cl = cl2)
2217 cl2 = cl->next;
2218 gfc_free_expr (cl->length);
2219 gfc_free (cl);
2222 free_st_labels (ns->st_labels);
2224 gfc_free_equiv (ns->equiv);
2226 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2227 gfc_free_interface (ns->operator[i]);
2229 gfc_free_data (ns->data);
2230 p = ns->contained;
2231 gfc_free (ns);
2233 /* Recursively free any contained namespaces. */
2234 while (p != NULL)
2236 q = p;
2237 p = p->sibling;
2239 gfc_free_namespace (q);
2244 void
2245 gfc_symbol_init_2 (void)
2248 gfc_current_ns = gfc_get_namespace (NULL);
2252 void
2253 gfc_symbol_done_2 (void)
2256 gfc_free_namespace (gfc_current_ns);
2257 gfc_current_ns = NULL;
2261 /* Clear mark bits from symbol nodes associated with a symtree node. */
2263 static void
2264 clear_sym_mark (gfc_symtree * st)
2267 st->n.sym->mark = 0;
2271 /* Recursively traverse the symtree nodes. */
2273 void
2274 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2276 if (st != NULL)
2278 (*func) (st);
2280 gfc_traverse_symtree (st->left, func);
2281 gfc_traverse_symtree (st->right, func);
2286 /* Recursive namespace traversal function. */
2288 static void
2289 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2292 if (st == NULL)
2293 return;
2295 if (st->n.sym->mark == 0)
2296 (*func) (st->n.sym);
2297 st->n.sym->mark = 1;
2299 traverse_ns (st->left, func);
2300 traverse_ns (st->right, func);
2304 /* Call a given function for all symbols in the namespace. We take
2305 care that each gfc_symbol node is called exactly once. */
2307 void
2308 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2311 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2313 traverse_ns (ns->sym_root, func);
2317 /* Given a symbol, mark it as SAVEd if it is allowed. */
2319 static void
2320 save_symbol (gfc_symbol * sym)
2323 if (sym->attr.use_assoc)
2324 return;
2326 if (sym->attr.in_common
2327 || sym->attr.dummy
2328 || sym->attr.flavor != FL_VARIABLE)
2329 return;
2331 gfc_add_save (&sym->attr, &sym->declared_at);
2335 /* Mark those symbols which can be SAVEd as such. */
2337 void
2338 gfc_save_all (gfc_namespace * ns)
2341 gfc_traverse_ns (ns, save_symbol);
2345 #ifdef GFC_DEBUG
2346 /* Make sure that no changes to symbols are pending. */
2348 void
2349 gfc_symbol_state(void) {
2351 if (changed_syms != NULL)
2352 gfc_internal_error("Symbol changes still pending!");
2354 #endif
2357 /************** Global symbol handling ************/
2360 /* Search a tree for the global symbol. */
2362 gfc_gsymbol *
2363 gfc_find_gsymbol (gfc_gsymbol *symbol, char *name)
2365 gfc_gsymbol *s;
2367 if (symbol == NULL)
2368 return NULL;
2369 if (strcmp (symbol->name, name) == 0)
2370 return symbol;
2372 s = gfc_find_gsymbol (symbol->left, name);
2373 if (s != NULL)
2374 return s;
2376 s = gfc_find_gsymbol (symbol->right, name);
2377 if (s != NULL)
2378 return s;
2380 return NULL;
2384 /* Compare two global symbols. Used for managing the BB tree. */
2386 static int
2387 gsym_compare (void * _s1, void * _s2)
2389 gfc_gsymbol *s1, *s2;
2391 s1 = (gfc_gsymbol *)_s1;
2392 s2 = (gfc_gsymbol *)_s2;
2393 return strcmp(s1->name, s2->name);
2397 /* Get a global symbol, creating it if it doesn't exist. */
2399 gfc_gsymbol *
2400 gfc_get_gsymbol (char *name)
2402 gfc_gsymbol *s;
2404 s = gfc_find_gsymbol (gfc_gsym_root, name);
2405 if (s != NULL)
2406 return s;
2408 s = gfc_getmem (sizeof (gfc_gsymbol));
2409 s->type = GSYM_UNKNOWN;
2410 strcpy (s->name, name);
2412 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2414 return s;