1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software 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
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
31 /* Strings for all symbol attributes. We use these for dumping the
32 parse tree, in error messages, and also when reading and writing
35 const mstring flavors[] =
37 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
38 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
39 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
40 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
41 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
45 const mstring procedures[] =
47 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
48 minit ("MODULE-PROC", PROC_MODULE),
49 minit ("INTERNAL-PROC", PROC_INTERNAL),
50 minit ("DUMMY-PROC", PROC_DUMMY),
51 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
52 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
53 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
57 const mstring intents[] =
59 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
60 minit ("IN", INTENT_IN),
61 minit ("OUT", INTENT_OUT),
62 minit ("INOUT", INTENT_INOUT),
66 const mstring access_types[] =
68 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
69 minit ("PUBLIC", ACCESS_PUBLIC),
70 minit ("PRIVATE", ACCESS_PRIVATE),
74 const mstring ifsrc_types[] =
76 minit ("UNKNOWN", IFSRC_UNKNOWN),
77 minit ("DECL", IFSRC_DECL),
78 minit ("BODY", IFSRC_IFBODY),
79 minit ("USAGE", IFSRC_USAGE)
82 const mstring save_status[] =
84 minit ("UNKNOWN", SAVE_NONE),
85 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
86 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
89 /* This is to make sure the backend generates setup code in the correct
92 static int next_dummy_order = 1;
95 gfc_namespace *gfc_current_ns;
97 gfc_gsymbol *gfc_gsym_root = NULL;
99 static gfc_symbol *changed_syms = NULL;
101 gfc_dt_list *gfc_derived_types;
104 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
106 /* The following static variable indicates whether a particular element has
107 been explicitly set or not. */
109 static int new_flag[GFC_LETTERS];
112 /* Handle a correctly parsed IMPLICIT NONE. */
115 gfc_set_implicit_none (void)
119 if (gfc_current_ns->seen_implicit_none)
121 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
125 gfc_current_ns->seen_implicit_none = 1;
127 for (i = 0; i < GFC_LETTERS; i++)
129 gfc_clear_ts (&gfc_current_ns->default_type[i]);
130 gfc_current_ns->set_flag[i] = 1;
135 /* Reset the implicit range flags. */
138 gfc_clear_new_implicit (void)
142 for (i = 0; i < GFC_LETTERS; i++)
147 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
150 gfc_add_new_implicit_range (int c1, int c2)
157 for (i = c1; i <= c2; i++)
161 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
173 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
174 the new implicit types back into the existing types will work. */
177 gfc_merge_new_implicit (gfc_typespec *ts)
181 if (gfc_current_ns->seen_implicit_none)
183 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
187 for (i = 0; i < GFC_LETTERS; i++)
192 if (gfc_current_ns->set_flag[i])
194 gfc_error ("Letter %c already has an IMPLICIT type at %C",
198 gfc_current_ns->default_type[i] = *ts;
199 gfc_current_ns->set_flag[i] = 1;
206 /* Given a symbol, return a pointer to the typespec for its default type. */
209 gfc_get_default_type (gfc_symbol *sym, gfc_namespace *ns)
213 letter = sym->name[0];
215 if (gfc_option.flag_allow_leading_underscore && letter == '_')
216 gfc_internal_error ("Option -fallow_leading_underscore is for use only by "
217 "gfortran developers, and should not be used for "
218 "implicitly typed variables");
220 if (letter < 'a' || letter > 'z')
221 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
226 return &ns->default_type[letter - 'a'];
230 /* Given a pointer to a symbol, set its type according to the first
231 letter of its name. Fails if the letter in question has no default
235 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
239 if (sym->ts.type != BT_UNKNOWN)
240 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
242 ts = gfc_get_default_type (sym, ns);
244 if (ts->type == BT_UNKNOWN)
246 if (error_flag && !sym->attr.untyped)
248 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
249 sym->name, &sym->declared_at);
250 sym->attr.untyped = 1; /* Ensure we only give an error once. */
257 sym->attr.implicit_type = 1;
259 if (sym->attr.is_bind_c == 1)
261 /* BIND(C) variables should not be implicitly declared. */
262 gfc_warning_now ("Implicitly declared BIND(C) variable '%s' at %L may "
263 "not be C interoperable", sym->name, &sym->declared_at);
264 sym->ts.f90_type = sym->ts.type;
267 if (sym->attr.dummy != 0)
269 if (sym->ns->proc_name != NULL
270 && (sym->ns->proc_name->attr.subroutine != 0
271 || sym->ns->proc_name->attr.function != 0)
272 && sym->ns->proc_name->attr.is_bind_c != 0)
274 /* Dummy args to a BIND(C) routine may not be interoperable if
275 they are implicitly typed. */
276 gfc_warning_now ("Implicity declared variable '%s' at %L may not "
277 "be C interoperable but it is a dummy argument to "
278 "the BIND(C) procedure '%s' at %L", sym->name,
279 &(sym->declared_at), sym->ns->proc_name->name,
280 &(sym->ns->proc_name->declared_at));
281 sym->ts.f90_type = sym->ts.type;
289 /* This function is called from parse.c(parse_progunit) to check the
290 type of the function is not implicitly typed in the host namespace
291 and to implicitly type the function result, if necessary. */
294 gfc_check_function_type (gfc_namespace *ns)
296 gfc_symbol *proc = ns->proc_name;
298 if (!proc->attr.contained || proc->result->attr.implicit_type)
301 if (proc->result->ts.type == BT_UNKNOWN)
303 if (gfc_set_default_type (proc->result, 0, gfc_current_ns)
306 if (proc->result != proc)
308 proc->ts = proc->result->ts;
309 proc->as = gfc_copy_array_spec (proc->result->as);
310 proc->attr.dimension = proc->result->attr.dimension;
311 proc->attr.pointer = proc->result->attr.pointer;
312 proc->attr.allocatable = proc->result->attr.allocatable;
317 gfc_error ("Function result '%s' at %L has no IMPLICIT type",
318 proc->result->name, &proc->result->declared_at);
319 proc->result->attr.untyped = 1;
325 /******************** Symbol attribute stuff *********************/
327 /* This is a generic conflict-checker. We do this to avoid having a
328 single conflict in two places. */
330 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
331 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
332 #define conf_std(a, b, std) if (attr->a && attr->b)\
341 check_conflict (symbol_attribute *attr, const char *name, locus *where)
343 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
344 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
345 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
346 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
347 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
348 *private = "PRIVATE", *recursive = "RECURSIVE",
349 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
350 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
351 *function = "FUNCTION", *subroutine = "SUBROUTINE",
352 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
353 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
354 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
355 *volatile_ = "VOLATILE", *protected = "PROTECTED",
356 *is_bind_c = "BIND(C)";
357 static const char *threadprivate = "THREADPRIVATE";
363 where = &gfc_current_locus;
365 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
369 standard = GFC_STD_F2003;
373 /* Check for attributes not allowed in a BLOCK DATA. */
374 if (gfc_current_state () == COMP_BLOCK_DATA)
378 if (attr->in_namelist)
380 if (attr->allocatable)
386 if (attr->access == ACCESS_PRIVATE)
388 if (attr->access == ACCESS_PUBLIC)
390 if (attr->intent != INTENT_UNKNOWN)
396 ("%s attribute not allowed in BLOCK DATA program unit at %L",
402 if (attr->save == SAVE_EXPLICIT)
405 conf (in_common, save);
408 switch (attr->flavor)
417 a1 = gfc_code2string (flavors, attr->flavor);
429 conf (dummy, intrinsic);
430 conf (dummy, threadprivate);
431 conf (pointer, target);
432 conf (pointer, intrinsic);
433 conf (pointer, elemental);
434 conf (allocatable, elemental);
436 conf (target, external);
437 conf (target, intrinsic);
438 conf (external, dimension); /* See Fortran 95's R504. */
440 conf (external, intrinsic);
442 if (attr->if_source || attr->contained)
444 conf (external, subroutine);
445 conf (external, function);
448 conf (allocatable, pointer);
449 conf_std (allocatable, dummy, GFC_STD_F2003);
450 conf_std (allocatable, function, GFC_STD_F2003);
451 conf_std (allocatable, result, GFC_STD_F2003);
452 conf (elemental, recursive);
454 conf (in_common, dummy);
455 conf (in_common, allocatable);
456 conf (in_common, result);
458 conf (dummy, result);
460 conf (in_equivalence, use_assoc);
461 conf (in_equivalence, dummy);
462 conf (in_equivalence, target);
463 conf (in_equivalence, pointer);
464 conf (in_equivalence, function);
465 conf (in_equivalence, result);
466 conf (in_equivalence, entry);
467 conf (in_equivalence, allocatable);
468 conf (in_equivalence, threadprivate);
470 conf (in_namelist, pointer);
471 conf (in_namelist, allocatable);
473 conf (entry, result);
475 conf (function, subroutine);
477 if (!function && !subroutine)
478 conf (is_bind_c, dummy);
480 conf (is_bind_c, cray_pointer);
481 conf (is_bind_c, cray_pointee);
482 conf (is_bind_c, allocatable);
484 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
485 Parameter conflict caught below. Also, value cannot be specified
486 for a dummy procedure. */
488 /* Cray pointer/pointee conflicts. */
489 conf (cray_pointer, cray_pointee);
490 conf (cray_pointer, dimension);
491 conf (cray_pointer, pointer);
492 conf (cray_pointer, target);
493 conf (cray_pointer, allocatable);
494 conf (cray_pointer, external);
495 conf (cray_pointer, intrinsic);
496 conf (cray_pointer, in_namelist);
497 conf (cray_pointer, function);
498 conf (cray_pointer, subroutine);
499 conf (cray_pointer, entry);
501 conf (cray_pointee, allocatable);
502 conf (cray_pointee, intent);
503 conf (cray_pointee, optional);
504 conf (cray_pointee, dummy);
505 conf (cray_pointee, target);
506 conf (cray_pointee, intrinsic);
507 conf (cray_pointee, pointer);
508 conf (cray_pointee, entry);
509 conf (cray_pointee, in_common);
510 conf (cray_pointee, in_equivalence);
511 conf (cray_pointee, threadprivate);
514 conf (data, function);
516 conf (data, allocatable);
517 conf (data, use_assoc);
519 conf (value, pointer)
520 conf (value, allocatable)
521 conf (value, subroutine)
522 conf (value, function)
523 conf (value, volatile_)
524 conf (value, dimension)
525 conf (value, external)
528 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
531 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
535 conf (protected, intrinsic)
536 conf (protected, external)
537 conf (protected, in_common)
539 conf (volatile_, intrinsic)
540 conf (volatile_, external)
542 if (attr->volatile_ && attr->intent == INTENT_IN)
549 a1 = gfc_code2string (flavors, attr->flavor);
551 if (attr->in_namelist
552 && attr->flavor != FL_VARIABLE
553 && attr->flavor != FL_PROCEDURE
554 && attr->flavor != FL_UNKNOWN)
560 switch (attr->flavor)
580 conf2 (threadprivate);
590 if (attr->subroutine)
599 conf2 (threadprivate);
604 case PROC_ST_FUNCTION:
616 conf2 (threadprivate);
636 conf2 (threadprivate);
638 if (attr->intent != INTENT_UNKNOWN)
660 conf2 (threadprivate);
661 /* TODO: hmm, double check this. */
673 gfc_error ("%s attribute conflicts with %s attribute at %L",
676 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
677 a1, a2, name, where);
684 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
685 "with %s attribute at %L", a1, a2,
690 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
691 "with %s attribute in '%s' at %L",
692 a1, a2, name, where);
701 /* Mark a symbol as referenced. */
704 gfc_set_sym_referenced (gfc_symbol *sym)
707 if (sym->attr.referenced)
710 sym->attr.referenced = 1;
712 /* Remember which order dummy variables are accessed in. */
714 sym->dummy_order = next_dummy_order++;
718 /* Common subroutine called by attribute changing subroutines in order
719 to prevent them from changing a symbol that has been
720 use-associated. Returns zero if it is OK to change the symbol,
724 check_used (symbol_attribute *attr, const char *name, locus *where)
727 if (attr->use_assoc == 0)
731 where = &gfc_current_locus;
734 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
737 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
744 /* Generate an error because of a duplicate attribute. */
747 duplicate_attr (const char *attr, locus *where)
751 where = &gfc_current_locus;
753 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
757 /* Called from decl.c (attr_decl1) to check attributes, when declared
761 gfc_add_attribute (symbol_attribute *attr, locus *where)
764 if (check_used (attr, NULL, where))
767 return check_conflict (attr, NULL, where);
771 gfc_add_allocatable (symbol_attribute *attr, locus *where)
774 if (check_used (attr, NULL, where))
777 if (attr->allocatable)
779 duplicate_attr ("ALLOCATABLE", where);
783 attr->allocatable = 1;
784 return check_conflict (attr, NULL, where);
789 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
792 if (check_used (attr, name, where))
797 duplicate_attr ("DIMENSION", where);
802 return check_conflict (attr, name, where);
807 gfc_add_external (symbol_attribute *attr, locus *where)
810 if (check_used (attr, NULL, where))
815 duplicate_attr ("EXTERNAL", where);
821 return check_conflict (attr, NULL, where);
826 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
829 if (check_used (attr, NULL, where))
834 duplicate_attr ("INTRINSIC", where);
840 return check_conflict (attr, NULL, where);
845 gfc_add_optional (symbol_attribute *attr, locus *where)
848 if (check_used (attr, NULL, where))
853 duplicate_attr ("OPTIONAL", where);
858 return check_conflict (attr, NULL, where);
863 gfc_add_pointer (symbol_attribute *attr, locus *where)
866 if (check_used (attr, NULL, where))
870 return check_conflict (attr, NULL, where);
875 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
878 if (check_used (attr, NULL, where))
881 attr->cray_pointer = 1;
882 return check_conflict (attr, NULL, where);
887 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
890 if (check_used (attr, NULL, where))
893 if (attr->cray_pointee)
895 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
896 " statements", where);
900 attr->cray_pointee = 1;
901 return check_conflict (attr, NULL, where);
906 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
908 if (check_used (attr, name, where))
913 if (gfc_notify_std (GFC_STD_LEGACY,
914 "Duplicate PROTECTED attribute specified at %L",
921 return check_conflict (attr, name, where);
926 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
929 if (check_used (attr, name, where))
933 return check_conflict (attr, name, where);
938 gfc_add_save (symbol_attribute *attr, const char *name, locus *where)
941 if (check_used (attr, name, where))
947 ("SAVE attribute at %L cannot be specified in a PURE procedure",
952 if (attr->save == SAVE_EXPLICIT)
954 if (gfc_notify_std (GFC_STD_LEGACY,
955 "Duplicate SAVE attribute specified at %L",
961 attr->save = SAVE_EXPLICIT;
962 return check_conflict (attr, name, where);
967 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
970 if (check_used (attr, name, where))
975 if (gfc_notify_std (GFC_STD_LEGACY,
976 "Duplicate VALUE attribute specified at %L",
983 return check_conflict (attr, name, where);
988 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
990 /* No check_used needed as 11.2.1 of the F2003 standard allows
991 that the local identifier made accessible by a use statement can be
992 given a VOLATILE attribute. */
994 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
995 if (gfc_notify_std (GFC_STD_LEGACY,
996 "Duplicate VOLATILE attribute specified at %L", where)
1000 attr->volatile_ = 1;
1001 attr->volatile_ns = gfc_current_ns;
1002 return check_conflict (attr, name, where);
1007 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1010 if (check_used (attr, name, where))
1013 if (attr->threadprivate)
1015 duplicate_attr ("THREADPRIVATE", where);
1019 attr->threadprivate = 1;
1020 return check_conflict (attr, name, where);
1025 gfc_add_target (symbol_attribute *attr, locus *where)
1028 if (check_used (attr, NULL, where))
1033 duplicate_attr ("TARGET", where);
1038 return check_conflict (attr, NULL, where);
1043 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1046 if (check_used (attr, name, where))
1049 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1051 return check_conflict (attr, name, where);
1056 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1059 if (check_used (attr, name, where))
1062 /* Duplicate attribute already checked for. */
1063 attr->in_common = 1;
1064 if (check_conflict (attr, name, where) == FAILURE)
1067 if (attr->flavor == FL_VARIABLE)
1070 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1075 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1078 /* Duplicate attribute already checked for. */
1079 attr->in_equivalence = 1;
1080 if (check_conflict (attr, name, where) == FAILURE)
1083 if (attr->flavor == FL_VARIABLE)
1086 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1091 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1094 if (check_used (attr, name, where))
1098 return check_conflict (attr, name, where);
1103 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1106 attr->in_namelist = 1;
1107 return check_conflict (attr, name, where);
1112 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1115 if (check_used (attr, name, where))
1119 return check_conflict (attr, name, where);
1124 gfc_add_elemental (symbol_attribute *attr, locus *where)
1127 if (check_used (attr, NULL, where))
1130 attr->elemental = 1;
1131 return check_conflict (attr, NULL, where);
1136 gfc_add_pure (symbol_attribute *attr, locus *where)
1139 if (check_used (attr, NULL, where))
1143 return check_conflict (attr, NULL, where);
1148 gfc_add_recursive (symbol_attribute *attr, locus *where)
1151 if (check_used (attr, NULL, where))
1154 attr->recursive = 1;
1155 return check_conflict (attr, NULL, where);
1160 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1163 if (check_used (attr, name, where))
1168 duplicate_attr ("ENTRY", where);
1173 return check_conflict (attr, name, where);
1178 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1181 if (attr->flavor != FL_PROCEDURE
1182 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1186 return check_conflict (attr, name, where);
1191 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1194 if (attr->flavor != FL_PROCEDURE
1195 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1198 attr->subroutine = 1;
1199 return check_conflict (attr, name, where);
1204 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1207 if (attr->flavor != FL_PROCEDURE
1208 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1212 return check_conflict (attr, name, where);
1216 /* Flavors are special because some flavors are not what Fortran
1217 considers attributes and can be reaffirmed multiple times. */
1220 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1224 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1225 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1226 || f == FL_NAMELIST) && check_used (attr, name, where))
1229 if (attr->flavor == f && f == FL_VARIABLE)
1232 if (attr->flavor != FL_UNKNOWN)
1235 where = &gfc_current_locus;
1238 gfc_error ("%s attribute of '%s' conflicts with %s attribute at %L",
1239 gfc_code2string (flavors, attr->flavor), name,
1240 gfc_code2string (flavors, f), where);
1242 gfc_error ("%s attribute conflicts with %s attribute at %L",
1243 gfc_code2string (flavors, attr->flavor),
1244 gfc_code2string (flavors, f), where);
1251 return check_conflict (attr, name, where);
1256 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1257 const char *name, locus *where)
1260 if (check_used (attr, name, where))
1263 if (attr->flavor != FL_PROCEDURE
1264 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1268 where = &gfc_current_locus;
1270 if (attr->proc != PROC_UNKNOWN)
1272 gfc_error ("%s procedure at %L is already declared as %s procedure",
1273 gfc_code2string (procedures, t), where,
1274 gfc_code2string (procedures, attr->proc));
1281 /* Statement functions are always scalar and functions. */
1282 if (t == PROC_ST_FUNCTION
1283 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1284 || attr->dimension))
1287 return check_conflict (attr, name, where);
1292 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1295 if (check_used (attr, NULL, where))
1298 if (attr->intent == INTENT_UNKNOWN)
1300 attr->intent = intent;
1301 return check_conflict (attr, NULL, where);
1305 where = &gfc_current_locus;
1307 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1308 gfc_intent_string (attr->intent),
1309 gfc_intent_string (intent), where);
1315 /* No checks for use-association in public and private statements. */
1318 gfc_add_access (symbol_attribute *attr, gfc_access access,
1319 const char *name, locus *where)
1322 if (attr->access == ACCESS_UNKNOWN)
1324 attr->access = access;
1325 return check_conflict (attr, name, where);
1329 where = &gfc_current_locus;
1330 gfc_error ("ACCESS specification at %L was already specified", where);
1336 /* Set the is_bind_c field for the given symbol_attribute. */
1339 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1340 int is_proc_lang_bind_spec)
1343 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1344 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1345 "variables or common blocks", where);
1346 else if (attr->is_bind_c)
1347 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1349 attr->is_bind_c = 1;
1352 where = &gfc_current_locus;
1354 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BIND(C) at %L", where)
1358 return check_conflict (attr, name, where);
1363 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1364 gfc_formal_arglist * formal, locus *where)
1367 if (check_used (&sym->attr, sym->name, where))
1371 where = &gfc_current_locus;
1373 if (sym->attr.if_source != IFSRC_UNKNOWN
1374 && sym->attr.if_source != IFSRC_DECL)
1376 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1381 sym->formal = formal;
1382 sym->attr.if_source = source;
1388 /* Add a type to a symbol. */
1391 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1396 where = &gfc_current_locus;
1398 if (sym->ts.type != BT_UNKNOWN)
1400 const char *msg = "Symbol '%s' at %L already has basic type of %s";
1401 if (!(sym->ts.type == ts->type
1402 && (sym->attr.flavor == FL_PROCEDURE || sym->attr.result))
1403 || gfc_notification_std (GFC_STD_GNU) == ERROR
1406 gfc_error (msg, sym->name, where, gfc_basic_typename (sym->ts.type));
1409 else if (gfc_notify_std (GFC_STD_GNU, msg, sym->name, where,
1410 gfc_basic_typename (sym->ts.type)) == FAILURE)
1414 flavor = sym->attr.flavor;
1416 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1417 || flavor == FL_LABEL
1418 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
1419 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1421 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1430 /* Clears all attributes. */
1433 gfc_clear_attr (symbol_attribute *attr)
1435 memset (attr, 0, sizeof (symbol_attribute));
1439 /* Check for missing attributes in the new symbol. Currently does
1440 nothing, but it's not clear that it is unnecessary yet. */
1443 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1444 locus *where ATTRIBUTE_UNUSED)
1451 /* Copy an attribute to a symbol attribute, bit by bit. Some
1452 attributes have a lot of side-effects but cannot be present given
1453 where we are called from, so we ignore some bits. */
1456 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
1458 int is_proc_lang_bind_spec;
1460 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1463 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1465 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1467 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1469 if (src->protected && gfc_add_protected (dest, NULL, where) == FAILURE)
1471 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1473 if (src->value && gfc_add_value (dest, NULL, where) == FAILURE)
1475 if (src->volatile_ && gfc_add_volatile (dest, NULL, where) == FAILURE)
1477 if (src->threadprivate
1478 && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
1480 if (src->target && gfc_add_target (dest, where) == FAILURE)
1482 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1484 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1489 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1492 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1495 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1497 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1499 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1502 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1504 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1506 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1508 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1511 if (src->flavor != FL_UNKNOWN
1512 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1515 if (src->intent != INTENT_UNKNOWN
1516 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1519 if (src->access != ACCESS_UNKNOWN
1520 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1523 if (gfc_missing_attr (dest, where) == FAILURE)
1526 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1528 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1531 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1533 && gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec)
1537 if (src->is_c_interop)
1538 dest->is_c_interop = 1;
1542 if (src->external && gfc_add_external (dest, where) == FAILURE)
1544 if (src->intrinsic && gfc_add_intrinsic (dest, where) == FAILURE)
1554 /************** Component name management ************/
1556 /* Component names of a derived type form their own little namespaces
1557 that are separate from all other spaces. The space is composed of
1558 a singly linked list of gfc_component structures whose head is
1559 located in the parent symbol. */
1562 /* Add a component name to a symbol. The call fails if the name is
1563 already present. On success, the component pointer is modified to
1564 point to the additional component structure. */
1567 gfc_add_component (gfc_symbol *sym, const char *name,
1568 gfc_component **component)
1570 gfc_component *p, *tail;
1574 for (p = sym->components; p; p = p->next)
1576 if (strcmp (p->name, name) == 0)
1578 gfc_error ("Component '%s' at %C already declared at %L",
1586 /* Allocate a new component. */
1587 p = gfc_get_component ();
1590 sym->components = p;
1594 p->name = gfc_get_string (name);
1595 p->loc = gfc_current_locus;
1602 /* Recursive function to switch derived types of all symbol in a
1606 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
1614 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1615 sym->ts.derived = to;
1617 switch_types (st->left, from, to);
1618 switch_types (st->right, from, to);
1622 /* This subroutine is called when a derived type is used in order to
1623 make the final determination about which version to use. The
1624 standard requires that a type be defined before it is 'used', but
1625 such types can appear in IMPLICIT statements before the actual
1626 definition. 'Using' in this context means declaring a variable to
1627 be that type or using the type constructor.
1629 If a type is used and the components haven't been defined, then we
1630 have to have a derived type in a parent unit. We find the node in
1631 the other namespace and point the symtree node in this namespace to
1632 that node. Further reference to this name point to the correct
1633 node. If we can't find the node in a parent namespace, then we have
1636 This subroutine takes a pointer to a symbol node and returns a
1637 pointer to the translated node or NULL for an error. Usually there
1638 is no translation and we return the node we were passed. */
1641 gfc_use_derived (gfc_symbol *sym)
1648 if (sym->components != NULL)
1649 return sym; /* Already defined. */
1651 if (sym->ns->parent == NULL)
1654 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1656 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1660 if (s == NULL || s->attr.flavor != FL_DERIVED)
1663 /* Get rid of symbol sym, translating all references to s. */
1664 for (i = 0; i < GFC_LETTERS; i++)
1666 t = &sym->ns->default_type[i];
1667 if (t->derived == sym)
1671 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1676 /* Unlink from list of modified symbols. */
1677 gfc_commit_symbol (sym);
1679 switch_types (sym->ns->sym_root, sym, s);
1681 /* TODO: Also have to replace sym -> s in other lists like
1682 namelists, common lists and interface lists. */
1683 gfc_free_symbol (sym);
1688 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1694 /* Given a derived type node and a component name, try to locate the
1695 component structure. Returns the NULL pointer if the component is
1696 not found or the components are private. */
1699 gfc_find_component (gfc_symbol *sym, const char *name)
1706 sym = gfc_use_derived (sym);
1711 for (p = sym->components; p; p = p->next)
1712 if (strcmp (p->name, name) == 0)
1716 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1720 if (sym->attr.use_assoc && (sym->component_access == ACCESS_PRIVATE
1721 || p->access == ACCESS_PRIVATE))
1723 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1733 /* Given a symbol, free all of the component structures and everything
1737 free_components (gfc_component *p)
1745 gfc_free_array_spec (p->as);
1746 gfc_free_expr (p->initializer);
1753 /* Set component attributes from a standard symbol attribute structure. */
1756 gfc_set_component_attr (gfc_component *c, symbol_attribute *attr)
1759 c->dimension = attr->dimension;
1760 c->pointer = attr->pointer;
1761 c->allocatable = attr->allocatable;
1762 c->access = attr->access;
1766 /* Get a standard symbol attribute structure given the component
1770 gfc_get_component_attr (symbol_attribute *attr, gfc_component *c)
1773 gfc_clear_attr (attr);
1774 attr->dimension = c->dimension;
1775 attr->pointer = c->pointer;
1776 attr->allocatable = c->allocatable;
1777 attr->access = c->access;
1781 /******************** Statement label management ********************/
1783 /* Comparison function for statement labels, used for managing the
1787 compare_st_labels (void *a1, void *b1)
1789 int a = ((gfc_st_label *) a1)->value;
1790 int b = ((gfc_st_label *) b1)->value;
1796 /* Free a single gfc_st_label structure, making sure the tree is not
1797 messed up. This function is called only when some parse error
1801 gfc_free_st_label (gfc_st_label *label)
1807 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
1809 if (label->format != NULL)
1810 gfc_free_expr (label->format);
1816 /* Free a whole tree of gfc_st_label structures. */
1819 free_st_labels (gfc_st_label *label)
1825 free_st_labels (label->left);
1826 free_st_labels (label->right);
1828 if (label->format != NULL)
1829 gfc_free_expr (label->format);
1834 /* Given a label number, search for and return a pointer to the label
1835 structure, creating it if it does not exist. */
1838 gfc_get_st_label (int labelno)
1842 /* First see if the label is already in this namespace. */
1843 lp = gfc_current_ns->st_labels;
1846 if (lp->value == labelno)
1849 if (lp->value < labelno)
1855 lp = gfc_getmem (sizeof (gfc_st_label));
1857 lp->value = labelno;
1858 lp->defined = ST_LABEL_UNKNOWN;
1859 lp->referenced = ST_LABEL_UNKNOWN;
1861 gfc_insert_bbt (&gfc_current_ns->st_labels, lp, compare_st_labels);
1867 /* Called when a statement with a statement label is about to be
1868 accepted. We add the label to the list of the current namespace,
1869 making sure it hasn't been defined previously and referenced
1873 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
1877 labelno = lp->value;
1879 if (lp->defined != ST_LABEL_UNKNOWN)
1880 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1881 &lp->where, label_locus);
1884 lp->where = *label_locus;
1888 case ST_LABEL_FORMAT:
1889 if (lp->referenced == ST_LABEL_TARGET)
1890 gfc_error ("Label %d at %C already referenced as branch target",
1893 lp->defined = ST_LABEL_FORMAT;
1897 case ST_LABEL_TARGET:
1898 if (lp->referenced == ST_LABEL_FORMAT)
1899 gfc_error ("Label %d at %C already referenced as a format label",
1902 lp->defined = ST_LABEL_TARGET;
1907 lp->defined = ST_LABEL_BAD_TARGET;
1908 lp->referenced = ST_LABEL_BAD_TARGET;
1914 /* Reference a label. Given a label and its type, see if that
1915 reference is consistent with what is known about that label,
1916 updating the unknown state. Returns FAILURE if something goes
1920 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
1922 gfc_sl_type label_type;
1929 labelno = lp->value;
1931 if (lp->defined != ST_LABEL_UNKNOWN)
1932 label_type = lp->defined;
1935 label_type = lp->referenced;
1936 lp->where = gfc_current_locus;
1939 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1941 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1946 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1947 && type == ST_LABEL_FORMAT)
1949 gfc_error ("Label %d at %C previously used as branch target", labelno);
1954 lp->referenced = type;
1962 /************** Symbol table management subroutines ****************/
1964 /* Basic details: Fortran 95 requires a potentially unlimited number
1965 of distinct namespaces when compiling a program unit. This case
1966 occurs during a compilation of internal subprograms because all of
1967 the internal subprograms must be read before we can start
1968 generating code for the host.
1970 Given the tricky nature of the Fortran grammar, we must be able to
1971 undo changes made to a symbol table if the current interpretation
1972 of a statement is found to be incorrect. Whenever a symbol is
1973 looked up, we make a copy of it and link to it. All of these
1974 symbols are kept in a singly linked list so that we can commit or
1975 undo the changes at a later time.
1977 A symtree may point to a symbol node outside of its namespace. In
1978 this case, that symbol has been used as a host associated variable
1979 at some previous time. */
1981 /* Allocate a new namespace structure. Copies the implicit types from
1982 PARENT if PARENT_TYPES is set. */
1985 gfc_get_namespace (gfc_namespace *parent, int parent_types)
1989 gfc_intrinsic_op in;
1992 ns = gfc_getmem (sizeof (gfc_namespace));
1993 ns->sym_root = NULL;
1994 ns->uop_root = NULL;
1995 ns->default_access = ACCESS_UNKNOWN;
1996 ns->parent = parent;
1998 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1999 ns->operator_access[in] = ACCESS_UNKNOWN;
2001 /* Initialize default implicit types. */
2002 for (i = 'a'; i <= 'z'; i++)
2004 ns->set_flag[i - 'a'] = 0;
2005 ts = &ns->default_type[i - 'a'];
2007 if (parent_types && ns->parent != NULL)
2009 /* Copy parent settings. */
2010 *ts = ns->parent->default_type[i - 'a'];
2014 if (gfc_option.flag_implicit_none != 0)
2020 if ('i' <= i && i <= 'n')
2022 ts->type = BT_INTEGER;
2023 ts->kind = gfc_default_integer_kind;
2028 ts->kind = gfc_default_real_kind;
2038 /* Comparison function for symtree nodes. */
2041 compare_symtree (void *_st1, void *_st2)
2043 gfc_symtree *st1, *st2;
2045 st1 = (gfc_symtree *) _st1;
2046 st2 = (gfc_symtree *) _st2;
2048 return strcmp (st1->name, st2->name);
2052 /* Allocate a new symtree node and associate it with the new symbol. */
2055 gfc_new_symtree (gfc_symtree **root, const char *name)
2059 st = gfc_getmem (sizeof (gfc_symtree));
2060 st->name = gfc_get_string (name);
2062 gfc_insert_bbt (root, st, compare_symtree);
2067 /* Delete a symbol from the tree. Does not free the symbol itself! */
2070 delete_symtree (gfc_symtree **root, const char *name)
2072 gfc_symtree st, *st0;
2074 st0 = gfc_find_symtree (*root, name);
2076 st.name = gfc_get_string (name);
2077 gfc_delete_bbt (root, &st, compare_symtree);
2083 /* Given a root symtree node and a name, try to find the symbol within
2084 the namespace. Returns NULL if the symbol is not found. */
2087 gfc_find_symtree (gfc_symtree *st, const char *name)
2093 c = strcmp (name, st->name);
2097 st = (c < 0) ? st->left : st->right;
2104 /* Given a name find a user operator node, creating it if it doesn't
2105 exist. These are much simpler than symbols because they can't be
2106 ambiguous with one another. */
2109 gfc_get_uop (const char *name)
2114 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
2118 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
2120 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
2121 uop->name = gfc_get_string (name);
2122 uop->access = ACCESS_UNKNOWN;
2123 uop->ns = gfc_current_ns;
2129 /* Given a name find the user operator node. Returns NULL if it does
2133 gfc_find_uop (const char *name, gfc_namespace *ns)
2138 ns = gfc_current_ns;
2140 st = gfc_find_symtree (ns->uop_root, name);
2141 return (st == NULL) ? NULL : st->n.uop;
2145 /* Remove a gfc_symbol structure and everything it points to. */
2148 gfc_free_symbol (gfc_symbol *sym)
2154 gfc_free_array_spec (sym->as);
2156 free_components (sym->components);
2158 gfc_free_expr (sym->value);
2160 gfc_free_namelist (sym->namelist);
2162 gfc_free_namespace (sym->formal_ns);
2164 if (!sym->attr.generic_copy)
2165 gfc_free_interface (sym->generic);
2167 gfc_free_formal_arglist (sym->formal);
2173 /* Allocate and initialize a new symbol node. */
2176 gfc_new_symbol (const char *name, gfc_namespace *ns)
2180 p = gfc_getmem (sizeof (gfc_symbol));
2182 gfc_clear_ts (&p->ts);
2183 gfc_clear_attr (&p->attr);
2186 p->declared_at = gfc_current_locus;
2188 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2189 gfc_internal_error ("new_symbol(): Symbol name too long");
2191 p->name = gfc_get_string (name);
2193 /* Make sure flags for symbol being C bound are clear initially. */
2194 p->attr.is_bind_c = 0;
2195 p->attr.is_iso_c = 0;
2196 /* Make sure the binding label field has a Nul char to start. */
2197 p->binding_label[0] = '\0';
2199 /* Clear the ptrs we may need. */
2200 p->common_block = NULL;
2206 /* Generate an error if a symbol is ambiguous. */
2209 ambiguous_symbol (const char *name, gfc_symtree *st)
2212 if (st->n.sym->module)
2213 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2214 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
2216 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2217 "from current program unit", name, st->n.sym->name);
2221 /* Search for a symtree starting in the current namespace, resorting to
2222 any parent namespaces if requested by a nonzero parent_flag.
2223 Returns nonzero if the name is ambiguous. */
2226 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2227 gfc_symtree **result)
2232 ns = gfc_current_ns;
2236 st = gfc_find_symtree (ns->sym_root, name);
2240 /* Ambiguous generic interfaces are permitted, as long
2241 as the specific interfaces are different. */
2242 if (st->ambiguous && !st->n.sym->attr.generic)
2244 ambiguous_symbol (name, st);
2263 /* Same, but returns the symbol instead. */
2266 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
2267 gfc_symbol **result)
2272 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2277 *result = st->n.sym;
2283 /* Save symbol with the information necessary to back it out. */
2286 save_symbol_data (gfc_symbol *sym)
2289 if (sym->new || sym->old_symbol != NULL)
2292 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
2293 *(sym->old_symbol) = *sym;
2295 sym->tlink = changed_syms;
2300 /* Given a name, find a symbol, or create it if it does not exist yet
2301 in the current namespace. If the symbol is found we make sure that
2304 The integer return code indicates
2306 1 The symbol name was ambiguous
2307 2 The name meant to be established was already host associated.
2309 So if the return value is nonzero, then an error was issued. */
2312 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result)
2317 /* This doesn't usually happen during resolution. */
2319 ns = gfc_current_ns;
2321 /* Try to find the symbol in ns. */
2322 st = gfc_find_symtree (ns->sym_root, name);
2326 /* If not there, create a new symbol. */
2327 p = gfc_new_symbol (name, ns);
2329 /* Add to the list of tentative symbols. */
2330 p->old_symbol = NULL;
2331 p->tlink = changed_syms;
2336 st = gfc_new_symtree (&ns->sym_root, name);
2343 /* Make sure the existing symbol is OK. Ambiguous
2344 generic interfaces are permitted, as long as the
2345 specific interfaces are different. */
2346 if (st->ambiguous && !st->n.sym->attr.generic)
2348 ambiguous_symbol (name, st);
2354 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2356 /* Symbol is from another namespace. */
2357 gfc_error ("Symbol '%s' at %C has already been host associated",
2364 /* Copy in case this symbol is changed. */
2365 save_symbol_data (p);
2374 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
2379 i = gfc_get_sym_tree (name, ns, &st);
2384 *result = st->n.sym;
2391 /* Subroutine that searches for a symbol, creating it if it doesn't
2392 exist, but tries to host-associate the symbol if possible. */
2395 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
2400 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2403 save_symbol_data (st->n.sym);
2408 if (gfc_current_ns->parent != NULL)
2410 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2421 return gfc_get_sym_tree (name, gfc_current_ns, result);
2426 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
2431 i = gfc_get_ha_sym_tree (name, &st);
2434 *result = st->n.sym;
2441 /* Return true if both symbols could refer to the same data object. Does
2442 not take account of aliasing due to equivalence statements. */
2445 gfc_symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym)
2447 /* Aliasing isn't possible if the symbols have different base types. */
2448 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2451 /* Pointers can point to other pointers, target objects and allocatable
2452 objects. Two allocatable objects cannot share the same storage. */
2453 if (lsym->attr.pointer
2454 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2456 if (lsym->attr.target && rsym->attr.pointer)
2458 if (lsym->attr.allocatable && rsym->attr.pointer)
2465 /* Undoes all the changes made to symbols in the current statement.
2466 This subroutine is made simpler due to the fact that attributes are
2467 never removed once added. */
2470 gfc_undo_symbols (void)
2472 gfc_symbol *p, *q, *old;
2474 for (p = changed_syms; p; p = q)
2480 /* Symbol was new. */
2481 delete_symtree (&p->ns->sym_root, p->name);
2485 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2487 gfc_free_symbol (p);
2491 /* Restore previous state of symbol. Just copy simple stuff. */
2493 old = p->old_symbol;
2495 p->ts.type = old->ts.type;
2496 p->ts.kind = old->ts.kind;
2498 p->attr = old->attr;
2500 if (p->value != old->value)
2502 gfc_free_expr (old->value);
2506 if (p->as != old->as)
2509 gfc_free_array_spec (p->as);
2513 p->generic = old->generic;
2514 p->component_access = old->component_access;
2516 if (p->namelist != NULL && old->namelist == NULL)
2518 gfc_free_namelist (p->namelist);
2523 if (p->namelist_tail != old->namelist_tail)
2525 gfc_free_namelist (old->namelist_tail);
2526 old->namelist_tail->next = NULL;
2530 p->namelist_tail = old->namelist_tail;
2532 if (p->formal != old->formal)
2534 gfc_free_formal_arglist (p->formal);
2535 p->formal = old->formal;
2538 gfc_free (p->old_symbol);
2539 p->old_symbol = NULL;
2543 changed_syms = NULL;
2547 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2548 components of old_symbol that might need deallocation are the "allocatables"
2549 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2550 namelist_tail. In case these differ between old_symbol and sym, it's just
2551 because sym->namelist has gotten a few more items. */
2554 free_old_symbol (gfc_symbol *sym)
2557 if (sym->old_symbol == NULL)
2560 if (sym->old_symbol->as != sym->as)
2561 gfc_free_array_spec (sym->old_symbol->as);
2563 if (sym->old_symbol->value != sym->value)
2564 gfc_free_expr (sym->old_symbol->value);
2566 if (sym->old_symbol->formal != sym->formal)
2567 gfc_free_formal_arglist (sym->old_symbol->formal);
2569 gfc_free (sym->old_symbol);
2570 sym->old_symbol = NULL;
2574 /* Makes the changes made in the current statement permanent-- gets
2575 rid of undo information. */
2578 gfc_commit_symbols (void)
2582 for (p = changed_syms; p; p = q)
2588 free_old_symbol (p);
2590 changed_syms = NULL;
2594 /* Makes the changes made in one symbol permanent -- gets rid of undo
2598 gfc_commit_symbol (gfc_symbol *sym)
2602 if (changed_syms == sym)
2603 changed_syms = sym->tlink;
2606 for (p = changed_syms; p; p = p->tlink)
2607 if (p->tlink == sym)
2609 p->tlink = sym->tlink;
2618 free_old_symbol (sym);
2622 /* Recursive function that deletes an entire tree and all the common
2623 head structures it points to. */
2626 free_common_tree (gfc_symtree * common_tree)
2628 if (common_tree == NULL)
2631 free_common_tree (common_tree->left);
2632 free_common_tree (common_tree->right);
2634 gfc_free (common_tree);
2638 /* Recursive function that deletes an entire tree and all the user
2639 operator nodes that it contains. */
2642 free_uop_tree (gfc_symtree *uop_tree)
2645 if (uop_tree == NULL)
2648 free_uop_tree (uop_tree->left);
2649 free_uop_tree (uop_tree->right);
2651 gfc_free_interface (uop_tree->n.uop->operator);
2653 gfc_free (uop_tree->n.uop);
2654 gfc_free (uop_tree);
2658 /* Recursive function that deletes an entire tree and all the symbols
2659 that it contains. */
2662 free_sym_tree (gfc_symtree *sym_tree)
2667 if (sym_tree == NULL)
2670 free_sym_tree (sym_tree->left);
2671 free_sym_tree (sym_tree->right);
2673 sym = sym_tree->n.sym;
2677 gfc_internal_error ("free_sym_tree(): Negative refs");
2679 if (sym->formal_ns != NULL && sym->refs == 1)
2681 /* As formal_ns contains a reference to sym, delete formal_ns just
2682 before the deletion of sym. */
2683 ns = sym->formal_ns;
2684 sym->formal_ns = NULL;
2685 gfc_free_namespace (ns);
2687 else if (sym->refs == 0)
2689 /* Go ahead and delete the symbol. */
2690 gfc_free_symbol (sym);
2693 gfc_free (sym_tree);
2697 /* Free the derived type list. */
2700 gfc_free_dt_list (void)
2702 gfc_dt_list *dt, *n;
2704 for (dt = gfc_derived_types; dt; dt = n)
2710 gfc_derived_types = NULL;
2714 /* Free the gfc_equiv_info's. */
2717 gfc_free_equiv_infos (gfc_equiv_info *s)
2721 gfc_free_equiv_infos (s->next);
2726 /* Free the gfc_equiv_lists. */
2729 gfc_free_equiv_lists (gfc_equiv_list *l)
2733 gfc_free_equiv_lists (l->next);
2734 gfc_free_equiv_infos (l->equiv);
2739 /* Free a namespace structure and everything below it. Interface
2740 lists associated with intrinsic operators are not freed. These are
2741 taken care of when a specific name is freed. */
2744 gfc_free_namespace (gfc_namespace *ns)
2746 gfc_charlen *cl, *cl2;
2747 gfc_namespace *p, *q;
2756 gcc_assert (ns->refs == 0);
2758 gfc_free_statements (ns->code);
2760 free_sym_tree (ns->sym_root);
2761 free_uop_tree (ns->uop_root);
2762 free_common_tree (ns->common_root);
2764 for (cl = ns->cl_list; cl; cl = cl2)
2767 gfc_free_expr (cl->length);
2771 free_st_labels (ns->st_labels);
2773 gfc_free_equiv (ns->equiv);
2774 gfc_free_equiv_lists (ns->equiv_lists);
2776 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2777 gfc_free_interface (ns->operator[i]);
2779 gfc_free_data (ns->data);
2783 /* Recursively free any contained namespaces. */
2788 gfc_free_namespace (q);
2794 gfc_symbol_init_2 (void)
2797 gfc_current_ns = gfc_get_namespace (NULL, 0);
2802 gfc_symbol_done_2 (void)
2805 gfc_free_namespace (gfc_current_ns);
2806 gfc_current_ns = NULL;
2807 gfc_free_dt_list ();
2811 /* Clear mark bits from symbol nodes associated with a symtree node. */
2814 clear_sym_mark (gfc_symtree *st)
2817 st->n.sym->mark = 0;
2821 /* Recursively traverse the symtree nodes. */
2824 gfc_traverse_symtree (gfc_symtree *st, void (*func) (gfc_symtree *))
2830 gfc_traverse_symtree (st->left, func);
2831 gfc_traverse_symtree (st->right, func);
2836 /* Recursive namespace traversal function. */
2839 traverse_ns (gfc_symtree *st, void (*func) (gfc_symbol *))
2845 if (st->n.sym->mark == 0)
2846 (*func) (st->n.sym);
2847 st->n.sym->mark = 1;
2849 traverse_ns (st->left, func);
2850 traverse_ns (st->right, func);
2854 /* Call a given function for all symbols in the namespace. We take
2855 care that each gfc_symbol node is called exactly once. */
2858 gfc_traverse_ns (gfc_namespace *ns, void (*func) (gfc_symbol *))
2861 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2863 traverse_ns (ns->sym_root, func);
2867 /* Return TRUE if the symbol is an automatic variable. */
2870 gfc_is_var_automatic (gfc_symbol *sym)
2872 /* Pointer and allocatable variables are never automatic. */
2873 if (sym->attr.pointer || sym->attr.allocatable)
2875 /* Check for arrays with non-constant size. */
2876 if (sym->attr.dimension && sym->as
2877 && !gfc_is_compile_time_shape (sym->as))
2879 /* Check for non-constant length character variables. */
2880 if (sym->ts.type == BT_CHARACTER
2882 && !gfc_is_constant_expr (sym->ts.cl->length))
2887 /* Given a symbol, mark it as SAVEd if it is allowed. */
2890 save_symbol (gfc_symbol *sym)
2893 if (sym->attr.use_assoc)
2896 if (sym->attr.in_common
2898 || sym->attr.flavor != FL_VARIABLE)
2900 /* Automatic objects are not saved. */
2901 if (gfc_is_var_automatic (sym))
2903 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2907 /* Mark those symbols which can be SAVEd as such. */
2910 gfc_save_all (gfc_namespace *ns)
2913 gfc_traverse_ns (ns, save_symbol);
2918 /* Make sure that no changes to symbols are pending. */
2921 gfc_symbol_state(void) {
2923 if (changed_syms != NULL)
2924 gfc_internal_error("Symbol changes still pending!");
2929 /************** Global symbol handling ************/
2932 /* Search a tree for the global symbol. */
2935 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2944 c = strcmp (name, symbol->name);
2948 symbol = (c < 0) ? symbol->left : symbol->right;
2955 /* Compare two global symbols. Used for managing the BB tree. */
2958 gsym_compare (void *_s1, void *_s2)
2960 gfc_gsymbol *s1, *s2;
2962 s1 = (gfc_gsymbol *) _s1;
2963 s2 = (gfc_gsymbol *) _s2;
2964 return strcmp (s1->name, s2->name);
2968 /* Get a global symbol, creating it if it doesn't exist. */
2971 gfc_get_gsymbol (const char *name)
2975 s = gfc_find_gsymbol (gfc_gsym_root, name);
2979 s = gfc_getmem (sizeof (gfc_gsymbol));
2980 s->type = GSYM_UNKNOWN;
2981 s->name = gfc_get_string (name);
2983 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2990 get_iso_c_binding_dt (int sym_id)
2992 gfc_dt_list *dt_list;
2994 dt_list = gfc_derived_types;
2996 /* Loop through the derived types in the name list, searching for
2997 the desired symbol from iso_c_binding. Search the parent namespaces
2998 if necessary and requested to (parent_flag). */
2999 while (dt_list != NULL)
3001 if (dt_list->derived->from_intmod != INTMOD_NONE
3002 && dt_list->derived->intmod_sym_id == sym_id)
3003 return dt_list->derived;
3005 dt_list = dt_list->next;
3012 /* Verifies that the given derived type symbol, derived_sym, is interoperable
3013 with C. This is necessary for any derived type that is BIND(C) and for
3014 derived types that are parameters to functions that are BIND(C). All
3015 fields of the derived type are required to be interoperable, and are tested
3016 for such. If an error occurs, the errors are reported here, allowing for
3017 multiple errors to be handled for a single derived type. */
3020 verify_bind_c_derived_type (gfc_symbol *derived_sym)
3022 gfc_component *curr_comp = NULL;
3023 try is_c_interop = FAILURE;
3024 try retval = SUCCESS;
3026 if (derived_sym == NULL)
3027 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
3028 "unexpectedly NULL");
3030 /* If we've already looked at this derived symbol, do not look at it again
3031 so we don't repeat warnings/errors. */
3032 if (derived_sym->ts.is_c_interop)
3035 /* The derived type must have the BIND attribute to be interoperable
3036 J3/04-007, Section 15.2.3. */
3037 if (derived_sym->attr.is_bind_c != 1)
3039 derived_sym->ts.is_c_interop = 0;
3040 gfc_error_now ("Derived type '%s' declared at %L must have the BIND "
3041 "attribute to be C interoperable", derived_sym->name,
3042 &(derived_sym->declared_at));
3046 curr_comp = derived_sym->components;
3048 /* TODO: is this really an error? */
3049 if (curr_comp == NULL)
3051 gfc_error ("Derived type '%s' at %L is empty",
3052 derived_sym->name, &(derived_sym->declared_at));
3056 /* Initialize the derived type as being C interoperable.
3057 If we find an error in the components, this will be set false. */
3058 derived_sym->ts.is_c_interop = 1;
3060 /* Loop through the list of components to verify that the kind of
3061 each is a C interoperable type. */
3064 /* The components cannot be pointers (fortran sense).
3065 J3/04-007, Section 15.2.3, C1505. */
3066 if (curr_comp->pointer != 0)
3068 gfc_error ("Component '%s' at %L cannot have the "
3069 "POINTER attribute because it is a member "
3070 "of the BIND(C) derived type '%s' at %L",
3071 curr_comp->name, &(curr_comp->loc),
3072 derived_sym->name, &(derived_sym->declared_at));
3076 /* The components cannot be allocatable.
3077 J3/04-007, Section 15.2.3, C1505. */
3078 if (curr_comp->allocatable != 0)
3080 gfc_error ("Component '%s' at %L cannot have the "
3081 "ALLOCATABLE attribute because it is a member "
3082 "of the BIND(C) derived type '%s' at %L",
3083 curr_comp->name, &(curr_comp->loc),
3084 derived_sym->name, &(derived_sym->declared_at));
3088 /* BIND(C) derived types must have interoperable components. */
3089 if (curr_comp->ts.type == BT_DERIVED
3090 && curr_comp->ts.derived->ts.is_iso_c != 1
3091 && curr_comp->ts.derived != derived_sym)
3093 /* This should be allowed; the draft says a derived-type can not
3094 have type parameters if it is has the BIND attribute. Type
3095 parameters seem to be for making parameterized derived types.
3096 There's no need to verify the type if it is c_ptr/c_funptr. */
3097 retval = verify_bind_c_derived_type (curr_comp->ts.derived);
3101 /* Grab the typespec for the given component and test the kind. */
3102 is_c_interop = verify_c_interop (&(curr_comp->ts), curr_comp->name,
3105 if (is_c_interop != SUCCESS)
3107 /* Report warning and continue since not fatal. The
3108 draft does specify a constraint that requires all fields
3109 to interoperate, but if the user says real(4), etc., it
3110 may interoperate with *something* in C, but the compiler
3111 most likely won't know exactly what. Further, it may not
3112 interoperate with the same data type(s) in C if the user
3113 recompiles with different flags (e.g., -m32 and -m64 on
3114 x86_64 and using integer(4) to claim interop with a
3116 if (derived_sym->attr.is_bind_c == 1)
3117 /* If the derived type is bind(c), all fields must be
3119 gfc_warning ("Component '%s' in derived type '%s' at %L "
3120 "may not be C interoperable, even though "
3121 "derived type '%s' is BIND(C)",
3122 curr_comp->name, derived_sym->name,
3123 &(curr_comp->loc), derived_sym->name);
3125 /* If derived type is param to bind(c) routine, or to one
3126 of the iso_c_binding procs, it must be interoperable, so
3127 all fields must interop too. */
3128 gfc_warning ("Component '%s' in derived type '%s' at %L "
3129 "may not be C interoperable",
3130 curr_comp->name, derived_sym->name,
3135 curr_comp = curr_comp->next;
3136 } while (curr_comp != NULL);
3139 /* Make sure we don't have conflicts with the attributes. */
3140 if (derived_sym->attr.access == ACCESS_PRIVATE)
3142 gfc_error ("Derived type '%s' at %L cannot be declared with both "
3143 "PRIVATE and BIND(C) attributes", derived_sym->name,
3144 &(derived_sym->declared_at));
3148 if (derived_sym->attr.sequence != 0)
3150 gfc_error ("Derived type '%s' at %L cannot have the SEQUENCE "
3151 "attribute because it is BIND(C)", derived_sym->name,
3152 &(derived_sym->declared_at));
3156 /* Mark the derived type as not being C interoperable if we found an
3157 error. If there were only warnings, proceed with the assumption
3158 it's interoperable. */
3159 if (retval == FAILURE)
3160 derived_sym->ts.is_c_interop = 0;
3166 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
3169 gen_special_c_interop_ptr (int ptr_id, const char *ptr_name,
3170 const char *module_name)
3172 gfc_symtree *tmp_symtree;
3173 gfc_symbol *tmp_sym;
3175 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, ptr_name);
3177 if (tmp_symtree != NULL)
3178 tmp_sym = tmp_symtree->n.sym;
3182 gfc_internal_error ("gen_special_c_interop_ptr(): Unable to "
3183 "create symbol for %s", ptr_name);
3186 /* Set up the symbol's important fields. Save attr required so we can
3187 initialize the ptr to NULL. */
3188 tmp_sym->attr.save = SAVE_EXPLICIT;
3189 tmp_sym->ts.is_c_interop = 1;
3190 tmp_sym->attr.is_c_interop = 1;
3191 tmp_sym->ts.is_iso_c = 1;
3192 tmp_sym->ts.type = BT_DERIVED;
3194 /* The c_ptr and c_funptr derived types will provide the
3195 definition for c_null_ptr and c_null_funptr, respectively. */
3196 if (ptr_id == ISOCBINDING_NULL_PTR)
3197 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_PTR);
3199 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3200 if (tmp_sym->ts.derived == NULL)
3202 /* This can occur if the user forgot to declare c_ptr or
3203 c_funptr and they're trying to use one of the procedures
3204 that has arg(s) of the missing type. In this case, a
3205 regular version of the thing should have been put in the
3207 generate_isocbinding_symbol (module_name, ptr_id == ISOCBINDING_NULL_PTR
3208 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR,
3209 (char *) (ptr_id == ISOCBINDING_NULL_PTR
3210 ? "_gfortran_iso_c_binding_c_ptr"
3211 : "_gfortran_iso_c_binding_c_funptr"));
3213 tmp_sym->ts.derived =
3214 get_iso_c_binding_dt (ptr_id == ISOCBINDING_NULL_PTR
3215 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR);
3218 /* Module name is some mangled version of iso_c_binding. */
3219 tmp_sym->module = gfc_get_string (module_name);
3221 /* Say it's from the iso_c_binding module. */
3222 tmp_sym->attr.is_iso_c = 1;
3224 tmp_sym->attr.use_assoc = 1;
3225 tmp_sym->attr.is_bind_c = 1;
3226 /* Set the binding_label. */
3227 sprintf (tmp_sym->binding_label, "%s_%s", module_name, tmp_sym->name);
3229 /* Set the c_address field of c_null_ptr and c_null_funptr to
3230 the value of NULL. */
3231 tmp_sym->value = gfc_get_expr ();
3232 tmp_sym->value->expr_type = EXPR_STRUCTURE;
3233 tmp_sym->value->ts.type = BT_DERIVED;
3234 tmp_sym->value->ts.derived = tmp_sym->ts.derived;
3235 tmp_sym->value->value.constructor = gfc_get_constructor ();
3236 /* This line will initialize the c_null_ptr/c_null_funptr
3237 c_address field to NULL. */
3238 tmp_sym->value->value.constructor->expr = gfc_int_expr (0);
3239 /* Must declare c_null_ptr and c_null_funptr as having the
3240 PARAMETER attribute so they can be used in init expressions. */
3241 tmp_sym->attr.flavor = FL_PARAMETER;
3247 /* Add a formal argument, gfc_formal_arglist, to the
3248 end of the given list of arguments. Set the reference to the
3249 provided symbol, param_sym, in the argument. */
3252 add_formal_arg (gfc_formal_arglist **head,
3253 gfc_formal_arglist **tail,
3254 gfc_formal_arglist *formal_arg,
3255 gfc_symbol *param_sym)
3257 /* Put in list, either as first arg or at the tail (curr arg). */
3259 *head = *tail = formal_arg;
3262 (*tail)->next = formal_arg;
3263 (*tail) = formal_arg;
3266 (*tail)->sym = param_sym;
3267 (*tail)->next = NULL;
3273 /* Generates a symbol representing the CPTR argument to an
3274 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3275 CPTR and add it to the provided argument list. */
3278 gen_cptr_param (gfc_formal_arglist **head,
3279 gfc_formal_arglist **tail,
3280 const char *module_name,
3281 gfc_namespace *ns, const char *c_ptr_name,
3284 gfc_symbol *param_sym = NULL;
3285 gfc_symbol *c_ptr_sym = NULL;
3286 gfc_symtree *param_symtree = NULL;
3287 gfc_formal_arglist *formal_arg = NULL;
3288 const char *c_ptr_in;
3289 const char *c_ptr_type = NULL;
3291 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3292 c_ptr_type = "_gfortran_iso_c_binding_c_funptr";
3294 c_ptr_type = "_gfortran_iso_c_binding_c_ptr";
3296 if(c_ptr_name == NULL)
3297 c_ptr_in = "gfc_cptr__";
3299 c_ptr_in = c_ptr_name;
3300 gfc_get_sym_tree (c_ptr_in, ns, ¶m_symtree);
3301 if (param_symtree != NULL)
3302 param_sym = param_symtree->n.sym;
3304 gfc_internal_error ("gen_cptr_param(): Unable to "
3305 "create symbol for %s", c_ptr_in);
3307 /* Set up the appropriate fields for the new c_ptr param sym. */
3309 param_sym->attr.flavor = FL_DERIVED;
3310 param_sym->ts.type = BT_DERIVED;
3311 param_sym->attr.intent = INTENT_IN;
3312 param_sym->attr.dummy = 1;
3314 /* This will pass the ptr to the iso_c routines as a (void *). */
3315 param_sym->attr.value = 1;
3316 param_sym->attr.use_assoc = 1;
3318 /* Get the symbol for c_ptr or c_funptr, no matter what it's name is
3320 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3321 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3323 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_PTR);
3324 if (c_ptr_sym == NULL)
3326 /* This can happen if the user did not define c_ptr but they are
3327 trying to use one of the iso_c_binding functions that need it. */
3328 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3329 generate_isocbinding_symbol (module_name, ISOCBINDING_FUNPTR,
3330 (char *)c_ptr_type);
3332 generate_isocbinding_symbol (module_name, ISOCBINDING_PTR,
3333 (char *)c_ptr_type);
3335 gfc_get_ha_symbol (c_ptr_type, &(c_ptr_sym));
3338 param_sym->ts.derived = c_ptr_sym;
3339 param_sym->module = gfc_get_string (module_name);
3341 /* Make new formal arg. */
3342 formal_arg = gfc_get_formal_arglist ();
3343 /* Add arg to list of formal args (the CPTR arg). */
3344 add_formal_arg (head, tail, formal_arg, param_sym);
3348 /* Generates a symbol representing the FPTR argument to an
3349 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3350 FPTR and add it to the provided argument list. */
3353 gen_fptr_param (gfc_formal_arglist **head,
3354 gfc_formal_arglist **tail,
3355 const char *module_name,
3356 gfc_namespace *ns, const char *f_ptr_name)
3358 gfc_symbol *param_sym = NULL;
3359 gfc_symtree *param_symtree = NULL;
3360 gfc_formal_arglist *formal_arg = NULL;
3361 const char *f_ptr_out = "gfc_fptr__";
3363 if (f_ptr_name != NULL)
3364 f_ptr_out = f_ptr_name;
3366 gfc_get_sym_tree (f_ptr_out, ns, ¶m_symtree);
3367 if (param_symtree != NULL)
3368 param_sym = param_symtree->n.sym;
3370 gfc_internal_error ("generateFPtrParam(): Unable to "
3371 "create symbol for %s", f_ptr_out);
3373 /* Set up the necessary fields for the fptr output param sym. */
3375 param_sym->attr.pointer = 1;
3376 param_sym->attr.dummy = 1;
3377 param_sym->attr.use_assoc = 1;
3379 /* ISO C Binding type to allow any pointer type as actual param. */
3380 param_sym->ts.type = BT_VOID;
3381 param_sym->module = gfc_get_string (module_name);
3384 formal_arg = gfc_get_formal_arglist ();
3385 /* Add arg to list of formal args. */
3386 add_formal_arg (head, tail, formal_arg, param_sym);
3390 /* Generates a symbol representing the optional SHAPE argument for the
3391 iso_c_binding c_f_pointer() procedure. Also, create a
3392 gfc_formal_arglist for the SHAPE and add it to the provided
3396 gen_shape_param (gfc_formal_arglist **head,
3397 gfc_formal_arglist **tail,
3398 const char *module_name,
3399 gfc_namespace *ns, const char *shape_param_name)
3401 gfc_symbol *param_sym = NULL;
3402 gfc_symtree *param_symtree = NULL;
3403 gfc_formal_arglist *formal_arg = NULL;
3404 const char *shape_param = "gfc_shape_array__";
3407 if (shape_param_name != NULL)
3408 shape_param = shape_param_name;
3410 gfc_get_sym_tree (shape_param, ns, ¶m_symtree);
3411 if (param_symtree != NULL)
3412 param_sym = param_symtree->n.sym;
3414 gfc_internal_error ("generateShapeParam(): Unable to "
3415 "create symbol for %s", shape_param);
3417 /* Set up the necessary fields for the shape input param sym. */
3419 param_sym->attr.dummy = 1;
3420 param_sym->attr.use_assoc = 1;
3422 /* Integer array, rank 1, describing the shape of the object. */
3423 param_sym->ts.type = BT_INTEGER;
3424 /* Initialize the kind to default integer. However, it will be overriden
3425 during resolution to match the kind of the SHAPE parameter given as
3426 the actual argument (to allow for any valid integer kind). */
3427 param_sym->ts.kind = gfc_default_integer_kind;
3428 param_sym->as = gfc_get_array_spec ();
3430 /* Clear out the dimension info for the array. */
3431 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3433 param_sym->as->lower[i] = NULL;
3434 param_sym->as->upper[i] = NULL;
3436 param_sym->as->rank = 1;
3437 param_sym->as->lower[0] = gfc_int_expr (1);
3439 /* The extent is unknown until we get it. The length give us
3440 the rank the incoming pointer. */
3441 param_sym->as->type = AS_ASSUMED_SHAPE;
3443 /* The arg is also optional; it is required iff the second arg
3444 (fptr) is to an array, otherwise, it's ignored. */
3445 param_sym->attr.optional = 1;
3446 param_sym->attr.intent = INTENT_IN;
3447 param_sym->attr.dimension = 1;
3448 param_sym->module = gfc_get_string (module_name);
3451 formal_arg = gfc_get_formal_arglist ();
3452 /* Add arg to list of formal args. */
3453 add_formal_arg (head, tail, formal_arg, param_sym);
3456 /* Add a procedure interface to the given symbol (i.e., store a
3457 reference to the list of formal arguments). */
3460 add_proc_interface (gfc_symbol *sym, ifsrc source,
3461 gfc_formal_arglist *formal)
3464 sym->formal = formal;
3465 sym->attr.if_source = source;
3469 /* Builds the parameter list for the iso_c_binding procedure
3470 c_f_pointer or c_f_procpointer. The old_sym typically refers to a
3471 generic version of either the c_f_pointer or c_f_procpointer
3472 functions. The new_proc_sym represents a "resolved" version of the
3473 symbol. The functions are resolved to match the types of their
3474 parameters; for example, c_f_pointer(cptr, fptr) would resolve to
3475 something similar to c_f_pointer_i4 if the type of data object fptr
3476 pointed to was a default integer. The actual name of the resolved
3477 procedure symbol is further mangled with the module name, etc., but
3478 the idea holds true. */
3481 build_formal_args (gfc_symbol *new_proc_sym,
3482 gfc_symbol *old_sym, int add_optional_arg)
3484 gfc_formal_arglist *head = NULL, *tail = NULL;
3485 gfc_namespace *parent_ns = NULL;
3487 parent_ns = gfc_current_ns;
3488 /* Create a new namespace, which will be the formal ns (namespace
3489 of the formal args). */
3490 gfc_current_ns = gfc_get_namespace(parent_ns, 0);
3491 gfc_current_ns->proc_name = new_proc_sym;
3493 /* Generate the params. */
3494 if ((old_sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3495 (old_sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3497 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3498 gfc_current_ns, "cptr", old_sym->intmod_sym_id);
3499 gen_fptr_param (&head, &tail, (const char *) new_proc_sym->module,
3500 gfc_current_ns, "fptr");
3502 /* If we're dealing with c_f_pointer, it has an optional third arg. */
3503 if (old_sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3505 gen_shape_param (&head, &tail,
3506 (const char *) new_proc_sym->module,
3507 gfc_current_ns, "shape");
3510 else if (old_sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
3512 /* c_associated has one required arg and one optional; both
3514 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3515 gfc_current_ns, "c_ptr_1", ISOCBINDING_ASSOCIATED);
3516 if (add_optional_arg)
3518 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3519 gfc_current_ns, "c_ptr_2", ISOCBINDING_ASSOCIATED);
3520 /* The last param is optional so mark it as such. */
3521 tail->sym->attr.optional = 1;
3525 /* Add the interface (store formal args to new_proc_sym). */
3526 add_proc_interface (new_proc_sym, IFSRC_DECL, head);
3528 /* Set up the formal_ns pointer to the one created for the
3529 new procedure so it'll get cleaned up during gfc_free_symbol(). */
3530 new_proc_sym->formal_ns = gfc_current_ns;
3532 gfc_current_ns = parent_ns;
3536 /* Generate the given set of C interoperable kind objects, or all
3537 interoperable kinds. This function will only be given kind objects
3538 for valid iso_c_binding defined types because this is verified when
3539 the 'use' statement is parsed. If the user gives an 'only' clause,
3540 the specific kinds are looked up; if they don't exist, an error is
3541 reported. If the user does not give an 'only' clause, all
3542 iso_c_binding symbols are generated. If a list of specific kinds
3543 is given, it must have a NULL in the first empty spot to mark the
3548 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
3551 char *name = (local_name && local_name[0]) ? local_name
3552 : c_interop_kinds_table[s].name;
3553 gfc_symtree *tmp_symtree = NULL;
3554 gfc_symbol *tmp_sym = NULL;
3555 gfc_dt_list **dt_list_ptr = NULL;
3556 gfc_component *tmp_comp = NULL;
3557 char comp_name[(GFC_MAX_SYMBOL_LEN * 2) + 1];
3560 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
3562 /* Already exists in this scope so don't re-add it.
3563 TODO: we should probably check that it's really the same symbol. */
3564 if (tmp_symtree != NULL)
3567 /* Create the sym tree in the current ns. */
3568 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree);
3570 tmp_sym = tmp_symtree->n.sym;
3572 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3575 /* Say what module this symbol belongs to. */
3576 tmp_sym->module = gfc_get_string (mod_name);
3577 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
3578 tmp_sym->intmod_sym_id = s;
3583 #define NAMED_INTCST(a,b,c) case a :
3584 #define NAMED_REALCST(a,b,c) case a :
3585 #define NAMED_CMPXCST(a,b,c) case a :
3586 #define NAMED_LOGCST(a,b,c) case a :
3587 #define NAMED_CHARKNDCST(a,b,c) case a :
3588 #include "iso-c-binding.def"
3590 tmp_sym->value = gfc_int_expr (c_interop_kinds_table[s].value);
3592 /* Initialize an integer constant expression node. */
3593 tmp_sym->attr.flavor = FL_PARAMETER;
3594 tmp_sym->ts.type = BT_INTEGER;
3595 tmp_sym->ts.kind = gfc_default_integer_kind;
3597 /* Mark this type as a C interoperable one. */
3598 tmp_sym->ts.is_c_interop = 1;
3599 tmp_sym->ts.is_iso_c = 1;
3600 tmp_sym->value->ts.is_c_interop = 1;
3601 tmp_sym->value->ts.is_iso_c = 1;
3602 tmp_sym->attr.is_c_interop = 1;
3604 /* Tell what f90 type this c interop kind is valid. */
3605 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
3607 /* Say it's from the iso_c_binding module. */
3608 tmp_sym->attr.is_iso_c = 1;
3610 /* Make it use associated. */
3611 tmp_sym->attr.use_assoc = 1;
3615 #define NAMED_CHARCST(a,b,c) case a :
3616 #include "iso-c-binding.def"
3618 /* Initialize an integer constant expression node for the
3619 length of the character. */
3620 tmp_sym->value = gfc_get_expr ();
3621 tmp_sym->value->expr_type = EXPR_CONSTANT;
3622 tmp_sym->value->ts.type = BT_CHARACTER;
3623 tmp_sym->value->ts.kind = gfc_default_character_kind;
3624 tmp_sym->value->where = gfc_current_locus;
3625 tmp_sym->value->ts.is_c_interop = 1;
3626 tmp_sym->value->ts.is_iso_c = 1;
3627 tmp_sym->value->value.character.length = 1;
3628 tmp_sym->value->value.character.string = gfc_getmem (2);
3629 tmp_sym->value->value.character.string[0]
3630 = (char) c_interop_kinds_table[s].value;
3631 tmp_sym->value->value.character.string[1] = '\0';
3633 /* May not need this in both attr and ts, but do need in
3634 attr for writing module file. */
3635 tmp_sym->attr.is_c_interop = 1;
3637 tmp_sym->attr.flavor = FL_PARAMETER;
3638 tmp_sym->ts.type = BT_CHARACTER;
3640 /* Need to set it to the C_CHAR kind. */
3641 tmp_sym->ts.kind = gfc_default_character_kind;
3643 /* Mark this type as a C interoperable one. */
3644 tmp_sym->ts.is_c_interop = 1;
3645 tmp_sym->ts.is_iso_c = 1;
3647 /* Tell what f90 type this c interop kind is valid. */
3648 tmp_sym->ts.f90_type = BT_CHARACTER;
3650 /* Say it's from the iso_c_binding module. */
3651 tmp_sym->attr.is_iso_c = 1;
3653 /* Make it use associated. */
3654 tmp_sym->attr.use_assoc = 1;
3657 case ISOCBINDING_PTR:
3658 case ISOCBINDING_FUNPTR:
3660 /* Initialize an integer constant expression node. */
3661 tmp_sym->attr.flavor = FL_DERIVED;
3662 tmp_sym->ts.is_c_interop = 1;
3663 tmp_sym->attr.is_c_interop = 1;
3664 tmp_sym->attr.is_iso_c = 1;
3665 tmp_sym->ts.is_iso_c = 1;
3666 tmp_sym->ts.type = BT_DERIVED;
3668 /* A derived type must have the bind attribute to be
3669 interoperable (J3/04-007, Section 15.2.3), even though
3670 the binding label is not used. */
3671 tmp_sym->attr.is_bind_c = 1;
3673 tmp_sym->attr.referenced = 1;
3675 tmp_sym->ts.derived = tmp_sym;
3677 /* Add the symbol created for the derived type to the current ns. */
3678 dt_list_ptr = &(gfc_derived_types);
3679 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
3680 dt_list_ptr = &((*dt_list_ptr)->next);
3682 /* There is already at least one derived type in the list, so append
3683 the one we're currently building for c_ptr or c_funptr. */
3684 if (*dt_list_ptr != NULL)
3685 dt_list_ptr = &((*dt_list_ptr)->next);
3686 (*dt_list_ptr) = gfc_get_dt_list ();
3687 (*dt_list_ptr)->derived = tmp_sym;
3688 (*dt_list_ptr)->next = NULL;
3690 /* Set up the component of the derived type, which will be
3691 an integer with kind equal to c_ptr_size. Mangle the name of
3692 the field for the c_address to prevent the curious user from
3693 trying to access it from Fortran. */
3694 sprintf (comp_name, "__%s_%s", tmp_sym->name, "c_address");
3695 gfc_add_component (tmp_sym, comp_name, &tmp_comp);
3696 if (tmp_comp == NULL)
3697 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3698 "create component for c_address");
3700 tmp_comp->ts.type = BT_INTEGER;
3702 /* Set this because the module will need to read/write this field. */
3703 tmp_comp->ts.f90_type = BT_INTEGER;
3705 /* The kinds for c_ptr and c_funptr are the same. */
3706 index = get_c_kind ("c_ptr", c_interop_kinds_table);
3707 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
3709 tmp_comp->pointer = 0;
3710 tmp_comp->dimension = 0;
3712 /* Mark the component as C interoperable. */
3713 tmp_comp->ts.is_c_interop = 1;
3715 /* Make it use associated (iso_c_binding module). */
3716 tmp_sym->attr.use_assoc = 1;
3719 case ISOCBINDING_NULL_PTR:
3720 case ISOCBINDING_NULL_FUNPTR:
3721 gen_special_c_interop_ptr (s, name, mod_name);
3724 case ISOCBINDING_F_POINTER:
3725 case ISOCBINDING_ASSOCIATED:
3726 case ISOCBINDING_LOC:
3727 case ISOCBINDING_FUNLOC:
3728 case ISOCBINDING_F_PROCPOINTER:
3730 tmp_sym->attr.proc = PROC_MODULE;
3732 /* Use the procedure's name as it is in the iso_c_binding module for
3733 setting the binding label in case the user renamed the symbol. */
3734 sprintf (tmp_sym->binding_label, "%s_%s", mod_name,
3735 c_interop_kinds_table[s].name);
3736 tmp_sym->attr.is_iso_c = 1;
3737 if (s == ISOCBINDING_F_POINTER || s == ISOCBINDING_F_PROCPOINTER)
3738 tmp_sym->attr.subroutine = 1;
3741 /* TODO! This needs to be finished more for the expr of the
3742 function or something!
3743 This may not need to be here, because trying to do c_loc
3745 if (s == ISOCBINDING_ASSOCIATED)
3747 tmp_sym->attr.function = 1;
3748 tmp_sym->ts.type = BT_LOGICAL;
3749 tmp_sym->ts.kind = gfc_default_logical_kind;
3750 tmp_sym->result = tmp_sym;
3754 /* Here, we're taking the simple approach. We're defining
3755 c_loc as an external identifier so the compiler will put
3756 what we expect on the stack for the address we want the
3758 tmp_sym->ts.type = BT_DERIVED;
3759 if (s == ISOCBINDING_LOC)
3760 tmp_sym->ts.derived =
3761 get_iso_c_binding_dt (ISOCBINDING_PTR);
3763 tmp_sym->ts.derived =
3764 get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3766 if (tmp_sym->ts.derived == NULL)
3768 /* Create the necessary derived type so we can continue
3769 processing the file. */
3770 generate_isocbinding_symbol
3771 (mod_name, s == ISOCBINDING_FUNLOC
3772 ? ISOCBINDING_FUNPTR : ISOCBINDING_PTR,
3773 (char *)(s == ISOCBINDING_FUNLOC
3774 ? "_gfortran_iso_c_binding_c_funptr"
3775 : "_gfortran_iso_c_binding_c_ptr"));
3776 tmp_sym->ts.derived =
3777 get_iso_c_binding_dt (s == ISOCBINDING_FUNLOC
3778 ? ISOCBINDING_FUNPTR
3782 /* The function result is itself (no result clause). */
3783 tmp_sym->result = tmp_sym;
3784 tmp_sym->attr.external = 1;
3785 tmp_sym->attr.use_assoc = 0;
3786 tmp_sym->attr.if_source = IFSRC_UNKNOWN;
3787 tmp_sym->attr.proc = PROC_UNKNOWN;
3791 tmp_sym->attr.flavor = FL_PROCEDURE;
3792 tmp_sym->attr.contained = 0;
3794 /* Try using this builder routine, with the new and old symbols
3795 both being the generic iso_c proc sym being created. This
3796 will create the formal args (and the new namespace for them).
3797 Don't build an arg list for c_loc because we're going to treat
3798 c_loc as an external procedure. */
3799 if (s != ISOCBINDING_LOC && s != ISOCBINDING_FUNLOC)
3800 /* The 1 says to add any optional args, if applicable. */
3801 build_formal_args (tmp_sym, tmp_sym, 1);
3803 /* Set this after setting up the symbol, to prevent error messages. */
3804 tmp_sym->attr.use_assoc = 1;
3806 /* This symbol will not be referenced directly. It will be
3807 resolved to the implementation for the given f90 kind. */
3808 tmp_sym->attr.referenced = 0;
3818 /* Creates a new symbol based off of an old iso_c symbol, with a new
3819 binding label. This function can be used to create a new,
3820 resolved, version of a procedure symbol for c_f_pointer or
3821 c_f_procpointer that is based on the generic symbols. A new
3822 parameter list is created for the new symbol using
3823 build_formal_args(). The add_optional_flag specifies whether the
3824 to add the optional SHAPE argument. The new symbol is
3828 get_iso_c_sym (gfc_symbol *old_sym, char *new_name,
3829 char *new_binding_label, int add_optional_arg)
3831 gfc_symtree *new_symtree = NULL;
3833 /* See if we have a symbol by that name already available, looking
3834 through any parent namespaces. */
3835 gfc_find_sym_tree (new_name, gfc_current_ns, 1, &new_symtree);
3836 if (new_symtree != NULL)
3837 /* Return the existing symbol. */
3838 return new_symtree->n.sym;
3840 /* Create the symtree/symbol, with attempted host association. */
3841 gfc_get_ha_sym_tree (new_name, &new_symtree);
3842 if (new_symtree == NULL)
3843 gfc_internal_error ("get_iso_c_sym(): Unable to create "
3844 "symtree for '%s'", new_name);
3846 /* Now fill in the fields of the resolved symbol with the old sym. */
3847 strcpy (new_symtree->n.sym->binding_label, new_binding_label);
3848 new_symtree->n.sym->attr = old_sym->attr;
3849 new_symtree->n.sym->ts = old_sym->ts;
3850 new_symtree->n.sym->module = gfc_get_string (old_sym->module);
3851 /* Build the formal arg list. */
3852 build_formal_args (new_symtree->n.sym, old_sym, add_optional_arg);
3854 gfc_commit_symbol (new_symtree->n.sym);
3856 return new_symtree->n.sym;