2005-10-12 Joe Buck <Joe.Buck@synopsys.com>
[official-gcc.git] / gcc / fortran / symbol.c
blob98ce66fef98da861af621e6b7bb83214ae780b5f
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
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, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "gfortran.h"
27 #include "parse.h"
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. */
33 const mstring flavors[] =
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)
43 const mstring procedures[] =
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)
55 const mstring intents[] =
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)
64 const mstring access_types[] =
66 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
67 minit ("PUBLIC", ACCESS_PUBLIC),
68 minit ("PRIVATE", ACCESS_PRIVATE),
69 minit (NULL, -1)
72 const mstring ifsrc_types[] =
74 minit ("UNKNOWN", IFSRC_UNKNOWN),
75 minit ("DECL", IFSRC_DECL),
76 minit ("BODY", IFSRC_IFBODY),
77 minit ("USAGE", IFSRC_USAGE)
81 /* This is to make sure the backend generates setup code in the correct
82 order. */
84 static int next_dummy_order = 1;
87 gfc_namespace *gfc_current_ns;
89 gfc_gsymbol *gfc_gsym_root = NULL;
91 static gfc_symbol *changed_syms = NULL;
94 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
96 /* The following static variable indicates whether a particular element has
97 been explicitly set or not. */
99 static int new_flag[GFC_LETTERS];
102 /* Handle a correctly parsed IMPLICIT NONE. */
104 void
105 gfc_set_implicit_none (void)
107 int i;
109 if (gfc_current_ns->seen_implicit_none)
111 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
112 return;
115 gfc_current_ns->seen_implicit_none = 1;
117 for (i = 0; i < GFC_LETTERS; i++)
119 gfc_clear_ts (&gfc_current_ns->default_type[i]);
120 gfc_current_ns->set_flag[i] = 1;
125 /* Reset the implicit range flags. */
127 void
128 gfc_clear_new_implicit (void)
130 int i;
132 for (i = 0; i < GFC_LETTERS; i++)
133 new_flag[i] = 0;
137 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
140 gfc_add_new_implicit_range (int c1, int c2)
142 int i;
144 c1 -= 'a';
145 c2 -= 'a';
147 for (i = c1; i <= c2; i++)
149 if (new_flag[i])
151 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
152 i + 'A');
153 return FAILURE;
156 new_flag[i] = 1;
159 return SUCCESS;
163 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
164 the new implicit types back into the existing types will work. */
167 gfc_merge_new_implicit (gfc_typespec * ts)
169 int i;
171 if (gfc_current_ns->seen_implicit_none)
173 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
174 return FAILURE;
177 for (i = 0; i < GFC_LETTERS; i++)
179 if (new_flag[i])
182 if (gfc_current_ns->set_flag[i])
184 gfc_error ("Letter %c already has an IMPLICIT type at %C",
185 i + 'A');
186 return FAILURE;
188 gfc_current_ns->default_type[i] = *ts;
189 gfc_current_ns->set_flag[i] = 1;
192 return SUCCESS;
196 /* Given a symbol, return a pointer to the typespec for its default type. */
198 gfc_typespec *
199 gfc_get_default_type (gfc_symbol * sym, gfc_namespace * ns)
201 char letter;
203 letter = sym->name[0];
204 if (letter < 'a' || letter > 'z')
205 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
207 if (ns == NULL)
208 ns = gfc_current_ns;
210 return &ns->default_type[letter - 'a'];
214 /* Given a pointer to a symbol, set its type according to the first
215 letter of its name. Fails if the letter in question has no default
216 type. */
219 gfc_set_default_type (gfc_symbol * sym, int error_flag, gfc_namespace * ns)
221 gfc_typespec *ts;
223 if (sym->ts.type != BT_UNKNOWN)
224 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
226 ts = gfc_get_default_type (sym, ns);
228 if (ts->type == BT_UNKNOWN)
230 if (error_flag && !sym->attr.untyped)
232 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
233 sym->name, &sym->declared_at);
234 sym->attr.untyped = 1; /* Ensure we only give an error once. */
237 return FAILURE;
240 sym->ts = *ts;
241 sym->attr.implicit_type = 1;
243 return SUCCESS;
247 /******************** Symbol attribute stuff *********************/
249 /* This is a generic conflict-checker. We do this to avoid having a
250 single conflict in two places. */
252 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
253 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
255 static try
256 check_conflict (symbol_attribute * attr, const char * name, locus * where)
258 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
259 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
260 *intrinsic = "INTRINSIC", *allocatable = "ALLOCATABLE",
261 *elemental = "ELEMENTAL", *private = "PRIVATE", *recursive = "RECURSIVE",
262 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
263 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
264 *function = "FUNCTION", *subroutine = "SUBROUTINE",
265 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
266 *use_assoc = "USE ASSOCIATED";
268 const char *a1, *a2;
270 if (where == NULL)
271 where = &gfc_current_locus;
273 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
275 a1 = pointer;
276 a2 = intent;
277 goto conflict;
280 /* Check for attributes not allowed in a BLOCK DATA. */
281 if (gfc_current_state () == COMP_BLOCK_DATA)
283 a1 = NULL;
285 if (attr->allocatable)
286 a1 = allocatable;
287 if (attr->external)
288 a1 = external;
289 if (attr->optional)
290 a1 = optional;
291 if (attr->access == ACCESS_PRIVATE)
292 a1 = private;
293 if (attr->access == ACCESS_PUBLIC)
294 a1 = public;
295 if (attr->intent != INTENT_UNKNOWN)
296 a1 = intent;
298 if (a1 != NULL)
300 gfc_error
301 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
302 where);
303 return FAILURE;
307 conf (dummy, save);
308 conf (pointer, target);
309 conf (pointer, external);
310 conf (pointer, intrinsic);
311 conf (target, external);
312 conf (target, intrinsic);
313 conf (external, dimension); /* See Fortran 95's R504. */
315 conf (external, intrinsic);
316 conf (allocatable, pointer);
317 conf (allocatable, dummy); /* TODO: Allowed in Fortran 200x. */
318 conf (allocatable, function); /* TODO: Allowed in Fortran 200x. */
319 conf (allocatable, result); /* TODO: Allowed in Fortran 200x. */
320 conf (elemental, recursive);
322 conf (in_common, dummy);
323 conf (in_common, allocatable);
324 conf (in_common, result);
325 conf (in_common, save);
326 conf (result, save);
328 conf (dummy, result);
330 conf (in_equivalence, use_assoc);
331 conf (in_equivalence, dummy);
332 conf (in_equivalence, target);
333 conf (in_equivalence, pointer);
334 conf (in_equivalence, function);
335 conf (in_equivalence, result);
336 conf (in_equivalence, entry);
337 conf (in_equivalence, allocatable);
339 conf (in_namelist, pointer);
340 conf (in_namelist, allocatable);
342 conf (entry, result);
344 conf (function, subroutine);
346 a1 = gfc_code2string (flavors, attr->flavor);
348 if (attr->in_namelist
349 && attr->flavor != FL_VARIABLE
350 && attr->flavor != FL_UNKNOWN)
353 a2 = in_namelist;
354 goto conflict;
357 switch (attr->flavor)
359 case FL_PROGRAM:
360 case FL_BLOCK_DATA:
361 case FL_MODULE:
362 case FL_LABEL:
363 conf2 (dummy);
364 conf2 (save);
365 conf2 (pointer);
366 conf2 (target);
367 conf2 (external);
368 conf2 (intrinsic);
369 conf2 (allocatable);
370 conf2 (result);
371 conf2 (in_namelist);
372 conf2 (optional);
373 conf2 (function);
374 conf2 (subroutine);
375 break;
377 case FL_VARIABLE:
378 case FL_NAMELIST:
379 break;
381 case FL_PROCEDURE:
382 conf2 (intent);
384 if (attr->subroutine)
386 conf2(save);
387 conf2(pointer);
388 conf2(target);
389 conf2(allocatable);
390 conf2(result);
391 conf2(in_namelist);
392 conf2(function);
395 switch (attr->proc)
397 case PROC_ST_FUNCTION:
398 conf2 (in_common);
399 conf2 (dummy);
400 break;
402 case PROC_MODULE:
403 conf2 (dummy);
404 break;
406 case PROC_DUMMY:
407 conf2 (result);
408 conf2 (in_common);
409 conf2 (save);
410 break;
412 default:
413 break;
416 break;
418 case FL_DERIVED:
419 conf2 (dummy);
420 conf2 (save);
421 conf2 (pointer);
422 conf2 (target);
423 conf2 (external);
424 conf2 (intrinsic);
425 conf2 (allocatable);
426 conf2 (optional);
427 conf2 (entry);
428 conf2 (function);
429 conf2 (subroutine);
431 if (attr->intent != INTENT_UNKNOWN)
433 a2 = intent;
434 goto conflict;
436 break;
438 case FL_PARAMETER:
439 conf2 (external);
440 conf2 (intrinsic);
441 conf2 (optional);
442 conf2 (allocatable);
443 conf2 (function);
444 conf2 (subroutine);
445 conf2 (entry);
446 conf2 (pointer);
447 conf2 (target);
448 conf2 (dummy);
449 conf2 (in_common);
450 conf2 (save);
451 break;
453 default:
454 break;
457 return SUCCESS;
459 conflict:
460 if (name == NULL)
461 gfc_error ("%s attribute conflicts with %s attribute at %L",
462 a1, a2, where);
463 else
464 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
465 a1, a2, name, where);
467 return FAILURE;
470 #undef conf
471 #undef conf2
474 /* Mark a symbol as referenced. */
476 void
477 gfc_set_sym_referenced (gfc_symbol * sym)
479 if (sym->attr.referenced)
480 return;
482 sym->attr.referenced = 1;
484 /* Remember which order dummy variables are accessed in. */
485 if (sym->attr.dummy)
486 sym->dummy_order = next_dummy_order++;
490 /* Common subroutine called by attribute changing subroutines in order
491 to prevent them from changing a symbol that has been
492 use-associated. Returns zero if it is OK to change the symbol,
493 nonzero if not. */
495 static int
496 check_used (symbol_attribute * attr, const char * name, locus * where)
499 if (attr->use_assoc == 0)
500 return 0;
502 if (where == NULL)
503 where = &gfc_current_locus;
505 if (name == NULL)
506 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
507 where);
508 else
509 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
510 name, where);
512 return 1;
516 /* Used to prevent changing the attributes of a symbol after it has been
517 used. This check is only done for dummy variables as only these can be
518 used in specification expressions. Applying this to all symbols causes
519 an error when we reach the body of a contained function. */
521 static int
522 check_done (symbol_attribute * attr, locus * where)
525 if (!(attr->dummy && attr->referenced))
526 return 0;
528 if (where == NULL)
529 where = &gfc_current_locus;
531 gfc_error ("Cannot change attributes of symbol at %L"
532 " after it has been used", where);
534 return 1;
538 /* Generate an error because of a duplicate attribute. */
540 static void
541 duplicate_attr (const char *attr, locus * where)
544 if (where == NULL)
545 where = &gfc_current_locus;
547 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
552 gfc_add_allocatable (symbol_attribute * attr, locus * where)
555 if (check_used (attr, NULL, where) || check_done (attr, where))
556 return FAILURE;
558 if (attr->allocatable)
560 duplicate_attr ("ALLOCATABLE", where);
561 return FAILURE;
564 attr->allocatable = 1;
565 return check_conflict (attr, NULL, where);
570 gfc_add_dimension (symbol_attribute * attr, const char *name, locus * where)
573 if (check_used (attr, name, where) || check_done (attr, where))
574 return FAILURE;
576 if (attr->dimension)
578 duplicate_attr ("DIMENSION", where);
579 return FAILURE;
582 attr->dimension = 1;
583 return check_conflict (attr, name, where);
588 gfc_add_external (symbol_attribute * attr, locus * where)
591 if (check_used (attr, NULL, where) || check_done (attr, where))
592 return FAILURE;
594 if (attr->external)
596 duplicate_attr ("EXTERNAL", where);
597 return FAILURE;
600 attr->external = 1;
602 return check_conflict (attr, NULL, where);
607 gfc_add_intrinsic (symbol_attribute * attr, locus * where)
610 if (check_used (attr, NULL, where) || check_done (attr, where))
611 return FAILURE;
613 if (attr->intrinsic)
615 duplicate_attr ("INTRINSIC", where);
616 return FAILURE;
619 attr->intrinsic = 1;
621 return check_conflict (attr, NULL, where);
626 gfc_add_optional (symbol_attribute * attr, locus * where)
629 if (check_used (attr, NULL, where) || check_done (attr, where))
630 return FAILURE;
632 if (attr->optional)
634 duplicate_attr ("OPTIONAL", where);
635 return FAILURE;
638 attr->optional = 1;
639 return check_conflict (attr, NULL, where);
644 gfc_add_pointer (symbol_attribute * attr, locus * where)
647 if (check_used (attr, NULL, where) || check_done (attr, where))
648 return FAILURE;
650 attr->pointer = 1;
651 return check_conflict (attr, NULL, where);
656 gfc_add_result (symbol_attribute * attr, const char *name, locus * where)
659 if (check_used (attr, name, where) || check_done (attr, where))
660 return FAILURE;
662 attr->result = 1;
663 return check_conflict (attr, name, where);
668 gfc_add_save (symbol_attribute * attr, const char *name, locus * where)
671 if (check_used (attr, name, where))
672 return FAILURE;
674 if (gfc_pure (NULL))
676 gfc_error
677 ("SAVE attribute at %L cannot be specified in a PURE procedure",
678 where);
679 return FAILURE;
682 if (attr->save)
684 duplicate_attr ("SAVE", where);
685 return FAILURE;
688 attr->save = 1;
689 return check_conflict (attr, name, where);
694 gfc_add_target (symbol_attribute * attr, locus * where)
697 if (check_used (attr, NULL, where) || check_done (attr, where))
698 return FAILURE;
700 if (attr->target)
702 duplicate_attr ("TARGET", where);
703 return FAILURE;
706 attr->target = 1;
707 return check_conflict (attr, NULL, where);
712 gfc_add_dummy (symbol_attribute * attr, const char *name, locus * where)
715 if (check_used (attr, name, where))
716 return FAILURE;
718 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
719 attr->dummy = 1;
720 return check_conflict (attr, name, where);
725 gfc_add_in_common (symbol_attribute * attr, const char *name, locus * where)
728 if (check_used (attr, name, where) || check_done (attr, where))
729 return FAILURE;
731 /* Duplicate attribute already checked for. */
732 attr->in_common = 1;
733 if (check_conflict (attr, name, where) == FAILURE)
734 return FAILURE;
736 if (attr->flavor == FL_VARIABLE)
737 return SUCCESS;
739 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
743 gfc_add_in_equivalence (symbol_attribute * attr, const char *name, locus * where)
746 /* Duplicate attribute already checked for. */
747 attr->in_equivalence = 1;
748 if (check_conflict (attr, name, where) == FAILURE)
749 return FAILURE;
751 if (attr->flavor == FL_VARIABLE)
752 return SUCCESS;
754 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
759 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
762 if (check_used (attr, name, where))
763 return FAILURE;
765 attr->data = 1;
766 return check_conflict (attr, name, where);
771 gfc_add_in_namelist (symbol_attribute * attr, const char *name,
772 locus * where)
775 attr->in_namelist = 1;
776 return check_conflict (attr, name, where);
781 gfc_add_sequence (symbol_attribute * attr, const char *name, locus * where)
784 if (check_used (attr, name, where))
785 return FAILURE;
787 attr->sequence = 1;
788 return check_conflict (attr, name, where);
793 gfc_add_elemental (symbol_attribute * attr, locus * where)
796 if (check_used (attr, NULL, where) || check_done (attr, where))
797 return FAILURE;
799 attr->elemental = 1;
800 return check_conflict (attr, NULL, where);
805 gfc_add_pure (symbol_attribute * attr, locus * where)
808 if (check_used (attr, NULL, where) || check_done (attr, where))
809 return FAILURE;
811 attr->pure = 1;
812 return check_conflict (attr, NULL, where);
817 gfc_add_recursive (symbol_attribute * attr, locus * where)
820 if (check_used (attr, NULL, where) || check_done (attr, where))
821 return FAILURE;
823 attr->recursive = 1;
824 return check_conflict (attr, NULL, where);
829 gfc_add_entry (symbol_attribute * attr, const char *name, locus * where)
832 if (check_used (attr, name, where))
833 return FAILURE;
835 if (attr->entry)
837 duplicate_attr ("ENTRY", where);
838 return FAILURE;
841 attr->entry = 1;
842 return check_conflict (attr, name, where);
847 gfc_add_function (symbol_attribute * attr, const char *name, locus * where)
850 if (attr->flavor != FL_PROCEDURE
851 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
852 return FAILURE;
854 attr->function = 1;
855 return check_conflict (attr, name, where);
860 gfc_add_subroutine (symbol_attribute * attr, const char *name, locus * where)
863 if (attr->flavor != FL_PROCEDURE
864 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
865 return FAILURE;
867 attr->subroutine = 1;
868 return check_conflict (attr, name, where);
873 gfc_add_generic (symbol_attribute * attr, const char *name, locus * where)
876 if (attr->flavor != FL_PROCEDURE
877 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
878 return FAILURE;
880 attr->generic = 1;
881 return check_conflict (attr, name, where);
885 /* Flavors are special because some flavors are not what Fortran
886 considers attributes and can be reaffirmed multiple times. */
889 gfc_add_flavor (symbol_attribute * attr, sym_flavor f, const char *name,
890 locus * where)
893 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
894 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
895 || f == FL_NAMELIST) && check_used (attr, name, where))
896 return FAILURE;
898 if (attr->flavor == f && f == FL_VARIABLE)
899 return SUCCESS;
901 if (attr->flavor != FL_UNKNOWN)
903 if (where == NULL)
904 where = &gfc_current_locus;
906 gfc_error ("%s attribute conflicts with %s attribute at %L",
907 gfc_code2string (flavors, attr->flavor),
908 gfc_code2string (flavors, f), where);
910 return FAILURE;
913 attr->flavor = f;
915 return check_conflict (attr, name, where);
920 gfc_add_procedure (symbol_attribute * attr, procedure_type t,
921 const char *name, locus * where)
924 if (check_used (attr, name, where) || check_done (attr, where))
925 return FAILURE;
927 if (attr->flavor != FL_PROCEDURE
928 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
929 return FAILURE;
931 if (where == NULL)
932 where = &gfc_current_locus;
934 if (attr->proc != PROC_UNKNOWN)
936 gfc_error ("%s procedure at %L is already declared as %s procedure",
937 gfc_code2string (procedures, t), where,
938 gfc_code2string (procedures, attr->proc));
940 return FAILURE;
943 attr->proc = t;
945 /* Statement functions are always scalar and functions. */
946 if (t == PROC_ST_FUNCTION
947 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
948 || attr->dimension))
949 return FAILURE;
951 return check_conflict (attr, name, where);
956 gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
959 if (check_used (attr, NULL, where))
960 return FAILURE;
962 if (attr->intent == INTENT_UNKNOWN)
964 attr->intent = intent;
965 return check_conflict (attr, NULL, where);
968 if (where == NULL)
969 where = &gfc_current_locus;
971 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
972 gfc_intent_string (attr->intent),
973 gfc_intent_string (intent), where);
975 return FAILURE;
979 /* No checks for use-association in public and private statements. */
982 gfc_add_access (symbol_attribute * attr, gfc_access access,
983 const char *name, locus * where)
986 if (attr->access == ACCESS_UNKNOWN)
988 attr->access = access;
989 return check_conflict (attr, name, where);
992 if (where == NULL)
993 where = &gfc_current_locus;
994 gfc_error ("ACCESS specification at %L was already specified", where);
996 return FAILURE;
1001 gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
1002 gfc_formal_arglist * formal, locus * where)
1005 if (check_used (&sym->attr, sym->name, where))
1006 return FAILURE;
1008 if (where == NULL)
1009 where = &gfc_current_locus;
1011 if (sym->attr.if_source != IFSRC_UNKNOWN
1012 && sym->attr.if_source != IFSRC_DECL)
1014 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1015 sym->name, where);
1016 return FAILURE;
1019 sym->formal = formal;
1020 sym->attr.if_source = source;
1022 return SUCCESS;
1026 /* Add a type to a symbol. */
1029 gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
1031 sym_flavor flavor;
1033 /* TODO: This is legal if it is reaffirming an implicit type.
1034 if (check_done (&sym->attr, where))
1035 return FAILURE;*/
1037 if (where == NULL)
1038 where = &gfc_current_locus;
1040 if (sym->ts.type != BT_UNKNOWN)
1042 gfc_error ("Symbol '%s' at %L already has basic type of %s", sym->name,
1043 where, gfc_basic_typename (sym->ts.type));
1044 return FAILURE;
1047 flavor = sym->attr.flavor;
1049 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1050 || flavor == FL_LABEL || (flavor == FL_PROCEDURE
1051 && sym->attr.subroutine)
1052 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1054 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1055 return FAILURE;
1058 sym->ts = *ts;
1059 return SUCCESS;
1063 /* Clears all attributes. */
1065 void
1066 gfc_clear_attr (symbol_attribute * attr)
1068 memset (attr, 0, sizeof(symbol_attribute));
1072 /* Check for missing attributes in the new symbol. Currently does
1073 nothing, but it's not clear that it is unnecessary yet. */
1076 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1077 locus * where ATTRIBUTE_UNUSED)
1080 return SUCCESS;
1084 /* Copy an attribute to a symbol attribute, bit by bit. Some
1085 attributes have a lot of side-effects but cannot be present given
1086 where we are called from, so we ignore some bits. */
1089 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1092 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1093 goto fail;
1095 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1096 goto fail;
1097 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1098 goto fail;
1099 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1100 goto fail;
1101 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1102 goto fail;
1103 if (src->target && gfc_add_target (dest, where) == FAILURE)
1104 goto fail;
1105 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1106 goto fail;
1107 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1108 goto fail;
1109 if (src->entry)
1110 dest->entry = 1;
1112 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1113 goto fail;
1115 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1116 goto fail;
1118 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1119 goto fail;
1120 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1121 goto fail;
1122 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1123 goto fail;
1125 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1126 goto fail;
1127 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1128 goto fail;
1129 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1130 goto fail;
1131 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1132 goto fail;
1134 if (src->flavor != FL_UNKNOWN
1135 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1136 goto fail;
1138 if (src->intent != INTENT_UNKNOWN
1139 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1140 goto fail;
1142 if (src->access != ACCESS_UNKNOWN
1143 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1144 goto fail;
1146 if (gfc_missing_attr (dest, where) == FAILURE)
1147 goto fail;
1149 /* The subroutines that set these bits also cause flavors to be set,
1150 and that has already happened in the original, so don't let it
1151 happen again. */
1152 if (src->external)
1153 dest->external = 1;
1154 if (src->intrinsic)
1155 dest->intrinsic = 1;
1157 return SUCCESS;
1159 fail:
1160 return FAILURE;
1164 /************** Component name management ************/
1166 /* Component names of a derived type form their own little namespaces
1167 that are separate from all other spaces. The space is composed of
1168 a singly linked list of gfc_component structures whose head is
1169 located in the parent symbol. */
1172 /* Add a component name to a symbol. The call fails if the name is
1173 already present. On success, the component pointer is modified to
1174 point to the additional component structure. */
1177 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1179 gfc_component *p, *tail;
1181 tail = NULL;
1183 for (p = sym->components; p; p = p->next)
1185 if (strcmp (p->name, name) == 0)
1187 gfc_error ("Component '%s' at %C already declared at %L",
1188 name, &p->loc);
1189 return FAILURE;
1192 tail = p;
1195 /* Allocate a new component. */
1196 p = gfc_get_component ();
1198 if (tail == NULL)
1199 sym->components = p;
1200 else
1201 tail->next = p;
1203 p->name = gfc_get_string (name);
1204 p->loc = gfc_current_locus;
1206 *component = p;
1207 return SUCCESS;
1211 /* Recursive function to switch derived types of all symbol in a
1212 namespace. */
1214 static void
1215 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1217 gfc_symbol *sym;
1219 if (st == NULL)
1220 return;
1222 sym = st->n.sym;
1223 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1224 sym->ts.derived = to;
1226 switch_types (st->left, from, to);
1227 switch_types (st->right, from, to);
1231 /* This subroutine is called when a derived type is used in order to
1232 make the final determination about which version to use. The
1233 standard requires that a type be defined before it is 'used', but
1234 such types can appear in IMPLICIT statements before the actual
1235 definition. 'Using' in this context means declaring a variable to
1236 be that type or using the type constructor.
1238 If a type is used and the components haven't been defined, then we
1239 have to have a derived type in a parent unit. We find the node in
1240 the other namespace and point the symtree node in this namespace to
1241 that node. Further reference to this name point to the correct
1242 node. If we can't find the node in a parent namespace, then we have
1243 an error.
1245 This subroutine takes a pointer to a symbol node and returns a
1246 pointer to the translated node or NULL for an error. Usually there
1247 is no translation and we return the node we were passed. */
1249 gfc_symbol *
1250 gfc_use_derived (gfc_symbol * sym)
1252 gfc_symbol *s, *p;
1253 gfc_typespec *t;
1254 gfc_symtree *st;
1255 int i;
1257 if (sym->components != NULL)
1258 return sym; /* Already defined. */
1260 if (sym->ns->parent == NULL)
1261 goto bad;
1263 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1265 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1266 return NULL;
1269 if (s == NULL || s->attr.flavor != FL_DERIVED)
1270 goto bad;
1272 /* Get rid of symbol sym, translating all references to s. */
1273 for (i = 0; i < GFC_LETTERS; i++)
1275 t = &sym->ns->default_type[i];
1276 if (t->derived == sym)
1277 t->derived = s;
1280 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1281 st->n.sym = s;
1283 s->refs++;
1285 /* Unlink from list of modified symbols. */
1286 if (changed_syms == sym)
1287 changed_syms = sym->tlink;
1288 else
1289 for (p = changed_syms; p; p = p->tlink)
1290 if (p->tlink == sym)
1292 p->tlink = sym->tlink;
1293 break;
1296 switch_types (sym->ns->sym_root, sym, s);
1298 /* TODO: Also have to replace sym -> s in other lists like
1299 namelists, common lists and interface lists. */
1300 gfc_free_symbol (sym);
1302 return s;
1304 bad:
1305 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1306 sym->name);
1307 return NULL;
1311 /* Given a derived type node and a component name, try to locate the
1312 component structure. Returns the NULL pointer if the component is
1313 not found or the components are private. */
1315 gfc_component *
1316 gfc_find_component (gfc_symbol * sym, const char *name)
1318 gfc_component *p;
1320 if (name == NULL)
1321 return NULL;
1323 sym = gfc_use_derived (sym);
1325 if (sym == NULL)
1326 return NULL;
1328 for (p = sym->components; p; p = p->next)
1329 if (strcmp (p->name, name) == 0)
1330 break;
1332 if (p == NULL)
1333 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1334 name, sym->name);
1335 else
1337 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1339 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1340 name, sym->name);
1341 p = NULL;
1345 return p;
1349 /* Given a symbol, free all of the component structures and everything
1350 they point to. */
1352 static void
1353 free_components (gfc_component * p)
1355 gfc_component *q;
1357 for (; p; p = q)
1359 q = p->next;
1361 gfc_free_array_spec (p->as);
1362 gfc_free_expr (p->initializer);
1364 gfc_free (p);
1369 /* Set component attributes from a standard symbol attribute
1370 structure. */
1372 void
1373 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1376 c->dimension = attr->dimension;
1377 c->pointer = attr->pointer;
1381 /* Get a standard symbol attribute structure given the component
1382 structure. */
1384 void
1385 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1388 gfc_clear_attr (attr);
1389 attr->dimension = c->dimension;
1390 attr->pointer = c->pointer;
1394 /******************** Statement label management ********************/
1396 /* Free a single gfc_st_label structure, making sure the list is not
1397 messed up. This function is called only when some parse error
1398 occurs. */
1400 void
1401 gfc_free_st_label (gfc_st_label * l)
1404 if (l == NULL)
1405 return;
1407 if (l->prev)
1408 (l->prev->next = l->next);
1410 if (l->next)
1411 (l->next->prev = l->prev);
1413 if (l->format != NULL)
1414 gfc_free_expr (l->format);
1415 gfc_free (l);
1418 /* Free a whole list of gfc_st_label structures. */
1420 static void
1421 free_st_labels (gfc_st_label * l1)
1423 gfc_st_label *l2;
1425 for (; l1; l1 = l2)
1427 l2 = l1->next;
1428 if (l1->format != NULL)
1429 gfc_free_expr (l1->format);
1430 gfc_free (l1);
1435 /* Given a label number, search for and return a pointer to the label
1436 structure, creating it if it does not exist. */
1438 gfc_st_label *
1439 gfc_get_st_label (int labelno)
1441 gfc_st_label *lp;
1443 /* First see if the label is already in this namespace. */
1444 for (lp = gfc_current_ns->st_labels; lp; lp = lp->next)
1445 if (lp->value == labelno)
1446 break;
1447 if (lp != NULL)
1448 return lp;
1450 lp = gfc_getmem (sizeof (gfc_st_label));
1452 lp->value = labelno;
1453 lp->defined = ST_LABEL_UNKNOWN;
1454 lp->referenced = ST_LABEL_UNKNOWN;
1456 lp->prev = NULL;
1457 lp->next = gfc_current_ns->st_labels;
1458 if (gfc_current_ns->st_labels)
1459 gfc_current_ns->st_labels->prev = lp;
1460 gfc_current_ns->st_labels = lp;
1462 return lp;
1466 /* Called when a statement with a statement label is about to be
1467 accepted. We add the label to the list of the current namespace,
1468 making sure it hasn't been defined previously and referenced
1469 correctly. */
1471 void
1472 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1474 int labelno;
1476 labelno = lp->value;
1478 if (lp->defined != ST_LABEL_UNKNOWN)
1479 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1480 &lp->where, label_locus);
1481 else
1483 lp->where = *label_locus;
1485 switch (type)
1487 case ST_LABEL_FORMAT:
1488 if (lp->referenced == ST_LABEL_TARGET)
1489 gfc_error ("Label %d at %C already referenced as branch target",
1490 labelno);
1491 else
1492 lp->defined = ST_LABEL_FORMAT;
1494 break;
1496 case ST_LABEL_TARGET:
1497 if (lp->referenced == ST_LABEL_FORMAT)
1498 gfc_error ("Label %d at %C already referenced as a format label",
1499 labelno);
1500 else
1501 lp->defined = ST_LABEL_TARGET;
1503 break;
1505 default:
1506 lp->defined = ST_LABEL_BAD_TARGET;
1507 lp->referenced = ST_LABEL_BAD_TARGET;
1513 /* Reference a label. Given a label and its type, see if that
1514 reference is consistent with what is known about that label,
1515 updating the unknown state. Returns FAILURE if something goes
1516 wrong. */
1519 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1521 gfc_sl_type label_type;
1522 int labelno;
1523 try rc;
1525 if (lp == NULL)
1526 return SUCCESS;
1528 labelno = lp->value;
1530 if (lp->defined != ST_LABEL_UNKNOWN)
1531 label_type = lp->defined;
1532 else
1534 label_type = lp->referenced;
1535 lp->where = gfc_current_locus;
1538 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1540 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1541 rc = FAILURE;
1542 goto done;
1545 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1546 && type == ST_LABEL_FORMAT)
1548 gfc_error ("Label %d at %C previously used as branch target", labelno);
1549 rc = FAILURE;
1550 goto done;
1553 lp->referenced = type;
1554 rc = SUCCESS;
1556 done:
1557 return rc;
1561 /************** Symbol table management subroutines ****************/
1563 /* Basic details: Fortran 95 requires a potentially unlimited number
1564 of distinct namespaces when compiling a program unit. This case
1565 occurs during a compilation of internal subprograms because all of
1566 the internal subprograms must be read before we can start
1567 generating code for the host.
1569 Given the tricky nature of the Fortran grammar, we must be able to
1570 undo changes made to a symbol table if the current interpretation
1571 of a statement is found to be incorrect. Whenever a symbol is
1572 looked up, we make a copy of it and link to it. All of these
1573 symbols are kept in a singly linked list so that we can commit or
1574 undo the changes at a later time.
1576 A symtree may point to a symbol node outside of its namespace. In
1577 this case, that symbol has been used as a host associated variable
1578 at some previous time. */
1580 /* Allocate a new namespace structure. Copies the implicit types from
1581 PARENT if PARENT_TYPES is set. */
1583 gfc_namespace *
1584 gfc_get_namespace (gfc_namespace * parent, int parent_types)
1586 gfc_namespace *ns;
1587 gfc_typespec *ts;
1588 gfc_intrinsic_op in;
1589 int i;
1591 ns = gfc_getmem (sizeof (gfc_namespace));
1592 ns->sym_root = NULL;
1593 ns->uop_root = NULL;
1594 ns->default_access = ACCESS_UNKNOWN;
1595 ns->parent = parent;
1597 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1598 ns->operator_access[in] = ACCESS_UNKNOWN;
1600 /* Initialize default implicit types. */
1601 for (i = 'a'; i <= 'z'; i++)
1603 ns->set_flag[i - 'a'] = 0;
1604 ts = &ns->default_type[i - 'a'];
1606 if (parent_types && ns->parent != NULL)
1608 /* Copy parent settings */
1609 *ts = ns->parent->default_type[i - 'a'];
1610 continue;
1613 if (gfc_option.flag_implicit_none != 0)
1615 gfc_clear_ts (ts);
1616 continue;
1619 if ('i' <= i && i <= 'n')
1621 ts->type = BT_INTEGER;
1622 ts->kind = gfc_default_integer_kind;
1624 else
1626 ts->type = BT_REAL;
1627 ts->kind = gfc_default_real_kind;
1631 ns->refs = 1;
1633 return ns;
1637 /* Comparison function for symtree nodes. */
1639 static int
1640 compare_symtree (void * _st1, void * _st2)
1642 gfc_symtree *st1, *st2;
1644 st1 = (gfc_symtree *) _st1;
1645 st2 = (gfc_symtree *) _st2;
1647 return strcmp (st1->name, st2->name);
1651 /* Allocate a new symtree node and associate it with the new symbol. */
1653 gfc_symtree *
1654 gfc_new_symtree (gfc_symtree ** root, const char *name)
1656 gfc_symtree *st;
1658 st = gfc_getmem (sizeof (gfc_symtree));
1659 st->name = gfc_get_string (name);
1661 gfc_insert_bbt (root, st, compare_symtree);
1662 return st;
1666 /* Delete a symbol from the tree. Does not free the symbol itself! */
1668 static void
1669 delete_symtree (gfc_symtree ** root, const char *name)
1671 gfc_symtree st, *st0;
1673 st0 = gfc_find_symtree (*root, name);
1675 st.name = gfc_get_string (name);
1676 gfc_delete_bbt (root, &st, compare_symtree);
1678 gfc_free (st0);
1682 /* Given a root symtree node and a name, try to find the symbol within
1683 the namespace. Returns NULL if the symbol is not found. */
1685 gfc_symtree *
1686 gfc_find_symtree (gfc_symtree * st, const char *name)
1688 int c;
1690 while (st != NULL)
1692 c = strcmp (name, st->name);
1693 if (c == 0)
1694 return st;
1696 st = (c < 0) ? st->left : st->right;
1699 return NULL;
1703 /* Given a name find a user operator node, creating it if it doesn't
1704 exist. These are much simpler than symbols because they can't be
1705 ambiguous with one another. */
1707 gfc_user_op *
1708 gfc_get_uop (const char *name)
1710 gfc_user_op *uop;
1711 gfc_symtree *st;
1713 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1714 if (st != NULL)
1715 return st->n.uop;
1717 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1719 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1720 uop->name = gfc_get_string (name);
1721 uop->access = ACCESS_UNKNOWN;
1722 uop->ns = gfc_current_ns;
1724 return uop;
1728 /* Given a name find the user operator node. Returns NULL if it does
1729 not exist. */
1731 gfc_user_op *
1732 gfc_find_uop (const char *name, gfc_namespace * ns)
1734 gfc_symtree *st;
1736 if (ns == NULL)
1737 ns = gfc_current_ns;
1739 st = gfc_find_symtree (ns->uop_root, name);
1740 return (st == NULL) ? NULL : st->n.uop;
1744 /* Remove a gfc_symbol structure and everything it points to. */
1746 void
1747 gfc_free_symbol (gfc_symbol * sym)
1750 if (sym == NULL)
1751 return;
1753 gfc_free_array_spec (sym->as);
1755 free_components (sym->components);
1757 gfc_free_expr (sym->value);
1759 gfc_free_namelist (sym->namelist);
1761 gfc_free_namespace (sym->formal_ns);
1763 gfc_free_interface (sym->generic);
1765 gfc_free_formal_arglist (sym->formal);
1767 gfc_free (sym);
1771 /* Allocate and initialize a new symbol node. */
1773 gfc_symbol *
1774 gfc_new_symbol (const char *name, gfc_namespace * ns)
1776 gfc_symbol *p;
1778 p = gfc_getmem (sizeof (gfc_symbol));
1780 gfc_clear_ts (&p->ts);
1781 gfc_clear_attr (&p->attr);
1782 p->ns = ns;
1784 p->declared_at = gfc_current_locus;
1786 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1787 gfc_internal_error ("new_symbol(): Symbol name too long");
1789 p->name = gfc_get_string (name);
1790 return p;
1794 /* Generate an error if a symbol is ambiguous. */
1796 static void
1797 ambiguous_symbol (const char *name, gfc_symtree * st)
1800 if (st->n.sym->module)
1801 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1802 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1803 else
1804 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1805 "from current program unit", name, st->n.sym->name);
1809 /* Search for a symtree starting in the current namespace, resorting to
1810 any parent namespaces if requested by a nonzero parent_flag.
1811 Returns nonzero if the name is ambiguous. */
1814 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1815 gfc_symtree ** result)
1817 gfc_symtree *st;
1819 if (ns == NULL)
1820 ns = gfc_current_ns;
1824 st = gfc_find_symtree (ns->sym_root, name);
1825 if (st != NULL)
1827 *result = st;
1828 if (st->ambiguous)
1830 ambiguous_symbol (name, st);
1831 return 1;
1834 return 0;
1837 if (!parent_flag)
1838 break;
1840 ns = ns->parent;
1842 while (ns != NULL);
1844 *result = NULL;
1845 return 0;
1849 /* Same, but returns the symbol instead. */
1852 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
1853 gfc_symbol ** result)
1855 gfc_symtree *st;
1856 int i;
1858 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
1860 if (st == NULL)
1861 *result = NULL;
1862 else
1863 *result = st->n.sym;
1865 return i;
1869 /* Save symbol with the information necessary to back it out. */
1871 static void
1872 save_symbol_data (gfc_symbol * sym)
1875 if (sym->new || sym->old_symbol != NULL)
1876 return;
1878 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
1879 *(sym->old_symbol) = *sym;
1881 sym->tlink = changed_syms;
1882 changed_syms = sym;
1886 /* Given a name, find a symbol, or create it if it does not exist yet
1887 in the current namespace. If the symbol is found we make sure that
1888 it's OK.
1890 The integer return code indicates
1891 0 All OK
1892 1 The symbol name was ambiguous
1893 2 The name meant to be established was already host associated.
1895 So if the return value is nonzero, then an error was issued. */
1898 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
1900 gfc_symtree *st;
1901 gfc_symbol *p;
1903 /* This doesn't usually happen during resolution. */
1904 if (ns == NULL)
1905 ns = gfc_current_ns;
1907 /* Try to find the symbol in ns. */
1908 st = gfc_find_symtree (ns->sym_root, name);
1910 if (st == NULL)
1912 /* If not there, create a new symbol. */
1913 p = gfc_new_symbol (name, ns);
1915 /* Add to the list of tentative symbols. */
1916 p->old_symbol = NULL;
1917 p->tlink = changed_syms;
1918 p->mark = 1;
1919 p->new = 1;
1920 changed_syms = p;
1922 st = gfc_new_symtree (&ns->sym_root, name);
1923 st->n.sym = p;
1924 p->refs++;
1927 else
1929 /* Make sure the existing symbol is OK. */
1930 if (st->ambiguous)
1932 ambiguous_symbol (name, st);
1933 return 1;
1936 p = st->n.sym;
1938 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
1940 /* Symbol is from another namespace. */
1941 gfc_error ("Symbol '%s' at %C has already been host associated",
1942 name);
1943 return 2;
1946 p->mark = 1;
1948 /* Copy in case this symbol is changed. */
1949 save_symbol_data (p);
1952 *result = st;
1953 return 0;
1958 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
1960 gfc_symtree *st;
1961 int i;
1964 i = gfc_get_sym_tree (name, ns, &st);
1965 if (i != 0)
1966 return i;
1968 if (st)
1969 *result = st->n.sym;
1970 else
1971 *result = NULL;
1972 return i;
1976 /* Subroutine that searches for a symbol, creating it if it doesn't
1977 exist, but tries to host-associate the symbol if possible. */
1980 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
1982 gfc_symtree *st;
1983 int i;
1985 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1986 if (st != NULL)
1988 save_symbol_data (st->n.sym);
1990 *result = st;
1991 return i;
1994 if (gfc_current_ns->parent != NULL)
1996 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
1997 if (i)
1998 return i;
2000 if (st != NULL)
2002 *result = st;
2003 return 0;
2007 return gfc_get_sym_tree (name, gfc_current_ns, result);
2012 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
2014 int i;
2015 gfc_symtree *st;
2017 i = gfc_get_ha_sym_tree (name, &st);
2019 if (st)
2020 *result = st->n.sym;
2021 else
2022 *result = NULL;
2024 return i;
2027 /* Return true if both symbols could refer to the same data object. Does
2028 not take account of aliasing due to equivalence statements. */
2031 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
2033 /* Aliasing isn't possible if the symbols have different base types. */
2034 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2035 return 0;
2037 /* Pointers can point to other pointers, target objects and allocatable
2038 objects. Two allocatable objects cannot share the same storage. */
2039 if (lsym->attr.pointer
2040 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2041 return 1;
2042 if (lsym->attr.target && rsym->attr.pointer)
2043 return 1;
2044 if (lsym->attr.allocatable && rsym->attr.pointer)
2045 return 1;
2047 return 0;
2051 /* Undoes all the changes made to symbols in the current statement.
2052 This subroutine is made simpler due to the fact that attributes are
2053 never removed once added. */
2055 void
2056 gfc_undo_symbols (void)
2058 gfc_symbol *p, *q, *old;
2060 for (p = changed_syms; p; p = q)
2062 q = p->tlink;
2064 if (p->new)
2066 /* Symbol was new. */
2067 delete_symtree (&p->ns->sym_root, p->name);
2069 p->refs--;
2070 if (p->refs < 0)
2071 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2072 if (p->refs == 0)
2073 gfc_free_symbol (p);
2074 continue;
2077 /* Restore previous state of symbol. Just copy simple stuff. */
2078 p->mark = 0;
2079 old = p->old_symbol;
2081 p->ts.type = old->ts.type;
2082 p->ts.kind = old->ts.kind;
2084 p->attr = old->attr;
2086 if (p->value != old->value)
2088 gfc_free_expr (old->value);
2089 p->value = NULL;
2092 if (p->as != old->as)
2094 if (p->as)
2095 gfc_free_array_spec (p->as);
2096 p->as = old->as;
2099 p->generic = old->generic;
2100 p->component_access = old->component_access;
2102 if (p->namelist != NULL && old->namelist == NULL)
2104 gfc_free_namelist (p->namelist);
2105 p->namelist = NULL;
2107 else
2110 if (p->namelist_tail != old->namelist_tail)
2112 gfc_free_namelist (old->namelist_tail);
2113 old->namelist_tail->next = NULL;
2117 p->namelist_tail = old->namelist_tail;
2119 if (p->formal != old->formal)
2121 gfc_free_formal_arglist (p->formal);
2122 p->formal = old->formal;
2125 gfc_free (p->old_symbol);
2126 p->old_symbol = NULL;
2127 p->tlink = NULL;
2130 changed_syms = NULL;
2134 /* Makes the changes made in the current statement permanent-- gets
2135 rid of undo information. */
2137 void
2138 gfc_commit_symbols (void)
2140 gfc_symbol *p, *q;
2142 for (p = changed_syms; p; p = q)
2144 q = p->tlink;
2145 p->tlink = NULL;
2146 p->mark = 0;
2147 p->new = 0;
2149 if (p->old_symbol != NULL)
2151 gfc_free (p->old_symbol);
2152 p->old_symbol = NULL;
2156 changed_syms = NULL;
2160 /* Recursive function that deletes an entire tree and all the common
2161 head structures it points to. */
2163 static void
2164 free_common_tree (gfc_symtree * common_tree)
2166 if (common_tree == NULL)
2167 return;
2169 free_common_tree (common_tree->left);
2170 free_common_tree (common_tree->right);
2172 gfc_free (common_tree);
2176 /* Recursive function that deletes an entire tree and all the user
2177 operator nodes that it contains. */
2179 static void
2180 free_uop_tree (gfc_symtree * uop_tree)
2183 if (uop_tree == NULL)
2184 return;
2186 free_uop_tree (uop_tree->left);
2187 free_uop_tree (uop_tree->right);
2189 gfc_free_interface (uop_tree->n.uop->operator);
2191 gfc_free (uop_tree->n.uop);
2192 gfc_free (uop_tree);
2196 /* Recursive function that deletes an entire tree and all the symbols
2197 that it contains. */
2199 static void
2200 free_sym_tree (gfc_symtree * sym_tree)
2202 gfc_namespace *ns;
2203 gfc_symbol *sym;
2205 if (sym_tree == NULL)
2206 return;
2208 free_sym_tree (sym_tree->left);
2209 free_sym_tree (sym_tree->right);
2211 sym = sym_tree->n.sym;
2213 sym->refs--;
2214 if (sym->refs < 0)
2215 gfc_internal_error ("free_sym_tree(): Negative refs");
2217 if (sym->formal_ns != NULL && sym->refs == 1)
2219 /* As formal_ns contains a reference to sym, delete formal_ns just
2220 before the deletion of sym. */
2221 ns = sym->formal_ns;
2222 sym->formal_ns = NULL;
2223 gfc_free_namespace (ns);
2225 else if (sym->refs == 0)
2227 /* Go ahead and delete the symbol. */
2228 gfc_free_symbol (sym);
2231 gfc_free (sym_tree);
2235 /* Free a namespace structure and everything below it. Interface
2236 lists associated with intrinsic operators are not freed. These are
2237 taken care of when a specific name is freed. */
2239 void
2240 gfc_free_namespace (gfc_namespace * ns)
2242 gfc_charlen *cl, *cl2;
2243 gfc_namespace *p, *q;
2244 gfc_intrinsic_op i;
2246 if (ns == NULL)
2247 return;
2249 ns->refs--;
2250 if (ns->refs > 0)
2251 return;
2252 gcc_assert (ns->refs == 0);
2254 gfc_free_statements (ns->code);
2256 free_sym_tree (ns->sym_root);
2257 free_uop_tree (ns->uop_root);
2258 free_common_tree (ns->common_root);
2260 for (cl = ns->cl_list; cl; cl = cl2)
2262 cl2 = cl->next;
2263 gfc_free_expr (cl->length);
2264 gfc_free (cl);
2267 free_st_labels (ns->st_labels);
2269 gfc_free_equiv (ns->equiv);
2271 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2272 gfc_free_interface (ns->operator[i]);
2274 gfc_free_data (ns->data);
2275 p = ns->contained;
2276 gfc_free (ns);
2278 /* Recursively free any contained namespaces. */
2279 while (p != NULL)
2281 q = p;
2282 p = p->sibling;
2284 gfc_free_namespace (q);
2289 void
2290 gfc_symbol_init_2 (void)
2293 gfc_current_ns = gfc_get_namespace (NULL, 0);
2297 void
2298 gfc_symbol_done_2 (void)
2301 gfc_free_namespace (gfc_current_ns);
2302 gfc_current_ns = NULL;
2306 /* Clear mark bits from symbol nodes associated with a symtree node. */
2308 static void
2309 clear_sym_mark (gfc_symtree * st)
2312 st->n.sym->mark = 0;
2316 /* Recursively traverse the symtree nodes. */
2318 void
2319 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2321 if (st != NULL)
2323 (*func) (st);
2325 gfc_traverse_symtree (st->left, func);
2326 gfc_traverse_symtree (st->right, func);
2331 /* Recursive namespace traversal function. */
2333 static void
2334 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2337 if (st == NULL)
2338 return;
2340 if (st->n.sym->mark == 0)
2341 (*func) (st->n.sym);
2342 st->n.sym->mark = 1;
2344 traverse_ns (st->left, func);
2345 traverse_ns (st->right, func);
2349 /* Call a given function for all symbols in the namespace. We take
2350 care that each gfc_symbol node is called exactly once. */
2352 void
2353 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2356 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2358 traverse_ns (ns->sym_root, func);
2362 /* Return TRUE if the symbol is an automatic variable. */
2363 static bool
2364 gfc_is_var_automatic (gfc_symbol * sym)
2366 /* Pointer and allocatable variables are never automatic. */
2367 if (sym->attr.pointer || sym->attr.allocatable)
2368 return false;
2369 /* Check for arrays with non-constant size. */
2370 if (sym->attr.dimension && sym->as
2371 && !gfc_is_compile_time_shape (sym->as))
2372 return true;
2373 /* Check for non-constant length character variables. */
2374 if (sym->ts.type == BT_CHARACTER
2375 && sym->ts.cl
2376 && !gfc_is_constant_expr (sym->ts.cl->length))
2377 return true;
2378 return false;
2381 /* Given a symbol, mark it as SAVEd if it is allowed. */
2383 static void
2384 save_symbol (gfc_symbol * sym)
2387 if (sym->attr.use_assoc)
2388 return;
2390 if (sym->attr.in_common
2391 || sym->attr.dummy
2392 || sym->attr.flavor != FL_VARIABLE)
2393 return;
2394 /* Automatic objects are not saved. */
2395 if (gfc_is_var_automatic (sym))
2396 return;
2397 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2401 /* Mark those symbols which can be SAVEd as such. */
2403 void
2404 gfc_save_all (gfc_namespace * ns)
2407 gfc_traverse_ns (ns, save_symbol);
2411 #ifdef GFC_DEBUG
2412 /* Make sure that no changes to symbols are pending. */
2414 void
2415 gfc_symbol_state(void) {
2417 if (changed_syms != NULL)
2418 gfc_internal_error("Symbol changes still pending!");
2420 #endif
2423 /************** Global symbol handling ************/
2426 /* Search a tree for the global symbol. */
2428 gfc_gsymbol *
2429 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2431 gfc_gsymbol *s;
2433 if (symbol == NULL)
2434 return NULL;
2435 if (strcmp (symbol->name, name) == 0)
2436 return symbol;
2438 s = gfc_find_gsymbol (symbol->left, name);
2439 if (s != NULL)
2440 return s;
2442 s = gfc_find_gsymbol (symbol->right, name);
2443 if (s != NULL)
2444 return s;
2446 return NULL;
2450 /* Compare two global symbols. Used for managing the BB tree. */
2452 static int
2453 gsym_compare (void * _s1, void * _s2)
2455 gfc_gsymbol *s1, *s2;
2457 s1 = (gfc_gsymbol *)_s1;
2458 s2 = (gfc_gsymbol *)_s2;
2459 return strcmp(s1->name, s2->name);
2463 /* Get a global symbol, creating it if it doesn't exist. */
2465 gfc_gsymbol *
2466 gfc_get_gsymbol (const char *name)
2468 gfc_gsymbol *s;
2470 s = gfc_find_gsymbol (gfc_gsym_root, name);
2471 if (s != NULL)
2472 return s;
2474 s = gfc_getmem (sizeof (gfc_gsymbol));
2475 s->type = GSYM_UNKNOWN;
2476 s->name = gfc_get_string (name);
2478 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2480 return s;