pr88074.c: Require c99_runtime.
[official-gcc.git] / gcc / fortran / symbol.c
blobc342d62ead1b0ef59762c1b8cf8d1f51867078f1
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000-2019 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 "options.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 ("UNION", FL_UNION), minit ("STRUCTURE", FL_STRUCT),
44 minit (NULL, -1)
47 const mstring procedures[] =
49 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
50 minit ("MODULE-PROC", PROC_MODULE),
51 minit ("INTERNAL-PROC", PROC_INTERNAL),
52 minit ("DUMMY-PROC", PROC_DUMMY),
53 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
54 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
55 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
56 minit (NULL, -1)
59 const mstring intents[] =
61 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
62 minit ("IN", INTENT_IN),
63 minit ("OUT", INTENT_OUT),
64 minit ("INOUT", INTENT_INOUT),
65 minit (NULL, -1)
68 const mstring access_types[] =
70 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
71 minit ("PUBLIC", ACCESS_PUBLIC),
72 minit ("PRIVATE", ACCESS_PRIVATE),
73 minit (NULL, -1)
76 const mstring ifsrc_types[] =
78 minit ("UNKNOWN", IFSRC_UNKNOWN),
79 minit ("DECL", IFSRC_DECL),
80 minit ("BODY", IFSRC_IFBODY)
83 const mstring save_status[] =
85 minit ("UNKNOWN", SAVE_NONE),
86 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
87 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
90 /* Set the mstrings for DTIO procedure names. */
91 const mstring dtio_procs[] =
93 minit ("_dtio_formatted_read", DTIO_RF),
94 minit ("_dtio_formatted_write", DTIO_WF),
95 minit ("_dtio_unformatted_read", DTIO_RUF),
96 minit ("_dtio_unformatted_write", DTIO_WUF),
99 /* This is to make sure the backend generates setup code in the correct
100 order. */
102 static int next_dummy_order = 1;
105 gfc_namespace *gfc_current_ns;
106 gfc_namespace *gfc_global_ns_list;
108 gfc_gsymbol *gfc_gsym_root = NULL;
110 gfc_symbol *gfc_derived_types;
112 static gfc_undo_change_set default_undo_chgset_var = { vNULL, vNULL, NULL };
113 static gfc_undo_change_set *latest_undo_chgset = &default_undo_chgset_var;
116 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
118 /* The following static variable indicates whether a particular element has
119 been explicitly set or not. */
121 static int new_flag[GFC_LETTERS];
124 /* Handle a correctly parsed IMPLICIT NONE. */
126 void
127 gfc_set_implicit_none (bool type, bool external, locus *loc)
129 int i;
131 if (external)
132 gfc_current_ns->has_implicit_none_export = 1;
134 if (type)
136 gfc_current_ns->seen_implicit_none = 1;
137 for (i = 0; i < GFC_LETTERS; i++)
139 if (gfc_current_ns->set_flag[i])
141 gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
142 "IMPLICIT statement", loc);
143 return;
145 gfc_clear_ts (&gfc_current_ns->default_type[i]);
146 gfc_current_ns->set_flag[i] = 1;
152 /* Reset the implicit range flags. */
154 void
155 gfc_clear_new_implicit (void)
157 int i;
159 for (i = 0; i < GFC_LETTERS; i++)
160 new_flag[i] = 0;
164 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
166 bool
167 gfc_add_new_implicit_range (int c1, int c2)
169 int i;
171 c1 -= 'a';
172 c2 -= 'a';
174 for (i = c1; i <= c2; i++)
176 if (new_flag[i])
178 gfc_error ("Letter %qc already set in IMPLICIT statement at %C",
179 i + 'A');
180 return false;
183 new_flag[i] = 1;
186 return true;
190 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
191 the new implicit types back into the existing types will work. */
193 bool
194 gfc_merge_new_implicit (gfc_typespec *ts)
196 int i;
198 if (gfc_current_ns->seen_implicit_none)
200 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
201 return false;
204 for (i = 0; i < GFC_LETTERS; i++)
206 if (new_flag[i])
208 if (gfc_current_ns->set_flag[i])
210 gfc_error ("Letter %qc already has an IMPLICIT type at %C",
211 i + 'A');
212 return false;
215 gfc_current_ns->default_type[i] = *ts;
216 gfc_current_ns->implicit_loc[i] = gfc_current_locus;
217 gfc_current_ns->set_flag[i] = 1;
220 return true;
224 /* Given a symbol, return a pointer to the typespec for its default type. */
226 gfc_typespec *
227 gfc_get_default_type (const char *name, gfc_namespace *ns)
229 char letter;
231 letter = name[0];
233 if (flag_allow_leading_underscore && letter == '_')
234 gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
235 "gfortran developers, and should not be used for "
236 "implicitly typed variables");
238 if (letter < 'a' || letter > 'z')
239 gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
241 if (ns == NULL)
242 ns = gfc_current_ns;
244 return &ns->default_type[letter - 'a'];
248 /* Recursively append candidate SYM to CANDIDATES. Store the number of
249 candidates in CANDIDATES_LEN. */
251 static void
252 lookup_symbol_fuzzy_find_candidates (gfc_symtree *sym,
253 char **&candidates,
254 size_t &candidates_len)
256 gfc_symtree *p;
258 if (sym == NULL)
259 return;
261 if (sym->n.sym->ts.type != BT_UNKNOWN && sym->n.sym->ts.type != BT_PROCEDURE)
262 vec_push (candidates, candidates_len, sym->name);
263 p = sym->left;
264 if (p)
265 lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
267 p = sym->right;
268 if (p)
269 lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
273 /* Lookup symbol SYM_NAME fuzzily, taking names in SYMBOL into account. */
275 static const char*
276 lookup_symbol_fuzzy (const char *sym_name, gfc_symbol *symbol)
278 char **candidates = NULL;
279 size_t candidates_len = 0;
280 lookup_symbol_fuzzy_find_candidates (symbol->ns->sym_root, candidates,
281 candidates_len);
282 return gfc_closest_fuzzy_match (sym_name, candidates);
286 /* Given a pointer to a symbol, set its type according to the first
287 letter of its name. Fails if the letter in question has no default
288 type. */
290 bool
291 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
293 gfc_typespec *ts;
295 if (sym->ts.type != BT_UNKNOWN)
296 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
298 ts = gfc_get_default_type (sym->name, ns);
300 if (ts->type == BT_UNKNOWN)
302 if (error_flag && !sym->attr.untyped)
304 const char *guessed = lookup_symbol_fuzzy (sym->name, sym);
305 if (guessed)
306 gfc_error ("Symbol %qs at %L has no IMPLICIT type"
307 "; did you mean %qs?",
308 sym->name, &sym->declared_at, guessed);
309 else
310 gfc_error ("Symbol %qs at %L has no IMPLICIT type",
311 sym->name, &sym->declared_at);
312 sym->attr.untyped = 1; /* Ensure we only give an error once. */
315 return false;
318 sym->ts = *ts;
319 sym->attr.implicit_type = 1;
321 if (ts->type == BT_CHARACTER && ts->u.cl)
322 sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
323 else if (ts->type == BT_CLASS
324 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
325 return false;
327 if (sym->attr.is_bind_c == 1 && warn_c_binding_type)
329 /* BIND(C) variables should not be implicitly declared. */
330 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared BIND(C) "
331 "variable %qs at %L may not be C interoperable",
332 sym->name, &sym->declared_at);
333 sym->ts.f90_type = sym->ts.type;
336 if (sym->attr.dummy != 0)
338 if (sym->ns->proc_name != NULL
339 && (sym->ns->proc_name->attr.subroutine != 0
340 || sym->ns->proc_name->attr.function != 0)
341 && sym->ns->proc_name->attr.is_bind_c != 0
342 && warn_c_binding_type)
344 /* Dummy args to a BIND(C) routine may not be interoperable if
345 they are implicitly typed. */
346 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared variable "
347 "%qs at %L may not be C interoperable but it is a "
348 "dummy argument to the BIND(C) procedure %qs at %L",
349 sym->name, &(sym->declared_at),
350 sym->ns->proc_name->name,
351 &(sym->ns->proc_name->declared_at));
352 sym->ts.f90_type = sym->ts.type;
356 return true;
360 /* This function is called from parse.c(parse_progunit) to check the
361 type of the function is not implicitly typed in the host namespace
362 and to implicitly type the function result, if necessary. */
364 void
365 gfc_check_function_type (gfc_namespace *ns)
367 gfc_symbol *proc = ns->proc_name;
369 if (!proc->attr.contained || proc->result->attr.implicit_type)
370 return;
372 if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
374 if (gfc_set_default_type (proc->result, 0, gfc_current_ns))
376 if (proc->result != proc)
378 proc->ts = proc->result->ts;
379 proc->as = gfc_copy_array_spec (proc->result->as);
380 proc->attr.dimension = proc->result->attr.dimension;
381 proc->attr.pointer = proc->result->attr.pointer;
382 proc->attr.allocatable = proc->result->attr.allocatable;
385 else if (!proc->result->attr.proc_pointer)
387 gfc_error ("Function result %qs at %L has no IMPLICIT type",
388 proc->result->name, &proc->result->declared_at);
389 proc->result->attr.untyped = 1;
395 /******************** Symbol attribute stuff *********************/
397 /* This is a generic conflict-checker. We do this to avoid having a
398 single conflict in two places. */
400 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
401 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
402 #define conf_std(a, b, std) if (attr->a && attr->b)\
404 a1 = a;\
405 a2 = b;\
406 standard = std;\
407 goto conflict_std;\
410 static bool
411 check_conflict (symbol_attribute *attr, const char *name, locus *where)
413 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
414 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
415 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
416 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
417 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
418 *privat = "PRIVATE", *recursive = "RECURSIVE",
419 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
420 *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
421 *function = "FUNCTION", *subroutine = "SUBROUTINE",
422 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
423 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
424 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
425 *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
426 *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
427 *proc_pointer = "PROCEDURE POINTER", *abstract = "ABSTRACT",
428 *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION",
429 *contiguous = "CONTIGUOUS", *generic = "GENERIC", *automatic = "AUTOMATIC",
430 *pdt_len = "LEN", *pdt_kind = "KIND";
431 static const char *threadprivate = "THREADPRIVATE";
432 static const char *omp_declare_target = "OMP DECLARE TARGET";
433 static const char *omp_declare_target_link = "OMP DECLARE TARGET LINK";
434 static const char *oacc_declare_copyin = "OACC DECLARE COPYIN";
435 static const char *oacc_declare_create = "OACC DECLARE CREATE";
436 static const char *oacc_declare_deviceptr = "OACC DECLARE DEVICEPTR";
437 static const char *oacc_declare_device_resident =
438 "OACC DECLARE DEVICE_RESIDENT";
440 const char *a1, *a2;
441 int standard;
443 if (attr->artificial)
444 return true;
446 if (where == NULL)
447 where = &gfc_current_locus;
449 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
451 a1 = pointer;
452 a2 = intent;
453 standard = GFC_STD_F2003;
454 goto conflict_std;
457 if (attr->in_namelist && (attr->allocatable || attr->pointer))
459 a1 = in_namelist;
460 a2 = attr->allocatable ? allocatable : pointer;
461 standard = GFC_STD_F2003;
462 goto conflict_std;
465 /* Check for attributes not allowed in a BLOCK DATA. */
466 if (gfc_current_state () == COMP_BLOCK_DATA)
468 a1 = NULL;
470 if (attr->in_namelist)
471 a1 = in_namelist;
472 if (attr->allocatable)
473 a1 = allocatable;
474 if (attr->external)
475 a1 = external;
476 if (attr->optional)
477 a1 = optional;
478 if (attr->access == ACCESS_PRIVATE)
479 a1 = privat;
480 if (attr->access == ACCESS_PUBLIC)
481 a1 = publik;
482 if (attr->intent != INTENT_UNKNOWN)
483 a1 = intent;
485 if (a1 != NULL)
487 gfc_error
488 ("%s attribute not allowed in BLOCK DATA program unit at %L",
489 a1, where);
490 return false;
494 if (attr->save == SAVE_EXPLICIT)
496 conf (dummy, save);
497 conf (in_common, save);
498 conf (result, save);
499 conf (automatic, save);
501 switch (attr->flavor)
503 case FL_PROGRAM:
504 case FL_BLOCK_DATA:
505 case FL_MODULE:
506 case FL_LABEL:
507 case_fl_struct:
508 case FL_PARAMETER:
509 a1 = gfc_code2string (flavors, attr->flavor);
510 a2 = save;
511 goto conflict;
512 case FL_NAMELIST:
513 gfc_error ("Namelist group name at %L cannot have the "
514 "SAVE attribute", where);
515 return false;
516 case FL_PROCEDURE:
517 /* Conflicts between SAVE and PROCEDURE will be checked at
518 resolution stage, see "resolve_fl_procedure". */
519 case FL_VARIABLE:
520 default:
521 break;
525 /* The copying of procedure dummy arguments for module procedures in
526 a submodule occur whilst the current state is COMP_CONTAINS. It
527 is necessary, therefore, to let this through. */
528 if (name && attr->dummy
529 && (attr->function || attr->subroutine)
530 && gfc_current_state () == COMP_CONTAINS
531 && !(gfc_new_block && gfc_new_block->abr_modproc_decl))
532 gfc_error_now ("internal procedure %qs at %L conflicts with "
533 "DUMMY argument", name, where);
535 conf (dummy, entry);
536 conf (dummy, intrinsic);
537 conf (dummy, threadprivate);
538 conf (dummy, omp_declare_target);
539 conf (dummy, omp_declare_target_link);
540 conf (pointer, target);
541 conf (pointer, intrinsic);
542 conf (pointer, elemental);
543 conf (pointer, codimension);
544 conf (allocatable, elemental);
546 conf (in_common, automatic);
547 conf (in_equivalence, automatic);
548 conf (result, automatic);
549 conf (use_assoc, automatic);
550 conf (dummy, automatic);
552 conf (target, external);
553 conf (target, intrinsic);
555 if (!attr->if_source)
556 conf (external, dimension); /* See Fortran 95's R504. */
558 conf (external, intrinsic);
559 conf (entry, intrinsic);
561 if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
562 conf (external, subroutine);
564 if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
565 "Procedure pointer at %C"))
566 return false;
568 conf (allocatable, pointer);
569 conf_std (allocatable, dummy, GFC_STD_F2003);
570 conf_std (allocatable, function, GFC_STD_F2003);
571 conf_std (allocatable, result, GFC_STD_F2003);
572 conf (elemental, recursive);
574 conf (in_common, dummy);
575 conf (in_common, allocatable);
576 conf (in_common, codimension);
577 conf (in_common, result);
579 conf (in_equivalence, use_assoc);
580 conf (in_equivalence, codimension);
581 conf (in_equivalence, dummy);
582 conf (in_equivalence, target);
583 conf (in_equivalence, pointer);
584 conf (in_equivalence, function);
585 conf (in_equivalence, result);
586 conf (in_equivalence, entry);
587 conf (in_equivalence, allocatable);
588 conf (in_equivalence, threadprivate);
589 conf (in_equivalence, omp_declare_target);
590 conf (in_equivalence, omp_declare_target_link);
591 conf (in_equivalence, oacc_declare_create);
592 conf (in_equivalence, oacc_declare_copyin);
593 conf (in_equivalence, oacc_declare_deviceptr);
594 conf (in_equivalence, oacc_declare_device_resident);
595 conf (in_equivalence, is_bind_c);
597 conf (dummy, result);
598 conf (entry, result);
599 conf (generic, result);
600 conf (generic, omp_declare_target);
601 conf (generic, omp_declare_target_link);
603 conf (function, subroutine);
605 if (!function && !subroutine)
606 conf (is_bind_c, dummy);
608 conf (is_bind_c, cray_pointer);
609 conf (is_bind_c, cray_pointee);
610 conf (is_bind_c, codimension);
611 conf (is_bind_c, allocatable);
612 conf (is_bind_c, elemental);
614 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
615 Parameter conflict caught below. Also, value cannot be specified
616 for a dummy procedure. */
618 /* Cray pointer/pointee conflicts. */
619 conf (cray_pointer, cray_pointee);
620 conf (cray_pointer, dimension);
621 conf (cray_pointer, codimension);
622 conf (cray_pointer, contiguous);
623 conf (cray_pointer, pointer);
624 conf (cray_pointer, target);
625 conf (cray_pointer, allocatable);
626 conf (cray_pointer, external);
627 conf (cray_pointer, intrinsic);
628 conf (cray_pointer, in_namelist);
629 conf (cray_pointer, function);
630 conf (cray_pointer, subroutine);
631 conf (cray_pointer, entry);
633 conf (cray_pointee, allocatable);
634 conf (cray_pointee, contiguous);
635 conf (cray_pointee, codimension);
636 conf (cray_pointee, intent);
637 conf (cray_pointee, optional);
638 conf (cray_pointee, dummy);
639 conf (cray_pointee, target);
640 conf (cray_pointee, intrinsic);
641 conf (cray_pointee, pointer);
642 conf (cray_pointee, entry);
643 conf (cray_pointee, in_common);
644 conf (cray_pointee, in_equivalence);
645 conf (cray_pointee, threadprivate);
646 conf (cray_pointee, omp_declare_target);
647 conf (cray_pointee, omp_declare_target_link);
648 conf (cray_pointee, oacc_declare_create);
649 conf (cray_pointee, oacc_declare_copyin);
650 conf (cray_pointee, oacc_declare_deviceptr);
651 conf (cray_pointee, oacc_declare_device_resident);
653 conf (data, dummy);
654 conf (data, function);
655 conf (data, result);
656 conf (data, allocatable);
658 conf (value, pointer)
659 conf (value, allocatable)
660 conf (value, subroutine)
661 conf (value, function)
662 conf (value, volatile_)
663 conf (value, dimension)
664 conf (value, codimension)
665 conf (value, external)
667 conf (codimension, result)
669 if (attr->value
670 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
672 a1 = value;
673 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
674 goto conflict;
677 conf (is_protected, intrinsic)
678 conf (is_protected, in_common)
680 conf (asynchronous, intrinsic)
681 conf (asynchronous, external)
683 conf (volatile_, intrinsic)
684 conf (volatile_, external)
686 if (attr->volatile_ && attr->intent == INTENT_IN)
688 a1 = volatile_;
689 a2 = intent_in;
690 goto conflict;
693 conf (procedure, allocatable)
694 conf (procedure, dimension)
695 conf (procedure, codimension)
696 conf (procedure, intrinsic)
697 conf (procedure, target)
698 conf (procedure, value)
699 conf (procedure, volatile_)
700 conf (procedure, asynchronous)
701 conf (procedure, entry)
703 conf (proc_pointer, abstract)
704 conf (proc_pointer, omp_declare_target)
705 conf (proc_pointer, omp_declare_target_link)
707 conf (entry, omp_declare_target)
708 conf (entry, omp_declare_target_link)
709 conf (entry, oacc_declare_create)
710 conf (entry, oacc_declare_copyin)
711 conf (entry, oacc_declare_deviceptr)
712 conf (entry, oacc_declare_device_resident)
714 conf (pdt_kind, allocatable)
715 conf (pdt_kind, pointer)
716 conf (pdt_kind, dimension)
717 conf (pdt_kind, codimension)
719 conf (pdt_len, allocatable)
720 conf (pdt_len, pointer)
721 conf (pdt_len, dimension)
722 conf (pdt_len, codimension)
724 if (attr->access == ACCESS_PRIVATE)
726 a1 = privat;
727 conf2 (pdt_kind);
728 conf2 (pdt_len);
731 a1 = gfc_code2string (flavors, attr->flavor);
733 if (attr->in_namelist
734 && attr->flavor != FL_VARIABLE
735 && attr->flavor != FL_PROCEDURE
736 && attr->flavor != FL_UNKNOWN)
738 a2 = in_namelist;
739 goto conflict;
742 switch (attr->flavor)
744 case FL_PROGRAM:
745 case FL_BLOCK_DATA:
746 case FL_MODULE:
747 case FL_LABEL:
748 conf2 (codimension);
749 conf2 (dimension);
750 conf2 (dummy);
751 conf2 (volatile_);
752 conf2 (asynchronous);
753 conf2 (contiguous);
754 conf2 (pointer);
755 conf2 (is_protected);
756 conf2 (target);
757 conf2 (external);
758 conf2 (intrinsic);
759 conf2 (allocatable);
760 conf2 (result);
761 conf2 (in_namelist);
762 conf2 (optional);
763 conf2 (function);
764 conf2 (subroutine);
765 conf2 (threadprivate);
766 conf2 (omp_declare_target);
767 conf2 (omp_declare_target_link);
768 conf2 (oacc_declare_create);
769 conf2 (oacc_declare_copyin);
770 conf2 (oacc_declare_deviceptr);
771 conf2 (oacc_declare_device_resident);
773 if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
775 a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
776 gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
777 name, where);
778 return false;
781 if (attr->is_bind_c)
783 gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
784 return false;
787 break;
789 case FL_VARIABLE:
790 break;
792 case FL_NAMELIST:
793 conf2 (result);
794 break;
796 case FL_PROCEDURE:
797 /* Conflicts with INTENT, SAVE and RESULT will be checked
798 at resolution stage, see "resolve_fl_procedure". */
800 if (attr->subroutine)
802 a1 = subroutine;
803 conf2 (target);
804 conf2 (allocatable);
805 conf2 (volatile_);
806 conf2 (asynchronous);
807 conf2 (in_namelist);
808 conf2 (codimension);
809 conf2 (dimension);
810 conf2 (function);
811 if (!attr->proc_pointer)
812 conf2 (threadprivate);
815 /* Procedure pointers in COMMON blocks are allowed in F03,
816 * but forbidden per F08:C5100. */
817 if (!attr->proc_pointer || (gfc_option.allow_std & GFC_STD_F2008))
818 conf2 (in_common);
820 conf2 (omp_declare_target_link);
822 switch (attr->proc)
824 case PROC_ST_FUNCTION:
825 conf2 (dummy);
826 conf2 (target);
827 break;
829 case PROC_MODULE:
830 conf2 (dummy);
831 break;
833 case PROC_DUMMY:
834 conf2 (result);
835 conf2 (threadprivate);
836 break;
838 default:
839 break;
842 break;
844 case_fl_struct:
845 conf2 (dummy);
846 conf2 (pointer);
847 conf2 (target);
848 conf2 (external);
849 conf2 (intrinsic);
850 conf2 (allocatable);
851 conf2 (optional);
852 conf2 (entry);
853 conf2 (function);
854 conf2 (subroutine);
855 conf2 (threadprivate);
856 conf2 (result);
857 conf2 (omp_declare_target);
858 conf2 (omp_declare_target_link);
859 conf2 (oacc_declare_create);
860 conf2 (oacc_declare_copyin);
861 conf2 (oacc_declare_deviceptr);
862 conf2 (oacc_declare_device_resident);
864 if (attr->intent != INTENT_UNKNOWN)
866 a2 = intent;
867 goto conflict;
869 break;
871 case FL_PARAMETER:
872 conf2 (external);
873 conf2 (intrinsic);
874 conf2 (optional);
875 conf2 (allocatable);
876 conf2 (function);
877 conf2 (subroutine);
878 conf2 (entry);
879 conf2 (contiguous);
880 conf2 (pointer);
881 conf2 (is_protected);
882 conf2 (target);
883 conf2 (dummy);
884 conf2 (in_common);
885 conf2 (value);
886 conf2 (volatile_);
887 conf2 (asynchronous);
888 conf2 (threadprivate);
889 conf2 (value);
890 conf2 (codimension);
891 conf2 (result);
892 if (!attr->is_iso_c)
893 conf2 (is_bind_c);
894 break;
896 default:
897 break;
900 return true;
902 conflict:
903 if (name == NULL)
904 gfc_error ("%s attribute conflicts with %s attribute at %L",
905 a1, a2, where);
906 else
907 gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
908 a1, a2, name, where);
910 return false;
912 conflict_std:
913 if (name == NULL)
915 return gfc_notify_std (standard, "%s attribute conflicts "
916 "with %s attribute at %L", a1, a2,
917 where);
919 else
921 return gfc_notify_std (standard, "%s attribute conflicts "
922 "with %s attribute in %qs at %L",
923 a1, a2, name, where);
927 #undef conf
928 #undef conf2
929 #undef conf_std
932 /* Mark a symbol as referenced. */
934 void
935 gfc_set_sym_referenced (gfc_symbol *sym)
938 if (sym->attr.referenced)
939 return;
941 sym->attr.referenced = 1;
943 /* Remember which order dummy variables are accessed in. */
944 if (sym->attr.dummy)
945 sym->dummy_order = next_dummy_order++;
949 /* Common subroutine called by attribute changing subroutines in order
950 to prevent them from changing a symbol that has been
951 use-associated. Returns zero if it is OK to change the symbol,
952 nonzero if not. */
954 static int
955 check_used (symbol_attribute *attr, const char *name, locus *where)
958 if (attr->use_assoc == 0)
959 return 0;
961 if (where == NULL)
962 where = &gfc_current_locus;
964 if (name == NULL)
965 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
966 where);
967 else
968 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
969 name, where);
971 return 1;
975 /* Generate an error because of a duplicate attribute. */
977 static void
978 duplicate_attr (const char *attr, locus *where)
981 if (where == NULL)
982 where = &gfc_current_locus;
984 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
988 bool
989 gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
990 locus *where ATTRIBUTE_UNUSED)
992 attr->ext_attr |= 1 << ext_attr;
993 return true;
997 /* Called from decl.c (attr_decl1) to check attributes, when declared
998 separately. */
1000 bool
1001 gfc_add_attribute (symbol_attribute *attr, locus *where)
1003 if (check_used (attr, NULL, where))
1004 return false;
1006 return check_conflict (attr, NULL, where);
1010 bool
1011 gfc_add_allocatable (symbol_attribute *attr, locus *where)
1014 if (check_used (attr, NULL, where))
1015 return false;
1017 if (attr->allocatable)
1019 duplicate_attr ("ALLOCATABLE", where);
1020 return false;
1023 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1024 && !gfc_find_state (COMP_INTERFACE))
1026 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
1027 where);
1028 return false;
1031 attr->allocatable = 1;
1032 return check_conflict (attr, NULL, where);
1036 bool
1037 gfc_add_automatic (symbol_attribute *attr, const char *name, locus *where)
1039 if (check_used (attr, name, where))
1040 return false;
1042 if (attr->automatic && !gfc_notify_std (GFC_STD_LEGACY,
1043 "Duplicate AUTOMATIC attribute specified at %L", where))
1044 return false;
1046 attr->automatic = 1;
1047 return check_conflict (attr, name, where);
1051 bool
1052 gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
1055 if (check_used (attr, name, where))
1056 return false;
1058 if (attr->codimension)
1060 duplicate_attr ("CODIMENSION", where);
1061 return false;
1064 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1065 && !gfc_find_state (COMP_INTERFACE))
1067 gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
1068 "at %L", name, where);
1069 return false;
1072 attr->codimension = 1;
1073 return check_conflict (attr, name, where);
1077 bool
1078 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
1081 if (check_used (attr, name, where))
1082 return false;
1084 if (attr->dimension)
1086 duplicate_attr ("DIMENSION", where);
1087 return false;
1090 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1091 && !gfc_find_state (COMP_INTERFACE))
1093 gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
1094 "at %L", name, where);
1095 return false;
1098 attr->dimension = 1;
1099 return check_conflict (attr, name, where);
1103 bool
1104 gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
1107 if (check_used (attr, name, where))
1108 return false;
1110 attr->contiguous = 1;
1111 return check_conflict (attr, name, where);
1115 bool
1116 gfc_add_external (symbol_attribute *attr, locus *where)
1119 if (check_used (attr, NULL, where))
1120 return false;
1122 if (attr->external)
1124 duplicate_attr ("EXTERNAL", where);
1125 return false;
1128 if (attr->pointer && attr->if_source != IFSRC_IFBODY)
1130 attr->pointer = 0;
1131 attr->proc_pointer = 1;
1134 attr->external = 1;
1136 return check_conflict (attr, NULL, where);
1140 bool
1141 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
1144 if (check_used (attr, NULL, where))
1145 return false;
1147 if (attr->intrinsic)
1149 duplicate_attr ("INTRINSIC", where);
1150 return false;
1153 attr->intrinsic = 1;
1155 return check_conflict (attr, NULL, where);
1159 bool
1160 gfc_add_optional (symbol_attribute *attr, locus *where)
1163 if (check_used (attr, NULL, where))
1164 return false;
1166 if (attr->optional)
1168 duplicate_attr ("OPTIONAL", where);
1169 return false;
1172 attr->optional = 1;
1173 return check_conflict (attr, NULL, where);
1176 bool
1177 gfc_add_kind (symbol_attribute *attr, locus *where)
1179 if (attr->pdt_kind)
1181 duplicate_attr ("KIND", where);
1182 return false;
1185 attr->pdt_kind = 1;
1186 return check_conflict (attr, NULL, where);
1189 bool
1190 gfc_add_len (symbol_attribute *attr, locus *where)
1192 if (attr->pdt_len)
1194 duplicate_attr ("LEN", where);
1195 return false;
1198 attr->pdt_len = 1;
1199 return check_conflict (attr, NULL, where);
1203 bool
1204 gfc_add_pointer (symbol_attribute *attr, locus *where)
1207 if (check_used (attr, NULL, where))
1208 return false;
1210 if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
1211 && !gfc_find_state (COMP_INTERFACE)))
1213 duplicate_attr ("POINTER", where);
1214 return false;
1217 if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
1218 || (attr->if_source == IFSRC_IFBODY
1219 && !gfc_find_state (COMP_INTERFACE)))
1220 attr->proc_pointer = 1;
1221 else
1222 attr->pointer = 1;
1224 return check_conflict (attr, NULL, where);
1228 bool
1229 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
1232 if (check_used (attr, NULL, where))
1233 return false;
1235 attr->cray_pointer = 1;
1236 return check_conflict (attr, NULL, where);
1240 bool
1241 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
1244 if (check_used (attr, NULL, where))
1245 return false;
1247 if (attr->cray_pointee)
1249 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1250 " statements", where);
1251 return false;
1254 attr->cray_pointee = 1;
1255 return check_conflict (attr, NULL, where);
1259 bool
1260 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
1262 if (check_used (attr, name, where))
1263 return false;
1265 if (attr->is_protected)
1267 if (!gfc_notify_std (GFC_STD_LEGACY,
1268 "Duplicate PROTECTED attribute specified at %L",
1269 where))
1270 return false;
1273 attr->is_protected = 1;
1274 return check_conflict (attr, name, where);
1278 bool
1279 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
1282 if (check_used (attr, name, where))
1283 return false;
1285 attr->result = 1;
1286 return check_conflict (attr, name, where);
1290 bool
1291 gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
1292 locus *where)
1295 if (check_used (attr, name, where))
1296 return false;
1298 if (s == SAVE_EXPLICIT && gfc_pure (NULL))
1300 gfc_error
1301 ("SAVE attribute at %L cannot be specified in a PURE procedure",
1302 where);
1303 return false;
1306 if (s == SAVE_EXPLICIT)
1307 gfc_unset_implicit_pure (NULL);
1309 if (s == SAVE_EXPLICIT && attr->save == SAVE_EXPLICIT
1310 && (flag_automatic || pedantic))
1312 if (!gfc_notify_std (GFC_STD_LEGACY,
1313 "Duplicate SAVE attribute specified at %L",
1314 where))
1315 return false;
1318 attr->save = s;
1319 return check_conflict (attr, name, where);
1323 bool
1324 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1327 if (check_used (attr, name, where))
1328 return false;
1330 if (attr->value)
1332 if (!gfc_notify_std (GFC_STD_LEGACY,
1333 "Duplicate VALUE attribute specified at %L",
1334 where))
1335 return false;
1338 attr->value = 1;
1339 return check_conflict (attr, name, where);
1343 bool
1344 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1346 /* No check_used needed as 11.2.1 of the F2003 standard allows
1347 that the local identifier made accessible by a use statement can be
1348 given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1350 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1351 if (!gfc_notify_std (GFC_STD_LEGACY,
1352 "Duplicate VOLATILE attribute specified at %L",
1353 where))
1354 return false;
1356 /* F2008: C1282 A designator of a variable with the VOLATILE attribute
1357 shall not appear in a pure subprogram.
1359 F2018: C1588 A local variable of a pure subprogram, or of a BLOCK
1360 construct within a pure subprogram, shall not have the SAVE or
1361 VOLATILE attribute. */
1362 if (gfc_pure (NULL))
1364 gfc_error ("VOLATILE attribute at %L cannot be specified in a "
1365 "PURE procedure", where);
1366 return false;
1370 attr->volatile_ = 1;
1371 attr->volatile_ns = gfc_current_ns;
1372 return check_conflict (attr, name, where);
1376 bool
1377 gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
1379 /* No check_used needed as 11.2.1 of the F2003 standard allows
1380 that the local identifier made accessible by a use statement can be
1381 given a ASYNCHRONOUS attribute. */
1383 if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
1384 if (!gfc_notify_std (GFC_STD_LEGACY,
1385 "Duplicate ASYNCHRONOUS attribute specified at %L",
1386 where))
1387 return false;
1389 attr->asynchronous = 1;
1390 attr->asynchronous_ns = gfc_current_ns;
1391 return check_conflict (attr, name, where);
1395 bool
1396 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1399 if (check_used (attr, name, where))
1400 return false;
1402 if (attr->threadprivate)
1404 duplicate_attr ("THREADPRIVATE", where);
1405 return false;
1408 attr->threadprivate = 1;
1409 return check_conflict (attr, name, where);
1413 bool
1414 gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
1415 locus *where)
1418 if (check_used (attr, name, where))
1419 return false;
1421 if (attr->omp_declare_target)
1422 return true;
1424 attr->omp_declare_target = 1;
1425 return check_conflict (attr, name, where);
1429 bool
1430 gfc_add_omp_declare_target_link (symbol_attribute *attr, const char *name,
1431 locus *where)
1434 if (check_used (attr, name, where))
1435 return false;
1437 if (attr->omp_declare_target_link)
1438 return true;
1440 attr->omp_declare_target_link = 1;
1441 return check_conflict (attr, name, where);
1445 bool
1446 gfc_add_oacc_declare_create (symbol_attribute *attr, const char *name,
1447 locus *where)
1449 if (check_used (attr, name, where))
1450 return false;
1452 if (attr->oacc_declare_create)
1453 return true;
1455 attr->oacc_declare_create = 1;
1456 return check_conflict (attr, name, where);
1460 bool
1461 gfc_add_oacc_declare_copyin (symbol_attribute *attr, const char *name,
1462 locus *where)
1464 if (check_used (attr, name, where))
1465 return false;
1467 if (attr->oacc_declare_copyin)
1468 return true;
1470 attr->oacc_declare_copyin = 1;
1471 return check_conflict (attr, name, where);
1475 bool
1476 gfc_add_oacc_declare_deviceptr (symbol_attribute *attr, const char *name,
1477 locus *where)
1479 if (check_used (attr, name, where))
1480 return false;
1482 if (attr->oacc_declare_deviceptr)
1483 return true;
1485 attr->oacc_declare_deviceptr = 1;
1486 return check_conflict (attr, name, where);
1490 bool
1491 gfc_add_oacc_declare_device_resident (symbol_attribute *attr, const char *name,
1492 locus *where)
1494 if (check_used (attr, name, where))
1495 return false;
1497 if (attr->oacc_declare_device_resident)
1498 return true;
1500 attr->oacc_declare_device_resident = 1;
1501 return check_conflict (attr, name, where);
1505 bool
1506 gfc_add_target (symbol_attribute *attr, locus *where)
1509 if (check_used (attr, NULL, where))
1510 return false;
1512 if (attr->target)
1514 duplicate_attr ("TARGET", where);
1515 return false;
1518 attr->target = 1;
1519 return check_conflict (attr, NULL, where);
1523 bool
1524 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1527 if (check_used (attr, name, where))
1528 return false;
1530 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1531 attr->dummy = 1;
1532 return check_conflict (attr, name, where);
1536 bool
1537 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1540 if (check_used (attr, name, where))
1541 return false;
1543 /* Duplicate attribute already checked for. */
1544 attr->in_common = 1;
1545 return check_conflict (attr, name, where);
1549 bool
1550 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1553 /* Duplicate attribute already checked for. */
1554 attr->in_equivalence = 1;
1555 if (!check_conflict (attr, name, where))
1556 return false;
1558 if (attr->flavor == FL_VARIABLE)
1559 return true;
1561 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1565 bool
1566 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1569 if (check_used (attr, name, where))
1570 return false;
1572 attr->data = 1;
1573 return check_conflict (attr, name, where);
1577 bool
1578 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1581 attr->in_namelist = 1;
1582 return check_conflict (attr, name, where);
1586 bool
1587 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1590 if (check_used (attr, name, where))
1591 return false;
1593 attr->sequence = 1;
1594 return check_conflict (attr, name, where);
1598 bool
1599 gfc_add_elemental (symbol_attribute *attr, locus *where)
1602 if (check_used (attr, NULL, where))
1603 return false;
1605 if (attr->elemental)
1607 duplicate_attr ("ELEMENTAL", where);
1608 return false;
1611 attr->elemental = 1;
1612 return check_conflict (attr, NULL, where);
1616 bool
1617 gfc_add_pure (symbol_attribute *attr, locus *where)
1620 if (check_used (attr, NULL, where))
1621 return false;
1623 if (attr->pure)
1625 duplicate_attr ("PURE", where);
1626 return false;
1629 attr->pure = 1;
1630 return check_conflict (attr, NULL, where);
1634 bool
1635 gfc_add_recursive (symbol_attribute *attr, locus *where)
1638 if (check_used (attr, NULL, where))
1639 return false;
1641 if (attr->recursive)
1643 duplicate_attr ("RECURSIVE", where);
1644 return false;
1647 attr->recursive = 1;
1648 return check_conflict (attr, NULL, where);
1652 bool
1653 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1656 if (check_used (attr, name, where))
1657 return false;
1659 if (attr->entry)
1661 duplicate_attr ("ENTRY", where);
1662 return false;
1665 attr->entry = 1;
1666 return check_conflict (attr, name, where);
1670 bool
1671 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1674 if (attr->flavor != FL_PROCEDURE
1675 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1676 return false;
1678 attr->function = 1;
1679 return check_conflict (attr, name, where);
1683 bool
1684 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1687 if (attr->flavor != FL_PROCEDURE
1688 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1689 return false;
1691 attr->subroutine = 1;
1693 /* If we are looking at a BLOCK DATA statement and we encounter a
1694 name with a leading underscore (which must be
1695 compiler-generated), do not check. See PR 84394. */
1697 if (name && *name != '_' && gfc_current_state () != COMP_BLOCK_DATA)
1698 return check_conflict (attr, name, where);
1699 else
1700 return true;
1704 bool
1705 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1708 if (attr->flavor != FL_PROCEDURE
1709 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1710 return false;
1712 attr->generic = 1;
1713 return check_conflict (attr, name, where);
1717 bool
1718 gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1721 if (check_used (attr, NULL, where))
1722 return false;
1724 if (attr->flavor != FL_PROCEDURE
1725 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1726 return false;
1728 if (attr->procedure)
1730 duplicate_attr ("PROCEDURE", where);
1731 return false;
1734 attr->procedure = 1;
1736 return check_conflict (attr, NULL, where);
1740 bool
1741 gfc_add_abstract (symbol_attribute* attr, locus* where)
1743 if (attr->abstract)
1745 duplicate_attr ("ABSTRACT", where);
1746 return false;
1749 attr->abstract = 1;
1751 return check_conflict (attr, NULL, where);
1755 /* Flavors are special because some flavors are not what Fortran
1756 considers attributes and can be reaffirmed multiple times. */
1758 bool
1759 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1760 locus *where)
1763 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1764 || f == FL_PARAMETER || f == FL_LABEL || gfc_fl_struct(f)
1765 || f == FL_NAMELIST) && check_used (attr, name, where))
1766 return false;
1768 if (attr->flavor == f && f == FL_VARIABLE)
1769 return true;
1771 /* Copying a procedure dummy argument for a module procedure in a
1772 submodule results in the flavor being copied and would result in
1773 an error without this. */
1774 if (gfc_new_block && gfc_new_block->abr_modproc_decl
1775 && attr->flavor == f && f == FL_PROCEDURE)
1776 return true;
1778 if (attr->flavor != FL_UNKNOWN)
1780 if (where == NULL)
1781 where = &gfc_current_locus;
1783 if (name)
1784 gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
1785 gfc_code2string (flavors, attr->flavor), name,
1786 gfc_code2string (flavors, f), where);
1787 else
1788 gfc_error ("%s attribute conflicts with %s attribute at %L",
1789 gfc_code2string (flavors, attr->flavor),
1790 gfc_code2string (flavors, f), where);
1792 return false;
1795 attr->flavor = f;
1797 return check_conflict (attr, name, where);
1801 bool
1802 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1803 const char *name, locus *where)
1806 if (check_used (attr, name, where))
1807 return false;
1809 if (attr->flavor != FL_PROCEDURE
1810 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1811 return false;
1813 if (where == NULL)
1814 where = &gfc_current_locus;
1816 if (attr->proc != PROC_UNKNOWN && !attr->module_procedure
1817 && attr->access == ACCESS_UNKNOWN)
1819 if (attr->proc == PROC_ST_FUNCTION && t == PROC_INTERNAL
1820 && !gfc_notification_std (GFC_STD_F2008))
1821 gfc_error ("%s procedure at %L is already declared as %s "
1822 "procedure. \nF2008: A pointer function assignment "
1823 "is ambiguous if it is the first executable statement "
1824 "after the specification block. Please add any other "
1825 "kind of executable statement before it. FIXME",
1826 gfc_code2string (procedures, t), where,
1827 gfc_code2string (procedures, attr->proc));
1828 else
1829 gfc_error ("%s procedure at %L is already declared as %s "
1830 "procedure", gfc_code2string (procedures, t), where,
1831 gfc_code2string (procedures, attr->proc));
1833 return false;
1836 attr->proc = t;
1838 /* Statement functions are always scalar and functions. */
1839 if (t == PROC_ST_FUNCTION
1840 && ((!attr->function && !gfc_add_function (attr, name, where))
1841 || attr->dimension))
1842 return false;
1844 return check_conflict (attr, name, where);
1848 bool
1849 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1852 if (check_used (attr, NULL, where))
1853 return false;
1855 if (attr->intent == INTENT_UNKNOWN)
1857 attr->intent = intent;
1858 return check_conflict (attr, NULL, where);
1861 if (where == NULL)
1862 where = &gfc_current_locus;
1864 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1865 gfc_intent_string (attr->intent),
1866 gfc_intent_string (intent), where);
1868 return false;
1872 /* No checks for use-association in public and private statements. */
1874 bool
1875 gfc_add_access (symbol_attribute *attr, gfc_access access,
1876 const char *name, locus *where)
1879 if (attr->access == ACCESS_UNKNOWN
1880 || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
1882 attr->access = access;
1883 return check_conflict (attr, name, where);
1886 if (where == NULL)
1887 where = &gfc_current_locus;
1888 gfc_error ("ACCESS specification at %L was already specified", where);
1890 return false;
1894 /* Set the is_bind_c field for the given symbol_attribute. */
1896 bool
1897 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1898 int is_proc_lang_bind_spec)
1901 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1902 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1903 "variables or common blocks", where);
1904 else if (attr->is_bind_c)
1905 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1906 else
1907 attr->is_bind_c = 1;
1909 if (where == NULL)
1910 where = &gfc_current_locus;
1912 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
1913 return false;
1915 return check_conflict (attr, name, where);
1919 /* Set the extension field for the given symbol_attribute. */
1921 bool
1922 gfc_add_extension (symbol_attribute *attr, locus *where)
1924 if (where == NULL)
1925 where = &gfc_current_locus;
1927 if (attr->extension)
1928 gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
1929 else
1930 attr->extension = 1;
1932 if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
1933 return false;
1935 return true;
1939 bool
1940 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1941 gfc_formal_arglist * formal, locus *where)
1943 if (check_used (&sym->attr, sym->name, where))
1944 return false;
1946 /* Skip the following checks in the case of a module_procedures in a
1947 submodule since they will manifestly fail. */
1948 if (sym->attr.module_procedure == 1
1949 && source == IFSRC_DECL)
1950 goto finish;
1952 if (where == NULL)
1953 where = &gfc_current_locus;
1955 if (sym->attr.if_source != IFSRC_UNKNOWN
1956 && sym->attr.if_source != IFSRC_DECL)
1958 gfc_error ("Symbol %qs at %L already has an explicit interface",
1959 sym->name, where);
1960 return false;
1963 if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1965 gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
1966 "body", sym->name, where);
1967 return false;
1970 finish:
1971 sym->formal = formal;
1972 sym->attr.if_source = source;
1974 return true;
1978 /* Add a type to a symbol. */
1980 bool
1981 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1983 sym_flavor flavor;
1984 bt type;
1986 if (where == NULL)
1987 where = &gfc_current_locus;
1989 if (sym->result)
1990 type = sym->result->ts.type;
1991 else
1992 type = sym->ts.type;
1994 if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
1995 type = sym->ns->proc_name->ts.type;
1997 if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
1998 && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
1999 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
2000 && !sym->attr.module_procedure)
2002 if (sym->attr.use_assoc)
2003 gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
2004 "use-associated at %L", sym->name, where, sym->module,
2005 &sym->declared_at);
2006 else
2007 gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
2008 where, gfc_basic_typename (type));
2009 return false;
2012 if (sym->attr.procedure && sym->ts.interface)
2014 gfc_error ("Procedure %qs at %L may not have basic type of %s",
2015 sym->name, where, gfc_basic_typename (ts->type));
2016 return false;
2019 flavor = sym->attr.flavor;
2021 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
2022 || flavor == FL_LABEL
2023 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
2024 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
2026 gfc_error ("Symbol %qs at %L cannot have a type", sym->name, where);
2027 return false;
2030 sym->ts = *ts;
2031 return true;
2035 /* Clears all attributes. */
2037 void
2038 gfc_clear_attr (symbol_attribute *attr)
2040 memset (attr, 0, sizeof (symbol_attribute));
2044 /* Check for missing attributes in the new symbol. Currently does
2045 nothing, but it's not clear that it is unnecessary yet. */
2047 bool
2048 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
2049 locus *where ATTRIBUTE_UNUSED)
2052 return true;
2056 /* Copy an attribute to a symbol attribute, bit by bit. Some
2057 attributes have a lot of side-effects but cannot be present given
2058 where we are called from, so we ignore some bits. */
2060 bool
2061 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
2063 int is_proc_lang_bind_spec;
2065 /* In line with the other attributes, we only add bits but do not remove
2066 them; cf. also PR 41034. */
2067 dest->ext_attr |= src->ext_attr;
2069 if (src->allocatable && !gfc_add_allocatable (dest, where))
2070 goto fail;
2072 if (src->automatic && !gfc_add_automatic (dest, NULL, where))
2073 goto fail;
2074 if (src->dimension && !gfc_add_dimension (dest, NULL, where))
2075 goto fail;
2076 if (src->codimension && !gfc_add_codimension (dest, NULL, where))
2077 goto fail;
2078 if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
2079 goto fail;
2080 if (src->optional && !gfc_add_optional (dest, where))
2081 goto fail;
2082 if (src->pointer && !gfc_add_pointer (dest, where))
2083 goto fail;
2084 if (src->is_protected && !gfc_add_protected (dest, NULL, where))
2085 goto fail;
2086 if (src->save && !gfc_add_save (dest, src->save, NULL, where))
2087 goto fail;
2088 if (src->value && !gfc_add_value (dest, NULL, where))
2089 goto fail;
2090 if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
2091 goto fail;
2092 if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
2093 goto fail;
2094 if (src->threadprivate
2095 && !gfc_add_threadprivate (dest, NULL, where))
2096 goto fail;
2097 if (src->omp_declare_target
2098 && !gfc_add_omp_declare_target (dest, NULL, where))
2099 goto fail;
2100 if (src->omp_declare_target_link
2101 && !gfc_add_omp_declare_target_link (dest, NULL, where))
2102 goto fail;
2103 if (src->oacc_declare_create
2104 && !gfc_add_oacc_declare_create (dest, NULL, where))
2105 goto fail;
2106 if (src->oacc_declare_copyin
2107 && !gfc_add_oacc_declare_copyin (dest, NULL, where))
2108 goto fail;
2109 if (src->oacc_declare_deviceptr
2110 && !gfc_add_oacc_declare_deviceptr (dest, NULL, where))
2111 goto fail;
2112 if (src->oacc_declare_device_resident
2113 && !gfc_add_oacc_declare_device_resident (dest, NULL, where))
2114 goto fail;
2115 if (src->target && !gfc_add_target (dest, where))
2116 goto fail;
2117 if (src->dummy && !gfc_add_dummy (dest, NULL, where))
2118 goto fail;
2119 if (src->result && !gfc_add_result (dest, NULL, where))
2120 goto fail;
2121 if (src->entry)
2122 dest->entry = 1;
2124 if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
2125 goto fail;
2127 if (src->in_common && !gfc_add_in_common (dest, NULL, where))
2128 goto fail;
2130 if (src->generic && !gfc_add_generic (dest, NULL, where))
2131 goto fail;
2132 if (src->function && !gfc_add_function (dest, NULL, where))
2133 goto fail;
2134 if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
2135 goto fail;
2137 if (src->sequence && !gfc_add_sequence (dest, NULL, where))
2138 goto fail;
2139 if (src->elemental && !gfc_add_elemental (dest, where))
2140 goto fail;
2141 if (src->pure && !gfc_add_pure (dest, where))
2142 goto fail;
2143 if (src->recursive && !gfc_add_recursive (dest, where))
2144 goto fail;
2146 if (src->flavor != FL_UNKNOWN
2147 && !gfc_add_flavor (dest, src->flavor, NULL, where))
2148 goto fail;
2150 if (src->intent != INTENT_UNKNOWN
2151 && !gfc_add_intent (dest, src->intent, where))
2152 goto fail;
2154 if (src->access != ACCESS_UNKNOWN
2155 && !gfc_add_access (dest, src->access, NULL, where))
2156 goto fail;
2158 if (!gfc_missing_attr (dest, where))
2159 goto fail;
2161 if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
2162 goto fail;
2163 if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
2164 goto fail;
2166 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
2167 if (src->is_bind_c
2168 && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
2169 return false;
2171 if (src->is_c_interop)
2172 dest->is_c_interop = 1;
2173 if (src->is_iso_c)
2174 dest->is_iso_c = 1;
2176 if (src->external && !gfc_add_external (dest, where))
2177 goto fail;
2178 if (src->intrinsic && !gfc_add_intrinsic (dest, where))
2179 goto fail;
2180 if (src->proc_pointer)
2181 dest->proc_pointer = 1;
2183 return true;
2185 fail:
2186 return false;
2190 /* A function to generate a dummy argument symbol using that from the
2191 interface declaration. Can be used for the result symbol as well if
2192 the flag is set. */
2195 gfc_copy_dummy_sym (gfc_symbol **dsym, gfc_symbol *sym, int result)
2197 int rc;
2199 rc = gfc_get_symbol (sym->name, NULL, dsym);
2200 if (rc)
2201 return rc;
2203 if (!gfc_add_type (*dsym, &(sym->ts), &gfc_current_locus))
2204 return 1;
2206 if (!gfc_copy_attr (&(*dsym)->attr, &(sym->attr),
2207 &gfc_current_locus))
2208 return 1;
2210 if ((*dsym)->attr.dimension)
2211 (*dsym)->as = gfc_copy_array_spec (sym->as);
2213 (*dsym)->attr.class_ok = sym->attr.class_ok;
2215 if ((*dsym) != NULL && !result
2216 && (!gfc_add_dummy(&(*dsym)->attr, (*dsym)->name, NULL)
2217 || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2218 return 1;
2219 else if ((*dsym) != NULL && result
2220 && (!gfc_add_result(&(*dsym)->attr, (*dsym)->name, NULL)
2221 || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2222 return 1;
2224 return 0;
2228 /************** Component name management ************/
2230 /* Component names of a derived type form their own little namespaces
2231 that are separate from all other spaces. The space is composed of
2232 a singly linked list of gfc_component structures whose head is
2233 located in the parent symbol. */
2236 /* Add a component name to a symbol. The call fails if the name is
2237 already present. On success, the component pointer is modified to
2238 point to the additional component structure. */
2240 bool
2241 gfc_add_component (gfc_symbol *sym, const char *name,
2242 gfc_component **component)
2244 gfc_component *p, *tail;
2246 /* Check for existing components with the same name, but not for union
2247 components or containers. Unions and maps are anonymous so they have
2248 unique internal names which will never conflict.
2249 Don't use gfc_find_component here because it calls gfc_use_derived,
2250 but the derived type may not be fully defined yet. */
2251 tail = NULL;
2253 for (p = sym->components; p; p = p->next)
2255 if (strcmp (p->name, name) == 0)
2257 gfc_error ("Component %qs at %C already declared at %L",
2258 name, &p->loc);
2259 return false;
2262 tail = p;
2265 if (sym->attr.extension
2266 && gfc_find_component (sym->components->ts.u.derived,
2267 name, true, true, NULL))
2269 gfc_error ("Component %qs at %C already in the parent type "
2270 "at %L", name, &sym->components->ts.u.derived->declared_at);
2271 return false;
2274 /* Allocate a new component. */
2275 p = gfc_get_component ();
2277 if (tail == NULL)
2278 sym->components = p;
2279 else
2280 tail->next = p;
2282 p->name = gfc_get_string ("%s", name);
2283 p->loc = gfc_current_locus;
2284 p->ts.type = BT_UNKNOWN;
2286 *component = p;
2287 return true;
2291 /* Recursive function to switch derived types of all symbol in a
2292 namespace. */
2294 static void
2295 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
2297 gfc_symbol *sym;
2299 if (st == NULL)
2300 return;
2302 sym = st->n.sym;
2303 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
2304 sym->ts.u.derived = to;
2306 switch_types (st->left, from, to);
2307 switch_types (st->right, from, to);
2311 /* This subroutine is called when a derived type is used in order to
2312 make the final determination about which version to use. The
2313 standard requires that a type be defined before it is 'used', but
2314 such types can appear in IMPLICIT statements before the actual
2315 definition. 'Using' in this context means declaring a variable to
2316 be that type or using the type constructor.
2318 If a type is used and the components haven't been defined, then we
2319 have to have a derived type in a parent unit. We find the node in
2320 the other namespace and point the symtree node in this namespace to
2321 that node. Further reference to this name point to the correct
2322 node. If we can't find the node in a parent namespace, then we have
2323 an error.
2325 This subroutine takes a pointer to a symbol node and returns a
2326 pointer to the translated node or NULL for an error. Usually there
2327 is no translation and we return the node we were passed. */
2329 gfc_symbol *
2330 gfc_use_derived (gfc_symbol *sym)
2332 gfc_symbol *s;
2333 gfc_typespec *t;
2334 gfc_symtree *st;
2335 int i;
2337 if (!sym)
2338 return NULL;
2340 if (sym->attr.unlimited_polymorphic)
2341 return sym;
2343 if (sym->attr.generic)
2344 sym = gfc_find_dt_in_generic (sym);
2346 if (sym->components != NULL || sym->attr.zero_comp)
2347 return sym; /* Already defined. */
2349 if (sym->ns->parent == NULL)
2350 goto bad;
2352 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
2354 gfc_error ("Symbol %qs at %C is ambiguous", sym->name);
2355 return NULL;
2358 if (s == NULL || !gfc_fl_struct (s->attr.flavor))
2359 goto bad;
2361 /* Get rid of symbol sym, translating all references to s. */
2362 for (i = 0; i < GFC_LETTERS; i++)
2364 t = &sym->ns->default_type[i];
2365 if (t->u.derived == sym)
2366 t->u.derived = s;
2369 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
2370 st->n.sym = s;
2372 s->refs++;
2374 /* Unlink from list of modified symbols. */
2375 gfc_commit_symbol (sym);
2377 switch_types (sym->ns->sym_root, sym, s);
2379 /* TODO: Also have to replace sym -> s in other lists like
2380 namelists, common lists and interface lists. */
2381 gfc_free_symbol (sym);
2383 return s;
2385 bad:
2386 gfc_error ("Derived type %qs at %C is being used before it is defined",
2387 sym->name);
2388 return NULL;
2392 /* Find the component with the given name in the union type symbol.
2393 If ref is not NULL it will be set to the chain of components through which
2394 the component can actually be accessed. This is necessary for unions because
2395 intermediate structures may be maps, nested structures, or other unions,
2396 all of which may (or must) be 'anonymous' to user code. */
2398 static gfc_component *
2399 find_union_component (gfc_symbol *un, const char *name,
2400 bool noaccess, gfc_ref **ref)
2402 gfc_component *m, *check;
2403 gfc_ref *sref, *tmp;
2405 for (m = un->components; m; m = m->next)
2407 check = gfc_find_component (m->ts.u.derived, name, noaccess, true, &tmp);
2408 if (check == NULL)
2409 continue;
2411 /* Found component somewhere in m; chain the refs together. */
2412 if (ref)
2414 /* Map ref. */
2415 sref = gfc_get_ref ();
2416 sref->type = REF_COMPONENT;
2417 sref->u.c.component = m;
2418 sref->u.c.sym = m->ts.u.derived;
2419 sref->next = tmp;
2421 *ref = sref;
2423 /* Other checks (such as access) were done in the recursive calls. */
2424 return check;
2426 return NULL;
2430 /* Recursively append candidate COMPONENT structures to CANDIDATES. Store
2431 the number of total candidates in CANDIDATES_LEN. */
2433 static void
2434 lookup_component_fuzzy_find_candidates (gfc_component *component,
2435 char **&candidates,
2436 size_t &candidates_len)
2438 for (gfc_component *p = component; p; p = p->next)
2439 vec_push (candidates, candidates_len, p->name);
2443 /* Lookup component MEMBER fuzzily, taking names in COMPONENT into account. */
2445 static const char*
2446 lookup_component_fuzzy (const char *member, gfc_component *component)
2448 char **candidates = NULL;
2449 size_t candidates_len = 0;
2450 lookup_component_fuzzy_find_candidates (component, candidates,
2451 candidates_len);
2452 return gfc_closest_fuzzy_match (member, candidates);
2456 /* Given a derived type node and a component name, try to locate the
2457 component structure. Returns the NULL pointer if the component is
2458 not found or the components are private. If noaccess is set, no access
2459 checks are done. If silent is set, an error will not be generated if
2460 the component cannot be found or accessed.
2462 If ref is not NULL, *ref is set to represent the chain of components
2463 required to get to the ultimate component.
2465 If the component is simply a direct subcomponent, or is inherited from a
2466 parent derived type in the given derived type, this is a single ref with its
2467 component set to the returned component.
2469 Otherwise, *ref is constructed as a chain of subcomponents. This occurs
2470 when the component is found through an implicit chain of nested union and
2471 map components. Unions and maps are "anonymous" substructures in FORTRAN
2472 which cannot be explicitly referenced, but the reference chain must be
2473 considered as in C for backend translation to correctly compute layouts.
2474 (For example, x.a may refer to x->(UNION)->(MAP)->(UNION)->(MAP)->a). */
2476 gfc_component *
2477 gfc_find_component (gfc_symbol *sym, const char *name,
2478 bool noaccess, bool silent, gfc_ref **ref)
2480 gfc_component *p, *check;
2481 gfc_ref *sref = NULL, *tmp = NULL;
2483 if (name == NULL || sym == NULL)
2484 return NULL;
2486 if (sym->attr.flavor == FL_DERIVED)
2487 sym = gfc_use_derived (sym);
2488 else
2489 gcc_assert (gfc_fl_struct (sym->attr.flavor));
2491 if (sym == NULL)
2492 return NULL;
2494 /* Handle UNIONs specially - mutually recursive with gfc_find_component. */
2495 if (sym->attr.flavor == FL_UNION)
2496 return find_union_component (sym, name, noaccess, ref);
2498 if (ref) *ref = NULL;
2499 for (p = sym->components; p; p = p->next)
2501 /* Nest search into union's maps. */
2502 if (p->ts.type == BT_UNION)
2504 check = find_union_component (p->ts.u.derived, name, noaccess, &tmp);
2505 if (check != NULL)
2507 /* Union ref. */
2508 if (ref)
2510 sref = gfc_get_ref ();
2511 sref->type = REF_COMPONENT;
2512 sref->u.c.component = p;
2513 sref->u.c.sym = p->ts.u.derived;
2514 sref->next = tmp;
2515 *ref = sref;
2517 return check;
2520 else if (strcmp (p->name, name) == 0)
2521 break;
2523 continue;
2526 if (p && sym->attr.use_assoc && !noaccess)
2528 bool is_parent_comp = sym->attr.extension && (p == sym->components);
2529 if (p->attr.access == ACCESS_PRIVATE ||
2530 (p->attr.access != ACCESS_PUBLIC
2531 && sym->component_access == ACCESS_PRIVATE
2532 && !is_parent_comp))
2534 if (!silent)
2535 gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
2536 name, sym->name);
2537 return NULL;
2541 if (p == NULL
2542 && sym->attr.extension
2543 && sym->components->ts.type == BT_DERIVED)
2545 p = gfc_find_component (sym->components->ts.u.derived, name,
2546 noaccess, silent, ref);
2547 /* Do not overwrite the error. */
2548 if (p == NULL)
2549 return p;
2552 if (p == NULL && !silent)
2554 const char *guessed = lookup_component_fuzzy (name, sym->components);
2555 if (guessed)
2556 gfc_error ("%qs at %C is not a member of the %qs structure"
2557 "; did you mean %qs?",
2558 name, sym->name, guessed);
2559 else
2560 gfc_error ("%qs at %C is not a member of the %qs structure",
2561 name, sym->name);
2564 /* Component was found; build the ultimate component reference. */
2565 if (p != NULL && ref)
2567 tmp = gfc_get_ref ();
2568 tmp->type = REF_COMPONENT;
2569 tmp->u.c.component = p;
2570 tmp->u.c.sym = sym;
2571 /* Link the final component ref to the end of the chain of subrefs. */
2572 if (sref)
2574 *ref = sref;
2575 for (; sref->next; sref = sref->next)
2577 sref->next = tmp;
2579 else
2580 *ref = tmp;
2583 return p;
2587 /* Given a symbol, free all of the component structures and everything
2588 they point to. */
2590 static void
2591 free_components (gfc_component *p)
2593 gfc_component *q;
2595 for (; p; p = q)
2597 q = p->next;
2599 gfc_free_array_spec (p->as);
2600 gfc_free_expr (p->initializer);
2601 if (p->kind_expr)
2602 gfc_free_expr (p->kind_expr);
2603 if (p->param_list)
2604 gfc_free_actual_arglist (p->param_list);
2605 free (p->tb);
2607 free (p);
2612 /******************** Statement label management ********************/
2614 /* Comparison function for statement labels, used for managing the
2615 binary tree. */
2617 static int
2618 compare_st_labels (void *a1, void *b1)
2620 int a = ((gfc_st_label *) a1)->value;
2621 int b = ((gfc_st_label *) b1)->value;
2623 return (b - a);
2627 /* Free a single gfc_st_label structure, making sure the tree is not
2628 messed up. This function is called only when some parse error
2629 occurs. */
2631 void
2632 gfc_free_st_label (gfc_st_label *label)
2635 if (label == NULL)
2636 return;
2638 gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
2640 if (label->format != NULL)
2641 gfc_free_expr (label->format);
2643 free (label);
2647 /* Free a whole tree of gfc_st_label structures. */
2649 static void
2650 free_st_labels (gfc_st_label *label)
2653 if (label == NULL)
2654 return;
2656 free_st_labels (label->left);
2657 free_st_labels (label->right);
2659 if (label->format != NULL)
2660 gfc_free_expr (label->format);
2661 free (label);
2665 /* Given a label number, search for and return a pointer to the label
2666 structure, creating it if it does not exist. */
2668 gfc_st_label *
2669 gfc_get_st_label (int labelno)
2671 gfc_st_label *lp;
2672 gfc_namespace *ns;
2674 if (gfc_current_state () == COMP_DERIVED)
2675 ns = gfc_current_block ()->f2k_derived;
2676 else
2678 /* Find the namespace of the scoping unit:
2679 If we're in a BLOCK construct, jump to the parent namespace. */
2680 ns = gfc_current_ns;
2681 while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
2682 ns = ns->parent;
2685 /* First see if the label is already in this namespace. */
2686 lp = ns->st_labels;
2687 while (lp)
2689 if (lp->value == labelno)
2690 return lp;
2692 if (lp->value < labelno)
2693 lp = lp->left;
2694 else
2695 lp = lp->right;
2698 lp = XCNEW (gfc_st_label);
2700 lp->value = labelno;
2701 lp->defined = ST_LABEL_UNKNOWN;
2702 lp->referenced = ST_LABEL_UNKNOWN;
2703 lp->ns = ns;
2705 gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
2707 return lp;
2711 /* Called when a statement with a statement label is about to be
2712 accepted. We add the label to the list of the current namespace,
2713 making sure it hasn't been defined previously and referenced
2714 correctly. */
2716 void
2717 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
2719 int labelno;
2721 labelno = lp->value;
2723 if (lp->defined != ST_LABEL_UNKNOWN)
2724 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
2725 &lp->where, label_locus);
2726 else
2728 lp->where = *label_locus;
2730 switch (type)
2732 case ST_LABEL_FORMAT:
2733 if (lp->referenced == ST_LABEL_TARGET
2734 || lp->referenced == ST_LABEL_DO_TARGET)
2735 gfc_error ("Label %d at %C already referenced as branch target",
2736 labelno);
2737 else
2738 lp->defined = ST_LABEL_FORMAT;
2740 break;
2742 case ST_LABEL_TARGET:
2743 case ST_LABEL_DO_TARGET:
2744 if (lp->referenced == ST_LABEL_FORMAT)
2745 gfc_error ("Label %d at %C already referenced as a format label",
2746 labelno);
2747 else
2748 lp->defined = type;
2750 if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
2751 && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
2752 "DO termination statement which is not END DO"
2753 " or CONTINUE with label %d at %C", labelno))
2754 return;
2755 break;
2757 default:
2758 lp->defined = ST_LABEL_BAD_TARGET;
2759 lp->referenced = ST_LABEL_BAD_TARGET;
2765 /* Reference a label. Given a label and its type, see if that
2766 reference is consistent with what is known about that label,
2767 updating the unknown state. Returns false if something goes
2768 wrong. */
2770 bool
2771 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2773 gfc_sl_type label_type;
2774 int labelno;
2775 bool rc;
2777 if (lp == NULL)
2778 return true;
2780 labelno = lp->value;
2782 if (lp->defined != ST_LABEL_UNKNOWN)
2783 label_type = lp->defined;
2784 else
2786 label_type = lp->referenced;
2787 lp->where = gfc_current_locus;
2790 if (label_type == ST_LABEL_FORMAT
2791 && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
2793 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2794 rc = false;
2795 goto done;
2798 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
2799 || label_type == ST_LABEL_BAD_TARGET)
2800 && type == ST_LABEL_FORMAT)
2802 gfc_error ("Label %d at %C previously used as branch target", labelno);
2803 rc = false;
2804 goto done;
2807 if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
2808 && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
2809 "Shared DO termination label %d at %C", labelno))
2810 return false;
2812 if (type == ST_LABEL_DO_TARGET
2813 && !gfc_notify_std (GFC_STD_F2018_OBS, "Labeled DO statement "
2814 "at %L", &gfc_current_locus))
2815 return false;
2817 if (lp->referenced != ST_LABEL_DO_TARGET)
2818 lp->referenced = type;
2819 rc = true;
2821 done:
2822 return rc;
2826 /************** Symbol table management subroutines ****************/
2828 /* Basic details: Fortran 95 requires a potentially unlimited number
2829 of distinct namespaces when compiling a program unit. This case
2830 occurs during a compilation of internal subprograms because all of
2831 the internal subprograms must be read before we can start
2832 generating code for the host.
2834 Given the tricky nature of the Fortran grammar, we must be able to
2835 undo changes made to a symbol table if the current interpretation
2836 of a statement is found to be incorrect. Whenever a symbol is
2837 looked up, we make a copy of it and link to it. All of these
2838 symbols are kept in a vector so that we can commit or
2839 undo the changes at a later time.
2841 A symtree may point to a symbol node outside of its namespace. In
2842 this case, that symbol has been used as a host associated variable
2843 at some previous time. */
2845 /* Allocate a new namespace structure. Copies the implicit types from
2846 PARENT if PARENT_TYPES is set. */
2848 gfc_namespace *
2849 gfc_get_namespace (gfc_namespace *parent, int parent_types)
2851 gfc_namespace *ns;
2852 gfc_typespec *ts;
2853 int in;
2854 int i;
2856 ns = XCNEW (gfc_namespace);
2857 ns->sym_root = NULL;
2858 ns->uop_root = NULL;
2859 ns->tb_sym_root = NULL;
2860 ns->finalizers = NULL;
2861 ns->default_access = ACCESS_UNKNOWN;
2862 ns->parent = parent;
2864 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
2866 ns->operator_access[in] = ACCESS_UNKNOWN;
2867 ns->tb_op[in] = NULL;
2870 /* Initialize default implicit types. */
2871 for (i = 'a'; i <= 'z'; i++)
2873 ns->set_flag[i - 'a'] = 0;
2874 ts = &ns->default_type[i - 'a'];
2876 if (parent_types && ns->parent != NULL)
2878 /* Copy parent settings. */
2879 *ts = ns->parent->default_type[i - 'a'];
2880 continue;
2883 if (flag_implicit_none != 0)
2885 gfc_clear_ts (ts);
2886 continue;
2889 if ('i' <= i && i <= 'n')
2891 ts->type = BT_INTEGER;
2892 ts->kind = gfc_default_integer_kind;
2894 else
2896 ts->type = BT_REAL;
2897 ts->kind = gfc_default_real_kind;
2901 if (parent_types && ns->parent != NULL)
2902 ns->has_implicit_none_export = ns->parent->has_implicit_none_export;
2904 ns->refs = 1;
2906 return ns;
2910 /* Comparison function for symtree nodes. */
2912 static int
2913 compare_symtree (void *_st1, void *_st2)
2915 gfc_symtree *st1, *st2;
2917 st1 = (gfc_symtree *) _st1;
2918 st2 = (gfc_symtree *) _st2;
2920 return strcmp (st1->name, st2->name);
2924 /* Allocate a new symtree node and associate it with the new symbol. */
2926 gfc_symtree *
2927 gfc_new_symtree (gfc_symtree **root, const char *name)
2929 gfc_symtree *st;
2931 st = XCNEW (gfc_symtree);
2932 st->name = gfc_get_string ("%s", name);
2934 gfc_insert_bbt (root, st, compare_symtree);
2935 return st;
2939 /* Delete a symbol from the tree. Does not free the symbol itself! */
2941 void
2942 gfc_delete_symtree (gfc_symtree **root, const char *name)
2944 gfc_symtree st, *st0;
2945 const char *p;
2947 /* Submodules are marked as mod.submod. When freeing a submodule
2948 symbol, the symtree only has "submod", so adjust that here. */
2950 p = strrchr(name, '.');
2951 if (p)
2952 p++;
2953 else
2954 p = name;
2956 st0 = gfc_find_symtree (*root, p);
2958 st.name = gfc_get_string ("%s", p);
2959 gfc_delete_bbt (root, &st, compare_symtree);
2961 free (st0);
2965 /* Given a root symtree node and a name, try to find the symbol within
2966 the namespace. Returns NULL if the symbol is not found. */
2968 gfc_symtree *
2969 gfc_find_symtree (gfc_symtree *st, const char *name)
2971 int c;
2973 while (st != NULL)
2975 c = strcmp (name, st->name);
2976 if (c == 0)
2977 return st;
2979 st = (c < 0) ? st->left : st->right;
2982 return NULL;
2986 /* Return a symtree node with a name that is guaranteed to be unique
2987 within the namespace and corresponds to an illegal fortran name. */
2989 gfc_symtree *
2990 gfc_get_unique_symtree (gfc_namespace *ns)
2992 char name[GFC_MAX_SYMBOL_LEN + 1];
2993 static int serial = 0;
2995 sprintf (name, "@%d", serial++);
2996 return gfc_new_symtree (&ns->sym_root, name);
3000 /* Given a name find a user operator node, creating it if it doesn't
3001 exist. These are much simpler than symbols because they can't be
3002 ambiguous with one another. */
3004 gfc_user_op *
3005 gfc_get_uop (const char *name)
3007 gfc_user_op *uop;
3008 gfc_symtree *st;
3009 gfc_namespace *ns = gfc_current_ns;
3011 if (ns->omp_udr_ns)
3012 ns = ns->parent;
3013 st = gfc_find_symtree (ns->uop_root, name);
3014 if (st != NULL)
3015 return st->n.uop;
3017 st = gfc_new_symtree (&ns->uop_root, name);
3019 uop = st->n.uop = XCNEW (gfc_user_op);
3020 uop->name = gfc_get_string ("%s", name);
3021 uop->access = ACCESS_UNKNOWN;
3022 uop->ns = ns;
3024 return uop;
3028 /* Given a name find the user operator node. Returns NULL if it does
3029 not exist. */
3031 gfc_user_op *
3032 gfc_find_uop (const char *name, gfc_namespace *ns)
3034 gfc_symtree *st;
3036 if (ns == NULL)
3037 ns = gfc_current_ns;
3039 st = gfc_find_symtree (ns->uop_root, name);
3040 return (st == NULL) ? NULL : st->n.uop;
3044 /* Update a symbol's common_block field, and take care of the associated
3045 memory management. */
3047 static void
3048 set_symbol_common_block (gfc_symbol *sym, gfc_common_head *common_block)
3050 if (sym->common_block == common_block)
3051 return;
3053 if (sym->common_block && sym->common_block->name[0] != '\0')
3055 sym->common_block->refs--;
3056 if (sym->common_block->refs == 0)
3057 free (sym->common_block);
3059 sym->common_block = common_block;
3063 /* Remove a gfc_symbol structure and everything it points to. */
3065 void
3066 gfc_free_symbol (gfc_symbol *sym)
3069 if (sym == NULL)
3070 return;
3072 gfc_free_array_spec (sym->as);
3074 free_components (sym->components);
3076 gfc_free_expr (sym->value);
3078 gfc_free_namelist (sym->namelist);
3080 if (sym->ns != sym->formal_ns)
3081 gfc_free_namespace (sym->formal_ns);
3083 if (!sym->attr.generic_copy)
3084 gfc_free_interface (sym->generic);
3086 gfc_free_formal_arglist (sym->formal);
3088 gfc_free_namespace (sym->f2k_derived);
3090 set_symbol_common_block (sym, NULL);
3092 if (sym->param_list)
3093 gfc_free_actual_arglist (sym->param_list);
3095 free (sym);
3099 /* Decrease the reference counter and free memory when we reach zero. */
3101 void
3102 gfc_release_symbol (gfc_symbol *sym)
3104 if (sym == NULL)
3105 return;
3107 if (sym->formal_ns != NULL && sym->refs == 2 && sym->formal_ns != sym->ns
3108 && (!sym->attr.entry || !sym->module))
3110 /* As formal_ns contains a reference to sym, delete formal_ns just
3111 before the deletion of sym. */
3112 gfc_namespace *ns = sym->formal_ns;
3113 sym->formal_ns = NULL;
3114 gfc_free_namespace (ns);
3117 sym->refs--;
3118 if (sym->refs > 0)
3119 return;
3121 gcc_assert (sym->refs == 0);
3122 gfc_free_symbol (sym);
3126 /* Allocate and initialize a new symbol node. */
3128 gfc_symbol *
3129 gfc_new_symbol (const char *name, gfc_namespace *ns)
3131 gfc_symbol *p;
3133 p = XCNEW (gfc_symbol);
3135 gfc_clear_ts (&p->ts);
3136 gfc_clear_attr (&p->attr);
3137 p->ns = ns;
3139 p->declared_at = gfc_current_locus;
3141 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
3142 gfc_internal_error ("new_symbol(): Symbol name too long");
3144 p->name = gfc_get_string ("%s", name);
3146 /* Make sure flags for symbol being C bound are clear initially. */
3147 p->attr.is_bind_c = 0;
3148 p->attr.is_iso_c = 0;
3150 /* Clear the ptrs we may need. */
3151 p->common_block = NULL;
3152 p->f2k_derived = NULL;
3153 p->assoc = NULL;
3154 p->dt_next = NULL;
3155 p->fn_result_spec = 0;
3157 return p;
3161 /* Generate an error if a symbol is ambiguous. */
3163 static void
3164 ambiguous_symbol (const char *name, gfc_symtree *st)
3167 if (st->n.sym->module)
3168 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
3169 "from module %qs", name, st->n.sym->name, st->n.sym->module);
3170 else
3171 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
3172 "from current program unit", name, st->n.sym->name);
3176 /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
3177 selector on the stack. If yes, replace it by the corresponding temporary. */
3179 static void
3180 select_type_insert_tmp (gfc_symtree **st)
3182 gfc_select_type_stack *stack = select_type_stack;
3183 for (; stack; stack = stack->prev)
3184 if ((*st)->n.sym == stack->selector && stack->tmp)
3186 *st = stack->tmp;
3187 select_type_insert_tmp (st);
3188 return;
3193 /* Look for a symtree in the current procedure -- that is, go up to
3194 parent namespaces but only if inside a BLOCK. Returns NULL if not found. */
3196 gfc_symtree*
3197 gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
3199 while (ns)
3201 gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
3202 if (st)
3203 return st;
3205 if (!ns->construct_entities)
3206 break;
3207 ns = ns->parent;
3210 return NULL;
3214 /* Search for a symtree starting in the current namespace, resorting to
3215 any parent namespaces if requested by a nonzero parent_flag.
3216 Returns nonzero if the name is ambiguous. */
3219 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
3220 gfc_symtree **result)
3222 gfc_symtree *st;
3224 if (ns == NULL)
3225 ns = gfc_current_ns;
3229 st = gfc_find_symtree (ns->sym_root, name);
3230 if (st != NULL)
3232 select_type_insert_tmp (&st);
3234 *result = st;
3235 /* Ambiguous generic interfaces are permitted, as long
3236 as the specific interfaces are different. */
3237 if (st->ambiguous && !st->n.sym->attr.generic)
3239 ambiguous_symbol (name, st);
3240 return 1;
3243 return 0;
3246 if (!parent_flag)
3247 break;
3249 /* Don't escape an interface block. */
3250 if (ns && !ns->has_import_set
3251 && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
3252 break;
3254 ns = ns->parent;
3256 while (ns != NULL);
3258 if (gfc_current_state() == COMP_DERIVED
3259 && gfc_current_block ()->attr.pdt_template)
3261 gfc_symbol *der = gfc_current_block ();
3262 for (; der; der = gfc_get_derived_super_type (der))
3264 if (der->f2k_derived && der->f2k_derived->sym_root)
3266 st = gfc_find_symtree (der->f2k_derived->sym_root, name);
3267 if (st)
3268 break;
3271 *result = st;
3272 return 0;
3275 *result = NULL;
3277 return 0;
3281 /* Same, but returns the symbol instead. */
3284 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
3285 gfc_symbol **result)
3287 gfc_symtree *st;
3288 int i;
3290 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
3292 if (st == NULL)
3293 *result = NULL;
3294 else
3295 *result = st->n.sym;
3297 return i;
3301 /* Tells whether there is only one set of changes in the stack. */
3303 static bool
3304 single_undo_checkpoint_p (void)
3306 if (latest_undo_chgset == &default_undo_chgset_var)
3308 gcc_assert (latest_undo_chgset->previous == NULL);
3309 return true;
3311 else
3313 gcc_assert (latest_undo_chgset->previous != NULL);
3314 return false;
3318 /* Save symbol with the information necessary to back it out. */
3320 void
3321 gfc_save_symbol_data (gfc_symbol *sym)
3323 gfc_symbol *s;
3324 unsigned i;
3326 if (!single_undo_checkpoint_p ())
3328 /* If there is more than one change set, look for the symbol in the
3329 current one. If it is found there, we can reuse it. */
3330 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3331 if (s == sym)
3333 gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
3334 return;
3337 else if (sym->gfc_new || sym->old_symbol != NULL)
3338 return;
3340 s = XCNEW (gfc_symbol);
3341 *s = *sym;
3342 sym->old_symbol = s;
3343 sym->gfc_new = 0;
3345 latest_undo_chgset->syms.safe_push (sym);
3349 /* Given a name, find a symbol, or create it if it does not exist yet
3350 in the current namespace. If the symbol is found we make sure that
3351 it's OK.
3353 The integer return code indicates
3354 0 All OK
3355 1 The symbol name was ambiguous
3356 2 The name meant to be established was already host associated.
3358 So if the return value is nonzero, then an error was issued. */
3361 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
3362 bool allow_subroutine)
3364 gfc_symtree *st;
3365 gfc_symbol *p;
3367 /* This doesn't usually happen during resolution. */
3368 if (ns == NULL)
3369 ns = gfc_current_ns;
3371 /* Try to find the symbol in ns. */
3372 st = gfc_find_symtree (ns->sym_root, name);
3374 if (st == NULL && ns->omp_udr_ns)
3376 ns = ns->parent;
3377 st = gfc_find_symtree (ns->sym_root, name);
3380 if (st == NULL)
3382 /* If not there, create a new symbol. */
3383 p = gfc_new_symbol (name, ns);
3385 /* Add to the list of tentative symbols. */
3386 p->old_symbol = NULL;
3387 p->mark = 1;
3388 p->gfc_new = 1;
3389 latest_undo_chgset->syms.safe_push (p);
3391 st = gfc_new_symtree (&ns->sym_root, name);
3392 st->n.sym = p;
3393 p->refs++;
3396 else
3398 /* Make sure the existing symbol is OK. Ambiguous
3399 generic interfaces are permitted, as long as the
3400 specific interfaces are different. */
3401 if (st->ambiguous && !st->n.sym->attr.generic)
3403 ambiguous_symbol (name, st);
3404 return 1;
3407 p = st->n.sym;
3408 if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
3409 && !(allow_subroutine && p->attr.subroutine)
3410 && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
3411 && (ns->has_import_set || p->attr.imported)))
3413 /* Symbol is from another namespace. */
3414 gfc_error ("Symbol %qs at %C has already been host associated",
3415 name);
3416 return 2;
3419 p->mark = 1;
3421 /* Copy in case this symbol is changed. */
3422 gfc_save_symbol_data (p);
3425 *result = st;
3426 return 0;
3431 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
3433 gfc_symtree *st;
3434 int i;
3436 i = gfc_get_sym_tree (name, ns, &st, false);
3437 if (i != 0)
3438 return i;
3440 if (st)
3441 *result = st->n.sym;
3442 else
3443 *result = NULL;
3444 return i;
3448 /* Subroutine that searches for a symbol, creating it if it doesn't
3449 exist, but tries to host-associate the symbol if possible. */
3452 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
3454 gfc_symtree *st;
3455 int i;
3457 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
3459 if (st != NULL)
3461 gfc_save_symbol_data (st->n.sym);
3462 *result = st;
3463 return i;
3466 i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
3467 if (i)
3468 return i;
3470 if (st != NULL)
3472 *result = st;
3473 return 0;
3476 return gfc_get_sym_tree (name, gfc_current_ns, result, false);
3481 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
3483 int i;
3484 gfc_symtree *st;
3486 i = gfc_get_ha_sym_tree (name, &st);
3488 if (st)
3489 *result = st->n.sym;
3490 else
3491 *result = NULL;
3493 return i;
3497 /* Search for the symtree belonging to a gfc_common_head; we cannot use
3498 head->name as the common_root symtree's name might be mangled. */
3500 static gfc_symtree *
3501 find_common_symtree (gfc_symtree *st, gfc_common_head *head)
3504 gfc_symtree *result;
3506 if (st == NULL)
3507 return NULL;
3509 if (st->n.common == head)
3510 return st;
3512 result = find_common_symtree (st->left, head);
3513 if (!result)
3514 result = find_common_symtree (st->right, head);
3516 return result;
3520 /* Restore previous state of symbol. Just copy simple stuff. */
3522 static void
3523 restore_old_symbol (gfc_symbol *p)
3525 gfc_symbol *old;
3527 p->mark = 0;
3528 old = p->old_symbol;
3530 p->ts.type = old->ts.type;
3531 p->ts.kind = old->ts.kind;
3533 p->attr = old->attr;
3535 if (p->value != old->value)
3537 gcc_checking_assert (old->value == NULL);
3538 gfc_free_expr (p->value);
3539 p->value = NULL;
3542 if (p->as != old->as)
3544 if (p->as)
3545 gfc_free_array_spec (p->as);
3546 p->as = old->as;
3549 p->generic = old->generic;
3550 p->component_access = old->component_access;
3552 if (p->namelist != NULL && old->namelist == NULL)
3554 gfc_free_namelist (p->namelist);
3555 p->namelist = NULL;
3557 else
3559 if (p->namelist_tail != old->namelist_tail)
3561 gfc_free_namelist (old->namelist_tail->next);
3562 old->namelist_tail->next = NULL;
3566 p->namelist_tail = old->namelist_tail;
3568 if (p->formal != old->formal)
3570 gfc_free_formal_arglist (p->formal);
3571 p->formal = old->formal;
3574 set_symbol_common_block (p, old->common_block);
3575 p->common_head = old->common_head;
3577 p->old_symbol = old->old_symbol;
3578 free (old);
3582 /* Frees the internal data of a gfc_undo_change_set structure. Doesn't free
3583 the structure itself. */
3585 static void
3586 free_undo_change_set_data (gfc_undo_change_set &cs)
3588 cs.syms.release ();
3589 cs.tbps.release ();
3593 /* Given a change set pointer, free its target's contents and update it with
3594 the address of the previous change set. Note that only the contents are
3595 freed, not the target itself (the contents' container). It is not a problem
3596 as the latter will be a local variable usually. */
3598 static void
3599 pop_undo_change_set (gfc_undo_change_set *&cs)
3601 free_undo_change_set_data (*cs);
3602 cs = cs->previous;
3606 static void free_old_symbol (gfc_symbol *sym);
3609 /* Merges the current change set into the previous one. The changes themselves
3610 are left untouched; only one checkpoint is forgotten. */
3612 void
3613 gfc_drop_last_undo_checkpoint (void)
3615 gfc_symbol *s, *t;
3616 unsigned i, j;
3618 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3620 /* No need to loop in this case. */
3621 if (s->old_symbol == NULL)
3622 continue;
3624 /* Remove the duplicate symbols. */
3625 FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
3626 if (t == s)
3628 latest_undo_chgset->previous->syms.unordered_remove (j);
3630 /* S->OLD_SYMBOL is the backup symbol for S as it was at the
3631 last checkpoint. We drop that checkpoint, so S->OLD_SYMBOL
3632 shall contain from now on the backup symbol for S as it was
3633 at the checkpoint before. */
3634 if (s->old_symbol->gfc_new)
3636 gcc_assert (s->old_symbol->old_symbol == NULL);
3637 s->gfc_new = s->old_symbol->gfc_new;
3638 free_old_symbol (s);
3640 else
3641 restore_old_symbol (s->old_symbol);
3642 break;
3646 latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
3647 latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
3649 pop_undo_change_set (latest_undo_chgset);
3653 /* Undoes all the changes made to symbols since the previous checkpoint.
3654 This subroutine is made simpler due to the fact that attributes are
3655 never removed once added. */
3657 void
3658 gfc_restore_last_undo_checkpoint (void)
3660 gfc_symbol *p;
3661 unsigned i;
3663 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3665 /* Symbol in a common block was new. Or was old and just put in common */
3666 if (p->common_block
3667 && (p->gfc_new || !p->old_symbol->common_block))
3669 /* If the symbol was added to any common block, it
3670 needs to be removed to stop the resolver looking
3671 for a (possibly) dead symbol. */
3672 if (p->common_block->head == p && !p->common_next)
3674 gfc_symtree st, *st0;
3675 st0 = find_common_symtree (p->ns->common_root,
3676 p->common_block);
3677 if (st0)
3679 st.name = st0->name;
3680 gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
3681 free (st0);
3685 if (p->common_block->head == p)
3686 p->common_block->head = p->common_next;
3687 else
3689 gfc_symbol *cparent, *csym;
3691 cparent = p->common_block->head;
3692 csym = cparent->common_next;
3694 while (csym != p)
3696 cparent = csym;
3697 csym = csym->common_next;
3700 gcc_assert(cparent->common_next == p);
3701 cparent->common_next = csym->common_next;
3703 p->common_next = NULL;
3705 if (p->gfc_new)
3707 /* The derived type is saved in the symtree with the first
3708 letter capitalized; the all lower-case version to the
3709 derived type contains its associated generic function. */
3710 if (gfc_fl_struct (p->attr.flavor))
3711 gfc_delete_symtree (&p->ns->sym_root,gfc_dt_upper_string (p->name));
3712 else
3713 gfc_delete_symtree (&p->ns->sym_root, p->name);
3715 gfc_release_symbol (p);
3717 else
3718 restore_old_symbol (p);
3721 latest_undo_chgset->syms.truncate (0);
3722 latest_undo_chgset->tbps.truncate (0);
3724 if (!single_undo_checkpoint_p ())
3725 pop_undo_change_set (latest_undo_chgset);
3729 /* Makes sure that there is only one set of changes; in other words we haven't
3730 forgotten to pair a call to gfc_new_checkpoint with a call to either
3731 gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint. */
3733 static void
3734 enforce_single_undo_checkpoint (void)
3736 gcc_checking_assert (single_undo_checkpoint_p ());
3740 /* Undoes all the changes made to symbols in the current statement. */
3742 void
3743 gfc_undo_symbols (void)
3745 enforce_single_undo_checkpoint ();
3746 gfc_restore_last_undo_checkpoint ();
3750 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
3751 components of old_symbol that might need deallocation are the "allocatables"
3752 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
3753 namelist_tail. In case these differ between old_symbol and sym, it's just
3754 because sym->namelist has gotten a few more items. */
3756 static void
3757 free_old_symbol (gfc_symbol *sym)
3760 if (sym->old_symbol == NULL)
3761 return;
3763 if (sym->old_symbol->as != sym->as)
3764 gfc_free_array_spec (sym->old_symbol->as);
3766 if (sym->old_symbol->value != sym->value)
3767 gfc_free_expr (sym->old_symbol->value);
3769 if (sym->old_symbol->formal != sym->formal)
3770 gfc_free_formal_arglist (sym->old_symbol->formal);
3772 free (sym->old_symbol);
3773 sym->old_symbol = NULL;
3777 /* Makes the changes made in the current statement permanent-- gets
3778 rid of undo information. */
3780 void
3781 gfc_commit_symbols (void)
3783 gfc_symbol *p;
3784 gfc_typebound_proc *tbp;
3785 unsigned i;
3787 enforce_single_undo_checkpoint ();
3789 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3791 p->mark = 0;
3792 p->gfc_new = 0;
3793 free_old_symbol (p);
3795 latest_undo_chgset->syms.truncate (0);
3797 FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
3798 tbp->error = 0;
3799 latest_undo_chgset->tbps.truncate (0);
3803 /* Makes the changes made in one symbol permanent -- gets rid of undo
3804 information. */
3806 void
3807 gfc_commit_symbol (gfc_symbol *sym)
3809 gfc_symbol *p;
3810 unsigned i;
3812 enforce_single_undo_checkpoint ();
3814 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3815 if (p == sym)
3817 latest_undo_chgset->syms.unordered_remove (i);
3818 break;
3821 sym->mark = 0;
3822 sym->gfc_new = 0;
3824 free_old_symbol (sym);
3828 /* Recursively free trees containing type-bound procedures. */
3830 static void
3831 free_tb_tree (gfc_symtree *t)
3833 if (t == NULL)
3834 return;
3836 free_tb_tree (t->left);
3837 free_tb_tree (t->right);
3839 /* TODO: Free type-bound procedure structs themselves; probably needs some
3840 sort of ref-counting mechanism. */
3842 free (t);
3846 /* Recursive function that deletes an entire tree and all the common
3847 head structures it points to. */
3849 static void
3850 free_common_tree (gfc_symtree * common_tree)
3852 if (common_tree == NULL)
3853 return;
3855 free_common_tree (common_tree->left);
3856 free_common_tree (common_tree->right);
3858 free (common_tree);
3862 /* Recursive function that deletes an entire tree and all the common
3863 head structures it points to. */
3865 static void
3866 free_omp_udr_tree (gfc_symtree * omp_udr_tree)
3868 if (omp_udr_tree == NULL)
3869 return;
3871 free_omp_udr_tree (omp_udr_tree->left);
3872 free_omp_udr_tree (omp_udr_tree->right);
3874 gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
3875 free (omp_udr_tree);
3879 /* Recursive function that deletes an entire tree and all the user
3880 operator nodes that it contains. */
3882 static void
3883 free_uop_tree (gfc_symtree *uop_tree)
3885 if (uop_tree == NULL)
3886 return;
3888 free_uop_tree (uop_tree->left);
3889 free_uop_tree (uop_tree->right);
3891 gfc_free_interface (uop_tree->n.uop->op);
3892 free (uop_tree->n.uop);
3893 free (uop_tree);
3897 /* Recursive function that deletes an entire tree and all the symbols
3898 that it contains. */
3900 static void
3901 free_sym_tree (gfc_symtree *sym_tree)
3903 if (sym_tree == NULL)
3904 return;
3906 free_sym_tree (sym_tree->left);
3907 free_sym_tree (sym_tree->right);
3909 gfc_release_symbol (sym_tree->n.sym);
3910 free (sym_tree);
3914 /* Free the gfc_equiv_info's. */
3916 static void
3917 gfc_free_equiv_infos (gfc_equiv_info *s)
3919 if (s == NULL)
3920 return;
3921 gfc_free_equiv_infos (s->next);
3922 free (s);
3926 /* Free the gfc_equiv_lists. */
3928 static void
3929 gfc_free_equiv_lists (gfc_equiv_list *l)
3931 if (l == NULL)
3932 return;
3933 gfc_free_equiv_lists (l->next);
3934 gfc_free_equiv_infos (l->equiv);
3935 free (l);
3939 /* Free a finalizer procedure list. */
3941 void
3942 gfc_free_finalizer (gfc_finalizer* el)
3944 if (el)
3946 gfc_release_symbol (el->proc_sym);
3947 free (el);
3951 static void
3952 gfc_free_finalizer_list (gfc_finalizer* list)
3954 while (list)
3956 gfc_finalizer* current = list;
3957 list = list->next;
3958 gfc_free_finalizer (current);
3963 /* Create a new gfc_charlen structure and add it to a namespace.
3964 If 'old_cl' is given, the newly created charlen will be a copy of it. */
3966 gfc_charlen*
3967 gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
3969 gfc_charlen *cl;
3971 cl = gfc_get_charlen ();
3973 /* Copy old_cl. */
3974 if (old_cl)
3976 cl->length = gfc_copy_expr (old_cl->length);
3977 cl->length_from_typespec = old_cl->length_from_typespec;
3978 cl->backend_decl = old_cl->backend_decl;
3979 cl->passed_length = old_cl->passed_length;
3980 cl->resolved = old_cl->resolved;
3983 /* Put into namespace. */
3984 cl->next = ns->cl_list;
3985 ns->cl_list = cl;
3987 return cl;
3991 /* Free the charlen list from cl to end (end is not freed).
3992 Free the whole list if end is NULL. */
3994 void
3995 gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
3997 gfc_charlen *cl2;
3999 for (; cl != end; cl = cl2)
4001 gcc_assert (cl);
4003 cl2 = cl->next;
4004 gfc_free_expr (cl->length);
4005 free (cl);
4010 /* Free entry list structs. */
4012 static void
4013 free_entry_list (gfc_entry_list *el)
4015 gfc_entry_list *next;
4017 if (el == NULL)
4018 return;
4020 next = el->next;
4021 free (el);
4022 free_entry_list (next);
4026 /* Free a namespace structure and everything below it. Interface
4027 lists associated with intrinsic operators are not freed. These are
4028 taken care of when a specific name is freed. */
4030 void
4031 gfc_free_namespace (gfc_namespace *ns)
4033 gfc_namespace *p, *q;
4034 int i;
4036 if (ns == NULL)
4037 return;
4039 ns->refs--;
4040 if (ns->refs > 0)
4041 return;
4043 gcc_assert (ns->refs == 0);
4045 gfc_free_statements (ns->code);
4047 free_sym_tree (ns->sym_root);
4048 free_uop_tree (ns->uop_root);
4049 free_common_tree (ns->common_root);
4050 free_omp_udr_tree (ns->omp_udr_root);
4051 free_tb_tree (ns->tb_sym_root);
4052 free_tb_tree (ns->tb_uop_root);
4053 gfc_free_finalizer_list (ns->finalizers);
4054 gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
4055 gfc_free_charlen (ns->cl_list, NULL);
4056 free_st_labels (ns->st_labels);
4058 free_entry_list (ns->entries);
4059 gfc_free_equiv (ns->equiv);
4060 gfc_free_equiv_lists (ns->equiv_lists);
4061 gfc_free_use_stmts (ns->use_stmts);
4063 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
4064 gfc_free_interface (ns->op[i]);
4066 gfc_free_data (ns->data);
4067 p = ns->contained;
4068 free (ns);
4070 /* Recursively free any contained namespaces. */
4071 while (p != NULL)
4073 q = p;
4074 p = p->sibling;
4075 gfc_free_namespace (q);
4080 void
4081 gfc_symbol_init_2 (void)
4084 gfc_current_ns = gfc_get_namespace (NULL, 0);
4088 void
4089 gfc_symbol_done_2 (void)
4091 if (gfc_current_ns != NULL)
4093 /* free everything from the root. */
4094 while (gfc_current_ns->parent != NULL)
4095 gfc_current_ns = gfc_current_ns->parent;
4096 gfc_free_namespace (gfc_current_ns);
4097 gfc_current_ns = NULL;
4099 gfc_derived_types = NULL;
4101 enforce_single_undo_checkpoint ();
4102 free_undo_change_set_data (*latest_undo_chgset);
4106 /* Count how many nodes a symtree has. */
4108 static unsigned
4109 count_st_nodes (const gfc_symtree *st)
4111 unsigned nodes;
4112 if (!st)
4113 return 0;
4115 nodes = count_st_nodes (st->left);
4116 nodes++;
4117 nodes += count_st_nodes (st->right);
4119 return nodes;
4123 /* Convert symtree tree into symtree vector. */
4125 static unsigned
4126 fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
4128 if (!st)
4129 return node_cntr;
4131 node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
4132 st_vec[node_cntr++] = st;
4133 node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
4135 return node_cntr;
4139 /* Traverse namespace. As the functions might modify the symtree, we store the
4140 symtree as a vector and operate on this vector. Note: We assume that
4141 sym_func or st_func never deletes nodes from the symtree - only adding is
4142 allowed. Additionally, newly added nodes are not traversed. */
4144 static void
4145 do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
4146 void (*sym_func) (gfc_symbol *))
4148 gfc_symtree **st_vec;
4149 unsigned nodes, i, node_cntr;
4151 gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
4152 nodes = count_st_nodes (st);
4153 st_vec = XALLOCAVEC (gfc_symtree *, nodes);
4154 node_cntr = 0;
4155 fill_st_vector (st, st_vec, node_cntr);
4157 if (sym_func)
4159 /* Clear marks. */
4160 for (i = 0; i < nodes; i++)
4161 st_vec[i]->n.sym->mark = 0;
4162 for (i = 0; i < nodes; i++)
4163 if (!st_vec[i]->n.sym->mark)
4165 (*sym_func) (st_vec[i]->n.sym);
4166 st_vec[i]->n.sym->mark = 1;
4169 else
4170 for (i = 0; i < nodes; i++)
4171 (*st_func) (st_vec[i]);
4175 /* Recursively traverse the symtree nodes. */
4177 void
4178 gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
4180 do_traverse_symtree (st, st_func, NULL);
4184 /* Call a given function for all symbols in the namespace. We take
4185 care that each gfc_symbol node is called exactly once. */
4187 void
4188 gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
4190 do_traverse_symtree (ns->sym_root, NULL, sym_func);
4194 /* Return TRUE when name is the name of an intrinsic type. */
4196 bool
4197 gfc_is_intrinsic_typename (const char *name)
4199 if (strcmp (name, "integer") == 0
4200 || strcmp (name, "real") == 0
4201 || strcmp (name, "character") == 0
4202 || strcmp (name, "logical") == 0
4203 || strcmp (name, "complex") == 0
4204 || strcmp (name, "doubleprecision") == 0
4205 || strcmp (name, "doublecomplex") == 0)
4206 return true;
4207 else
4208 return false;
4212 /* Return TRUE if the symbol is an automatic variable. */
4214 static bool
4215 gfc_is_var_automatic (gfc_symbol *sym)
4217 /* Pointer and allocatable variables are never automatic. */
4218 if (sym->attr.pointer || sym->attr.allocatable)
4219 return false;
4220 /* Check for arrays with non-constant size. */
4221 if (sym->attr.dimension && sym->as
4222 && !gfc_is_compile_time_shape (sym->as))
4223 return true;
4224 /* Check for non-constant length character variables. */
4225 if (sym->ts.type == BT_CHARACTER
4226 && sym->ts.u.cl
4227 && !gfc_is_constant_expr (sym->ts.u.cl->length))
4228 return true;
4229 /* Variables with explicit AUTOMATIC attribute. */
4230 if (sym->attr.automatic)
4231 return true;
4233 return false;
4236 /* Given a symbol, mark it as SAVEd if it is allowed. */
4238 static void
4239 save_symbol (gfc_symbol *sym)
4242 if (sym->attr.use_assoc)
4243 return;
4245 if (sym->attr.in_common
4246 || sym->attr.dummy
4247 || sym->attr.result
4248 || sym->attr.flavor != FL_VARIABLE)
4249 return;
4250 /* Automatic objects are not saved. */
4251 if (gfc_is_var_automatic (sym))
4252 return;
4253 gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
4257 /* Mark those symbols which can be SAVEd as such. */
4259 void
4260 gfc_save_all (gfc_namespace *ns)
4262 gfc_traverse_ns (ns, save_symbol);
4266 /* Make sure that no changes to symbols are pending. */
4268 void
4269 gfc_enforce_clean_symbol_state(void)
4271 enforce_single_undo_checkpoint ();
4272 gcc_assert (latest_undo_chgset->syms.is_empty ());
4276 /************** Global symbol handling ************/
4279 /* Search a tree for the global symbol. */
4281 gfc_gsymbol *
4282 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
4284 int c;
4286 if (symbol == NULL)
4287 return NULL;
4289 while (symbol)
4291 c = strcmp (name, symbol->name);
4292 if (!c)
4293 return symbol;
4295 symbol = (c < 0) ? symbol->left : symbol->right;
4298 return NULL;
4302 /* Case insensitive search a tree for the global symbol. */
4304 gfc_gsymbol *
4305 gfc_find_case_gsymbol (gfc_gsymbol *symbol, const char *name)
4307 int c;
4309 if (symbol == NULL)
4310 return NULL;
4312 while (symbol)
4314 c = strcasecmp (name, symbol->name);
4315 if (!c)
4316 return symbol;
4318 symbol = (c < 0) ? symbol->left : symbol->right;
4321 return NULL;
4325 /* Compare two global symbols. Used for managing the BB tree. */
4327 static int
4328 gsym_compare (void *_s1, void *_s2)
4330 gfc_gsymbol *s1, *s2;
4332 s1 = (gfc_gsymbol *) _s1;
4333 s2 = (gfc_gsymbol *) _s2;
4334 return strcmp (s1->name, s2->name);
4338 /* Get a global symbol, creating it if it doesn't exist. */
4340 gfc_gsymbol *
4341 gfc_get_gsymbol (const char *name, bool bind_c)
4343 gfc_gsymbol *s;
4345 s = gfc_find_gsymbol (gfc_gsym_root, name);
4346 if (s != NULL)
4347 return s;
4349 s = XCNEW (gfc_gsymbol);
4350 s->type = GSYM_UNKNOWN;
4351 s->name = gfc_get_string ("%s", name);
4352 s->bind_c = bind_c;
4354 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
4356 return s;
4360 static gfc_symbol *
4361 get_iso_c_binding_dt (int sym_id)
4363 gfc_symbol *dt_list = gfc_derived_types;
4365 /* Loop through the derived types in the name list, searching for
4366 the desired symbol from iso_c_binding. Search the parent namespaces
4367 if necessary and requested to (parent_flag). */
4368 if (dt_list)
4370 while (dt_list->dt_next != gfc_derived_types)
4372 if (dt_list->from_intmod != INTMOD_NONE
4373 && dt_list->intmod_sym_id == sym_id)
4374 return dt_list;
4376 dt_list = dt_list->dt_next;
4380 return NULL;
4384 /* Verifies that the given derived type symbol, derived_sym, is interoperable
4385 with C. This is necessary for any derived type that is BIND(C) and for
4386 derived types that are parameters to functions that are BIND(C). All
4387 fields of the derived type are required to be interoperable, and are tested
4388 for such. If an error occurs, the errors are reported here, allowing for
4389 multiple errors to be handled for a single derived type. */
4391 bool
4392 verify_bind_c_derived_type (gfc_symbol *derived_sym)
4394 gfc_component *curr_comp = NULL;
4395 bool is_c_interop = false;
4396 bool retval = true;
4398 if (derived_sym == NULL)
4399 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
4400 "unexpectedly NULL");
4402 /* If we've already looked at this derived symbol, do not look at it again
4403 so we don't repeat warnings/errors. */
4404 if (derived_sym->ts.is_c_interop)
4405 return true;
4407 /* The derived type must have the BIND attribute to be interoperable
4408 J3/04-007, Section 15.2.3. */
4409 if (derived_sym->attr.is_bind_c != 1)
4411 derived_sym->ts.is_c_interop = 0;
4412 gfc_error_now ("Derived type %qs declared at %L must have the BIND "
4413 "attribute to be C interoperable", derived_sym->name,
4414 &(derived_sym->declared_at));
4415 retval = false;
4418 curr_comp = derived_sym->components;
4420 /* Fortran 2003 allows an empty derived type. C99 appears to disallow an
4421 empty struct. Section 15.2 in Fortran 2003 states: "The following
4422 subclauses define the conditions under which a Fortran entity is
4423 interoperable. If a Fortran entity is interoperable, an equivalent
4424 entity may be defined by means of C and the Fortran entity is said
4425 to be interoperable with the C entity. There does not have to be such
4426 an interoperating C entity."
4428 if (curr_comp == NULL)
4430 gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L is empty, "
4431 "and may be inaccessible by the C companion processor",
4432 derived_sym->name, &(derived_sym->declared_at));
4433 derived_sym->ts.is_c_interop = 1;
4434 derived_sym->attr.is_bind_c = 1;
4435 return true;
4439 /* Initialize the derived type as being C interoperable.
4440 If we find an error in the components, this will be set false. */
4441 derived_sym->ts.is_c_interop = 1;
4443 /* Loop through the list of components to verify that the kind of
4444 each is a C interoperable type. */
4447 /* The components cannot be pointers (fortran sense).
4448 J3/04-007, Section 15.2.3, C1505. */
4449 if (curr_comp->attr.pointer != 0)
4451 gfc_error ("Component %qs at %L cannot have the "
4452 "POINTER attribute because it is a member "
4453 "of the BIND(C) derived type %qs at %L",
4454 curr_comp->name, &(curr_comp->loc),
4455 derived_sym->name, &(derived_sym->declared_at));
4456 retval = false;
4459 if (curr_comp->attr.proc_pointer != 0)
4461 gfc_error ("Procedure pointer component %qs at %L cannot be a member"
4462 " of the BIND(C) derived type %qs at %L", curr_comp->name,
4463 &curr_comp->loc, derived_sym->name,
4464 &derived_sym->declared_at);
4465 retval = false;
4468 /* The components cannot be allocatable.
4469 J3/04-007, Section 15.2.3, C1505. */
4470 if (curr_comp->attr.allocatable != 0)
4472 gfc_error ("Component %qs at %L cannot have the "
4473 "ALLOCATABLE attribute because it is a member "
4474 "of the BIND(C) derived type %qs at %L",
4475 curr_comp->name, &(curr_comp->loc),
4476 derived_sym->name, &(derived_sym->declared_at));
4477 retval = false;
4480 /* BIND(C) derived types must have interoperable components. */
4481 if (curr_comp->ts.type == BT_DERIVED
4482 && curr_comp->ts.u.derived->ts.is_iso_c != 1
4483 && curr_comp->ts.u.derived != derived_sym)
4485 /* This should be allowed; the draft says a derived-type cannot
4486 have type parameters if it is has the BIND attribute. Type
4487 parameters seem to be for making parameterized derived types.
4488 There's no need to verify the type if it is c_ptr/c_funptr. */
4489 retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
4491 else
4493 /* Grab the typespec for the given component and test the kind. */
4494 is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
4496 if (!is_c_interop)
4498 /* Report warning and continue since not fatal. The
4499 draft does specify a constraint that requires all fields
4500 to interoperate, but if the user says real(4), etc., it
4501 may interoperate with *something* in C, but the compiler
4502 most likely won't know exactly what. Further, it may not
4503 interoperate with the same data type(s) in C if the user
4504 recompiles with different flags (e.g., -m32 and -m64 on
4505 x86_64 and using integer(4) to claim interop with a
4506 C_LONG). */
4507 if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
4508 /* If the derived type is bind(c), all fields must be
4509 interop. */
4510 gfc_warning (OPT_Wc_binding_type,
4511 "Component %qs in derived type %qs at %L "
4512 "may not be C interoperable, even though "
4513 "derived type %qs is BIND(C)",
4514 curr_comp->name, derived_sym->name,
4515 &(curr_comp->loc), derived_sym->name);
4516 else if (warn_c_binding_type)
4517 /* If derived type is param to bind(c) routine, or to one
4518 of the iso_c_binding procs, it must be interoperable, so
4519 all fields must interop too. */
4520 gfc_warning (OPT_Wc_binding_type,
4521 "Component %qs in derived type %qs at %L "
4522 "may not be C interoperable",
4523 curr_comp->name, derived_sym->name,
4524 &(curr_comp->loc));
4528 curr_comp = curr_comp->next;
4529 } while (curr_comp != NULL);
4532 /* Make sure we don't have conflicts with the attributes. */
4533 if (derived_sym->attr.access == ACCESS_PRIVATE)
4535 gfc_error ("Derived type %qs at %L cannot be declared with both "
4536 "PRIVATE and BIND(C) attributes", derived_sym->name,
4537 &(derived_sym->declared_at));
4538 retval = false;
4541 if (derived_sym->attr.sequence != 0)
4543 gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
4544 "attribute because it is BIND(C)", derived_sym->name,
4545 &(derived_sym->declared_at));
4546 retval = false;
4549 /* Mark the derived type as not being C interoperable if we found an
4550 error. If there were only warnings, proceed with the assumption
4551 it's interoperable. */
4552 if (!retval)
4553 derived_sym->ts.is_c_interop = 0;
4555 return retval;
4559 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
4561 static bool
4562 gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
4564 gfc_constructor *c;
4566 gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
4567 dt_symtree->n.sym->attr.referenced = 1;
4569 tmp_sym->attr.is_c_interop = 1;
4570 tmp_sym->attr.is_bind_c = 1;
4571 tmp_sym->ts.is_c_interop = 1;
4572 tmp_sym->ts.is_iso_c = 1;
4573 tmp_sym->ts.type = BT_DERIVED;
4574 tmp_sym->ts.f90_type = BT_VOID;
4575 tmp_sym->attr.flavor = FL_PARAMETER;
4576 tmp_sym->ts.u.derived = dt_symtree->n.sym;
4578 /* Set the c_address field of c_null_ptr and c_null_funptr to
4579 the value of NULL. */
4580 tmp_sym->value = gfc_get_expr ();
4581 tmp_sym->value->expr_type = EXPR_STRUCTURE;
4582 tmp_sym->value->ts.type = BT_DERIVED;
4583 tmp_sym->value->ts.f90_type = BT_VOID;
4584 tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
4585 gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
4586 c = gfc_constructor_first (tmp_sym->value->value.constructor);
4587 c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
4588 c->expr->ts.is_iso_c = 1;
4590 return true;
4594 /* Add a formal argument, gfc_formal_arglist, to the
4595 end of the given list of arguments. Set the reference to the
4596 provided symbol, param_sym, in the argument. */
4598 static void
4599 add_formal_arg (gfc_formal_arglist **head,
4600 gfc_formal_arglist **tail,
4601 gfc_formal_arglist *formal_arg,
4602 gfc_symbol *param_sym)
4604 /* Put in list, either as first arg or at the tail (curr arg). */
4605 if (*head == NULL)
4606 *head = *tail = formal_arg;
4607 else
4609 (*tail)->next = formal_arg;
4610 (*tail) = formal_arg;
4613 (*tail)->sym = param_sym;
4614 (*tail)->next = NULL;
4616 return;
4620 /* Add a procedure interface to the given symbol (i.e., store a
4621 reference to the list of formal arguments). */
4623 static void
4624 add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
4627 sym->formal = formal;
4628 sym->attr.if_source = source;
4632 /* Copy the formal args from an existing symbol, src, into a new
4633 symbol, dest. New formal args are created, and the description of
4634 each arg is set according to the existing ones. This function is
4635 used when creating procedure declaration variables from a procedure
4636 declaration statement (see match_proc_decl()) to create the formal
4637 args based on the args of a given named interface.
4639 When an actual argument list is provided, skip the absent arguments.
4640 To be used together with gfc_se->ignore_optional. */
4642 void
4643 gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
4644 gfc_actual_arglist *actual)
4646 gfc_formal_arglist *head = NULL;
4647 gfc_formal_arglist *tail = NULL;
4648 gfc_formal_arglist *formal_arg = NULL;
4649 gfc_intrinsic_arg *curr_arg = NULL;
4650 gfc_formal_arglist *formal_prev = NULL;
4651 gfc_actual_arglist *act_arg = actual;
4652 /* Save current namespace so we can change it for formal args. */
4653 gfc_namespace *parent_ns = gfc_current_ns;
4655 /* Create a new namespace, which will be the formal ns (namespace
4656 of the formal args). */
4657 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
4658 gfc_current_ns->proc_name = dest;
4660 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4662 /* Skip absent arguments. */
4663 if (actual)
4665 gcc_assert (act_arg != NULL);
4666 if (act_arg->expr == NULL)
4668 act_arg = act_arg->next;
4669 continue;
4671 act_arg = act_arg->next;
4673 formal_arg = gfc_get_formal_arglist ();
4674 gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
4676 /* May need to copy more info for the symbol. */
4677 formal_arg->sym->ts = curr_arg->ts;
4678 formal_arg->sym->attr.optional = curr_arg->optional;
4679 formal_arg->sym->attr.value = curr_arg->value;
4680 formal_arg->sym->attr.intent = curr_arg->intent;
4681 formal_arg->sym->attr.flavor = FL_VARIABLE;
4682 formal_arg->sym->attr.dummy = 1;
4684 if (formal_arg->sym->ts.type == BT_CHARACTER)
4685 formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4687 /* If this isn't the first arg, set up the next ptr. For the
4688 last arg built, the formal_arg->next will never get set to
4689 anything other than NULL. */
4690 if (formal_prev != NULL)
4691 formal_prev->next = formal_arg;
4692 else
4693 formal_arg->next = NULL;
4695 formal_prev = formal_arg;
4697 /* Add arg to list of formal args. */
4698 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4700 /* Validate changes. */
4701 gfc_commit_symbol (formal_arg->sym);
4704 /* Add the interface to the symbol. */
4705 add_proc_interface (dest, IFSRC_DECL, head);
4707 /* Store the formal namespace information. */
4708 if (dest->formal != NULL)
4709 /* The current ns should be that for the dest proc. */
4710 dest->formal_ns = gfc_current_ns;
4711 /* Restore the current namespace to what it was on entry. */
4712 gfc_current_ns = parent_ns;
4716 static int
4717 std_for_isocbinding_symbol (int id)
4719 switch (id)
4721 #define NAMED_INTCST(a,b,c,d) \
4722 case a:\
4723 return d;
4724 #include "iso-c-binding.def"
4725 #undef NAMED_INTCST
4727 #define NAMED_FUNCTION(a,b,c,d) \
4728 case a:\
4729 return d;
4730 #define NAMED_SUBROUTINE(a,b,c,d) \
4731 case a:\
4732 return d;
4733 #include "iso-c-binding.def"
4734 #undef NAMED_FUNCTION
4735 #undef NAMED_SUBROUTINE
4737 default:
4738 return GFC_STD_F2003;
4742 /* Generate the given set of C interoperable kind objects, or all
4743 interoperable kinds. This function will only be given kind objects
4744 for valid iso_c_binding defined types because this is verified when
4745 the 'use' statement is parsed. If the user gives an 'only' clause,
4746 the specific kinds are looked up; if they don't exist, an error is
4747 reported. If the user does not give an 'only' clause, all
4748 iso_c_binding symbols are generated. If a list of specific kinds
4749 is given, it must have a NULL in the first empty spot to mark the
4750 end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
4751 point to the symtree for c_(fun)ptr. */
4753 gfc_symtree *
4754 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
4755 const char *local_name, gfc_symtree *dt_symtree,
4756 bool hidden)
4758 const char *const name = (local_name && local_name[0])
4759 ? local_name : c_interop_kinds_table[s].name;
4760 gfc_symtree *tmp_symtree;
4761 gfc_symbol *tmp_sym = NULL;
4762 int index;
4764 if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
4765 return NULL;
4767 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
4768 if (hidden
4769 && (!tmp_symtree || !tmp_symtree->n.sym
4770 || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
4771 || tmp_symtree->n.sym->intmod_sym_id != s))
4772 tmp_symtree = NULL;
4774 /* Already exists in this scope so don't re-add it. */
4775 if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
4776 && (!tmp_sym->attr.generic
4777 || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
4778 && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
4780 if (tmp_sym->attr.flavor == FL_DERIVED
4781 && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
4783 if (gfc_derived_types)
4785 tmp_sym->dt_next = gfc_derived_types->dt_next;
4786 gfc_derived_types->dt_next = tmp_sym;
4788 else
4790 tmp_sym->dt_next = tmp_sym;
4792 gfc_derived_types = tmp_sym;
4795 return tmp_symtree;
4798 /* Create the sym tree in the current ns. */
4799 if (hidden)
4801 tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
4802 tmp_sym = gfc_new_symbol (name, gfc_current_ns);
4804 /* Add to the list of tentative symbols. */
4805 latest_undo_chgset->syms.safe_push (tmp_sym);
4806 tmp_sym->old_symbol = NULL;
4807 tmp_sym->mark = 1;
4808 tmp_sym->gfc_new = 1;
4810 tmp_symtree->n.sym = tmp_sym;
4811 tmp_sym->refs++;
4813 else
4815 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
4816 gcc_assert (tmp_symtree);
4817 tmp_sym = tmp_symtree->n.sym;
4820 /* Say what module this symbol belongs to. */
4821 tmp_sym->module = gfc_get_string ("%s", mod_name);
4822 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
4823 tmp_sym->intmod_sym_id = s;
4824 tmp_sym->attr.is_iso_c = 1;
4825 tmp_sym->attr.use_assoc = 1;
4827 gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
4828 || s == ISOCBINDING_NULL_PTR);
4830 switch (s)
4833 #define NAMED_INTCST(a,b,c,d) case a :
4834 #define NAMED_REALCST(a,b,c,d) case a :
4835 #define NAMED_CMPXCST(a,b,c,d) case a :
4836 #define NAMED_LOGCST(a,b,c) case a :
4837 #define NAMED_CHARKNDCST(a,b,c) case a :
4838 #include "iso-c-binding.def"
4840 tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4841 c_interop_kinds_table[s].value);
4843 /* Initialize an integer constant expression node. */
4844 tmp_sym->attr.flavor = FL_PARAMETER;
4845 tmp_sym->ts.type = BT_INTEGER;
4846 tmp_sym->ts.kind = gfc_default_integer_kind;
4848 /* Mark this type as a C interoperable one. */
4849 tmp_sym->ts.is_c_interop = 1;
4850 tmp_sym->ts.is_iso_c = 1;
4851 tmp_sym->value->ts.is_c_interop = 1;
4852 tmp_sym->value->ts.is_iso_c = 1;
4853 tmp_sym->attr.is_c_interop = 1;
4855 /* Tell what f90 type this c interop kind is valid. */
4856 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
4858 break;
4861 #define NAMED_CHARCST(a,b,c) case a :
4862 #include "iso-c-binding.def"
4864 /* Initialize an integer constant expression node for the
4865 length of the character. */
4866 tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
4867 &gfc_current_locus, NULL, 1);
4868 tmp_sym->value->ts.is_c_interop = 1;
4869 tmp_sym->value->ts.is_iso_c = 1;
4870 tmp_sym->value->value.character.length = 1;
4871 tmp_sym->value->value.character.string[0]
4872 = (gfc_char_t) c_interop_kinds_table[s].value;
4873 tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4874 tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4875 NULL, 1);
4877 /* May not need this in both attr and ts, but do need in
4878 attr for writing module file. */
4879 tmp_sym->attr.is_c_interop = 1;
4881 tmp_sym->attr.flavor = FL_PARAMETER;
4882 tmp_sym->ts.type = BT_CHARACTER;
4884 /* Need to set it to the C_CHAR kind. */
4885 tmp_sym->ts.kind = gfc_default_character_kind;
4887 /* Mark this type as a C interoperable one. */
4888 tmp_sym->ts.is_c_interop = 1;
4889 tmp_sym->ts.is_iso_c = 1;
4891 /* Tell what f90 type this c interop kind is valid. */
4892 tmp_sym->ts.f90_type = BT_CHARACTER;
4894 break;
4896 case ISOCBINDING_PTR:
4897 case ISOCBINDING_FUNPTR:
4899 gfc_symbol *dt_sym;
4900 gfc_component *tmp_comp = NULL;
4902 /* Generate real derived type. */
4903 if (hidden)
4904 dt_sym = tmp_sym;
4905 else
4907 const char *hidden_name;
4908 gfc_interface *intr, *head;
4910 hidden_name = gfc_dt_upper_string (tmp_sym->name);
4911 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
4912 hidden_name);
4913 gcc_assert (tmp_symtree == NULL);
4914 gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
4915 dt_sym = tmp_symtree->n.sym;
4916 dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
4917 ? "c_ptr" : "c_funptr");
4919 /* Generate an artificial generic function. */
4920 head = tmp_sym->generic;
4921 intr = gfc_get_interface ();
4922 intr->sym = dt_sym;
4923 intr->where = gfc_current_locus;
4924 intr->next = head;
4925 tmp_sym->generic = intr;
4927 if (!tmp_sym->attr.generic
4928 && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
4929 return NULL;
4931 if (!tmp_sym->attr.function
4932 && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
4933 return NULL;
4936 /* Say what module this symbol belongs to. */
4937 dt_sym->module = gfc_get_string ("%s", mod_name);
4938 dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
4939 dt_sym->intmod_sym_id = s;
4940 dt_sym->attr.use_assoc = 1;
4942 /* Initialize an integer constant expression node. */
4943 dt_sym->attr.flavor = FL_DERIVED;
4944 dt_sym->ts.is_c_interop = 1;
4945 dt_sym->attr.is_c_interop = 1;
4946 dt_sym->attr.private_comp = 1;
4947 dt_sym->component_access = ACCESS_PRIVATE;
4948 dt_sym->ts.is_iso_c = 1;
4949 dt_sym->ts.type = BT_DERIVED;
4950 dt_sym->ts.f90_type = BT_VOID;
4952 /* A derived type must have the bind attribute to be
4953 interoperable (J3/04-007, Section 15.2.3), even though
4954 the binding label is not used. */
4955 dt_sym->attr.is_bind_c = 1;
4957 dt_sym->attr.referenced = 1;
4958 dt_sym->ts.u.derived = dt_sym;
4960 /* Add the symbol created for the derived type to the current ns. */
4961 if (gfc_derived_types)
4963 dt_sym->dt_next = gfc_derived_types->dt_next;
4964 gfc_derived_types->dt_next = dt_sym;
4966 else
4968 dt_sym->dt_next = dt_sym;
4970 gfc_derived_types = dt_sym;
4972 gfc_add_component (dt_sym, "c_address", &tmp_comp);
4973 if (tmp_comp == NULL)
4974 gcc_unreachable ();
4976 tmp_comp->ts.type = BT_INTEGER;
4978 /* Set this because the module will need to read/write this field. */
4979 tmp_comp->ts.f90_type = BT_INTEGER;
4981 /* The kinds for c_ptr and c_funptr are the same. */
4982 index = get_c_kind ("c_ptr", c_interop_kinds_table);
4983 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
4984 tmp_comp->attr.access = ACCESS_PRIVATE;
4986 /* Mark the component as C interoperable. */
4987 tmp_comp->ts.is_c_interop = 1;
4990 break;
4992 case ISOCBINDING_NULL_PTR:
4993 case ISOCBINDING_NULL_FUNPTR:
4994 gen_special_c_interop_ptr (tmp_sym, dt_symtree);
4995 break;
4997 default:
4998 gcc_unreachable ();
5000 gfc_commit_symbol (tmp_sym);
5001 return tmp_symtree;
5005 /* Check that a symbol is already typed. If strict is not set, an untyped
5006 symbol is acceptable for non-standard-conforming mode. */
5008 bool
5009 gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
5010 bool strict, locus where)
5012 gcc_assert (sym);
5014 if (gfc_matching_prefix)
5015 return true;
5017 /* Check for the type and try to give it an implicit one. */
5018 if (sym->ts.type == BT_UNKNOWN
5019 && !gfc_set_default_type (sym, 0, ns))
5021 if (strict)
5023 gfc_error ("Symbol %qs is used before it is typed at %L",
5024 sym->name, &where);
5025 return false;
5028 if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
5029 " it is typed at %L", sym->name, &where))
5030 return false;
5033 /* Everything is ok. */
5034 return true;
5038 /* Construct a typebound-procedure structure. Those are stored in a tentative
5039 list and marked `error' until symbols are committed. */
5041 gfc_typebound_proc*
5042 gfc_get_typebound_proc (gfc_typebound_proc *tb0)
5044 gfc_typebound_proc *result;
5046 result = XCNEW (gfc_typebound_proc);
5047 if (tb0)
5048 *result = *tb0;
5049 result->error = 1;
5051 latest_undo_chgset->tbps.safe_push (result);
5053 return result;
5057 /* Get the super-type of a given derived type. */
5059 gfc_symbol*
5060 gfc_get_derived_super_type (gfc_symbol* derived)
5062 gcc_assert (derived);
5064 if (derived->attr.generic)
5065 derived = gfc_find_dt_in_generic (derived);
5067 if (!derived->attr.extension)
5068 return NULL;
5070 gcc_assert (derived->components);
5071 gcc_assert (derived->components->ts.type == BT_DERIVED);
5072 gcc_assert (derived->components->ts.u.derived);
5074 if (derived->components->ts.u.derived->attr.generic)
5075 return gfc_find_dt_in_generic (derived->components->ts.u.derived);
5077 return derived->components->ts.u.derived;
5081 /* Get the ultimate super-type of a given derived type. */
5083 gfc_symbol*
5084 gfc_get_ultimate_derived_super_type (gfc_symbol* derived)
5086 if (!derived->attr.extension)
5087 return NULL;
5089 derived = gfc_get_derived_super_type (derived);
5091 if (derived->attr.extension)
5092 return gfc_get_ultimate_derived_super_type (derived);
5093 else
5094 return derived;
5098 /* Check if a derived type t2 is an extension of (or equal to) a type t1. */
5100 bool
5101 gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
5103 while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
5104 t2 = gfc_get_derived_super_type (t2);
5105 return gfc_compare_derived_types (t1, t2);
5109 /* Check if two typespecs are type compatible (F03:5.1.1.2):
5110 If ts1 is nonpolymorphic, ts2 must be the same type.
5111 If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1. */
5113 bool
5114 gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
5116 bool is_class1 = (ts1->type == BT_CLASS);
5117 bool is_class2 = (ts2->type == BT_CLASS);
5118 bool is_derived1 = (ts1->type == BT_DERIVED);
5119 bool is_derived2 = (ts2->type == BT_DERIVED);
5120 bool is_union1 = (ts1->type == BT_UNION);
5121 bool is_union2 = (ts2->type == BT_UNION);
5123 if (is_class1
5124 && ts1->u.derived->components
5125 && ((ts1->u.derived->attr.is_class
5126 && ts1->u.derived->components->ts.u.derived->attr
5127 .unlimited_polymorphic)
5128 || ts1->u.derived->attr.unlimited_polymorphic))
5129 return 1;
5131 if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2
5132 && !is_union1 && !is_union2)
5133 return (ts1->type == ts2->type);
5135 if ((is_derived1 && is_derived2) || (is_union1 && is_union2))
5136 return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
5138 if (is_derived1 && is_class2)
5139 return gfc_compare_derived_types (ts1->u.derived,
5140 ts2->u.derived->attr.is_class ?
5141 ts2->u.derived->components->ts.u.derived
5142 : ts2->u.derived);
5143 if (is_class1 && is_derived2)
5144 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
5145 ts1->u.derived->components->ts.u.derived
5146 : ts1->u.derived,
5147 ts2->u.derived);
5148 else if (is_class1 && is_class2)
5149 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
5150 ts1->u.derived->components->ts.u.derived
5151 : ts1->u.derived,
5152 ts2->u.derived->attr.is_class ?
5153 ts2->u.derived->components->ts.u.derived
5154 : ts2->u.derived);
5155 else
5156 return 0;
5160 /* Find the parent-namespace of the current function. If we're inside
5161 BLOCK constructs, it may not be the current one. */
5163 gfc_namespace*
5164 gfc_find_proc_namespace (gfc_namespace* ns)
5166 while (ns->construct_entities)
5168 ns = ns->parent;
5169 gcc_assert (ns);
5172 return ns;
5176 /* Check if an associate-variable should be translated as an `implicit' pointer
5177 internally (if it is associated to a variable and not an array with
5178 descriptor). */
5180 bool
5181 gfc_is_associate_pointer (gfc_symbol* sym)
5183 if (!sym->assoc)
5184 return false;
5186 if (sym->ts.type == BT_CLASS)
5187 return true;
5189 if (sym->ts.type == BT_CHARACTER
5190 && sym->ts.deferred
5191 && sym->assoc->target
5192 && sym->assoc->target->expr_type == EXPR_FUNCTION)
5193 return true;
5195 if (!sym->assoc->variable)
5196 return false;
5198 if (sym->attr.dimension && sym->as->type != AS_EXPLICIT)
5199 return false;
5201 return true;
5205 gfc_symbol *
5206 gfc_find_dt_in_generic (gfc_symbol *sym)
5208 gfc_interface *intr = NULL;
5210 if (!sym || gfc_fl_struct (sym->attr.flavor))
5211 return sym;
5213 if (sym->attr.generic)
5214 for (intr = sym->generic; intr; intr = intr->next)
5215 if (gfc_fl_struct (intr->sym->attr.flavor))
5216 break;
5217 return intr ? intr->sym : NULL;
5221 /* Get the dummy arguments from a procedure symbol. If it has been declared
5222 via a PROCEDURE statement with a named interface, ts.interface will be set
5223 and the arguments need to be taken from there. */
5225 gfc_formal_arglist *
5226 gfc_sym_get_dummy_args (gfc_symbol *sym)
5228 gfc_formal_arglist *dummies;
5230 dummies = sym->formal;
5231 if (dummies == NULL && sym->ts.interface != NULL)
5232 dummies = sym->ts.interface->formal;
5234 return dummies;