1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
30 #include "constructor.h"
33 /* Strings for all symbol attributes. We use these for dumping the
34 parse tree, in error messages, and also when reading and writing
37 const mstring flavors[] =
39 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
40 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
41 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
42 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
43 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
47 const mstring procedures[] =
49 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
50 minit ("MODULE-PROC", PROC_MODULE),
51 minit ("INTERNAL-PROC", PROC_INTERNAL),
52 minit ("DUMMY-PROC", PROC_DUMMY),
53 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
54 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
55 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
59 const mstring intents[] =
61 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
62 minit ("IN", INTENT_IN),
63 minit ("OUT", INTENT_OUT),
64 minit ("INOUT", INTENT_INOUT),
68 const mstring access_types[] =
70 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
71 minit ("PUBLIC", ACCESS_PUBLIC),
72 minit ("PRIVATE", ACCESS_PRIVATE),
76 const mstring ifsrc_types[] =
78 minit ("UNKNOWN", IFSRC_UNKNOWN),
79 minit ("DECL", IFSRC_DECL),
80 minit ("BODY", IFSRC_IFBODY)
83 const mstring save_status[] =
85 minit ("UNKNOWN", SAVE_NONE),
86 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
87 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
90 /* This is to make sure the backend generates setup code in the correct
93 static int next_dummy_order = 1;
96 gfc_namespace *gfc_current_ns;
97 gfc_namespace *gfc_global_ns_list;
99 gfc_gsymbol *gfc_gsym_root = NULL;
101 static gfc_symbol *changed_syms = NULL;
103 gfc_dt_list *gfc_derived_types;
106 /* List of tentative typebound-procedures. */
108 typedef struct tentative_tbp
110 gfc_typebound_proc *proc;
111 struct tentative_tbp *next;
115 static tentative_tbp *tentative_tbp_list = NULL;
118 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
120 /* The following static variable indicates whether a particular element has
121 been explicitly set or not. */
123 static int new_flag[GFC_LETTERS];
126 /* Handle a correctly parsed IMPLICIT NONE. */
129 gfc_set_implicit_none (void)
133 if (gfc_current_ns->seen_implicit_none)
135 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
139 gfc_current_ns->seen_implicit_none = 1;
141 for (i = 0; i < GFC_LETTERS; i++)
143 gfc_clear_ts (&gfc_current_ns->default_type[i]);
144 gfc_current_ns->set_flag[i] = 1;
149 /* Reset the implicit range flags. */
152 gfc_clear_new_implicit (void)
156 for (i = 0; i < GFC_LETTERS; i++)
161 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
164 gfc_add_new_implicit_range (int c1, int c2)
171 for (i = c1; i <= c2; i++)
175 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
187 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
188 the new implicit types back into the existing types will work. */
191 gfc_merge_new_implicit (gfc_typespec *ts)
195 if (gfc_current_ns->seen_implicit_none)
197 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
201 for (i = 0; i < GFC_LETTERS; i++)
205 if (gfc_current_ns->set_flag[i])
207 gfc_error ("Letter %c already has an IMPLICIT type at %C",
212 gfc_current_ns->default_type[i] = *ts;
213 gfc_current_ns->implicit_loc[i] = gfc_current_locus;
214 gfc_current_ns->set_flag[i] = 1;
221 /* Given a symbol, return a pointer to the typespec for its default type. */
224 gfc_get_default_type (const char *name, gfc_namespace *ns)
230 if (gfc_option.flag_allow_leading_underscore && letter == '_')
231 gfc_internal_error ("Option -fallow-leading-underscore is for use only by "
232 "gfortran developers, and should not be used for "
233 "implicitly typed variables");
235 if (letter < 'a' || letter > 'z')
236 gfc_internal_error ("gfc_get_default_type(): Bad symbol '%s'", name);
241 return &ns->default_type[letter - 'a'];
245 /* Given a pointer to a symbol, set its type according to the first
246 letter of its name. Fails if the letter in question has no default
250 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
254 if (sym->ts.type != BT_UNKNOWN)
255 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
257 ts = gfc_get_default_type (sym->name, ns);
259 if (ts->type == BT_UNKNOWN)
261 if (error_flag && !sym->attr.untyped)
263 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
264 sym->name, &sym->declared_at);
265 sym->attr.untyped = 1; /* Ensure we only give an error once. */
272 sym->attr.implicit_type = 1;
274 if (ts->type == BT_CHARACTER && ts->u.cl)
275 sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
277 if (sym->attr.is_bind_c == 1)
279 /* BIND(C) variables should not be implicitly declared. */
280 gfc_warning_now ("Implicitly declared BIND(C) variable '%s' at %L may "
281 "not be C interoperable", sym->name, &sym->declared_at);
282 sym->ts.f90_type = sym->ts.type;
285 if (sym->attr.dummy != 0)
287 if (sym->ns->proc_name != NULL
288 && (sym->ns->proc_name->attr.subroutine != 0
289 || sym->ns->proc_name->attr.function != 0)
290 && sym->ns->proc_name->attr.is_bind_c != 0)
292 /* Dummy args to a BIND(C) routine may not be interoperable if
293 they are implicitly typed. */
294 gfc_warning_now ("Implicitly declared variable '%s' at %L may not "
295 "be C interoperable but it is a dummy argument to "
296 "the BIND(C) procedure '%s' at %L", sym->name,
297 &(sym->declared_at), sym->ns->proc_name->name,
298 &(sym->ns->proc_name->declared_at));
299 sym->ts.f90_type = sym->ts.type;
307 /* This function is called from parse.c(parse_progunit) to check the
308 type of the function is not implicitly typed in the host namespace
309 and to implicitly type the function result, if necessary. */
312 gfc_check_function_type (gfc_namespace *ns)
314 gfc_symbol *proc = ns->proc_name;
316 if (!proc->attr.contained || proc->result->attr.implicit_type)
319 if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
321 if (gfc_set_default_type (proc->result, 0, gfc_current_ns)
324 if (proc->result != proc)
326 proc->ts = proc->result->ts;
327 proc->as = gfc_copy_array_spec (proc->result->as);
328 proc->attr.dimension = proc->result->attr.dimension;
329 proc->attr.pointer = proc->result->attr.pointer;
330 proc->attr.allocatable = proc->result->attr.allocatable;
333 else if (!proc->result->attr.proc_pointer)
335 gfc_error ("Function result '%s' at %L has no IMPLICIT type",
336 proc->result->name, &proc->result->declared_at);
337 proc->result->attr.untyped = 1;
343 /******************** Symbol attribute stuff *********************/
345 /* This is a generic conflict-checker. We do this to avoid having a
346 single conflict in two places. */
348 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
349 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
350 #define conf_std(a, b, std) if (attr->a && attr->b)\
359 check_conflict (symbol_attribute *attr, const char *name, locus *where)
361 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
362 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
363 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
364 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
365 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
366 *privat = "PRIVATE", *recursive = "RECURSIVE",
367 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
368 *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
369 *function = "FUNCTION", *subroutine = "SUBROUTINE",
370 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
371 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
372 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
373 *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
374 *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
375 *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION";
376 static const char *threadprivate = "THREADPRIVATE";
382 where = &gfc_current_locus;
384 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
388 standard = GFC_STD_F2003;
392 /* Check for attributes not allowed in a BLOCK DATA. */
393 if (gfc_current_state () == COMP_BLOCK_DATA)
397 if (attr->in_namelist)
399 if (attr->allocatable)
405 if (attr->access == ACCESS_PRIVATE)
407 if (attr->access == ACCESS_PUBLIC)
409 if (attr->intent != INTENT_UNKNOWN)
415 ("%s attribute not allowed in BLOCK DATA program unit at %L",
421 if (attr->save == SAVE_EXPLICIT)
424 conf (in_common, save);
427 switch (attr->flavor)
435 a1 = gfc_code2string (flavors, attr->flavor);
440 /* Conflicts between SAVE and PROCEDURE will be checked at
441 resolution stage, see "resolve_fl_procedure". */
450 conf (dummy, intrinsic);
451 conf (dummy, threadprivate);
452 conf (pointer, target);
453 conf (pointer, intrinsic);
454 conf (pointer, elemental);
455 conf (allocatable, elemental);
457 conf (target, external);
458 conf (target, intrinsic);
460 if (!attr->if_source)
461 conf (external, dimension); /* See Fortran 95's R504. */
463 conf (external, intrinsic);
464 conf (entry, intrinsic);
466 if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
467 conf (external, subroutine);
469 if (attr->proc_pointer && gfc_notify_std (GFC_STD_F2003,
470 "Fortran 2003: Procedure pointer at %C") == FAILURE)
473 conf (allocatable, pointer);
474 conf_std (allocatable, dummy, GFC_STD_F2003);
475 conf_std (allocatable, function, GFC_STD_F2003);
476 conf_std (allocatable, result, GFC_STD_F2003);
477 conf (elemental, recursive);
479 conf (in_common, dummy);
480 conf (in_common, allocatable);
481 conf (in_common, codimension);
482 conf (in_common, result);
484 conf (dummy, result);
486 conf (in_equivalence, use_assoc);
487 conf (in_equivalence, codimension);
488 conf (in_equivalence, dummy);
489 conf (in_equivalence, target);
490 conf (in_equivalence, pointer);
491 conf (in_equivalence, function);
492 conf (in_equivalence, result);
493 conf (in_equivalence, entry);
494 conf (in_equivalence, allocatable);
495 conf (in_equivalence, threadprivate);
497 conf (in_namelist, pointer);
498 conf (in_namelist, allocatable);
500 conf (entry, result);
502 conf (function, subroutine);
504 if (!function && !subroutine)
505 conf (is_bind_c, dummy);
507 conf (is_bind_c, cray_pointer);
508 conf (is_bind_c, cray_pointee);
509 conf (is_bind_c, codimension);
510 conf (is_bind_c, allocatable);
511 conf (is_bind_c, elemental);
513 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
514 Parameter conflict caught below. Also, value cannot be specified
515 for a dummy procedure. */
517 /* Cray pointer/pointee conflicts. */
518 conf (cray_pointer, cray_pointee);
519 conf (cray_pointer, dimension);
520 conf (cray_pointer, codimension);
521 conf (cray_pointer, pointer);
522 conf (cray_pointer, target);
523 conf (cray_pointer, allocatable);
524 conf (cray_pointer, external);
525 conf (cray_pointer, intrinsic);
526 conf (cray_pointer, in_namelist);
527 conf (cray_pointer, function);
528 conf (cray_pointer, subroutine);
529 conf (cray_pointer, entry);
531 conf (cray_pointee, allocatable);
532 conf (cray_pointer, codimension);
533 conf (cray_pointee, intent);
534 conf (cray_pointee, optional);
535 conf (cray_pointee, dummy);
536 conf (cray_pointee, target);
537 conf (cray_pointee, intrinsic);
538 conf (cray_pointee, pointer);
539 conf (cray_pointee, entry);
540 conf (cray_pointee, in_common);
541 conf (cray_pointee, in_equivalence);
542 conf (cray_pointee, threadprivate);
545 conf (data, function);
547 conf (data, allocatable);
548 conf (data, use_assoc);
550 conf (value, pointer)
551 conf (value, allocatable)
552 conf (value, subroutine)
553 conf (value, function)
554 conf (value, volatile_)
555 conf (value, dimension)
556 conf (value, codimension)
557 conf (value, external)
559 conf (codimension, result)
562 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
565 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
569 conf (is_protected, intrinsic)
570 conf (is_protected, external)
571 conf (is_protected, in_common)
573 conf (asynchronous, intrinsic)
574 conf (asynchronous, external)
576 conf (volatile_, intrinsic)
577 conf (volatile_, external)
579 if (attr->volatile_ && attr->intent == INTENT_IN)
586 conf (procedure, allocatable)
587 conf (procedure, dimension)
588 conf (procedure, codimension)
589 conf (procedure, intrinsic)
590 conf (procedure, is_protected)
591 conf (procedure, target)
592 conf (procedure, value)
593 conf (procedure, volatile_)
594 conf (procedure, asynchronous)
595 conf (procedure, entry)
597 a1 = gfc_code2string (flavors, attr->flavor);
599 if (attr->in_namelist
600 && attr->flavor != FL_VARIABLE
601 && attr->flavor != FL_PROCEDURE
602 && attr->flavor != FL_UNKNOWN)
608 switch (attr->flavor)
618 conf2 (asynchronous);
620 conf2 (is_protected);
630 conf2 (threadprivate);
632 if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
634 a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
635 gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
642 gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
656 /* Conflicts with INTENT, SAVE and RESULT will be checked
657 at resolution stage, see "resolve_fl_procedure". */
659 if (attr->subroutine)
665 conf2 (asynchronous);
670 conf2 (threadprivate);
673 if (!attr->proc_pointer)
678 case PROC_ST_FUNCTION:
688 conf2 (threadprivate);
708 conf2 (threadprivate);
711 if (attr->intent != INTENT_UNKNOWN)
727 conf2 (is_protected);
733 conf2 (asynchronous);
734 conf2 (threadprivate);
749 gfc_error ("%s attribute conflicts with %s attribute at %L",
752 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
753 a1, a2, name, where);
760 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
761 "with %s attribute at %L", a1, a2,
766 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
767 "with %s attribute in '%s' at %L",
768 a1, a2, name, where);
777 /* Mark a symbol as referenced. */
780 gfc_set_sym_referenced (gfc_symbol *sym)
783 if (sym->attr.referenced)
786 sym->attr.referenced = 1;
788 /* Remember which order dummy variables are accessed in. */
790 sym->dummy_order = next_dummy_order++;
794 /* Common subroutine called by attribute changing subroutines in order
795 to prevent them from changing a symbol that has been
796 use-associated. Returns zero if it is OK to change the symbol,
800 check_used (symbol_attribute *attr, const char *name, locus *where)
803 if (attr->use_assoc == 0)
807 where = &gfc_current_locus;
810 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
813 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
820 /* Generate an error because of a duplicate attribute. */
823 duplicate_attr (const char *attr, locus *where)
827 where = &gfc_current_locus;
829 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
834 gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
835 locus *where ATTRIBUTE_UNUSED)
837 attr->ext_attr |= 1 << ext_attr;
842 /* Called from decl.c (attr_decl1) to check attributes, when declared
846 gfc_add_attribute (symbol_attribute *attr, locus *where)
848 if (check_used (attr, NULL, where))
851 return check_conflict (attr, NULL, where);
856 gfc_add_allocatable (symbol_attribute *attr, locus *where)
859 if (check_used (attr, NULL, where))
862 if (attr->allocatable)
864 duplicate_attr ("ALLOCATABLE", where);
868 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
869 && gfc_find_state (COMP_INTERFACE) == FAILURE)
871 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
876 attr->allocatable = 1;
877 return check_conflict (attr, NULL, where);
882 gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
885 if (check_used (attr, name, where))
888 if (attr->codimension)
890 duplicate_attr ("CODIMENSION", where);
894 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
895 && gfc_find_state (COMP_INTERFACE) == FAILURE)
897 gfc_error ("CODIMENSION specified for '%s' outside its INTERFACE body "
898 "at %L", name, where);
902 attr->codimension = 1;
903 return check_conflict (attr, name, where);
908 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
911 if (check_used (attr, name, where))
916 duplicate_attr ("DIMENSION", where);
920 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
921 && gfc_find_state (COMP_INTERFACE) == FAILURE)
923 gfc_error ("DIMENSION specified for '%s' outside its INTERFACE body "
924 "at %L", name, where);
929 return check_conflict (attr, name, where);
934 gfc_add_external (symbol_attribute *attr, locus *where)
937 if (check_used (attr, NULL, where))
942 duplicate_attr ("EXTERNAL", where);
946 if (attr->pointer && attr->if_source != IFSRC_IFBODY)
949 attr->proc_pointer = 1;
954 return check_conflict (attr, NULL, where);
959 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
962 if (check_used (attr, NULL, where))
967 duplicate_attr ("INTRINSIC", where);
973 return check_conflict (attr, NULL, where);
978 gfc_add_optional (symbol_attribute *attr, locus *where)
981 if (check_used (attr, NULL, where))
986 duplicate_attr ("OPTIONAL", where);
991 return check_conflict (attr, NULL, where);
996 gfc_add_pointer (symbol_attribute *attr, locus *where)
999 if (check_used (attr, NULL, where))
1002 if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
1003 && gfc_find_state (COMP_INTERFACE) == FAILURE))
1005 duplicate_attr ("POINTER", where);
1009 if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
1010 || (attr->if_source == IFSRC_IFBODY
1011 && gfc_find_state (COMP_INTERFACE) == FAILURE))
1012 attr->proc_pointer = 1;
1016 return check_conflict (attr, NULL, where);
1021 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
1024 if (check_used (attr, NULL, where))
1027 attr->cray_pointer = 1;
1028 return check_conflict (attr, NULL, where);
1033 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
1036 if (check_used (attr, NULL, where))
1039 if (attr->cray_pointee)
1041 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1042 " statements", where);
1046 attr->cray_pointee = 1;
1047 return check_conflict (attr, NULL, where);
1052 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
1054 if (check_used (attr, name, where))
1057 if (attr->is_protected)
1059 if (gfc_notify_std (GFC_STD_LEGACY,
1060 "Duplicate PROTECTED attribute specified at %L",
1066 attr->is_protected = 1;
1067 return check_conflict (attr, name, where);
1072 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
1075 if (check_used (attr, name, where))
1079 return check_conflict (attr, name, where);
1084 gfc_add_save (symbol_attribute *attr, const char *name, locus *where)
1087 if (check_used (attr, name, where))
1090 if (gfc_pure (NULL))
1093 ("SAVE attribute at %L cannot be specified in a PURE procedure",
1098 if (attr->save == SAVE_EXPLICIT && !attr->vtab)
1100 if (gfc_notify_std (GFC_STD_LEGACY,
1101 "Duplicate SAVE attribute specified at %L",
1107 attr->save = SAVE_EXPLICIT;
1108 return check_conflict (attr, name, where);
1113 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1116 if (check_used (attr, name, where))
1121 if (gfc_notify_std (GFC_STD_LEGACY,
1122 "Duplicate VALUE attribute specified at %L",
1129 return check_conflict (attr, name, where);
1134 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1136 /* No check_used needed as 11.2.1 of the F2003 standard allows
1137 that the local identifier made accessible by a use statement can be
1138 given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1140 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1141 if (gfc_notify_std (GFC_STD_LEGACY,
1142 "Duplicate VOLATILE attribute specified at %L", where)
1146 attr->volatile_ = 1;
1147 attr->volatile_ns = gfc_current_ns;
1148 return check_conflict (attr, name, where);
1153 gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
1155 /* No check_used needed as 11.2.1 of the F2003 standard allows
1156 that the local identifier made accessible by a use statement can be
1157 given a ASYNCHRONOUS attribute. */
1159 if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
1160 if (gfc_notify_std (GFC_STD_LEGACY,
1161 "Duplicate ASYNCHRONOUS attribute specified at %L",
1165 attr->asynchronous = 1;
1166 attr->asynchronous_ns = gfc_current_ns;
1167 return check_conflict (attr, name, where);
1172 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1175 if (check_used (attr, name, where))
1178 if (attr->threadprivate)
1180 duplicate_attr ("THREADPRIVATE", where);
1184 attr->threadprivate = 1;
1185 return check_conflict (attr, name, where);
1190 gfc_add_target (symbol_attribute *attr, locus *where)
1193 if (check_used (attr, NULL, where))
1198 duplicate_attr ("TARGET", where);
1203 return check_conflict (attr, NULL, where);
1208 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1211 if (check_used (attr, name, where))
1214 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1216 return check_conflict (attr, name, where);
1221 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1224 if (check_used (attr, name, where))
1227 /* Duplicate attribute already checked for. */
1228 attr->in_common = 1;
1229 return check_conflict (attr, name, where);
1234 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1237 /* Duplicate attribute already checked for. */
1238 attr->in_equivalence = 1;
1239 if (check_conflict (attr, name, where) == FAILURE)
1242 if (attr->flavor == FL_VARIABLE)
1245 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1250 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1253 if (check_used (attr, name, where))
1257 return check_conflict (attr, name, where);
1262 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1265 attr->in_namelist = 1;
1266 return check_conflict (attr, name, where);
1271 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1274 if (check_used (attr, name, where))
1278 return check_conflict (attr, name, where);
1283 gfc_add_elemental (symbol_attribute *attr, locus *where)
1286 if (check_used (attr, NULL, where))
1289 if (attr->elemental)
1291 duplicate_attr ("ELEMENTAL", where);
1295 attr->elemental = 1;
1296 return check_conflict (attr, NULL, where);
1301 gfc_add_pure (symbol_attribute *attr, locus *where)
1304 if (check_used (attr, NULL, where))
1309 duplicate_attr ("PURE", where);
1314 return check_conflict (attr, NULL, where);
1319 gfc_add_recursive (symbol_attribute *attr, locus *where)
1322 if (check_used (attr, NULL, where))
1325 if (attr->recursive)
1327 duplicate_attr ("RECURSIVE", where);
1331 attr->recursive = 1;
1332 return check_conflict (attr, NULL, where);
1337 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1340 if (check_used (attr, name, where))
1345 duplicate_attr ("ENTRY", where);
1350 return check_conflict (attr, name, where);
1355 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1358 if (attr->flavor != FL_PROCEDURE
1359 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1363 return check_conflict (attr, name, where);
1368 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1371 if (attr->flavor != FL_PROCEDURE
1372 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1375 attr->subroutine = 1;
1376 return check_conflict (attr, name, where);
1381 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1384 if (attr->flavor != FL_PROCEDURE
1385 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1389 return check_conflict (attr, name, where);
1394 gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1397 if (check_used (attr, NULL, where))
1400 if (attr->flavor != FL_PROCEDURE
1401 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1404 if (attr->procedure)
1406 duplicate_attr ("PROCEDURE", where);
1410 attr->procedure = 1;
1412 return check_conflict (attr, NULL, where);
1417 gfc_add_abstract (symbol_attribute* attr, locus* where)
1421 duplicate_attr ("ABSTRACT", where);
1430 /* Flavors are special because some flavors are not what Fortran
1431 considers attributes and can be reaffirmed multiple times. */
1434 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1438 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1439 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1440 || f == FL_NAMELIST) && check_used (attr, name, where))
1443 if (attr->flavor == f && f == FL_VARIABLE)
1446 if (attr->flavor != FL_UNKNOWN)
1449 where = &gfc_current_locus;
1452 gfc_error ("%s attribute of '%s' conflicts with %s attribute at %L",
1453 gfc_code2string (flavors, attr->flavor), name,
1454 gfc_code2string (flavors, f), where);
1456 gfc_error ("%s attribute conflicts with %s attribute at %L",
1457 gfc_code2string (flavors, attr->flavor),
1458 gfc_code2string (flavors, f), where);
1465 return check_conflict (attr, name, where);
1470 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1471 const char *name, locus *where)
1474 if (check_used (attr, name, where))
1477 if (attr->flavor != FL_PROCEDURE
1478 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1482 where = &gfc_current_locus;
1484 if (attr->proc != PROC_UNKNOWN)
1486 gfc_error ("%s procedure at %L is already declared as %s procedure",
1487 gfc_code2string (procedures, t), where,
1488 gfc_code2string (procedures, attr->proc));
1495 /* Statement functions are always scalar and functions. */
1496 if (t == PROC_ST_FUNCTION
1497 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1498 || attr->dimension))
1501 return check_conflict (attr, name, where);
1506 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1509 if (check_used (attr, NULL, where))
1512 if (attr->intent == INTENT_UNKNOWN)
1514 attr->intent = intent;
1515 return check_conflict (attr, NULL, where);
1519 where = &gfc_current_locus;
1521 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1522 gfc_intent_string (attr->intent),
1523 gfc_intent_string (intent), where);
1529 /* No checks for use-association in public and private statements. */
1532 gfc_add_access (symbol_attribute *attr, gfc_access access,
1533 const char *name, locus *where)
1536 if (attr->access == ACCESS_UNKNOWN
1537 || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
1539 attr->access = access;
1540 return check_conflict (attr, name, where);
1544 where = &gfc_current_locus;
1545 gfc_error ("ACCESS specification at %L was already specified", where);
1551 /* Set the is_bind_c field for the given symbol_attribute. */
1554 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1555 int is_proc_lang_bind_spec)
1558 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1559 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1560 "variables or common blocks", where);
1561 else if (attr->is_bind_c)
1562 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1564 attr->is_bind_c = 1;
1567 where = &gfc_current_locus;
1569 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BIND(C) at %L", where)
1573 return check_conflict (attr, name, where);
1577 /* Set the extension field for the given symbol_attribute. */
1580 gfc_add_extension (symbol_attribute *attr, locus *where)
1583 where = &gfc_current_locus;
1585 if (attr->extension)
1586 gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
1588 attr->extension = 1;
1590 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: EXTENDS at %L", where)
1599 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1600 gfc_formal_arglist * formal, locus *where)
1603 if (check_used (&sym->attr, sym->name, where))
1607 where = &gfc_current_locus;
1609 if (sym->attr.if_source != IFSRC_UNKNOWN
1610 && sym->attr.if_source != IFSRC_DECL)
1612 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1617 if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1619 gfc_error ("'%s' at %L has attributes specified outside its INTERFACE "
1620 "body", sym->name, where);
1624 sym->formal = formal;
1625 sym->attr.if_source = source;
1631 /* Add a type to a symbol. */
1634 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1640 where = &gfc_current_locus;
1643 type = sym->result->ts.type;
1645 type = sym->ts.type;
1647 if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
1648 type = sym->ns->proc_name->ts.type;
1650 if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type))
1652 gfc_error ("Symbol '%s' at %L already has basic type of %s", sym->name,
1653 where, gfc_basic_typename (type));
1657 if (sym->attr.procedure && sym->ts.interface)
1659 gfc_error ("Procedure '%s' at %L may not have basic type of %s",
1660 sym->name, where, gfc_basic_typename (ts->type));
1664 flavor = sym->attr.flavor;
1666 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1667 || flavor == FL_LABEL
1668 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
1669 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1671 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1680 /* Clears all attributes. */
1683 gfc_clear_attr (symbol_attribute *attr)
1685 memset (attr, 0, sizeof (symbol_attribute));
1689 /* Check for missing attributes in the new symbol. Currently does
1690 nothing, but it's not clear that it is unnecessary yet. */
1693 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1694 locus *where ATTRIBUTE_UNUSED)
1701 /* Copy an attribute to a symbol attribute, bit by bit. Some
1702 attributes have a lot of side-effects but cannot be present given
1703 where we are called from, so we ignore some bits. */
1706 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
1708 int is_proc_lang_bind_spec;
1710 /* In line with the other attributes, we only add bits but do not remove
1711 them; cf. also PR 41034. */
1712 dest->ext_attr |= src->ext_attr;
1714 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1717 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1719 if (src->codimension && gfc_add_codimension (dest, NULL, where) == FAILURE)
1721 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1723 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1725 if (src->is_protected && gfc_add_protected (dest, NULL, where) == FAILURE)
1727 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1729 if (src->value && gfc_add_value (dest, NULL, where) == FAILURE)
1731 if (src->volatile_ && gfc_add_volatile (dest, NULL, where) == FAILURE)
1733 if (src->asynchronous && gfc_add_asynchronous (dest, NULL, where) == FAILURE)
1735 if (src->threadprivate
1736 && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
1738 if (src->target && gfc_add_target (dest, where) == FAILURE)
1740 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1742 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1747 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1750 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1753 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1755 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1757 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1760 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1762 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1764 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1766 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1769 if (src->flavor != FL_UNKNOWN
1770 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1773 if (src->intent != INTENT_UNKNOWN
1774 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1777 if (src->access != ACCESS_UNKNOWN
1778 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1781 if (gfc_missing_attr (dest, where) == FAILURE)
1784 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1786 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1789 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1791 && gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec)
1795 if (src->is_c_interop)
1796 dest->is_c_interop = 1;
1800 if (src->external && gfc_add_external (dest, where) == FAILURE)
1802 if (src->intrinsic && gfc_add_intrinsic (dest, where) == FAILURE)
1804 if (src->proc_pointer)
1805 dest->proc_pointer = 1;
1814 /************** Component name management ************/
1816 /* Component names of a derived type form their own little namespaces
1817 that are separate from all other spaces. The space is composed of
1818 a singly linked list of gfc_component structures whose head is
1819 located in the parent symbol. */
1822 /* Add a component name to a symbol. The call fails if the name is
1823 already present. On success, the component pointer is modified to
1824 point to the additional component structure. */
1827 gfc_add_component (gfc_symbol *sym, const char *name,
1828 gfc_component **component)
1830 gfc_component *p, *tail;
1834 for (p = sym->components; p; p = p->next)
1836 if (strcmp (p->name, name) == 0)
1838 gfc_error ("Component '%s' at %C already declared at %L",
1846 if (sym->attr.extension
1847 && gfc_find_component (sym->components->ts.u.derived, name, true, true))
1849 gfc_error ("Component '%s' at %C already in the parent type "
1850 "at %L", name, &sym->components->ts.u.derived->declared_at);
1854 /* Allocate a new component. */
1855 p = gfc_get_component ();
1858 sym->components = p;
1862 p->name = gfc_get_string (name);
1863 p->loc = gfc_current_locus;
1864 p->ts.type = BT_UNKNOWN;
1871 /* Recursive function to switch derived types of all symbol in a
1875 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
1883 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
1884 sym->ts.u.derived = to;
1886 switch_types (st->left, from, to);
1887 switch_types (st->right, from, to);
1891 /* This subroutine is called when a derived type is used in order to
1892 make the final determination about which version to use. The
1893 standard requires that a type be defined before it is 'used', but
1894 such types can appear in IMPLICIT statements before the actual
1895 definition. 'Using' in this context means declaring a variable to
1896 be that type or using the type constructor.
1898 If a type is used and the components haven't been defined, then we
1899 have to have a derived type in a parent unit. We find the node in
1900 the other namespace and point the symtree node in this namespace to
1901 that node. Further reference to this name point to the correct
1902 node. If we can't find the node in a parent namespace, then we have
1905 This subroutine takes a pointer to a symbol node and returns a
1906 pointer to the translated node or NULL for an error. Usually there
1907 is no translation and we return the node we were passed. */
1910 gfc_use_derived (gfc_symbol *sym)
1917 if (sym->components != NULL || sym->attr.zero_comp)
1918 return sym; /* Already defined. */
1920 if (sym->ns->parent == NULL)
1923 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1925 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1929 if (s == NULL || s->attr.flavor != FL_DERIVED)
1932 /* Get rid of symbol sym, translating all references to s. */
1933 for (i = 0; i < GFC_LETTERS; i++)
1935 t = &sym->ns->default_type[i];
1936 if (t->u.derived == sym)
1940 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1945 /* Unlink from list of modified symbols. */
1946 gfc_commit_symbol (sym);
1948 switch_types (sym->ns->sym_root, sym, s);
1950 /* TODO: Also have to replace sym -> s in other lists like
1951 namelists, common lists and interface lists. */
1952 gfc_free_symbol (sym);
1957 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1963 /* Given a derived type node and a component name, try to locate the
1964 component structure. Returns the NULL pointer if the component is
1965 not found or the components are private. If noaccess is set, no access
1969 gfc_find_component (gfc_symbol *sym, const char *name,
1970 bool noaccess, bool silent)
1977 sym = gfc_use_derived (sym);
1982 for (p = sym->components; p; p = p->next)
1983 if (strcmp (p->name, name) == 0)
1987 && sym->attr.extension
1988 && sym->components->ts.type == BT_DERIVED)
1990 p = gfc_find_component (sym->components->ts.u.derived, name,
1992 /* Do not overwrite the error. */
1997 if (p == NULL && !silent)
1998 gfc_error ("'%s' at %C is not a member of the '%s' structure",
2001 else if (sym->attr.use_assoc && !noaccess)
2003 bool is_parent_comp = sym->attr.extension && (p == sym->components);
2004 if (p->attr.access == ACCESS_PRIVATE ||
2005 (p->attr.access != ACCESS_PUBLIC
2006 && sym->component_access == ACCESS_PRIVATE
2007 && !is_parent_comp))
2010 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
2020 /* Given a symbol, free all of the component structures and everything
2024 free_components (gfc_component *p)
2032 gfc_free_array_spec (p->as);
2033 gfc_free_expr (p->initializer);
2040 /******************** Statement label management ********************/
2042 /* Comparison function for statement labels, used for managing the
2046 compare_st_labels (void *a1, void *b1)
2048 int a = ((gfc_st_label *) a1)->value;
2049 int b = ((gfc_st_label *) b1)->value;
2055 /* Free a single gfc_st_label structure, making sure the tree is not
2056 messed up. This function is called only when some parse error
2060 gfc_free_st_label (gfc_st_label *label)
2066 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
2068 if (label->format != NULL)
2069 gfc_free_expr (label->format);
2075 /* Free a whole tree of gfc_st_label structures. */
2078 free_st_labels (gfc_st_label *label)
2084 free_st_labels (label->left);
2085 free_st_labels (label->right);
2087 if (label->format != NULL)
2088 gfc_free_expr (label->format);
2093 /* Given a label number, search for and return a pointer to the label
2094 structure, creating it if it does not exist. */
2097 gfc_get_st_label (int labelno)
2102 /* Find the namespace of the scoping unit:
2103 If we're in a BLOCK construct, jump to the parent namespace. */
2104 ns = gfc_current_ns;
2105 while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
2108 /* First see if the label is already in this namespace. */
2112 if (lp->value == labelno)
2115 if (lp->value < labelno)
2121 lp = XCNEW (gfc_st_label);
2123 lp->value = labelno;
2124 lp->defined = ST_LABEL_UNKNOWN;
2125 lp->referenced = ST_LABEL_UNKNOWN;
2127 gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
2133 /* Called when a statement with a statement label is about to be
2134 accepted. We add the label to the list of the current namespace,
2135 making sure it hasn't been defined previously and referenced
2139 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
2143 labelno = lp->value;
2145 if (lp->defined != ST_LABEL_UNKNOWN)
2146 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
2147 &lp->where, label_locus);
2150 lp->where = *label_locus;
2154 case ST_LABEL_FORMAT:
2155 if (lp->referenced == ST_LABEL_TARGET)
2156 gfc_error ("Label %d at %C already referenced as branch target",
2159 lp->defined = ST_LABEL_FORMAT;
2163 case ST_LABEL_TARGET:
2164 if (lp->referenced == ST_LABEL_FORMAT)
2165 gfc_error ("Label %d at %C already referenced as a format label",
2168 lp->defined = ST_LABEL_TARGET;
2173 lp->defined = ST_LABEL_BAD_TARGET;
2174 lp->referenced = ST_LABEL_BAD_TARGET;
2180 /* Reference a label. Given a label and its type, see if that
2181 reference is consistent with what is known about that label,
2182 updating the unknown state. Returns FAILURE if something goes
2186 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2188 gfc_sl_type label_type;
2195 labelno = lp->value;
2197 if (lp->defined != ST_LABEL_UNKNOWN)
2198 label_type = lp->defined;
2201 label_type = lp->referenced;
2202 lp->where = gfc_current_locus;
2205 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
2207 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2212 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
2213 && type == ST_LABEL_FORMAT)
2215 gfc_error ("Label %d at %C previously used as branch target", labelno);
2220 lp->referenced = type;
2228 /*******A helper function for creating new expressions*************/
2232 gfc_lval_expr_from_sym (gfc_symbol *sym)
2235 lval = gfc_get_expr ();
2236 lval->expr_type = EXPR_VARIABLE;
2237 lval->where = sym->declared_at;
2239 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
2241 /* It will always be a full array. */
2242 lval->rank = sym->as ? sym->as->rank : 0;
2245 lval->ref = gfc_get_ref ();
2246 lval->ref->type = REF_ARRAY;
2247 lval->ref->u.ar.type = AR_FULL;
2248 lval->ref->u.ar.dimen = lval->rank;
2249 lval->ref->u.ar.where = sym->declared_at;
2250 lval->ref->u.ar.as = sym->as;
2257 /************** Symbol table management subroutines ****************/
2259 /* Basic details: Fortran 95 requires a potentially unlimited number
2260 of distinct namespaces when compiling a program unit. This case
2261 occurs during a compilation of internal subprograms because all of
2262 the internal subprograms must be read before we can start
2263 generating code for the host.
2265 Given the tricky nature of the Fortran grammar, we must be able to
2266 undo changes made to a symbol table if the current interpretation
2267 of a statement is found to be incorrect. Whenever a symbol is
2268 looked up, we make a copy of it and link to it. All of these
2269 symbols are kept in a singly linked list so that we can commit or
2270 undo the changes at a later time.
2272 A symtree may point to a symbol node outside of its namespace. In
2273 this case, that symbol has been used as a host associated variable
2274 at some previous time. */
2276 /* Allocate a new namespace structure. Copies the implicit types from
2277 PARENT if PARENT_TYPES is set. */
2280 gfc_get_namespace (gfc_namespace *parent, int parent_types)
2287 ns = XCNEW (gfc_namespace);
2288 ns->sym_root = NULL;
2289 ns->uop_root = NULL;
2290 ns->tb_sym_root = NULL;
2291 ns->finalizers = NULL;
2292 ns->default_access = ACCESS_UNKNOWN;
2293 ns->parent = parent;
2295 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
2297 ns->operator_access[in] = ACCESS_UNKNOWN;
2298 ns->tb_op[in] = NULL;
2301 /* Initialize default implicit types. */
2302 for (i = 'a'; i <= 'z'; i++)
2304 ns->set_flag[i - 'a'] = 0;
2305 ts = &ns->default_type[i - 'a'];
2307 if (parent_types && ns->parent != NULL)
2309 /* Copy parent settings. */
2310 *ts = ns->parent->default_type[i - 'a'];
2314 if (gfc_option.flag_implicit_none != 0)
2320 if ('i' <= i && i <= 'n')
2322 ts->type = BT_INTEGER;
2323 ts->kind = gfc_default_integer_kind;
2328 ts->kind = gfc_default_real_kind;
2338 /* Comparison function for symtree nodes. */
2341 compare_symtree (void *_st1, void *_st2)
2343 gfc_symtree *st1, *st2;
2345 st1 = (gfc_symtree *) _st1;
2346 st2 = (gfc_symtree *) _st2;
2348 return strcmp (st1->name, st2->name);
2352 /* Allocate a new symtree node and associate it with the new symbol. */
2355 gfc_new_symtree (gfc_symtree **root, const char *name)
2359 st = XCNEW (gfc_symtree);
2360 st->name = gfc_get_string (name);
2362 gfc_insert_bbt (root, st, compare_symtree);
2367 /* Delete a symbol from the tree. Does not free the symbol itself! */
2370 gfc_delete_symtree (gfc_symtree **root, const char *name)
2372 gfc_symtree st, *st0;
2374 st0 = gfc_find_symtree (*root, name);
2376 st.name = gfc_get_string (name);
2377 gfc_delete_bbt (root, &st, compare_symtree);
2383 /* Given a root symtree node and a name, try to find the symbol within
2384 the namespace. Returns NULL if the symbol is not found. */
2387 gfc_find_symtree (gfc_symtree *st, const char *name)
2393 c = strcmp (name, st->name);
2397 st = (c < 0) ? st->left : st->right;
2404 /* Return a symtree node with a name that is guaranteed to be unique
2405 within the namespace and corresponds to an illegal fortran name. */
2408 gfc_get_unique_symtree (gfc_namespace *ns)
2410 char name[GFC_MAX_SYMBOL_LEN + 1];
2411 static int serial = 0;
2413 sprintf (name, "@%d", serial++);
2414 return gfc_new_symtree (&ns->sym_root, name);
2418 /* Given a name find a user operator node, creating it if it doesn't
2419 exist. These are much simpler than symbols because they can't be
2420 ambiguous with one another. */
2423 gfc_get_uop (const char *name)
2428 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
2432 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
2434 uop = st->n.uop = XCNEW (gfc_user_op);
2435 uop->name = gfc_get_string (name);
2436 uop->access = ACCESS_UNKNOWN;
2437 uop->ns = gfc_current_ns;
2443 /* Given a name find the user operator node. Returns NULL if it does
2447 gfc_find_uop (const char *name, gfc_namespace *ns)
2452 ns = gfc_current_ns;
2454 st = gfc_find_symtree (ns->uop_root, name);
2455 return (st == NULL) ? NULL : st->n.uop;
2459 /* Remove a gfc_symbol structure and everything it points to. */
2462 gfc_free_symbol (gfc_symbol *sym)
2468 gfc_free_array_spec (sym->as);
2470 free_components (sym->components);
2472 gfc_free_expr (sym->value);
2474 gfc_free_namelist (sym->namelist);
2476 gfc_free_namespace (sym->formal_ns);
2478 if (!sym->attr.generic_copy)
2479 gfc_free_interface (sym->generic);
2481 gfc_free_formal_arglist (sym->formal);
2483 gfc_free_namespace (sym->f2k_derived);
2489 /* Allocate and initialize a new symbol node. */
2492 gfc_new_symbol (const char *name, gfc_namespace *ns)
2496 p = XCNEW (gfc_symbol);
2498 gfc_clear_ts (&p->ts);
2499 gfc_clear_attr (&p->attr);
2502 p->declared_at = gfc_current_locus;
2504 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2505 gfc_internal_error ("new_symbol(): Symbol name too long");
2507 p->name = gfc_get_string (name);
2509 /* Make sure flags for symbol being C bound are clear initially. */
2510 p->attr.is_bind_c = 0;
2511 p->attr.is_iso_c = 0;
2512 /* Make sure the binding label field has a Nul char to start. */
2513 p->binding_label[0] = '\0';
2515 /* Clear the ptrs we may need. */
2516 p->common_block = NULL;
2517 p->f2k_derived = NULL;
2523 /* Generate an error if a symbol is ambiguous. */
2526 ambiguous_symbol (const char *name, gfc_symtree *st)
2529 if (st->n.sym->module)
2530 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2531 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
2533 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2534 "from current program unit", name, st->n.sym->name);
2538 /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
2539 selector on the stack. If yes, replace it by the corresponding temporary. */
2542 select_type_insert_tmp (gfc_symtree **st)
2544 gfc_select_type_stack *stack = select_type_stack;
2545 for (; stack; stack = stack->prev)
2546 if ((*st)->n.sym == stack->selector && stack->tmp)
2551 /* Search for a symtree starting in the current namespace, resorting to
2552 any parent namespaces if requested by a nonzero parent_flag.
2553 Returns nonzero if the name is ambiguous. */
2556 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2557 gfc_symtree **result)
2562 ns = gfc_current_ns;
2566 st = gfc_find_symtree (ns->sym_root, name);
2569 select_type_insert_tmp (&st);
2572 /* Ambiguous generic interfaces are permitted, as long
2573 as the specific interfaces are different. */
2574 if (st->ambiguous && !st->n.sym->attr.generic)
2576 ambiguous_symbol (name, st);
2595 /* Same, but returns the symbol instead. */
2598 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
2599 gfc_symbol **result)
2604 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2609 *result = st->n.sym;
2615 /* Save symbol with the information necessary to back it out. */
2618 save_symbol_data (gfc_symbol *sym)
2621 if (sym->gfc_new || sym->old_symbol != NULL)
2624 sym->old_symbol = XCNEW (gfc_symbol);
2625 *(sym->old_symbol) = *sym;
2627 sym->tlink = changed_syms;
2632 /* Given a name, find a symbol, or create it if it does not exist yet
2633 in the current namespace. If the symbol is found we make sure that
2636 The integer return code indicates
2638 1 The symbol name was ambiguous
2639 2 The name meant to be established was already host associated.
2641 So if the return value is nonzero, then an error was issued. */
2644 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
2645 bool allow_subroutine)
2650 /* This doesn't usually happen during resolution. */
2652 ns = gfc_current_ns;
2654 /* Try to find the symbol in ns. */
2655 st = gfc_find_symtree (ns->sym_root, name);
2659 /* If not there, create a new symbol. */
2660 p = gfc_new_symbol (name, ns);
2662 /* Add to the list of tentative symbols. */
2663 p->old_symbol = NULL;
2664 p->tlink = changed_syms;
2669 st = gfc_new_symtree (&ns->sym_root, name);
2676 /* Make sure the existing symbol is OK. Ambiguous
2677 generic interfaces are permitted, as long as the
2678 specific interfaces are different. */
2679 if (st->ambiguous && !st->n.sym->attr.generic)
2681 ambiguous_symbol (name, st);
2686 if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
2687 && !(allow_subroutine && p->attr.subroutine)
2688 && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
2689 && (ns->has_import_set || p->attr.imported)))
2691 /* Symbol is from another namespace. */
2692 gfc_error ("Symbol '%s' at %C has already been host associated",
2699 /* Copy in case this symbol is changed. */
2700 save_symbol_data (p);
2709 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
2714 i = gfc_get_sym_tree (name, ns, &st, false);
2719 *result = st->n.sym;
2726 /* Subroutine that searches for a symbol, creating it if it doesn't
2727 exist, but tries to host-associate the symbol if possible. */
2730 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
2735 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2739 save_symbol_data (st->n.sym);
2744 if (gfc_current_ns->parent != NULL)
2746 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2757 return gfc_get_sym_tree (name, gfc_current_ns, result, false);
2762 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
2767 i = gfc_get_ha_sym_tree (name, &st);
2770 *result = st->n.sym;
2777 /* Return true if both symbols could refer to the same data object. Does
2778 not take account of aliasing due to equivalence statements. */
2781 gfc_symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym)
2783 /* Aliasing isn't possible if the symbols have different base types. */
2784 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2787 /* Pointers can point to other pointers, target objects and allocatable
2788 objects. Two allocatable objects cannot share the same storage. */
2789 if (lsym->attr.pointer
2790 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2792 if (lsym->attr.target && rsym->attr.pointer)
2794 if (lsym->attr.allocatable && rsym->attr.pointer)
2801 /* Undoes all the changes made to symbols in the current statement.
2802 This subroutine is made simpler due to the fact that attributes are
2803 never removed once added. */
2806 gfc_undo_symbols (void)
2808 gfc_symbol *p, *q, *old;
2809 tentative_tbp *tbp, *tbq;
2811 for (p = changed_syms; p; p = q)
2817 /* Symbol was new. */
2818 if (p->attr.in_common && p->common_block && p->common_block->head)
2820 /* If the symbol was added to any common block, it
2821 needs to be removed to stop the resolver looking
2822 for a (possibly) dead symbol. */
2824 if (p->common_block->head == p)
2825 p->common_block->head = p->common_next;
2828 gfc_symbol *cparent, *csym;
2830 cparent = p->common_block->head;
2831 csym = cparent->common_next;
2836 csym = csym->common_next;
2839 gcc_assert(cparent->common_next == p);
2841 cparent->common_next = csym->common_next;
2845 gfc_delete_symtree (&p->ns->sym_root, p->name);
2849 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2851 gfc_free_symbol (p);
2855 /* Restore previous state of symbol. Just copy simple stuff. */
2857 old = p->old_symbol;
2859 p->ts.type = old->ts.type;
2860 p->ts.kind = old->ts.kind;
2862 p->attr = old->attr;
2864 if (p->value != old->value)
2866 gfc_free_expr (old->value);
2870 if (p->as != old->as)
2873 gfc_free_array_spec (p->as);
2877 p->generic = old->generic;
2878 p->component_access = old->component_access;
2880 if (p->namelist != NULL && old->namelist == NULL)
2882 gfc_free_namelist (p->namelist);
2887 if (p->namelist_tail != old->namelist_tail)
2889 gfc_free_namelist (old->namelist_tail);
2890 old->namelist_tail->next = NULL;
2894 p->namelist_tail = old->namelist_tail;
2896 if (p->formal != old->formal)
2898 gfc_free_formal_arglist (p->formal);
2899 p->formal = old->formal;
2902 gfc_free (p->old_symbol);
2903 p->old_symbol = NULL;
2907 changed_syms = NULL;
2909 for (tbp = tentative_tbp_list; tbp; tbp = tbq)
2912 /* Procedure is already marked `error' by default. */
2915 tentative_tbp_list = NULL;
2919 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2920 components of old_symbol that might need deallocation are the "allocatables"
2921 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2922 namelist_tail. In case these differ between old_symbol and sym, it's just
2923 because sym->namelist has gotten a few more items. */
2926 free_old_symbol (gfc_symbol *sym)
2929 if (sym->old_symbol == NULL)
2932 if (sym->old_symbol->as != sym->as)
2933 gfc_free_array_spec (sym->old_symbol->as);
2935 if (sym->old_symbol->value != sym->value)
2936 gfc_free_expr (sym->old_symbol->value);
2938 if (sym->old_symbol->formal != sym->formal)
2939 gfc_free_formal_arglist (sym->old_symbol->formal);
2941 gfc_free (sym->old_symbol);
2942 sym->old_symbol = NULL;
2946 /* Makes the changes made in the current statement permanent-- gets
2947 rid of undo information. */
2950 gfc_commit_symbols (void)
2953 tentative_tbp *tbp, *tbq;
2955 for (p = changed_syms; p; p = q)
2961 free_old_symbol (p);
2963 changed_syms = NULL;
2965 for (tbp = tentative_tbp_list; tbp; tbp = tbq)
2968 tbp->proc->error = 0;
2971 tentative_tbp_list = NULL;
2975 /* Makes the changes made in one symbol permanent -- gets rid of undo
2979 gfc_commit_symbol (gfc_symbol *sym)
2983 if (changed_syms == sym)
2984 changed_syms = sym->tlink;
2987 for (p = changed_syms; p; p = p->tlink)
2988 if (p->tlink == sym)
2990 p->tlink = sym->tlink;
2999 free_old_symbol (sym);
3003 /* Recursively free trees containing type-bound procedures. */
3006 free_tb_tree (gfc_symtree *t)
3011 free_tb_tree (t->left);
3012 free_tb_tree (t->right);
3014 /* TODO: Free type-bound procedure structs themselves; probably needs some
3015 sort of ref-counting mechanism. */
3021 /* Recursive function that deletes an entire tree and all the common
3022 head structures it points to. */
3025 free_common_tree (gfc_symtree * common_tree)
3027 if (common_tree == NULL)
3030 free_common_tree (common_tree->left);
3031 free_common_tree (common_tree->right);
3033 gfc_free (common_tree);
3037 /* Recursive function that deletes an entire tree and all the user
3038 operator nodes that it contains. */
3041 free_uop_tree (gfc_symtree *uop_tree)
3043 if (uop_tree == NULL)
3046 free_uop_tree (uop_tree->left);
3047 free_uop_tree (uop_tree->right);
3049 gfc_free_interface (uop_tree->n.uop->op);
3050 gfc_free (uop_tree->n.uop);
3051 gfc_free (uop_tree);
3055 /* Recursive function that deletes an entire tree and all the symbols
3056 that it contains. */
3059 free_sym_tree (gfc_symtree *sym_tree)
3064 if (sym_tree == NULL)
3067 free_sym_tree (sym_tree->left);
3068 free_sym_tree (sym_tree->right);
3070 sym = sym_tree->n.sym;
3074 gfc_internal_error ("free_sym_tree(): Negative refs");
3076 if (sym->formal_ns != NULL && sym->refs == 1)
3078 /* As formal_ns contains a reference to sym, delete formal_ns just
3079 before the deletion of sym. */
3080 ns = sym->formal_ns;
3081 sym->formal_ns = NULL;
3082 gfc_free_namespace (ns);
3084 else if (sym->refs == 0)
3086 /* Go ahead and delete the symbol. */
3087 gfc_free_symbol (sym);
3090 gfc_free (sym_tree);
3094 /* Free the derived type list. */
3097 gfc_free_dt_list (void)
3099 gfc_dt_list *dt, *n;
3101 for (dt = gfc_derived_types; dt; dt = n)
3107 gfc_derived_types = NULL;
3111 /* Free the gfc_equiv_info's. */
3114 gfc_free_equiv_infos (gfc_equiv_info *s)
3118 gfc_free_equiv_infos (s->next);
3123 /* Free the gfc_equiv_lists. */
3126 gfc_free_equiv_lists (gfc_equiv_list *l)
3130 gfc_free_equiv_lists (l->next);
3131 gfc_free_equiv_infos (l->equiv);
3136 /* Free a finalizer procedure list. */
3139 gfc_free_finalizer (gfc_finalizer* el)
3145 --el->proc_sym->refs;
3146 if (!el->proc_sym->refs)
3147 gfc_free_symbol (el->proc_sym);
3155 gfc_free_finalizer_list (gfc_finalizer* list)
3159 gfc_finalizer* current = list;
3161 gfc_free_finalizer (current);
3166 /* Create a new gfc_charlen structure and add it to a namespace.
3167 If 'old_cl' is given, the newly created charlen will be a copy of it. */
3170 gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
3173 cl = gfc_get_charlen ();
3175 /* Put into namespace. */
3176 cl->next = ns->cl_list;
3182 cl->length = gfc_copy_expr (old_cl->length);
3183 cl->length_from_typespec = old_cl->length_from_typespec;
3184 cl->backend_decl = old_cl->backend_decl;
3185 cl->passed_length = old_cl->passed_length;
3186 cl->resolved = old_cl->resolved;
3193 /* Free the charlen list from cl to end (end is not freed).
3194 Free the whole list if end is NULL. */
3196 void gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
3200 for (; cl != end; cl = cl2)
3205 gfc_free_expr (cl->length);
3211 /* Free a namespace structure and everything below it. Interface
3212 lists associated with intrinsic operators are not freed. These are
3213 taken care of when a specific name is freed. */
3216 gfc_free_namespace (gfc_namespace *ns)
3218 gfc_namespace *p, *q;
3227 gcc_assert (ns->refs == 0);
3229 gfc_free_statements (ns->code);
3231 free_sym_tree (ns->sym_root);
3232 free_uop_tree (ns->uop_root);
3233 free_common_tree (ns->common_root);
3234 free_tb_tree (ns->tb_sym_root);
3235 free_tb_tree (ns->tb_uop_root);
3236 gfc_free_finalizer_list (ns->finalizers);
3237 gfc_free_charlen (ns->cl_list, NULL);
3238 free_st_labels (ns->st_labels);
3240 gfc_free_equiv (ns->equiv);
3241 gfc_free_equiv_lists (ns->equiv_lists);
3242 gfc_free_use_stmts (ns->use_stmts);
3244 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
3245 gfc_free_interface (ns->op[i]);
3247 gfc_free_data (ns->data);
3251 /* Recursively free any contained namespaces. */
3256 gfc_free_namespace (q);
3262 gfc_symbol_init_2 (void)
3265 gfc_current_ns = gfc_get_namespace (NULL, 0);
3270 gfc_symbol_done_2 (void)
3273 gfc_free_namespace (gfc_current_ns);
3274 gfc_current_ns = NULL;
3275 gfc_free_dt_list ();
3279 /* Clear mark bits from symbol nodes associated with a symtree node. */
3282 clear_sym_mark (gfc_symtree *st)
3285 st->n.sym->mark = 0;
3289 /* Recursively traverse the symtree nodes. */
3292 gfc_traverse_symtree (gfc_symtree *st, void (*func) (gfc_symtree *))
3297 gfc_traverse_symtree (st->left, func);
3299 gfc_traverse_symtree (st->right, func);
3303 /* Recursive namespace traversal function. */
3306 traverse_ns (gfc_symtree *st, void (*func) (gfc_symbol *))
3312 traverse_ns (st->left, func);
3314 if (st->n.sym->mark == 0)
3315 (*func) (st->n.sym);
3316 st->n.sym->mark = 1;
3318 traverse_ns (st->right, func);
3322 /* Call a given function for all symbols in the namespace. We take
3323 care that each gfc_symbol node is called exactly once. */
3326 gfc_traverse_ns (gfc_namespace *ns, void (*func) (gfc_symbol *))
3329 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
3331 traverse_ns (ns->sym_root, func);
3335 /* Return TRUE when name is the name of an intrinsic type. */
3338 gfc_is_intrinsic_typename (const char *name)
3340 if (strcmp (name, "integer") == 0
3341 || strcmp (name, "real") == 0
3342 || strcmp (name, "character") == 0
3343 || strcmp (name, "logical") == 0
3344 || strcmp (name, "complex") == 0
3345 || strcmp (name, "doubleprecision") == 0
3346 || strcmp (name, "doublecomplex") == 0)
3353 /* Return TRUE if the symbol is an automatic variable. */
3356 gfc_is_var_automatic (gfc_symbol *sym)
3358 /* Pointer and allocatable variables are never automatic. */
3359 if (sym->attr.pointer || sym->attr.allocatable)
3361 /* Check for arrays with non-constant size. */
3362 if (sym->attr.dimension && sym->as
3363 && !gfc_is_compile_time_shape (sym->as))
3365 /* Check for non-constant length character variables. */
3366 if (sym->ts.type == BT_CHARACTER
3368 && !gfc_is_constant_expr (sym->ts.u.cl->length))
3373 /* Given a symbol, mark it as SAVEd if it is allowed. */
3376 save_symbol (gfc_symbol *sym)
3379 if (sym->attr.use_assoc)
3382 if (sym->attr.in_common
3385 || sym->attr.flavor != FL_VARIABLE)
3387 /* Automatic objects are not saved. */
3388 if (gfc_is_var_automatic (sym))
3390 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
3394 /* Mark those symbols which can be SAVEd as such. */
3397 gfc_save_all (gfc_namespace *ns)
3399 gfc_traverse_ns (ns, save_symbol);
3404 /* Make sure that no changes to symbols are pending. */
3407 gfc_symbol_state(void) {
3409 if (changed_syms != NULL)
3410 gfc_internal_error("Symbol changes still pending!");
3415 /************** Global symbol handling ************/
3418 /* Search a tree for the global symbol. */
3421 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
3430 c = strcmp (name, symbol->name);
3434 symbol = (c < 0) ? symbol->left : symbol->right;
3441 /* Compare two global symbols. Used for managing the BB tree. */
3444 gsym_compare (void *_s1, void *_s2)
3446 gfc_gsymbol *s1, *s2;
3448 s1 = (gfc_gsymbol *) _s1;
3449 s2 = (gfc_gsymbol *) _s2;
3450 return strcmp (s1->name, s2->name);
3454 /* Get a global symbol, creating it if it doesn't exist. */
3457 gfc_get_gsymbol (const char *name)
3461 s = gfc_find_gsymbol (gfc_gsym_root, name);
3465 s = XCNEW (gfc_gsymbol);
3466 s->type = GSYM_UNKNOWN;
3467 s->name = gfc_get_string (name);
3469 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
3476 get_iso_c_binding_dt (int sym_id)
3478 gfc_dt_list *dt_list;
3480 dt_list = gfc_derived_types;
3482 /* Loop through the derived types in the name list, searching for
3483 the desired symbol from iso_c_binding. Search the parent namespaces
3484 if necessary and requested to (parent_flag). */
3485 while (dt_list != NULL)
3487 if (dt_list->derived->from_intmod != INTMOD_NONE
3488 && dt_list->derived->intmod_sym_id == sym_id)
3489 return dt_list->derived;
3491 dt_list = dt_list->next;
3498 /* Verifies that the given derived type symbol, derived_sym, is interoperable
3499 with C. This is necessary for any derived type that is BIND(C) and for
3500 derived types that are parameters to functions that are BIND(C). All
3501 fields of the derived type are required to be interoperable, and are tested
3502 for such. If an error occurs, the errors are reported here, allowing for
3503 multiple errors to be handled for a single derived type. */
3506 verify_bind_c_derived_type (gfc_symbol *derived_sym)
3508 gfc_component *curr_comp = NULL;
3509 gfc_try is_c_interop = FAILURE;
3510 gfc_try retval = SUCCESS;
3512 if (derived_sym == NULL)
3513 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
3514 "unexpectedly NULL");
3516 /* If we've already looked at this derived symbol, do not look at it again
3517 so we don't repeat warnings/errors. */
3518 if (derived_sym->ts.is_c_interop)
3521 /* The derived type must have the BIND attribute to be interoperable
3522 J3/04-007, Section 15.2.3. */
3523 if (derived_sym->attr.is_bind_c != 1)
3525 derived_sym->ts.is_c_interop = 0;
3526 gfc_error_now ("Derived type '%s' declared at %L must have the BIND "
3527 "attribute to be C interoperable", derived_sym->name,
3528 &(derived_sym->declared_at));
3532 curr_comp = derived_sym->components;
3534 /* TODO: is this really an error? */
3535 if (curr_comp == NULL)
3537 gfc_error ("Derived type '%s' at %L is empty",
3538 derived_sym->name, &(derived_sym->declared_at));
3542 /* Initialize the derived type as being C interoperable.
3543 If we find an error in the components, this will be set false. */
3544 derived_sym->ts.is_c_interop = 1;
3546 /* Loop through the list of components to verify that the kind of
3547 each is a C interoperable type. */
3550 /* The components cannot be pointers (fortran sense).
3551 J3/04-007, Section 15.2.3, C1505. */
3552 if (curr_comp->attr.pointer != 0)
3554 gfc_error ("Component '%s' at %L cannot have the "
3555 "POINTER attribute because it is a member "
3556 "of the BIND(C) derived type '%s' at %L",
3557 curr_comp->name, &(curr_comp->loc),
3558 derived_sym->name, &(derived_sym->declared_at));
3562 if (curr_comp->attr.proc_pointer != 0)
3564 gfc_error ("Procedure pointer component '%s' at %L cannot be a member"
3565 " of the BIND(C) derived type '%s' at %L", curr_comp->name,
3566 &curr_comp->loc, derived_sym->name,
3567 &derived_sym->declared_at);
3571 /* The components cannot be allocatable.
3572 J3/04-007, Section 15.2.3, C1505. */
3573 if (curr_comp->attr.allocatable != 0)
3575 gfc_error ("Component '%s' at %L cannot have the "
3576 "ALLOCATABLE attribute because it is a member "
3577 "of the BIND(C) derived type '%s' at %L",
3578 curr_comp->name, &(curr_comp->loc),
3579 derived_sym->name, &(derived_sym->declared_at));
3583 /* BIND(C) derived types must have interoperable components. */
3584 if (curr_comp->ts.type == BT_DERIVED
3585 && curr_comp->ts.u.derived->ts.is_iso_c != 1
3586 && curr_comp->ts.u.derived != derived_sym)
3588 /* This should be allowed; the draft says a derived-type can not
3589 have type parameters if it is has the BIND attribute. Type
3590 parameters seem to be for making parameterized derived types.
3591 There's no need to verify the type if it is c_ptr/c_funptr. */
3592 retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
3596 /* Grab the typespec for the given component and test the kind. */
3597 is_c_interop = verify_c_interop (&(curr_comp->ts));
3599 if (is_c_interop != SUCCESS)
3601 /* Report warning and continue since not fatal. The
3602 draft does specify a constraint that requires all fields
3603 to interoperate, but if the user says real(4), etc., it
3604 may interoperate with *something* in C, but the compiler
3605 most likely won't know exactly what. Further, it may not
3606 interoperate with the same data type(s) in C if the user
3607 recompiles with different flags (e.g., -m32 and -m64 on
3608 x86_64 and using integer(4) to claim interop with a
3610 if (derived_sym->attr.is_bind_c == 1)
3611 /* If the derived type is bind(c), all fields must be
3613 gfc_warning ("Component '%s' in derived type '%s' at %L "
3614 "may not be C interoperable, even though "
3615 "derived type '%s' is BIND(C)",
3616 curr_comp->name, derived_sym->name,
3617 &(curr_comp->loc), derived_sym->name);
3619 /* If derived type is param to bind(c) routine, or to one
3620 of the iso_c_binding procs, it must be interoperable, so
3621 all fields must interop too. */
3622 gfc_warning ("Component '%s' in derived type '%s' at %L "
3623 "may not be C interoperable",
3624 curr_comp->name, derived_sym->name,
3629 curr_comp = curr_comp->next;
3630 } while (curr_comp != NULL);
3633 /* Make sure we don't have conflicts with the attributes. */
3634 if (derived_sym->attr.access == ACCESS_PRIVATE)
3636 gfc_error ("Derived type '%s' at %L cannot be declared with both "
3637 "PRIVATE and BIND(C) attributes", derived_sym->name,
3638 &(derived_sym->declared_at));
3642 if (derived_sym->attr.sequence != 0)
3644 gfc_error ("Derived type '%s' at %L cannot have the SEQUENCE "
3645 "attribute because it is BIND(C)", derived_sym->name,
3646 &(derived_sym->declared_at));
3650 /* Mark the derived type as not being C interoperable if we found an
3651 error. If there were only warnings, proceed with the assumption
3652 it's interoperable. */
3653 if (retval == FAILURE)
3654 derived_sym->ts.is_c_interop = 0;
3660 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
3663 gen_special_c_interop_ptr (int ptr_id, const char *ptr_name,
3664 const char *module_name)
3666 gfc_symtree *tmp_symtree;
3667 gfc_symbol *tmp_sym;
3670 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, ptr_name);
3672 if (tmp_symtree != NULL)
3673 tmp_sym = tmp_symtree->n.sym;
3677 gfc_internal_error ("gen_special_c_interop_ptr(): Unable to "
3678 "create symbol for %s", ptr_name);
3681 /* Set up the symbol's important fields. Save attr required so we can
3682 initialize the ptr to NULL. */
3683 tmp_sym->attr.save = SAVE_EXPLICIT;
3684 tmp_sym->ts.is_c_interop = 1;
3685 tmp_sym->attr.is_c_interop = 1;
3686 tmp_sym->ts.is_iso_c = 1;
3687 tmp_sym->ts.type = BT_DERIVED;
3689 /* The c_ptr and c_funptr derived types will provide the
3690 definition for c_null_ptr and c_null_funptr, respectively. */
3691 if (ptr_id == ISOCBINDING_NULL_PTR)
3692 tmp_sym->ts.u.derived = get_iso_c_binding_dt (ISOCBINDING_PTR);
3694 tmp_sym->ts.u.derived = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3695 if (tmp_sym->ts.u.derived == NULL)
3697 /* This can occur if the user forgot to declare c_ptr or
3698 c_funptr and they're trying to use one of the procedures
3699 that has arg(s) of the missing type. In this case, a
3700 regular version of the thing should have been put in the
3702 generate_isocbinding_symbol (module_name, ptr_id == ISOCBINDING_NULL_PTR
3703 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR,
3704 (const char *) (ptr_id == ISOCBINDING_NULL_PTR
3705 ? "_gfortran_iso_c_binding_c_ptr"
3706 : "_gfortran_iso_c_binding_c_funptr"));
3708 tmp_sym->ts.u.derived =
3709 get_iso_c_binding_dt (ptr_id == ISOCBINDING_NULL_PTR
3710 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR);
3713 /* Module name is some mangled version of iso_c_binding. */
3714 tmp_sym->module = gfc_get_string (module_name);
3716 /* Say it's from the iso_c_binding module. */
3717 tmp_sym->attr.is_iso_c = 1;
3719 tmp_sym->attr.use_assoc = 1;
3720 tmp_sym->attr.is_bind_c = 1;
3721 /* Set the binding_label. */
3722 sprintf (tmp_sym->binding_label, "%s_%s", module_name, tmp_sym->name);
3724 /* Set the c_address field of c_null_ptr and c_null_funptr to
3725 the value of NULL. */
3726 tmp_sym->value = gfc_get_expr ();
3727 tmp_sym->value->expr_type = EXPR_STRUCTURE;
3728 tmp_sym->value->ts.type = BT_DERIVED;
3729 tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
3730 gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
3731 c = gfc_constructor_first (tmp_sym->value->value.constructor);
3732 c->expr = gfc_get_expr ();
3733 c->expr->expr_type = EXPR_NULL;
3734 c->expr->ts.is_iso_c = 1;
3735 /* Must declare c_null_ptr and c_null_funptr as having the
3736 PARAMETER attribute so they can be used in init expressions. */
3737 tmp_sym->attr.flavor = FL_PARAMETER;
3743 /* Add a formal argument, gfc_formal_arglist, to the
3744 end of the given list of arguments. Set the reference to the
3745 provided symbol, param_sym, in the argument. */
3748 add_formal_arg (gfc_formal_arglist **head,
3749 gfc_formal_arglist **tail,
3750 gfc_formal_arglist *formal_arg,
3751 gfc_symbol *param_sym)
3753 /* Put in list, either as first arg or at the tail (curr arg). */
3755 *head = *tail = formal_arg;
3758 (*tail)->next = formal_arg;
3759 (*tail) = formal_arg;
3762 (*tail)->sym = param_sym;
3763 (*tail)->next = NULL;
3769 /* Generates a symbol representing the CPTR argument to an
3770 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3771 CPTR and add it to the provided argument list. */
3774 gen_cptr_param (gfc_formal_arglist **head,
3775 gfc_formal_arglist **tail,
3776 const char *module_name,
3777 gfc_namespace *ns, const char *c_ptr_name,
3780 gfc_symbol *param_sym = NULL;
3781 gfc_symbol *c_ptr_sym = NULL;
3782 gfc_symtree *param_symtree = NULL;
3783 gfc_formal_arglist *formal_arg = NULL;
3784 const char *c_ptr_in;
3785 const char *c_ptr_type = NULL;
3787 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3788 c_ptr_type = "_gfortran_iso_c_binding_c_funptr";
3790 c_ptr_type = "_gfortran_iso_c_binding_c_ptr";
3792 if(c_ptr_name == NULL)
3793 c_ptr_in = "gfc_cptr__";
3795 c_ptr_in = c_ptr_name;
3796 gfc_get_sym_tree (c_ptr_in, ns, ¶m_symtree, false);
3797 if (param_symtree != NULL)
3798 param_sym = param_symtree->n.sym;
3800 gfc_internal_error ("gen_cptr_param(): Unable to "
3801 "create symbol for %s", c_ptr_in);
3803 /* Set up the appropriate fields for the new c_ptr param sym. */
3805 param_sym->attr.flavor = FL_DERIVED;
3806 param_sym->ts.type = BT_DERIVED;
3807 param_sym->attr.intent = INTENT_IN;
3808 param_sym->attr.dummy = 1;
3810 /* This will pass the ptr to the iso_c routines as a (void *). */
3811 param_sym->attr.value = 1;
3812 param_sym->attr.use_assoc = 1;
3814 /* Get the symbol for c_ptr or c_funptr, no matter what it's name is
3816 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3817 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3819 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_PTR);
3820 if (c_ptr_sym == NULL)
3822 /* This can happen if the user did not define c_ptr but they are
3823 trying to use one of the iso_c_binding functions that need it. */
3824 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3825 generate_isocbinding_symbol (module_name, ISOCBINDING_FUNPTR,
3826 (const char *)c_ptr_type);
3828 generate_isocbinding_symbol (module_name, ISOCBINDING_PTR,
3829 (const char *)c_ptr_type);
3831 gfc_get_ha_symbol (c_ptr_type, &(c_ptr_sym));
3834 param_sym->ts.u.derived = c_ptr_sym;
3835 param_sym->module = gfc_get_string (module_name);
3837 /* Make new formal arg. */
3838 formal_arg = gfc_get_formal_arglist ();
3839 /* Add arg to list of formal args (the CPTR arg). */
3840 add_formal_arg (head, tail, formal_arg, param_sym);
3844 /* Generates a symbol representing the FPTR argument to an
3845 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3846 FPTR and add it to the provided argument list. */
3849 gen_fptr_param (gfc_formal_arglist **head,
3850 gfc_formal_arglist **tail,
3851 const char *module_name,
3852 gfc_namespace *ns, const char *f_ptr_name, int proc)
3854 gfc_symbol *param_sym = NULL;
3855 gfc_symtree *param_symtree = NULL;
3856 gfc_formal_arglist *formal_arg = NULL;
3857 const char *f_ptr_out = "gfc_fptr__";
3859 if (f_ptr_name != NULL)
3860 f_ptr_out = f_ptr_name;
3862 gfc_get_sym_tree (f_ptr_out, ns, ¶m_symtree, false);
3863 if (param_symtree != NULL)
3864 param_sym = param_symtree->n.sym;
3866 gfc_internal_error ("generateFPtrParam(): Unable to "
3867 "create symbol for %s", f_ptr_out);
3869 /* Set up the necessary fields for the fptr output param sym. */
3872 param_sym->attr.proc_pointer = 1;
3874 param_sym->attr.pointer = 1;
3875 param_sym->attr.dummy = 1;
3876 param_sym->attr.use_assoc = 1;
3878 /* ISO C Binding type to allow any pointer type as actual param. */
3879 param_sym->ts.type = BT_VOID;
3880 param_sym->module = gfc_get_string (module_name);
3883 formal_arg = gfc_get_formal_arglist ();
3884 /* Add arg to list of formal args. */
3885 add_formal_arg (head, tail, formal_arg, param_sym);
3889 /* Generates a symbol representing the optional SHAPE argument for the
3890 iso_c_binding c_f_pointer() procedure. Also, create a
3891 gfc_formal_arglist for the SHAPE and add it to the provided
3895 gen_shape_param (gfc_formal_arglist **head,
3896 gfc_formal_arglist **tail,
3897 const char *module_name,
3898 gfc_namespace *ns, const char *shape_param_name)
3900 gfc_symbol *param_sym = NULL;
3901 gfc_symtree *param_symtree = NULL;
3902 gfc_formal_arglist *formal_arg = NULL;
3903 const char *shape_param = "gfc_shape_array__";
3906 if (shape_param_name != NULL)
3907 shape_param = shape_param_name;
3909 gfc_get_sym_tree (shape_param, ns, ¶m_symtree, false);
3910 if (param_symtree != NULL)
3911 param_sym = param_symtree->n.sym;
3913 gfc_internal_error ("generateShapeParam(): Unable to "
3914 "create symbol for %s", shape_param);
3916 /* Set up the necessary fields for the shape input param sym. */
3918 param_sym->attr.dummy = 1;
3919 param_sym->attr.use_assoc = 1;
3921 /* Integer array, rank 1, describing the shape of the object. Make it's
3922 type BT_VOID initially so we can accept any type/kind combination of
3923 integer. During gfc_iso_c_sub_interface (resolve.c), we'll make it
3924 of BT_INTEGER type. */
3925 param_sym->ts.type = BT_VOID;
3927 /* Initialize the kind to default integer. However, it will be overridden
3928 during resolution to match the kind of the SHAPE parameter given as
3929 the actual argument (to allow for any valid integer kind). */
3930 param_sym->ts.kind = gfc_default_integer_kind;
3931 param_sym->as = gfc_get_array_spec ();
3933 /* Clear out the dimension info for the array. */
3934 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3936 param_sym->as->lower[i] = NULL;
3937 param_sym->as->upper[i] = NULL;
3939 param_sym->as->rank = 1;
3940 param_sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
3943 /* The extent is unknown until we get it. The length give us
3944 the rank the incoming pointer. */
3945 param_sym->as->type = AS_ASSUMED_SHAPE;
3947 /* The arg is also optional; it is required iff the second arg
3948 (fptr) is to an array, otherwise, it's ignored. */
3949 param_sym->attr.optional = 1;
3950 param_sym->attr.intent = INTENT_IN;
3951 param_sym->attr.dimension = 1;
3952 param_sym->module = gfc_get_string (module_name);
3955 formal_arg = gfc_get_formal_arglist ();
3956 /* Add arg to list of formal args. */
3957 add_formal_arg (head, tail, formal_arg, param_sym);
3961 /* Add a procedure interface to the given symbol (i.e., store a
3962 reference to the list of formal arguments). */
3965 add_proc_interface (gfc_symbol *sym, ifsrc source,
3966 gfc_formal_arglist *formal)
3969 sym->formal = formal;
3970 sym->attr.if_source = source;
3974 /* Copy the formal args from an existing symbol, src, into a new
3975 symbol, dest. New formal args are created, and the description of
3976 each arg is set according to the existing ones. This function is
3977 used when creating procedure declaration variables from a procedure
3978 declaration statement (see match_proc_decl()) to create the formal
3979 args based on the args of a given named interface. */
3982 gfc_copy_formal_args (gfc_symbol *dest, gfc_symbol *src)
3984 gfc_formal_arglist *head = NULL;
3985 gfc_formal_arglist *tail = NULL;
3986 gfc_formal_arglist *formal_arg = NULL;
3987 gfc_formal_arglist *curr_arg = NULL;
3988 gfc_formal_arglist *formal_prev = NULL;
3989 /* Save current namespace so we can change it for formal args. */
3990 gfc_namespace *parent_ns = gfc_current_ns;
3992 /* Create a new namespace, which will be the formal ns (namespace
3993 of the formal args). */
3994 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
3995 gfc_current_ns->proc_name = dest;
3997 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
3999 formal_arg = gfc_get_formal_arglist ();
4000 gfc_get_symbol (curr_arg->sym->name, gfc_current_ns, &(formal_arg->sym));
4002 /* May need to copy more info for the symbol. */
4003 formal_arg->sym->attr = curr_arg->sym->attr;
4004 formal_arg->sym->ts = curr_arg->sym->ts;
4005 formal_arg->sym->as = gfc_copy_array_spec (curr_arg->sym->as);
4006 gfc_copy_formal_args (formal_arg->sym, curr_arg->sym);
4008 /* If this isn't the first arg, set up the next ptr. For the
4009 last arg built, the formal_arg->next will never get set to
4010 anything other than NULL. */
4011 if (formal_prev != NULL)
4012 formal_prev->next = formal_arg;
4014 formal_arg->next = NULL;
4016 formal_prev = formal_arg;
4018 /* Add arg to list of formal args. */
4019 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4022 /* Add the interface to the symbol. */
4023 add_proc_interface (dest, IFSRC_DECL, head);
4025 /* Store the formal namespace information. */
4026 if (dest->formal != NULL)
4027 /* The current ns should be that for the dest proc. */
4028 dest->formal_ns = gfc_current_ns;
4029 /* Restore the current namespace to what it was on entry. */
4030 gfc_current_ns = parent_ns;
4035 gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src)
4037 gfc_formal_arglist *head = NULL;
4038 gfc_formal_arglist *tail = NULL;
4039 gfc_formal_arglist *formal_arg = NULL;
4040 gfc_intrinsic_arg *curr_arg = NULL;
4041 gfc_formal_arglist *formal_prev = NULL;
4042 /* Save current namespace so we can change it for formal args. */
4043 gfc_namespace *parent_ns = gfc_current_ns;
4045 /* Create a new namespace, which will be the formal ns (namespace
4046 of the formal args). */
4047 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
4048 gfc_current_ns->proc_name = dest;
4050 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4052 formal_arg = gfc_get_formal_arglist ();
4053 gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
4055 /* May need to copy more info for the symbol. */
4056 formal_arg->sym->ts = curr_arg->ts;
4057 formal_arg->sym->attr.optional = curr_arg->optional;
4058 formal_arg->sym->attr.intent = curr_arg->intent;
4059 formal_arg->sym->attr.flavor = FL_VARIABLE;
4060 formal_arg->sym->attr.dummy = 1;
4062 if (formal_arg->sym->ts.type == BT_CHARACTER)
4063 formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4065 /* If this isn't the first arg, set up the next ptr. For the
4066 last arg built, the formal_arg->next will never get set to
4067 anything other than NULL. */
4068 if (formal_prev != NULL)
4069 formal_prev->next = formal_arg;
4071 formal_arg->next = NULL;
4073 formal_prev = formal_arg;
4075 /* Add arg to list of formal args. */
4076 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4079 /* Add the interface to the symbol. */
4080 add_proc_interface (dest, IFSRC_DECL, head);
4082 /* Store the formal namespace information. */
4083 if (dest->formal != NULL)
4084 /* The current ns should be that for the dest proc. */
4085 dest->formal_ns = gfc_current_ns;
4086 /* Restore the current namespace to what it was on entry. */
4087 gfc_current_ns = parent_ns;
4092 gfc_copy_formal_args_ppc (gfc_component *dest, gfc_symbol *src)
4094 gfc_formal_arglist *head = NULL;
4095 gfc_formal_arglist *tail = NULL;
4096 gfc_formal_arglist *formal_arg = NULL;
4097 gfc_formal_arglist *curr_arg = NULL;
4098 gfc_formal_arglist *formal_prev = NULL;
4099 /* Save current namespace so we can change it for formal args. */
4100 gfc_namespace *parent_ns = gfc_current_ns;
4102 /* Create a new namespace, which will be the formal ns (namespace
4103 of the formal args). */
4104 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
4105 /* TODO: gfc_current_ns->proc_name = dest;*/
4107 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4109 formal_arg = gfc_get_formal_arglist ();
4110 gfc_get_symbol (curr_arg->sym->name, gfc_current_ns, &(formal_arg->sym));
4112 /* May need to copy more info for the symbol. */
4113 formal_arg->sym->attr = curr_arg->sym->attr;
4114 formal_arg->sym->ts = curr_arg->sym->ts;
4115 formal_arg->sym->as = gfc_copy_array_spec (curr_arg->sym->as);
4116 gfc_copy_formal_args (formal_arg->sym, curr_arg->sym);
4118 /* If this isn't the first arg, set up the next ptr. For the
4119 last arg built, the formal_arg->next will never get set to
4120 anything other than NULL. */
4121 if (formal_prev != NULL)
4122 formal_prev->next = formal_arg;
4124 formal_arg->next = NULL;
4126 formal_prev = formal_arg;
4128 /* Add arg to list of formal args. */
4129 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4132 /* Add the interface to the symbol. */
4133 dest->formal = head;
4134 dest->attr.if_source = IFSRC_DECL;
4136 /* Store the formal namespace information. */
4137 if (dest->formal != NULL)
4138 /* The current ns should be that for the dest proc. */
4139 dest->formal_ns = gfc_current_ns;
4140 /* Restore the current namespace to what it was on entry. */
4141 gfc_current_ns = parent_ns;
4145 /* Builds the parameter list for the iso_c_binding procedure
4146 c_f_pointer or c_f_procpointer. The old_sym typically refers to a
4147 generic version of either the c_f_pointer or c_f_procpointer
4148 functions. The new_proc_sym represents a "resolved" version of the
4149 symbol. The functions are resolved to match the types of their
4150 parameters; for example, c_f_pointer(cptr, fptr) would resolve to
4151 something similar to c_f_pointer_i4 if the type of data object fptr
4152 pointed to was a default integer. The actual name of the resolved
4153 procedure symbol is further mangled with the module name, etc., but
4154 the idea holds true. */
4157 build_formal_args (gfc_symbol *new_proc_sym,
4158 gfc_symbol *old_sym, int add_optional_arg)
4160 gfc_formal_arglist *head = NULL, *tail = NULL;
4161 gfc_namespace *parent_ns = NULL;
4163 parent_ns = gfc_current_ns;
4164 /* Create a new namespace, which will be the formal ns (namespace
4165 of the formal args). */
4166 gfc_current_ns = gfc_get_namespace(parent_ns, 0);
4167 gfc_current_ns->proc_name = new_proc_sym;
4169 /* Generate the params. */
4170 if (old_sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
4172 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
4173 gfc_current_ns, "cptr", old_sym->intmod_sym_id);
4174 gen_fptr_param (&head, &tail, (const char *) new_proc_sym->module,
4175 gfc_current_ns, "fptr", 1);
4177 else if (old_sym->intmod_sym_id == ISOCBINDING_F_POINTER)
4179 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
4180 gfc_current_ns, "cptr", old_sym->intmod_sym_id);
4181 gen_fptr_param (&head, &tail, (const char *) new_proc_sym->module,
4182 gfc_current_ns, "fptr", 0);
4183 /* If we're dealing with c_f_pointer, it has an optional third arg. */
4184 gen_shape_param (&head, &tail,(const char *) new_proc_sym->module,
4185 gfc_current_ns, "shape");
4188 else if (old_sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
4190 /* c_associated has one required arg and one optional; both
4192 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
4193 gfc_current_ns, "c_ptr_1", ISOCBINDING_ASSOCIATED);
4194 if (add_optional_arg)
4196 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
4197 gfc_current_ns, "c_ptr_2", ISOCBINDING_ASSOCIATED);
4198 /* The last param is optional so mark it as such. */
4199 tail->sym->attr.optional = 1;
4203 /* Add the interface (store formal args to new_proc_sym). */
4204 add_proc_interface (new_proc_sym, IFSRC_DECL, head);
4206 /* Set up the formal_ns pointer to the one created for the
4207 new procedure so it'll get cleaned up during gfc_free_symbol(). */
4208 new_proc_sym->formal_ns = gfc_current_ns;
4210 gfc_current_ns = parent_ns;
4214 std_for_isocbinding_symbol (int id)
4218 #define NAMED_INTCST(a,b,c,d) \
4221 #include "iso-c-binding.def"
4224 return GFC_STD_F2003;
4228 /* Generate the given set of C interoperable kind objects, or all
4229 interoperable kinds. This function will only be given kind objects
4230 for valid iso_c_binding defined types because this is verified when
4231 the 'use' statement is parsed. If the user gives an 'only' clause,
4232 the specific kinds are looked up; if they don't exist, an error is
4233 reported. If the user does not give an 'only' clause, all
4234 iso_c_binding symbols are generated. If a list of specific kinds
4235 is given, it must have a NULL in the first empty spot to mark the
4240 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
4241 const char *local_name)
4243 const char *const name = (local_name && local_name[0]) ? local_name
4244 : c_interop_kinds_table[s].name;
4245 gfc_symtree *tmp_symtree = NULL;
4246 gfc_symbol *tmp_sym = NULL;
4247 gfc_dt_list **dt_list_ptr = NULL;
4248 gfc_component *tmp_comp = NULL;
4249 char comp_name[(GFC_MAX_SYMBOL_LEN * 2) + 1];
4252 if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
4254 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
4256 /* Already exists in this scope so don't re-add it.
4257 TODO: we should probably check that it's really the same symbol. */
4258 if (tmp_symtree != NULL)
4261 /* Create the sym tree in the current ns. */
4262 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
4264 tmp_sym = tmp_symtree->n.sym;
4266 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
4269 /* Say what module this symbol belongs to. */
4270 tmp_sym->module = gfc_get_string (mod_name);
4271 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
4272 tmp_sym->intmod_sym_id = s;
4277 #define NAMED_INTCST(a,b,c,d) case a :
4278 #define NAMED_REALCST(a,b,c) case a :
4279 #define NAMED_CMPXCST(a,b,c) case a :
4280 #define NAMED_LOGCST(a,b,c) case a :
4281 #define NAMED_CHARKNDCST(a,b,c) case a :
4282 #include "iso-c-binding.def"
4284 tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4285 c_interop_kinds_table[s].value);
4287 /* Initialize an integer constant expression node. */
4288 tmp_sym->attr.flavor = FL_PARAMETER;
4289 tmp_sym->ts.type = BT_INTEGER;
4290 tmp_sym->ts.kind = gfc_default_integer_kind;
4292 /* Mark this type as a C interoperable one. */
4293 tmp_sym->ts.is_c_interop = 1;
4294 tmp_sym->ts.is_iso_c = 1;
4295 tmp_sym->value->ts.is_c_interop = 1;
4296 tmp_sym->value->ts.is_iso_c = 1;
4297 tmp_sym->attr.is_c_interop = 1;
4299 /* Tell what f90 type this c interop kind is valid. */
4300 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
4302 /* Say it's from the iso_c_binding module. */
4303 tmp_sym->attr.is_iso_c = 1;
4305 /* Make it use associated. */
4306 tmp_sym->attr.use_assoc = 1;
4310 #define NAMED_CHARCST(a,b,c) case a :
4311 #include "iso-c-binding.def"
4313 /* Initialize an integer constant expression node for the
4314 length of the character. */
4315 tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
4316 &gfc_current_locus, NULL, 1);
4317 tmp_sym->value->ts.is_c_interop = 1;
4318 tmp_sym->value->ts.is_iso_c = 1;
4319 tmp_sym->value->value.character.length = 1;
4320 tmp_sym->value->value.character.string[0]
4321 = (gfc_char_t) c_interop_kinds_table[s].value;
4322 tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4323 tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
4326 /* May not need this in both attr and ts, but do need in
4327 attr for writing module file. */
4328 tmp_sym->attr.is_c_interop = 1;
4330 tmp_sym->attr.flavor = FL_PARAMETER;
4331 tmp_sym->ts.type = BT_CHARACTER;
4333 /* Need to set it to the C_CHAR kind. */
4334 tmp_sym->ts.kind = gfc_default_character_kind;
4336 /* Mark this type as a C interoperable one. */
4337 tmp_sym->ts.is_c_interop = 1;
4338 tmp_sym->ts.is_iso_c = 1;
4340 /* Tell what f90 type this c interop kind is valid. */
4341 tmp_sym->ts.f90_type = BT_CHARACTER;
4343 /* Say it's from the iso_c_binding module. */
4344 tmp_sym->attr.is_iso_c = 1;
4346 /* Make it use associated. */
4347 tmp_sym->attr.use_assoc = 1;
4350 case ISOCBINDING_PTR:
4351 case ISOCBINDING_FUNPTR:
4353 /* Initialize an integer constant expression node. */
4354 tmp_sym->attr.flavor = FL_DERIVED;
4355 tmp_sym->ts.is_c_interop = 1;
4356 tmp_sym->attr.is_c_interop = 1;
4357 tmp_sym->attr.is_iso_c = 1;
4358 tmp_sym->ts.is_iso_c = 1;
4359 tmp_sym->ts.type = BT_DERIVED;
4361 /* A derived type must have the bind attribute to be
4362 interoperable (J3/04-007, Section 15.2.3), even though
4363 the binding label is not used. */
4364 tmp_sym->attr.is_bind_c = 1;
4366 tmp_sym->attr.referenced = 1;
4368 tmp_sym->ts.u.derived = tmp_sym;
4370 /* Add the symbol created for the derived type to the current ns. */
4371 dt_list_ptr = &(gfc_derived_types);
4372 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
4373 dt_list_ptr = &((*dt_list_ptr)->next);
4375 /* There is already at least one derived type in the list, so append
4376 the one we're currently building for c_ptr or c_funptr. */
4377 if (*dt_list_ptr != NULL)
4378 dt_list_ptr = &((*dt_list_ptr)->next);
4379 (*dt_list_ptr) = gfc_get_dt_list ();
4380 (*dt_list_ptr)->derived = tmp_sym;
4381 (*dt_list_ptr)->next = NULL;
4383 /* Set up the component of the derived type, which will be
4384 an integer with kind equal to c_ptr_size. Mangle the name of
4385 the field for the c_address to prevent the curious user from
4386 trying to access it from Fortran. */
4387 sprintf (comp_name, "__%s_%s", tmp_sym->name, "c_address");
4388 gfc_add_component (tmp_sym, comp_name, &tmp_comp);
4389 if (tmp_comp == NULL)
4390 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
4391 "create component for c_address");
4393 tmp_comp->ts.type = BT_INTEGER;
4395 /* Set this because the module will need to read/write this field. */
4396 tmp_comp->ts.f90_type = BT_INTEGER;
4398 /* The kinds for c_ptr and c_funptr are the same. */
4399 index = get_c_kind ("c_ptr", c_interop_kinds_table);
4400 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
4402 tmp_comp->attr.pointer = 0;