[PATCH 9/13] x86 musl support
[official-gcc.git] / gcc / fortran / symbol.c
blobb18608b9ab226ef4d886d208ccddb0e90c5c416e
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "parse.h"
28 #include "match.h"
29 #include "constructor.h"
32 /* Strings for all symbol attributes. We use these for dumping the
33 parse tree, in error messages, and also when reading and writing
34 modules. */
36 const mstring flavors[] =
38 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
39 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
40 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
41 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
42 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
43 minit (NULL, -1)
46 const mstring procedures[] =
48 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
49 minit ("MODULE-PROC", PROC_MODULE),
50 minit ("INTERNAL-PROC", PROC_INTERNAL),
51 minit ("DUMMY-PROC", PROC_DUMMY),
52 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
53 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
54 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
55 minit (NULL, -1)
58 const mstring intents[] =
60 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
61 minit ("IN", INTENT_IN),
62 minit ("OUT", INTENT_OUT),
63 minit ("INOUT", INTENT_INOUT),
64 minit (NULL, -1)
67 const mstring access_types[] =
69 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
70 minit ("PUBLIC", ACCESS_PUBLIC),
71 minit ("PRIVATE", ACCESS_PRIVATE),
72 minit (NULL, -1)
75 const mstring ifsrc_types[] =
77 minit ("UNKNOWN", IFSRC_UNKNOWN),
78 minit ("DECL", IFSRC_DECL),
79 minit ("BODY", IFSRC_IFBODY)
82 const mstring save_status[] =
84 minit ("UNKNOWN", SAVE_NONE),
85 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
86 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
89 /* This is to make sure the backend generates setup code in the correct
90 order. */
92 static int next_dummy_order = 1;
95 gfc_namespace *gfc_current_ns;
96 gfc_namespace *gfc_global_ns_list;
98 gfc_gsymbol *gfc_gsym_root = NULL;
100 gfc_dt_list *gfc_derived_types;
102 static gfc_undo_change_set default_undo_chgset_var = { vNULL, vNULL, NULL };
103 static gfc_undo_change_set *latest_undo_chgset = &default_undo_chgset_var;
106 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
108 /* The following static variable indicates whether a particular element has
109 been explicitly set or not. */
111 static int new_flag[GFC_LETTERS];
114 /* Handle a correctly parsed IMPLICIT NONE. */
116 void
117 gfc_set_implicit_none (bool type, bool external, locus *loc)
119 int i;
121 if (external)
122 gfc_current_ns->has_implicit_none_export = 1;
124 if (type)
126 gfc_current_ns->seen_implicit_none = 1;
127 for (i = 0; i < GFC_LETTERS; i++)
129 if (gfc_current_ns->set_flag[i])
131 gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
132 "IMPLICIT statement", loc);
133 return;
135 gfc_clear_ts (&gfc_current_ns->default_type[i]);
136 gfc_current_ns->set_flag[i] = 1;
142 /* Reset the implicit range flags. */
144 void
145 gfc_clear_new_implicit (void)
147 int i;
149 for (i = 0; i < GFC_LETTERS; i++)
150 new_flag[i] = 0;
154 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
156 bool
157 gfc_add_new_implicit_range (int c1, int c2)
159 int i;
161 c1 -= 'a';
162 c2 -= 'a';
164 for (i = c1; i <= c2; i++)
166 if (new_flag[i])
168 gfc_error ("Letter %<%c%> already set in IMPLICIT statement at %C",
169 i + 'A');
170 return false;
173 new_flag[i] = 1;
176 return true;
180 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
181 the new implicit types back into the existing types will work. */
183 bool
184 gfc_merge_new_implicit (gfc_typespec *ts)
186 int i;
188 if (gfc_current_ns->seen_implicit_none)
190 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
191 return false;
194 for (i = 0; i < GFC_LETTERS; i++)
196 if (new_flag[i])
198 if (gfc_current_ns->set_flag[i])
200 gfc_error ("Letter %c already has an IMPLICIT type at %C",
201 i + 'A');
202 return false;
205 gfc_current_ns->default_type[i] = *ts;
206 gfc_current_ns->implicit_loc[i] = gfc_current_locus;
207 gfc_current_ns->set_flag[i] = 1;
210 return true;
214 /* Given a symbol, return a pointer to the typespec for its default type. */
216 gfc_typespec *
217 gfc_get_default_type (const char *name, gfc_namespace *ns)
219 char letter;
221 letter = name[0];
223 if (flag_allow_leading_underscore && letter == '_')
224 gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
225 "gfortran developers, and should not be used for "
226 "implicitly typed variables");
228 if (letter < 'a' || letter > 'z')
229 gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
231 if (ns == NULL)
232 ns = gfc_current_ns;
234 return &ns->default_type[letter - 'a'];
238 /* Given a pointer to a symbol, set its type according to the first
239 letter of its name. Fails if the letter in question has no default
240 type. */
242 bool
243 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
245 gfc_typespec *ts;
247 if (sym->ts.type != BT_UNKNOWN)
248 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
250 ts = gfc_get_default_type (sym->name, ns);
252 if (ts->type == BT_UNKNOWN)
254 if (error_flag && !sym->attr.untyped)
256 gfc_error ("Symbol %qs at %L has no IMPLICIT type",
257 sym->name, &sym->declared_at);
258 sym->attr.untyped = 1; /* Ensure we only give an error once. */
261 return false;
264 sym->ts = *ts;
265 sym->attr.implicit_type = 1;
267 if (ts->type == BT_CHARACTER && ts->u.cl)
268 sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
269 else if (ts->type == BT_CLASS
270 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
271 return false;
273 if (sym->attr.is_bind_c == 1 && warn_c_binding_type)
275 /* BIND(C) variables should not be implicitly declared. */
276 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared BIND(C) "
277 "variable %qs at %L may not be C interoperable",
278 sym->name, &sym->declared_at);
279 sym->ts.f90_type = sym->ts.type;
282 if (sym->attr.dummy != 0)
284 if (sym->ns->proc_name != NULL
285 && (sym->ns->proc_name->attr.subroutine != 0
286 || sym->ns->proc_name->attr.function != 0)
287 && sym->ns->proc_name->attr.is_bind_c != 0
288 && warn_c_binding_type)
290 /* Dummy args to a BIND(C) routine may not be interoperable if
291 they are implicitly typed. */
292 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared variable "
293 "%qs at %L may not be C interoperable but it is a "
294 "dummy argument to the BIND(C) procedure %qs at %L",
295 sym->name, &(sym->declared_at),
296 sym->ns->proc_name->name,
297 &(sym->ns->proc_name->declared_at));
298 sym->ts.f90_type = sym->ts.type;
302 return true;
306 /* This function is called from parse.c(parse_progunit) to check the
307 type of the function is not implicitly typed in the host namespace
308 and to implicitly type the function result, if necessary. */
310 void
311 gfc_check_function_type (gfc_namespace *ns)
313 gfc_symbol *proc = ns->proc_name;
315 if (!proc->attr.contained || proc->result->attr.implicit_type)
316 return;
318 if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
320 if (gfc_set_default_type (proc->result, 0, gfc_current_ns))
322 if (proc->result != proc)
324 proc->ts = proc->result->ts;
325 proc->as = gfc_copy_array_spec (proc->result->as);
326 proc->attr.dimension = proc->result->attr.dimension;
327 proc->attr.pointer = proc->result->attr.pointer;
328 proc->attr.allocatable = proc->result->attr.allocatable;
331 else if (!proc->result->attr.proc_pointer)
333 gfc_error ("Function result %qs at %L has no IMPLICIT type",
334 proc->result->name, &proc->result->declared_at);
335 proc->result->attr.untyped = 1;
341 /******************** Symbol attribute stuff *********************/
343 /* This is a generic conflict-checker. We do this to avoid having a
344 single conflict in two places. */
346 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
347 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
348 #define conf_std(a, b, std) if (attr->a && attr->b)\
350 a1 = a;\
351 a2 = b;\
352 standard = std;\
353 goto conflict_std;\
356 static bool
357 check_conflict (symbol_attribute *attr, const char *name, locus *where)
359 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
360 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
361 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
362 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
363 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
364 *privat = "PRIVATE", *recursive = "RECURSIVE",
365 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
366 *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
367 *function = "FUNCTION", *subroutine = "SUBROUTINE",
368 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
369 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
370 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
371 *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
372 *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
373 *proc_pointer = "PROCEDURE POINTER", *abstract = "ABSTRACT",
374 *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION",
375 *contiguous = "CONTIGUOUS", *generic = "GENERIC";
376 static const char *threadprivate = "THREADPRIVATE";
377 static const char *omp_declare_target = "OMP DECLARE TARGET";
379 const char *a1, *a2;
380 int standard;
382 if (where == NULL)
383 where = &gfc_current_locus;
385 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
387 a1 = pointer;
388 a2 = intent;
389 standard = GFC_STD_F2003;
390 goto conflict_std;
393 if (attr->in_namelist && (attr->allocatable || attr->pointer))
395 a1 = in_namelist;
396 a2 = attr->allocatable ? allocatable : pointer;
397 standard = GFC_STD_F2003;
398 goto conflict_std;
401 /* Check for attributes not allowed in a BLOCK DATA. */
402 if (gfc_current_state () == COMP_BLOCK_DATA)
404 a1 = NULL;
406 if (attr->in_namelist)
407 a1 = in_namelist;
408 if (attr->allocatable)
409 a1 = allocatable;
410 if (attr->external)
411 a1 = external;
412 if (attr->optional)
413 a1 = optional;
414 if (attr->access == ACCESS_PRIVATE)
415 a1 = privat;
416 if (attr->access == ACCESS_PUBLIC)
417 a1 = publik;
418 if (attr->intent != INTENT_UNKNOWN)
419 a1 = intent;
421 if (a1 != NULL)
423 gfc_error
424 ("%s attribute not allowed in BLOCK DATA program unit at %L",
425 a1, where);
426 return false;
430 if (attr->save == SAVE_EXPLICIT)
432 conf (dummy, save);
433 conf (in_common, save);
434 conf (result, save);
436 switch (attr->flavor)
438 case FL_PROGRAM:
439 case FL_BLOCK_DATA:
440 case FL_MODULE:
441 case FL_LABEL:
442 case FL_DERIVED:
443 case FL_PARAMETER:
444 a1 = gfc_code2string (flavors, attr->flavor);
445 a2 = save;
446 goto conflict;
447 case FL_NAMELIST:
448 gfc_error ("Namelist group name at %L cannot have the "
449 "SAVE attribute", where);
450 return false;
451 break;
452 case FL_PROCEDURE:
453 /* Conflicts between SAVE and PROCEDURE will be checked at
454 resolution stage, see "resolve_fl_procedure". */
455 case FL_VARIABLE:
456 default:
457 break;
461 conf (dummy, entry);
462 conf (dummy, intrinsic);
463 conf (dummy, threadprivate);
464 conf (dummy, omp_declare_target);
465 conf (pointer, target);
466 conf (pointer, intrinsic);
467 conf (pointer, elemental);
468 conf (pointer, codimension);
469 conf (allocatable, elemental);
471 conf (target, external);
472 conf (target, intrinsic);
474 if (!attr->if_source)
475 conf (external, dimension); /* See Fortran 95's R504. */
477 conf (external, intrinsic);
478 conf (entry, intrinsic);
480 if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
481 conf (external, subroutine);
483 if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
484 "Procedure pointer at %C"))
485 return false;
487 conf (allocatable, pointer);
488 conf_std (allocatable, dummy, GFC_STD_F2003);
489 conf_std (allocatable, function, GFC_STD_F2003);
490 conf_std (allocatable, result, GFC_STD_F2003);
491 conf (elemental, recursive);
493 conf (in_common, dummy);
494 conf (in_common, allocatable);
495 conf (in_common, codimension);
496 conf (in_common, result);
498 conf (in_equivalence, use_assoc);
499 conf (in_equivalence, codimension);
500 conf (in_equivalence, dummy);
501 conf (in_equivalence, target);
502 conf (in_equivalence, pointer);
503 conf (in_equivalence, function);
504 conf (in_equivalence, result);
505 conf (in_equivalence, entry);
506 conf (in_equivalence, allocatable);
507 conf (in_equivalence, threadprivate);
508 conf (in_equivalence, omp_declare_target);
510 conf (dummy, result);
511 conf (entry, result);
512 conf (generic, result);
514 conf (function, subroutine);
516 if (!function && !subroutine)
517 conf (is_bind_c, dummy);
519 conf (is_bind_c, cray_pointer);
520 conf (is_bind_c, cray_pointee);
521 conf (is_bind_c, codimension);
522 conf (is_bind_c, allocatable);
523 conf (is_bind_c, elemental);
525 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
526 Parameter conflict caught below. Also, value cannot be specified
527 for a dummy procedure. */
529 /* Cray pointer/pointee conflicts. */
530 conf (cray_pointer, cray_pointee);
531 conf (cray_pointer, dimension);
532 conf (cray_pointer, codimension);
533 conf (cray_pointer, contiguous);
534 conf (cray_pointer, pointer);
535 conf (cray_pointer, target);
536 conf (cray_pointer, allocatable);
537 conf (cray_pointer, external);
538 conf (cray_pointer, intrinsic);
539 conf (cray_pointer, in_namelist);
540 conf (cray_pointer, function);
541 conf (cray_pointer, subroutine);
542 conf (cray_pointer, entry);
544 conf (cray_pointee, allocatable);
545 conf (cray_pointee, contiguous);
546 conf (cray_pointee, codimension);
547 conf (cray_pointee, intent);
548 conf (cray_pointee, optional);
549 conf (cray_pointee, dummy);
550 conf (cray_pointee, target);
551 conf (cray_pointee, intrinsic);
552 conf (cray_pointee, pointer);
553 conf (cray_pointee, entry);
554 conf (cray_pointee, in_common);
555 conf (cray_pointee, in_equivalence);
556 conf (cray_pointee, threadprivate);
557 conf (cray_pointee, omp_declare_target);
559 conf (data, dummy);
560 conf (data, function);
561 conf (data, result);
562 conf (data, allocatable);
564 conf (value, pointer)
565 conf (value, allocatable)
566 conf (value, subroutine)
567 conf (value, function)
568 conf (value, volatile_)
569 conf (value, dimension)
570 conf (value, codimension)
571 conf (value, external)
573 conf (codimension, result)
575 if (attr->value
576 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
578 a1 = value;
579 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
580 goto conflict;
583 conf (is_protected, intrinsic)
584 conf (is_protected, in_common)
586 conf (asynchronous, intrinsic)
587 conf (asynchronous, external)
589 conf (volatile_, intrinsic)
590 conf (volatile_, external)
592 if (attr->volatile_ && attr->intent == INTENT_IN)
594 a1 = volatile_;
595 a2 = intent_in;
596 goto conflict;
599 conf (procedure, allocatable)
600 conf (procedure, dimension)
601 conf (procedure, codimension)
602 conf (procedure, intrinsic)
603 conf (procedure, target)
604 conf (procedure, value)
605 conf (procedure, volatile_)
606 conf (procedure, asynchronous)
607 conf (procedure, entry)
609 conf (proc_pointer, abstract)
611 conf (entry, omp_declare_target)
613 a1 = gfc_code2string (flavors, attr->flavor);
615 if (attr->in_namelist
616 && attr->flavor != FL_VARIABLE
617 && attr->flavor != FL_PROCEDURE
618 && attr->flavor != FL_UNKNOWN)
620 a2 = in_namelist;
621 goto conflict;
624 switch (attr->flavor)
626 case FL_PROGRAM:
627 case FL_BLOCK_DATA:
628 case FL_MODULE:
629 case FL_LABEL:
630 conf2 (codimension);
631 conf2 (dimension);
632 conf2 (dummy);
633 conf2 (volatile_);
634 conf2 (asynchronous);
635 conf2 (contiguous);
636 conf2 (pointer);
637 conf2 (is_protected);
638 conf2 (target);
639 conf2 (external);
640 conf2 (intrinsic);
641 conf2 (allocatable);
642 conf2 (result);
643 conf2 (in_namelist);
644 conf2 (optional);
645 conf2 (function);
646 conf2 (subroutine);
647 conf2 (threadprivate);
648 conf2 (omp_declare_target);
650 if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
652 a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
653 gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
654 name, where);
655 return false;
658 if (attr->is_bind_c)
660 gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
661 return false;
664 break;
666 case FL_VARIABLE:
667 break;
669 case FL_NAMELIST:
670 conf2 (result);
671 break;
673 case FL_PROCEDURE:
674 /* Conflicts with INTENT, SAVE and RESULT will be checked
675 at resolution stage, see "resolve_fl_procedure". */
677 if (attr->subroutine)
679 a1 = subroutine;
680 conf2 (target);
681 conf2 (allocatable);
682 conf2 (volatile_);
683 conf2 (asynchronous);
684 conf2 (in_namelist);
685 conf2 (codimension);
686 conf2 (dimension);
687 conf2 (function);
688 if (!attr->proc_pointer)
689 conf2 (threadprivate);
692 if (!attr->proc_pointer)
693 conf2 (in_common);
695 switch (attr->proc)
697 case PROC_ST_FUNCTION:
698 conf2 (dummy);
699 conf2 (target);
700 break;
702 case PROC_MODULE:
703 conf2 (dummy);
704 break;
706 case PROC_DUMMY:
707 conf2 (result);
708 conf2 (threadprivate);
709 break;
711 default:
712 break;
715 break;
717 case FL_DERIVED:
718 conf2 (dummy);
719 conf2 (pointer);
720 conf2 (target);
721 conf2 (external);
722 conf2 (intrinsic);
723 conf2 (allocatable);
724 conf2 (optional);
725 conf2 (entry);
726 conf2 (function);
727 conf2 (subroutine);
728 conf2 (threadprivate);
729 conf2 (result);
730 conf2 (omp_declare_target);
732 if (attr->intent != INTENT_UNKNOWN)
734 a2 = intent;
735 goto conflict;
737 break;
739 case FL_PARAMETER:
740 conf2 (external);
741 conf2 (intrinsic);
742 conf2 (optional);
743 conf2 (allocatable);
744 conf2 (function);
745 conf2 (subroutine);
746 conf2 (entry);
747 conf2 (contiguous);
748 conf2 (pointer);
749 conf2 (is_protected);
750 conf2 (target);
751 conf2 (dummy);
752 conf2 (in_common);
753 conf2 (value);
754 conf2 (volatile_);
755 conf2 (asynchronous);
756 conf2 (threadprivate);
757 conf2 (value);
758 conf2 (codimension);
759 conf2 (result);
760 if (!attr->is_iso_c)
761 conf2 (is_bind_c);
762 break;
764 default:
765 break;
768 return true;
770 conflict:
771 if (name == NULL)
772 gfc_error ("%s attribute conflicts with %s attribute at %L",
773 a1, a2, where);
774 else
775 gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
776 a1, a2, name, where);
778 return false;
780 conflict_std:
781 if (name == NULL)
783 return gfc_notify_std (standard, "%s attribute "
784 "with %s attribute at %L", a1, a2,
785 where);
787 else
789 return gfc_notify_std (standard, "%s attribute "
790 "with %s attribute in %qs at %L",
791 a1, a2, name, where);
795 #undef conf
796 #undef conf2
797 #undef conf_std
800 /* Mark a symbol as referenced. */
802 void
803 gfc_set_sym_referenced (gfc_symbol *sym)
806 if (sym->attr.referenced)
807 return;
809 sym->attr.referenced = 1;
811 /* Remember which order dummy variables are accessed in. */
812 if (sym->attr.dummy)
813 sym->dummy_order = next_dummy_order++;
817 /* Common subroutine called by attribute changing subroutines in order
818 to prevent them from changing a symbol that has been
819 use-associated. Returns zero if it is OK to change the symbol,
820 nonzero if not. */
822 static int
823 check_used (symbol_attribute *attr, const char *name, locus *where)
826 if (attr->use_assoc == 0)
827 return 0;
829 if (where == NULL)
830 where = &gfc_current_locus;
832 if (name == NULL)
833 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
834 where);
835 else
836 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
837 name, where);
839 return 1;
843 /* Generate an error because of a duplicate attribute. */
845 static void
846 duplicate_attr (const char *attr, locus *where)
849 if (where == NULL)
850 where = &gfc_current_locus;
852 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
856 bool
857 gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
858 locus *where ATTRIBUTE_UNUSED)
860 attr->ext_attr |= 1 << ext_attr;
861 return true;
865 /* Called from decl.c (attr_decl1) to check attributes, when declared
866 separately. */
868 bool
869 gfc_add_attribute (symbol_attribute *attr, locus *where)
871 if (check_used (attr, NULL, where))
872 return false;
874 return check_conflict (attr, NULL, where);
878 bool
879 gfc_add_allocatable (symbol_attribute *attr, locus *where)
882 if (check_used (attr, NULL, where))
883 return false;
885 if (attr->allocatable)
887 duplicate_attr ("ALLOCATABLE", where);
888 return false;
891 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
892 && !gfc_find_state (COMP_INTERFACE))
894 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
895 where);
896 return false;
899 attr->allocatable = 1;
900 return check_conflict (attr, NULL, where);
904 bool
905 gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
908 if (check_used (attr, name, where))
909 return false;
911 if (attr->codimension)
913 duplicate_attr ("CODIMENSION", where);
914 return false;
917 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
918 && !gfc_find_state (COMP_INTERFACE))
920 gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
921 "at %L", name, where);
922 return false;
925 attr->codimension = 1;
926 return check_conflict (attr, name, where);
930 bool
931 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
934 if (check_used (attr, name, where))
935 return false;
937 if (attr->dimension)
939 duplicate_attr ("DIMENSION", where);
940 return false;
943 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
944 && !gfc_find_state (COMP_INTERFACE))
946 gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
947 "at %L", name, where);
948 return false;
951 attr->dimension = 1;
952 return check_conflict (attr, name, where);
956 bool
957 gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
960 if (check_used (attr, name, where))
961 return false;
963 attr->contiguous = 1;
964 return check_conflict (attr, name, where);
968 bool
969 gfc_add_external (symbol_attribute *attr, locus *where)
972 if (check_used (attr, NULL, where))
973 return false;
975 if (attr->external)
977 duplicate_attr ("EXTERNAL", where);
978 return false;
981 if (attr->pointer && attr->if_source != IFSRC_IFBODY)
983 attr->pointer = 0;
984 attr->proc_pointer = 1;
987 attr->external = 1;
989 return check_conflict (attr, NULL, where);
993 bool
994 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
997 if (check_used (attr, NULL, where))
998 return false;
1000 if (attr->intrinsic)
1002 duplicate_attr ("INTRINSIC", where);
1003 return false;
1006 attr->intrinsic = 1;
1008 return check_conflict (attr, NULL, where);
1012 bool
1013 gfc_add_optional (symbol_attribute *attr, locus *where)
1016 if (check_used (attr, NULL, where))
1017 return false;
1019 if (attr->optional)
1021 duplicate_attr ("OPTIONAL", where);
1022 return false;
1025 attr->optional = 1;
1026 return check_conflict (attr, NULL, where);
1030 bool
1031 gfc_add_pointer (symbol_attribute *attr, locus *where)
1034 if (check_used (attr, NULL, where))
1035 return false;
1037 if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
1038 && !gfc_find_state (COMP_INTERFACE)))
1040 duplicate_attr ("POINTER", where);
1041 return false;
1044 if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
1045 || (attr->if_source == IFSRC_IFBODY
1046 && !gfc_find_state (COMP_INTERFACE)))
1047 attr->proc_pointer = 1;
1048 else
1049 attr->pointer = 1;
1051 return check_conflict (attr, NULL, where);
1055 bool
1056 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
1059 if (check_used (attr, NULL, where))
1060 return false;
1062 attr->cray_pointer = 1;
1063 return check_conflict (attr, NULL, where);
1067 bool
1068 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
1071 if (check_used (attr, NULL, where))
1072 return false;
1074 if (attr->cray_pointee)
1076 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1077 " statements", where);
1078 return false;
1081 attr->cray_pointee = 1;
1082 return check_conflict (attr, NULL, where);
1086 bool
1087 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
1089 if (check_used (attr, name, where))
1090 return false;
1092 if (attr->is_protected)
1094 if (!gfc_notify_std (GFC_STD_LEGACY,
1095 "Duplicate PROTECTED attribute specified at %L",
1096 where))
1097 return false;
1100 attr->is_protected = 1;
1101 return check_conflict (attr, name, where);
1105 bool
1106 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
1109 if (check_used (attr, name, where))
1110 return false;
1112 attr->result = 1;
1113 return check_conflict (attr, name, where);
1117 bool
1118 gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
1119 locus *where)
1122 if (check_used (attr, name, where))
1123 return false;
1125 if (s == SAVE_EXPLICIT && gfc_pure (NULL))
1127 gfc_error
1128 ("SAVE attribute at %L cannot be specified in a PURE procedure",
1129 where);
1130 return false;
1133 if (s == SAVE_EXPLICIT)
1134 gfc_unset_implicit_pure (NULL);
1136 if (s == SAVE_EXPLICIT && attr->save == SAVE_EXPLICIT)
1138 if (!gfc_notify_std (GFC_STD_LEGACY,
1139 "Duplicate SAVE attribute specified at %L",
1140 where))
1141 return false;
1144 attr->save = s;
1145 return check_conflict (attr, name, where);
1149 bool
1150 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1153 if (check_used (attr, name, where))
1154 return false;
1156 if (attr->value)
1158 if (!gfc_notify_std (GFC_STD_LEGACY,
1159 "Duplicate VALUE attribute specified at %L",
1160 where))
1161 return false;
1164 attr->value = 1;
1165 return check_conflict (attr, name, where);
1169 bool
1170 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1172 /* No check_used needed as 11.2.1 of the F2003 standard allows
1173 that the local identifier made accessible by a use statement can be
1174 given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1176 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1177 if (!gfc_notify_std (GFC_STD_LEGACY,
1178 "Duplicate VOLATILE attribute specified at %L",
1179 where))
1180 return false;
1182 attr->volatile_ = 1;
1183 attr->volatile_ns = gfc_current_ns;
1184 return check_conflict (attr, name, where);
1188 bool
1189 gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
1191 /* No check_used needed as 11.2.1 of the F2003 standard allows
1192 that the local identifier made accessible by a use statement can be
1193 given a ASYNCHRONOUS attribute. */
1195 if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
1196 if (!gfc_notify_std (GFC_STD_LEGACY,
1197 "Duplicate ASYNCHRONOUS attribute specified at %L",
1198 where))
1199 return false;
1201 attr->asynchronous = 1;
1202 attr->asynchronous_ns = gfc_current_ns;
1203 return check_conflict (attr, name, where);
1207 bool
1208 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1211 if (check_used (attr, name, where))
1212 return false;
1214 if (attr->threadprivate)
1216 duplicate_attr ("THREADPRIVATE", where);
1217 return false;
1220 attr->threadprivate = 1;
1221 return check_conflict (attr, name, where);
1225 bool
1226 gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
1227 locus *where)
1230 if (check_used (attr, name, where))
1231 return false;
1233 if (attr->omp_declare_target)
1234 return true;
1236 attr->omp_declare_target = 1;
1237 return check_conflict (attr, name, where);
1241 bool
1242 gfc_add_target (symbol_attribute *attr, locus *where)
1245 if (check_used (attr, NULL, where))
1246 return false;
1248 if (attr->target)
1250 duplicate_attr ("TARGET", where);
1251 return false;
1254 attr->target = 1;
1255 return check_conflict (attr, NULL, where);
1259 bool
1260 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1263 if (check_used (attr, name, where))
1264 return false;
1266 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1267 attr->dummy = 1;
1268 return check_conflict (attr, name, where);
1272 bool
1273 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1276 if (check_used (attr, name, where))
1277 return false;
1279 /* Duplicate attribute already checked for. */
1280 attr->in_common = 1;
1281 return check_conflict (attr, name, where);
1285 bool
1286 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1289 /* Duplicate attribute already checked for. */
1290 attr->in_equivalence = 1;
1291 if (!check_conflict (attr, name, where))
1292 return false;
1294 if (attr->flavor == FL_VARIABLE)
1295 return true;
1297 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1301 bool
1302 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1305 if (check_used (attr, name, where))
1306 return false;
1308 attr->data = 1;
1309 return check_conflict (attr, name, where);
1313 bool
1314 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1317 attr->in_namelist = 1;
1318 return check_conflict (attr, name, where);
1322 bool
1323 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1326 if (check_used (attr, name, where))
1327 return false;
1329 attr->sequence = 1;
1330 return check_conflict (attr, name, where);
1334 bool
1335 gfc_add_elemental (symbol_attribute *attr, locus *where)
1338 if (check_used (attr, NULL, where))
1339 return false;
1341 if (attr->elemental)
1343 duplicate_attr ("ELEMENTAL", where);
1344 return false;
1347 attr->elemental = 1;
1348 return check_conflict (attr, NULL, where);
1352 bool
1353 gfc_add_pure (symbol_attribute *attr, locus *where)
1356 if (check_used (attr, NULL, where))
1357 return false;
1359 if (attr->pure)
1361 duplicate_attr ("PURE", where);
1362 return false;
1365 attr->pure = 1;
1366 return check_conflict (attr, NULL, where);
1370 bool
1371 gfc_add_recursive (symbol_attribute *attr, locus *where)
1374 if (check_used (attr, NULL, where))
1375 return false;
1377 if (attr->recursive)
1379 duplicate_attr ("RECURSIVE", where);
1380 return false;
1383 attr->recursive = 1;
1384 return check_conflict (attr, NULL, where);
1388 bool
1389 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1392 if (check_used (attr, name, where))
1393 return false;
1395 if (attr->entry)
1397 duplicate_attr ("ENTRY", where);
1398 return false;
1401 attr->entry = 1;
1402 return check_conflict (attr, name, where);
1406 bool
1407 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1410 if (attr->flavor != FL_PROCEDURE
1411 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1412 return false;
1414 attr->function = 1;
1415 return check_conflict (attr, name, where);
1419 bool
1420 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1423 if (attr->flavor != FL_PROCEDURE
1424 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1425 return false;
1427 attr->subroutine = 1;
1428 return check_conflict (attr, name, where);
1432 bool
1433 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1436 if (attr->flavor != FL_PROCEDURE
1437 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1438 return false;
1440 attr->generic = 1;
1441 return check_conflict (attr, name, where);
1445 bool
1446 gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1449 if (check_used (attr, NULL, where))
1450 return false;
1452 if (attr->flavor != FL_PROCEDURE
1453 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1454 return false;
1456 if (attr->procedure)
1458 duplicate_attr ("PROCEDURE", where);
1459 return false;
1462 attr->procedure = 1;
1464 return check_conflict (attr, NULL, where);
1468 bool
1469 gfc_add_abstract (symbol_attribute* attr, locus* where)
1471 if (attr->abstract)
1473 duplicate_attr ("ABSTRACT", where);
1474 return false;
1477 attr->abstract = 1;
1479 return check_conflict (attr, NULL, where);
1483 /* Flavors are special because some flavors are not what Fortran
1484 considers attributes and can be reaffirmed multiple times. */
1486 bool
1487 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1488 locus *where)
1491 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1492 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1493 || f == FL_NAMELIST) && check_used (attr, name, where))
1494 return false;
1496 if (attr->flavor == f && f == FL_VARIABLE)
1497 return true;
1499 if (attr->flavor != FL_UNKNOWN)
1501 if (where == NULL)
1502 where = &gfc_current_locus;
1504 if (name)
1505 gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
1506 gfc_code2string (flavors, attr->flavor), name,
1507 gfc_code2string (flavors, f), where);
1508 else
1509 gfc_error ("%s attribute conflicts with %s attribute at %L",
1510 gfc_code2string (flavors, attr->flavor),
1511 gfc_code2string (flavors, f), where);
1513 return false;
1516 attr->flavor = f;
1518 return check_conflict (attr, name, where);
1522 bool
1523 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1524 const char *name, locus *where)
1527 if (check_used (attr, name, where))
1528 return false;
1530 if (attr->flavor != FL_PROCEDURE
1531 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1532 return false;
1534 if (where == NULL)
1535 where = &gfc_current_locus;
1537 if (attr->proc != PROC_UNKNOWN)
1539 gfc_error ("%s procedure at %L is already declared as %s procedure",
1540 gfc_code2string (procedures, t), where,
1541 gfc_code2string (procedures, attr->proc));
1543 return false;
1546 attr->proc = t;
1548 /* Statement functions are always scalar and functions. */
1549 if (t == PROC_ST_FUNCTION
1550 && ((!attr->function && !gfc_add_function (attr, name, where))
1551 || attr->dimension))
1552 return false;
1554 return check_conflict (attr, name, where);
1558 bool
1559 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1562 if (check_used (attr, NULL, where))
1563 return false;
1565 if (attr->intent == INTENT_UNKNOWN)
1567 attr->intent = intent;
1568 return check_conflict (attr, NULL, where);
1571 if (where == NULL)
1572 where = &gfc_current_locus;
1574 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1575 gfc_intent_string (attr->intent),
1576 gfc_intent_string (intent), where);
1578 return false;
1582 /* No checks for use-association in public and private statements. */
1584 bool
1585 gfc_add_access (symbol_attribute *attr, gfc_access access,
1586 const char *name, locus *where)
1589 if (attr->access == ACCESS_UNKNOWN
1590 || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
1592 attr->access = access;
1593 return check_conflict (attr, name, where);
1596 if (where == NULL)
1597 where = &gfc_current_locus;
1598 gfc_error ("ACCESS specification at %L was already specified", where);
1600 return false;
1604 /* Set the is_bind_c field for the given symbol_attribute. */
1606 bool
1607 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1608 int is_proc_lang_bind_spec)
1611 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1612 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1613 "variables or common blocks", where);
1614 else if (attr->is_bind_c)
1615 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1616 else
1617 attr->is_bind_c = 1;
1619 if (where == NULL)
1620 where = &gfc_current_locus;
1622 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
1623 return false;
1625 return check_conflict (attr, name, where);
1629 /* Set the extension field for the given symbol_attribute. */
1631 bool
1632 gfc_add_extension (symbol_attribute *attr, locus *where)
1634 if (where == NULL)
1635 where = &gfc_current_locus;
1637 if (attr->extension)
1638 gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
1639 else
1640 attr->extension = 1;
1642 if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
1643 return false;
1645 return true;
1649 bool
1650 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1651 gfc_formal_arglist * formal, locus *where)
1654 if (check_used (&sym->attr, sym->name, where))
1655 return false;
1657 if (where == NULL)
1658 where = &gfc_current_locus;
1660 if (sym->attr.if_source != IFSRC_UNKNOWN
1661 && sym->attr.if_source != IFSRC_DECL)
1663 gfc_error ("Symbol %qs at %L already has an explicit interface",
1664 sym->name, where);
1665 return false;
1668 if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1670 gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
1671 "body", sym->name, where);
1672 return false;
1675 sym->formal = formal;
1676 sym->attr.if_source = source;
1678 return true;
1682 /* Add a type to a symbol. */
1684 bool
1685 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1687 sym_flavor flavor;
1688 bt type;
1690 if (where == NULL)
1691 where = &gfc_current_locus;
1693 if (sym->result)
1694 type = sym->result->ts.type;
1695 else
1696 type = sym->ts.type;
1698 if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
1699 type = sym->ns->proc_name->ts.type;
1701 if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type))
1703 if (sym->attr.use_assoc)
1704 gfc_error_1 ("Symbol '%s' at %L conflicts with symbol from module '%s', "
1705 "use-associated at %L", sym->name, where, sym->module,
1706 &sym->declared_at);
1707 else
1708 gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
1709 where, gfc_basic_typename (type));
1710 return false;
1713 if (sym->attr.procedure && sym->ts.interface)
1715 gfc_error ("Procedure %qs at %L may not have basic type of %s",
1716 sym->name, where, gfc_basic_typename (ts->type));
1717 return false;
1720 flavor = sym->attr.flavor;
1722 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1723 || flavor == FL_LABEL
1724 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
1725 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1727 gfc_error ("Symbol %qs at %L cannot have a type", sym->name, where);
1728 return false;
1731 sym->ts = *ts;
1732 return true;
1736 /* Clears all attributes. */
1738 void
1739 gfc_clear_attr (symbol_attribute *attr)
1741 memset (attr, 0, sizeof (symbol_attribute));
1745 /* Check for missing attributes in the new symbol. Currently does
1746 nothing, but it's not clear that it is unnecessary yet. */
1748 bool
1749 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1750 locus *where ATTRIBUTE_UNUSED)
1753 return true;
1757 /* Copy an attribute to a symbol attribute, bit by bit. Some
1758 attributes have a lot of side-effects but cannot be present given
1759 where we are called from, so we ignore some bits. */
1761 bool
1762 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
1764 int is_proc_lang_bind_spec;
1766 /* In line with the other attributes, we only add bits but do not remove
1767 them; cf. also PR 41034. */
1768 dest->ext_attr |= src->ext_attr;
1770 if (src->allocatable && !gfc_add_allocatable (dest, where))
1771 goto fail;
1773 if (src->dimension && !gfc_add_dimension (dest, NULL, where))
1774 goto fail;
1775 if (src->codimension && !gfc_add_codimension (dest, NULL, where))
1776 goto fail;
1777 if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
1778 goto fail;
1779 if (src->optional && !gfc_add_optional (dest, where))
1780 goto fail;
1781 if (src->pointer && !gfc_add_pointer (dest, where))
1782 goto fail;
1783 if (src->is_protected && !gfc_add_protected (dest, NULL, where))
1784 goto fail;
1785 if (src->save && !gfc_add_save (dest, src->save, NULL, where))
1786 goto fail;
1787 if (src->value && !gfc_add_value (dest, NULL, where))
1788 goto fail;
1789 if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
1790 goto fail;
1791 if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
1792 goto fail;
1793 if (src->threadprivate
1794 && !gfc_add_threadprivate (dest, NULL, where))
1795 goto fail;
1796 if (src->omp_declare_target
1797 && !gfc_add_omp_declare_target (dest, NULL, where))
1798 goto fail;
1799 if (src->target && !gfc_add_target (dest, where))
1800 goto fail;
1801 if (src->dummy && !gfc_add_dummy (dest, NULL, where))
1802 goto fail;
1803 if (src->result && !gfc_add_result (dest, NULL, where))
1804 goto fail;
1805 if (src->entry)
1806 dest->entry = 1;
1808 if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
1809 goto fail;
1811 if (src->in_common && !gfc_add_in_common (dest, NULL, where))
1812 goto fail;
1814 if (src->generic && !gfc_add_generic (dest, NULL, where))
1815 goto fail;
1816 if (src->function && !gfc_add_function (dest, NULL, where))
1817 goto fail;
1818 if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
1819 goto fail;
1821 if (src->sequence && !gfc_add_sequence (dest, NULL, where))
1822 goto fail;
1823 if (src->elemental && !gfc_add_elemental (dest, where))
1824 goto fail;
1825 if (src->pure && !gfc_add_pure (dest, where))
1826 goto fail;
1827 if (src->recursive && !gfc_add_recursive (dest, where))
1828 goto fail;
1830 if (src->flavor != FL_UNKNOWN
1831 && !gfc_add_flavor (dest, src->flavor, NULL, where))
1832 goto fail;
1834 if (src->intent != INTENT_UNKNOWN
1835 && !gfc_add_intent (dest, src->intent, where))
1836 goto fail;
1838 if (src->access != ACCESS_UNKNOWN
1839 && !gfc_add_access (dest, src->access, NULL, where))
1840 goto fail;
1842 if (!gfc_missing_attr (dest, where))
1843 goto fail;
1845 if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
1846 goto fail;
1847 if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
1848 goto fail;
1850 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1851 if (src->is_bind_c
1852 && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
1853 return false;
1855 if (src->is_c_interop)
1856 dest->is_c_interop = 1;
1857 if (src->is_iso_c)
1858 dest->is_iso_c = 1;
1860 if (src->external && !gfc_add_external (dest, where))
1861 goto fail;
1862 if (src->intrinsic && !gfc_add_intrinsic (dest, where))
1863 goto fail;
1864 if (src->proc_pointer)
1865 dest->proc_pointer = 1;
1867 return true;
1869 fail:
1870 return false;
1874 /************** Component name management ************/
1876 /* Component names of a derived type form their own little namespaces
1877 that are separate from all other spaces. The space is composed of
1878 a singly linked list of gfc_component structures whose head is
1879 located in the parent symbol. */
1882 /* Add a component name to a symbol. The call fails if the name is
1883 already present. On success, the component pointer is modified to
1884 point to the additional component structure. */
1886 bool
1887 gfc_add_component (gfc_symbol *sym, const char *name,
1888 gfc_component **component)
1890 gfc_component *p, *tail;
1892 tail = NULL;
1894 for (p = sym->components; p; p = p->next)
1896 if (strcmp (p->name, name) == 0)
1898 gfc_error_1 ("Component '%s' at %C already declared at %L",
1899 name, &p->loc);
1900 return false;
1903 tail = p;
1906 if (sym->attr.extension
1907 && gfc_find_component (sym->components->ts.u.derived, name, true, true))
1909 gfc_error_1 ("Component '%s' at %C already in the parent type "
1910 "at %L", name, &sym->components->ts.u.derived->declared_at);
1911 return false;
1914 /* Allocate a new component. */
1915 p = gfc_get_component ();
1917 if (tail == NULL)
1918 sym->components = p;
1919 else
1920 tail->next = p;
1922 p->name = gfc_get_string (name);
1923 p->loc = gfc_current_locus;
1924 p->ts.type = BT_UNKNOWN;
1926 *component = p;
1927 return true;
1931 /* Recursive function to switch derived types of all symbol in a
1932 namespace. */
1934 static void
1935 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
1937 gfc_symbol *sym;
1939 if (st == NULL)
1940 return;
1942 sym = st->n.sym;
1943 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
1944 sym->ts.u.derived = to;
1946 switch_types (st->left, from, to);
1947 switch_types (st->right, from, to);
1951 /* This subroutine is called when a derived type is used in order to
1952 make the final determination about which version to use. The
1953 standard requires that a type be defined before it is 'used', but
1954 such types can appear in IMPLICIT statements before the actual
1955 definition. 'Using' in this context means declaring a variable to
1956 be that type or using the type constructor.
1958 If a type is used and the components haven't been defined, then we
1959 have to have a derived type in a parent unit. We find the node in
1960 the other namespace and point the symtree node in this namespace to
1961 that node. Further reference to this name point to the correct
1962 node. If we can't find the node in a parent namespace, then we have
1963 an error.
1965 This subroutine takes a pointer to a symbol node and returns a
1966 pointer to the translated node or NULL for an error. Usually there
1967 is no translation and we return the node we were passed. */
1969 gfc_symbol *
1970 gfc_use_derived (gfc_symbol *sym)
1972 gfc_symbol *s;
1973 gfc_typespec *t;
1974 gfc_symtree *st;
1975 int i;
1977 if (!sym)
1978 return NULL;
1980 if (sym->attr.unlimited_polymorphic)
1981 return sym;
1983 if (sym->attr.generic)
1984 sym = gfc_find_dt_in_generic (sym);
1986 if (sym->components != NULL || sym->attr.zero_comp)
1987 return sym; /* Already defined. */
1989 if (sym->ns->parent == NULL)
1990 goto bad;
1992 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1994 gfc_error ("Symbol %qs at %C is ambiguous", sym->name);
1995 return NULL;
1998 if (s == NULL || s->attr.flavor != FL_DERIVED)
1999 goto bad;
2001 /* Get rid of symbol sym, translating all references to s. */
2002 for (i = 0; i < GFC_LETTERS; i++)
2004 t = &sym->ns->default_type[i];
2005 if (t->u.derived == sym)
2006 t->u.derived = s;
2009 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
2010 st->n.sym = s;
2012 s->refs++;
2014 /* Unlink from list of modified symbols. */
2015 gfc_commit_symbol (sym);
2017 switch_types (sym->ns->sym_root, sym, s);
2019 /* TODO: Also have to replace sym -> s in other lists like
2020 namelists, common lists and interface lists. */
2021 gfc_free_symbol (sym);
2023 return s;
2025 bad:
2026 gfc_error ("Derived type %qs at %C is being used before it is defined",
2027 sym->name);
2028 return NULL;
2032 /* Given a derived type node and a component name, try to locate the
2033 component structure. Returns the NULL pointer if the component is
2034 not found or the components are private. If noaccess is set, no access
2035 checks are done. */
2037 gfc_component *
2038 gfc_find_component (gfc_symbol *sym, const char *name,
2039 bool noaccess, bool silent)
2041 gfc_component *p;
2043 if (name == NULL || sym == NULL)
2044 return NULL;
2046 sym = gfc_use_derived (sym);
2048 if (sym == NULL)
2049 return NULL;
2051 for (p = sym->components; p; p = p->next)
2052 if (strcmp (p->name, name) == 0)
2053 break;
2055 if (p && sym->attr.use_assoc && !noaccess)
2057 bool is_parent_comp = sym->attr.extension && (p == sym->components);
2058 if (p->attr.access == ACCESS_PRIVATE ||
2059 (p->attr.access != ACCESS_PUBLIC
2060 && sym->component_access == ACCESS_PRIVATE
2061 && !is_parent_comp))
2063 if (!silent)
2064 gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
2065 name, sym->name);
2066 return NULL;
2070 if (p == NULL
2071 && sym->attr.extension
2072 && sym->components->ts.type == BT_DERIVED)
2074 p = gfc_find_component (sym->components->ts.u.derived, name,
2075 noaccess, silent);
2076 /* Do not overwrite the error. */
2077 if (p == NULL)
2078 return p;
2081 if (p == NULL && !silent)
2082 gfc_error ("%qs at %C is not a member of the %qs structure",
2083 name, sym->name);
2085 return p;
2089 /* Given a symbol, free all of the component structures and everything
2090 they point to. */
2092 static void
2093 free_components (gfc_component *p)
2095 gfc_component *q;
2097 for (; p; p = q)
2099 q = p->next;
2101 gfc_free_array_spec (p->as);
2102 gfc_free_expr (p->initializer);
2103 free (p->tb);
2105 free (p);
2110 /******************** Statement label management ********************/
2112 /* Comparison function for statement labels, used for managing the
2113 binary tree. */
2115 static int
2116 compare_st_labels (void *a1, void *b1)
2118 int a = ((gfc_st_label *) a1)->value;
2119 int b = ((gfc_st_label *) b1)->value;
2121 return (b - a);
2125 /* Free a single gfc_st_label structure, making sure the tree is not
2126 messed up. This function is called only when some parse error
2127 occurs. */
2129 void
2130 gfc_free_st_label (gfc_st_label *label)
2133 if (label == NULL)
2134 return;
2136 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
2138 if (label->format != NULL)
2139 gfc_free_expr (label->format);
2141 free (label);
2145 /* Free a whole tree of gfc_st_label structures. */
2147 static void
2148 free_st_labels (gfc_st_label *label)
2151 if (label == NULL)
2152 return;
2154 free_st_labels (label->left);
2155 free_st_labels (label->right);
2157 if (label->format != NULL)
2158 gfc_free_expr (label->format);
2159 free (label);
2163 /* Given a label number, search for and return a pointer to the label
2164 structure, creating it if it does not exist. */
2166 gfc_st_label *
2167 gfc_get_st_label (int labelno)
2169 gfc_st_label *lp;
2170 gfc_namespace *ns;
2172 if (gfc_current_state () == COMP_DERIVED)
2173 ns = gfc_current_block ()->f2k_derived;
2174 else
2176 /* Find the namespace of the scoping unit:
2177 If we're in a BLOCK construct, jump to the parent namespace. */
2178 ns = gfc_current_ns;
2179 while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
2180 ns = ns->parent;
2183 /* First see if the label is already in this namespace. */
2184 lp = ns->st_labels;
2185 while (lp)
2187 if (lp->value == labelno)
2188 return lp;
2190 if (lp->value < labelno)
2191 lp = lp->left;
2192 else
2193 lp = lp->right;
2196 lp = XCNEW (gfc_st_label);
2198 lp->value = labelno;
2199 lp->defined = ST_LABEL_UNKNOWN;
2200 lp->referenced = ST_LABEL_UNKNOWN;
2202 gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
2204 return lp;
2208 /* Called when a statement with a statement label is about to be
2209 accepted. We add the label to the list of the current namespace,
2210 making sure it hasn't been defined previously and referenced
2211 correctly. */
2213 void
2214 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
2216 int labelno;
2218 labelno = lp->value;
2220 if (lp->defined != ST_LABEL_UNKNOWN)
2221 gfc_error_1 ("Duplicate statement label %d at %L and %L", labelno,
2222 &lp->where, label_locus);
2223 else
2225 lp->where = *label_locus;
2227 switch (type)
2229 case ST_LABEL_FORMAT:
2230 if (lp->referenced == ST_LABEL_TARGET
2231 || lp->referenced == ST_LABEL_DO_TARGET)
2232 gfc_error ("Label %d at %C already referenced as branch target",
2233 labelno);
2234 else
2235 lp->defined = ST_LABEL_FORMAT;
2237 break;
2239 case ST_LABEL_TARGET:
2240 case ST_LABEL_DO_TARGET:
2241 if (lp->referenced == ST_LABEL_FORMAT)
2242 gfc_error ("Label %d at %C already referenced as a format label",
2243 labelno);
2244 else
2245 lp->defined = type;
2247 if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
2248 && !gfc_notify_std (GFC_STD_F95_OBS, "DO termination statement "
2249 "which is not END DO or CONTINUE with "
2250 "label %d at %C", labelno))
2251 return;
2252 break;
2254 default:
2255 lp->defined = ST_LABEL_BAD_TARGET;
2256 lp->referenced = ST_LABEL_BAD_TARGET;
2262 /* Reference a label. Given a label and its type, see if that
2263 reference is consistent with what is known about that label,
2264 updating the unknown state. Returns false if something goes
2265 wrong. */
2267 bool
2268 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2270 gfc_sl_type label_type;
2271 int labelno;
2272 bool rc;
2274 if (lp == NULL)
2275 return true;
2277 labelno = lp->value;
2279 if (lp->defined != ST_LABEL_UNKNOWN)
2280 label_type = lp->defined;
2281 else
2283 label_type = lp->referenced;
2284 lp->where = gfc_current_locus;
2287 if (label_type == ST_LABEL_FORMAT
2288 && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
2290 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2291 rc = false;
2292 goto done;
2295 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
2296 || label_type == ST_LABEL_BAD_TARGET)
2297 && type == ST_LABEL_FORMAT)
2299 gfc_error ("Label %d at %C previously used as branch target", labelno);
2300 rc = false;
2301 goto done;
2304 if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
2305 && !gfc_notify_std (GFC_STD_F95_OBS, "Shared DO termination label %d "
2306 "at %C", labelno))
2307 return false;
2309 if (lp->referenced != ST_LABEL_DO_TARGET)
2310 lp->referenced = type;
2311 rc = true;
2313 done:
2314 return rc;
2318 /************** Symbol table management subroutines ****************/
2320 /* Basic details: Fortran 95 requires a potentially unlimited number
2321 of distinct namespaces when compiling a program unit. This case
2322 occurs during a compilation of internal subprograms because all of
2323 the internal subprograms must be read before we can start
2324 generating code for the host.
2326 Given the tricky nature of the Fortran grammar, we must be able to
2327 undo changes made to a symbol table if the current interpretation
2328 of a statement is found to be incorrect. Whenever a symbol is
2329 looked up, we make a copy of it and link to it. All of these
2330 symbols are kept in a vector so that we can commit or
2331 undo the changes at a later time.
2333 A symtree may point to a symbol node outside of its namespace. In
2334 this case, that symbol has been used as a host associated variable
2335 at some previous time. */
2337 /* Allocate a new namespace structure. Copies the implicit types from
2338 PARENT if PARENT_TYPES is set. */
2340 gfc_namespace *
2341 gfc_get_namespace (gfc_namespace *parent, int parent_types)
2343 gfc_namespace *ns;
2344 gfc_typespec *ts;
2345 int in;
2346 int i;
2348 ns = XCNEW (gfc_namespace);
2349 ns->sym_root = NULL;
2350 ns->uop_root = NULL;
2351 ns->tb_sym_root = NULL;
2352 ns->finalizers = NULL;
2353 ns->default_access = ACCESS_UNKNOWN;
2354 ns->parent = parent;
2356 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
2358 ns->operator_access[in] = ACCESS_UNKNOWN;
2359 ns->tb_op[in] = NULL;
2362 /* Initialize default implicit types. */
2363 for (i = 'a'; i <= 'z'; i++)
2365 ns->set_flag[i - 'a'] = 0;
2366 ts = &ns->default_type[i - 'a'];
2368 if (parent_types && ns->parent != NULL)
2370 /* Copy parent settings. */
2371 *ts = ns->parent->default_type[i - 'a'];
2372 continue;
2375 if (flag_implicit_none != 0)
2377 gfc_clear_ts (ts);
2378 continue;
2381 if ('i' <= i && i <= 'n')
2383 ts->type = BT_INTEGER;
2384 ts->kind = gfc_default_integer_kind;
2386 else
2388 ts->type = BT_REAL;
2389 ts->kind = gfc_default_real_kind;
2393 if (parent_types && ns->parent != NULL)
2394 ns->has_implicit_none_export = ns->parent->has_implicit_none_export;
2396 ns->refs = 1;
2398 return ns;
2402 /* Comparison function for symtree nodes. */
2404 static int
2405 compare_symtree (void *_st1, void *_st2)
2407 gfc_symtree *st1, *st2;
2409 st1 = (gfc_symtree *) _st1;
2410 st2 = (gfc_symtree *) _st2;
2412 return strcmp (st1->name, st2->name);
2416 /* Allocate a new symtree node and associate it with the new symbol. */
2418 gfc_symtree *
2419 gfc_new_symtree (gfc_symtree **root, const char *name)
2421 gfc_symtree *st;
2423 st = XCNEW (gfc_symtree);
2424 st->name = gfc_get_string (name);
2426 gfc_insert_bbt (root, st, compare_symtree);
2427 return st;
2431 /* Delete a symbol from the tree. Does not free the symbol itself! */
2433 void
2434 gfc_delete_symtree (gfc_symtree **root, const char *name)
2436 gfc_symtree st, *st0;
2438 st0 = gfc_find_symtree (*root, name);
2440 st.name = gfc_get_string (name);
2441 gfc_delete_bbt (root, &st, compare_symtree);
2443 free (st0);
2447 /* Given a root symtree node and a name, try to find the symbol within
2448 the namespace. Returns NULL if the symbol is not found. */
2450 gfc_symtree *
2451 gfc_find_symtree (gfc_symtree *st, const char *name)
2453 int c;
2455 while (st != NULL)
2457 c = strcmp (name, st->name);
2458 if (c == 0)
2459 return st;
2461 st = (c < 0) ? st->left : st->right;
2464 return NULL;
2468 /* Return a symtree node with a name that is guaranteed to be unique
2469 within the namespace and corresponds to an illegal fortran name. */
2471 gfc_symtree *
2472 gfc_get_unique_symtree (gfc_namespace *ns)
2474 char name[GFC_MAX_SYMBOL_LEN + 1];
2475 static int serial = 0;
2477 sprintf (name, "@%d", serial++);
2478 return gfc_new_symtree (&ns->sym_root, name);
2482 /* Given a name find a user operator node, creating it if it doesn't
2483 exist. These are much simpler than symbols because they can't be
2484 ambiguous with one another. */
2486 gfc_user_op *
2487 gfc_get_uop (const char *name)
2489 gfc_user_op *uop;
2490 gfc_symtree *st;
2491 gfc_namespace *ns = gfc_current_ns;
2493 if (ns->omp_udr_ns)
2494 ns = ns->parent;
2495 st = gfc_find_symtree (ns->uop_root, name);
2496 if (st != NULL)
2497 return st->n.uop;
2499 st = gfc_new_symtree (&ns->uop_root, name);
2501 uop = st->n.uop = XCNEW (gfc_user_op);
2502 uop->name = gfc_get_string (name);
2503 uop->access = ACCESS_UNKNOWN;
2504 uop->ns = ns;
2506 return uop;
2510 /* Given a name find the user operator node. Returns NULL if it does
2511 not exist. */
2513 gfc_user_op *
2514 gfc_find_uop (const char *name, gfc_namespace *ns)
2516 gfc_symtree *st;
2518 if (ns == NULL)
2519 ns = gfc_current_ns;
2521 st = gfc_find_symtree (ns->uop_root, name);
2522 return (st == NULL) ? NULL : st->n.uop;
2526 /* Remove a gfc_symbol structure and everything it points to. */
2528 void
2529 gfc_free_symbol (gfc_symbol *sym)
2532 if (sym == NULL)
2533 return;
2535 gfc_free_array_spec (sym->as);
2537 free_components (sym->components);
2539 gfc_free_expr (sym->value);
2541 gfc_free_namelist (sym->namelist);
2543 if (sym->ns != sym->formal_ns)
2544 gfc_free_namespace (sym->formal_ns);
2546 if (!sym->attr.generic_copy)
2547 gfc_free_interface (sym->generic);
2549 gfc_free_formal_arglist (sym->formal);
2551 gfc_free_namespace (sym->f2k_derived);
2553 if (sym->common_block && sym->common_block->name[0] != '\0')
2555 sym->common_block->refs--;
2556 if (sym->common_block->refs == 0)
2557 free (sym->common_block);
2560 free (sym);
2564 /* Decrease the reference counter and free memory when we reach zero. */
2566 void
2567 gfc_release_symbol (gfc_symbol *sym)
2569 if (sym == NULL)
2570 return;
2572 if (sym->formal_ns != NULL && sym->refs == 2 && sym->formal_ns != sym->ns
2573 && (!sym->attr.entry || !sym->module))
2575 /* As formal_ns contains a reference to sym, delete formal_ns just
2576 before the deletion of sym. */
2577 gfc_namespace *ns = sym->formal_ns;
2578 sym->formal_ns = NULL;
2579 gfc_free_namespace (ns);
2582 sym->refs--;
2583 if (sym->refs > 0)
2584 return;
2586 gcc_assert (sym->refs == 0);
2587 gfc_free_symbol (sym);
2591 /* Allocate and initialize a new symbol node. */
2593 gfc_symbol *
2594 gfc_new_symbol (const char *name, gfc_namespace *ns)
2596 gfc_symbol *p;
2598 p = XCNEW (gfc_symbol);
2600 gfc_clear_ts (&p->ts);
2601 gfc_clear_attr (&p->attr);
2602 p->ns = ns;
2604 p->declared_at = gfc_current_locus;
2606 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2607 gfc_internal_error ("new_symbol(): Symbol name too long");
2609 p->name = gfc_get_string (name);
2611 /* Make sure flags for symbol being C bound are clear initially. */
2612 p->attr.is_bind_c = 0;
2613 p->attr.is_iso_c = 0;
2615 /* Clear the ptrs we may need. */
2616 p->common_block = NULL;
2617 p->f2k_derived = NULL;
2618 p->assoc = NULL;
2620 return p;
2624 /* Generate an error if a symbol is ambiguous. */
2626 static void
2627 ambiguous_symbol (const char *name, gfc_symtree *st)
2630 if (st->n.sym->module)
2631 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2632 "from module %qs", name, st->n.sym->name, st->n.sym->module);
2633 else
2634 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2635 "from current program unit", name, st->n.sym->name);
2639 /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
2640 selector on the stack. If yes, replace it by the corresponding temporary. */
2642 static void
2643 select_type_insert_tmp (gfc_symtree **st)
2645 gfc_select_type_stack *stack = select_type_stack;
2646 for (; stack; stack = stack->prev)
2647 if ((*st)->n.sym == stack->selector && stack->tmp)
2648 *st = stack->tmp;
2652 /* Look for a symtree in the current procedure -- that is, go up to
2653 parent namespaces but only if inside a BLOCK. Returns NULL if not found. */
2655 gfc_symtree*
2656 gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
2658 while (ns)
2660 gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
2661 if (st)
2662 return st;
2664 if (!ns->construct_entities)
2665 break;
2666 ns = ns->parent;
2669 return NULL;
2673 /* Search for a symtree starting in the current namespace, resorting to
2674 any parent namespaces if requested by a nonzero parent_flag.
2675 Returns nonzero if the name is ambiguous. */
2678 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2679 gfc_symtree **result)
2681 gfc_symtree *st;
2683 if (ns == NULL)
2684 ns = gfc_current_ns;
2688 st = gfc_find_symtree (ns->sym_root, name);
2689 if (st != NULL)
2691 select_type_insert_tmp (&st);
2693 *result = st;
2694 /* Ambiguous generic interfaces are permitted, as long
2695 as the specific interfaces are different. */
2696 if (st->ambiguous && !st->n.sym->attr.generic)
2698 ambiguous_symbol (name, st);
2699 return 1;
2702 return 0;
2705 if (!parent_flag)
2706 break;
2708 /* Don't escape an interface block. */
2709 if (ns && !ns->has_import_set
2710 && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
2711 break;
2713 ns = ns->parent;
2715 while (ns != NULL);
2717 *result = NULL;
2718 return 0;
2722 /* Same, but returns the symbol instead. */
2725 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
2726 gfc_symbol **result)
2728 gfc_symtree *st;
2729 int i;
2731 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2733 if (st == NULL)
2734 *result = NULL;
2735 else
2736 *result = st->n.sym;
2738 return i;
2742 /* Tells whether there is only one set of changes in the stack. */
2744 static bool
2745 single_undo_checkpoint_p (void)
2747 if (latest_undo_chgset == &default_undo_chgset_var)
2749 gcc_assert (latest_undo_chgset->previous == NULL);
2750 return true;
2752 else
2754 gcc_assert (latest_undo_chgset->previous != NULL);
2755 return false;
2759 /* Save symbol with the information necessary to back it out. */
2761 void
2762 gfc_save_symbol_data (gfc_symbol *sym)
2764 gfc_symbol *s;
2765 unsigned i;
2767 if (!single_undo_checkpoint_p ())
2769 /* If there is more than one change set, look for the symbol in the
2770 current one. If it is found there, we can reuse it. */
2771 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
2772 if (s == sym)
2774 gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
2775 return;
2778 else if (sym->gfc_new || sym->old_symbol != NULL)
2779 return;
2781 s = XCNEW (gfc_symbol);
2782 *s = *sym;
2783 sym->old_symbol = s;
2784 sym->gfc_new = 0;
2786 latest_undo_chgset->syms.safe_push (sym);
2790 /* Given a name, find a symbol, or create it if it does not exist yet
2791 in the current namespace. If the symbol is found we make sure that
2792 it's OK.
2794 The integer return code indicates
2795 0 All OK
2796 1 The symbol name was ambiguous
2797 2 The name meant to be established was already host associated.
2799 So if the return value is nonzero, then an error was issued. */
2802 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
2803 bool allow_subroutine)
2805 gfc_symtree *st;
2806 gfc_symbol *p;
2808 /* This doesn't usually happen during resolution. */
2809 if (ns == NULL)
2810 ns = gfc_current_ns;
2812 /* Try to find the symbol in ns. */
2813 st = gfc_find_symtree (ns->sym_root, name);
2815 if (st == NULL && ns->omp_udr_ns)
2817 ns = ns->parent;
2818 st = gfc_find_symtree (ns->sym_root, name);
2821 if (st == NULL)
2823 /* If not there, create a new symbol. */
2824 p = gfc_new_symbol (name, ns);
2826 /* Add to the list of tentative symbols. */
2827 p->old_symbol = NULL;
2828 p->mark = 1;
2829 p->gfc_new = 1;
2830 latest_undo_chgset->syms.safe_push (p);
2832 st = gfc_new_symtree (&ns->sym_root, name);
2833 st->n.sym = p;
2834 p->refs++;
2837 else
2839 /* Make sure the existing symbol is OK. Ambiguous
2840 generic interfaces are permitted, as long as the
2841 specific interfaces are different. */
2842 if (st->ambiguous && !st->n.sym->attr.generic)
2844 ambiguous_symbol (name, st);
2845 return 1;
2848 p = st->n.sym;
2849 if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
2850 && !(allow_subroutine && p->attr.subroutine)
2851 && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
2852 && (ns->has_import_set || p->attr.imported)))
2854 /* Symbol is from another namespace. */
2855 gfc_error ("Symbol %qs at %C has already been host associated",
2856 name);
2857 return 2;
2860 p->mark = 1;
2862 /* Copy in case this symbol is changed. */
2863 gfc_save_symbol_data (p);
2866 *result = st;
2867 return 0;
2872 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
2874 gfc_symtree *st;
2875 int i;
2877 i = gfc_get_sym_tree (name, ns, &st, false);
2878 if (i != 0)
2879 return i;
2881 if (st)
2882 *result = st->n.sym;
2883 else
2884 *result = NULL;
2885 return i;
2889 /* Subroutine that searches for a symbol, creating it if it doesn't
2890 exist, but tries to host-associate the symbol if possible. */
2893 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
2895 gfc_symtree *st;
2896 int i;
2898 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2900 if (st != NULL)
2902 gfc_save_symbol_data (st->n.sym);
2903 *result = st;
2904 return i;
2907 i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
2908 if (i)
2909 return i;
2911 if (st != NULL)
2913 *result = st;
2914 return 0;
2917 return gfc_get_sym_tree (name, gfc_current_ns, result, false);
2922 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
2924 int i;
2925 gfc_symtree *st;
2927 i = gfc_get_ha_sym_tree (name, &st);
2929 if (st)
2930 *result = st->n.sym;
2931 else
2932 *result = NULL;
2934 return i;
2938 /* Search for the symtree belonging to a gfc_common_head; we cannot use
2939 head->name as the common_root symtree's name might be mangled. */
2941 static gfc_symtree *
2942 find_common_symtree (gfc_symtree *st, gfc_common_head *head)
2945 gfc_symtree *result;
2947 if (st == NULL)
2948 return NULL;
2950 if (st->n.common == head)
2951 return st;
2953 result = find_common_symtree (st->left, head);
2954 if (!result)
2955 result = find_common_symtree (st->right, head);
2957 return result;
2961 /* Clear the given storage, and make it the current change set for registering
2962 changed symbols. Its contents are freed after a call to
2963 gfc_restore_last_undo_checkpoint or gfc_drop_last_undo_checkpoint, but
2964 it is up to the caller to free the storage itself. It is usually a local
2965 variable, so there is nothing to do anyway. */
2967 void
2968 gfc_new_undo_checkpoint (gfc_undo_change_set &chg_syms)
2970 chg_syms.syms = vNULL;
2971 chg_syms.tbps = vNULL;
2972 chg_syms.previous = latest_undo_chgset;
2973 latest_undo_chgset = &chg_syms;
2977 /* Restore previous state of symbol. Just copy simple stuff. */
2979 static void
2980 restore_old_symbol (gfc_symbol *p)
2982 gfc_symbol *old;
2984 p->mark = 0;
2985 old = p->old_symbol;
2987 p->ts.type = old->ts.type;
2988 p->ts.kind = old->ts.kind;
2990 p->attr = old->attr;
2992 if (p->value != old->value)
2994 gcc_checking_assert (old->value == NULL);
2995 gfc_free_expr (p->value);
2996 p->value = NULL;
2999 if (p->as != old->as)
3001 if (p->as)
3002 gfc_free_array_spec (p->as);
3003 p->as = old->as;
3006 p->generic = old->generic;
3007 p->component_access = old->component_access;
3009 if (p->namelist != NULL && old->namelist == NULL)
3011 gfc_free_namelist (p->namelist);
3012 p->namelist = NULL;
3014 else
3016 if (p->namelist_tail != old->namelist_tail)
3018 gfc_free_namelist (old->namelist_tail->next);
3019 old->namelist_tail->next = NULL;
3023 p->namelist_tail = old->namelist_tail;
3025 if (p->formal != old->formal)
3027 gfc_free_formal_arglist (p->formal);
3028 p->formal = old->formal;
3031 p->old_symbol = old->old_symbol;
3032 free (old);
3036 /* Frees the internal data of a gfc_undo_change_set structure. Doesn't free
3037 the structure itself. */
3039 static void
3040 free_undo_change_set_data (gfc_undo_change_set &cs)
3042 cs.syms.release ();
3043 cs.tbps.release ();
3047 /* Given a change set pointer, free its target's contents and update it with
3048 the address of the previous change set. Note that only the contents are
3049 freed, not the target itself (the contents' container). It is not a problem
3050 as the latter will be a local variable usually. */
3052 static void
3053 pop_undo_change_set (gfc_undo_change_set *&cs)
3055 free_undo_change_set_data (*cs);
3056 cs = cs->previous;
3060 static void free_old_symbol (gfc_symbol *sym);
3063 /* Merges the current change set into the previous one. The changes themselves
3064 are left untouched; only one checkpoint is forgotten. */
3066 void
3067 gfc_drop_last_undo_checkpoint (void)
3069 gfc_symbol *s, *t;
3070 unsigned i, j;
3072 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3074 /* No need to loop in this case. */
3075 if (s->old_symbol == NULL)
3076 continue;
3078 /* Remove the duplicate symbols. */
3079 FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
3080 if (t == s)
3082 latest_undo_chgset->previous->syms.unordered_remove (j);
3084 /* S->OLD_SYMBOL is the backup symbol for S as it was at the
3085 last checkpoint. We drop that checkpoint, so S->OLD_SYMBOL
3086 shall contain from now on the backup symbol for S as it was
3087 at the checkpoint before. */
3088 if (s->old_symbol->gfc_new)
3090 gcc_assert (s->old_symbol->old_symbol == NULL);
3091 s->gfc_new = s->old_symbol->gfc_new;
3092 free_old_symbol (s);
3094 else
3095 restore_old_symbol (s->old_symbol);
3096 break;
3100 latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
3101 latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
3103 pop_undo_change_set (latest_undo_chgset);
3107 /* Undoes all the changes made to symbols since the previous checkpoint.
3108 This subroutine is made simpler due to the fact that attributes are
3109 never removed once added. */
3111 void
3112 gfc_restore_last_undo_checkpoint (void)
3114 gfc_symbol *p;
3115 unsigned i;
3117 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3119 if (p->gfc_new)
3121 /* Symbol was new. */
3122 if (p->attr.in_common && p->common_block && p->common_block->head)
3124 /* If the symbol was added to any common block, it
3125 needs to be removed to stop the resolver looking
3126 for a (possibly) dead symbol. */
3128 if (p->common_block->head == p && !p->common_next)
3130 gfc_symtree st, *st0;
3131 st0 = find_common_symtree (p->ns->common_root,
3132 p->common_block);
3133 if (st0)
3135 st.name = st0->name;
3136 gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
3137 free (st0);
3141 if (p->common_block->head == p)
3142 p->common_block->head = p->common_next;
3143 else
3145 gfc_symbol *cparent, *csym;
3147 cparent = p->common_block->head;
3148 csym = cparent->common_next;
3150 while (csym != p)
3152 cparent = csym;
3153 csym = csym->common_next;
3156 gcc_assert(cparent->common_next == p);
3158 cparent->common_next = csym->common_next;
3162 /* The derived type is saved in the symtree with the first
3163 letter capitalized; the all lower-case version to the
3164 derived type contains its associated generic function. */
3165 if (p->attr.flavor == FL_DERIVED)
3166 gfc_delete_symtree (&p->ns->sym_root, gfc_get_string ("%c%s",
3167 (char) TOUPPER ((unsigned char) p->name[0]),
3168 &p->name[1]));
3169 else
3170 gfc_delete_symtree (&p->ns->sym_root, p->name);
3172 gfc_release_symbol (p);
3174 else
3175 restore_old_symbol (p);
3178 latest_undo_chgset->syms.truncate (0);
3179 latest_undo_chgset->tbps.truncate (0);
3181 if (!single_undo_checkpoint_p ())
3182 pop_undo_change_set (latest_undo_chgset);
3186 /* Makes sure that there is only one set of changes; in other words we haven't
3187 forgotten to pair a call to gfc_new_checkpoint with a call to either
3188 gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint. */
3190 static void
3191 enforce_single_undo_checkpoint (void)
3193 gcc_checking_assert (single_undo_checkpoint_p ());
3197 /* Undoes all the changes made to symbols in the current statement. */
3199 void
3200 gfc_undo_symbols (void)
3202 enforce_single_undo_checkpoint ();
3203 gfc_restore_last_undo_checkpoint ();
3207 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
3208 components of old_symbol that might need deallocation are the "allocatables"
3209 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
3210 namelist_tail. In case these differ between old_symbol and sym, it's just
3211 because sym->namelist has gotten a few more items. */
3213 static void
3214 free_old_symbol (gfc_symbol *sym)
3217 if (sym->old_symbol == NULL)
3218 return;
3220 if (sym->old_symbol->as != sym->as)
3221 gfc_free_array_spec (sym->old_symbol->as);
3223 if (sym->old_symbol->value != sym->value)
3224 gfc_free_expr (sym->old_symbol->value);
3226 if (sym->old_symbol->formal != sym->formal)
3227 gfc_free_formal_arglist (sym->old_symbol->formal);
3229 free (sym->old_symbol);
3230 sym->old_symbol = NULL;
3234 /* Makes the changes made in the current statement permanent-- gets
3235 rid of undo information. */
3237 void
3238 gfc_commit_symbols (void)
3240 gfc_symbol *p;
3241 gfc_typebound_proc *tbp;
3242 unsigned i;
3244 enforce_single_undo_checkpoint ();
3246 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3248 p->mark = 0;
3249 p->gfc_new = 0;
3250 free_old_symbol (p);
3252 latest_undo_chgset->syms.truncate (0);
3254 FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
3255 tbp->error = 0;
3256 latest_undo_chgset->tbps.truncate (0);
3260 /* Makes the changes made in one symbol permanent -- gets rid of undo
3261 information. */
3263 void
3264 gfc_commit_symbol (gfc_symbol *sym)
3266 gfc_symbol *p;
3267 unsigned i;
3269 enforce_single_undo_checkpoint ();
3271 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3272 if (p == sym)
3274 latest_undo_chgset->syms.unordered_remove (i);
3275 break;
3278 sym->mark = 0;
3279 sym->gfc_new = 0;
3281 free_old_symbol (sym);
3285 /* Recursively free trees containing type-bound procedures. */
3287 static void
3288 free_tb_tree (gfc_symtree *t)
3290 if (t == NULL)
3291 return;
3293 free_tb_tree (t->left);
3294 free_tb_tree (t->right);
3296 /* TODO: Free type-bound procedure structs themselves; probably needs some
3297 sort of ref-counting mechanism. */
3299 free (t);
3303 /* Recursive function that deletes an entire tree and all the common
3304 head structures it points to. */
3306 static void
3307 free_common_tree (gfc_symtree * common_tree)
3309 if (common_tree == NULL)
3310 return;
3312 free_common_tree (common_tree->left);
3313 free_common_tree (common_tree->right);
3315 free (common_tree);
3319 /* Recursive function that deletes an entire tree and all the common
3320 head structures it points to. */
3322 static void
3323 free_omp_udr_tree (gfc_symtree * omp_udr_tree)
3325 if (omp_udr_tree == NULL)
3326 return;
3328 free_omp_udr_tree (omp_udr_tree->left);
3329 free_omp_udr_tree (omp_udr_tree->right);
3331 gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
3332 free (omp_udr_tree);
3336 /* Recursive function that deletes an entire tree and all the user
3337 operator nodes that it contains. */
3339 static void
3340 free_uop_tree (gfc_symtree *uop_tree)
3342 if (uop_tree == NULL)
3343 return;
3345 free_uop_tree (uop_tree->left);
3346 free_uop_tree (uop_tree->right);
3348 gfc_free_interface (uop_tree->n.uop->op);
3349 free (uop_tree->n.uop);
3350 free (uop_tree);
3354 /* Recursive function that deletes an entire tree and all the symbols
3355 that it contains. */
3357 static void
3358 free_sym_tree (gfc_symtree *sym_tree)
3360 if (sym_tree == NULL)
3361 return;
3363 free_sym_tree (sym_tree->left);
3364 free_sym_tree (sym_tree->right);
3366 gfc_release_symbol (sym_tree->n.sym);
3367 free (sym_tree);
3371 /* Free the derived type list. */
3373 void
3374 gfc_free_dt_list (void)
3376 gfc_dt_list *dt, *n;
3378 for (dt = gfc_derived_types; dt; dt = n)
3380 n = dt->next;
3381 free (dt);
3384 gfc_derived_types = NULL;
3388 /* Free the gfc_equiv_info's. */
3390 static void
3391 gfc_free_equiv_infos (gfc_equiv_info *s)
3393 if (s == NULL)
3394 return;
3395 gfc_free_equiv_infos (s->next);
3396 free (s);
3400 /* Free the gfc_equiv_lists. */
3402 static void
3403 gfc_free_equiv_lists (gfc_equiv_list *l)
3405 if (l == NULL)
3406 return;
3407 gfc_free_equiv_lists (l->next);
3408 gfc_free_equiv_infos (l->equiv);
3409 free (l);
3413 /* Free a finalizer procedure list. */
3415 void
3416 gfc_free_finalizer (gfc_finalizer* el)
3418 if (el)
3420 gfc_release_symbol (el->proc_sym);
3421 free (el);
3425 static void
3426 gfc_free_finalizer_list (gfc_finalizer* list)
3428 while (list)
3430 gfc_finalizer* current = list;
3431 list = list->next;
3432 gfc_free_finalizer (current);
3437 /* Create a new gfc_charlen structure and add it to a namespace.
3438 If 'old_cl' is given, the newly created charlen will be a copy of it. */
3440 gfc_charlen*
3441 gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
3443 gfc_charlen *cl;
3444 cl = gfc_get_charlen ();
3446 /* Copy old_cl. */
3447 if (old_cl)
3449 /* Put into namespace, but don't allow reject_statement
3450 to free it if old_cl is given. */
3451 gfc_charlen **prev = &ns->cl_list;
3452 cl->next = ns->old_cl_list;
3453 while (*prev != ns->old_cl_list)
3454 prev = &(*prev)->next;
3455 *prev = cl;
3456 ns->old_cl_list = cl;
3457 cl->length = gfc_copy_expr (old_cl->length);
3458 cl->length_from_typespec = old_cl->length_from_typespec;
3459 cl->backend_decl = old_cl->backend_decl;
3460 cl->passed_length = old_cl->passed_length;
3461 cl->resolved = old_cl->resolved;
3463 else
3465 /* Put into namespace. */
3466 cl->next = ns->cl_list;
3467 ns->cl_list = cl;
3470 return cl;
3474 /* Free the charlen list from cl to end (end is not freed).
3475 Free the whole list if end is NULL. */
3477 void
3478 gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
3480 gfc_charlen *cl2;
3482 for (; cl != end; cl = cl2)
3484 gcc_assert (cl);
3486 cl2 = cl->next;
3487 gfc_free_expr (cl->length);
3488 free (cl);
3493 /* Free entry list structs. */
3495 static void
3496 free_entry_list (gfc_entry_list *el)
3498 gfc_entry_list *next;
3500 if (el == NULL)
3501 return;
3503 next = el->next;
3504 free (el);
3505 free_entry_list (next);
3509 /* Free a namespace structure and everything below it. Interface
3510 lists associated with intrinsic operators are not freed. These are
3511 taken care of when a specific name is freed. */
3513 void
3514 gfc_free_namespace (gfc_namespace *ns)
3516 gfc_namespace *p, *q;
3517 int i;
3519 if (ns == NULL)
3520 return;
3522 ns->refs--;
3523 if (ns->refs > 0)
3524 return;
3525 gcc_assert (ns->refs == 0);
3527 gfc_free_statements (ns->code);
3529 free_sym_tree (ns->sym_root);
3530 free_uop_tree (ns->uop_root);
3531 free_common_tree (ns->common_root);
3532 free_omp_udr_tree (ns->omp_udr_root);
3533 free_tb_tree (ns->tb_sym_root);
3534 free_tb_tree (ns->tb_uop_root);
3535 gfc_free_finalizer_list (ns->finalizers);
3536 gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
3537 gfc_free_charlen (ns->cl_list, NULL);
3538 free_st_labels (ns->st_labels);
3540 free_entry_list (ns->entries);
3541 gfc_free_equiv (ns->equiv);
3542 gfc_free_equiv_lists (ns->equiv_lists);
3543 gfc_free_use_stmts (ns->use_stmts);
3545 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
3546 gfc_free_interface (ns->op[i]);
3548 gfc_free_data (ns->data);
3549 p = ns->contained;
3550 free (ns);
3552 /* Recursively free any contained namespaces. */
3553 while (p != NULL)
3555 q = p;
3556 p = p->sibling;
3557 gfc_free_namespace (q);
3562 void
3563 gfc_symbol_init_2 (void)
3566 gfc_current_ns = gfc_get_namespace (NULL, 0);
3570 void
3571 gfc_symbol_done_2 (void)
3573 gfc_free_namespace (gfc_current_ns);
3574 gfc_current_ns = NULL;
3575 gfc_free_dt_list ();
3577 enforce_single_undo_checkpoint ();
3578 free_undo_change_set_data (*latest_undo_chgset);
3582 /* Count how many nodes a symtree has. */
3584 static unsigned
3585 count_st_nodes (const gfc_symtree *st)
3587 unsigned nodes;
3588 if (!st)
3589 return 0;
3591 nodes = count_st_nodes (st->left);
3592 nodes++;
3593 nodes += count_st_nodes (st->right);
3595 return nodes;
3599 /* Convert symtree tree into symtree vector. */
3601 static unsigned
3602 fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
3604 if (!st)
3605 return node_cntr;
3607 node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
3608 st_vec[node_cntr++] = st;
3609 node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
3611 return node_cntr;
3615 /* Traverse namespace. As the functions might modify the symtree, we store the
3616 symtree as a vector and operate on this vector. Note: We assume that
3617 sym_func or st_func never deletes nodes from the symtree - only adding is
3618 allowed. Additionally, newly added nodes are not traversed. */
3620 static void
3621 do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
3622 void (*sym_func) (gfc_symbol *))
3624 gfc_symtree **st_vec;
3625 unsigned nodes, i, node_cntr;
3627 gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
3628 nodes = count_st_nodes (st);
3629 st_vec = XALLOCAVEC (gfc_symtree *, nodes);
3630 node_cntr = 0;
3631 fill_st_vector (st, st_vec, node_cntr);
3633 if (sym_func)
3635 /* Clear marks. */
3636 for (i = 0; i < nodes; i++)
3637 st_vec[i]->n.sym->mark = 0;
3638 for (i = 0; i < nodes; i++)
3639 if (!st_vec[i]->n.sym->mark)
3641 (*sym_func) (st_vec[i]->n.sym);
3642 st_vec[i]->n.sym->mark = 1;
3645 else
3646 for (i = 0; i < nodes; i++)
3647 (*st_func) (st_vec[i]);
3651 /* Recursively traverse the symtree nodes. */
3653 void
3654 gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
3656 do_traverse_symtree (st, st_func, NULL);
3660 /* Call a given function for all symbols in the namespace. We take
3661 care that each gfc_symbol node is called exactly once. */
3663 void
3664 gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
3666 do_traverse_symtree (ns->sym_root, NULL, sym_func);
3670 /* Return TRUE when name is the name of an intrinsic type. */
3672 bool
3673 gfc_is_intrinsic_typename (const char *name)
3675 if (strcmp (name, "integer") == 0
3676 || strcmp (name, "real") == 0
3677 || strcmp (name, "character") == 0
3678 || strcmp (name, "logical") == 0
3679 || strcmp (name, "complex") == 0
3680 || strcmp (name, "doubleprecision") == 0
3681 || strcmp (name, "doublecomplex") == 0)
3682 return true;
3683 else
3684 return false;
3688 /* Return TRUE if the symbol is an automatic variable. */
3690 static bool
3691 gfc_is_var_automatic (gfc_symbol *sym)
3693 /* Pointer and allocatable variables are never automatic. */
3694 if (sym->attr.pointer || sym->attr.allocatable)
3695 return false;
3696 /* Check for arrays with non-constant size. */
3697 if (sym->attr.dimension && sym->as
3698 && !gfc_is_compile_time_shape (sym->as))
3699 return true;
3700 /* Check for non-constant length character variables. */
3701 if (sym->ts.type == BT_CHARACTER
3702 && sym->ts.u.cl
3703 && !gfc_is_constant_expr (sym->ts.u.cl->length))
3704 return true;
3705 return false;
3708 /* Given a symbol, mark it as SAVEd if it is allowed. */
3710 static void
3711 save_symbol (gfc_symbol *sym)
3714 if (sym->attr.use_assoc)
3715 return;
3717 if (sym->attr.in_common
3718 || sym->attr.dummy
3719 || sym->attr.result
3720 || sym->attr.flavor != FL_VARIABLE)
3721 return;
3722 /* Automatic objects are not saved. */
3723 if (gfc_is_var_automatic (sym))
3724 return;
3725 gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
3729 /* Mark those symbols which can be SAVEd as such. */
3731 void
3732 gfc_save_all (gfc_namespace *ns)
3734 gfc_traverse_ns (ns, save_symbol);
3738 /* Make sure that no changes to symbols are pending. */
3740 void
3741 gfc_enforce_clean_symbol_state(void)
3743 enforce_single_undo_checkpoint ();
3744 gcc_assert (latest_undo_chgset->syms.is_empty ());
3748 /************** Global symbol handling ************/
3751 /* Search a tree for the global symbol. */
3753 gfc_gsymbol *
3754 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
3756 int c;
3758 if (symbol == NULL)
3759 return NULL;
3761 while (symbol)
3763 c = strcmp (name, symbol->name);
3764 if (!c)
3765 return symbol;
3767 symbol = (c < 0) ? symbol->left : symbol->right;
3770 return NULL;
3774 /* Compare two global symbols. Used for managing the BB tree. */
3776 static int
3777 gsym_compare (void *_s1, void *_s2)
3779 gfc_gsymbol *s1, *s2;
3781 s1 = (gfc_gsymbol *) _s1;
3782 s2 = (gfc_gsymbol *) _s2;
3783 return strcmp (s1->name, s2->name);
3787 /* Get a global symbol, creating it if it doesn't exist. */
3789 gfc_gsymbol *
3790 gfc_get_gsymbol (const char *name)
3792 gfc_gsymbol *s;
3794 s = gfc_find_gsymbol (gfc_gsym_root, name);
3795 if (s != NULL)
3796 return s;
3798 s = XCNEW (gfc_gsymbol);
3799 s->type = GSYM_UNKNOWN;
3800 s->name = gfc_get_string (name);
3802 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
3804 return s;
3808 static gfc_symbol *
3809 get_iso_c_binding_dt (int sym_id)
3811 gfc_dt_list *dt_list;
3813 dt_list = gfc_derived_types;
3815 /* Loop through the derived types in the name list, searching for
3816 the desired symbol from iso_c_binding. Search the parent namespaces
3817 if necessary and requested to (parent_flag). */
3818 while (dt_list != NULL)
3820 if (dt_list->derived->from_intmod != INTMOD_NONE
3821 && dt_list->derived->intmod_sym_id == sym_id)
3822 return dt_list->derived;
3824 dt_list = dt_list->next;
3827 return NULL;
3831 /* Verifies that the given derived type symbol, derived_sym, is interoperable
3832 with C. This is necessary for any derived type that is BIND(C) and for
3833 derived types that are parameters to functions that are BIND(C). All
3834 fields of the derived type are required to be interoperable, and are tested
3835 for such. If an error occurs, the errors are reported here, allowing for
3836 multiple errors to be handled for a single derived type. */
3838 bool
3839 verify_bind_c_derived_type (gfc_symbol *derived_sym)
3841 gfc_component *curr_comp = NULL;
3842 bool is_c_interop = false;
3843 bool retval = true;
3845 if (derived_sym == NULL)
3846 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
3847 "unexpectedly NULL");
3849 /* If we've already looked at this derived symbol, do not look at it again
3850 so we don't repeat warnings/errors. */
3851 if (derived_sym->ts.is_c_interop)
3852 return true;
3854 /* The derived type must have the BIND attribute to be interoperable
3855 J3/04-007, Section 15.2.3. */
3856 if (derived_sym->attr.is_bind_c != 1)
3858 derived_sym->ts.is_c_interop = 0;
3859 gfc_error_now ("Derived type %qs declared at %L must have the BIND "
3860 "attribute to be C interoperable", derived_sym->name,
3861 &(derived_sym->declared_at));
3862 retval = false;
3865 curr_comp = derived_sym->components;
3867 /* Fortran 2003 allows an empty derived type. C99 appears to disallow an
3868 empty struct. Section 15.2 in Fortran 2003 states: "The following
3869 subclauses define the conditions under which a Fortran entity is
3870 interoperable. If a Fortran entity is interoperable, an equivalent
3871 entity may be defined by means of C and the Fortran entity is said
3872 to be interoperable with the C entity. There does not have to be such
3873 an interoperating C entity."
3875 if (curr_comp == NULL)
3877 gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L is empty, "
3878 "and may be inaccessible by the C companion processor",
3879 derived_sym->name, &(derived_sym->declared_at));
3880 derived_sym->ts.is_c_interop = 1;
3881 derived_sym->attr.is_bind_c = 1;
3882 return true;
3886 /* Initialize the derived type as being C interoperable.
3887 If we find an error in the components, this will be set false. */
3888 derived_sym->ts.is_c_interop = 1;
3890 /* Loop through the list of components to verify that the kind of
3891 each is a C interoperable type. */
3894 /* The components cannot be pointers (fortran sense).
3895 J3/04-007, Section 15.2.3, C1505. */
3896 if (curr_comp->attr.pointer != 0)
3898 gfc_error_1 ("Component '%s' at %L cannot have the "
3899 "POINTER attribute because it is a member "
3900 "of the BIND(C) derived type '%s' at %L",
3901 curr_comp->name, &(curr_comp->loc),
3902 derived_sym->name, &(derived_sym->declared_at));
3903 retval = false;
3906 if (curr_comp->attr.proc_pointer != 0)
3908 gfc_error_1 ("Procedure pointer component '%s' at %L cannot be a member"
3909 " of the BIND(C) derived type '%s' at %L", curr_comp->name,
3910 &curr_comp->loc, derived_sym->name,
3911 &derived_sym->declared_at);
3912 retval = false;
3915 /* The components cannot be allocatable.
3916 J3/04-007, Section 15.2.3, C1505. */
3917 if (curr_comp->attr.allocatable != 0)
3919 gfc_error_1 ("Component '%s' at %L cannot have the "
3920 "ALLOCATABLE attribute because it is a member "
3921 "of the BIND(C) derived type '%s' at %L",
3922 curr_comp->name, &(curr_comp->loc),
3923 derived_sym->name, &(derived_sym->declared_at));
3924 retval = false;
3927 /* BIND(C) derived types must have interoperable components. */
3928 if (curr_comp->ts.type == BT_DERIVED
3929 && curr_comp->ts.u.derived->ts.is_iso_c != 1
3930 && curr_comp->ts.u.derived != derived_sym)
3932 /* This should be allowed; the draft says a derived-type can not
3933 have type parameters if it is has the BIND attribute. Type
3934 parameters seem to be for making parameterized derived types.
3935 There's no need to verify the type if it is c_ptr/c_funptr. */
3936 retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
3938 else
3940 /* Grab the typespec for the given component and test the kind. */
3941 is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
3943 if (!is_c_interop)
3945 /* Report warning and continue since not fatal. The
3946 draft does specify a constraint that requires all fields
3947 to interoperate, but if the user says real(4), etc., it
3948 may interoperate with *something* in C, but the compiler
3949 most likely won't know exactly what. Further, it may not
3950 interoperate with the same data type(s) in C if the user
3951 recompiles with different flags (e.g., -m32 and -m64 on
3952 x86_64 and using integer(4) to claim interop with a
3953 C_LONG). */
3954 if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
3955 /* If the derived type is bind(c), all fields must be
3956 interop. */
3957 gfc_warning (OPT_Wc_binding_type,
3958 "Component %qs in derived type %qs at %L "
3959 "may not be C interoperable, even though "
3960 "derived type %qs is BIND(C)",
3961 curr_comp->name, derived_sym->name,
3962 &(curr_comp->loc), derived_sym->name);
3963 else if (warn_c_binding_type)
3964 /* If derived type is param to bind(c) routine, or to one
3965 of the iso_c_binding procs, it must be interoperable, so
3966 all fields must interop too. */
3967 gfc_warning (OPT_Wc_binding_type,
3968 "Component %qs in derived type %qs at %L "
3969 "may not be C interoperable",
3970 curr_comp->name, derived_sym->name,
3971 &(curr_comp->loc));
3975 curr_comp = curr_comp->next;
3976 } while (curr_comp != NULL);
3979 /* Make sure we don't have conflicts with the attributes. */
3980 if (derived_sym->attr.access == ACCESS_PRIVATE)
3982 gfc_error ("Derived type %qs at %L cannot be declared with both "
3983 "PRIVATE and BIND(C) attributes", derived_sym->name,
3984 &(derived_sym->declared_at));
3985 retval = false;
3988 if (derived_sym->attr.sequence != 0)
3990 gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
3991 "attribute because it is BIND(C)", derived_sym->name,
3992 &(derived_sym->declared_at));
3993 retval = false;
3996 /* Mark the derived type as not being C interoperable if we found an
3997 error. If there were only warnings, proceed with the assumption
3998 it's interoperable. */
3999 if (!retval)
4000 derived_sym->ts.is_c_interop = 0;
4002 return retval;
4006 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
4008 static bool
4009 gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
4011 gfc_constructor *c;
4013 gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
4014 dt_symtree->n.sym->attr.referenced = 1;
4016 tmp_sym->attr.is_c_interop = 1;
4017 tmp_sym->attr.is_bind_c = 1;
4018 tmp_sym->ts.is_c_interop = 1;
4019 tmp_sym->ts.is_iso_c = 1;
4020 tmp_sym->ts.type = BT_DERIVED;
4021 tmp_sym->ts.f90_type = BT_VOID;
4022 tmp_sym->attr.flavor = FL_PARAMETER;
4023 tmp_sym->ts.u.derived = dt_symtree->n.sym;
4025 /* Set the c_address field of c_null_ptr and c_null_funptr to
4026 the value of NULL. */
4027 tmp_sym->value = gfc_get_expr ();
4028 tmp_sym->value->expr_type = EXPR_STRUCTURE;
4029 tmp_sym->value->ts.type = BT_DERIVED;
4030 tmp_sym->value->ts.f90_type = BT_VOID;
4031 tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
4032 gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
4033 c = gfc_constructor_first (tmp_sym->value->value.constructor);
4034 c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
4035 c->expr->ts.is_iso_c = 1;
4037 return true;
4041 /* Add a formal argument, gfc_formal_arglist, to the
4042 end of the given list of arguments. Set the reference to the
4043 provided symbol, param_sym, in the argument. */
4045 static void
4046 add_formal_arg (gfc_formal_arglist **head,
4047 gfc_formal_arglist **tail,
4048 gfc_formal_arglist *formal_arg,
4049 gfc_symbol *param_sym)
4051 /* Put in list, either as first arg or at the tail (curr arg). */
4052 if (*head == NULL)
4053 *head = *tail = formal_arg;
4054 else
4056 (*tail)->next = formal_arg;
4057 (*tail) = formal_arg;
4060 (*tail)->sym = param_sym;
4061 (*tail)->next = NULL;
4063 return;
4067 /* Add a procedure interface to the given symbol (i.e., store a
4068 reference to the list of formal arguments). */
4070 static void
4071 add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
4074 sym->formal = formal;
4075 sym->attr.if_source = source;
4079 /* Copy the formal args from an existing symbol, src, into a new
4080 symbol, dest. New formal args are created, and the description of
4081 each arg is set according to the existing ones. This function is
4082 used when creating procedure declaration variables from a procedure
4083 declaration statement (see match_proc_decl()) to create the formal
4084 args based on the args of a given named interface.
4086 When an actual argument list is provided, skip the absent arguments.
4087 To be used together with gfc_se->ignore_optional. */
4089 void
4090 gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
4091 gfc_actual_arglist *actual)
4093 gfc_formal_arglist *head = NULL;
4094 gfc_formal_arglist *tail = NULL;
4095 gfc_formal_arglist *formal_arg = NULL;
4096 gfc_intrinsic_arg *curr_arg = NULL;
4097 gfc_formal_arglist *formal_prev = NULL;
4098 gfc_actual_arglist *act_arg = actual;
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 gfc_current_ns->proc_name = dest;
4107 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4109 /* Skip absent arguments. */
4110 if (actual)
4112 gcc_assert (act_arg != NULL);
4113 if (act_arg->expr == NULL)
4115 act_arg = act_arg->next;
4116 continue;
4118 act_arg = act_arg->next;
4120 formal_arg = gfc_get_formal_arglist ();
4121 gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
4123 /* May need to copy more info for the symbol. */
4124 formal_arg->sym->ts = curr_arg->ts;
4125 formal_arg->sym->attr.optional = curr_arg->optional;
4126 formal_arg->sym->attr.value = curr_arg->value;
4127 formal_arg->sym->attr.intent = curr_arg->intent;
4128 formal_arg->sym->attr.flavor = FL_VARIABLE;
4129 formal_arg->sym->attr.dummy = 1;
4131 if (formal_arg->sym->ts.type == BT_CHARACTER)
4132 formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4134 /* If this isn't the first arg, set up the next ptr. For the
4135 last arg built, the formal_arg->next will never get set to
4136 anything other than NULL. */
4137 if (formal_prev != NULL)
4138 formal_prev->next = formal_arg;
4139 else
4140 formal_arg->next = NULL;
4142 formal_prev = formal_arg;
4144 /* Add arg to list of formal args. */
4145 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4147 /* Validate changes. */
4148 gfc_commit_symbol (formal_arg->sym);
4151 /* Add the interface to the symbol. */
4152 add_proc_interface (dest, IFSRC_DECL, head);
4154 /* Store the formal namespace information. */
4155 if (dest->formal != NULL)
4156 /* The current ns should be that for the dest proc. */
4157 dest->formal_ns = gfc_current_ns;
4158 /* Restore the current namespace to what it was on entry. */
4159 gfc_current_ns = parent_ns;
4163 static int
4164 std_for_isocbinding_symbol (int id)
4166 switch (id)
4168 #define NAMED_INTCST(a,b,c,d) \
4169 case a:\
4170 return d;
4171 #include "iso-c-binding.def"
4172 #undef NAMED_INTCST
4174 #define NAMED_FUNCTION(a,b,c,d) \
4175 case a:\
4176 return d;
4177 #define NAMED_SUBROUTINE(a,b,c,d) \
4178 case a:\
4179 return d;
4180 #include "iso-c-binding.def"
4181 #undef NAMED_FUNCTION
4182 #undef NAMED_SUBROUTINE
4184 default:
4185 return GFC_STD_F2003;
4189 /* Generate the given set of C interoperable kind objects, or all
4190 interoperable kinds. This function will only be given kind objects
4191 for valid iso_c_binding defined types because this is verified when
4192 the 'use' statement is parsed. If the user gives an 'only' clause,
4193 the specific kinds are looked up; if they don't exist, an error is
4194 reported. If the user does not give an 'only' clause, all
4195 iso_c_binding symbols are generated. If a list of specific kinds
4196 is given, it must have a NULL in the first empty spot to mark the
4197 end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
4198 point to the symtree for c_(fun)ptr. */
4200 gfc_symtree *
4201 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
4202 const char *local_name, gfc_symtree *dt_symtree,
4203 bool hidden)
4205 const char *const name = (local_name && local_name[0])
4206 ? local_name : c_interop_kinds_table[s].name;
4207 gfc_symtree *tmp_symtree;
4208 gfc_symbol *tmp_sym = NULL;
4209 int index;
4211 if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
4212 return NULL;
4214 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
4215 if (hidden
4216 && (!tmp_symtree || !tmp_symtree->n.sym
4217 || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
4218 || tmp_symtree->n.sym->intmod_sym_id != s))
4219 tmp_symtree = NULL;
4221 /* Already exists in this scope so don't re-add it. */
4222 if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
4223 && (!tmp_sym->attr.generic
4224 || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
4225 && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
4227 if (tmp_sym->attr.flavor == FL_DERIVED
4228 && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
4230 gfc_dt_list *dt_list;
4231 dt_list = gfc_get_dt_list ();
4232 dt_list->derived = tmp_sym;
4233 dt_list->next = gfc_derived_types;
4234 gfc_derived_types = dt_list;
4237 return tmp_symtree;
4240 /* Create the sym tree in the current ns. */
4241 if (hidden)
4243 tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
4244 tmp_sym = gfc_new_symbol (name, gfc_current_ns);
4246 /* Add to the list of tentative symbols. */
4247 latest_undo_chgset->syms.safe_push (tmp_sym);
4248 tmp_sym->old_symbol = NULL;
4249 tmp_sym->mark = 1;
4250 tmp_sym->gfc_new = 1;
4252 tmp_symtree->n.sym = tmp_sym;
4253 tmp_sym->refs++;
4255 else
4257 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
4258 gcc_assert (tmp_symtree);
4259 tmp_sym = tmp_symtree->n.sym;
4262 /* Say what module this symbol belongs to. */
4263 tmp_sym->module = gfc_get_string (mod_name);
4264 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
4265 tmp_sym->intmod_sym_id = s;
4266 tmp_sym->attr.is_iso_c = 1;
4267 tmp_sym->attr.use_assoc = 1;
4269 gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
4270 || s == ISOCBINDING_NULL_PTR);
4272 switch (s)
4275 #define NAMED_INTCST(a,b,c,d) case a :
4276 #define NAMED_REALCST(a,b,c,d) case a :
4277 #define NAMED_CMPXCST(a,b,c,d) case a :
4278 #define NAMED_LOGCST(a,b,c) case a :
4279 #define NAMED_CHARKNDCST(a,b,c) case a :
4280 #include "iso-c-binding.def"
4282 tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4283 c_interop_kinds_table[s].value);
4285 /* Initialize an integer constant expression node. */
4286 tmp_sym->attr.flavor = FL_PARAMETER;
4287 tmp_sym->ts.type = BT_INTEGER;
4288 tmp_sym->ts.kind = gfc_default_integer_kind;
4290 /* Mark this type as a C interoperable one. */
4291 tmp_sym->ts.is_c_interop = 1;
4292 tmp_sym->ts.is_iso_c = 1;
4293 tmp_sym->value->ts.is_c_interop = 1;
4294 tmp_sym->value->ts.is_iso_c = 1;
4295 tmp_sym->attr.is_c_interop = 1;
4297 /* Tell what f90 type this c interop kind is valid. */
4298 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
4300 break;
4303 #define NAMED_CHARCST(a,b,c) case a :
4304 #include "iso-c-binding.def"
4306 /* Initialize an integer constant expression node for the
4307 length of the character. */
4308 tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
4309 &gfc_current_locus, NULL, 1);
4310 tmp_sym->value->ts.is_c_interop = 1;
4311 tmp_sym->value->ts.is_iso_c = 1;
4312 tmp_sym->value->value.character.length = 1;
4313 tmp_sym->value->value.character.string[0]
4314 = (gfc_char_t) c_interop_kinds_table[s].value;
4315 tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4316 tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
4317 NULL, 1);
4319 /* May not need this in both attr and ts, but do need in
4320 attr for writing module file. */
4321 tmp_sym->attr.is_c_interop = 1;
4323 tmp_sym->attr.flavor = FL_PARAMETER;
4324 tmp_sym->ts.type = BT_CHARACTER;
4326 /* Need to set it to the C_CHAR kind. */
4327 tmp_sym->ts.kind = gfc_default_character_kind;
4329 /* Mark this type as a C interoperable one. */
4330 tmp_sym->ts.is_c_interop = 1;
4331 tmp_sym->ts.is_iso_c = 1;
4333 /* Tell what f90 type this c interop kind is valid. */
4334 tmp_sym->ts.f90_type = BT_CHARACTER;
4336 break;
4338 case ISOCBINDING_PTR:
4339 case ISOCBINDING_FUNPTR:
4341 gfc_symbol *dt_sym;
4342 gfc_dt_list **dt_list_ptr = NULL;
4343 gfc_component *tmp_comp = NULL;
4345 /* Generate real derived type. */
4346 if (hidden)
4347 dt_sym = tmp_sym;
4348 else
4350 const char *hidden_name;
4351 gfc_interface *intr, *head;
4353 hidden_name = gfc_get_string ("%c%s",
4354 (char) TOUPPER ((unsigned char)
4355 tmp_sym->name[0]),
4356 &tmp_sym->name[1]);
4357 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
4358 hidden_name);
4359 gcc_assert (tmp_symtree == NULL);
4360 gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
4361 dt_sym = tmp_symtree->n.sym;
4362 dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
4363 ? "c_ptr" : "c_funptr");
4365 /* Generate an artificial generic function. */
4366 head = tmp_sym->generic;
4367 intr = gfc_get_interface ();
4368 intr->sym = dt_sym;
4369 intr->where = gfc_current_locus;
4370 intr->next = head;
4371 tmp_sym->generic = intr;
4373 if (!tmp_sym->attr.generic
4374 && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
4375 return NULL;
4377 if (!tmp_sym->attr.function
4378 && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
4379 return NULL;
4382 /* Say what module this symbol belongs to. */
4383 dt_sym->module = gfc_get_string (mod_name);
4384 dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
4385 dt_sym->intmod_sym_id = s;
4386 dt_sym->attr.use_assoc = 1;
4388 /* Initialize an integer constant expression node. */
4389 dt_sym->attr.flavor = FL_DERIVED;
4390 dt_sym->ts.is_c_interop = 1;
4391 dt_sym->attr.is_c_interop = 1;
4392 dt_sym->attr.private_comp = 1;
4393 dt_sym->component_access = ACCESS_PRIVATE;
4394 dt_sym->ts.is_iso_c = 1;
4395 dt_sym->ts.type = BT_DERIVED;
4396 dt_sym->ts.f90_type = BT_VOID;
4398 /* A derived type must have the bind attribute to be
4399 interoperable (J3/04-007, Section 15.2.3), even though
4400 the binding label is not used. */
4401 dt_sym->attr.is_bind_c = 1;
4403 dt_sym->attr.referenced = 1;
4404 dt_sym->ts.u.derived = dt_sym;
4406 /* Add the symbol created for the derived type to the current ns. */
4407 dt_list_ptr = &(gfc_derived_types);
4408 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
4409 dt_list_ptr = &((*dt_list_ptr)->next);
4411 /* There is already at least one derived type in the list, so append
4412 the one we're currently building for c_ptr or c_funptr. */
4413 if (*dt_list_ptr != NULL)
4414 dt_list_ptr = &((*dt_list_ptr)->next);
4415 (*dt_list_ptr) = gfc_get_dt_list ();
4416 (*dt_list_ptr)->derived = dt_sym;
4417 (*dt_list_ptr)->next = NULL;
4419 gfc_add_component (dt_sym, "c_address", &tmp_comp);
4420 if (tmp_comp == NULL)
4421 gcc_unreachable ();
4423 tmp_comp->ts.type = BT_INTEGER;
4425 /* Set this because the module will need to read/write this field. */
4426 tmp_comp->ts.f90_type = BT_INTEGER;
4428 /* The kinds for c_ptr and c_funptr are the same. */
4429 index = get_c_kind ("c_ptr", c_interop_kinds_table);
4430 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
4431 tmp_comp->attr.access = ACCESS_PRIVATE;
4433 /* Mark the component as C interoperable. */
4434 tmp_comp->ts.is_c_interop = 1;
4437 break;
4439 case ISOCBINDING_NULL_PTR:
4440 case ISOCBINDING_NULL_FUNPTR:
4441 gen_special_c_interop_ptr (tmp_sym, dt_symtree);
4442 break;
4444 default:
4445 gcc_unreachable ();
4447 gfc_commit_symbol (tmp_sym);
4448 return tmp_symtree;
4452 /* Check that a symbol is already typed. If strict is not set, an untyped
4453 symbol is acceptable for non-standard-conforming mode. */
4455 bool
4456 gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
4457 bool strict, locus where)
4459 gcc_assert (sym);
4461 if (gfc_matching_prefix)
4462 return true;
4464 /* Check for the type and try to give it an implicit one. */
4465 if (sym->ts.type == BT_UNKNOWN
4466 && !gfc_set_default_type (sym, 0, ns))
4468 if (strict)
4470 gfc_error ("Symbol %qs is used before it is typed at %L",
4471 sym->name, &where);
4472 return false;
4475 if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
4476 " it is typed at %L", sym->name, &where))
4477 return false;
4480 /* Everything is ok. */
4481 return true;
4485 /* Construct a typebound-procedure structure. Those are stored in a tentative
4486 list and marked `error' until symbols are committed. */
4488 gfc_typebound_proc*
4489 gfc_get_typebound_proc (gfc_typebound_proc *tb0)
4491 gfc_typebound_proc *result;
4493 result = XCNEW (gfc_typebound_proc);
4494 if (tb0)
4495 *result = *tb0;
4496 result->error = 1;
4498 latest_undo_chgset->tbps.safe_push (result);
4500 return result;
4504 /* Get the super-type of a given derived type. */
4506 gfc_symbol*
4507 gfc_get_derived_super_type (gfc_symbol* derived)
4509 gcc_assert (derived);
4511 if (derived->attr.generic)
4512 derived = gfc_find_dt_in_generic (derived);
4514 if (!derived->attr.extension)
4515 return NULL;
4517 gcc_assert (derived->components);
4518 gcc_assert (derived->components->ts.type == BT_DERIVED);
4519 gcc_assert (derived->components->ts.u.derived);
4521 if (derived->components->ts.u.derived->attr.generic)
4522 return gfc_find_dt_in_generic (derived->components->ts.u.derived);
4524 return derived->components->ts.u.derived;
4528 /* Get the ultimate super-type of a given derived type. */
4530 gfc_symbol*
4531 gfc_get_ultimate_derived_super_type (gfc_symbol* derived)
4533 if (!derived->attr.extension)
4534 return NULL;
4536 derived = gfc_get_derived_super_type (derived);
4538 if (derived->attr.extension)
4539 return gfc_get_ultimate_derived_super_type (derived);
4540 else
4541 return derived;
4545 /* Check if a derived type t2 is an extension of (or equal to) a type t1. */
4547 bool
4548 gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
4550 while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
4551 t2 = gfc_get_derived_super_type (t2);
4552 return gfc_compare_derived_types (t1, t2);
4556 /* Check if two typespecs are type compatible (F03:5.1.1.2):
4557 If ts1 is nonpolymorphic, ts2 must be the same type.
4558 If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1. */
4560 bool
4561 gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
4563 bool is_class1 = (ts1->type == BT_CLASS);
4564 bool is_class2 = (ts2->type == BT_CLASS);
4565 bool is_derived1 = (ts1->type == BT_DERIVED);
4566 bool is_derived2 = (ts2->type == BT_DERIVED);
4568 if (is_class1
4569 && ts1->u.derived->components
4570 && ((ts1->u.derived->attr.is_class
4571 && ts1->u.derived->components->ts.u.derived->attr
4572 .unlimited_polymorphic)
4573 || ts1->u.derived->attr.unlimited_polymorphic))
4574 return 1;
4576 if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2)
4577 return (ts1->type == ts2->type);
4579 if (is_derived1 && is_derived2)
4580 return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
4582 if (is_derived1 && is_class2)
4583 return gfc_compare_derived_types (ts1->u.derived,
4584 ts2->u.derived->attr.is_class ?
4585 ts2->u.derived->components->ts.u.derived
4586 : ts2->u.derived);
4587 if (is_class1 && is_derived2)
4588 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
4589 ts1->u.derived->components->ts.u.derived
4590 : ts1->u.derived,
4591 ts2->u.derived);
4592 else if (is_class1 && is_class2)
4593 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
4594 ts1->u.derived->components->ts.u.derived
4595 : ts1->u.derived,
4596 ts2->u.derived->attr.is_class ?
4597 ts2->u.derived->components->ts.u.derived
4598 : ts2->u.derived);
4599 else
4600 return 0;
4604 /* Find the parent-namespace of the current function. If we're inside
4605 BLOCK constructs, it may not be the current one. */
4607 gfc_namespace*
4608 gfc_find_proc_namespace (gfc_namespace* ns)
4610 while (ns->construct_entities)
4612 ns = ns->parent;
4613 gcc_assert (ns);
4616 return ns;
4620 /* Check if an associate-variable should be translated as an `implicit' pointer
4621 internally (if it is associated to a variable and not an array with
4622 descriptor). */
4624 bool
4625 gfc_is_associate_pointer (gfc_symbol* sym)
4627 if (!sym->assoc)
4628 return false;
4630 if (sym->ts.type == BT_CLASS)
4631 return true;
4633 if (!sym->assoc->variable)
4634 return false;
4636 if (sym->attr.dimension && sym->as->type != AS_EXPLICIT)
4637 return false;
4639 return true;
4643 gfc_symbol *
4644 gfc_find_dt_in_generic (gfc_symbol *sym)
4646 gfc_interface *intr = NULL;
4648 if (!sym || sym->attr.flavor == FL_DERIVED)
4649 return sym;
4651 if (sym->attr.generic)
4652 for (intr = sym->generic; intr; intr = intr->next)
4653 if (intr->sym->attr.flavor == FL_DERIVED)
4654 break;
4655 return intr ? intr->sym : NULL;
4659 /* Get the dummy arguments from a procedure symbol. If it has been declared
4660 via a PROCEDURE statement with a named interface, ts.interface will be set
4661 and the arguments need to be taken from there. */
4663 gfc_formal_arglist *
4664 gfc_sym_get_dummy_args (gfc_symbol *sym)
4666 gfc_formal_arglist *dummies;
4668 dummies = sym->formal;
4669 if (dummies == NULL && sym->ts.interface != NULL)
4670 dummies = sym->ts.interface->formal;
4672 return dummies;