1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
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
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
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
30 /* Strings for all symbol attributes. We use these for dumping the
31 parse tree, in error messages, and also when reading and writing
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
),
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
),
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
),
65 const mstring access_types
[] =
67 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN
),
68 minit ("PUBLIC", ACCESS_PUBLIC
),
69 minit ("PRIVATE", ACCESS_PRIVATE
),
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
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. */
106 gfc_set_implicit_none (void)
110 if (gfc_current_ns
->seen_implicit_none
)
112 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
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. */
129 gfc_clear_new_implicit (void)
133 for (i
= 0; i
< GFC_LETTERS
; i
++)
138 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
141 gfc_add_new_implicit_range (int c1
, int c2
)
148 for (i
= c1
; i
<= c2
; i
++)
152 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
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
)
172 if (gfc_current_ns
->seen_implicit_none
)
174 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
178 for (i
= 0; i
< GFC_LETTERS
; i
++)
183 if (gfc_current_ns
->set_flag
[i
])
185 gfc_error ("Letter %c already has an IMPLICIT type at %C",
189 gfc_current_ns
->default_type
[i
] = *ts
;
190 gfc_current_ns
->set_flag
[i
] = 1;
197 /* Given a symbol, return a pointer to the typespec for its default type. */
200 gfc_get_default_type (gfc_symbol
* sym
, gfc_namespace
* ns
)
204 letter
= sym
->name
[0];
205 if (letter
< 'a' || letter
> 'z')
206 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
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
220 gfc_set_default_type (gfc_symbol
* sym
, int error_flag
, gfc_namespace
* ns
)
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. */
242 sym
->attr
.implicit_type
= 1;
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)\
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";
282 where
= &gfc_current_locus
;
284 if (attr
->pointer
&& attr
->intent
!= INTENT_UNKNOWN
)
291 /* Check for attributes not allowed in a BLOCK DATA. */
292 if (gfc_current_state () == COMP_BLOCK_DATA
)
296 if (attr
->in_namelist
)
298 if (attr
->allocatable
)
304 if (attr
->access
== ACCESS_PRIVATE
)
306 if (attr
->access
== ACCESS_PUBLIC
)
308 if (attr
->intent
!= INTENT_UNKNOWN
)
314 ("%s attribute not allowed in BLOCK DATA program unit at %L", a1
,
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
);
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
);
397 conf (data
, function
);
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
)
413 switch (attr
->flavor
)
431 conf2 (threadprivate
);
441 if (attr
->subroutine
)
450 conf2(threadprivate
);
455 case PROC_ST_FUNCTION
:
468 conf2 (threadprivate
);
489 conf2 (threadprivate
);
491 if (attr
->intent
!= INTENT_UNKNOWN
)
511 conf2 (threadprivate
);
522 gfc_error ("%s attribute conflicts with %s attribute at %L",
525 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
526 a1
, a2
, name
, where
);
533 return gfc_notify_std (standard
, "In the selected standard, %s attribute "
534 "conflicts with %s attribute at %L", a1
, a2
,
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
);
550 /* Mark a symbol as referenced. */
553 gfc_set_sym_referenced (gfc_symbol
* sym
)
555 if (sym
->attr
.referenced
)
558 sym
->attr
.referenced
= 1;
560 /* Remember which order dummy variables are accessed in. */
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,
572 check_used (symbol_attribute
* attr
, const char * name
, locus
* where
)
575 if (attr
->use_assoc
== 0)
579 where
= &gfc_current_locus
;
582 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
585 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
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. */
598 check_done (symbol_attribute
* attr
, locus
* where
)
601 if (!(attr
->dummy
&& attr
->referenced
))
605 where
= &gfc_current_locus
;
607 gfc_error ("Cannot change attributes of symbol at %L"
608 " after it has been used", where
);
614 /* Generate an error because of a duplicate attribute. */
617 duplicate_attr (const char *attr
, locus
* where
)
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
)))
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
))
647 if (attr
->allocatable
)
649 duplicate_attr ("ALLOCATABLE", where
);
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
))
667 duplicate_attr ("DIMENSION", where
);
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
))
685 duplicate_attr ("EXTERNAL", where
);
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
))
704 duplicate_attr ("INTRINSIC", where
);
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
))
723 duplicate_attr ("OPTIONAL", where
);
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
))
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
))
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
))
763 if (attr
->cray_pointee
)
765 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
766 " statements.", where
);
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
))
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
))
797 ("SAVE attribute at %L cannot be specified in a PURE procedure",
804 if (gfc_notify_std (GFC_STD_LEGACY
,
805 "Duplicate SAVE attribute specified at %L",
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
))
822 if (attr
->threadprivate
)
824 duplicate_attr ("THREADPRIVATE", where
);
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
))
842 duplicate_attr ("TARGET", where
);
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
))
858 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
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
))
871 /* Duplicate attribute already checked for. */
873 if (check_conflict (attr
, name
, where
) == FAILURE
)
876 if (attr
->flavor
== FL_VARIABLE
)
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
)
891 if (attr
->flavor
== FL_VARIABLE
)
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
))
906 return check_conflict (attr
, name
, where
);
911 gfc_add_in_namelist (symbol_attribute
* attr
, const char *name
,
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
))
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
))
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
))
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
))
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
))
977 duplicate_attr ("ENTRY", where
);
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
)
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
)
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
)
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
,
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
))
1038 if (attr
->flavor
== f
&& f
== FL_VARIABLE
)
1041 if (attr
->flavor
!= FL_UNKNOWN
)
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
);
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
))
1067 if (attr
->flavor
!= FL_PROCEDURE
1068 && gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
) == FAILURE
)
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
));
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
))
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
))
1102 if (attr
->intent
== INTENT_UNKNOWN
)
1104 attr
->intent
= intent
;
1105 return check_conflict (attr
, NULL
, where
);
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
);
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
);
1133 where
= &gfc_current_locus
;
1134 gfc_error ("ACCESS specification at %L was already specified", where
);
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
))
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",
1159 sym
->formal
= formal
;
1160 sym
->attr
.if_source
= source
;
1166 /* Add a type to a symbol. */
1169 gfc_add_type (gfc_symbol
* sym
, gfc_typespec
* ts
, locus
* where
)
1173 /* TODO: This is legal if it is reaffirming an implicit type.
1174 if (check_done (&sym->attr, where))
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
1188 gfc_error (msg
, sym
->name
, where
, gfc_basic_typename (sym
->ts
.type
));
1191 else if (gfc_notify_std (GFC_STD_GNU
, msg
, sym
->name
, where
,
1192 gfc_basic_typename (sym
->ts
.type
)) == 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
);
1212 /* Clears all attributes. */
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
)
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
)
1244 if (src
->dimension
&& gfc_add_dimension (dest
, NULL
, where
) == FAILURE
)
1246 if (src
->optional
&& gfc_add_optional (dest
, where
) == FAILURE
)
1248 if (src
->pointer
&& gfc_add_pointer (dest
, where
) == FAILURE
)
1250 if (src
->save
&& gfc_add_save (dest
, NULL
, where
) == FAILURE
)
1252 if (src
->threadprivate
&& gfc_add_threadprivate (dest
, NULL
, where
) == FAILURE
)
1254 if (src
->target
&& gfc_add_target (dest
, where
) == FAILURE
)
1256 if (src
->dummy
&& gfc_add_dummy (dest
, NULL
, where
) == FAILURE
)
1258 if (src
->result
&& gfc_add_result (dest
, NULL
, where
) == FAILURE
)
1263 if (src
->in_namelist
&& gfc_add_in_namelist (dest
, NULL
, where
) == FAILURE
)
1266 if (src
->in_common
&& gfc_add_in_common (dest
, NULL
, where
) == FAILURE
)
1269 if (src
->generic
&& gfc_add_generic (dest
, NULL
, where
) == FAILURE
)
1271 if (src
->function
&& gfc_add_function (dest
, NULL
, where
) == FAILURE
)
1273 if (src
->subroutine
&& gfc_add_subroutine (dest
, NULL
, where
) == FAILURE
)
1276 if (src
->sequence
&& gfc_add_sequence (dest
, NULL
, where
) == FAILURE
)
1278 if (src
->elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
1280 if (src
->pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
1282 if (src
->recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
1285 if (src
->flavor
!= FL_UNKNOWN
1286 && gfc_add_flavor (dest
, src
->flavor
, NULL
, where
) == FAILURE
)
1289 if (src
->intent
!= INTENT_UNKNOWN
1290 && gfc_add_intent (dest
, src
->intent
, where
) == FAILURE
)
1293 if (src
->access
!= ACCESS_UNKNOWN
1294 && gfc_add_access (dest
, src
->access
, NULL
, where
) == FAILURE
)
1297 if (gfc_missing_attr (dest
, where
) == FAILURE
)
1300 if (src
->cray_pointer
&& gfc_add_cray_pointer (dest
, where
) == FAILURE
)
1302 if (src
->cray_pointee
&& gfc_add_cray_pointee (dest
, where
) == FAILURE
)
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
1311 dest
->intrinsic
= 1;
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
;
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",
1351 /* Allocate a new component. */
1352 p
= gfc_get_component ();
1355 sym
->components
= p
;
1359 p
->name
= gfc_get_string (name
);
1360 p
->loc
= gfc_current_locus
;
1367 /* Recursive search for a renamed derived type. */
1370 find_renamed_type (gfc_symbol
* der
, gfc_symtree
* st
)
1372 gfc_symbol
*sym
= NULL
;
1377 sym
= find_renamed_type (der
, st
->left
);
1381 sym
= find_renamed_type (der
, st
->right
);
1385 if (strcmp (der
->name
, st
->n
.sym
->name
) == 0
1386 && st
->n
.sym
->attr
.use_assoc
1387 && st
->n
.sym
->attr
.flavor
== FL_DERIVED
1388 && gfc_compare_derived_types (der
, st
->n
.sym
))
1394 /* Recursive function to switch derived types of all symbol in a
1398 switch_types (gfc_symtree
* st
, gfc_symbol
* from
, gfc_symbol
* to
)
1406 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.derived
== from
)
1407 sym
->ts
.derived
= to
;
1409 switch_types (st
->left
, from
, to
);
1410 switch_types (st
->right
, from
, to
);
1414 /* This subroutine is called when a derived type is used in order to
1415 make the final determination about which version to use. The
1416 standard requires that a type be defined before it is 'used', but
1417 such types can appear in IMPLICIT statements before the actual
1418 definition. 'Using' in this context means declaring a variable to
1419 be that type or using the type constructor.
1421 If a type is used and the components haven't been defined, then we
1422 have to have a derived type in a parent unit. We find the node in
1423 the other namespace and point the symtree node in this namespace to
1424 that node. Further reference to this name point to the correct
1425 node. If we can't find the node in a parent namespace, then we have
1428 This subroutine takes a pointer to a symbol node and returns a
1429 pointer to the translated node or NULL for an error. Usually there
1430 is no translation and we return the node we were passed. */
1433 gfc_use_derived (gfc_symbol
* sym
)
1441 if (sym
->ns
->parent
== NULL
)
1443 /* Already defined in highest possible namespace. */
1444 if (sym
->components
!= NULL
)
1447 /* There is no scope for finding a definition elsewhere. */
1453 /* This type can only be locally associated. */
1454 if (!(sym
->attr
.use_assoc
|| sym
->attr
.sequence
))
1457 /* Derived types must be defined within an interface. */
1458 if (gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
1462 /* Look in parent namespace for a derived type of the same name. */
1463 if (gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
))
1465 gfc_error ("Symbol '%s' at %C is ambiguous", sym
->name
);
1469 if (s
== NULL
|| s
->attr
.flavor
!= FL_DERIVED
)
1471 /* Check to see if type has been renamed in parent namespace.
1472 Leave cleanup of local symbols until the end of the
1473 compilation because doing it here is complicated by
1474 multiple association with the same type. */
1475 s
= find_renamed_type (sym
, sym
->ns
->parent
->sym_root
);
1478 switch_types (sym
->ns
->sym_root
, sym
, s
);
1482 /* The local definition is all that there is. */
1483 if (sym
->components
!= NULL
)
1485 /* Non-pointer derived type components have already been checked
1486 but pointer types need to be correctly associated. */
1487 for (c
= sym
->components
; c
; c
= c
->next
)
1488 if (c
->ts
.type
== BT_DERIVED
&& c
->pointer
)
1489 c
->ts
.derived
= gfc_use_derived (c
->ts
.derived
);
1495 /* Although the parent namespace has a derived type of the same name, it is
1496 not an identical derived type and so cannot be used. */
1497 if (s
!= NULL
&& sym
->components
!= NULL
&& !gfc_compare_derived_types (s
, sym
))
1500 if (s
== NULL
|| s
->attr
.flavor
!= FL_DERIVED
)
1503 /* Get rid of symbol sym, translating all references to s. */
1504 for (i
= 0; i
< GFC_LETTERS
; i
++)
1506 t
= &sym
->ns
->default_type
[i
];
1507 if (t
->derived
== sym
)
1511 st
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
1516 /* Unlink from list of modified symbols. */
1517 gfc_commit_symbol (sym
);
1519 switch_types (sym
->ns
->sym_root
, sym
, s
);
1521 /* TODO: Also have to replace sym -> s in other lists like
1522 namelists, common lists and interface lists. */
1523 gfc_free_symbol (sym
);
1528 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1534 /* Given a derived type node and a component name, try to locate the
1535 component structure. Returns the NULL pointer if the component is
1536 not found or the components are private. */
1539 gfc_find_component (gfc_symbol
* sym
, const char *name
)
1546 sym
= gfc_use_derived (sym
);
1551 for (p
= sym
->components
; p
; p
= p
->next
)
1552 if (strcmp (p
->name
, name
) == 0)
1556 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1560 if (sym
->attr
.use_assoc
&& sym
->component_access
== ACCESS_PRIVATE
)
1562 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1572 /* Given a symbol, free all of the component structures and everything
1576 free_components (gfc_component
* p
)
1584 gfc_free_array_spec (p
->as
);
1585 gfc_free_expr (p
->initializer
);
1592 /* Set component attributes from a standard symbol attribute
1596 gfc_set_component_attr (gfc_component
* c
, symbol_attribute
* attr
)
1599 c
->dimension
= attr
->dimension
;
1600 c
->pointer
= attr
->pointer
;
1604 /* Get a standard symbol attribute structure given the component
1608 gfc_get_component_attr (symbol_attribute
* attr
, gfc_component
* c
)
1611 gfc_clear_attr (attr
);
1612 attr
->dimension
= c
->dimension
;
1613 attr
->pointer
= c
->pointer
;
1617 /******************** Statement label management ********************/
1619 /* Comparison function for statement labels, used for managing the
1623 compare_st_labels (void * a1
, void * b1
)
1625 int a
= ((gfc_st_label
*)a1
)->value
;
1626 int b
= ((gfc_st_label
*)b1
)->value
;
1632 /* Free a single gfc_st_label structure, making sure the tree is not
1633 messed up. This function is called only when some parse error
1637 gfc_free_st_label (gfc_st_label
* label
)
1642 gfc_delete_bbt (&gfc_current_ns
->st_labels
, label
, compare_st_labels
);
1644 if (label
->format
!= NULL
)
1645 gfc_free_expr (label
->format
);
1650 /* Free a whole tree of gfc_st_label structures. */
1653 free_st_labels (gfc_st_label
* label
)
1658 free_st_labels (label
->left
);
1659 free_st_labels (label
->right
);
1661 if (label
->format
!= NULL
)
1662 gfc_free_expr (label
->format
);
1667 /* Given a label number, search for and return a pointer to the label
1668 structure, creating it if it does not exist. */
1671 gfc_get_st_label (int labelno
)
1675 /* First see if the label is already in this namespace. */
1676 lp
= gfc_current_ns
->st_labels
;
1679 if (lp
->value
== labelno
)
1682 if (lp
->value
< labelno
)
1688 lp
= gfc_getmem (sizeof (gfc_st_label
));
1690 lp
->value
= labelno
;
1691 lp
->defined
= ST_LABEL_UNKNOWN
;
1692 lp
->referenced
= ST_LABEL_UNKNOWN
;
1694 gfc_insert_bbt (&gfc_current_ns
->st_labels
, lp
, compare_st_labels
);
1700 /* Called when a statement with a statement label is about to be
1701 accepted. We add the label to the list of the current namespace,
1702 making sure it hasn't been defined previously and referenced
1706 gfc_define_st_label (gfc_st_label
* lp
, gfc_sl_type type
, locus
* label_locus
)
1710 labelno
= lp
->value
;
1712 if (lp
->defined
!= ST_LABEL_UNKNOWN
)
1713 gfc_error ("Duplicate statement label %d at %L and %L", labelno
,
1714 &lp
->where
, label_locus
);
1717 lp
->where
= *label_locus
;
1721 case ST_LABEL_FORMAT
:
1722 if (lp
->referenced
== ST_LABEL_TARGET
)
1723 gfc_error ("Label %d at %C already referenced as branch target",
1726 lp
->defined
= ST_LABEL_FORMAT
;
1730 case ST_LABEL_TARGET
:
1731 if (lp
->referenced
== ST_LABEL_FORMAT
)
1732 gfc_error ("Label %d at %C already referenced as a format label",
1735 lp
->defined
= ST_LABEL_TARGET
;
1740 lp
->defined
= ST_LABEL_BAD_TARGET
;
1741 lp
->referenced
= ST_LABEL_BAD_TARGET
;
1747 /* Reference a label. Given a label and its type, see if that
1748 reference is consistent with what is known about that label,
1749 updating the unknown state. Returns FAILURE if something goes
1753 gfc_reference_st_label (gfc_st_label
* lp
, gfc_sl_type type
)
1755 gfc_sl_type label_type
;
1762 labelno
= lp
->value
;
1764 if (lp
->defined
!= ST_LABEL_UNKNOWN
)
1765 label_type
= lp
->defined
;
1768 label_type
= lp
->referenced
;
1769 lp
->where
= gfc_current_locus
;
1772 if (label_type
== ST_LABEL_FORMAT
&& type
== ST_LABEL_TARGET
)
1774 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno
);
1779 if ((label_type
== ST_LABEL_TARGET
|| label_type
== ST_LABEL_BAD_TARGET
)
1780 && type
== ST_LABEL_FORMAT
)
1782 gfc_error ("Label %d at %C previously used as branch target", labelno
);
1787 lp
->referenced
= type
;
1795 /************** Symbol table management subroutines ****************/
1797 /* Basic details: Fortran 95 requires a potentially unlimited number
1798 of distinct namespaces when compiling a program unit. This case
1799 occurs during a compilation of internal subprograms because all of
1800 the internal subprograms must be read before we can start
1801 generating code for the host.
1803 Given the tricky nature of the Fortran grammar, we must be able to
1804 undo changes made to a symbol table if the current interpretation
1805 of a statement is found to be incorrect. Whenever a symbol is
1806 looked up, we make a copy of it and link to it. All of these
1807 symbols are kept in a singly linked list so that we can commit or
1808 undo the changes at a later time.
1810 A symtree may point to a symbol node outside of its namespace. In
1811 this case, that symbol has been used as a host associated variable
1812 at some previous time. */
1814 /* Allocate a new namespace structure. Copies the implicit types from
1815 PARENT if PARENT_TYPES is set. */
1818 gfc_get_namespace (gfc_namespace
* parent
, int parent_types
)
1822 gfc_intrinsic_op in
;
1825 ns
= gfc_getmem (sizeof (gfc_namespace
));
1826 ns
->sym_root
= NULL
;
1827 ns
->uop_root
= NULL
;
1828 ns
->default_access
= ACCESS_UNKNOWN
;
1829 ns
->parent
= parent
;
1831 for (in
= GFC_INTRINSIC_BEGIN
; in
!= GFC_INTRINSIC_END
; in
++)
1832 ns
->operator_access
[in
] = ACCESS_UNKNOWN
;
1834 /* Initialize default implicit types. */
1835 for (i
= 'a'; i
<= 'z'; i
++)
1837 ns
->set_flag
[i
- 'a'] = 0;
1838 ts
= &ns
->default_type
[i
- 'a'];
1840 if (parent_types
&& ns
->parent
!= NULL
)
1842 /* Copy parent settings */
1843 *ts
= ns
->parent
->default_type
[i
- 'a'];
1847 if (gfc_option
.flag_implicit_none
!= 0)
1853 if ('i' <= i
&& i
<= 'n')
1855 ts
->type
= BT_INTEGER
;
1856 ts
->kind
= gfc_default_integer_kind
;
1861 ts
->kind
= gfc_default_real_kind
;
1871 /* Comparison function for symtree nodes. */
1874 compare_symtree (void * _st1
, void * _st2
)
1876 gfc_symtree
*st1
, *st2
;
1878 st1
= (gfc_symtree
*) _st1
;
1879 st2
= (gfc_symtree
*) _st2
;
1881 return strcmp (st1
->name
, st2
->name
);
1885 /* Allocate a new symtree node and associate it with the new symbol. */
1888 gfc_new_symtree (gfc_symtree
** root
, const char *name
)
1892 st
= gfc_getmem (sizeof (gfc_symtree
));
1893 st
->name
= gfc_get_string (name
);
1895 gfc_insert_bbt (root
, st
, compare_symtree
);
1900 /* Delete a symbol from the tree. Does not free the symbol itself! */
1903 delete_symtree (gfc_symtree
** root
, const char *name
)
1905 gfc_symtree st
, *st0
;
1907 st0
= gfc_find_symtree (*root
, name
);
1909 st
.name
= gfc_get_string (name
);
1910 gfc_delete_bbt (root
, &st
, compare_symtree
);
1916 /* Given a root symtree node and a name, try to find the symbol within
1917 the namespace. Returns NULL if the symbol is not found. */
1920 gfc_find_symtree (gfc_symtree
* st
, const char *name
)
1926 c
= strcmp (name
, st
->name
);
1930 st
= (c
< 0) ? st
->left
: st
->right
;
1937 /* Given a name find a user operator node, creating it if it doesn't
1938 exist. These are much simpler than symbols because they can't be
1939 ambiguous with one another. */
1942 gfc_get_uop (const char *name
)
1947 st
= gfc_find_symtree (gfc_current_ns
->uop_root
, name
);
1951 st
= gfc_new_symtree (&gfc_current_ns
->uop_root
, name
);
1953 uop
= st
->n
.uop
= gfc_getmem (sizeof (gfc_user_op
));
1954 uop
->name
= gfc_get_string (name
);
1955 uop
->access
= ACCESS_UNKNOWN
;
1956 uop
->ns
= gfc_current_ns
;
1962 /* Given a name find the user operator node. Returns NULL if it does
1966 gfc_find_uop (const char *name
, gfc_namespace
* ns
)
1971 ns
= gfc_current_ns
;
1973 st
= gfc_find_symtree (ns
->uop_root
, name
);
1974 return (st
== NULL
) ? NULL
: st
->n
.uop
;
1978 /* Remove a gfc_symbol structure and everything it points to. */
1981 gfc_free_symbol (gfc_symbol
* sym
)
1987 gfc_free_array_spec (sym
->as
);
1989 free_components (sym
->components
);
1991 gfc_free_expr (sym
->value
);
1993 gfc_free_namelist (sym
->namelist
);
1995 gfc_free_namespace (sym
->formal_ns
);
1997 gfc_free_interface (sym
->generic
);
1999 gfc_free_formal_arglist (sym
->formal
);
2005 /* Allocate and initialize a new symbol node. */
2008 gfc_new_symbol (const char *name
, gfc_namespace
* ns
)
2012 p
= gfc_getmem (sizeof (gfc_symbol
));
2014 gfc_clear_ts (&p
->ts
);
2015 gfc_clear_attr (&p
->attr
);
2018 p
->declared_at
= gfc_current_locus
;
2020 if (strlen (name
) > GFC_MAX_SYMBOL_LEN
)
2021 gfc_internal_error ("new_symbol(): Symbol name too long");
2023 p
->name
= gfc_get_string (name
);
2028 /* Generate an error if a symbol is ambiguous. */
2031 ambiguous_symbol (const char *name
, gfc_symtree
* st
)
2034 if (st
->n
.sym
->module
)
2035 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2036 "from module '%s'", name
, st
->n
.sym
->name
, st
->n
.sym
->module
);
2038 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2039 "from current program unit", name
, st
->n
.sym
->name
);
2043 /* Search for a symtree starting in the current namespace, resorting to
2044 any parent namespaces if requested by a nonzero parent_flag.
2045 Returns nonzero if the name is ambiguous. */
2048 gfc_find_sym_tree (const char *name
, gfc_namespace
* ns
, int parent_flag
,
2049 gfc_symtree
** result
)
2054 ns
= gfc_current_ns
;
2058 st
= gfc_find_symtree (ns
->sym_root
, name
);
2064 ambiguous_symbol (name
, st
);
2083 /* Same, but returns the symbol instead. */
2086 gfc_find_symbol (const char *name
, gfc_namespace
* ns
, int parent_flag
,
2087 gfc_symbol
** result
)
2092 i
= gfc_find_sym_tree (name
, ns
, parent_flag
, &st
);
2097 *result
= st
->n
.sym
;
2103 /* Save symbol with the information necessary to back it out. */
2106 save_symbol_data (gfc_symbol
* sym
)
2109 if (sym
->new || sym
->old_symbol
!= NULL
)
2112 sym
->old_symbol
= gfc_getmem (sizeof (gfc_symbol
));
2113 *(sym
->old_symbol
) = *sym
;
2115 sym
->tlink
= changed_syms
;
2120 /* Given a name, find a symbol, or create it if it does not exist yet
2121 in the current namespace. If the symbol is found we make sure that
2124 The integer return code indicates
2126 1 The symbol name was ambiguous
2127 2 The name meant to be established was already host associated.
2129 So if the return value is nonzero, then an error was issued. */
2132 gfc_get_sym_tree (const char *name
, gfc_namespace
* ns
, gfc_symtree
** result
)
2137 /* This doesn't usually happen during resolution. */
2139 ns
= gfc_current_ns
;
2141 /* Try to find the symbol in ns. */
2142 st
= gfc_find_symtree (ns
->sym_root
, name
);
2146 /* If not there, create a new symbol. */
2147 p
= gfc_new_symbol (name
, ns
);
2149 /* Add to the list of tentative symbols. */
2150 p
->old_symbol
= NULL
;
2151 p
->tlink
= changed_syms
;
2156 st
= gfc_new_symtree (&ns
->sym_root
, name
);
2163 /* Make sure the existing symbol is OK. */
2166 ambiguous_symbol (name
, st
);
2172 if (p
->ns
!= ns
&& (!p
->attr
.function
|| ns
->proc_name
!= p
))
2174 /* Symbol is from another namespace. */
2175 gfc_error ("Symbol '%s' at %C has already been host associated",
2182 /* Copy in case this symbol is changed. */
2183 save_symbol_data (p
);
2192 gfc_get_symbol (const char *name
, gfc_namespace
* ns
, gfc_symbol
** result
)
2198 i
= gfc_get_sym_tree (name
, ns
, &st
);
2203 *result
= st
->n
.sym
;
2210 /* Subroutine that searches for a symbol, creating it if it doesn't
2211 exist, but tries to host-associate the symbol if possible. */
2214 gfc_get_ha_sym_tree (const char *name
, gfc_symtree
** result
)
2219 i
= gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
2222 save_symbol_data (st
->n
.sym
);
2228 if (gfc_current_ns
->parent
!= NULL
)
2230 i
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 1, &st
);
2241 return gfc_get_sym_tree (name
, gfc_current_ns
, result
);
2246 gfc_get_ha_symbol (const char *name
, gfc_symbol
** result
)
2251 i
= gfc_get_ha_sym_tree (name
, &st
);
2254 *result
= st
->n
.sym
;
2261 /* Return true if both symbols could refer to the same data object. Does
2262 not take account of aliasing due to equivalence statements. */
2265 gfc_symbols_could_alias (gfc_symbol
* lsym
, gfc_symbol
* rsym
)
2267 /* Aliasing isn't possible if the symbols have different base types. */
2268 if (gfc_compare_types (&lsym
->ts
, &rsym
->ts
) == 0)
2271 /* Pointers can point to other pointers, target objects and allocatable
2272 objects. Two allocatable objects cannot share the same storage. */
2273 if (lsym
->attr
.pointer
2274 && (rsym
->attr
.pointer
|| rsym
->attr
.allocatable
|| rsym
->attr
.target
))
2276 if (lsym
->attr
.target
&& rsym
->attr
.pointer
)
2278 if (lsym
->attr
.allocatable
&& rsym
->attr
.pointer
)
2285 /* Undoes all the changes made to symbols in the current statement.
2286 This subroutine is made simpler due to the fact that attributes are
2287 never removed once added. */
2290 gfc_undo_symbols (void)
2292 gfc_symbol
*p
, *q
, *old
;
2294 for (p
= changed_syms
; p
; p
= q
)
2300 /* Symbol was new. */
2301 delete_symtree (&p
->ns
->sym_root
, p
->name
);
2305 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2307 gfc_free_symbol (p
);
2311 /* Restore previous state of symbol. Just copy simple stuff. */
2313 old
= p
->old_symbol
;
2315 p
->ts
.type
= old
->ts
.type
;
2316 p
->ts
.kind
= old
->ts
.kind
;
2318 p
->attr
= old
->attr
;
2320 if (p
->value
!= old
->value
)
2322 gfc_free_expr (old
->value
);
2326 if (p
->as
!= old
->as
)
2329 gfc_free_array_spec (p
->as
);
2333 p
->generic
= old
->generic
;
2334 p
->component_access
= old
->component_access
;
2336 if (p
->namelist
!= NULL
&& old
->namelist
== NULL
)
2338 gfc_free_namelist (p
->namelist
);
2344 if (p
->namelist_tail
!= old
->namelist_tail
)
2346 gfc_free_namelist (old
->namelist_tail
);
2347 old
->namelist_tail
->next
= NULL
;
2351 p
->namelist_tail
= old
->namelist_tail
;
2353 if (p
->formal
!= old
->formal
)
2355 gfc_free_formal_arglist (p
->formal
);
2356 p
->formal
= old
->formal
;
2359 gfc_free (p
->old_symbol
);
2360 p
->old_symbol
= NULL
;
2364 changed_syms
= NULL
;
2368 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2369 components of old_symbol that might need deallocation are the "allocatables"
2370 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2371 namelist_tail. In case these differ between old_symbol and sym, it's just
2372 because sym->namelist has gotten a few more items. */
2375 free_old_symbol (gfc_symbol
* sym
)
2377 if (sym
->old_symbol
== NULL
)
2380 if (sym
->old_symbol
->as
!= sym
->as
)
2381 gfc_free_array_spec (sym
->old_symbol
->as
);
2383 if (sym
->old_symbol
->value
!= sym
->value
)
2384 gfc_free_expr (sym
->old_symbol
->value
);
2386 if (sym
->old_symbol
->formal
!= sym
->formal
)
2387 gfc_free_formal_arglist (sym
->old_symbol
->formal
);
2389 gfc_free (sym
->old_symbol
);
2390 sym
->old_symbol
= NULL
;
2394 /* Makes the changes made in the current statement permanent-- gets
2395 rid of undo information. */
2398 gfc_commit_symbols (void)
2402 for (p
= changed_syms
; p
; p
= q
)
2409 free_old_symbol (p
);
2411 changed_syms
= NULL
;
2415 /* Makes the changes made in one symbol permanent -- gets rid of undo
2419 gfc_commit_symbol (gfc_symbol
* sym
)
2423 if (changed_syms
== sym
)
2424 changed_syms
= sym
->tlink
;
2427 for (p
= changed_syms
; p
; p
= p
->tlink
)
2428 if (p
->tlink
== sym
)
2430 p
->tlink
= sym
->tlink
;
2439 free_old_symbol (sym
);
2443 /* Recursive function that deletes an entire tree and all the common
2444 head structures it points to. */
2447 free_common_tree (gfc_symtree
* common_tree
)
2449 if (common_tree
== NULL
)
2452 free_common_tree (common_tree
->left
);
2453 free_common_tree (common_tree
->right
);
2455 gfc_free (common_tree
);
2459 /* Recursive function that deletes an entire tree and all the user
2460 operator nodes that it contains. */
2463 free_uop_tree (gfc_symtree
* uop_tree
)
2466 if (uop_tree
== NULL
)
2469 free_uop_tree (uop_tree
->left
);
2470 free_uop_tree (uop_tree
->right
);
2472 gfc_free_interface (uop_tree
->n
.uop
->operator);
2474 gfc_free (uop_tree
->n
.uop
);
2475 gfc_free (uop_tree
);
2479 /* Recursive function that deletes an entire tree and all the symbols
2480 that it contains. */
2483 free_sym_tree (gfc_symtree
* sym_tree
)
2488 if (sym_tree
== NULL
)
2491 free_sym_tree (sym_tree
->left
);
2492 free_sym_tree (sym_tree
->right
);
2494 sym
= sym_tree
->n
.sym
;
2498 gfc_internal_error ("free_sym_tree(): Negative refs");
2500 if (sym
->formal_ns
!= NULL
&& sym
->refs
== 1)
2502 /* As formal_ns contains a reference to sym, delete formal_ns just
2503 before the deletion of sym. */
2504 ns
= sym
->formal_ns
;
2505 sym
->formal_ns
= NULL
;
2506 gfc_free_namespace (ns
);
2508 else if (sym
->refs
== 0)
2510 /* Go ahead and delete the symbol. */
2511 gfc_free_symbol (sym
);
2514 gfc_free (sym_tree
);
2518 /* Free the gfc_equiv_info's. */
2521 gfc_free_equiv_infos (gfc_equiv_info
* s
)
2525 gfc_free_equiv_infos (s
->next
);
2530 /* Free the gfc_equiv_lists. */
2533 gfc_free_equiv_lists (gfc_equiv_list
* l
)
2537 gfc_free_equiv_lists (l
->next
);
2538 gfc_free_equiv_infos (l
->equiv
);
2543 /* Free a namespace structure and everything below it. Interface
2544 lists associated with intrinsic operators are not freed. These are
2545 taken care of when a specific name is freed. */
2548 gfc_free_namespace (gfc_namespace
* ns
)
2550 gfc_charlen
*cl
, *cl2
;
2551 gfc_namespace
*p
, *q
;
2560 gcc_assert (ns
->refs
== 0);
2562 gfc_free_statements (ns
->code
);
2564 free_sym_tree (ns
->sym_root
);
2565 free_uop_tree (ns
->uop_root
);
2566 free_common_tree (ns
->common_root
);
2568 for (cl
= ns
->cl_list
; cl
; cl
= cl2
)
2571 gfc_free_expr (cl
->length
);
2575 free_st_labels (ns
->st_labels
);
2577 gfc_free_equiv (ns
->equiv
);
2578 gfc_free_equiv_lists (ns
->equiv_lists
);
2580 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
2581 gfc_free_interface (ns
->operator[i
]);
2583 gfc_free_data (ns
->data
);
2587 /* Recursively free any contained namespaces. */
2593 gfc_free_namespace (q
);
2599 gfc_symbol_init_2 (void)
2602 gfc_current_ns
= gfc_get_namespace (NULL
, 0);
2607 gfc_symbol_done_2 (void)
2610 gfc_free_namespace (gfc_current_ns
);
2611 gfc_current_ns
= NULL
;
2615 /* Clear mark bits from symbol nodes associated with a symtree node. */
2618 clear_sym_mark (gfc_symtree
* st
)
2621 st
->n
.sym
->mark
= 0;
2625 /* Recursively traverse the symtree nodes. */
2628 gfc_traverse_symtree (gfc_symtree
* st
, void (*func
) (gfc_symtree
*))
2634 gfc_traverse_symtree (st
->left
, func
);
2635 gfc_traverse_symtree (st
->right
, func
);
2640 /* Recursive namespace traversal function. */
2643 traverse_ns (gfc_symtree
* st
, void (*func
) (gfc_symbol
*))
2649 if (st
->n
.sym
->mark
== 0)
2650 (*func
) (st
->n
.sym
);
2651 st
->n
.sym
->mark
= 1;
2653 traverse_ns (st
->left
, func
);
2654 traverse_ns (st
->right
, func
);
2658 /* Call a given function for all symbols in the namespace. We take
2659 care that each gfc_symbol node is called exactly once. */
2662 gfc_traverse_ns (gfc_namespace
* ns
, void (*func
) (gfc_symbol
*))
2665 gfc_traverse_symtree (ns
->sym_root
, clear_sym_mark
);
2667 traverse_ns (ns
->sym_root
, func
);
2671 /* Return TRUE if the symbol is an automatic variable. */
2673 gfc_is_var_automatic (gfc_symbol
* sym
)
2675 /* Pointer and allocatable variables are never automatic. */
2676 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
2678 /* Check for arrays with non-constant size. */
2679 if (sym
->attr
.dimension
&& sym
->as
2680 && !gfc_is_compile_time_shape (sym
->as
))
2682 /* Check for non-constant length character variables. */
2683 if (sym
->ts
.type
== BT_CHARACTER
2685 && !gfc_is_constant_expr (sym
->ts
.cl
->length
))
2690 /* Given a symbol, mark it as SAVEd if it is allowed. */
2693 save_symbol (gfc_symbol
* sym
)
2696 if (sym
->attr
.use_assoc
)
2699 if (sym
->attr
.in_common
2701 || sym
->attr
.flavor
!= FL_VARIABLE
)
2703 /* Automatic objects are not saved. */
2704 if (gfc_is_var_automatic (sym
))
2706 gfc_add_save (&sym
->attr
, sym
->name
, &sym
->declared_at
);
2710 /* Mark those symbols which can be SAVEd as such. */
2713 gfc_save_all (gfc_namespace
* ns
)
2716 gfc_traverse_ns (ns
, save_symbol
);
2721 /* Make sure that no changes to symbols are pending. */
2724 gfc_symbol_state(void) {
2726 if (changed_syms
!= NULL
)
2727 gfc_internal_error("Symbol changes still pending!");
2732 /************** Global symbol handling ************/
2735 /* Search a tree for the global symbol. */
2738 gfc_find_gsymbol (gfc_gsymbol
*symbol
, const char *name
)
2744 if (strcmp (symbol
->name
, name
) == 0)
2747 s
= gfc_find_gsymbol (symbol
->left
, name
);
2751 s
= gfc_find_gsymbol (symbol
->right
, name
);
2759 /* Compare two global symbols. Used for managing the BB tree. */
2762 gsym_compare (void * _s1
, void * _s2
)
2764 gfc_gsymbol
*s1
, *s2
;
2766 s1
= (gfc_gsymbol
*)_s1
;
2767 s2
= (gfc_gsymbol
*)_s2
;
2768 return strcmp(s1
->name
, s2
->name
);
2772 /* Get a global symbol, creating it if it doesn't exist. */
2775 gfc_get_gsymbol (const char *name
)
2779 s
= gfc_find_gsymbol (gfc_gsym_root
, name
);
2783 s
= gfc_getmem (sizeof (gfc_gsymbol
));
2784 s
->type
= GSYM_UNKNOWN
;
2785 s
->name
= gfc_get_string (name
);
2787 gfc_insert_bbt (&gfc_gsym_root
, s
, gsym_compare
);