2016-05-07 Fritz Reese <fritzoreese@gmail.com>
[official-gcc.git] / gcc / fortran / symbol.c
blob0ee7decffd4b67e29d191fdd052d429bd3afabca
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000-2016 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 /* This is to make sure the backend generates setup code in the correct
91 order. */
93 static int next_dummy_order = 1;
96 gfc_namespace *gfc_current_ns;
97 gfc_namespace *gfc_global_ns_list;
99 gfc_gsymbol *gfc_gsym_root = NULL;
101 gfc_dt_list *gfc_derived_types;
103 static gfc_undo_change_set default_undo_chgset_var = { vNULL, vNULL, NULL };
104 static gfc_undo_change_set *latest_undo_chgset = &default_undo_chgset_var;
107 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
109 /* The following static variable indicates whether a particular element has
110 been explicitly set or not. */
112 static int new_flag[GFC_LETTERS];
115 /* Handle a correctly parsed IMPLICIT NONE. */
117 void
118 gfc_set_implicit_none (bool type, bool external, locus *loc)
120 int i;
122 if (external)
123 gfc_current_ns->has_implicit_none_export = 1;
125 if (type)
127 gfc_current_ns->seen_implicit_none = 1;
128 for (i = 0; i < GFC_LETTERS; i++)
130 if (gfc_current_ns->set_flag[i])
132 gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
133 "IMPLICIT statement", loc);
134 return;
136 gfc_clear_ts (&gfc_current_ns->default_type[i]);
137 gfc_current_ns->set_flag[i] = 1;
143 /* Reset the implicit range flags. */
145 void
146 gfc_clear_new_implicit (void)
148 int i;
150 for (i = 0; i < GFC_LETTERS; i++)
151 new_flag[i] = 0;
155 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
157 bool
158 gfc_add_new_implicit_range (int c1, int c2)
160 int i;
162 c1 -= 'a';
163 c2 -= 'a';
165 for (i = c1; i <= c2; i++)
167 if (new_flag[i])
169 gfc_error ("Letter %qc already set in IMPLICIT statement at %C",
170 i + 'A');
171 return false;
174 new_flag[i] = 1;
177 return true;
181 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
182 the new implicit types back into the existing types will work. */
184 bool
185 gfc_merge_new_implicit (gfc_typespec *ts)
187 int i;
189 if (gfc_current_ns->seen_implicit_none)
191 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
192 return false;
195 for (i = 0; i < GFC_LETTERS; i++)
197 if (new_flag[i])
199 if (gfc_current_ns->set_flag[i])
201 gfc_error ("Letter %qc already has an IMPLICIT type at %C",
202 i + 'A');
203 return false;
206 gfc_current_ns->default_type[i] = *ts;
207 gfc_current_ns->implicit_loc[i] = gfc_current_locus;
208 gfc_current_ns->set_flag[i] = 1;
211 return true;
215 /* Given a symbol, return a pointer to the typespec for its default type. */
217 gfc_typespec *
218 gfc_get_default_type (const char *name, gfc_namespace *ns)
220 char letter;
222 letter = name[0];
224 if (flag_allow_leading_underscore && letter == '_')
225 gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
226 "gfortran developers, and should not be used for "
227 "implicitly typed variables");
229 if (letter < 'a' || letter > 'z')
230 gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
232 if (ns == NULL)
233 ns = gfc_current_ns;
235 return &ns->default_type[letter - 'a'];
239 /* Given a pointer to a symbol, set its type according to the first
240 letter of its name. Fails if the letter in question has no default
241 type. */
243 bool
244 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
246 gfc_typespec *ts;
248 if (sym->ts.type != BT_UNKNOWN)
249 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
251 ts = gfc_get_default_type (sym->name, ns);
253 if (ts->type == BT_UNKNOWN)
255 if (error_flag && !sym->attr.untyped)
257 gfc_error ("Symbol %qs at %L has no IMPLICIT type",
258 sym->name, &sym->declared_at);
259 sym->attr.untyped = 1; /* Ensure we only give an error once. */
262 return false;
265 sym->ts = *ts;
266 sym->attr.implicit_type = 1;
268 if (ts->type == BT_CHARACTER && ts->u.cl)
269 sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
270 else if (ts->type == BT_CLASS
271 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
272 return false;
274 if (sym->attr.is_bind_c == 1 && warn_c_binding_type)
276 /* BIND(C) variables should not be implicitly declared. */
277 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared BIND(C) "
278 "variable %qs at %L may not be C interoperable",
279 sym->name, &sym->declared_at);
280 sym->ts.f90_type = sym->ts.type;
283 if (sym->attr.dummy != 0)
285 if (sym->ns->proc_name != NULL
286 && (sym->ns->proc_name->attr.subroutine != 0
287 || sym->ns->proc_name->attr.function != 0)
288 && sym->ns->proc_name->attr.is_bind_c != 0
289 && warn_c_binding_type)
291 /* Dummy args to a BIND(C) routine may not be interoperable if
292 they are implicitly typed. */
293 gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared variable "
294 "%qs at %L may not be C interoperable but it is a "
295 "dummy argument to the BIND(C) procedure %qs at %L",
296 sym->name, &(sym->declared_at),
297 sym->ns->proc_name->name,
298 &(sym->ns->proc_name->declared_at));
299 sym->ts.f90_type = sym->ts.type;
303 return true;
307 /* This function is called from parse.c(parse_progunit) to check the
308 type of the function is not implicitly typed in the host namespace
309 and to implicitly type the function result, if necessary. */
311 void
312 gfc_check_function_type (gfc_namespace *ns)
314 gfc_symbol *proc = ns->proc_name;
316 if (!proc->attr.contained || proc->result->attr.implicit_type)
317 return;
319 if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
321 if (gfc_set_default_type (proc->result, 0, gfc_current_ns))
323 if (proc->result != proc)
325 proc->ts = proc->result->ts;
326 proc->as = gfc_copy_array_spec (proc->result->as);
327 proc->attr.dimension = proc->result->attr.dimension;
328 proc->attr.pointer = proc->result->attr.pointer;
329 proc->attr.allocatable = proc->result->attr.allocatable;
332 else if (!proc->result->attr.proc_pointer)
334 gfc_error ("Function result %qs at %L has no IMPLICIT type",
335 proc->result->name, &proc->result->declared_at);
336 proc->result->attr.untyped = 1;
342 /******************** Symbol attribute stuff *********************/
344 /* This is a generic conflict-checker. We do this to avoid having a
345 single conflict in two places. */
347 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
348 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
349 #define conf_std(a, b, std) if (attr->a && attr->b)\
351 a1 = a;\
352 a2 = b;\
353 standard = std;\
354 goto conflict_std;\
357 static bool
358 check_conflict (symbol_attribute *attr, const char *name, locus *where)
360 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
361 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
362 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
363 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
364 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
365 *privat = "PRIVATE", *recursive = "RECURSIVE",
366 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
367 *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
368 *function = "FUNCTION", *subroutine = "SUBROUTINE",
369 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
370 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
371 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
372 *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
373 *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
374 *proc_pointer = "PROCEDURE POINTER", *abstract = "ABSTRACT",
375 *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION",
376 *contiguous = "CONTIGUOUS", *generic = "GENERIC";
377 static const char *threadprivate = "THREADPRIVATE";
378 static const char *omp_declare_target = "OMP DECLARE TARGET";
379 static const char *oacc_declare_copyin = "OACC DECLARE COPYIN";
380 static const char *oacc_declare_create = "OACC DECLARE CREATE";
381 static const char *oacc_declare_deviceptr = "OACC DECLARE DEVICEPTR";
382 static const char *oacc_declare_device_resident =
383 "OACC DECLARE DEVICE_RESIDENT";
385 const char *a1, *a2;
386 int standard;
388 if (where == NULL)
389 where = &gfc_current_locus;
391 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
393 a1 = pointer;
394 a2 = intent;
395 standard = GFC_STD_F2003;
396 goto conflict_std;
399 if (attr->in_namelist && (attr->allocatable || attr->pointer))
401 a1 = in_namelist;
402 a2 = attr->allocatable ? allocatable : pointer;
403 standard = GFC_STD_F2003;
404 goto conflict_std;
407 /* Check for attributes not allowed in a BLOCK DATA. */
408 if (gfc_current_state () == COMP_BLOCK_DATA)
410 a1 = NULL;
412 if (attr->in_namelist)
413 a1 = in_namelist;
414 if (attr->allocatable)
415 a1 = allocatable;
416 if (attr->external)
417 a1 = external;
418 if (attr->optional)
419 a1 = optional;
420 if (attr->access == ACCESS_PRIVATE)
421 a1 = privat;
422 if (attr->access == ACCESS_PUBLIC)
423 a1 = publik;
424 if (attr->intent != INTENT_UNKNOWN)
425 a1 = intent;
427 if (a1 != NULL)
429 gfc_error
430 ("%s attribute not allowed in BLOCK DATA program unit at %L",
431 a1, where);
432 return false;
436 if (attr->save == SAVE_EXPLICIT)
438 conf (dummy, save);
439 conf (in_common, save);
440 conf (result, save);
442 switch (attr->flavor)
444 case FL_PROGRAM:
445 case FL_BLOCK_DATA:
446 case FL_MODULE:
447 case FL_LABEL:
448 case_fl_struct:
449 case FL_PARAMETER:
450 a1 = gfc_code2string (flavors, attr->flavor);
451 a2 = save;
452 goto conflict;
453 case FL_NAMELIST:
454 gfc_error ("Namelist group name at %L cannot have the "
455 "SAVE attribute", where);
456 return false;
457 break;
458 case FL_PROCEDURE:
459 /* Conflicts between SAVE and PROCEDURE will be checked at
460 resolution stage, see "resolve_fl_procedure". */
461 case FL_VARIABLE:
462 default:
463 break;
467 if (attr->dummy && ((attr->function || attr->subroutine) &&
468 gfc_current_state () == COMP_CONTAINS))
469 gfc_error_now ("internal procedure %qs at %L conflicts with "
470 "DUMMY argument", name, where);
472 conf (dummy, entry);
473 conf (dummy, intrinsic);
474 conf (dummy, threadprivate);
475 conf (dummy, omp_declare_target);
476 conf (pointer, target);
477 conf (pointer, intrinsic);
478 conf (pointer, elemental);
479 conf (pointer, codimension);
480 conf (allocatable, elemental);
482 conf (target, external);
483 conf (target, intrinsic);
485 if (!attr->if_source)
486 conf (external, dimension); /* See Fortran 95's R504. */
488 conf (external, intrinsic);
489 conf (entry, intrinsic);
491 if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
492 conf (external, subroutine);
494 if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
495 "Procedure pointer at %C"))
496 return false;
498 conf (allocatable, pointer);
499 conf_std (allocatable, dummy, GFC_STD_F2003);
500 conf_std (allocatable, function, GFC_STD_F2003);
501 conf_std (allocatable, result, GFC_STD_F2003);
502 conf (elemental, recursive);
504 conf (in_common, dummy);
505 conf (in_common, allocatable);
506 conf (in_common, codimension);
507 conf (in_common, result);
509 conf (in_equivalence, use_assoc);
510 conf (in_equivalence, codimension);
511 conf (in_equivalence, dummy);
512 conf (in_equivalence, target);
513 conf (in_equivalence, pointer);
514 conf (in_equivalence, function);
515 conf (in_equivalence, result);
516 conf (in_equivalence, entry);
517 conf (in_equivalence, allocatable);
518 conf (in_equivalence, threadprivate);
519 conf (in_equivalence, omp_declare_target);
520 conf (in_equivalence, oacc_declare_create);
521 conf (in_equivalence, oacc_declare_copyin);
522 conf (in_equivalence, oacc_declare_deviceptr);
523 conf (in_equivalence, oacc_declare_device_resident);
525 conf (dummy, result);
526 conf (entry, result);
527 conf (generic, result);
529 conf (function, subroutine);
531 if (!function && !subroutine)
532 conf (is_bind_c, dummy);
534 conf (is_bind_c, cray_pointer);
535 conf (is_bind_c, cray_pointee);
536 conf (is_bind_c, codimension);
537 conf (is_bind_c, allocatable);
538 conf (is_bind_c, elemental);
540 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
541 Parameter conflict caught below. Also, value cannot be specified
542 for a dummy procedure. */
544 /* Cray pointer/pointee conflicts. */
545 conf (cray_pointer, cray_pointee);
546 conf (cray_pointer, dimension);
547 conf (cray_pointer, codimension);
548 conf (cray_pointer, contiguous);
549 conf (cray_pointer, pointer);
550 conf (cray_pointer, target);
551 conf (cray_pointer, allocatable);
552 conf (cray_pointer, external);
553 conf (cray_pointer, intrinsic);
554 conf (cray_pointer, in_namelist);
555 conf (cray_pointer, function);
556 conf (cray_pointer, subroutine);
557 conf (cray_pointer, entry);
559 conf (cray_pointee, allocatable);
560 conf (cray_pointee, contiguous);
561 conf (cray_pointee, codimension);
562 conf (cray_pointee, intent);
563 conf (cray_pointee, optional);
564 conf (cray_pointee, dummy);
565 conf (cray_pointee, target);
566 conf (cray_pointee, intrinsic);
567 conf (cray_pointee, pointer);
568 conf (cray_pointee, entry);
569 conf (cray_pointee, in_common);
570 conf (cray_pointee, in_equivalence);
571 conf (cray_pointee, threadprivate);
572 conf (cray_pointee, omp_declare_target);
573 conf (cray_pointee, oacc_declare_create);
574 conf (cray_pointee, oacc_declare_copyin);
575 conf (cray_pointee, oacc_declare_deviceptr);
576 conf (cray_pointee, oacc_declare_device_resident);
578 conf (data, dummy);
579 conf (data, function);
580 conf (data, result);
581 conf (data, allocatable);
583 conf (value, pointer)
584 conf (value, allocatable)
585 conf (value, subroutine)
586 conf (value, function)
587 conf (value, volatile_)
588 conf (value, dimension)
589 conf (value, codimension)
590 conf (value, external)
592 conf (codimension, result)
594 if (attr->value
595 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
597 a1 = value;
598 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
599 goto conflict;
602 conf (is_protected, intrinsic)
603 conf (is_protected, in_common)
605 conf (asynchronous, intrinsic)
606 conf (asynchronous, external)
608 conf (volatile_, intrinsic)
609 conf (volatile_, external)
611 if (attr->volatile_ && attr->intent == INTENT_IN)
613 a1 = volatile_;
614 a2 = intent_in;
615 goto conflict;
618 conf (procedure, allocatable)
619 conf (procedure, dimension)
620 conf (procedure, codimension)
621 conf (procedure, intrinsic)
622 conf (procedure, target)
623 conf (procedure, value)
624 conf (procedure, volatile_)
625 conf (procedure, asynchronous)
626 conf (procedure, entry)
628 conf (proc_pointer, abstract)
630 conf (entry, omp_declare_target)
631 conf (entry, oacc_declare_create)
632 conf (entry, oacc_declare_copyin)
633 conf (entry, oacc_declare_deviceptr)
634 conf (entry, oacc_declare_device_resident)
636 a1 = gfc_code2string (flavors, attr->flavor);
638 if (attr->in_namelist
639 && attr->flavor != FL_VARIABLE
640 && attr->flavor != FL_PROCEDURE
641 && attr->flavor != FL_UNKNOWN)
643 a2 = in_namelist;
644 goto conflict;
647 switch (attr->flavor)
649 case FL_PROGRAM:
650 case FL_BLOCK_DATA:
651 case FL_MODULE:
652 case FL_LABEL:
653 conf2 (codimension);
654 conf2 (dimension);
655 conf2 (dummy);
656 conf2 (volatile_);
657 conf2 (asynchronous);
658 conf2 (contiguous);
659 conf2 (pointer);
660 conf2 (is_protected);
661 conf2 (target);
662 conf2 (external);
663 conf2 (intrinsic);
664 conf2 (allocatable);
665 conf2 (result);
666 conf2 (in_namelist);
667 conf2 (optional);
668 conf2 (function);
669 conf2 (subroutine);
670 conf2 (threadprivate);
671 conf2 (omp_declare_target);
672 conf2 (oacc_declare_create);
673 conf2 (oacc_declare_copyin);
674 conf2 (oacc_declare_deviceptr);
675 conf2 (oacc_declare_device_resident);
677 if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
679 a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
680 gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
681 name, where);
682 return false;
685 if (attr->is_bind_c)
687 gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
688 return false;
691 break;
693 case FL_VARIABLE:
694 break;
696 case FL_NAMELIST:
697 conf2 (result);
698 break;
700 case FL_PROCEDURE:
701 /* Conflicts with INTENT, SAVE and RESULT will be checked
702 at resolution stage, see "resolve_fl_procedure". */
704 if (attr->subroutine)
706 a1 = subroutine;
707 conf2 (target);
708 conf2 (allocatable);
709 conf2 (volatile_);
710 conf2 (asynchronous);
711 conf2 (in_namelist);
712 conf2 (codimension);
713 conf2 (dimension);
714 conf2 (function);
715 if (!attr->proc_pointer)
716 conf2 (threadprivate);
719 if (!attr->proc_pointer)
720 conf2 (in_common);
722 switch (attr->proc)
724 case PROC_ST_FUNCTION:
725 conf2 (dummy);
726 conf2 (target);
727 break;
729 case PROC_MODULE:
730 conf2 (dummy);
731 break;
733 case PROC_DUMMY:
734 conf2 (result);
735 conf2 (threadprivate);
736 break;
738 default:
739 break;
742 break;
744 case_fl_struct:
745 conf2 (dummy);
746 conf2 (pointer);
747 conf2 (target);
748 conf2 (external);
749 conf2 (intrinsic);
750 conf2 (allocatable);
751 conf2 (optional);
752 conf2 (entry);
753 conf2 (function);
754 conf2 (subroutine);
755 conf2 (threadprivate);
756 conf2 (result);
757 conf2 (omp_declare_target);
758 conf2 (oacc_declare_create);
759 conf2 (oacc_declare_copyin);
760 conf2 (oacc_declare_deviceptr);
761 conf2 (oacc_declare_device_resident);
763 if (attr->intent != INTENT_UNKNOWN)
765 a2 = intent;
766 goto conflict;
768 break;
770 case FL_PARAMETER:
771 conf2 (external);
772 conf2 (intrinsic);
773 conf2 (optional);
774 conf2 (allocatable);
775 conf2 (function);
776 conf2 (subroutine);
777 conf2 (entry);
778 conf2 (contiguous);
779 conf2 (pointer);
780 conf2 (is_protected);
781 conf2 (target);
782 conf2 (dummy);
783 conf2 (in_common);
784 conf2 (value);
785 conf2 (volatile_);
786 conf2 (asynchronous);
787 conf2 (threadprivate);
788 conf2 (value);
789 conf2 (codimension);
790 conf2 (result);
791 if (!attr->is_iso_c)
792 conf2 (is_bind_c);
793 break;
795 default:
796 break;
799 return true;
801 conflict:
802 if (name == NULL)
803 gfc_error ("%s attribute conflicts with %s attribute at %L",
804 a1, a2, where);
805 else
806 gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
807 a1, a2, name, where);
809 return false;
811 conflict_std:
812 if (name == NULL)
814 return gfc_notify_std (standard, "%s attribute "
815 "with %s attribute at %L", a1, a2,
816 where);
818 else
820 return gfc_notify_std (standard, "%s attribute "
821 "with %s attribute in %qs at %L",
822 a1, a2, name, where);
826 #undef conf
827 #undef conf2
828 #undef conf_std
831 /* Mark a symbol as referenced. */
833 void
834 gfc_set_sym_referenced (gfc_symbol *sym)
837 if (sym->attr.referenced)
838 return;
840 sym->attr.referenced = 1;
842 /* Remember which order dummy variables are accessed in. */
843 if (sym->attr.dummy)
844 sym->dummy_order = next_dummy_order++;
848 /* Common subroutine called by attribute changing subroutines in order
849 to prevent them from changing a symbol that has been
850 use-associated. Returns zero if it is OK to change the symbol,
851 nonzero if not. */
853 static int
854 check_used (symbol_attribute *attr, const char *name, locus *where)
857 if (attr->use_assoc == 0)
858 return 0;
860 if (where == NULL)
861 where = &gfc_current_locus;
863 if (name == NULL)
864 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
865 where);
866 else
867 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
868 name, where);
870 return 1;
874 /* Generate an error because of a duplicate attribute. */
876 static void
877 duplicate_attr (const char *attr, locus *where)
880 if (where == NULL)
881 where = &gfc_current_locus;
883 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
887 bool
888 gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
889 locus *where ATTRIBUTE_UNUSED)
891 attr->ext_attr |= 1 << ext_attr;
892 return true;
896 /* Called from decl.c (attr_decl1) to check attributes, when declared
897 separately. */
899 bool
900 gfc_add_attribute (symbol_attribute *attr, locus *where)
902 if (check_used (attr, NULL, where))
903 return false;
905 return check_conflict (attr, NULL, where);
909 bool
910 gfc_add_allocatable (symbol_attribute *attr, locus *where)
913 if (check_used (attr, NULL, where))
914 return false;
916 if (attr->allocatable)
918 duplicate_attr ("ALLOCATABLE", where);
919 return false;
922 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
923 && !gfc_find_state (COMP_INTERFACE))
925 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
926 where);
927 return false;
930 attr->allocatable = 1;
931 return check_conflict (attr, NULL, where);
935 bool
936 gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
939 if (check_used (attr, name, where))
940 return false;
942 if (attr->codimension)
944 duplicate_attr ("CODIMENSION", where);
945 return false;
948 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
949 && !gfc_find_state (COMP_INTERFACE))
951 gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
952 "at %L", name, where);
953 return false;
956 attr->codimension = 1;
957 return check_conflict (attr, name, where);
961 bool
962 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
965 if (check_used (attr, name, where))
966 return false;
968 if (attr->dimension)
970 duplicate_attr ("DIMENSION", where);
971 return false;
974 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
975 && !gfc_find_state (COMP_INTERFACE))
977 gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
978 "at %L", name, where);
979 return false;
982 attr->dimension = 1;
983 return check_conflict (attr, name, where);
987 bool
988 gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
991 if (check_used (attr, name, where))
992 return false;
994 attr->contiguous = 1;
995 return check_conflict (attr, name, where);
999 bool
1000 gfc_add_external (symbol_attribute *attr, locus *where)
1003 if (check_used (attr, NULL, where))
1004 return false;
1006 if (attr->external)
1008 duplicate_attr ("EXTERNAL", where);
1009 return false;
1012 if (attr->pointer && attr->if_source != IFSRC_IFBODY)
1014 attr->pointer = 0;
1015 attr->proc_pointer = 1;
1018 attr->external = 1;
1020 return check_conflict (attr, NULL, where);
1024 bool
1025 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
1028 if (check_used (attr, NULL, where))
1029 return false;
1031 if (attr->intrinsic)
1033 duplicate_attr ("INTRINSIC", where);
1034 return false;
1037 attr->intrinsic = 1;
1039 return check_conflict (attr, NULL, where);
1043 bool
1044 gfc_add_optional (symbol_attribute *attr, locus *where)
1047 if (check_used (attr, NULL, where))
1048 return false;
1050 if (attr->optional)
1052 duplicate_attr ("OPTIONAL", where);
1053 return false;
1056 attr->optional = 1;
1057 return check_conflict (attr, NULL, where);
1061 bool
1062 gfc_add_pointer (symbol_attribute *attr, locus *where)
1065 if (check_used (attr, NULL, where))
1066 return false;
1068 if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
1069 && !gfc_find_state (COMP_INTERFACE)))
1071 duplicate_attr ("POINTER", where);
1072 return false;
1075 if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
1076 || (attr->if_source == IFSRC_IFBODY
1077 && !gfc_find_state (COMP_INTERFACE)))
1078 attr->proc_pointer = 1;
1079 else
1080 attr->pointer = 1;
1082 return check_conflict (attr, NULL, where);
1086 bool
1087 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
1090 if (check_used (attr, NULL, where))
1091 return false;
1093 attr->cray_pointer = 1;
1094 return check_conflict (attr, NULL, where);
1098 bool
1099 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
1102 if (check_used (attr, NULL, where))
1103 return false;
1105 if (attr->cray_pointee)
1107 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1108 " statements", where);
1109 return false;
1112 attr->cray_pointee = 1;
1113 return check_conflict (attr, NULL, where);
1117 bool
1118 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
1120 if (check_used (attr, name, where))
1121 return false;
1123 if (attr->is_protected)
1125 if (!gfc_notify_std (GFC_STD_LEGACY,
1126 "Duplicate PROTECTED attribute specified at %L",
1127 where))
1128 return false;
1131 attr->is_protected = 1;
1132 return check_conflict (attr, name, where);
1136 bool
1137 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
1140 if (check_used (attr, name, where))
1141 return false;
1143 attr->result = 1;
1144 return check_conflict (attr, name, where);
1148 bool
1149 gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
1150 locus *where)
1153 if (check_used (attr, name, where))
1154 return false;
1156 if (s == SAVE_EXPLICIT && gfc_pure (NULL))
1158 gfc_error
1159 ("SAVE attribute at %L cannot be specified in a PURE procedure",
1160 where);
1161 return false;
1164 if (s == SAVE_EXPLICIT)
1165 gfc_unset_implicit_pure (NULL);
1167 if (s == SAVE_EXPLICIT && attr->save == SAVE_EXPLICIT)
1169 if (!gfc_notify_std (GFC_STD_LEGACY,
1170 "Duplicate SAVE attribute specified at %L",
1171 where))
1172 return false;
1175 attr->save = s;
1176 return check_conflict (attr, name, where);
1180 bool
1181 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1184 if (check_used (attr, name, where))
1185 return false;
1187 if (attr->value)
1189 if (!gfc_notify_std (GFC_STD_LEGACY,
1190 "Duplicate VALUE attribute specified at %L",
1191 where))
1192 return false;
1195 attr->value = 1;
1196 return check_conflict (attr, name, where);
1200 bool
1201 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1203 /* No check_used needed as 11.2.1 of the F2003 standard allows
1204 that the local identifier made accessible by a use statement can be
1205 given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1207 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1208 if (!gfc_notify_std (GFC_STD_LEGACY,
1209 "Duplicate VOLATILE attribute specified at %L",
1210 where))
1211 return false;
1213 attr->volatile_ = 1;
1214 attr->volatile_ns = gfc_current_ns;
1215 return check_conflict (attr, name, where);
1219 bool
1220 gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
1222 /* No check_used needed as 11.2.1 of the F2003 standard allows
1223 that the local identifier made accessible by a use statement can be
1224 given a ASYNCHRONOUS attribute. */
1226 if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
1227 if (!gfc_notify_std (GFC_STD_LEGACY,
1228 "Duplicate ASYNCHRONOUS attribute specified at %L",
1229 where))
1230 return false;
1232 attr->asynchronous = 1;
1233 attr->asynchronous_ns = gfc_current_ns;
1234 return check_conflict (attr, name, where);
1238 bool
1239 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1242 if (check_used (attr, name, where))
1243 return false;
1245 if (attr->threadprivate)
1247 duplicate_attr ("THREADPRIVATE", where);
1248 return false;
1251 attr->threadprivate = 1;
1252 return check_conflict (attr, name, where);
1256 bool
1257 gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
1258 locus *where)
1261 if (check_used (attr, name, where))
1262 return false;
1264 if (attr->omp_declare_target)
1265 return true;
1267 attr->omp_declare_target = 1;
1268 return check_conflict (attr, name, where);
1272 bool
1273 gfc_add_oacc_declare_create (symbol_attribute *attr, const char *name,
1274 locus *where)
1276 if (check_used (attr, name, where))
1277 return false;
1279 if (attr->oacc_declare_create)
1280 return true;
1282 attr->oacc_declare_create = 1;
1283 return check_conflict (attr, name, where);
1287 bool
1288 gfc_add_oacc_declare_copyin (symbol_attribute *attr, const char *name,
1289 locus *where)
1291 if (check_used (attr, name, where))
1292 return false;
1294 if (attr->oacc_declare_copyin)
1295 return true;
1297 attr->oacc_declare_copyin = 1;
1298 return check_conflict (attr, name, where);
1302 bool
1303 gfc_add_oacc_declare_deviceptr (symbol_attribute *attr, const char *name,
1304 locus *where)
1306 if (check_used (attr, name, where))
1307 return false;
1309 if (attr->oacc_declare_deviceptr)
1310 return true;
1312 attr->oacc_declare_deviceptr = 1;
1313 return check_conflict (attr, name, where);
1317 bool
1318 gfc_add_oacc_declare_device_resident (symbol_attribute *attr, const char *name,
1319 locus *where)
1321 if (check_used (attr, name, where))
1322 return false;
1324 if (attr->oacc_declare_device_resident)
1325 return true;
1327 attr->oacc_declare_device_resident = 1;
1328 return check_conflict (attr, name, where);
1332 bool
1333 gfc_add_target (symbol_attribute *attr, locus *where)
1336 if (check_used (attr, NULL, where))
1337 return false;
1339 if (attr->target)
1341 duplicate_attr ("TARGET", where);
1342 return false;
1345 attr->target = 1;
1346 return check_conflict (attr, NULL, where);
1350 bool
1351 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1354 if (check_used (attr, name, where))
1355 return false;
1357 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1358 attr->dummy = 1;
1359 return check_conflict (attr, name, where);
1363 bool
1364 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1367 if (check_used (attr, name, where))
1368 return false;
1370 /* Duplicate attribute already checked for. */
1371 attr->in_common = 1;
1372 return check_conflict (attr, name, where);
1376 bool
1377 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1380 /* Duplicate attribute already checked for. */
1381 attr->in_equivalence = 1;
1382 if (!check_conflict (attr, name, where))
1383 return false;
1385 if (attr->flavor == FL_VARIABLE)
1386 return true;
1388 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1392 bool
1393 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1396 if (check_used (attr, name, where))
1397 return false;
1399 attr->data = 1;
1400 return check_conflict (attr, name, where);
1404 bool
1405 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1408 attr->in_namelist = 1;
1409 return check_conflict (attr, name, where);
1413 bool
1414 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1417 if (check_used (attr, name, where))
1418 return false;
1420 attr->sequence = 1;
1421 return check_conflict (attr, name, where);
1425 bool
1426 gfc_add_elemental (symbol_attribute *attr, locus *where)
1429 if (check_used (attr, NULL, where))
1430 return false;
1432 if (attr->elemental)
1434 duplicate_attr ("ELEMENTAL", where);
1435 return false;
1438 attr->elemental = 1;
1439 return check_conflict (attr, NULL, where);
1443 bool
1444 gfc_add_pure (symbol_attribute *attr, locus *where)
1447 if (check_used (attr, NULL, where))
1448 return false;
1450 if (attr->pure)
1452 duplicate_attr ("PURE", where);
1453 return false;
1456 attr->pure = 1;
1457 return check_conflict (attr, NULL, where);
1461 bool
1462 gfc_add_recursive (symbol_attribute *attr, locus *where)
1465 if (check_used (attr, NULL, where))
1466 return false;
1468 if (attr->recursive)
1470 duplicate_attr ("RECURSIVE", where);
1471 return false;
1474 attr->recursive = 1;
1475 return check_conflict (attr, NULL, where);
1479 bool
1480 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1483 if (check_used (attr, name, where))
1484 return false;
1486 if (attr->entry)
1488 duplicate_attr ("ENTRY", where);
1489 return false;
1492 attr->entry = 1;
1493 return check_conflict (attr, name, where);
1497 bool
1498 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1501 if (attr->flavor != FL_PROCEDURE
1502 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1503 return false;
1505 attr->function = 1;
1506 return check_conflict (attr, name, where);
1510 bool
1511 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1514 if (attr->flavor != FL_PROCEDURE
1515 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1516 return false;
1518 attr->subroutine = 1;
1519 return check_conflict (attr, name, where);
1523 bool
1524 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1527 if (attr->flavor != FL_PROCEDURE
1528 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1529 return false;
1531 attr->generic = 1;
1532 return check_conflict (attr, name, where);
1536 bool
1537 gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1540 if (check_used (attr, NULL, where))
1541 return false;
1543 if (attr->flavor != FL_PROCEDURE
1544 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1545 return false;
1547 if (attr->procedure)
1549 duplicate_attr ("PROCEDURE", where);
1550 return false;
1553 attr->procedure = 1;
1555 return check_conflict (attr, NULL, where);
1559 bool
1560 gfc_add_abstract (symbol_attribute* attr, locus* where)
1562 if (attr->abstract)
1564 duplicate_attr ("ABSTRACT", where);
1565 return false;
1568 attr->abstract = 1;
1570 return check_conflict (attr, NULL, where);
1574 /* Flavors are special because some flavors are not what Fortran
1575 considers attributes and can be reaffirmed multiple times. */
1577 bool
1578 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1579 locus *where)
1582 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1583 || f == FL_PARAMETER || f == FL_LABEL || gfc_fl_struct(f)
1584 || f == FL_NAMELIST) && check_used (attr, name, where))
1585 return false;
1587 if (attr->flavor == f && f == FL_VARIABLE)
1588 return true;
1590 if (attr->flavor != FL_UNKNOWN)
1592 if (where == NULL)
1593 where = &gfc_current_locus;
1595 if (name)
1596 gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
1597 gfc_code2string (flavors, attr->flavor), name,
1598 gfc_code2string (flavors, f), where);
1599 else
1600 gfc_error ("%s attribute conflicts with %s attribute at %L",
1601 gfc_code2string (flavors, attr->flavor),
1602 gfc_code2string (flavors, f), where);
1604 return false;
1607 attr->flavor = f;
1609 return check_conflict (attr, name, where);
1613 bool
1614 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1615 const char *name, locus *where)
1618 if (check_used (attr, name, where))
1619 return false;
1621 if (attr->flavor != FL_PROCEDURE
1622 && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1623 return false;
1625 if (where == NULL)
1626 where = &gfc_current_locus;
1628 if (attr->proc != PROC_UNKNOWN && !attr->module_procedure)
1630 if (attr->proc == PROC_ST_FUNCTION && t == PROC_INTERNAL
1631 && !gfc_notification_std (GFC_STD_F2008))
1632 gfc_error ("%s procedure at %L is already declared as %s "
1633 "procedure. \nF2008: A pointer function assignment "
1634 "is ambiguous if it is the first executable statement "
1635 "after the specification block. Please add any other "
1636 "kind of executable statement before it. FIXME",
1637 gfc_code2string (procedures, t), where,
1638 gfc_code2string (procedures, attr->proc));
1639 else
1640 gfc_error ("%s procedure at %L is already declared as %s "
1641 "procedure", gfc_code2string (procedures, t), where,
1642 gfc_code2string (procedures, attr->proc));
1644 return false;
1647 attr->proc = t;
1649 /* Statement functions are always scalar and functions. */
1650 if (t == PROC_ST_FUNCTION
1651 && ((!attr->function && !gfc_add_function (attr, name, where))
1652 || attr->dimension))
1653 return false;
1655 return check_conflict (attr, name, where);
1659 bool
1660 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1663 if (check_used (attr, NULL, where))
1664 return false;
1666 if (attr->intent == INTENT_UNKNOWN)
1668 attr->intent = intent;
1669 return check_conflict (attr, NULL, where);
1672 if (where == NULL)
1673 where = &gfc_current_locus;
1675 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1676 gfc_intent_string (attr->intent),
1677 gfc_intent_string (intent), where);
1679 return false;
1683 /* No checks for use-association in public and private statements. */
1685 bool
1686 gfc_add_access (symbol_attribute *attr, gfc_access access,
1687 const char *name, locus *where)
1690 if (attr->access == ACCESS_UNKNOWN
1691 || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
1693 attr->access = access;
1694 return check_conflict (attr, name, where);
1697 if (where == NULL)
1698 where = &gfc_current_locus;
1699 gfc_error ("ACCESS specification at %L was already specified", where);
1701 return false;
1705 /* Set the is_bind_c field for the given symbol_attribute. */
1707 bool
1708 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1709 int is_proc_lang_bind_spec)
1712 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1713 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1714 "variables or common blocks", where);
1715 else if (attr->is_bind_c)
1716 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1717 else
1718 attr->is_bind_c = 1;
1720 if (where == NULL)
1721 where = &gfc_current_locus;
1723 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
1724 return false;
1726 return check_conflict (attr, name, where);
1730 /* Set the extension field for the given symbol_attribute. */
1732 bool
1733 gfc_add_extension (symbol_attribute *attr, locus *where)
1735 if (where == NULL)
1736 where = &gfc_current_locus;
1738 if (attr->extension)
1739 gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
1740 else
1741 attr->extension = 1;
1743 if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
1744 return false;
1746 return true;
1750 bool
1751 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1752 gfc_formal_arglist * formal, locus *where)
1754 if (check_used (&sym->attr, sym->name, where))
1755 return false;
1757 /* Skip the following checks in the case of a module_procedures in a
1758 submodule since they will manifestly fail. */
1759 if (sym->attr.module_procedure == 1
1760 && source == IFSRC_DECL)
1761 goto finish;
1763 if (where == NULL)
1764 where = &gfc_current_locus;
1766 if (sym->attr.if_source != IFSRC_UNKNOWN
1767 && sym->attr.if_source != IFSRC_DECL)
1769 gfc_error ("Symbol %qs at %L already has an explicit interface",
1770 sym->name, where);
1771 return false;
1774 if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1776 gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
1777 "body", sym->name, where);
1778 return false;
1781 finish:
1782 sym->formal = formal;
1783 sym->attr.if_source = source;
1785 return true;
1789 /* Add a type to a symbol. */
1791 bool
1792 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1794 sym_flavor flavor;
1795 bt type;
1797 if (where == NULL)
1798 where = &gfc_current_locus;
1800 if (sym->result)
1801 type = sym->result->ts.type;
1802 else
1803 type = sym->ts.type;
1805 if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
1806 type = sym->ns->proc_name->ts.type;
1808 if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
1809 && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
1810 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
1811 && !sym->attr.module_procedure)
1813 if (sym->attr.use_assoc)
1814 gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
1815 "use-associated at %L", sym->name, where, sym->module,
1816 &sym->declared_at);
1817 else
1818 gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
1819 where, gfc_basic_typename (type));
1820 return false;
1823 if (sym->attr.procedure && sym->ts.interface)
1825 gfc_error ("Procedure %qs at %L may not have basic type of %s",
1826 sym->name, where, gfc_basic_typename (ts->type));
1827 return false;
1830 flavor = sym->attr.flavor;
1832 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1833 || flavor == FL_LABEL
1834 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
1835 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1837 gfc_error ("Symbol %qs at %L cannot have a type", sym->name, where);
1838 return false;
1841 sym->ts = *ts;
1842 return true;
1846 /* Clears all attributes. */
1848 void
1849 gfc_clear_attr (symbol_attribute *attr)
1851 memset (attr, 0, sizeof (symbol_attribute));
1855 /* Check for missing attributes in the new symbol. Currently does
1856 nothing, but it's not clear that it is unnecessary yet. */
1858 bool
1859 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1860 locus *where ATTRIBUTE_UNUSED)
1863 return true;
1867 /* Copy an attribute to a symbol attribute, bit by bit. Some
1868 attributes have a lot of side-effects but cannot be present given
1869 where we are called from, so we ignore some bits. */
1871 bool
1872 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
1874 int is_proc_lang_bind_spec;
1876 /* In line with the other attributes, we only add bits but do not remove
1877 them; cf. also PR 41034. */
1878 dest->ext_attr |= src->ext_attr;
1880 if (src->allocatable && !gfc_add_allocatable (dest, where))
1881 goto fail;
1883 if (src->dimension && !gfc_add_dimension (dest, NULL, where))
1884 goto fail;
1885 if (src->codimension && !gfc_add_codimension (dest, NULL, where))
1886 goto fail;
1887 if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
1888 goto fail;
1889 if (src->optional && !gfc_add_optional (dest, where))
1890 goto fail;
1891 if (src->pointer && !gfc_add_pointer (dest, where))
1892 goto fail;
1893 if (src->is_protected && !gfc_add_protected (dest, NULL, where))
1894 goto fail;
1895 if (src->save && !gfc_add_save (dest, src->save, NULL, where))
1896 goto fail;
1897 if (src->value && !gfc_add_value (dest, NULL, where))
1898 goto fail;
1899 if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
1900 goto fail;
1901 if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
1902 goto fail;
1903 if (src->threadprivate
1904 && !gfc_add_threadprivate (dest, NULL, where))
1905 goto fail;
1906 if (src->omp_declare_target
1907 && !gfc_add_omp_declare_target (dest, NULL, where))
1908 goto fail;
1909 if (src->oacc_declare_create
1910 && !gfc_add_oacc_declare_create (dest, NULL, where))
1911 goto fail;
1912 if (src->oacc_declare_copyin
1913 && !gfc_add_oacc_declare_copyin (dest, NULL, where))
1914 goto fail;
1915 if (src->oacc_declare_deviceptr
1916 && !gfc_add_oacc_declare_deviceptr (dest, NULL, where))
1917 goto fail;
1918 if (src->oacc_declare_device_resident
1919 && !gfc_add_oacc_declare_device_resident (dest, NULL, where))
1920 goto fail;
1921 if (src->target && !gfc_add_target (dest, where))
1922 goto fail;
1923 if (src->dummy && !gfc_add_dummy (dest, NULL, where))
1924 goto fail;
1925 if (src->result && !gfc_add_result (dest, NULL, where))
1926 goto fail;
1927 if (src->entry)
1928 dest->entry = 1;
1930 if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
1931 goto fail;
1933 if (src->in_common && !gfc_add_in_common (dest, NULL, where))
1934 goto fail;
1936 if (src->generic && !gfc_add_generic (dest, NULL, where))
1937 goto fail;
1938 if (src->function && !gfc_add_function (dest, NULL, where))
1939 goto fail;
1940 if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
1941 goto fail;
1943 if (src->sequence && !gfc_add_sequence (dest, NULL, where))
1944 goto fail;
1945 if (src->elemental && !gfc_add_elemental (dest, where))
1946 goto fail;
1947 if (src->pure && !gfc_add_pure (dest, where))
1948 goto fail;
1949 if (src->recursive && !gfc_add_recursive (dest, where))
1950 goto fail;
1952 if (src->flavor != FL_UNKNOWN
1953 && !gfc_add_flavor (dest, src->flavor, NULL, where))
1954 goto fail;
1956 if (src->intent != INTENT_UNKNOWN
1957 && !gfc_add_intent (dest, src->intent, where))
1958 goto fail;
1960 if (src->access != ACCESS_UNKNOWN
1961 && !gfc_add_access (dest, src->access, NULL, where))
1962 goto fail;
1964 if (!gfc_missing_attr (dest, where))
1965 goto fail;
1967 if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
1968 goto fail;
1969 if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
1970 goto fail;
1972 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1973 if (src->is_bind_c
1974 && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
1975 return false;
1977 if (src->is_c_interop)
1978 dest->is_c_interop = 1;
1979 if (src->is_iso_c)
1980 dest->is_iso_c = 1;
1982 if (src->external && !gfc_add_external (dest, where))
1983 goto fail;
1984 if (src->intrinsic && !gfc_add_intrinsic (dest, where))
1985 goto fail;
1986 if (src->proc_pointer)
1987 dest->proc_pointer = 1;
1989 return true;
1991 fail:
1992 return false;
1996 /* A function to generate a dummy argument symbol using that from the
1997 interface declaration. Can be used for the result symbol as well if
1998 the flag is set. */
2001 gfc_copy_dummy_sym (gfc_symbol **dsym, gfc_symbol *sym, int result)
2003 int rc;
2005 rc = gfc_get_symbol (sym->name, NULL, dsym);
2006 if (rc)
2007 return rc;
2009 if (!gfc_add_type (*dsym, &(sym->ts), &gfc_current_locus))
2010 return 1;
2012 if (!gfc_copy_attr (&(*dsym)->attr, &(sym->attr),
2013 &gfc_current_locus))
2014 return 1;
2016 if ((*dsym)->attr.dimension)
2017 (*dsym)->as = gfc_copy_array_spec (sym->as);
2019 (*dsym)->attr.class_ok = sym->attr.class_ok;
2021 if ((*dsym) != NULL && !result
2022 && (!gfc_add_dummy(&(*dsym)->attr, (*dsym)->name, NULL)
2023 || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2024 return 1;
2025 else if ((*dsym) != NULL && result
2026 && (!gfc_add_result(&(*dsym)->attr, (*dsym)->name, NULL)
2027 || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2028 return 1;
2030 return 0;
2034 /************** Component name management ************/
2036 /* Component names of a derived type form their own little namespaces
2037 that are separate from all other spaces. The space is composed of
2038 a singly linked list of gfc_component structures whose head is
2039 located in the parent symbol. */
2042 /* Add a component name to a symbol. The call fails if the name is
2043 already present. On success, the component pointer is modified to
2044 point to the additional component structure. */
2046 bool
2047 gfc_add_component (gfc_symbol *sym, const char *name,
2048 gfc_component **component)
2050 gfc_component *p, *tail;
2052 /* Check for existing components with the same name, but not for union
2053 components or containers. Unions and maps are anonymous so they have
2054 unique internal names which will never conflict.
2055 Don't use gfc_find_component here because it calls gfc_use_derived,
2056 but the derived type may not be fully defined yet. */
2057 tail = NULL;
2059 for (p = sym->components; p; p = p->next)
2061 if (strcmp (p->name, name) == 0)
2063 gfc_error ("Component %qs at %C already declared at %L",
2064 name, &p->loc);
2065 return false;
2068 tail = p;
2071 if (sym->attr.extension
2072 && gfc_find_component (sym->components->ts.u.derived,
2073 name, true, true, NULL))
2075 gfc_error ("Component %qs at %C already in the parent type "
2076 "at %L", name, &sym->components->ts.u.derived->declared_at);
2077 return false;
2080 /* Allocate a new component. */
2081 p = gfc_get_component ();
2083 if (tail == NULL)
2084 sym->components = p;
2085 else
2086 tail->next = p;
2088 p->name = gfc_get_string (name);
2089 p->loc = gfc_current_locus;
2090 p->ts.type = BT_UNKNOWN;
2092 *component = p;
2093 return true;
2097 /* Recursive function to switch derived types of all symbol in a
2098 namespace. */
2100 static void
2101 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
2103 gfc_symbol *sym;
2105 if (st == NULL)
2106 return;
2108 sym = st->n.sym;
2109 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
2110 sym->ts.u.derived = to;
2112 switch_types (st->left, from, to);
2113 switch_types (st->right, from, to);
2117 /* This subroutine is called when a derived type is used in order to
2118 make the final determination about which version to use. The
2119 standard requires that a type be defined before it is 'used', but
2120 such types can appear in IMPLICIT statements before the actual
2121 definition. 'Using' in this context means declaring a variable to
2122 be that type or using the type constructor.
2124 If a type is used and the components haven't been defined, then we
2125 have to have a derived type in a parent unit. We find the node in
2126 the other namespace and point the symtree node in this namespace to
2127 that node. Further reference to this name point to the correct
2128 node. If we can't find the node in a parent namespace, then we have
2129 an error.
2131 This subroutine takes a pointer to a symbol node and returns a
2132 pointer to the translated node or NULL for an error. Usually there
2133 is no translation and we return the node we were passed. */
2135 gfc_symbol *
2136 gfc_use_derived (gfc_symbol *sym)
2138 gfc_symbol *s;
2139 gfc_typespec *t;
2140 gfc_symtree *st;
2141 int i;
2143 if (!sym)
2144 return NULL;
2146 if (sym->attr.unlimited_polymorphic)
2147 return sym;
2149 if (sym->attr.generic)
2150 sym = gfc_find_dt_in_generic (sym);
2152 if (sym->components != NULL || sym->attr.zero_comp)
2153 return sym; /* Already defined. */
2155 if (sym->ns->parent == NULL)
2156 goto bad;
2158 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
2160 gfc_error ("Symbol %qs at %C is ambiguous", sym->name);
2161 return NULL;
2164 if (s == NULL || !gfc_fl_struct (s->attr.flavor))
2165 goto bad;
2167 /* Get rid of symbol sym, translating all references to s. */
2168 for (i = 0; i < GFC_LETTERS; i++)
2170 t = &sym->ns->default_type[i];
2171 if (t->u.derived == sym)
2172 t->u.derived = s;
2175 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
2176 st->n.sym = s;
2178 s->refs++;
2180 /* Unlink from list of modified symbols. */
2181 gfc_commit_symbol (sym);
2183 switch_types (sym->ns->sym_root, sym, s);
2185 /* TODO: Also have to replace sym -> s in other lists like
2186 namelists, common lists and interface lists. */
2187 gfc_free_symbol (sym);
2189 return s;
2191 bad:
2192 gfc_error ("Derived type %qs at %C is being used before it is defined",
2193 sym->name);
2194 return NULL;
2198 /* Find the component with the given name in the union type symbol.
2199 If ref is not NULL it will be set to the chain of components through which
2200 the component can actually be accessed. This is necessary for unions because
2201 intermediate structures may be maps, nested structures, or other unions,
2202 all of which may (or must) be 'anonymous' to user code. */
2204 static gfc_component *
2205 find_union_component (gfc_symbol *un, const char *name,
2206 bool noaccess, gfc_ref **ref)
2208 gfc_component *m, *check;
2209 gfc_ref *sref, *tmp;
2211 for (m = un->components; m; m = m->next)
2213 check = gfc_find_component (m->ts.u.derived, name, noaccess, true, &tmp);
2214 if (check == NULL)
2215 continue;
2217 /* Found component somewhere in m; chain the refs together. */
2218 if (ref)
2220 /* Map ref. */
2221 sref = gfc_get_ref ();
2222 sref->type = REF_COMPONENT;
2223 sref->u.c.component = m;
2224 sref->u.c.sym = m->ts.u.derived;
2225 sref->next = tmp;
2227 *ref = sref;
2229 /* Other checks (such as access) were done in the recursive calls. */
2230 return check;
2232 return NULL;
2236 /* Given a derived type node and a component name, try to locate the
2237 component structure. Returns the NULL pointer if the component is
2238 not found or the components are private. If noaccess is set, no access
2239 checks are done. If silent is set, an error will not be generated if
2240 the component cannot be found or accessed.
2242 If ref is not NULL, *ref is set to represent the chain of components
2243 required to get to the ultimate component.
2245 If the component is simply a direct subcomponent, or is inherited from a
2246 parent derived type in the given derived type, this is a single ref with its
2247 component set to the returned component.
2249 Otherwise, *ref is constructed as a chain of subcomponents. This occurs
2250 when the component is found through an implicit chain of nested union and
2251 map components. Unions and maps are "anonymous" substructures in FORTRAN
2252 which cannot be explicitly referenced, but the reference chain must be
2253 considered as in C for backend translation to correctly compute layouts.
2254 (For example, x.a may refer to x->(UNION)->(MAP)->(UNION)->(MAP)->a). */
2256 gfc_component *
2257 gfc_find_component (gfc_symbol *sym, const char *name,
2258 bool noaccess, bool silent, gfc_ref **ref)
2260 gfc_component *p, *check;
2261 gfc_ref *sref = NULL, *tmp = NULL;
2263 if (name == NULL || sym == NULL)
2264 return NULL;
2266 if (sym->attr.flavor == FL_DERIVED)
2267 sym = gfc_use_derived (sym);
2268 else
2269 gcc_assert (gfc_fl_struct (sym->attr.flavor));
2271 if (sym == NULL)
2272 return NULL;
2274 /* Handle UNIONs specially - mutually recursive with gfc_find_component. */
2275 if (sym->attr.flavor == FL_UNION)
2276 return find_union_component (sym, name, noaccess, ref);
2278 if (ref) *ref = NULL;
2279 for (p = sym->components; p; p = p->next)
2281 /* Nest search into union's maps. */
2282 if (p->ts.type == BT_UNION)
2284 check = find_union_component (p->ts.u.derived, name, noaccess, &tmp);
2285 if (check != NULL)
2287 /* Union ref. */
2288 if (ref)
2290 sref = gfc_get_ref ();
2291 sref->type = REF_COMPONENT;
2292 sref->u.c.component = p;
2293 sref->u.c.sym = p->ts.u.derived;
2294 sref->next = tmp;
2295 *ref = sref;
2297 return check;
2300 else if (strcmp (p->name, name) == 0)
2301 break;
2303 continue;
2306 if (p && sym->attr.use_assoc && !noaccess)
2308 bool is_parent_comp = sym->attr.extension && (p == sym->components);
2309 if (p->attr.access == ACCESS_PRIVATE ||
2310 (p->attr.access != ACCESS_PUBLIC
2311 && sym->component_access == ACCESS_PRIVATE
2312 && !is_parent_comp))
2314 if (!silent)
2315 gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
2316 name, sym->name);
2317 return NULL;
2321 if (p == NULL
2322 && sym->attr.extension
2323 && sym->components->ts.type == BT_DERIVED)
2325 p = gfc_find_component (sym->components->ts.u.derived, name,
2326 noaccess, silent, ref);
2327 /* Do not overwrite the error. */
2328 if (p == NULL)
2329 return p;
2332 if (p == NULL && !silent)
2333 gfc_error ("%qs at %C is not a member of the %qs structure",
2334 name, sym->name);
2336 /* Component was found; build the ultimate component reference. */
2337 if (p != NULL && ref)
2339 tmp = gfc_get_ref ();
2340 tmp->type = REF_COMPONENT;
2341 tmp->u.c.component = p;
2342 tmp->u.c.sym = sym;
2343 /* Link the final component ref to the end of the chain of subrefs. */
2344 if (sref)
2346 *ref = sref;
2347 for (; sref->next; sref = sref->next)
2349 sref->next = tmp;
2351 else
2352 *ref = tmp;
2355 return p;
2359 /* Given a symbol, free all of the component structures and everything
2360 they point to. */
2362 static void
2363 free_components (gfc_component *p)
2365 gfc_component *q;
2367 for (; p; p = q)
2369 q = p->next;
2371 gfc_free_array_spec (p->as);
2372 gfc_free_expr (p->initializer);
2373 free (p->tb);
2375 free (p);
2380 /******************** Statement label management ********************/
2382 /* Comparison function for statement labels, used for managing the
2383 binary tree. */
2385 static int
2386 compare_st_labels (void *a1, void *b1)
2388 int a = ((gfc_st_label *) a1)->value;
2389 int b = ((gfc_st_label *) b1)->value;
2391 return (b - a);
2395 /* Free a single gfc_st_label structure, making sure the tree is not
2396 messed up. This function is called only when some parse error
2397 occurs. */
2399 void
2400 gfc_free_st_label (gfc_st_label *label)
2403 if (label == NULL)
2404 return;
2406 gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
2408 if (label->format != NULL)
2409 gfc_free_expr (label->format);
2411 free (label);
2415 /* Free a whole tree of gfc_st_label structures. */
2417 static void
2418 free_st_labels (gfc_st_label *label)
2421 if (label == NULL)
2422 return;
2424 free_st_labels (label->left);
2425 free_st_labels (label->right);
2427 if (label->format != NULL)
2428 gfc_free_expr (label->format);
2429 free (label);
2433 /* Given a label number, search for and return a pointer to the label
2434 structure, creating it if it does not exist. */
2436 gfc_st_label *
2437 gfc_get_st_label (int labelno)
2439 gfc_st_label *lp;
2440 gfc_namespace *ns;
2442 if (gfc_current_state () == COMP_DERIVED)
2443 ns = gfc_current_block ()->f2k_derived;
2444 else
2446 /* Find the namespace of the scoping unit:
2447 If we're in a BLOCK construct, jump to the parent namespace. */
2448 ns = gfc_current_ns;
2449 while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
2450 ns = ns->parent;
2453 /* First see if the label is already in this namespace. */
2454 lp = ns->st_labels;
2455 while (lp)
2457 if (lp->value == labelno)
2458 return lp;
2460 if (lp->value < labelno)
2461 lp = lp->left;
2462 else
2463 lp = lp->right;
2466 lp = XCNEW (gfc_st_label);
2468 lp->value = labelno;
2469 lp->defined = ST_LABEL_UNKNOWN;
2470 lp->referenced = ST_LABEL_UNKNOWN;
2471 lp->ns = ns;
2473 gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
2475 return lp;
2479 /* Called when a statement with a statement label is about to be
2480 accepted. We add the label to the list of the current namespace,
2481 making sure it hasn't been defined previously and referenced
2482 correctly. */
2484 void
2485 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
2487 int labelno;
2489 labelno = lp->value;
2491 if (lp->defined != ST_LABEL_UNKNOWN)
2492 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
2493 &lp->where, label_locus);
2494 else
2496 lp->where = *label_locus;
2498 switch (type)
2500 case ST_LABEL_FORMAT:
2501 if (lp->referenced == ST_LABEL_TARGET
2502 || lp->referenced == ST_LABEL_DO_TARGET)
2503 gfc_error ("Label %d at %C already referenced as branch target",
2504 labelno);
2505 else
2506 lp->defined = ST_LABEL_FORMAT;
2508 break;
2510 case ST_LABEL_TARGET:
2511 case ST_LABEL_DO_TARGET:
2512 if (lp->referenced == ST_LABEL_FORMAT)
2513 gfc_error ("Label %d at %C already referenced as a format label",
2514 labelno);
2515 else
2516 lp->defined = type;
2518 if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
2519 && !gfc_notify_std (GFC_STD_F95_OBS, "DO termination statement "
2520 "which is not END DO or CONTINUE with "
2521 "label %d at %C", labelno))
2522 return;
2523 break;
2525 default:
2526 lp->defined = ST_LABEL_BAD_TARGET;
2527 lp->referenced = ST_LABEL_BAD_TARGET;
2533 /* Reference a label. Given a label and its type, see if that
2534 reference is consistent with what is known about that label,
2535 updating the unknown state. Returns false if something goes
2536 wrong. */
2538 bool
2539 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2541 gfc_sl_type label_type;
2542 int labelno;
2543 bool rc;
2545 if (lp == NULL)
2546 return true;
2548 labelno = lp->value;
2550 if (lp->defined != ST_LABEL_UNKNOWN)
2551 label_type = lp->defined;
2552 else
2554 label_type = lp->referenced;
2555 lp->where = gfc_current_locus;
2558 if (label_type == ST_LABEL_FORMAT
2559 && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
2561 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2562 rc = false;
2563 goto done;
2566 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
2567 || label_type == ST_LABEL_BAD_TARGET)
2568 && type == ST_LABEL_FORMAT)
2570 gfc_error ("Label %d at %C previously used as branch target", labelno);
2571 rc = false;
2572 goto done;
2575 if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
2576 && !gfc_notify_std (GFC_STD_F95_OBS, "Shared DO termination label %d "
2577 "at %C", labelno))
2578 return false;
2580 if (lp->referenced != ST_LABEL_DO_TARGET)
2581 lp->referenced = type;
2582 rc = true;
2584 done:
2585 return rc;
2589 /************** Symbol table management subroutines ****************/
2591 /* Basic details: Fortran 95 requires a potentially unlimited number
2592 of distinct namespaces when compiling a program unit. This case
2593 occurs during a compilation of internal subprograms because all of
2594 the internal subprograms must be read before we can start
2595 generating code for the host.
2597 Given the tricky nature of the Fortran grammar, we must be able to
2598 undo changes made to a symbol table if the current interpretation
2599 of a statement is found to be incorrect. Whenever a symbol is
2600 looked up, we make a copy of it and link to it. All of these
2601 symbols are kept in a vector so that we can commit or
2602 undo the changes at a later time.
2604 A symtree may point to a symbol node outside of its namespace. In
2605 this case, that symbol has been used as a host associated variable
2606 at some previous time. */
2608 /* Allocate a new namespace structure. Copies the implicit types from
2609 PARENT if PARENT_TYPES is set. */
2611 gfc_namespace *
2612 gfc_get_namespace (gfc_namespace *parent, int parent_types)
2614 gfc_namespace *ns;
2615 gfc_typespec *ts;
2616 int in;
2617 int i;
2619 ns = XCNEW (gfc_namespace);
2620 ns->sym_root = NULL;
2621 ns->uop_root = NULL;
2622 ns->tb_sym_root = NULL;
2623 ns->finalizers = NULL;
2624 ns->default_access = ACCESS_UNKNOWN;
2625 ns->parent = parent;
2627 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
2629 ns->operator_access[in] = ACCESS_UNKNOWN;
2630 ns->tb_op[in] = NULL;
2633 /* Initialize default implicit types. */
2634 for (i = 'a'; i <= 'z'; i++)
2636 ns->set_flag[i - 'a'] = 0;
2637 ts = &ns->default_type[i - 'a'];
2639 if (parent_types && ns->parent != NULL)
2641 /* Copy parent settings. */
2642 *ts = ns->parent->default_type[i - 'a'];
2643 continue;
2646 if (flag_implicit_none != 0)
2648 gfc_clear_ts (ts);
2649 continue;
2652 if ('i' <= i && i <= 'n')
2654 ts->type = BT_INTEGER;
2655 ts->kind = gfc_default_integer_kind;
2657 else
2659 ts->type = BT_REAL;
2660 ts->kind = gfc_default_real_kind;
2664 if (parent_types && ns->parent != NULL)
2665 ns->has_implicit_none_export = ns->parent->has_implicit_none_export;
2667 ns->refs = 1;
2669 return ns;
2673 /* Comparison function for symtree nodes. */
2675 static int
2676 compare_symtree (void *_st1, void *_st2)
2678 gfc_symtree *st1, *st2;
2680 st1 = (gfc_symtree *) _st1;
2681 st2 = (gfc_symtree *) _st2;
2683 return strcmp (st1->name, st2->name);
2687 /* Allocate a new symtree node and associate it with the new symbol. */
2689 gfc_symtree *
2690 gfc_new_symtree (gfc_symtree **root, const char *name)
2692 gfc_symtree *st;
2694 st = XCNEW (gfc_symtree);
2695 st->name = gfc_get_string (name);
2697 gfc_insert_bbt (root, st, compare_symtree);
2698 return st;
2702 /* Delete a symbol from the tree. Does not free the symbol itself! */
2704 void
2705 gfc_delete_symtree (gfc_symtree **root, const char *name)
2707 gfc_symtree st, *st0;
2709 st0 = gfc_find_symtree (*root, name);
2711 st.name = gfc_get_string (name);
2712 gfc_delete_bbt (root, &st, compare_symtree);
2714 free (st0);
2718 /* Given a root symtree node and a name, try to find the symbol within
2719 the namespace. Returns NULL if the symbol is not found. */
2721 gfc_symtree *
2722 gfc_find_symtree (gfc_symtree *st, const char *name)
2724 int c;
2726 while (st != NULL)
2728 c = strcmp (name, st->name);
2729 if (c == 0)
2730 return st;
2732 st = (c < 0) ? st->left : st->right;
2735 return NULL;
2739 /* Return a symtree node with a name that is guaranteed to be unique
2740 within the namespace and corresponds to an illegal fortran name. */
2742 gfc_symtree *
2743 gfc_get_unique_symtree (gfc_namespace *ns)
2745 char name[GFC_MAX_SYMBOL_LEN + 1];
2746 static int serial = 0;
2748 sprintf (name, "@%d", serial++);
2749 return gfc_new_symtree (&ns->sym_root, name);
2753 /* Given a name find a user operator node, creating it if it doesn't
2754 exist. These are much simpler than symbols because they can't be
2755 ambiguous with one another. */
2757 gfc_user_op *
2758 gfc_get_uop (const char *name)
2760 gfc_user_op *uop;
2761 gfc_symtree *st;
2762 gfc_namespace *ns = gfc_current_ns;
2764 if (ns->omp_udr_ns)
2765 ns = ns->parent;
2766 st = gfc_find_symtree (ns->uop_root, name);
2767 if (st != NULL)
2768 return st->n.uop;
2770 st = gfc_new_symtree (&ns->uop_root, name);
2772 uop = st->n.uop = XCNEW (gfc_user_op);
2773 uop->name = gfc_get_string (name);
2774 uop->access = ACCESS_UNKNOWN;
2775 uop->ns = ns;
2777 return uop;
2781 /* Given a name find the user operator node. Returns NULL if it does
2782 not exist. */
2784 gfc_user_op *
2785 gfc_find_uop (const char *name, gfc_namespace *ns)
2787 gfc_symtree *st;
2789 if (ns == NULL)
2790 ns = gfc_current_ns;
2792 st = gfc_find_symtree (ns->uop_root, name);
2793 return (st == NULL) ? NULL : st->n.uop;
2797 /* Update a symbol's common_block field, and take care of the associated
2798 memory management. */
2800 static void
2801 set_symbol_common_block (gfc_symbol *sym, gfc_common_head *common_block)
2803 if (sym->common_block == common_block)
2804 return;
2806 if (sym->common_block && sym->common_block->name[0] != '\0')
2808 sym->common_block->refs--;
2809 if (sym->common_block->refs == 0)
2810 free (sym->common_block);
2812 sym->common_block = common_block;
2816 /* Remove a gfc_symbol structure and everything it points to. */
2818 void
2819 gfc_free_symbol (gfc_symbol *sym)
2822 if (sym == NULL)
2823 return;
2825 gfc_free_array_spec (sym->as);
2827 free_components (sym->components);
2829 gfc_free_expr (sym->value);
2831 gfc_free_namelist (sym->namelist);
2833 if (sym->ns != sym->formal_ns)
2834 gfc_free_namespace (sym->formal_ns);
2836 if (!sym->attr.generic_copy)
2837 gfc_free_interface (sym->generic);
2839 gfc_free_formal_arglist (sym->formal);
2841 gfc_free_namespace (sym->f2k_derived);
2843 set_symbol_common_block (sym, NULL);
2845 free (sym);
2849 /* Decrease the reference counter and free memory when we reach zero. */
2851 void
2852 gfc_release_symbol (gfc_symbol *sym)
2854 if (sym == NULL)
2855 return;
2857 if (sym->formal_ns != NULL && sym->refs == 2 && sym->formal_ns != sym->ns
2858 && (!sym->attr.entry || !sym->module))
2860 /* As formal_ns contains a reference to sym, delete formal_ns just
2861 before the deletion of sym. */
2862 gfc_namespace *ns = sym->formal_ns;
2863 sym->formal_ns = NULL;
2864 gfc_free_namespace (ns);
2867 sym->refs--;
2868 if (sym->refs > 0)
2869 return;
2871 gcc_assert (sym->refs == 0);
2872 gfc_free_symbol (sym);
2876 /* Allocate and initialize a new symbol node. */
2878 gfc_symbol *
2879 gfc_new_symbol (const char *name, gfc_namespace *ns)
2881 gfc_symbol *p;
2883 p = XCNEW (gfc_symbol);
2885 gfc_clear_ts (&p->ts);
2886 gfc_clear_attr (&p->attr);
2887 p->ns = ns;
2889 p->declared_at = gfc_current_locus;
2891 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2892 gfc_internal_error ("new_symbol(): Symbol name too long");
2894 p->name = gfc_get_string (name);
2896 /* Make sure flags for symbol being C bound are clear initially. */
2897 p->attr.is_bind_c = 0;
2898 p->attr.is_iso_c = 0;
2900 /* Clear the ptrs we may need. */
2901 p->common_block = NULL;
2902 p->f2k_derived = NULL;
2903 p->assoc = NULL;
2905 return p;
2909 /* Generate an error if a symbol is ambiguous. */
2911 static void
2912 ambiguous_symbol (const char *name, gfc_symtree *st)
2915 if (st->n.sym->module)
2916 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2917 "from module %qs", name, st->n.sym->name, st->n.sym->module);
2918 else
2919 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2920 "from current program unit", name, st->n.sym->name);
2924 /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
2925 selector on the stack. If yes, replace it by the corresponding temporary. */
2927 static void
2928 select_type_insert_tmp (gfc_symtree **st)
2930 gfc_select_type_stack *stack = select_type_stack;
2931 for (; stack; stack = stack->prev)
2932 if ((*st)->n.sym == stack->selector && stack->tmp)
2933 *st = stack->tmp;
2937 /* Look for a symtree in the current procedure -- that is, go up to
2938 parent namespaces but only if inside a BLOCK. Returns NULL if not found. */
2940 gfc_symtree*
2941 gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
2943 while (ns)
2945 gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
2946 if (st)
2947 return st;
2949 if (!ns->construct_entities)
2950 break;
2951 ns = ns->parent;
2954 return NULL;
2958 /* Search for a symtree starting in the current namespace, resorting to
2959 any parent namespaces if requested by a nonzero parent_flag.
2960 Returns nonzero if the name is ambiguous. */
2963 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2964 gfc_symtree **result)
2966 gfc_symtree *st;
2968 if (ns == NULL)
2969 ns = gfc_current_ns;
2973 st = gfc_find_symtree (ns->sym_root, name);
2974 if (st != NULL)
2976 select_type_insert_tmp (&st);
2978 *result = st;
2979 /* Ambiguous generic interfaces are permitted, as long
2980 as the specific interfaces are different. */
2981 if (st->ambiguous && !st->n.sym->attr.generic)
2983 ambiguous_symbol (name, st);
2984 return 1;
2987 return 0;
2990 if (!parent_flag)
2991 break;
2993 /* Don't escape an interface block. */
2994 if (ns && !ns->has_import_set
2995 && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
2996 break;
2998 ns = ns->parent;
3000 while (ns != NULL);
3002 *result = NULL;
3003 return 0;
3007 /* Same, but returns the symbol instead. */
3010 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
3011 gfc_symbol **result)
3013 gfc_symtree *st;
3014 int i;
3016 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
3018 if (st == NULL)
3019 *result = NULL;
3020 else
3021 *result = st->n.sym;
3023 return i;
3027 /* Tells whether there is only one set of changes in the stack. */
3029 static bool
3030 single_undo_checkpoint_p (void)
3032 if (latest_undo_chgset == &default_undo_chgset_var)
3034 gcc_assert (latest_undo_chgset->previous == NULL);
3035 return true;
3037 else
3039 gcc_assert (latest_undo_chgset->previous != NULL);
3040 return false;
3044 /* Save symbol with the information necessary to back it out. */
3046 void
3047 gfc_save_symbol_data (gfc_symbol *sym)
3049 gfc_symbol *s;
3050 unsigned i;
3052 if (!single_undo_checkpoint_p ())
3054 /* If there is more than one change set, look for the symbol in the
3055 current one. If it is found there, we can reuse it. */
3056 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3057 if (s == sym)
3059 gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
3060 return;
3063 else if (sym->gfc_new || sym->old_symbol != NULL)
3064 return;
3066 s = XCNEW (gfc_symbol);
3067 *s = *sym;
3068 sym->old_symbol = s;
3069 sym->gfc_new = 0;
3071 latest_undo_chgset->syms.safe_push (sym);
3075 /* Given a name, find a symbol, or create it if it does not exist yet
3076 in the current namespace. If the symbol is found we make sure that
3077 it's OK.
3079 The integer return code indicates
3080 0 All OK
3081 1 The symbol name was ambiguous
3082 2 The name meant to be established was already host associated.
3084 So if the return value is nonzero, then an error was issued. */
3087 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
3088 bool allow_subroutine)
3090 gfc_symtree *st;
3091 gfc_symbol *p;
3093 /* This doesn't usually happen during resolution. */
3094 if (ns == NULL)
3095 ns = gfc_current_ns;
3097 /* Try to find the symbol in ns. */
3098 st = gfc_find_symtree (ns->sym_root, name);
3100 if (st == NULL && ns->omp_udr_ns)
3102 ns = ns->parent;
3103 st = gfc_find_symtree (ns->sym_root, name);
3106 if (st == NULL)
3108 /* If not there, create a new symbol. */
3109 p = gfc_new_symbol (name, ns);
3111 /* Add to the list of tentative symbols. */
3112 p->old_symbol = NULL;
3113 p->mark = 1;
3114 p->gfc_new = 1;
3115 latest_undo_chgset->syms.safe_push (p);
3117 st = gfc_new_symtree (&ns->sym_root, name);
3118 st->n.sym = p;
3119 p->refs++;
3122 else
3124 /* Make sure the existing symbol is OK. Ambiguous
3125 generic interfaces are permitted, as long as the
3126 specific interfaces are different. */
3127 if (st->ambiguous && !st->n.sym->attr.generic)
3129 ambiguous_symbol (name, st);
3130 return 1;
3133 p = st->n.sym;
3134 if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
3135 && !(allow_subroutine && p->attr.subroutine)
3136 && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
3137 && (ns->has_import_set || p->attr.imported)))
3139 /* Symbol is from another namespace. */
3140 gfc_error ("Symbol %qs at %C has already been host associated",
3141 name);
3142 return 2;
3145 p->mark = 1;
3147 /* Copy in case this symbol is changed. */
3148 gfc_save_symbol_data (p);
3151 *result = st;
3152 return 0;
3157 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
3159 gfc_symtree *st;
3160 int i;
3162 i = gfc_get_sym_tree (name, ns, &st, false);
3163 if (i != 0)
3164 return i;
3166 if (st)
3167 *result = st->n.sym;
3168 else
3169 *result = NULL;
3170 return i;
3174 /* Subroutine that searches for a symbol, creating it if it doesn't
3175 exist, but tries to host-associate the symbol if possible. */
3178 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
3180 gfc_symtree *st;
3181 int i;
3183 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
3185 if (st != NULL)
3187 gfc_save_symbol_data (st->n.sym);
3188 *result = st;
3189 return i;
3192 i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
3193 if (i)
3194 return i;
3196 if (st != NULL)
3198 *result = st;
3199 return 0;
3202 return gfc_get_sym_tree (name, gfc_current_ns, result, false);
3207 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
3209 int i;
3210 gfc_symtree *st;
3212 i = gfc_get_ha_sym_tree (name, &st);
3214 if (st)
3215 *result = st->n.sym;
3216 else
3217 *result = NULL;
3219 return i;
3223 /* Search for the symtree belonging to a gfc_common_head; we cannot use
3224 head->name as the common_root symtree's name might be mangled. */
3226 static gfc_symtree *
3227 find_common_symtree (gfc_symtree *st, gfc_common_head *head)
3230 gfc_symtree *result;
3232 if (st == NULL)
3233 return NULL;
3235 if (st->n.common == head)
3236 return st;
3238 result = find_common_symtree (st->left, head);
3239 if (!result)
3240 result = find_common_symtree (st->right, head);
3242 return result;
3246 /* Clear the given storage, and make it the current change set for registering
3247 changed symbols. Its contents are freed after a call to
3248 gfc_restore_last_undo_checkpoint or gfc_drop_last_undo_checkpoint, but
3249 it is up to the caller to free the storage itself. It is usually a local
3250 variable, so there is nothing to do anyway. */
3252 void
3253 gfc_new_undo_checkpoint (gfc_undo_change_set &chg_syms)
3255 chg_syms.syms = vNULL;
3256 chg_syms.tbps = vNULL;
3257 chg_syms.previous = latest_undo_chgset;
3258 latest_undo_chgset = &chg_syms;
3262 /* Restore previous state of symbol. Just copy simple stuff. */
3264 static void
3265 restore_old_symbol (gfc_symbol *p)
3267 gfc_symbol *old;
3269 p->mark = 0;
3270 old = p->old_symbol;
3272 p->ts.type = old->ts.type;
3273 p->ts.kind = old->ts.kind;
3275 p->attr = old->attr;
3277 if (p->value != old->value)
3279 gcc_checking_assert (old->value == NULL);
3280 gfc_free_expr (p->value);
3281 p->value = NULL;
3284 if (p->as != old->as)
3286 if (p->as)
3287 gfc_free_array_spec (p->as);
3288 p->as = old->as;
3291 p->generic = old->generic;
3292 p->component_access = old->component_access;
3294 if (p->namelist != NULL && old->namelist == NULL)
3296 gfc_free_namelist (p->namelist);
3297 p->namelist = NULL;
3299 else
3301 if (p->namelist_tail != old->namelist_tail)
3303 gfc_free_namelist (old->namelist_tail->next);
3304 old->namelist_tail->next = NULL;
3308 p->namelist_tail = old->namelist_tail;
3310 if (p->formal != old->formal)
3312 gfc_free_formal_arglist (p->formal);
3313 p->formal = old->formal;
3316 set_symbol_common_block (p, old->common_block);
3317 p->common_head = old->common_head;
3319 p->old_symbol = old->old_symbol;
3320 free (old);
3324 /* Frees the internal data of a gfc_undo_change_set structure. Doesn't free
3325 the structure itself. */
3327 static void
3328 free_undo_change_set_data (gfc_undo_change_set &cs)
3330 cs.syms.release ();
3331 cs.tbps.release ();
3335 /* Given a change set pointer, free its target's contents and update it with
3336 the address of the previous change set. Note that only the contents are
3337 freed, not the target itself (the contents' container). It is not a problem
3338 as the latter will be a local variable usually. */
3340 static void
3341 pop_undo_change_set (gfc_undo_change_set *&cs)
3343 free_undo_change_set_data (*cs);
3344 cs = cs->previous;
3348 static void free_old_symbol (gfc_symbol *sym);
3351 /* Merges the current change set into the previous one. The changes themselves
3352 are left untouched; only one checkpoint is forgotten. */
3354 void
3355 gfc_drop_last_undo_checkpoint (void)
3357 gfc_symbol *s, *t;
3358 unsigned i, j;
3360 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3362 /* No need to loop in this case. */
3363 if (s->old_symbol == NULL)
3364 continue;
3366 /* Remove the duplicate symbols. */
3367 FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
3368 if (t == s)
3370 latest_undo_chgset->previous->syms.unordered_remove (j);
3372 /* S->OLD_SYMBOL is the backup symbol for S as it was at the
3373 last checkpoint. We drop that checkpoint, so S->OLD_SYMBOL
3374 shall contain from now on the backup symbol for S as it was
3375 at the checkpoint before. */
3376 if (s->old_symbol->gfc_new)
3378 gcc_assert (s->old_symbol->old_symbol == NULL);
3379 s->gfc_new = s->old_symbol->gfc_new;
3380 free_old_symbol (s);
3382 else
3383 restore_old_symbol (s->old_symbol);
3384 break;
3388 latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
3389 latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
3391 pop_undo_change_set (latest_undo_chgset);
3395 /* Undoes all the changes made to symbols since the previous checkpoint.
3396 This subroutine is made simpler due to the fact that attributes are
3397 never removed once added. */
3399 void
3400 gfc_restore_last_undo_checkpoint (void)
3402 gfc_symbol *p;
3403 unsigned i;
3405 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3407 /* Symbol in a common block was new. Or was old and just put in common */
3408 if (p->common_block
3409 && (p->gfc_new || !p->old_symbol->common_block))
3411 /* If the symbol was added to any common block, it
3412 needs to be removed to stop the resolver looking
3413 for a (possibly) dead symbol. */
3414 if (p->common_block->head == p && !p->common_next)
3416 gfc_symtree st, *st0;
3417 st0 = find_common_symtree (p->ns->common_root,
3418 p->common_block);
3419 if (st0)
3421 st.name = st0->name;
3422 gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
3423 free (st0);
3427 if (p->common_block->head == p)
3428 p->common_block->head = p->common_next;
3429 else
3431 gfc_symbol *cparent, *csym;
3433 cparent = p->common_block->head;
3434 csym = cparent->common_next;
3436 while (csym != p)
3438 cparent = csym;
3439 csym = csym->common_next;
3442 gcc_assert(cparent->common_next == p);
3443 cparent->common_next = csym->common_next;
3445 p->common_next = NULL;
3447 if (p->gfc_new)
3449 /* The derived type is saved in the symtree with the first
3450 letter capitalized; the all lower-case version to the
3451 derived type contains its associated generic function. */
3452 if (gfc_fl_struct (p->attr.flavor))
3453 gfc_delete_symtree (&p->ns->sym_root,gfc_dt_upper_string (p->name));
3454 else
3455 gfc_delete_symtree (&p->ns->sym_root, p->name);
3457 gfc_release_symbol (p);
3459 else
3460 restore_old_symbol (p);
3463 latest_undo_chgset->syms.truncate (0);
3464 latest_undo_chgset->tbps.truncate (0);
3466 if (!single_undo_checkpoint_p ())
3467 pop_undo_change_set (latest_undo_chgset);
3471 /* Makes sure that there is only one set of changes; in other words we haven't
3472 forgotten to pair a call to gfc_new_checkpoint with a call to either
3473 gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint. */
3475 static void
3476 enforce_single_undo_checkpoint (void)
3478 gcc_checking_assert (single_undo_checkpoint_p ());
3482 /* Undoes all the changes made to symbols in the current statement. */
3484 void
3485 gfc_undo_symbols (void)
3487 enforce_single_undo_checkpoint ();
3488 gfc_restore_last_undo_checkpoint ();
3492 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
3493 components of old_symbol that might need deallocation are the "allocatables"
3494 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
3495 namelist_tail. In case these differ between old_symbol and sym, it's just
3496 because sym->namelist has gotten a few more items. */
3498 static void
3499 free_old_symbol (gfc_symbol *sym)
3502 if (sym->old_symbol == NULL)
3503 return;
3505 if (sym->old_symbol->as != sym->as)
3506 gfc_free_array_spec (sym->old_symbol->as);
3508 if (sym->old_symbol->value != sym->value)
3509 gfc_free_expr (sym->old_symbol->value);
3511 if (sym->old_symbol->formal != sym->formal)
3512 gfc_free_formal_arglist (sym->old_symbol->formal);
3514 free (sym->old_symbol);
3515 sym->old_symbol = NULL;
3519 /* Makes the changes made in the current statement permanent-- gets
3520 rid of undo information. */
3522 void
3523 gfc_commit_symbols (void)
3525 gfc_symbol *p;
3526 gfc_typebound_proc *tbp;
3527 unsigned i;
3529 enforce_single_undo_checkpoint ();
3531 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3533 p->mark = 0;
3534 p->gfc_new = 0;
3535 free_old_symbol (p);
3537 latest_undo_chgset->syms.truncate (0);
3539 FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
3540 tbp->error = 0;
3541 latest_undo_chgset->tbps.truncate (0);
3545 /* Makes the changes made in one symbol permanent -- gets rid of undo
3546 information. */
3548 void
3549 gfc_commit_symbol (gfc_symbol *sym)
3551 gfc_symbol *p;
3552 unsigned i;
3554 enforce_single_undo_checkpoint ();
3556 FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
3557 if (p == sym)
3559 latest_undo_chgset->syms.unordered_remove (i);
3560 break;
3563 sym->mark = 0;
3564 sym->gfc_new = 0;
3566 free_old_symbol (sym);
3570 /* Recursively free trees containing type-bound procedures. */
3572 static void
3573 free_tb_tree (gfc_symtree *t)
3575 if (t == NULL)
3576 return;
3578 free_tb_tree (t->left);
3579 free_tb_tree (t->right);
3581 /* TODO: Free type-bound procedure structs themselves; probably needs some
3582 sort of ref-counting mechanism. */
3584 free (t);
3588 /* Recursive function that deletes an entire tree and all the common
3589 head structures it points to. */
3591 static void
3592 free_common_tree (gfc_symtree * common_tree)
3594 if (common_tree == NULL)
3595 return;
3597 free_common_tree (common_tree->left);
3598 free_common_tree (common_tree->right);
3600 free (common_tree);
3604 /* Recursive function that deletes an entire tree and all the common
3605 head structures it points to. */
3607 static void
3608 free_omp_udr_tree (gfc_symtree * omp_udr_tree)
3610 if (omp_udr_tree == NULL)
3611 return;
3613 free_omp_udr_tree (omp_udr_tree->left);
3614 free_omp_udr_tree (omp_udr_tree->right);
3616 gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
3617 free (omp_udr_tree);
3621 /* Recursive function that deletes an entire tree and all the user
3622 operator nodes that it contains. */
3624 static void
3625 free_uop_tree (gfc_symtree *uop_tree)
3627 if (uop_tree == NULL)
3628 return;
3630 free_uop_tree (uop_tree->left);
3631 free_uop_tree (uop_tree->right);
3633 gfc_free_interface (uop_tree->n.uop->op);
3634 free (uop_tree->n.uop);
3635 free (uop_tree);
3639 /* Recursive function that deletes an entire tree and all the symbols
3640 that it contains. */
3642 static void
3643 free_sym_tree (gfc_symtree *sym_tree)
3645 if (sym_tree == NULL)
3646 return;
3648 free_sym_tree (sym_tree->left);
3649 free_sym_tree (sym_tree->right);
3651 gfc_release_symbol (sym_tree->n.sym);
3652 free (sym_tree);
3656 /* Free the derived type list. */
3658 void
3659 gfc_free_dt_list (void)
3661 gfc_dt_list *dt, *n;
3663 for (dt = gfc_derived_types; dt; dt = n)
3665 n = dt->next;
3666 free (dt);
3669 gfc_derived_types = NULL;
3673 /* Free the gfc_equiv_info's. */
3675 static void
3676 gfc_free_equiv_infos (gfc_equiv_info *s)
3678 if (s == NULL)
3679 return;
3680 gfc_free_equiv_infos (s->next);
3681 free (s);
3685 /* Free the gfc_equiv_lists. */
3687 static void
3688 gfc_free_equiv_lists (gfc_equiv_list *l)
3690 if (l == NULL)
3691 return;
3692 gfc_free_equiv_lists (l->next);
3693 gfc_free_equiv_infos (l->equiv);
3694 free (l);
3698 /* Free a finalizer procedure list. */
3700 void
3701 gfc_free_finalizer (gfc_finalizer* el)
3703 if (el)
3705 gfc_release_symbol (el->proc_sym);
3706 free (el);
3710 static void
3711 gfc_free_finalizer_list (gfc_finalizer* list)
3713 while (list)
3715 gfc_finalizer* current = list;
3716 list = list->next;
3717 gfc_free_finalizer (current);
3722 /* Create a new gfc_charlen structure and add it to a namespace.
3723 If 'old_cl' is given, the newly created charlen will be a copy of it. */
3725 gfc_charlen*
3726 gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
3728 gfc_charlen *cl;
3729 cl = gfc_get_charlen ();
3731 /* Copy old_cl. */
3732 if (old_cl)
3734 /* Put into namespace, but don't allow reject_statement
3735 to free it if old_cl is given. */
3736 gfc_charlen **prev = &ns->cl_list;
3737 cl->next = ns->old_cl_list;
3738 while (*prev != ns->old_cl_list)
3739 prev = &(*prev)->next;
3740 *prev = cl;
3741 ns->old_cl_list = cl;
3742 cl->length = gfc_copy_expr (old_cl->length);
3743 cl->length_from_typespec = old_cl->length_from_typespec;
3744 cl->backend_decl = old_cl->backend_decl;
3745 cl->passed_length = old_cl->passed_length;
3746 cl->resolved = old_cl->resolved;
3748 else
3750 /* Put into namespace. */
3751 cl->next = ns->cl_list;
3752 ns->cl_list = cl;
3755 return cl;
3759 /* Free the charlen list from cl to end (end is not freed).
3760 Free the whole list if end is NULL. */
3762 void
3763 gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
3765 gfc_charlen *cl2;
3767 for (; cl != end; cl = cl2)
3769 gcc_assert (cl);
3771 cl2 = cl->next;
3772 gfc_free_expr (cl->length);
3773 free (cl);
3778 /* Free entry list structs. */
3780 static void
3781 free_entry_list (gfc_entry_list *el)
3783 gfc_entry_list *next;
3785 if (el == NULL)
3786 return;
3788 next = el->next;
3789 free (el);
3790 free_entry_list (next);
3794 /* Free a namespace structure and everything below it. Interface
3795 lists associated with intrinsic operators are not freed. These are
3796 taken care of when a specific name is freed. */
3798 void
3799 gfc_free_namespace (gfc_namespace *ns)
3801 gfc_namespace *p, *q;
3802 int i;
3804 if (ns == NULL)
3805 return;
3807 ns->refs--;
3808 if (ns->refs > 0)
3809 return;
3810 gcc_assert (ns->refs == 0);
3812 gfc_free_statements (ns->code);
3814 free_sym_tree (ns->sym_root);
3815 free_uop_tree (ns->uop_root);
3816 free_common_tree (ns->common_root);
3817 free_omp_udr_tree (ns->omp_udr_root);
3818 free_tb_tree (ns->tb_sym_root);
3819 free_tb_tree (ns->tb_uop_root);
3820 gfc_free_finalizer_list (ns->finalizers);
3821 gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
3822 gfc_free_charlen (ns->cl_list, NULL);
3823 free_st_labels (ns->st_labels);
3825 free_entry_list (ns->entries);
3826 gfc_free_equiv (ns->equiv);
3827 gfc_free_equiv_lists (ns->equiv_lists);
3828 gfc_free_use_stmts (ns->use_stmts);
3830 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
3831 gfc_free_interface (ns->op[i]);
3833 gfc_free_data (ns->data);
3834 p = ns->contained;
3835 free (ns);
3837 /* Recursively free any contained namespaces. */
3838 while (p != NULL)
3840 q = p;
3841 p = p->sibling;
3842 gfc_free_namespace (q);
3847 void
3848 gfc_symbol_init_2 (void)
3851 gfc_current_ns = gfc_get_namespace (NULL, 0);
3855 void
3856 gfc_symbol_done_2 (void)
3858 gfc_free_namespace (gfc_current_ns);
3859 gfc_current_ns = NULL;
3860 gfc_free_dt_list ();
3862 enforce_single_undo_checkpoint ();
3863 free_undo_change_set_data (*latest_undo_chgset);
3867 /* Count how many nodes a symtree has. */
3869 static unsigned
3870 count_st_nodes (const gfc_symtree *st)
3872 unsigned nodes;
3873 if (!st)
3874 return 0;
3876 nodes = count_st_nodes (st->left);
3877 nodes++;
3878 nodes += count_st_nodes (st->right);
3880 return nodes;
3884 /* Convert symtree tree into symtree vector. */
3886 static unsigned
3887 fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
3889 if (!st)
3890 return node_cntr;
3892 node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
3893 st_vec[node_cntr++] = st;
3894 node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
3896 return node_cntr;
3900 /* Traverse namespace. As the functions might modify the symtree, we store the
3901 symtree as a vector and operate on this vector. Note: We assume that
3902 sym_func or st_func never deletes nodes from the symtree - only adding is
3903 allowed. Additionally, newly added nodes are not traversed. */
3905 static void
3906 do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
3907 void (*sym_func) (gfc_symbol *))
3909 gfc_symtree **st_vec;
3910 unsigned nodes, i, node_cntr;
3912 gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
3913 nodes = count_st_nodes (st);
3914 st_vec = XALLOCAVEC (gfc_symtree *, nodes);
3915 node_cntr = 0;
3916 fill_st_vector (st, st_vec, node_cntr);
3918 if (sym_func)
3920 /* Clear marks. */
3921 for (i = 0; i < nodes; i++)
3922 st_vec[i]->n.sym->mark = 0;
3923 for (i = 0; i < nodes; i++)
3924 if (!st_vec[i]->n.sym->mark)
3926 (*sym_func) (st_vec[i]->n.sym);
3927 st_vec[i]->n.sym->mark = 1;
3930 else
3931 for (i = 0; i < nodes; i++)
3932 (*st_func) (st_vec[i]);
3936 /* Recursively traverse the symtree nodes. */
3938 void
3939 gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
3941 do_traverse_symtree (st, st_func, NULL);
3945 /* Call a given function for all symbols in the namespace. We take
3946 care that each gfc_symbol node is called exactly once. */
3948 void
3949 gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
3951 do_traverse_symtree (ns->sym_root, NULL, sym_func);
3955 /* Return TRUE when name is the name of an intrinsic type. */
3957 bool
3958 gfc_is_intrinsic_typename (const char *name)
3960 if (strcmp (name, "integer") == 0
3961 || strcmp (name, "real") == 0
3962 || strcmp (name, "character") == 0
3963 || strcmp (name, "logical") == 0
3964 || strcmp (name, "complex") == 0
3965 || strcmp (name, "doubleprecision") == 0
3966 || strcmp (name, "doublecomplex") == 0)
3967 return true;
3968 else
3969 return false;
3973 /* Return TRUE if the symbol is an automatic variable. */
3975 static bool
3976 gfc_is_var_automatic (gfc_symbol *sym)
3978 /* Pointer and allocatable variables are never automatic. */
3979 if (sym->attr.pointer || sym->attr.allocatable)
3980 return false;
3981 /* Check for arrays with non-constant size. */
3982 if (sym->attr.dimension && sym->as
3983 && !gfc_is_compile_time_shape (sym->as))
3984 return true;
3985 /* Check for non-constant length character variables. */
3986 if (sym->ts.type == BT_CHARACTER
3987 && sym->ts.u.cl
3988 && !gfc_is_constant_expr (sym->ts.u.cl->length))
3989 return true;
3990 return false;
3993 /* Given a symbol, mark it as SAVEd if it is allowed. */
3995 static void
3996 save_symbol (gfc_symbol *sym)
3999 if (sym->attr.use_assoc)
4000 return;
4002 if (sym->attr.in_common
4003 || sym->attr.dummy
4004 || sym->attr.result
4005 || sym->attr.flavor != FL_VARIABLE)
4006 return;
4007 /* Automatic objects are not saved. */
4008 if (gfc_is_var_automatic (sym))
4009 return;
4010 gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
4014 /* Mark those symbols which can be SAVEd as such. */
4016 void
4017 gfc_save_all (gfc_namespace *ns)
4019 gfc_traverse_ns (ns, save_symbol);
4023 /* Make sure that no changes to symbols are pending. */
4025 void
4026 gfc_enforce_clean_symbol_state(void)
4028 enforce_single_undo_checkpoint ();
4029 gcc_assert (latest_undo_chgset->syms.is_empty ());
4033 /************** Global symbol handling ************/
4036 /* Search a tree for the global symbol. */
4038 gfc_gsymbol *
4039 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
4041 int c;
4043 if (symbol == NULL)
4044 return NULL;
4046 while (symbol)
4048 c = strcmp (name, symbol->name);
4049 if (!c)
4050 return symbol;
4052 symbol = (c < 0) ? symbol->left : symbol->right;
4055 return NULL;
4059 /* Compare two global symbols. Used for managing the BB tree. */
4061 static int
4062 gsym_compare (void *_s1, void *_s2)
4064 gfc_gsymbol *s1, *s2;
4066 s1 = (gfc_gsymbol *) _s1;
4067 s2 = (gfc_gsymbol *) _s2;
4068 return strcmp (s1->name, s2->name);
4072 /* Get a global symbol, creating it if it doesn't exist. */
4074 gfc_gsymbol *
4075 gfc_get_gsymbol (const char *name)
4077 gfc_gsymbol *s;
4079 s = gfc_find_gsymbol (gfc_gsym_root, name);
4080 if (s != NULL)
4081 return s;
4083 s = XCNEW (gfc_gsymbol);
4084 s->type = GSYM_UNKNOWN;
4085 s->name = gfc_get_string (name);
4087 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
4089 return s;
4093 static gfc_symbol *
4094 get_iso_c_binding_dt (int sym_id)
4096 gfc_dt_list *dt_list;
4098 dt_list = gfc_derived_types;
4100 /* Loop through the derived types in the name list, searching for
4101 the desired symbol from iso_c_binding. Search the parent namespaces
4102 if necessary and requested to (parent_flag). */
4103 while (dt_list != NULL)
4105 if (dt_list->derived->from_intmod != INTMOD_NONE
4106 && dt_list->derived->intmod_sym_id == sym_id)
4107 return dt_list->derived;
4109 dt_list = dt_list->next;
4112 return NULL;
4116 /* Verifies that the given derived type symbol, derived_sym, is interoperable
4117 with C. This is necessary for any derived type that is BIND(C) and for
4118 derived types that are parameters to functions that are BIND(C). All
4119 fields of the derived type are required to be interoperable, and are tested
4120 for such. If an error occurs, the errors are reported here, allowing for
4121 multiple errors to be handled for a single derived type. */
4123 bool
4124 verify_bind_c_derived_type (gfc_symbol *derived_sym)
4126 gfc_component *curr_comp = NULL;
4127 bool is_c_interop = false;
4128 bool retval = true;
4130 if (derived_sym == NULL)
4131 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
4132 "unexpectedly NULL");
4134 /* If we've already looked at this derived symbol, do not look at it again
4135 so we don't repeat warnings/errors. */
4136 if (derived_sym->ts.is_c_interop)
4137 return true;
4139 /* The derived type must have the BIND attribute to be interoperable
4140 J3/04-007, Section 15.2.3. */
4141 if (derived_sym->attr.is_bind_c != 1)
4143 derived_sym->ts.is_c_interop = 0;
4144 gfc_error_now ("Derived type %qs declared at %L must have the BIND "
4145 "attribute to be C interoperable", derived_sym->name,
4146 &(derived_sym->declared_at));
4147 retval = false;
4150 curr_comp = derived_sym->components;
4152 /* Fortran 2003 allows an empty derived type. C99 appears to disallow an
4153 empty struct. Section 15.2 in Fortran 2003 states: "The following
4154 subclauses define the conditions under which a Fortran entity is
4155 interoperable. If a Fortran entity is interoperable, an equivalent
4156 entity may be defined by means of C and the Fortran entity is said
4157 to be interoperable with the C entity. There does not have to be such
4158 an interoperating C entity."
4160 if (curr_comp == NULL)
4162 gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L is empty, "
4163 "and may be inaccessible by the C companion processor",
4164 derived_sym->name, &(derived_sym->declared_at));
4165 derived_sym->ts.is_c_interop = 1;
4166 derived_sym->attr.is_bind_c = 1;
4167 return true;
4171 /* Initialize the derived type as being C interoperable.
4172 If we find an error in the components, this will be set false. */
4173 derived_sym->ts.is_c_interop = 1;
4175 /* Loop through the list of components to verify that the kind of
4176 each is a C interoperable type. */
4179 /* The components cannot be pointers (fortran sense).
4180 J3/04-007, Section 15.2.3, C1505. */
4181 if (curr_comp->attr.pointer != 0)
4183 gfc_error ("Component %qs at %L cannot have the "
4184 "POINTER attribute because it is a member "
4185 "of the BIND(C) derived type %qs at %L",
4186 curr_comp->name, &(curr_comp->loc),
4187 derived_sym->name, &(derived_sym->declared_at));
4188 retval = false;
4191 if (curr_comp->attr.proc_pointer != 0)
4193 gfc_error ("Procedure pointer component %qs at %L cannot be a member"
4194 " of the BIND(C) derived type %qs at %L", curr_comp->name,
4195 &curr_comp->loc, derived_sym->name,
4196 &derived_sym->declared_at);
4197 retval = false;
4200 /* The components cannot be allocatable.
4201 J3/04-007, Section 15.2.3, C1505. */
4202 if (curr_comp->attr.allocatable != 0)
4204 gfc_error ("Component %qs at %L cannot have the "
4205 "ALLOCATABLE attribute because it is a member "
4206 "of the BIND(C) derived type %qs at %L",
4207 curr_comp->name, &(curr_comp->loc),
4208 derived_sym->name, &(derived_sym->declared_at));
4209 retval = false;
4212 /* BIND(C) derived types must have interoperable components. */
4213 if (curr_comp->ts.type == BT_DERIVED
4214 && curr_comp->ts.u.derived->ts.is_iso_c != 1
4215 && curr_comp->ts.u.derived != derived_sym)
4217 /* This should be allowed; the draft says a derived-type can not
4218 have type parameters if it is has the BIND attribute. Type
4219 parameters seem to be for making parameterized derived types.
4220 There's no need to verify the type if it is c_ptr/c_funptr. */
4221 retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
4223 else
4225 /* Grab the typespec for the given component and test the kind. */
4226 is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
4228 if (!is_c_interop)
4230 /* Report warning and continue since not fatal. The
4231 draft does specify a constraint that requires all fields
4232 to interoperate, but if the user says real(4), etc., it
4233 may interoperate with *something* in C, but the compiler
4234 most likely won't know exactly what. Further, it may not
4235 interoperate with the same data type(s) in C if the user
4236 recompiles with different flags (e.g., -m32 and -m64 on
4237 x86_64 and using integer(4) to claim interop with a
4238 C_LONG). */
4239 if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
4240 /* If the derived type is bind(c), all fields must be
4241 interop. */
4242 gfc_warning (OPT_Wc_binding_type,
4243 "Component %qs in derived type %qs at %L "
4244 "may not be C interoperable, even though "
4245 "derived type %qs is BIND(C)",
4246 curr_comp->name, derived_sym->name,
4247 &(curr_comp->loc), derived_sym->name);
4248 else if (warn_c_binding_type)
4249 /* If derived type is param to bind(c) routine, or to one
4250 of the iso_c_binding procs, it must be interoperable, so
4251 all fields must interop too. */
4252 gfc_warning (OPT_Wc_binding_type,
4253 "Component %qs in derived type %qs at %L "
4254 "may not be C interoperable",
4255 curr_comp->name, derived_sym->name,
4256 &(curr_comp->loc));
4260 curr_comp = curr_comp->next;
4261 } while (curr_comp != NULL);
4264 /* Make sure we don't have conflicts with the attributes. */
4265 if (derived_sym->attr.access == ACCESS_PRIVATE)
4267 gfc_error ("Derived type %qs at %L cannot be declared with both "
4268 "PRIVATE and BIND(C) attributes", derived_sym->name,
4269 &(derived_sym->declared_at));
4270 retval = false;
4273 if (derived_sym->attr.sequence != 0)
4275 gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
4276 "attribute because it is BIND(C)", derived_sym->name,
4277 &(derived_sym->declared_at));
4278 retval = false;
4281 /* Mark the derived type as not being C interoperable if we found an
4282 error. If there were only warnings, proceed with the assumption
4283 it's interoperable. */
4284 if (!retval)
4285 derived_sym->ts.is_c_interop = 0;
4287 return retval;
4291 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
4293 static bool
4294 gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
4296 gfc_constructor *c;
4298 gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
4299 dt_symtree->n.sym->attr.referenced = 1;
4301 tmp_sym->attr.is_c_interop = 1;
4302 tmp_sym->attr.is_bind_c = 1;
4303 tmp_sym->ts.is_c_interop = 1;
4304 tmp_sym->ts.is_iso_c = 1;
4305 tmp_sym->ts.type = BT_DERIVED;
4306 tmp_sym->ts.f90_type = BT_VOID;
4307 tmp_sym->attr.flavor = FL_PARAMETER;
4308 tmp_sym->ts.u.derived = dt_symtree->n.sym;
4310 /* Set the c_address field of c_null_ptr and c_null_funptr to
4311 the value of NULL. */
4312 tmp_sym->value = gfc_get_expr ();
4313 tmp_sym->value->expr_type = EXPR_STRUCTURE;
4314 tmp_sym->value->ts.type = BT_DERIVED;
4315 tmp_sym->value->ts.f90_type = BT_VOID;
4316 tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
4317 gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
4318 c = gfc_constructor_first (tmp_sym->value->value.constructor);
4319 c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
4320 c->expr->ts.is_iso_c = 1;
4322 return true;
4326 /* Add a formal argument, gfc_formal_arglist, to the
4327 end of the given list of arguments. Set the reference to the
4328 provided symbol, param_sym, in the argument. */
4330 static void
4331 add_formal_arg (gfc_formal_arglist **head,
4332 gfc_formal_arglist **tail,
4333 gfc_formal_arglist *formal_arg,
4334 gfc_symbol *param_sym)
4336 /* Put in list, either as first arg or at the tail (curr arg). */
4337 if (*head == NULL)
4338 *head = *tail = formal_arg;
4339 else
4341 (*tail)->next = formal_arg;
4342 (*tail) = formal_arg;
4345 (*tail)->sym = param_sym;
4346 (*tail)->next = NULL;
4348 return;
4352 /* Add a procedure interface to the given symbol (i.e., store a
4353 reference to the list of formal arguments). */
4355 static void
4356 add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
4359 sym->formal = formal;
4360 sym->attr.if_source = source;
4364 /* Copy the formal args from an existing symbol, src, into a new
4365 symbol, dest. New formal args are created, and the description of
4366 each arg is set according to the existing ones. This function is
4367 used when creating procedure declaration variables from a procedure
4368 declaration statement (see match_proc_decl()) to create the formal
4369 args based on the args of a given named interface.
4371 When an actual argument list is provided, skip the absent arguments.
4372 To be used together with gfc_se->ignore_optional. */
4374 void
4375 gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
4376 gfc_actual_arglist *actual)
4378 gfc_formal_arglist *head = NULL;
4379 gfc_formal_arglist *tail = NULL;
4380 gfc_formal_arglist *formal_arg = NULL;
4381 gfc_intrinsic_arg *curr_arg = NULL;
4382 gfc_formal_arglist *formal_prev = NULL;
4383 gfc_actual_arglist *act_arg = actual;
4384 /* Save current namespace so we can change it for formal args. */
4385 gfc_namespace *parent_ns = gfc_current_ns;
4387 /* Create a new namespace, which will be the formal ns (namespace
4388 of the formal args). */
4389 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
4390 gfc_current_ns->proc_name = dest;
4392 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4394 /* Skip absent arguments. */
4395 if (actual)
4397 gcc_assert (act_arg != NULL);
4398 if (act_arg->expr == NULL)
4400 act_arg = act_arg->next;
4401 continue;
4403 act_arg = act_arg->next;
4405 formal_arg = gfc_get_formal_arglist ();
4406 gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
4408 /* May need to copy more info for the symbol. */
4409 formal_arg->sym->ts = curr_arg->ts;
4410 formal_arg->sym->attr.optional = curr_arg->optional;
4411 formal_arg->sym->attr.value = curr_arg->value;
4412 formal_arg->sym->attr.intent = curr_arg->intent;
4413 formal_arg->sym->attr.flavor = FL_VARIABLE;
4414 formal_arg->sym->attr.dummy = 1;
4416 if (formal_arg->sym->ts.type == BT_CHARACTER)
4417 formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4419 /* If this isn't the first arg, set up the next ptr. For the
4420 last arg built, the formal_arg->next will never get set to
4421 anything other than NULL. */
4422 if (formal_prev != NULL)
4423 formal_prev->next = formal_arg;
4424 else
4425 formal_arg->next = NULL;
4427 formal_prev = formal_arg;
4429 /* Add arg to list of formal args. */
4430 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4432 /* Validate changes. */
4433 gfc_commit_symbol (formal_arg->sym);
4436 /* Add the interface to the symbol. */
4437 add_proc_interface (dest, IFSRC_DECL, head);
4439 /* Store the formal namespace information. */
4440 if (dest->formal != NULL)
4441 /* The current ns should be that for the dest proc. */
4442 dest->formal_ns = gfc_current_ns;
4443 /* Restore the current namespace to what it was on entry. */
4444 gfc_current_ns = parent_ns;
4448 static int
4449 std_for_isocbinding_symbol (int id)
4451 switch (id)
4453 #define NAMED_INTCST(a,b,c,d) \
4454 case a:\
4455 return d;
4456 #include "iso-c-binding.def"
4457 #undef NAMED_INTCST
4459 #define NAMED_FUNCTION(a,b,c,d) \
4460 case a:\
4461 return d;
4462 #define NAMED_SUBROUTINE(a,b,c,d) \
4463 case a:\
4464 return d;
4465 #include "iso-c-binding.def"
4466 #undef NAMED_FUNCTION
4467 #undef NAMED_SUBROUTINE
4469 default:
4470 return GFC_STD_F2003;
4474 /* Generate the given set of C interoperable kind objects, or all
4475 interoperable kinds. This function will only be given kind objects
4476 for valid iso_c_binding defined types because this is verified when
4477 the 'use' statement is parsed. If the user gives an 'only' clause,
4478 the specific kinds are looked up; if they don't exist, an error is
4479 reported. If the user does not give an 'only' clause, all
4480 iso_c_binding symbols are generated. If a list of specific kinds
4481 is given, it must have a NULL in the first empty spot to mark the
4482 end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
4483 point to the symtree for c_(fun)ptr. */
4485 gfc_symtree *
4486 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
4487 const char *local_name, gfc_symtree *dt_symtree,
4488 bool hidden)
4490 const char *const name = (local_name && local_name[0])
4491 ? local_name : c_interop_kinds_table[s].name;
4492 gfc_symtree *tmp_symtree;
4493 gfc_symbol *tmp_sym = NULL;
4494 int index;
4496 if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
4497 return NULL;
4499 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
4500 if (hidden
4501 && (!tmp_symtree || !tmp_symtree->n.sym
4502 || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
4503 || tmp_symtree->n.sym->intmod_sym_id != s))
4504 tmp_symtree = NULL;
4506 /* Already exists in this scope so don't re-add it. */
4507 if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
4508 && (!tmp_sym->attr.generic
4509 || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
4510 && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
4512 if (tmp_sym->attr.flavor == FL_DERIVED
4513 && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
4515 gfc_dt_list *dt_list;
4516 dt_list = gfc_get_dt_list ();
4517 dt_list->derived = tmp_sym;
4518 dt_list->next = gfc_derived_types;
4519 gfc_derived_types = dt_list;
4522 return tmp_symtree;
4525 /* Create the sym tree in the current ns. */
4526 if (hidden)
4528 tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
4529 tmp_sym = gfc_new_symbol (name, gfc_current_ns);
4531 /* Add to the list of tentative symbols. */
4532 latest_undo_chgset->syms.safe_push (tmp_sym);
4533 tmp_sym->old_symbol = NULL;
4534 tmp_sym->mark = 1;
4535 tmp_sym->gfc_new = 1;
4537 tmp_symtree->n.sym = tmp_sym;
4538 tmp_sym->refs++;
4540 else
4542 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
4543 gcc_assert (tmp_symtree);
4544 tmp_sym = tmp_symtree->n.sym;
4547 /* Say what module this symbol belongs to. */
4548 tmp_sym->module = gfc_get_string (mod_name);
4549 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
4550 tmp_sym->intmod_sym_id = s;
4551 tmp_sym->attr.is_iso_c = 1;
4552 tmp_sym->attr.use_assoc = 1;
4554 gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
4555 || s == ISOCBINDING_NULL_PTR);
4557 switch (s)
4560 #define NAMED_INTCST(a,b,c,d) case a :
4561 #define NAMED_REALCST(a,b,c,d) case a :
4562 #define NAMED_CMPXCST(a,b,c,d) case a :
4563 #define NAMED_LOGCST(a,b,c) case a :
4564 #define NAMED_CHARKNDCST(a,b,c) case a :
4565 #include "iso-c-binding.def"
4567 tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4568 c_interop_kinds_table[s].value);
4570 /* Initialize an integer constant expression node. */
4571 tmp_sym->attr.flavor = FL_PARAMETER;
4572 tmp_sym->ts.type = BT_INTEGER;
4573 tmp_sym->ts.kind = gfc_default_integer_kind;
4575 /* Mark this type as a C interoperable one. */
4576 tmp_sym->ts.is_c_interop = 1;
4577 tmp_sym->ts.is_iso_c = 1;
4578 tmp_sym->value->ts.is_c_interop = 1;
4579 tmp_sym->value->ts.is_iso_c = 1;
4580 tmp_sym->attr.is_c_interop = 1;
4582 /* Tell what f90 type this c interop kind is valid. */
4583 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
4585 break;
4588 #define NAMED_CHARCST(a,b,c) case a :
4589 #include "iso-c-binding.def"
4591 /* Initialize an integer constant expression node for the
4592 length of the character. */
4593 tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
4594 &gfc_current_locus, NULL, 1);
4595 tmp_sym->value->ts.is_c_interop = 1;
4596 tmp_sym->value->ts.is_iso_c = 1;
4597 tmp_sym->value->value.character.length = 1;
4598 tmp_sym->value->value.character.string[0]
4599 = (gfc_char_t) c_interop_kinds_table[s].value;
4600 tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4601 tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
4602 NULL, 1);
4604 /* May not need this in both attr and ts, but do need in
4605 attr for writing module file. */
4606 tmp_sym->attr.is_c_interop = 1;
4608 tmp_sym->attr.flavor = FL_PARAMETER;
4609 tmp_sym->ts.type = BT_CHARACTER;
4611 /* Need to set it to the C_CHAR kind. */
4612 tmp_sym->ts.kind = gfc_default_character_kind;
4614 /* Mark this type as a C interoperable one. */
4615 tmp_sym->ts.is_c_interop = 1;
4616 tmp_sym->ts.is_iso_c = 1;
4618 /* Tell what f90 type this c interop kind is valid. */
4619 tmp_sym->ts.f90_type = BT_CHARACTER;
4621 break;
4623 case ISOCBINDING_PTR:
4624 case ISOCBINDING_FUNPTR:
4626 gfc_symbol *dt_sym;
4627 gfc_dt_list **dt_list_ptr = NULL;
4628 gfc_component *tmp_comp = NULL;
4630 /* Generate real derived type. */
4631 if (hidden)
4632 dt_sym = tmp_sym;
4633 else
4635 const char *hidden_name;
4636 gfc_interface *intr, *head;
4638 hidden_name = gfc_dt_upper_string (tmp_sym->name);
4639 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
4640 hidden_name);
4641 gcc_assert (tmp_symtree == NULL);
4642 gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
4643 dt_sym = tmp_symtree->n.sym;
4644 dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
4645 ? "c_ptr" : "c_funptr");
4647 /* Generate an artificial generic function. */
4648 head = tmp_sym->generic;
4649 intr = gfc_get_interface ();
4650 intr->sym = dt_sym;
4651 intr->where = gfc_current_locus;
4652 intr->next = head;
4653 tmp_sym->generic = intr;
4655 if (!tmp_sym->attr.generic
4656 && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
4657 return NULL;
4659 if (!tmp_sym->attr.function
4660 && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
4661 return NULL;
4664 /* Say what module this symbol belongs to. */
4665 dt_sym->module = gfc_get_string (mod_name);
4666 dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
4667 dt_sym->intmod_sym_id = s;
4668 dt_sym->attr.use_assoc = 1;
4670 /* Initialize an integer constant expression node. */
4671 dt_sym->attr.flavor = FL_DERIVED;
4672 dt_sym->ts.is_c_interop = 1;
4673 dt_sym->attr.is_c_interop = 1;
4674 dt_sym->attr.private_comp = 1;
4675 dt_sym->component_access = ACCESS_PRIVATE;
4676 dt_sym->ts.is_iso_c = 1;
4677 dt_sym->ts.type = BT_DERIVED;
4678 dt_sym->ts.f90_type = BT_VOID;
4680 /* A derived type must have the bind attribute to be
4681 interoperable (J3/04-007, Section 15.2.3), even though
4682 the binding label is not used. */
4683 dt_sym->attr.is_bind_c = 1;
4685 dt_sym->attr.referenced = 1;
4686 dt_sym->ts.u.derived = dt_sym;
4688 /* Add the symbol created for the derived type to the current ns. */
4689 dt_list_ptr = &(gfc_derived_types);
4690 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
4691 dt_list_ptr = &((*dt_list_ptr)->next);
4693 /* There is already at least one derived type in the list, so append
4694 the one we're currently building for c_ptr or c_funptr. */
4695 if (*dt_list_ptr != NULL)
4696 dt_list_ptr = &((*dt_list_ptr)->next);
4697 (*dt_list_ptr) = gfc_get_dt_list ();
4698 (*dt_list_ptr)->derived = dt_sym;
4699 (*dt_list_ptr)->next = NULL;
4701 gfc_add_component (dt_sym, "c_address", &tmp_comp);
4702 if (tmp_comp == NULL)
4703 gcc_unreachable ();
4705 tmp_comp->ts.type = BT_INTEGER;
4707 /* Set this because the module will need to read/write this field. */
4708 tmp_comp->ts.f90_type = BT_INTEGER;
4710 /* The kinds for c_ptr and c_funptr are the same. */
4711 index = get_c_kind ("c_ptr", c_interop_kinds_table);
4712 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
4713 tmp_comp->attr.access = ACCESS_PRIVATE;
4715 /* Mark the component as C interoperable. */
4716 tmp_comp->ts.is_c_interop = 1;
4719 break;
4721 case ISOCBINDING_NULL_PTR:
4722 case ISOCBINDING_NULL_FUNPTR:
4723 gen_special_c_interop_ptr (tmp_sym, dt_symtree);
4724 break;
4726 default:
4727 gcc_unreachable ();
4729 gfc_commit_symbol (tmp_sym);
4730 return tmp_symtree;
4734 /* Check that a symbol is already typed. If strict is not set, an untyped
4735 symbol is acceptable for non-standard-conforming mode. */
4737 bool
4738 gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
4739 bool strict, locus where)
4741 gcc_assert (sym);
4743 if (gfc_matching_prefix)
4744 return true;
4746 /* Check for the type and try to give it an implicit one. */
4747 if (sym->ts.type == BT_UNKNOWN
4748 && !gfc_set_default_type (sym, 0, ns))
4750 if (strict)
4752 gfc_error ("Symbol %qs is used before it is typed at %L",
4753 sym->name, &where);
4754 return false;
4757 if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
4758 " it is typed at %L", sym->name, &where))
4759 return false;
4762 /* Everything is ok. */
4763 return true;
4767 /* Construct a typebound-procedure structure. Those are stored in a tentative
4768 list and marked `error' until symbols are committed. */
4770 gfc_typebound_proc*
4771 gfc_get_typebound_proc (gfc_typebound_proc *tb0)
4773 gfc_typebound_proc *result;
4775 result = XCNEW (gfc_typebound_proc);
4776 if (tb0)
4777 *result = *tb0;
4778 result->error = 1;
4780 latest_undo_chgset->tbps.safe_push (result);
4782 return result;
4786 /* Get the super-type of a given derived type. */
4788 gfc_symbol*
4789 gfc_get_derived_super_type (gfc_symbol* derived)
4791 gcc_assert (derived);
4793 if (derived->attr.generic)
4794 derived = gfc_find_dt_in_generic (derived);
4796 if (!derived->attr.extension)
4797 return NULL;
4799 gcc_assert (derived->components);
4800 gcc_assert (derived->components->ts.type == BT_DERIVED);
4801 gcc_assert (derived->components->ts.u.derived);
4803 if (derived->components->ts.u.derived->attr.generic)
4804 return gfc_find_dt_in_generic (derived->components->ts.u.derived);
4806 return derived->components->ts.u.derived;
4810 /* Get the ultimate super-type of a given derived type. */
4812 gfc_symbol*
4813 gfc_get_ultimate_derived_super_type (gfc_symbol* derived)
4815 if (!derived->attr.extension)
4816 return NULL;
4818 derived = gfc_get_derived_super_type (derived);
4820 if (derived->attr.extension)
4821 return gfc_get_ultimate_derived_super_type (derived);
4822 else
4823 return derived;
4827 /* Check if a derived type t2 is an extension of (or equal to) a type t1. */
4829 bool
4830 gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
4832 while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
4833 t2 = gfc_get_derived_super_type (t2);
4834 return gfc_compare_derived_types (t1, t2);
4838 /* Check if two typespecs are type compatible (F03:5.1.1.2):
4839 If ts1 is nonpolymorphic, ts2 must be the same type.
4840 If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1. */
4842 bool
4843 gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
4845 bool is_class1 = (ts1->type == BT_CLASS);
4846 bool is_class2 = (ts2->type == BT_CLASS);
4847 bool is_derived1 = (ts1->type == BT_DERIVED);
4848 bool is_derived2 = (ts2->type == BT_DERIVED);
4849 bool is_union1 = (ts1->type == BT_UNION);
4850 bool is_union2 = (ts2->type == BT_UNION);
4852 if (is_class1
4853 && ts1->u.derived->components
4854 && ((ts1->u.derived->attr.is_class
4855 && ts1->u.derived->components->ts.u.derived->attr
4856 .unlimited_polymorphic)
4857 || ts1->u.derived->attr.unlimited_polymorphic))
4858 return 1;
4860 if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2
4861 && !is_union1 && !is_union2)
4862 return (ts1->type == ts2->type);
4864 if ((is_derived1 && is_derived2) || (is_union1 && is_union1))
4865 return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
4867 if (is_derived1 && is_class2)
4868 return gfc_compare_derived_types (ts1->u.derived,
4869 ts2->u.derived->attr.is_class ?
4870 ts2->u.derived->components->ts.u.derived
4871 : ts2->u.derived);
4872 if (is_class1 && is_derived2)
4873 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
4874 ts1->u.derived->components->ts.u.derived
4875 : ts1->u.derived,
4876 ts2->u.derived);
4877 else if (is_class1 && is_class2)
4878 return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
4879 ts1->u.derived->components->ts.u.derived
4880 : ts1->u.derived,
4881 ts2->u.derived->attr.is_class ?
4882 ts2->u.derived->components->ts.u.derived
4883 : ts2->u.derived);
4884 else
4885 return 0;
4889 /* Find the parent-namespace of the current function. If we're inside
4890 BLOCK constructs, it may not be the current one. */
4892 gfc_namespace*
4893 gfc_find_proc_namespace (gfc_namespace* ns)
4895 while (ns->construct_entities)
4897 ns = ns->parent;
4898 gcc_assert (ns);
4901 return ns;
4905 /* Check if an associate-variable should be translated as an `implicit' pointer
4906 internally (if it is associated to a variable and not an array with
4907 descriptor). */
4909 bool
4910 gfc_is_associate_pointer (gfc_symbol* sym)
4912 if (!sym->assoc)
4913 return false;
4915 if (sym->ts.type == BT_CLASS)
4916 return true;
4918 if (!sym->assoc->variable)
4919 return false;
4921 if (sym->attr.dimension && sym->as->type != AS_EXPLICIT)
4922 return false;
4924 return true;
4928 gfc_symbol *
4929 gfc_find_dt_in_generic (gfc_symbol *sym)
4931 gfc_interface *intr = NULL;
4933 if (!sym || gfc_fl_struct (sym->attr.flavor))
4934 return sym;
4936 if (sym->attr.generic)
4937 for (intr = sym->generic; intr; intr = intr->next)
4938 if (gfc_fl_struct (intr->sym->attr.flavor))
4939 break;
4940 return intr ? intr->sym : NULL;
4944 /* Get the dummy arguments from a procedure symbol. If it has been declared
4945 via a PROCEDURE statement with a named interface, ts.interface will be set
4946 and the arguments need to be taken from there. */
4948 gfc_formal_arglist *
4949 gfc_sym_get_dummy_args (gfc_symbol *sym)
4951 gfc_formal_arglist *dummies;
4953 dummies = sym->formal;
4954 if (dummies == NULL && sym->ts.interface != NULL)
4955 dummies = sym->ts.interface->formal;
4957 return dummies;