* fold-const.c (add_double): Rename to add_double_with_sign.
[official-gcc.git] / gcc / fortran / symbol.c
blobcd38ef8dae49786309d0d32a7bdaa5583bc035dd
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
3 Foundation, 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 "flags.h"
27 #include "gfortran.h"
28 #include "parse.h"
30 /* Strings for all symbol attributes. We use these for dumping the
31 parse tree, in error messages, and also when reading and writing
32 modules. */
34 const mstring flavors[] =
36 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
37 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
38 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
39 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
40 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
41 minit (NULL, -1)
44 const mstring procedures[] =
46 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
47 minit ("MODULE-PROC", PROC_MODULE),
48 minit ("INTERNAL-PROC", PROC_INTERNAL),
49 minit ("DUMMY-PROC", PROC_DUMMY),
50 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
51 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
52 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
53 minit (NULL, -1)
56 const mstring intents[] =
58 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
59 minit ("IN", INTENT_IN),
60 minit ("OUT", INTENT_OUT),
61 minit ("INOUT", INTENT_INOUT),
62 minit (NULL, -1)
65 const mstring access_types[] =
67 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
68 minit ("PUBLIC", ACCESS_PUBLIC),
69 minit ("PRIVATE", ACCESS_PRIVATE),
70 minit (NULL, -1)
73 const mstring ifsrc_types[] =
75 minit ("UNKNOWN", IFSRC_UNKNOWN),
76 minit ("DECL", IFSRC_DECL),
77 minit ("BODY", IFSRC_IFBODY),
78 minit ("USAGE", IFSRC_USAGE)
82 /* This is to make sure the backend generates setup code in the correct
83 order. */
85 static int next_dummy_order = 1;
88 gfc_namespace *gfc_current_ns;
90 gfc_gsymbol *gfc_gsym_root = NULL;
92 static gfc_symbol *changed_syms = NULL;
95 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
97 /* The following static variable indicates whether a particular element has
98 been explicitly set or not. */
100 static int new_flag[GFC_LETTERS];
103 /* Handle a correctly parsed IMPLICIT NONE. */
105 void
106 gfc_set_implicit_none (void)
108 int i;
110 if (gfc_current_ns->seen_implicit_none)
112 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
113 return;
116 gfc_current_ns->seen_implicit_none = 1;
118 for (i = 0; i < GFC_LETTERS; i++)
120 gfc_clear_ts (&gfc_current_ns->default_type[i]);
121 gfc_current_ns->set_flag[i] = 1;
126 /* Reset the implicit range flags. */
128 void
129 gfc_clear_new_implicit (void)
131 int i;
133 for (i = 0; i < GFC_LETTERS; i++)
134 new_flag[i] = 0;
138 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
141 gfc_add_new_implicit_range (int c1, int c2)
143 int i;
145 c1 -= 'a';
146 c2 -= 'a';
148 for (i = c1; i <= c2; i++)
150 if (new_flag[i])
152 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
153 i + 'A');
154 return FAILURE;
157 new_flag[i] = 1;
160 return SUCCESS;
164 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
165 the new implicit types back into the existing types will work. */
168 gfc_merge_new_implicit (gfc_typespec * ts)
170 int i;
172 if (gfc_current_ns->seen_implicit_none)
174 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
175 return FAILURE;
178 for (i = 0; i < GFC_LETTERS; i++)
180 if (new_flag[i])
183 if (gfc_current_ns->set_flag[i])
185 gfc_error ("Letter %c already has an IMPLICIT type at %C",
186 i + 'A');
187 return FAILURE;
189 gfc_current_ns->default_type[i] = *ts;
190 gfc_current_ns->set_flag[i] = 1;
193 return SUCCESS;
197 /* Given a symbol, return a pointer to the typespec for its default type. */
199 gfc_typespec *
200 gfc_get_default_type (gfc_symbol * sym, gfc_namespace * ns)
202 char letter;
204 letter = sym->name[0];
205 if (letter < 'a' || letter > 'z')
206 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
208 if (ns == NULL)
209 ns = gfc_current_ns;
211 return &ns->default_type[letter - 'a'];
215 /* Given a pointer to a symbol, set its type according to the first
216 letter of its name. Fails if the letter in question has no default
217 type. */
220 gfc_set_default_type (gfc_symbol * sym, int error_flag, gfc_namespace * ns)
222 gfc_typespec *ts;
224 if (sym->ts.type != BT_UNKNOWN)
225 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
227 ts = gfc_get_default_type (sym, ns);
229 if (ts->type == BT_UNKNOWN)
231 if (error_flag && !sym->attr.untyped)
233 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
234 sym->name, &sym->declared_at);
235 sym->attr.untyped = 1; /* Ensure we only give an error once. */
238 return FAILURE;
241 sym->ts = *ts;
242 sym->attr.implicit_type = 1;
244 return SUCCESS;
248 /******************** Symbol attribute stuff *********************/
250 /* This is a generic conflict-checker. We do this to avoid having a
251 single conflict in two places. */
253 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
254 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
255 #define conf_std(a, b, std) if (attr->a && attr->b)\
257 a1 = a;\
258 a2 = b;\
259 standard = std;\
260 goto conflict_std;\
263 static try
264 check_conflict (symbol_attribute * attr, const char * name, locus * where)
266 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
267 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
268 *intrinsic = "INTRINSIC", *allocatable = "ALLOCATABLE",
269 *elemental = "ELEMENTAL", *private = "PRIVATE", *recursive = "RECURSIVE",
270 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
271 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
272 *function = "FUNCTION", *subroutine = "SUBROUTINE",
273 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
274 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
275 *cray_pointee = "CRAY POINTEE", *data = "DATA";
276 static const char *threadprivate = "THREADPRIVATE";
278 const char *a1, *a2;
279 int standard;
281 if (where == NULL)
282 where = &gfc_current_locus;
284 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
286 a1 = pointer;
287 a2 = intent;
288 goto conflict;
291 /* Check for attributes not allowed in a BLOCK DATA. */
292 if (gfc_current_state () == COMP_BLOCK_DATA)
294 a1 = NULL;
296 if (attr->in_namelist)
297 a1 = in_namelist;
298 if (attr->allocatable)
299 a1 = allocatable;
300 if (attr->external)
301 a1 = external;
302 if (attr->optional)
303 a1 = optional;
304 if (attr->access == ACCESS_PRIVATE)
305 a1 = private;
306 if (attr->access == ACCESS_PUBLIC)
307 a1 = public;
308 if (attr->intent != INTENT_UNKNOWN)
309 a1 = intent;
311 if (a1 != NULL)
313 gfc_error
314 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
315 where);
316 return FAILURE;
320 conf (dummy, save);
321 conf (dummy, threadprivate);
322 conf (pointer, target);
323 conf (pointer, external);
324 conf (pointer, intrinsic);
325 conf (pointer, elemental);
326 conf (allocatable, elemental);
328 conf (target, external);
329 conf (target, intrinsic);
330 conf (external, dimension); /* See Fortran 95's R504. */
332 conf (external, intrinsic);
334 if (attr->if_source || attr->contained)
336 conf (external, subroutine);
337 conf (external, function);
340 conf (allocatable, pointer);
341 conf_std (allocatable, dummy, GFC_STD_F2003);
342 conf_std (allocatable, function, GFC_STD_F2003);
343 conf_std (allocatable, result, GFC_STD_F2003);
344 conf (elemental, recursive);
346 conf (in_common, dummy);
347 conf (in_common, allocatable);
348 conf (in_common, result);
349 conf (in_common, save);
350 conf (result, save);
352 conf (dummy, result);
354 conf (in_equivalence, use_assoc);
355 conf (in_equivalence, dummy);
356 conf (in_equivalence, target);
357 conf (in_equivalence, pointer);
358 conf (in_equivalence, function);
359 conf (in_equivalence, result);
360 conf (in_equivalence, entry);
361 conf (in_equivalence, allocatable);
362 conf (in_equivalence, threadprivate);
364 conf (in_namelist, pointer);
365 conf (in_namelist, allocatable);
367 conf (entry, result);
369 conf (function, subroutine);
371 /* Cray pointer/pointee conflicts. */
372 conf (cray_pointer, cray_pointee);
373 conf (cray_pointer, dimension);
374 conf (cray_pointer, pointer);
375 conf (cray_pointer, target);
376 conf (cray_pointer, allocatable);
377 conf (cray_pointer, external);
378 conf (cray_pointer, intrinsic);
379 conf (cray_pointer, in_namelist);
380 conf (cray_pointer, function);
381 conf (cray_pointer, subroutine);
382 conf (cray_pointer, entry);
384 conf (cray_pointee, allocatable);
385 conf (cray_pointee, intent);
386 conf (cray_pointee, optional);
387 conf (cray_pointee, dummy);
388 conf (cray_pointee, target);
389 conf (cray_pointee, intrinsic);
390 conf (cray_pointee, pointer);
391 conf (cray_pointee, entry);
392 conf (cray_pointee, in_common);
393 conf (cray_pointee, in_equivalence);
394 conf (cray_pointee, threadprivate);
396 conf (data, dummy);
397 conf (data, function);
398 conf (data, result);
399 conf (data, allocatable);
400 conf (data, use_assoc);
402 a1 = gfc_code2string (flavors, attr->flavor);
404 if (attr->in_namelist
405 && attr->flavor != FL_VARIABLE
406 && attr->flavor != FL_UNKNOWN)
409 a2 = in_namelist;
410 goto conflict;
413 switch (attr->flavor)
415 case FL_PROGRAM:
416 case FL_BLOCK_DATA:
417 case FL_MODULE:
418 case FL_LABEL:
419 conf2 (dummy);
420 conf2 (save);
421 conf2 (pointer);
422 conf2 (target);
423 conf2 (external);
424 conf2 (intrinsic);
425 conf2 (allocatable);
426 conf2 (result);
427 conf2 (in_namelist);
428 conf2 (optional);
429 conf2 (function);
430 conf2 (subroutine);
431 conf2 (threadprivate);
432 break;
434 case FL_VARIABLE:
435 case FL_NAMELIST:
436 break;
438 case FL_PROCEDURE:
439 conf2 (intent);
441 if (attr->subroutine)
443 conf2(save);
444 conf2(pointer);
445 conf2(target);
446 conf2(allocatable);
447 conf2(result);
448 conf2(in_namelist);
449 conf2(function);
450 conf2(threadprivate);
453 switch (attr->proc)
455 case PROC_ST_FUNCTION:
456 conf2 (in_common);
457 conf2 (dummy);
458 break;
460 case PROC_MODULE:
461 conf2 (dummy);
462 break;
464 case PROC_DUMMY:
465 conf2 (result);
466 conf2 (in_common);
467 conf2 (save);
468 conf2 (threadprivate);
469 break;
471 default:
472 break;
475 break;
477 case FL_DERIVED:
478 conf2 (dummy);
479 conf2 (save);
480 conf2 (pointer);
481 conf2 (target);
482 conf2 (external);
483 conf2 (intrinsic);
484 conf2 (allocatable);
485 conf2 (optional);
486 conf2 (entry);
487 conf2 (function);
488 conf2 (subroutine);
489 conf2 (threadprivate);
491 if (attr->intent != INTENT_UNKNOWN)
493 a2 = intent;
494 goto conflict;
496 break;
498 case FL_PARAMETER:
499 conf2 (external);
500 conf2 (intrinsic);
501 conf2 (optional);
502 conf2 (allocatable);
503 conf2 (function);
504 conf2 (subroutine);
505 conf2 (entry);
506 conf2 (pointer);
507 conf2 (target);
508 conf2 (dummy);
509 conf2 (in_common);
510 conf2 (save);
511 conf2 (threadprivate);
512 break;
514 default:
515 break;
518 return SUCCESS;
520 conflict:
521 if (name == NULL)
522 gfc_error ("%s attribute conflicts with %s attribute at %L",
523 a1, a2, where);
524 else
525 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
526 a1, a2, name, where);
528 return FAILURE;
530 conflict_std:
531 if (name == NULL)
533 return gfc_notify_std (standard, "In the selected standard, %s attribute "
534 "conflicts with %s attribute at %L", a1, a2,
535 where);
537 else
539 return gfc_notify_std (standard, "In the selected standard, %s attribute "
540 "conflicts with %s attribute in '%s' at %L",
541 a1, a2, name, where);
545 #undef conf
546 #undef conf2
547 #undef conf_std
550 /* Mark a symbol as referenced. */
552 void
553 gfc_set_sym_referenced (gfc_symbol * sym)
555 if (sym->attr.referenced)
556 return;
558 sym->attr.referenced = 1;
560 /* Remember which order dummy variables are accessed in. */
561 if (sym->attr.dummy)
562 sym->dummy_order = next_dummy_order++;
566 /* Common subroutine called by attribute changing subroutines in order
567 to prevent them from changing a symbol that has been
568 use-associated. Returns zero if it is OK to change the symbol,
569 nonzero if not. */
571 static int
572 check_used (symbol_attribute * attr, const char * name, locus * where)
575 if (attr->use_assoc == 0)
576 return 0;
578 if (where == NULL)
579 where = &gfc_current_locus;
581 if (name == NULL)
582 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
583 where);
584 else
585 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
586 name, where);
588 return 1;
592 /* Used to prevent changing the attributes of a symbol after it has been
593 used. This check is only done for dummy variables as only these can be
594 used in specification expressions. Applying this to all symbols causes
595 an error when we reach the body of a contained function. */
597 static int
598 check_done (symbol_attribute * attr, locus * where)
601 if (!(attr->dummy && attr->referenced))
602 return 0;
604 if (where == NULL)
605 where = &gfc_current_locus;
607 gfc_error ("Cannot change attributes of symbol at %L"
608 " after it has been used", where);
610 return 1;
614 /* Generate an error because of a duplicate attribute. */
616 static void
617 duplicate_attr (const char *attr, locus * where)
620 if (where == NULL)
621 where = &gfc_current_locus;
623 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
626 /* Called from decl.c (attr_decl1) to check attributes, when declared separately. */
629 gfc_add_attribute (symbol_attribute * attr, locus * where,
630 unsigned int attr_intent)
633 if (check_used (attr, NULL, where)
634 || (attr_intent == 0 && check_done (attr, where)))
635 return FAILURE;
637 return check_conflict (attr, NULL, where);
641 gfc_add_allocatable (symbol_attribute * attr, locus * where)
644 if (check_used (attr, NULL, where) || check_done (attr, where))
645 return FAILURE;
647 if (attr->allocatable)
649 duplicate_attr ("ALLOCATABLE", where);
650 return FAILURE;
653 attr->allocatable = 1;
654 return check_conflict (attr, NULL, where);
659 gfc_add_dimension (symbol_attribute * attr, const char *name, locus * where)
662 if (check_used (attr, name, where) || check_done (attr, where))
663 return FAILURE;
665 if (attr->dimension)
667 duplicate_attr ("DIMENSION", where);
668 return FAILURE;
671 attr->dimension = 1;
672 return check_conflict (attr, name, where);
677 gfc_add_external (symbol_attribute * attr, locus * where)
680 if (check_used (attr, NULL, where) || check_done (attr, where))
681 return FAILURE;
683 if (attr->external)
685 duplicate_attr ("EXTERNAL", where);
686 return FAILURE;
689 attr->external = 1;
691 return check_conflict (attr, NULL, where);
696 gfc_add_intrinsic (symbol_attribute * attr, locus * where)
699 if (check_used (attr, NULL, where) || check_done (attr, where))
700 return FAILURE;
702 if (attr->intrinsic)
704 duplicate_attr ("INTRINSIC", where);
705 return FAILURE;
708 attr->intrinsic = 1;
710 return check_conflict (attr, NULL, where);
715 gfc_add_optional (symbol_attribute * attr, locus * where)
718 if (check_used (attr, NULL, where) || check_done (attr, where))
719 return FAILURE;
721 if (attr->optional)
723 duplicate_attr ("OPTIONAL", where);
724 return FAILURE;
727 attr->optional = 1;
728 return check_conflict (attr, NULL, where);
733 gfc_add_pointer (symbol_attribute * attr, locus * where)
736 if (check_used (attr, NULL, where) || check_done (attr, where))
737 return FAILURE;
739 attr->pointer = 1;
740 return check_conflict (attr, NULL, where);
745 gfc_add_cray_pointer (symbol_attribute * attr, locus * where)
748 if (check_used (attr, NULL, where) || check_done (attr, where))
749 return FAILURE;
751 attr->cray_pointer = 1;
752 return check_conflict (attr, NULL, where);
757 gfc_add_cray_pointee (symbol_attribute * attr, locus * where)
760 if (check_used (attr, NULL, where) || check_done (attr, where))
761 return FAILURE;
763 if (attr->cray_pointee)
765 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
766 " statements.", where);
767 return FAILURE;
770 attr->cray_pointee = 1;
771 return check_conflict (attr, NULL, where);
776 gfc_add_result (symbol_attribute * attr, const char *name, locus * where)
779 if (check_used (attr, name, where) || check_done (attr, where))
780 return FAILURE;
782 attr->result = 1;
783 return check_conflict (attr, name, where);
788 gfc_add_save (symbol_attribute * attr, const char *name, locus * where)
791 if (check_used (attr, name, where))
792 return FAILURE;
794 if (gfc_pure (NULL))
796 gfc_error
797 ("SAVE attribute at %L cannot be specified in a PURE procedure",
798 where);
799 return FAILURE;
802 if (attr->save)
804 if (gfc_notify_std (GFC_STD_LEGACY,
805 "Duplicate SAVE attribute specified at %L",
806 where)
807 == FAILURE)
808 return FAILURE;
811 attr->save = 1;
812 return check_conflict (attr, name, where);
817 gfc_add_threadprivate (symbol_attribute * attr, const char *name, locus * where)
819 if (check_used (attr, name, where))
820 return FAILURE;
822 if (attr->threadprivate)
824 duplicate_attr ("THREADPRIVATE", where);
825 return FAILURE;
828 attr->threadprivate = 1;
829 return check_conflict (attr, name, where);
834 gfc_add_target (symbol_attribute * attr, locus * where)
837 if (check_used (attr, NULL, where) || check_done (attr, where))
838 return FAILURE;
840 if (attr->target)
842 duplicate_attr ("TARGET", where);
843 return FAILURE;
846 attr->target = 1;
847 return check_conflict (attr, NULL, where);
852 gfc_add_dummy (symbol_attribute * attr, const char *name, locus * where)
855 if (check_used (attr, name, where))
856 return FAILURE;
858 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
859 attr->dummy = 1;
860 return check_conflict (attr, name, where);
865 gfc_add_in_common (symbol_attribute * attr, const char *name, locus * where)
868 if (check_used (attr, name, where) || check_done (attr, where))
869 return FAILURE;
871 /* Duplicate attribute already checked for. */
872 attr->in_common = 1;
873 if (check_conflict (attr, name, where) == FAILURE)
874 return FAILURE;
876 if (attr->flavor == FL_VARIABLE)
877 return SUCCESS;
879 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
883 gfc_add_in_equivalence (symbol_attribute * attr, const char *name, locus * where)
886 /* Duplicate attribute already checked for. */
887 attr->in_equivalence = 1;
888 if (check_conflict (attr, name, where) == FAILURE)
889 return FAILURE;
891 if (attr->flavor == FL_VARIABLE)
892 return SUCCESS;
894 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
899 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
902 if (check_used (attr, name, where))
903 return FAILURE;
905 attr->data = 1;
906 return check_conflict (attr, name, where);
911 gfc_add_in_namelist (symbol_attribute * attr, const char *name,
912 locus * where)
915 attr->in_namelist = 1;
916 return check_conflict (attr, name, where);
921 gfc_add_sequence (symbol_attribute * attr, const char *name, locus * where)
924 if (check_used (attr, name, where))
925 return FAILURE;
927 attr->sequence = 1;
928 return check_conflict (attr, name, where);
933 gfc_add_elemental (symbol_attribute * attr, locus * where)
936 if (check_used (attr, NULL, where) || check_done (attr, where))
937 return FAILURE;
939 attr->elemental = 1;
940 return check_conflict (attr, NULL, where);
945 gfc_add_pure (symbol_attribute * attr, locus * where)
948 if (check_used (attr, NULL, where) || check_done (attr, where))
949 return FAILURE;
951 attr->pure = 1;
952 return check_conflict (attr, NULL, where);
957 gfc_add_recursive (symbol_attribute * attr, locus * where)
960 if (check_used (attr, NULL, where) || check_done (attr, where))
961 return FAILURE;
963 attr->recursive = 1;
964 return check_conflict (attr, NULL, where);
969 gfc_add_entry (symbol_attribute * attr, const char *name, locus * where)
972 if (check_used (attr, name, where))
973 return FAILURE;
975 if (attr->entry)
977 duplicate_attr ("ENTRY", where);
978 return FAILURE;
981 attr->entry = 1;
982 return check_conflict (attr, name, where);
987 gfc_add_function (symbol_attribute * attr, const char *name, locus * where)
990 if (attr->flavor != FL_PROCEDURE
991 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
992 return FAILURE;
994 attr->function = 1;
995 return check_conflict (attr, name, where);
1000 gfc_add_subroutine (symbol_attribute * attr, const char *name, locus * where)
1003 if (attr->flavor != FL_PROCEDURE
1004 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1005 return FAILURE;
1007 attr->subroutine = 1;
1008 return check_conflict (attr, name, where);
1013 gfc_add_generic (symbol_attribute * attr, const char *name, locus * where)
1016 if (attr->flavor != FL_PROCEDURE
1017 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1018 return FAILURE;
1020 attr->generic = 1;
1021 return check_conflict (attr, name, where);
1025 /* Flavors are special because some flavors are not what Fortran
1026 considers attributes and can be reaffirmed multiple times. */
1029 gfc_add_flavor (symbol_attribute * attr, sym_flavor f, const char *name,
1030 locus * where)
1033 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1034 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1035 || f == FL_NAMELIST) && check_used (attr, name, where))
1036 return FAILURE;
1038 if (attr->flavor == f && f == FL_VARIABLE)
1039 return SUCCESS;
1041 if (attr->flavor != FL_UNKNOWN)
1043 if (where == NULL)
1044 where = &gfc_current_locus;
1046 gfc_error ("%s attribute conflicts with %s attribute at %L",
1047 gfc_code2string (flavors, attr->flavor),
1048 gfc_code2string (flavors, f), where);
1050 return FAILURE;
1053 attr->flavor = f;
1055 return check_conflict (attr, name, where);
1060 gfc_add_procedure (symbol_attribute * attr, procedure_type t,
1061 const char *name, locus * where)
1064 if (check_used (attr, name, where) || check_done (attr, where))
1065 return FAILURE;
1067 if (attr->flavor != FL_PROCEDURE
1068 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1069 return FAILURE;
1071 if (where == NULL)
1072 where = &gfc_current_locus;
1074 if (attr->proc != PROC_UNKNOWN)
1076 gfc_error ("%s procedure at %L is already declared as %s procedure",
1077 gfc_code2string (procedures, t), where,
1078 gfc_code2string (procedures, attr->proc));
1080 return FAILURE;
1083 attr->proc = t;
1085 /* Statement functions are always scalar and functions. */
1086 if (t == PROC_ST_FUNCTION
1087 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1088 || attr->dimension))
1089 return FAILURE;
1091 return check_conflict (attr, name, where);
1096 gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
1099 if (check_used (attr, NULL, where))
1100 return FAILURE;
1102 if (attr->intent == INTENT_UNKNOWN)
1104 attr->intent = intent;
1105 return check_conflict (attr, NULL, where);
1108 if (where == NULL)
1109 where = &gfc_current_locus;
1111 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1112 gfc_intent_string (attr->intent),
1113 gfc_intent_string (intent), where);
1115 return FAILURE;
1119 /* No checks for use-association in public and private statements. */
1122 gfc_add_access (symbol_attribute * attr, gfc_access access,
1123 const char *name, locus * where)
1126 if (attr->access == ACCESS_UNKNOWN)
1128 attr->access = access;
1129 return check_conflict (attr, name, where);
1132 if (where == NULL)
1133 where = &gfc_current_locus;
1134 gfc_error ("ACCESS specification at %L was already specified", where);
1136 return FAILURE;
1141 gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
1142 gfc_formal_arglist * formal, locus * where)
1145 if (check_used (&sym->attr, sym->name, where))
1146 return FAILURE;
1148 if (where == NULL)
1149 where = &gfc_current_locus;
1151 if (sym->attr.if_source != IFSRC_UNKNOWN
1152 && sym->attr.if_source != IFSRC_DECL)
1154 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1155 sym->name, where);
1156 return FAILURE;
1159 sym->formal = formal;
1160 sym->attr.if_source = source;
1162 return SUCCESS;
1166 /* Add a type to a symbol. */
1169 gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
1171 sym_flavor flavor;
1173 /* TODO: This is legal if it is reaffirming an implicit type.
1174 if (check_done (&sym->attr, where))
1175 return FAILURE;*/
1177 if (where == NULL)
1178 where = &gfc_current_locus;
1180 if (sym->ts.type != BT_UNKNOWN)
1182 const char *msg = "Symbol '%s' at %L already has basic type of %s";
1183 if (!(sym->ts.type == ts->type
1184 && (sym->attr.flavor == FL_PROCEDURE || sym->attr.result))
1185 || gfc_notification_std (GFC_STD_GNU) == ERROR
1186 || pedantic)
1188 gfc_error (msg, sym->name, where, gfc_basic_typename (sym->ts.type));
1189 return FAILURE;
1191 else if (gfc_notify_std (GFC_STD_GNU, msg, sym->name, where,
1192 gfc_basic_typename (sym->ts.type)) == FAILURE)
1193 return FAILURE;
1196 flavor = sym->attr.flavor;
1198 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1199 || flavor == FL_LABEL || (flavor == FL_PROCEDURE
1200 && sym->attr.subroutine)
1201 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1203 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1204 return FAILURE;
1207 sym->ts = *ts;
1208 return SUCCESS;
1212 /* Clears all attributes. */
1214 void
1215 gfc_clear_attr (symbol_attribute * attr)
1217 memset (attr, 0, sizeof(symbol_attribute));
1221 /* Check for missing attributes in the new symbol. Currently does
1222 nothing, but it's not clear that it is unnecessary yet. */
1225 gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1226 locus * where ATTRIBUTE_UNUSED)
1229 return SUCCESS;
1233 /* Copy an attribute to a symbol attribute, bit by bit. Some
1234 attributes have a lot of side-effects but cannot be present given
1235 where we are called from, so we ignore some bits. */
1238 gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1241 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1242 goto fail;
1244 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1245 goto fail;
1246 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1247 goto fail;
1248 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1249 goto fail;
1250 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1251 goto fail;
1252 if (src->threadprivate && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
1253 goto fail;
1254 if (src->target && gfc_add_target (dest, where) == FAILURE)
1255 goto fail;
1256 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1257 goto fail;
1258 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1259 goto fail;
1260 if (src->entry)
1261 dest->entry = 1;
1263 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1264 goto fail;
1266 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1267 goto fail;
1269 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1270 goto fail;
1271 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1272 goto fail;
1273 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1274 goto fail;
1276 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1277 goto fail;
1278 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1279 goto fail;
1280 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1281 goto fail;
1282 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1283 goto fail;
1285 if (src->flavor != FL_UNKNOWN
1286 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1287 goto fail;
1289 if (src->intent != INTENT_UNKNOWN
1290 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1291 goto fail;
1293 if (src->access != ACCESS_UNKNOWN
1294 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1295 goto fail;
1297 if (gfc_missing_attr (dest, where) == FAILURE)
1298 goto fail;
1300 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1301 goto fail;
1302 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1303 goto fail;
1305 /* The subroutines that set these bits also cause flavors to be set,
1306 and that has already happened in the original, so don't let it
1307 happen again. */
1308 if (src->external)
1309 dest->external = 1;
1310 if (src->intrinsic)
1311 dest->intrinsic = 1;
1313 return SUCCESS;
1315 fail:
1316 return FAILURE;
1320 /************** Component name management ************/
1322 /* Component names of a derived type form their own little namespaces
1323 that are separate from all other spaces. The space is composed of
1324 a singly linked list of gfc_component structures whose head is
1325 located in the parent symbol. */
1328 /* Add a component name to a symbol. The call fails if the name is
1329 already present. On success, the component pointer is modified to
1330 point to the additional component structure. */
1333 gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1335 gfc_component *p, *tail;
1337 tail = NULL;
1339 for (p = sym->components; p; p = p->next)
1341 if (strcmp (p->name, name) == 0)
1343 gfc_error ("Component '%s' at %C already declared at %L",
1344 name, &p->loc);
1345 return FAILURE;
1348 tail = p;
1351 /* Allocate a new component. */
1352 p = gfc_get_component ();
1354 if (tail == NULL)
1355 sym->components = p;
1356 else
1357 tail->next = p;
1359 p->name = gfc_get_string (name);
1360 p->loc = gfc_current_locus;
1362 *component = p;
1363 return SUCCESS;
1367 /* Recursive function to switch derived types of all symbol in a
1368 namespace. */
1370 static void
1371 switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1373 gfc_symbol *sym;
1375 if (st == NULL)
1376 return;
1378 sym = st->n.sym;
1379 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1380 sym->ts.derived = to;
1382 switch_types (st->left, from, to);
1383 switch_types (st->right, from, to);
1387 /* This subroutine is called when a derived type is used in order to
1388 make the final determination about which version to use. The
1389 standard requires that a type be defined before it is 'used', but
1390 such types can appear in IMPLICIT statements before the actual
1391 definition. 'Using' in this context means declaring a variable to
1392 be that type or using the type constructor.
1394 If a type is used and the components haven't been defined, then we
1395 have to have a derived type in a parent unit. We find the node in
1396 the other namespace and point the symtree node in this namespace to
1397 that node. Further reference to this name point to the correct
1398 node. If we can't find the node in a parent namespace, then we have
1399 an error.
1401 This subroutine takes a pointer to a symbol node and returns a
1402 pointer to the translated node or NULL for an error. Usually there
1403 is no translation and we return the node we were passed. */
1405 gfc_symbol *
1406 gfc_use_derived (gfc_symbol * sym)
1408 gfc_symbol *s;
1409 gfc_typespec *t;
1410 gfc_symtree *st;
1411 int i;
1413 if (sym->components != NULL)
1414 return sym; /* Already defined. */
1416 if (sym->ns->parent == NULL)
1417 goto bad;
1419 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1421 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1422 return NULL;
1425 if (s == NULL || s->attr.flavor != FL_DERIVED)
1426 goto bad;
1428 /* Get rid of symbol sym, translating all references to s. */
1429 for (i = 0; i < GFC_LETTERS; i++)
1431 t = &sym->ns->default_type[i];
1432 if (t->derived == sym)
1433 t->derived = s;
1436 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1437 st->n.sym = s;
1439 s->refs++;
1441 /* Unlink from list of modified symbols. */
1442 gfc_commit_symbol (sym);
1444 switch_types (sym->ns->sym_root, sym, s);
1446 /* TODO: Also have to replace sym -> s in other lists like
1447 namelists, common lists and interface lists. */
1448 gfc_free_symbol (sym);
1450 return s;
1452 bad:
1453 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1454 sym->name);
1455 return NULL;
1459 /* Given a derived type node and a component name, try to locate the
1460 component structure. Returns the NULL pointer if the component is
1461 not found or the components are private. */
1463 gfc_component *
1464 gfc_find_component (gfc_symbol * sym, const char *name)
1466 gfc_component *p;
1468 if (name == NULL)
1469 return NULL;
1471 sym = gfc_use_derived (sym);
1473 if (sym == NULL)
1474 return NULL;
1476 for (p = sym->components; p; p = p->next)
1477 if (strcmp (p->name, name) == 0)
1478 break;
1480 if (p == NULL)
1481 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1482 name, sym->name);
1483 else
1485 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1487 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1488 name, sym->name);
1489 p = NULL;
1493 return p;
1497 /* Given a symbol, free all of the component structures and everything
1498 they point to. */
1500 static void
1501 free_components (gfc_component * p)
1503 gfc_component *q;
1505 for (; p; p = q)
1507 q = p->next;
1509 gfc_free_array_spec (p->as);
1510 gfc_free_expr (p->initializer);
1512 gfc_free (p);
1517 /* Set component attributes from a standard symbol attribute
1518 structure. */
1520 void
1521 gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1524 c->dimension = attr->dimension;
1525 c->pointer = attr->pointer;
1526 c->allocatable = attr->allocatable;
1530 /* Get a standard symbol attribute structure given the component
1531 structure. */
1533 void
1534 gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1537 gfc_clear_attr (attr);
1538 attr->dimension = c->dimension;
1539 attr->pointer = c->pointer;
1540 attr->allocatable = c->allocatable;
1544 /******************** Statement label management ********************/
1546 /* Comparison function for statement labels, used for managing the
1547 binary tree. */
1549 static int
1550 compare_st_labels (void * a1, void * b1)
1552 int a = ((gfc_st_label *)a1)->value;
1553 int b = ((gfc_st_label *)b1)->value;
1555 return (b - a);
1559 /* Free a single gfc_st_label structure, making sure the tree is not
1560 messed up. This function is called only when some parse error
1561 occurs. */
1563 void
1564 gfc_free_st_label (gfc_st_label * label)
1566 if (label == NULL)
1567 return;
1569 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
1571 if (label->format != NULL)
1572 gfc_free_expr (label->format);
1574 gfc_free (label);
1577 /* Free a whole tree of gfc_st_label structures. */
1579 static void
1580 free_st_labels (gfc_st_label * label)
1582 if (label == NULL)
1583 return;
1585 free_st_labels (label->left);
1586 free_st_labels (label->right);
1588 if (label->format != NULL)
1589 gfc_free_expr (label->format);
1590 gfc_free (label);
1594 /* Given a label number, search for and return a pointer to the label
1595 structure, creating it if it does not exist. */
1597 gfc_st_label *
1598 gfc_get_st_label (int labelno)
1600 gfc_st_label *lp;
1602 /* First see if the label is already in this namespace. */
1603 lp = gfc_current_ns->st_labels;
1604 while (lp)
1606 if (lp->value == labelno)
1607 return lp;
1609 if (lp->value < labelno)
1610 lp = lp->left;
1611 else
1612 lp = lp->right;
1615 lp = gfc_getmem (sizeof (gfc_st_label));
1617 lp->value = labelno;
1618 lp->defined = ST_LABEL_UNKNOWN;
1619 lp->referenced = ST_LABEL_UNKNOWN;
1621 gfc_insert_bbt (&gfc_current_ns->st_labels, lp, compare_st_labels);
1623 return lp;
1627 /* Called when a statement with a statement label is about to be
1628 accepted. We add the label to the list of the current namespace,
1629 making sure it hasn't been defined previously and referenced
1630 correctly. */
1632 void
1633 gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1635 int labelno;
1637 labelno = lp->value;
1639 if (lp->defined != ST_LABEL_UNKNOWN)
1640 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1641 &lp->where, label_locus);
1642 else
1644 lp->where = *label_locus;
1646 switch (type)
1648 case ST_LABEL_FORMAT:
1649 if (lp->referenced == ST_LABEL_TARGET)
1650 gfc_error ("Label %d at %C already referenced as branch target",
1651 labelno);
1652 else
1653 lp->defined = ST_LABEL_FORMAT;
1655 break;
1657 case ST_LABEL_TARGET:
1658 if (lp->referenced == ST_LABEL_FORMAT)
1659 gfc_error ("Label %d at %C already referenced as a format label",
1660 labelno);
1661 else
1662 lp->defined = ST_LABEL_TARGET;
1664 break;
1666 default:
1667 lp->defined = ST_LABEL_BAD_TARGET;
1668 lp->referenced = ST_LABEL_BAD_TARGET;
1674 /* Reference a label. Given a label and its type, see if that
1675 reference is consistent with what is known about that label,
1676 updating the unknown state. Returns FAILURE if something goes
1677 wrong. */
1680 gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1682 gfc_sl_type label_type;
1683 int labelno;
1684 try rc;
1686 if (lp == NULL)
1687 return SUCCESS;
1689 labelno = lp->value;
1691 if (lp->defined != ST_LABEL_UNKNOWN)
1692 label_type = lp->defined;
1693 else
1695 label_type = lp->referenced;
1696 lp->where = gfc_current_locus;
1699 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1701 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1702 rc = FAILURE;
1703 goto done;
1706 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1707 && type == ST_LABEL_FORMAT)
1709 gfc_error ("Label %d at %C previously used as branch target", labelno);
1710 rc = FAILURE;
1711 goto done;
1714 lp->referenced = type;
1715 rc = SUCCESS;
1717 done:
1718 return rc;
1722 /************** Symbol table management subroutines ****************/
1724 /* Basic details: Fortran 95 requires a potentially unlimited number
1725 of distinct namespaces when compiling a program unit. This case
1726 occurs during a compilation of internal subprograms because all of
1727 the internal subprograms must be read before we can start
1728 generating code for the host.
1730 Given the tricky nature of the Fortran grammar, we must be able to
1731 undo changes made to a symbol table if the current interpretation
1732 of a statement is found to be incorrect. Whenever a symbol is
1733 looked up, we make a copy of it and link to it. All of these
1734 symbols are kept in a singly linked list so that we can commit or
1735 undo the changes at a later time.
1737 A symtree may point to a symbol node outside of its namespace. In
1738 this case, that symbol has been used as a host associated variable
1739 at some previous time. */
1741 /* Allocate a new namespace structure. Copies the implicit types from
1742 PARENT if PARENT_TYPES is set. */
1744 gfc_namespace *
1745 gfc_get_namespace (gfc_namespace * parent, int parent_types)
1747 gfc_namespace *ns;
1748 gfc_typespec *ts;
1749 gfc_intrinsic_op in;
1750 int i;
1752 ns = gfc_getmem (sizeof (gfc_namespace));
1753 ns->sym_root = NULL;
1754 ns->uop_root = NULL;
1755 ns->default_access = ACCESS_UNKNOWN;
1756 ns->parent = parent;
1758 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1759 ns->operator_access[in] = ACCESS_UNKNOWN;
1761 /* Initialize default implicit types. */
1762 for (i = 'a'; i <= 'z'; i++)
1764 ns->set_flag[i - 'a'] = 0;
1765 ts = &ns->default_type[i - 'a'];
1767 if (parent_types && ns->parent != NULL)
1769 /* Copy parent settings */
1770 *ts = ns->parent->default_type[i - 'a'];
1771 continue;
1774 if (gfc_option.flag_implicit_none != 0)
1776 gfc_clear_ts (ts);
1777 continue;
1780 if ('i' <= i && i <= 'n')
1782 ts->type = BT_INTEGER;
1783 ts->kind = gfc_default_integer_kind;
1785 else
1787 ts->type = BT_REAL;
1788 ts->kind = gfc_default_real_kind;
1792 ns->refs = 1;
1794 return ns;
1798 /* Comparison function for symtree nodes. */
1800 static int
1801 compare_symtree (void * _st1, void * _st2)
1803 gfc_symtree *st1, *st2;
1805 st1 = (gfc_symtree *) _st1;
1806 st2 = (gfc_symtree *) _st2;
1808 return strcmp (st1->name, st2->name);
1812 /* Allocate a new symtree node and associate it with the new symbol. */
1814 gfc_symtree *
1815 gfc_new_symtree (gfc_symtree ** root, const char *name)
1817 gfc_symtree *st;
1819 st = gfc_getmem (sizeof (gfc_symtree));
1820 st->name = gfc_get_string (name);
1822 gfc_insert_bbt (root, st, compare_symtree);
1823 return st;
1827 /* Delete a symbol from the tree. Does not free the symbol itself! */
1829 static void
1830 delete_symtree (gfc_symtree ** root, const char *name)
1832 gfc_symtree st, *st0;
1834 st0 = gfc_find_symtree (*root, name);
1836 st.name = gfc_get_string (name);
1837 gfc_delete_bbt (root, &st, compare_symtree);
1839 gfc_free (st0);
1843 /* Given a root symtree node and a name, try to find the symbol within
1844 the namespace. Returns NULL if the symbol is not found. */
1846 gfc_symtree *
1847 gfc_find_symtree (gfc_symtree * st, const char *name)
1849 int c;
1851 while (st != NULL)
1853 c = strcmp (name, st->name);
1854 if (c == 0)
1855 return st;
1857 st = (c < 0) ? st->left : st->right;
1860 return NULL;
1864 /* Given a name find a user operator node, creating it if it doesn't
1865 exist. These are much simpler than symbols because they can't be
1866 ambiguous with one another. */
1868 gfc_user_op *
1869 gfc_get_uop (const char *name)
1871 gfc_user_op *uop;
1872 gfc_symtree *st;
1874 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1875 if (st != NULL)
1876 return st->n.uop;
1878 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1880 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1881 uop->name = gfc_get_string (name);
1882 uop->access = ACCESS_UNKNOWN;
1883 uop->ns = gfc_current_ns;
1885 return uop;
1889 /* Given a name find the user operator node. Returns NULL if it does
1890 not exist. */
1892 gfc_user_op *
1893 gfc_find_uop (const char *name, gfc_namespace * ns)
1895 gfc_symtree *st;
1897 if (ns == NULL)
1898 ns = gfc_current_ns;
1900 st = gfc_find_symtree (ns->uop_root, name);
1901 return (st == NULL) ? NULL : st->n.uop;
1905 /* Remove a gfc_symbol structure and everything it points to. */
1907 void
1908 gfc_free_symbol (gfc_symbol * sym)
1911 if (sym == NULL)
1912 return;
1914 gfc_free_array_spec (sym->as);
1916 free_components (sym->components);
1918 gfc_free_expr (sym->value);
1920 gfc_free_namelist (sym->namelist);
1922 gfc_free_namespace (sym->formal_ns);
1924 gfc_free_interface (sym->generic);
1926 gfc_free_formal_arglist (sym->formal);
1928 gfc_free (sym);
1932 /* Allocate and initialize a new symbol node. */
1934 gfc_symbol *
1935 gfc_new_symbol (const char *name, gfc_namespace * ns)
1937 gfc_symbol *p;
1939 p = gfc_getmem (sizeof (gfc_symbol));
1941 gfc_clear_ts (&p->ts);
1942 gfc_clear_attr (&p->attr);
1943 p->ns = ns;
1945 p->declared_at = gfc_current_locus;
1947 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1948 gfc_internal_error ("new_symbol(): Symbol name too long");
1950 p->name = gfc_get_string (name);
1951 return p;
1955 /* Generate an error if a symbol is ambiguous. */
1957 static void
1958 ambiguous_symbol (const char *name, gfc_symtree * st)
1961 if (st->n.sym->module)
1962 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1963 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1964 else
1965 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1966 "from current program unit", name, st->n.sym->name);
1970 /* Search for a symtree starting in the current namespace, resorting to
1971 any parent namespaces if requested by a nonzero parent_flag.
1972 Returns nonzero if the name is ambiguous. */
1975 gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1976 gfc_symtree ** result)
1978 gfc_symtree *st;
1980 if (ns == NULL)
1981 ns = gfc_current_ns;
1985 st = gfc_find_symtree (ns->sym_root, name);
1986 if (st != NULL)
1988 *result = st;
1989 if (st->ambiguous)
1991 ambiguous_symbol (name, st);
1992 return 1;
1995 return 0;
1998 if (!parent_flag)
1999 break;
2001 ns = ns->parent;
2003 while (ns != NULL);
2005 *result = NULL;
2006 return 0;
2010 /* Same, but returns the symbol instead. */
2013 gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
2014 gfc_symbol ** result)
2016 gfc_symtree *st;
2017 int i;
2019 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2021 if (st == NULL)
2022 *result = NULL;
2023 else
2024 *result = st->n.sym;
2026 return i;
2030 /* Save symbol with the information necessary to back it out. */
2032 static void
2033 save_symbol_data (gfc_symbol * sym)
2036 if (sym->new || sym->old_symbol != NULL)
2037 return;
2039 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
2040 *(sym->old_symbol) = *sym;
2042 sym->tlink = changed_syms;
2043 changed_syms = sym;
2047 /* Given a name, find a symbol, or create it if it does not exist yet
2048 in the current namespace. If the symbol is found we make sure that
2049 it's OK.
2051 The integer return code indicates
2052 0 All OK
2053 1 The symbol name was ambiguous
2054 2 The name meant to be established was already host associated.
2056 So if the return value is nonzero, then an error was issued. */
2059 gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
2061 gfc_symtree *st;
2062 gfc_symbol *p;
2064 /* This doesn't usually happen during resolution. */
2065 if (ns == NULL)
2066 ns = gfc_current_ns;
2068 /* Try to find the symbol in ns. */
2069 st = gfc_find_symtree (ns->sym_root, name);
2071 if (st == NULL)
2073 /* If not there, create a new symbol. */
2074 p = gfc_new_symbol (name, ns);
2076 /* Add to the list of tentative symbols. */
2077 p->old_symbol = NULL;
2078 p->tlink = changed_syms;
2079 p->mark = 1;
2080 p->new = 1;
2081 changed_syms = p;
2083 st = gfc_new_symtree (&ns->sym_root, name);
2084 st->n.sym = p;
2085 p->refs++;
2088 else
2090 /* Make sure the existing symbol is OK. */
2091 if (st->ambiguous)
2093 ambiguous_symbol (name, st);
2094 return 1;
2097 p = st->n.sym;
2099 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2101 /* Symbol is from another namespace. */
2102 gfc_error ("Symbol '%s' at %C has already been host associated",
2103 name);
2104 return 2;
2107 p->mark = 1;
2109 /* Copy in case this symbol is changed. */
2110 save_symbol_data (p);
2113 *result = st;
2114 return 0;
2119 gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
2121 gfc_symtree *st;
2122 int i;
2125 i = gfc_get_sym_tree (name, ns, &st);
2126 if (i != 0)
2127 return i;
2129 if (st)
2130 *result = st->n.sym;
2131 else
2132 *result = NULL;
2133 return i;
2137 /* Subroutine that searches for a symbol, creating it if it doesn't
2138 exist, but tries to host-associate the symbol if possible. */
2141 gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
2143 gfc_symtree *st;
2144 int i;
2146 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2147 if (st != NULL)
2149 save_symbol_data (st->n.sym);
2151 *result = st;
2152 return i;
2155 if (gfc_current_ns->parent != NULL)
2157 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2158 if (i)
2159 return i;
2161 if (st != NULL)
2163 *result = st;
2164 return 0;
2168 return gfc_get_sym_tree (name, gfc_current_ns, result);
2173 gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
2175 int i;
2176 gfc_symtree *st;
2178 i = gfc_get_ha_sym_tree (name, &st);
2180 if (st)
2181 *result = st->n.sym;
2182 else
2183 *result = NULL;
2185 return i;
2188 /* Return true if both symbols could refer to the same data object. Does
2189 not take account of aliasing due to equivalence statements. */
2192 gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
2194 /* Aliasing isn't possible if the symbols have different base types. */
2195 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2196 return 0;
2198 /* Pointers can point to other pointers, target objects and allocatable
2199 objects. Two allocatable objects cannot share the same storage. */
2200 if (lsym->attr.pointer
2201 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2202 return 1;
2203 if (lsym->attr.target && rsym->attr.pointer)
2204 return 1;
2205 if (lsym->attr.allocatable && rsym->attr.pointer)
2206 return 1;
2208 return 0;
2212 /* Undoes all the changes made to symbols in the current statement.
2213 This subroutine is made simpler due to the fact that attributes are
2214 never removed once added. */
2216 void
2217 gfc_undo_symbols (void)
2219 gfc_symbol *p, *q, *old;
2221 for (p = changed_syms; p; p = q)
2223 q = p->tlink;
2225 if (p->new)
2227 /* Symbol was new. */
2228 delete_symtree (&p->ns->sym_root, p->name);
2230 p->refs--;
2231 if (p->refs < 0)
2232 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2233 if (p->refs == 0)
2234 gfc_free_symbol (p);
2235 continue;
2238 /* Restore previous state of symbol. Just copy simple stuff. */
2239 p->mark = 0;
2240 old = p->old_symbol;
2242 p->ts.type = old->ts.type;
2243 p->ts.kind = old->ts.kind;
2245 p->attr = old->attr;
2247 if (p->value != old->value)
2249 gfc_free_expr (old->value);
2250 p->value = NULL;
2253 if (p->as != old->as)
2255 if (p->as)
2256 gfc_free_array_spec (p->as);
2257 p->as = old->as;
2260 p->generic = old->generic;
2261 p->component_access = old->component_access;
2263 if (p->namelist != NULL && old->namelist == NULL)
2265 gfc_free_namelist (p->namelist);
2266 p->namelist = NULL;
2268 else
2271 if (p->namelist_tail != old->namelist_tail)
2273 gfc_free_namelist (old->namelist_tail);
2274 old->namelist_tail->next = NULL;
2278 p->namelist_tail = old->namelist_tail;
2280 if (p->formal != old->formal)
2282 gfc_free_formal_arglist (p->formal);
2283 p->formal = old->formal;
2286 gfc_free (p->old_symbol);
2287 p->old_symbol = NULL;
2288 p->tlink = NULL;
2291 changed_syms = NULL;
2295 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2296 components of old_symbol that might need deallocation are the "allocatables"
2297 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2298 namelist_tail. In case these differ between old_symbol and sym, it's just
2299 because sym->namelist has gotten a few more items. */
2301 static void
2302 free_old_symbol (gfc_symbol * sym)
2304 if (sym->old_symbol == NULL)
2305 return;
2307 if (sym->old_symbol->as != sym->as)
2308 gfc_free_array_spec (sym->old_symbol->as);
2310 if (sym->old_symbol->value != sym->value)
2311 gfc_free_expr (sym->old_symbol->value);
2313 if (sym->old_symbol->formal != sym->formal)
2314 gfc_free_formal_arglist (sym->old_symbol->formal);
2316 gfc_free (sym->old_symbol);
2317 sym->old_symbol = NULL;
2321 /* Makes the changes made in the current statement permanent-- gets
2322 rid of undo information. */
2324 void
2325 gfc_commit_symbols (void)
2327 gfc_symbol *p, *q;
2329 for (p = changed_syms; p; p = q)
2331 q = p->tlink;
2332 p->tlink = NULL;
2333 p->mark = 0;
2334 p->new = 0;
2336 free_old_symbol (p);
2338 changed_syms = NULL;
2342 /* Makes the changes made in one symbol permanent -- gets rid of undo
2343 information. */
2345 void
2346 gfc_commit_symbol (gfc_symbol * sym)
2348 gfc_symbol *p;
2350 if (changed_syms == sym)
2351 changed_syms = sym->tlink;
2352 else
2354 for (p = changed_syms; p; p = p->tlink)
2355 if (p->tlink == sym)
2357 p->tlink = sym->tlink;
2358 break;
2362 sym->tlink = NULL;
2363 sym->mark = 0;
2364 sym->new = 0;
2366 free_old_symbol (sym);
2370 /* Recursive function that deletes an entire tree and all the common
2371 head structures it points to. */
2373 static void
2374 free_common_tree (gfc_symtree * common_tree)
2376 if (common_tree == NULL)
2377 return;
2379 free_common_tree (common_tree->left);
2380 free_common_tree (common_tree->right);
2382 gfc_free (common_tree);
2386 /* Recursive function that deletes an entire tree and all the user
2387 operator nodes that it contains. */
2389 static void
2390 free_uop_tree (gfc_symtree * uop_tree)
2393 if (uop_tree == NULL)
2394 return;
2396 free_uop_tree (uop_tree->left);
2397 free_uop_tree (uop_tree->right);
2399 gfc_free_interface (uop_tree->n.uop->operator);
2401 gfc_free (uop_tree->n.uop);
2402 gfc_free (uop_tree);
2406 /* Recursive function that deletes an entire tree and all the symbols
2407 that it contains. */
2409 static void
2410 free_sym_tree (gfc_symtree * sym_tree)
2412 gfc_namespace *ns;
2413 gfc_symbol *sym;
2415 if (sym_tree == NULL)
2416 return;
2418 free_sym_tree (sym_tree->left);
2419 free_sym_tree (sym_tree->right);
2421 sym = sym_tree->n.sym;
2423 sym->refs--;
2424 if (sym->refs < 0)
2425 gfc_internal_error ("free_sym_tree(): Negative refs");
2427 if (sym->formal_ns != NULL && sym->refs == 1)
2429 /* As formal_ns contains a reference to sym, delete formal_ns just
2430 before the deletion of sym. */
2431 ns = sym->formal_ns;
2432 sym->formal_ns = NULL;
2433 gfc_free_namespace (ns);
2435 else if (sym->refs == 0)
2437 /* Go ahead and delete the symbol. */
2438 gfc_free_symbol (sym);
2441 gfc_free (sym_tree);
2445 /* Free a derived type list. */
2447 static void
2448 gfc_free_dt_list (gfc_dt_list * dt)
2450 gfc_dt_list *n;
2452 for (; dt; dt = n)
2454 n = dt->next;
2455 gfc_free (dt);
2460 /* Free the gfc_equiv_info's. */
2462 static void
2463 gfc_free_equiv_infos (gfc_equiv_info * s)
2465 if (s == NULL)
2466 return;
2467 gfc_free_equiv_infos (s->next);
2468 gfc_free (s);
2472 /* Free the gfc_equiv_lists. */
2474 static void
2475 gfc_free_equiv_lists (gfc_equiv_list * l)
2477 if (l == NULL)
2478 return;
2479 gfc_free_equiv_lists (l->next);
2480 gfc_free_equiv_infos (l->equiv);
2481 gfc_free (l);
2485 /* Free a namespace structure and everything below it. Interface
2486 lists associated with intrinsic operators are not freed. These are
2487 taken care of when a specific name is freed. */
2489 void
2490 gfc_free_namespace (gfc_namespace * ns)
2492 gfc_charlen *cl, *cl2;
2493 gfc_namespace *p, *q;
2494 gfc_intrinsic_op i;
2496 if (ns == NULL)
2497 return;
2499 ns->refs--;
2500 if (ns->refs > 0)
2501 return;
2502 gcc_assert (ns->refs == 0);
2504 gfc_free_statements (ns->code);
2506 free_sym_tree (ns->sym_root);
2507 free_uop_tree (ns->uop_root);
2508 free_common_tree (ns->common_root);
2510 for (cl = ns->cl_list; cl; cl = cl2)
2512 cl2 = cl->next;
2513 gfc_free_expr (cl->length);
2514 gfc_free (cl);
2517 free_st_labels (ns->st_labels);
2519 gfc_free_equiv (ns->equiv);
2520 gfc_free_equiv_lists (ns->equiv_lists);
2522 gfc_free_dt_list (ns->derived_types);
2524 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2525 gfc_free_interface (ns->operator[i]);
2527 gfc_free_data (ns->data);
2528 p = ns->contained;
2529 gfc_free (ns);
2531 /* Recursively free any contained namespaces. */
2532 while (p != NULL)
2534 q = p;
2535 p = p->sibling;
2537 gfc_free_namespace (q);
2542 void
2543 gfc_symbol_init_2 (void)
2546 gfc_current_ns = gfc_get_namespace (NULL, 0);
2550 void
2551 gfc_symbol_done_2 (void)
2554 gfc_free_namespace (gfc_current_ns);
2555 gfc_current_ns = NULL;
2559 /* Clear mark bits from symbol nodes associated with a symtree node. */
2561 static void
2562 clear_sym_mark (gfc_symtree * st)
2565 st->n.sym->mark = 0;
2569 /* Recursively traverse the symtree nodes. */
2571 void
2572 gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2574 if (st != NULL)
2576 (*func) (st);
2578 gfc_traverse_symtree (st->left, func);
2579 gfc_traverse_symtree (st->right, func);
2584 /* Recursive namespace traversal function. */
2586 static void
2587 traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2590 if (st == NULL)
2591 return;
2593 if (st->n.sym->mark == 0)
2594 (*func) (st->n.sym);
2595 st->n.sym->mark = 1;
2597 traverse_ns (st->left, func);
2598 traverse_ns (st->right, func);
2602 /* Call a given function for all symbols in the namespace. We take
2603 care that each gfc_symbol node is called exactly once. */
2605 void
2606 gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2609 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2611 traverse_ns (ns->sym_root, func);
2615 /* Return TRUE if the symbol is an automatic variable. */
2616 static bool
2617 gfc_is_var_automatic (gfc_symbol * sym)
2619 /* Pointer and allocatable variables are never automatic. */
2620 if (sym->attr.pointer || sym->attr.allocatable)
2621 return false;
2622 /* Check for arrays with non-constant size. */
2623 if (sym->attr.dimension && sym->as
2624 && !gfc_is_compile_time_shape (sym->as))
2625 return true;
2626 /* Check for non-constant length character variables. */
2627 if (sym->ts.type == BT_CHARACTER
2628 && sym->ts.cl
2629 && !gfc_is_constant_expr (sym->ts.cl->length))
2630 return true;
2631 return false;
2634 /* Given a symbol, mark it as SAVEd if it is allowed. */
2636 static void
2637 save_symbol (gfc_symbol * sym)
2640 if (sym->attr.use_assoc)
2641 return;
2643 if (sym->attr.in_common
2644 || sym->attr.dummy
2645 || sym->attr.flavor != FL_VARIABLE)
2646 return;
2647 /* Automatic objects are not saved. */
2648 if (gfc_is_var_automatic (sym))
2649 return;
2650 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2654 /* Mark those symbols which can be SAVEd as such. */
2656 void
2657 gfc_save_all (gfc_namespace * ns)
2660 gfc_traverse_ns (ns, save_symbol);
2664 #ifdef GFC_DEBUG
2665 /* Make sure that no changes to symbols are pending. */
2667 void
2668 gfc_symbol_state(void) {
2670 if (changed_syms != NULL)
2671 gfc_internal_error("Symbol changes still pending!");
2673 #endif
2676 /************** Global symbol handling ************/
2679 /* Search a tree for the global symbol. */
2681 gfc_gsymbol *
2682 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2684 gfc_gsymbol *s;
2686 if (symbol == NULL)
2687 return NULL;
2688 if (strcmp (symbol->name, name) == 0)
2689 return symbol;
2691 s = gfc_find_gsymbol (symbol->left, name);
2692 if (s != NULL)
2693 return s;
2695 s = gfc_find_gsymbol (symbol->right, name);
2696 if (s != NULL)
2697 return s;
2699 return NULL;
2703 /* Compare two global symbols. Used for managing the BB tree. */
2705 static int
2706 gsym_compare (void * _s1, void * _s2)
2708 gfc_gsymbol *s1, *s2;
2710 s1 = (gfc_gsymbol *)_s1;
2711 s2 = (gfc_gsymbol *)_s2;
2712 return strcmp(s1->name, s2->name);
2716 /* Get a global symbol, creating it if it doesn't exist. */
2718 gfc_gsymbol *
2719 gfc_get_gsymbol (const char *name)
2721 gfc_gsymbol *s;
2723 s = gfc_find_gsymbol (gfc_gsym_root, name);
2724 if (s != NULL)
2725 return s;
2727 s = gfc_getmem (sizeof (gfc_gsymbol));
2728 s->type = GSYM_UNKNOWN;
2729 s->name = gfc_get_string (name);
2731 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2733 return s;