*sigh* checked in the wrong patch
[official-gcc.git] / gcc / fortran / symbol.c
blob43209e4ccaea5eeb4358bc60600ed8804a5c6604
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", *cray_pointer = "CRAY POINTER",
267 *cray_pointee = "CRAY POINTEE";
269 const char *a1, *a2;
271 if (where == NULL)
272 where = &gfc_current_locus;
274 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
276 a1 = pointer;
277 a2 = intent;
278 goto conflict;
281 /* Check for attributes not allowed in a BLOCK DATA. */
282 if (gfc_current_state () == COMP_BLOCK_DATA)
284 a1 = NULL;
286 if (attr->in_namelist)
287 a1 = in_namelist;
288 if (attr->allocatable)
289 a1 = allocatable;
290 if (attr->external)
291 a1 = external;
292 if (attr->optional)
293 a1 = optional;
294 if (attr->access == ACCESS_PRIVATE)
295 a1 = private;
296 if (attr->access == ACCESS_PUBLIC)
297 a1 = public;
298 if (attr->intent != INTENT_UNKNOWN)
299 a1 = intent;
301 if (a1 != NULL)
303 gfc_error
304 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
305 where);
306 return FAILURE;
310 conf (dummy, save);
311 conf (pointer, target);
312 conf (pointer, external);
313 conf (pointer, intrinsic);
314 conf (target, external);
315 conf (target, intrinsic);
316 conf (external, dimension); /* See Fortran 95's R504. */
318 conf (external, intrinsic);
319 conf (allocatable, pointer);
320 conf (allocatable, dummy); /* TODO: Allowed in Fortran 200x. */
321 conf (allocatable, function); /* TODO: Allowed in Fortran 200x. */
322 conf (allocatable, result); /* TODO: Allowed in Fortran 200x. */
323 conf (elemental, recursive);
325 conf (in_common, dummy);
326 conf (in_common, allocatable);
327 conf (in_common, result);
328 conf (in_common, save);
329 conf (result, save);
331 conf (dummy, result);
333 conf (in_equivalence, use_assoc);
334 conf (in_equivalence, dummy);
335 conf (in_equivalence, target);
336 conf (in_equivalence, pointer);
337 conf (in_equivalence, function);
338 conf (in_equivalence, result);
339 conf (in_equivalence, entry);
340 conf (in_equivalence, allocatable);
342 conf (in_namelist, pointer);
343 conf (in_namelist, allocatable);
345 conf (entry, result);
347 conf (function, subroutine);
349 /* Cray pointer/pointee conflicts. */
350 conf (cray_pointer, cray_pointee);
351 conf (cray_pointer, dimension);
352 conf (cray_pointer, pointer);
353 conf (cray_pointer, target);
354 conf (cray_pointer, allocatable);
355 conf (cray_pointer, external);
356 conf (cray_pointer, intrinsic);
357 conf (cray_pointer, in_namelist);
358 conf (cray_pointer, function);
359 conf (cray_pointer, subroutine);
360 conf (cray_pointer, entry);
362 conf (cray_pointee, allocatable);
363 conf (cray_pointee, intent);
364 conf (cray_pointee, optional);
365 conf (cray_pointee, dummy);
366 conf (cray_pointee, target);
367 conf (cray_pointee, external);
368 conf (cray_pointee, intrinsic);
369 conf (cray_pointee, pointer);
370 conf (cray_pointee, function);
371 conf (cray_pointee, subroutine);
372 conf (cray_pointee, entry);
373 conf (cray_pointee, in_common);
374 conf (cray_pointee, in_equivalence);
376 a1 = gfc_code2string (flavors, attr->flavor);
378 if (attr->in_namelist
379 && attr->flavor != FL_VARIABLE
380 && attr->flavor != FL_UNKNOWN)
383 a2 = in_namelist;
384 goto conflict;
387 switch (attr->flavor)
389 case FL_PROGRAM:
390 case FL_BLOCK_DATA:
391 case FL_MODULE:
392 case FL_LABEL:
393 conf2 (dummy);
394 conf2 (save);
395 conf2 (pointer);
396 conf2 (target);
397 conf2 (external);
398 conf2 (intrinsic);
399 conf2 (allocatable);
400 conf2 (result);
401 conf2 (in_namelist);
402 conf2 (optional);
403 conf2 (function);
404 conf2 (subroutine);
405 break;
407 case FL_VARIABLE:
408 case FL_NAMELIST:
409 break;
411 case FL_PROCEDURE:
412 conf2 (intent);
414 if (attr->subroutine)
416 conf2(save);
417 conf2(pointer);
418 conf2(target);
419 conf2(allocatable);
420 conf2(result);
421 conf2(in_namelist);
422 conf2(function);
425 switch (attr->proc)
427 case PROC_ST_FUNCTION:
428 conf2 (in_common);
429 conf2 (dummy);
430 break;
432 case PROC_MODULE:
433 conf2 (dummy);
434 break;
436 case PROC_DUMMY:
437 conf2 (result);
438 conf2 (in_common);
439 conf2 (save);
440 break;
442 default:
443 break;
446 break;
448 case FL_DERIVED:
449 conf2 (dummy);
450 conf2 (save);
451 conf2 (pointer);
452 conf2 (target);
453 conf2 (external);
454 conf2 (intrinsic);
455 conf2 (allocatable);
456 conf2 (optional);
457 conf2 (entry);
458 conf2 (function);
459 conf2 (subroutine);
461 if (attr->intent != INTENT_UNKNOWN)
463 a2 = intent;
464 goto conflict;
466 break;
468 case FL_PARAMETER:
469 conf2 (external);
470 conf2 (intrinsic);
471 conf2 (optional);
472 conf2 (allocatable);
473 conf2 (function);
474 conf2 (subroutine);
475 conf2 (entry);
476 conf2 (pointer);
477 conf2 (target);
478 conf2 (dummy);
479 conf2 (in_common);
480 conf2 (save);
481 break;
483 default:
484 break;
487 return SUCCESS;
489 conflict:
490 if (name == NULL)
491 gfc_error ("%s attribute conflicts with %s attribute at %L",
492 a1, a2, where);
493 else
494 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
495 a1, a2, name, where);
497 return FAILURE;
500 #undef conf
501 #undef conf2
504 /* Mark a symbol as referenced. */
506 void
507 gfc_set_sym_referenced (gfc_symbol * sym)
509 if (sym->attr.referenced)
510 return;
512 sym->attr.referenced = 1;
514 /* Remember which order dummy variables are accessed in. */
515 if (sym->attr.dummy)
516 sym->dummy_order = next_dummy_order++;
520 /* Common subroutine called by attribute changing subroutines in order
521 to prevent them from changing a symbol that has been
522 use-associated. Returns zero if it is OK to change the symbol,
523 nonzero if not. */
525 static int
526 check_used (symbol_attribute * attr, const char * name, locus * where)
529 if (attr->use_assoc == 0)
530 return 0;
532 if (where == NULL)
533 where = &gfc_current_locus;
535 if (name == NULL)
536 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
537 where);
538 else
539 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
540 name, where);
542 return 1;
546 /* Used to prevent changing the attributes of a symbol after it has been
547 used. This check is only done for dummy variables as only these can be
548 used in specification expressions. Applying this to all symbols causes
549 an error when we reach the body of a contained function. */
551 static int
552 check_done (symbol_attribute * attr, locus * where)
555 if (!(attr->dummy && attr->referenced))
556 return 0;
558 if (where == NULL)
559 where = &gfc_current_locus;
561 gfc_error ("Cannot change attributes of symbol at %L"
562 " after it has been used", where);
564 return 1;
568 /* Generate an error because of a duplicate attribute. */
570 static void
571 duplicate_attr (const char *attr, locus * where)
574 if (where == NULL)
575 where = &gfc_current_locus;
577 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
582 gfc_add_allocatable (symbol_attribute * attr, locus * where)
585 if (check_used (attr, NULL, where) || check_done (attr, where))
586 return FAILURE;
588 if (attr->allocatable)
590 duplicate_attr ("ALLOCATABLE", where);
591 return FAILURE;
594 attr->allocatable = 1;
595 return check_conflict (attr, NULL, where);
600 gfc_add_dimension (symbol_attribute * attr, const char *name, locus * where)
603 if (check_used (attr, name, where) || check_done (attr, where))
604 return FAILURE;
606 if (attr->dimension)
608 duplicate_attr ("DIMENSION", where);
609 return FAILURE;
612 attr->dimension = 1;
613 return check_conflict (attr, name, where);
618 gfc_add_external (symbol_attribute * attr, locus * where)
621 if (check_used (attr, NULL, where) || check_done (attr, where))
622 return FAILURE;
624 if (attr->external)
626 duplicate_attr ("EXTERNAL", where);
627 return FAILURE;
630 attr->external = 1;
632 return check_conflict (attr, NULL, where);
637 gfc_add_intrinsic (symbol_attribute * attr, locus * where)
640 if (check_used (attr, NULL, where) || check_done (attr, where))
641 return FAILURE;
643 if (attr->intrinsic)
645 duplicate_attr ("INTRINSIC", where);
646 return FAILURE;
649 attr->intrinsic = 1;
651 return check_conflict (attr, NULL, where);
656 gfc_add_optional (symbol_attribute * attr, locus * where)
659 if (check_used (attr, NULL, where) || check_done (attr, where))
660 return FAILURE;
662 if (attr->optional)
664 duplicate_attr ("OPTIONAL", where);
665 return FAILURE;
668 attr->optional = 1;
669 return check_conflict (attr, NULL, where);
674 gfc_add_pointer (symbol_attribute * attr, locus * where)
677 if (check_used (attr, NULL, where) || check_done (attr, where))
678 return FAILURE;
680 attr->pointer = 1;
681 return check_conflict (attr, NULL, where);
686 gfc_add_cray_pointer (symbol_attribute * attr, locus * where)
689 if (check_used (attr, NULL, where) || check_done (attr, where))
690 return FAILURE;
692 attr->cray_pointer = 1;
693 return check_conflict (attr, NULL, where);
698 gfc_add_cray_pointee (symbol_attribute * attr, locus * where)
701 if (check_used (attr, NULL, where) || check_done (attr, where))
702 return FAILURE;
704 if (attr->cray_pointee)
706 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
707 " statements.", where);
708 return FAILURE;
711 attr->cray_pointee = 1;
712 return check_conflict (attr, NULL, where);
717 gfc_add_result (symbol_attribute * attr, const char *name, locus * where)
720 if (check_used (attr, name, where) || check_done (attr, where))
721 return FAILURE;
723 attr->result = 1;
724 return check_conflict (attr, name, where);
729 gfc_add_save (symbol_attribute * attr, const char *name, locus * where)
732 if (check_used (attr, name, where))
733 return FAILURE;
735 if (gfc_pure (NULL))
737 gfc_error
738 ("SAVE attribute at %L cannot be specified in a PURE procedure",
739 where);
740 return FAILURE;
743 if (attr->save)
745 if (gfc_notify_std (GFC_STD_LEGACY,
746 "Duplicate SAVE attribute specified at %L",
747 where)
748 == FAILURE)
749 return FAILURE;
752 attr->save = 1;
753 return check_conflict (attr, name, where);
758 gfc_add_target (symbol_attribute * attr, locus * where)
761 if (check_used (attr, NULL, where) || check_done (attr, where))
762 return FAILURE;
764 if (attr->target)
766 duplicate_attr ("TARGET", where);
767 return FAILURE;
770 attr->target = 1;
771 return check_conflict (attr, NULL, where);
776 gfc_add_dummy (symbol_attribute * attr, const char *name, locus * where)
779 if (check_used (attr, name, where))
780 return FAILURE;
782 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
783 attr->dummy = 1;
784 return check_conflict (attr, name, where);
789 gfc_add_in_common (symbol_attribute * attr, const char *name, locus * where)
792 if (check_used (attr, name, where) || check_done (attr, where))
793 return FAILURE;
795 /* Duplicate attribute already checked for. */
796 attr->in_common = 1;
797 if (check_conflict (attr, name, where) == FAILURE)
798 return FAILURE;
800 if (attr->flavor == FL_VARIABLE)
801 return SUCCESS;
803 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
807 gfc_add_in_equivalence (symbol_attribute * attr, const char *name, locus * where)
810 /* Duplicate attribute already checked for. */
811 attr->in_equivalence = 1;
812 if (check_conflict (attr, name, where) == FAILURE)
813 return FAILURE;
815 if (attr->flavor == FL_VARIABLE)
816 return SUCCESS;
818 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
823 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
826 if (check_used (attr, name, where))
827 return FAILURE;
829 attr->data = 1;
830 return check_conflict (attr, name, where);
835 gfc_add_in_namelist (symbol_attribute * attr, const char *name,
836 locus * where)
839 attr->in_namelist = 1;
840 return check_conflict (attr, name, where);
845 gfc_add_sequence (symbol_attribute * attr, const char *name, locus * where)
848 if (check_used (attr, name, where))
849 return FAILURE;
851 attr->sequence = 1;
852 return check_conflict (attr, name, where);
857 gfc_add_elemental (symbol_attribute * attr, locus * where)
860 if (check_used (attr, NULL, where) || check_done (attr, where))
861 return FAILURE;
863 attr->elemental = 1;
864 return check_conflict (attr, NULL, where);
869 gfc_add_pure (symbol_attribute * attr, locus * where)
872 if (check_used (attr, NULL, where) || check_done (attr, where))
873 return FAILURE;
875 attr->pure = 1;
876 return check_conflict (attr, NULL, where);
881 gfc_add_recursive (symbol_attribute * attr, locus * where)
884 if (check_used (attr, NULL, where) || check_done (attr, where))
885 return FAILURE;
887 attr->recursive = 1;
888 return check_conflict (attr, NULL, where);
893 gfc_add_entry (symbol_attribute * attr, const char *name, locus * where)
896 if (check_used (attr, name, where))
897 return FAILURE;
899 if (attr->entry)
901 duplicate_attr ("ENTRY", where);
902 return FAILURE;
905 attr->entry = 1;
906 return check_conflict (attr, name, where);
911 gfc_add_function (symbol_attribute * attr, const char *name, locus * where)
914 if (attr->flavor != FL_PROCEDURE
915 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
916 return FAILURE;
918 attr->function = 1;
919 return check_conflict (attr, name, where);
924 gfc_add_subroutine (symbol_attribute * attr, const char *name, locus * where)
927 if (attr->flavor != FL_PROCEDURE
928 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
929 return FAILURE;
931 attr->subroutine = 1;
932 return check_conflict (attr, name, where);
937 gfc_add_generic (symbol_attribute * attr, const char *name, locus * where)
940 if (attr->flavor != FL_PROCEDURE
941 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
942 return FAILURE;
944 attr->generic = 1;
945 return check_conflict (attr, name, where);
949 /* Flavors are special because some flavors are not what Fortran
950 considers attributes and can be reaffirmed multiple times. */
953 gfc_add_flavor (symbol_attribute * attr, sym_flavor f, const char *name,
954 locus * where)
957 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
958 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
959 || f == FL_NAMELIST) && check_used (attr, name, where))
960 return FAILURE;
962 if (attr->flavor == f && f == FL_VARIABLE)
963 return SUCCESS;
965 if (attr->flavor != FL_UNKNOWN)
967 if (where == NULL)
968 where = &gfc_current_locus;
970 gfc_error ("%s attribute conflicts with %s attribute at %L",
971 gfc_code2string (flavors, attr->flavor),
972 gfc_code2string (flavors, f), where);
974 return FAILURE;
977 attr->flavor = f;
979 return check_conflict (attr, name, where);
984 gfc_add_procedure (symbol_attribute * attr, procedure_type t,
985 const char *name, locus * where)
988 if (check_used (attr, name, where) || check_done (attr, where))
989 return FAILURE;
991 if (attr->flavor != FL_PROCEDURE
992 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
993 return FAILURE;
995 if (where == NULL)
996 where = &gfc_current_locus;
998 if (attr->proc != PROC_UNKNOWN)
1000 gfc_error ("%s procedure at %L is already declared as %s procedure",
1001 gfc_code2string (procedures, t), where,
1002 gfc_code2string (procedures, attr->proc));
1004 return FAILURE;
1007 attr->proc = t;
1009 /* Statement functions are always scalar and functions. */
1010 if (t == PROC_ST_FUNCTION
1011 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1012 || attr->dimension))
1013 return FAILURE;
1015 return check_conflict (attr, name, where);
1020 gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
1023 if (check_used (attr, NULL, where))
1024 return FAILURE;
1026 if (attr->intent == INTENT_UNKNOWN)
1028 attr->intent = intent;
1029 return check_conflict (attr, NULL, where);
1032 if (where == NULL)
1033 where = &gfc_current_locus;
1035 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1036 gfc_intent_string (attr->intent),
1037 gfc_intent_string (intent), where);
1039 return FAILURE;
1043 /* No checks for use-association in public and private statements. */
1046 gfc_add_access (symbol_attribute * attr, gfc_access access,
1047 const char *name, locus * where)
1050 if (attr->access == ACCESS_UNKNOWN)
1052 attr->access = access;
1053 return check_conflict (attr, name, where);
1056 if (where == NULL)
1057 where = &gfc_current_locus;
1058 gfc_error ("ACCESS specification at %L was already specified", where);
1060 return FAILURE;
1065 gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
1066 gfc_formal_arglist * formal, locus * where)
1069 if (check_used (&sym->attr, sym->name, where))
1070 return FAILURE;
1072 if (where == NULL)
1073 where = &gfc_current_locus;
1075 if (sym->attr.if_source != IFSRC_UNKNOWN
1076 && sym->attr.if_source != IFSRC_DECL)
1078 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1079 sym->name, where);
1080 return FAILURE;
1083 sym->formal = formal;
1084 sym->attr.if_source = source;
1086 return SUCCESS;
1090 /* Add a type to a symbol. */
1093 gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
1095 sym_flavor flavor;
1097 /* TODO: This is legal if it is reaffirming an implicit type.
1098 if (check_done (&sym->attr, where))
1099 return FAILURE;*/
1101 if (where == NULL)
1102 where = &gfc_current_locus;
1104 if (sym->ts.type != BT_UNKNOWN)
1106 gfc_error ("Symbol '%s' at %L already has basic type of %s", sym->name,
1107 where, gfc_basic_typename (sym->ts.type));
1108 return FAILURE;
1111 flavor = sym->attr.flavor;
1113 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1114 || flavor == FL_LABEL || (flavor == FL_PROCEDURE
1115 && sym->attr.subroutine)
1116 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1118 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1119 return FAILURE;
1122 sym->ts = *ts;
1123 return SUCCESS;
1127 /* Clears all attributes. */
1129 void
1130 gfc_clear_attr (symbol_attribute * attr)
1132 memset (attr, 0, sizeof(symbol_attribute));
1136 /* Check for missing attributes in the new symbol. Currently does
1137 nothing, but it's not clear that it is unnecessary yet. */
1140 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1141 locus * where ATTRIBUTE_UNUSED)
1144 return SUCCESS;
1148 /* Copy an attribute to a symbol attribute, bit by bit. Some
1149 attributes have a lot of side-effects but cannot be present given
1150 where we are called from, so we ignore some bits. */
1153 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1156 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1157 goto fail;
1159 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1160 goto fail;
1161 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1162 goto fail;
1163 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1164 goto fail;
1165 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1166 goto fail;
1167 if (src->target && gfc_add_target (dest, where) == FAILURE)
1168 goto fail;
1169 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1170 goto fail;
1171 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1172 goto fail;
1173 if (src->entry)
1174 dest->entry = 1;
1176 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1177 goto fail;
1179 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1180 goto fail;
1182 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1183 goto fail;
1184 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1185 goto fail;
1186 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1187 goto fail;
1189 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1190 goto fail;
1191 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1192 goto fail;
1193 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1194 goto fail;
1195 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1196 goto fail;
1198 if (src->flavor != FL_UNKNOWN
1199 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1200 goto fail;
1202 if (src->intent != INTENT_UNKNOWN
1203 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1204 goto fail;
1206 if (src->access != ACCESS_UNKNOWN
1207 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1208 goto fail;
1210 if (gfc_missing_attr (dest, where) == FAILURE)
1211 goto fail;
1213 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1214 goto fail;
1215 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1216 goto fail;
1218 /* The subroutines that set these bits also cause flavors to be set,
1219 and that has already happened in the original, so don't let it
1220 happen again. */
1221 if (src->external)
1222 dest->external = 1;
1223 if (src->intrinsic)
1224 dest->intrinsic = 1;
1226 return SUCCESS;
1228 fail:
1229 return FAILURE;
1233 /************** Component name management ************/
1235 /* Component names of a derived type form their own little namespaces
1236 that are separate from all other spaces. The space is composed of
1237 a singly linked list of gfc_component structures whose head is
1238 located in the parent symbol. */
1241 /* Add a component name to a symbol. The call fails if the name is
1242 already present. On success, the component pointer is modified to
1243 point to the additional component structure. */
1246 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1248 gfc_component *p, *tail;
1250 tail = NULL;
1252 for (p = sym->components; p; p = p->next)
1254 if (strcmp (p->name, name) == 0)
1256 gfc_error ("Component '%s' at %C already declared at %L",
1257 name, &p->loc);
1258 return FAILURE;
1261 tail = p;
1264 /* Allocate a new component. */
1265 p = gfc_get_component ();
1267 if (tail == NULL)
1268 sym->components = p;
1269 else
1270 tail->next = p;
1272 p->name = gfc_get_string (name);
1273 p->loc = gfc_current_locus;
1275 *component = p;
1276 return SUCCESS;
1280 /* Recursive function to switch derived types of all symbol in a
1281 namespace. */
1283 static void
1284 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1286 gfc_symbol *sym;
1288 if (st == NULL)
1289 return;
1291 sym = st->n.sym;
1292 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1293 sym->ts.derived = to;
1295 switch_types (st->left, from, to);
1296 switch_types (st->right, from, to);
1300 /* This subroutine is called when a derived type is used in order to
1301 make the final determination about which version to use. The
1302 standard requires that a type be defined before it is 'used', but
1303 such types can appear in IMPLICIT statements before the actual
1304 definition. 'Using' in this context means declaring a variable to
1305 be that type or using the type constructor.
1307 If a type is used and the components haven't been defined, then we
1308 have to have a derived type in a parent unit. We find the node in
1309 the other namespace and point the symtree node in this namespace to
1310 that node. Further reference to this name point to the correct
1311 node. If we can't find the node in a parent namespace, then we have
1312 an error.
1314 This subroutine takes a pointer to a symbol node and returns a
1315 pointer to the translated node or NULL for an error. Usually there
1316 is no translation and we return the node we were passed. */
1318 gfc_symbol *
1319 gfc_use_derived (gfc_symbol * sym)
1321 gfc_symbol *s, *p;
1322 gfc_typespec *t;
1323 gfc_symtree *st;
1324 int i;
1326 if (sym->components != NULL)
1327 return sym; /* Already defined. */
1329 if (sym->ns->parent == NULL)
1330 goto bad;
1332 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1334 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1335 return NULL;
1338 if (s == NULL || s->attr.flavor != FL_DERIVED)
1339 goto bad;
1341 /* Get rid of symbol sym, translating all references to s. */
1342 for (i = 0; i < GFC_LETTERS; i++)
1344 t = &sym->ns->default_type[i];
1345 if (t->derived == sym)
1346 t->derived = s;
1349 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1350 st->n.sym = s;
1352 s->refs++;
1354 /* Unlink from list of modified symbols. */
1355 if (changed_syms == sym)
1356 changed_syms = sym->tlink;
1357 else
1358 for (p = changed_syms; p; p = p->tlink)
1359 if (p->tlink == sym)
1361 p->tlink = sym->tlink;
1362 break;
1365 switch_types (sym->ns->sym_root, sym, s);
1367 /* TODO: Also have to replace sym -> s in other lists like
1368 namelists, common lists and interface lists. */
1369 gfc_free_symbol (sym);
1371 return s;
1373 bad:
1374 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1375 sym->name);
1376 return NULL;
1380 /* Given a derived type node and a component name, try to locate the
1381 component structure. Returns the NULL pointer if the component is
1382 not found or the components are private. */
1384 gfc_component *
1385 gfc_find_component (gfc_symbol * sym, const char *name)
1387 gfc_component *p;
1389 if (name == NULL)
1390 return NULL;
1392 sym = gfc_use_derived (sym);
1394 if (sym == NULL)
1395 return NULL;
1397 for (p = sym->components; p; p = p->next)
1398 if (strcmp (p->name, name) == 0)
1399 break;
1401 if (p == NULL)
1402 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1403 name, sym->name);
1404 else
1406 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1408 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1409 name, sym->name);
1410 p = NULL;
1414 return p;
1418 /* Given a symbol, free all of the component structures and everything
1419 they point to. */
1421 static void
1422 free_components (gfc_component * p)
1424 gfc_component *q;
1426 for (; p; p = q)
1428 q = p->next;
1430 gfc_free_array_spec (p->as);
1431 gfc_free_expr (p->initializer);
1433 gfc_free (p);
1438 /* Set component attributes from a standard symbol attribute
1439 structure. */
1441 void
1442 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1445 c->dimension = attr->dimension;
1446 c->pointer = attr->pointer;
1450 /* Get a standard symbol attribute structure given the component
1451 structure. */
1453 void
1454 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1457 gfc_clear_attr (attr);
1458 attr->dimension = c->dimension;
1459 attr->pointer = c->pointer;
1463 /******************** Statement label management ********************/
1465 /* Free a single gfc_st_label structure, making sure the list is not
1466 messed up. This function is called only when some parse error
1467 occurs. */
1469 void
1470 gfc_free_st_label (gfc_st_label * l)
1473 if (l == NULL)
1474 return;
1476 if (l->prev)
1477 (l->prev->next = l->next);
1479 if (l->next)
1480 (l->next->prev = l->prev);
1482 if (l->format != NULL)
1483 gfc_free_expr (l->format);
1484 gfc_free (l);
1487 /* Free a whole list of gfc_st_label structures. */
1489 static void
1490 free_st_labels (gfc_st_label * l1)
1492 gfc_st_label *l2;
1494 for (; l1; l1 = l2)
1496 l2 = l1->next;
1497 if (l1->format != NULL)
1498 gfc_free_expr (l1->format);
1499 gfc_free (l1);
1504 /* Given a label number, search for and return a pointer to the label
1505 structure, creating it if it does not exist. */
1507 gfc_st_label *
1508 gfc_get_st_label (int labelno)
1510 gfc_st_label *lp;
1512 /* First see if the label is already in this namespace. */
1513 for (lp = gfc_current_ns->st_labels; lp; lp = lp->next)
1514 if (lp->value == labelno)
1515 break;
1516 if (lp != NULL)
1517 return lp;
1519 lp = gfc_getmem (sizeof (gfc_st_label));
1521 lp->value = labelno;
1522 lp->defined = ST_LABEL_UNKNOWN;
1523 lp->referenced = ST_LABEL_UNKNOWN;
1525 lp->prev = NULL;
1526 lp->next = gfc_current_ns->st_labels;
1527 if (gfc_current_ns->st_labels)
1528 gfc_current_ns->st_labels->prev = lp;
1529 gfc_current_ns->st_labels = lp;
1531 return lp;
1535 /* Called when a statement with a statement label is about to be
1536 accepted. We add the label to the list of the current namespace,
1537 making sure it hasn't been defined previously and referenced
1538 correctly. */
1540 void
1541 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1543 int labelno;
1545 labelno = lp->value;
1547 if (lp->defined != ST_LABEL_UNKNOWN)
1548 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1549 &lp->where, label_locus);
1550 else
1552 lp->where = *label_locus;
1554 switch (type)
1556 case ST_LABEL_FORMAT:
1557 if (lp->referenced == ST_LABEL_TARGET)
1558 gfc_error ("Label %d at %C already referenced as branch target",
1559 labelno);
1560 else
1561 lp->defined = ST_LABEL_FORMAT;
1563 break;
1565 case ST_LABEL_TARGET:
1566 if (lp->referenced == ST_LABEL_FORMAT)
1567 gfc_error ("Label %d at %C already referenced as a format label",
1568 labelno);
1569 else
1570 lp->defined = ST_LABEL_TARGET;
1572 break;
1574 default:
1575 lp->defined = ST_LABEL_BAD_TARGET;
1576 lp->referenced = ST_LABEL_BAD_TARGET;
1582 /* Reference a label. Given a label and its type, see if that
1583 reference is consistent with what is known about that label,
1584 updating the unknown state. Returns FAILURE if something goes
1585 wrong. */
1588 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1590 gfc_sl_type label_type;
1591 int labelno;
1592 try rc;
1594 if (lp == NULL)
1595 return SUCCESS;
1597 labelno = lp->value;
1599 if (lp->defined != ST_LABEL_UNKNOWN)
1600 label_type = lp->defined;
1601 else
1603 label_type = lp->referenced;
1604 lp->where = gfc_current_locus;
1607 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1609 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1610 rc = FAILURE;
1611 goto done;
1614 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1615 && type == ST_LABEL_FORMAT)
1617 gfc_error ("Label %d at %C previously used as branch target", labelno);
1618 rc = FAILURE;
1619 goto done;
1622 lp->referenced = type;
1623 rc = SUCCESS;
1625 done:
1626 return rc;
1630 /************** Symbol table management subroutines ****************/
1632 /* Basic details: Fortran 95 requires a potentially unlimited number
1633 of distinct namespaces when compiling a program unit. This case
1634 occurs during a compilation of internal subprograms because all of
1635 the internal subprograms must be read before we can start
1636 generating code for the host.
1638 Given the tricky nature of the Fortran grammar, we must be able to
1639 undo changes made to a symbol table if the current interpretation
1640 of a statement is found to be incorrect. Whenever a symbol is
1641 looked up, we make a copy of it and link to it. All of these
1642 symbols are kept in a singly linked list so that we can commit or
1643 undo the changes at a later time.
1645 A symtree may point to a symbol node outside of its namespace. In
1646 this case, that symbol has been used as a host associated variable
1647 at some previous time. */
1649 /* Allocate a new namespace structure. Copies the implicit types from
1650 PARENT if PARENT_TYPES is set. */
1652 gfc_namespace *
1653 gfc_get_namespace (gfc_namespace * parent, int parent_types)
1655 gfc_namespace *ns;
1656 gfc_typespec *ts;
1657 gfc_intrinsic_op in;
1658 int i;
1660 ns = gfc_getmem (sizeof (gfc_namespace));
1661 ns->sym_root = NULL;
1662 ns->uop_root = NULL;
1663 ns->default_access = ACCESS_UNKNOWN;
1664 ns->parent = parent;
1666 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1667 ns->operator_access[in] = ACCESS_UNKNOWN;
1669 /* Initialize default implicit types. */
1670 for (i = 'a'; i <= 'z'; i++)
1672 ns->set_flag[i - 'a'] = 0;
1673 ts = &ns->default_type[i - 'a'];
1675 if (parent_types && ns->parent != NULL)
1677 /* Copy parent settings */
1678 *ts = ns->parent->default_type[i - 'a'];
1679 continue;
1682 if (gfc_option.flag_implicit_none != 0)
1684 gfc_clear_ts (ts);
1685 continue;
1688 if ('i' <= i && i <= 'n')
1690 ts->type = BT_INTEGER;
1691 ts->kind = gfc_default_integer_kind;
1693 else
1695 ts->type = BT_REAL;
1696 ts->kind = gfc_default_real_kind;
1700 ns->refs = 1;
1702 return ns;
1706 /* Comparison function for symtree nodes. */
1708 static int
1709 compare_symtree (void * _st1, void * _st2)
1711 gfc_symtree *st1, *st2;
1713 st1 = (gfc_symtree *) _st1;
1714 st2 = (gfc_symtree *) _st2;
1716 return strcmp (st1->name, st2->name);
1720 /* Allocate a new symtree node and associate it with the new symbol. */
1722 gfc_symtree *
1723 gfc_new_symtree (gfc_symtree ** root, const char *name)
1725 gfc_symtree *st;
1727 st = gfc_getmem (sizeof (gfc_symtree));
1728 st->name = gfc_get_string (name);
1730 gfc_insert_bbt (root, st, compare_symtree);
1731 return st;
1735 /* Delete a symbol from the tree. Does not free the symbol itself! */
1737 static void
1738 delete_symtree (gfc_symtree ** root, const char *name)
1740 gfc_symtree st, *st0;
1742 st0 = gfc_find_symtree (*root, name);
1744 st.name = gfc_get_string (name);
1745 gfc_delete_bbt (root, &st, compare_symtree);
1747 gfc_free (st0);
1751 /* Given a root symtree node and a name, try to find the symbol within
1752 the namespace. Returns NULL if the symbol is not found. */
1754 gfc_symtree *
1755 gfc_find_symtree (gfc_symtree * st, const char *name)
1757 int c;
1759 while (st != NULL)
1761 c = strcmp (name, st->name);
1762 if (c == 0)
1763 return st;
1765 st = (c < 0) ? st->left : st->right;
1768 return NULL;
1772 /* Given a name find a user operator node, creating it if it doesn't
1773 exist. These are much simpler than symbols because they can't be
1774 ambiguous with one another. */
1776 gfc_user_op *
1777 gfc_get_uop (const char *name)
1779 gfc_user_op *uop;
1780 gfc_symtree *st;
1782 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1783 if (st != NULL)
1784 return st->n.uop;
1786 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1788 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1789 uop->name = gfc_get_string (name);
1790 uop->access = ACCESS_UNKNOWN;
1791 uop->ns = gfc_current_ns;
1793 return uop;
1797 /* Given a name find the user operator node. Returns NULL if it does
1798 not exist. */
1800 gfc_user_op *
1801 gfc_find_uop (const char *name, gfc_namespace * ns)
1803 gfc_symtree *st;
1805 if (ns == NULL)
1806 ns = gfc_current_ns;
1808 st = gfc_find_symtree (ns->uop_root, name);
1809 return (st == NULL) ? NULL : st->n.uop;
1813 /* Remove a gfc_symbol structure and everything it points to. */
1815 void
1816 gfc_free_symbol (gfc_symbol * sym)
1819 if (sym == NULL)
1820 return;
1822 gfc_free_array_spec (sym->as);
1824 free_components (sym->components);
1826 gfc_free_expr (sym->value);
1828 gfc_free_namelist (sym->namelist);
1830 gfc_free_namespace (sym->formal_ns);
1832 gfc_free_interface (sym->generic);
1834 gfc_free_formal_arglist (sym->formal);
1836 gfc_free (sym);
1840 /* Allocate and initialize a new symbol node. */
1842 gfc_symbol *
1843 gfc_new_symbol (const char *name, gfc_namespace * ns)
1845 gfc_symbol *p;
1847 p = gfc_getmem (sizeof (gfc_symbol));
1849 gfc_clear_ts (&p->ts);
1850 gfc_clear_attr (&p->attr);
1851 p->ns = ns;
1853 p->declared_at = gfc_current_locus;
1855 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1856 gfc_internal_error ("new_symbol(): Symbol name too long");
1858 p->name = gfc_get_string (name);
1859 return p;
1863 /* Generate an error if a symbol is ambiguous. */
1865 static void
1866 ambiguous_symbol (const char *name, gfc_symtree * st)
1869 if (st->n.sym->module)
1870 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1871 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1872 else
1873 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1874 "from current program unit", name, st->n.sym->name);
1878 /* Search for a symtree starting in the current namespace, resorting to
1879 any parent namespaces if requested by a nonzero parent_flag.
1880 Returns nonzero if the name is ambiguous. */
1883 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1884 gfc_symtree ** result)
1886 gfc_symtree *st;
1888 if (ns == NULL)
1889 ns = gfc_current_ns;
1893 st = gfc_find_symtree (ns->sym_root, name);
1894 if (st != NULL)
1896 *result = st;
1897 if (st->ambiguous)
1899 ambiguous_symbol (name, st);
1900 return 1;
1903 return 0;
1906 if (!parent_flag)
1907 break;
1909 ns = ns->parent;
1911 while (ns != NULL);
1913 *result = NULL;
1914 return 0;
1918 /* Same, but returns the symbol instead. */
1921 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
1922 gfc_symbol ** result)
1924 gfc_symtree *st;
1925 int i;
1927 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
1929 if (st == NULL)
1930 *result = NULL;
1931 else
1932 *result = st->n.sym;
1934 return i;
1938 /* Save symbol with the information necessary to back it out. */
1940 static void
1941 save_symbol_data (gfc_symbol * sym)
1944 if (sym->new || sym->old_symbol != NULL)
1945 return;
1947 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
1948 *(sym->old_symbol) = *sym;
1950 sym->tlink = changed_syms;
1951 changed_syms = sym;
1955 /* Given a name, find a symbol, or create it if it does not exist yet
1956 in the current namespace. If the symbol is found we make sure that
1957 it's OK.
1959 The integer return code indicates
1960 0 All OK
1961 1 The symbol name was ambiguous
1962 2 The name meant to be established was already host associated.
1964 So if the return value is nonzero, then an error was issued. */
1967 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
1969 gfc_symtree *st;
1970 gfc_symbol *p;
1972 /* This doesn't usually happen during resolution. */
1973 if (ns == NULL)
1974 ns = gfc_current_ns;
1976 /* Try to find the symbol in ns. */
1977 st = gfc_find_symtree (ns->sym_root, name);
1979 if (st == NULL)
1981 /* If not there, create a new symbol. */
1982 p = gfc_new_symbol (name, ns);
1984 /* Add to the list of tentative symbols. */
1985 p->old_symbol = NULL;
1986 p->tlink = changed_syms;
1987 p->mark = 1;
1988 p->new = 1;
1989 changed_syms = p;
1991 st = gfc_new_symtree (&ns->sym_root, name);
1992 st->n.sym = p;
1993 p->refs++;
1996 else
1998 /* Make sure the existing symbol is OK. */
1999 if (st->ambiguous)
2001 ambiguous_symbol (name, st);
2002 return 1;
2005 p = st->n.sym;
2007 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2009 /* Symbol is from another namespace. */
2010 gfc_error ("Symbol '%s' at %C has already been host associated",
2011 name);
2012 return 2;
2015 p->mark = 1;
2017 /* Copy in case this symbol is changed. */
2018 save_symbol_data (p);
2021 *result = st;
2022 return 0;
2027 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
2029 gfc_symtree *st;
2030 int i;
2033 i = gfc_get_sym_tree (name, ns, &st);
2034 if (i != 0)
2035 return i;
2037 if (st)
2038 *result = st->n.sym;
2039 else
2040 *result = NULL;
2041 return i;
2045 /* Subroutine that searches for a symbol, creating it if it doesn't
2046 exist, but tries to host-associate the symbol if possible. */
2049 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
2051 gfc_symtree *st;
2052 int i;
2054 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2055 if (st != NULL)
2057 save_symbol_data (st->n.sym);
2059 *result = st;
2060 return i;
2063 if (gfc_current_ns->parent != NULL)
2065 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2066 if (i)
2067 return i;
2069 if (st != NULL)
2071 *result = st;
2072 return 0;
2076 return gfc_get_sym_tree (name, gfc_current_ns, result);
2081 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
2083 int i;
2084 gfc_symtree *st;
2086 i = gfc_get_ha_sym_tree (name, &st);
2088 if (st)
2089 *result = st->n.sym;
2090 else
2091 *result = NULL;
2093 return i;
2096 /* Return true if both symbols could refer to the same data object. Does
2097 not take account of aliasing due to equivalence statements. */
2100 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
2102 /* Aliasing isn't possible if the symbols have different base types. */
2103 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2104 return 0;
2106 /* Pointers can point to other pointers, target objects and allocatable
2107 objects. Two allocatable objects cannot share the same storage. */
2108 if (lsym->attr.pointer
2109 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2110 return 1;
2111 if (lsym->attr.target && rsym->attr.pointer)
2112 return 1;
2113 if (lsym->attr.allocatable && rsym->attr.pointer)
2114 return 1;
2116 return 0;
2120 /* Undoes all the changes made to symbols in the current statement.
2121 This subroutine is made simpler due to the fact that attributes are
2122 never removed once added. */
2124 void
2125 gfc_undo_symbols (void)
2127 gfc_symbol *p, *q, *old;
2129 for (p = changed_syms; p; p = q)
2131 q = p->tlink;
2133 if (p->new)
2135 /* Symbol was new. */
2136 delete_symtree (&p->ns->sym_root, p->name);
2138 p->refs--;
2139 if (p->refs < 0)
2140 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2141 if (p->refs == 0)
2142 gfc_free_symbol (p);
2143 continue;
2146 /* Restore previous state of symbol. Just copy simple stuff. */
2147 p->mark = 0;
2148 old = p->old_symbol;
2150 p->ts.type = old->ts.type;
2151 p->ts.kind = old->ts.kind;
2153 p->attr = old->attr;
2155 if (p->value != old->value)
2157 gfc_free_expr (old->value);
2158 p->value = NULL;
2161 if (p->as != old->as)
2163 if (p->as)
2164 gfc_free_array_spec (p->as);
2165 p->as = old->as;
2168 p->generic = old->generic;
2169 p->component_access = old->component_access;
2171 if (p->namelist != NULL && old->namelist == NULL)
2173 gfc_free_namelist (p->namelist);
2174 p->namelist = NULL;
2176 else
2179 if (p->namelist_tail != old->namelist_tail)
2181 gfc_free_namelist (old->namelist_tail);
2182 old->namelist_tail->next = NULL;
2186 p->namelist_tail = old->namelist_tail;
2188 if (p->formal != old->formal)
2190 gfc_free_formal_arglist (p->formal);
2191 p->formal = old->formal;
2194 gfc_free (p->old_symbol);
2195 p->old_symbol = NULL;
2196 p->tlink = NULL;
2199 changed_syms = NULL;
2203 /* Makes the changes made in the current statement permanent-- gets
2204 rid of undo information. */
2206 void
2207 gfc_commit_symbols (void)
2209 gfc_symbol *p, *q;
2211 for (p = changed_syms; p; p = q)
2213 q = p->tlink;
2214 p->tlink = NULL;
2215 p->mark = 0;
2216 p->new = 0;
2218 if (p->old_symbol != NULL)
2220 gfc_free (p->old_symbol);
2221 p->old_symbol = NULL;
2225 changed_syms = NULL;
2229 /* Recursive function that deletes an entire tree and all the common
2230 head structures it points to. */
2232 static void
2233 free_common_tree (gfc_symtree * common_tree)
2235 if (common_tree == NULL)
2236 return;
2238 free_common_tree (common_tree->left);
2239 free_common_tree (common_tree->right);
2241 gfc_free (common_tree);
2245 /* Recursive function that deletes an entire tree and all the user
2246 operator nodes that it contains. */
2248 static void
2249 free_uop_tree (gfc_symtree * uop_tree)
2252 if (uop_tree == NULL)
2253 return;
2255 free_uop_tree (uop_tree->left);
2256 free_uop_tree (uop_tree->right);
2258 gfc_free_interface (uop_tree->n.uop->operator);
2260 gfc_free (uop_tree->n.uop);
2261 gfc_free (uop_tree);
2265 /* Recursive function that deletes an entire tree and all the symbols
2266 that it contains. */
2268 static void
2269 free_sym_tree (gfc_symtree * sym_tree)
2271 gfc_namespace *ns;
2272 gfc_symbol *sym;
2274 if (sym_tree == NULL)
2275 return;
2277 free_sym_tree (sym_tree->left);
2278 free_sym_tree (sym_tree->right);
2280 sym = sym_tree->n.sym;
2282 sym->refs--;
2283 if (sym->refs < 0)
2284 gfc_internal_error ("free_sym_tree(): Negative refs");
2286 if (sym->formal_ns != NULL && sym->refs == 1)
2288 /* As formal_ns contains a reference to sym, delete formal_ns just
2289 before the deletion of sym. */
2290 ns = sym->formal_ns;
2291 sym->formal_ns = NULL;
2292 gfc_free_namespace (ns);
2294 else if (sym->refs == 0)
2296 /* Go ahead and delete the symbol. */
2297 gfc_free_symbol (sym);
2300 gfc_free (sym_tree);
2304 /* Free a namespace structure and everything below it. Interface
2305 lists associated with intrinsic operators are not freed. These are
2306 taken care of when a specific name is freed. */
2308 void
2309 gfc_free_namespace (gfc_namespace * ns)
2311 gfc_charlen *cl, *cl2;
2312 gfc_namespace *p, *q;
2313 gfc_intrinsic_op i;
2315 if (ns == NULL)
2316 return;
2318 ns->refs--;
2319 if (ns->refs > 0)
2320 return;
2321 gcc_assert (ns->refs == 0);
2323 gfc_free_statements (ns->code);
2325 free_sym_tree (ns->sym_root);
2326 free_uop_tree (ns->uop_root);
2327 free_common_tree (ns->common_root);
2329 for (cl = ns->cl_list; cl; cl = cl2)
2331 cl2 = cl->next;
2332 gfc_free_expr (cl->length);
2333 gfc_free (cl);
2336 free_st_labels (ns->st_labels);
2338 gfc_free_equiv (ns->equiv);
2340 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2341 gfc_free_interface (ns->operator[i]);
2343 gfc_free_data (ns->data);
2344 p = ns->contained;
2345 gfc_free (ns);
2347 /* Recursively free any contained namespaces. */
2348 while (p != NULL)
2350 q = p;
2351 p = p->sibling;
2353 gfc_free_namespace (q);
2358 void
2359 gfc_symbol_init_2 (void)
2362 gfc_current_ns = gfc_get_namespace (NULL, 0);
2366 void
2367 gfc_symbol_done_2 (void)
2370 gfc_free_namespace (gfc_current_ns);
2371 gfc_current_ns = NULL;
2375 /* Clear mark bits from symbol nodes associated with a symtree node. */
2377 static void
2378 clear_sym_mark (gfc_symtree * st)
2381 st->n.sym->mark = 0;
2385 /* Recursively traverse the symtree nodes. */
2387 void
2388 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2390 if (st != NULL)
2392 (*func) (st);
2394 gfc_traverse_symtree (st->left, func);
2395 gfc_traverse_symtree (st->right, func);
2400 /* Recursive namespace traversal function. */
2402 static void
2403 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2406 if (st == NULL)
2407 return;
2409 if (st->n.sym->mark == 0)
2410 (*func) (st->n.sym);
2411 st->n.sym->mark = 1;
2413 traverse_ns (st->left, func);
2414 traverse_ns (st->right, func);
2418 /* Call a given function for all symbols in the namespace. We take
2419 care that each gfc_symbol node is called exactly once. */
2421 void
2422 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2425 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2427 traverse_ns (ns->sym_root, func);
2431 /* Return TRUE if the symbol is an automatic variable. */
2432 static bool
2433 gfc_is_var_automatic (gfc_symbol * sym)
2435 /* Pointer and allocatable variables are never automatic. */
2436 if (sym->attr.pointer || sym->attr.allocatable)
2437 return false;
2438 /* Check for arrays with non-constant size. */
2439 if (sym->attr.dimension && sym->as
2440 && !gfc_is_compile_time_shape (sym->as))
2441 return true;
2442 /* Check for non-constant length character variables. */
2443 if (sym->ts.type == BT_CHARACTER
2444 && sym->ts.cl
2445 && !gfc_is_constant_expr (sym->ts.cl->length))
2446 return true;
2447 return false;
2450 /* Given a symbol, mark it as SAVEd if it is allowed. */
2452 static void
2453 save_symbol (gfc_symbol * sym)
2456 if (sym->attr.use_assoc)
2457 return;
2459 if (sym->attr.in_common
2460 || sym->attr.dummy
2461 || sym->attr.flavor != FL_VARIABLE)
2462 return;
2463 /* Automatic objects are not saved. */
2464 if (gfc_is_var_automatic (sym))
2465 return;
2466 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2470 /* Mark those symbols which can be SAVEd as such. */
2472 void
2473 gfc_save_all (gfc_namespace * ns)
2476 gfc_traverse_ns (ns, save_symbol);
2480 #ifdef GFC_DEBUG
2481 /* Make sure that no changes to symbols are pending. */
2483 void
2484 gfc_symbol_state(void) {
2486 if (changed_syms != NULL)
2487 gfc_internal_error("Symbol changes still pending!");
2489 #endif
2492 /************** Global symbol handling ************/
2495 /* Search a tree for the global symbol. */
2497 gfc_gsymbol *
2498 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2500 gfc_gsymbol *s;
2502 if (symbol == NULL)
2503 return NULL;
2504 if (strcmp (symbol->name, name) == 0)
2505 return symbol;
2507 s = gfc_find_gsymbol (symbol->left, name);
2508 if (s != NULL)
2509 return s;
2511 s = gfc_find_gsymbol (symbol->right, name);
2512 if (s != NULL)
2513 return s;
2515 return NULL;
2519 /* Compare two global symbols. Used for managing the BB tree. */
2521 static int
2522 gsym_compare (void * _s1, void * _s2)
2524 gfc_gsymbol *s1, *s2;
2526 s1 = (gfc_gsymbol *)_s1;
2527 s2 = (gfc_gsymbol *)_s2;
2528 return strcmp(s1->name, s2->name);
2532 /* Get a global symbol, creating it if it doesn't exist. */
2534 gfc_gsymbol *
2535 gfc_get_gsymbol (const char *name)
2537 gfc_gsymbol *s;
2539 s = gfc_find_gsymbol (gfc_gsym_root, name);
2540 if (s != NULL)
2541 return s;
2543 s = gfc_getmem (sizeof (gfc_gsymbol));
2544 s->type = GSYM_UNKNOWN;
2545 s->name = gfc_get_string (name);
2547 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2549 return s;