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
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
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/>. */
24 #include "coretypes.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
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
),
46 const mstring procedures
[] =
48 minit ("UNKNOWN-PROC", PROC_UNKNOWN
),
49 minit ("MODULE-PROC", PROC_MODULE
),
50 minit ("INTERNAL-PROC", PROC_INTERNAL
),
51 minit ("DUMMY-PROC", PROC_DUMMY
),
52 minit ("INTRINSIC-PROC", PROC_INTRINSIC
),
53 minit ("EXTERNAL-PROC", PROC_EXTERNAL
),
54 minit ("STATEMENT-PROC", PROC_ST_FUNCTION
),
58 const mstring intents
[] =
60 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN
),
61 minit ("IN", INTENT_IN
),
62 minit ("OUT", INTENT_OUT
),
63 minit ("INOUT", INTENT_INOUT
),
67 const mstring access_types
[] =
69 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN
),
70 minit ("PUBLIC", ACCESS_PUBLIC
),
71 minit ("PRIVATE", ACCESS_PRIVATE
),
75 const mstring ifsrc_types
[] =
77 minit ("UNKNOWN", IFSRC_UNKNOWN
),
78 minit ("DECL", IFSRC_DECL
),
79 minit ("BODY", IFSRC_IFBODY
)
82 const mstring save_status
[] =
84 minit ("UNKNOWN", SAVE_NONE
),
85 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT
),
86 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT
),
89 /* This is to make sure the backend generates setup code in the correct
92 static int next_dummy_order
= 1;
95 gfc_namespace
*gfc_current_ns
;
96 gfc_namespace
*gfc_global_ns_list
;
98 gfc_gsymbol
*gfc_gsym_root
= NULL
;
100 gfc_dt_list
*gfc_derived_types
;
102 static gfc_undo_change_set default_undo_chgset_var
= { vNULL
, vNULL
, NULL
};
103 static gfc_undo_change_set
*latest_undo_chgset
= &default_undo_chgset_var
;
106 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
108 /* The following static variable indicates whether a particular element has
109 been explicitly set or not. */
111 static int new_flag
[GFC_LETTERS
];
114 /* Handle a correctly parsed IMPLICIT NONE. */
117 gfc_set_implicit_none (bool type
, bool external
, locus
*loc
)
122 gfc_current_ns
->has_implicit_none_export
= 1;
126 gfc_current_ns
->seen_implicit_none
= 1;
127 for (i
= 0; i
< GFC_LETTERS
; i
++)
129 if (gfc_current_ns
->set_flag
[i
])
131 gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
132 "IMPLICIT statement", loc
);
135 gfc_clear_ts (&gfc_current_ns
->default_type
[i
]);
136 gfc_current_ns
->set_flag
[i
] = 1;
142 /* Reset the implicit range flags. */
145 gfc_clear_new_implicit (void)
149 for (i
= 0; i
< GFC_LETTERS
; i
++)
154 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
157 gfc_add_new_implicit_range (int c1
, int c2
)
164 for (i
= c1
; i
<= c2
; i
++)
168 gfc_error ("Letter %qc already set in IMPLICIT statement at %C",
180 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
181 the new implicit types back into the existing types will work. */
184 gfc_merge_new_implicit (gfc_typespec
*ts
)
188 if (gfc_current_ns
->seen_implicit_none
)
190 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
194 for (i
= 0; i
< GFC_LETTERS
; i
++)
198 if (gfc_current_ns
->set_flag
[i
])
200 gfc_error ("Letter %qc already has an IMPLICIT type at %C",
205 gfc_current_ns
->default_type
[i
] = *ts
;
206 gfc_current_ns
->implicit_loc
[i
] = gfc_current_locus
;
207 gfc_current_ns
->set_flag
[i
] = 1;
214 /* Given a symbol, return a pointer to the typespec for its default type. */
217 gfc_get_default_type (const char *name
, gfc_namespace
*ns
)
223 if (flag_allow_leading_underscore
&& letter
== '_')
224 gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
225 "gfortran developers, and should not be used for "
226 "implicitly typed variables");
228 if (letter
< 'a' || letter
> 'z')
229 gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name
);
234 return &ns
->default_type
[letter
- 'a'];
238 /* Given a pointer to a symbol, set its type according to the first
239 letter of its name. Fails if the letter in question has no default
243 gfc_set_default_type (gfc_symbol
*sym
, int error_flag
, gfc_namespace
*ns
)
247 if (sym
->ts
.type
!= BT_UNKNOWN
)
248 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
250 ts
= gfc_get_default_type (sym
->name
, ns
);
252 if (ts
->type
== BT_UNKNOWN
)
254 if (error_flag
&& !sym
->attr
.untyped
)
256 gfc_error ("Symbol %qs at %L has no IMPLICIT type",
257 sym
->name
, &sym
->declared_at
);
258 sym
->attr
.untyped
= 1; /* Ensure we only give an error once. */
265 sym
->attr
.implicit_type
= 1;
267 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
)
268 sym
->ts
.u
.cl
= gfc_new_charlen (sym
->ns
, ts
->u
.cl
);
269 else if (ts
->type
== BT_CLASS
270 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
273 if (sym
->attr
.is_bind_c
== 1 && warn_c_binding_type
)
275 /* BIND(C) variables should not be implicitly declared. */
276 gfc_warning_now (OPT_Wc_binding_type
, "Implicitly declared BIND(C) "
277 "variable %qs at %L may not be C interoperable",
278 sym
->name
, &sym
->declared_at
);
279 sym
->ts
.f90_type
= sym
->ts
.type
;
282 if (sym
->attr
.dummy
!= 0)
284 if (sym
->ns
->proc_name
!= NULL
285 && (sym
->ns
->proc_name
->attr
.subroutine
!= 0
286 || sym
->ns
->proc_name
->attr
.function
!= 0)
287 && sym
->ns
->proc_name
->attr
.is_bind_c
!= 0
288 && warn_c_binding_type
)
290 /* Dummy args to a BIND(C) routine may not be interoperable if
291 they are implicitly typed. */
292 gfc_warning_now (OPT_Wc_binding_type
, "Implicitly declared variable "
293 "%qs at %L may not be C interoperable but it is a "
294 "dummy argument to the BIND(C) procedure %qs at %L",
295 sym
->name
, &(sym
->declared_at
),
296 sym
->ns
->proc_name
->name
,
297 &(sym
->ns
->proc_name
->declared_at
));
298 sym
->ts
.f90_type
= sym
->ts
.type
;
306 /* This function is called from parse.c(parse_progunit) to check the
307 type of the function is not implicitly typed in the host namespace
308 and to implicitly type the function result, if necessary. */
311 gfc_check_function_type (gfc_namespace
*ns
)
313 gfc_symbol
*proc
= ns
->proc_name
;
315 if (!proc
->attr
.contained
|| proc
->result
->attr
.implicit_type
)
318 if (proc
->result
->ts
.type
== BT_UNKNOWN
&& proc
->result
->ts
.interface
== NULL
)
320 if (gfc_set_default_type (proc
->result
, 0, gfc_current_ns
))
322 if (proc
->result
!= proc
)
324 proc
->ts
= proc
->result
->ts
;
325 proc
->as
= gfc_copy_array_spec (proc
->result
->as
);
326 proc
->attr
.dimension
= proc
->result
->attr
.dimension
;
327 proc
->attr
.pointer
= proc
->result
->attr
.pointer
;
328 proc
->attr
.allocatable
= proc
->result
->attr
.allocatable
;
331 else if (!proc
->result
->attr
.proc_pointer
)
333 gfc_error ("Function result %qs at %L has no IMPLICIT type",
334 proc
->result
->name
, &proc
->result
->declared_at
);
335 proc
->result
->attr
.untyped
= 1;
341 /******************** Symbol attribute stuff *********************/
343 /* This is a generic conflict-checker. We do this to avoid having a
344 single conflict in two places. */
346 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
347 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
348 #define conf_std(a, b, std) if (attr->a && attr->b)\
357 check_conflict (symbol_attribute
*attr
, const char *name
, locus
*where
)
359 static const char *dummy
= "DUMMY", *save
= "SAVE", *pointer
= "POINTER",
360 *target
= "TARGET", *external
= "EXTERNAL", *intent
= "INTENT",
361 *intent_in
= "INTENT(IN)", *intrinsic
= "INTRINSIC",
362 *intent_out
= "INTENT(OUT)", *intent_inout
= "INTENT(INOUT)",
363 *allocatable
= "ALLOCATABLE", *elemental
= "ELEMENTAL",
364 *privat
= "PRIVATE", *recursive
= "RECURSIVE",
365 *in_common
= "COMMON", *result
= "RESULT", *in_namelist
= "NAMELIST",
366 *publik
= "PUBLIC", *optional
= "OPTIONAL", *entry
= "ENTRY",
367 *function
= "FUNCTION", *subroutine
= "SUBROUTINE",
368 *dimension
= "DIMENSION", *in_equivalence
= "EQUIVALENCE",
369 *use_assoc
= "USE ASSOCIATED", *cray_pointer
= "CRAY POINTER",
370 *cray_pointee
= "CRAY POINTEE", *data
= "DATA", *value
= "VALUE",
371 *volatile_
= "VOLATILE", *is_protected
= "PROTECTED",
372 *is_bind_c
= "BIND(C)", *procedure
= "PROCEDURE",
373 *proc_pointer
= "PROCEDURE POINTER", *abstract
= "ABSTRACT",
374 *asynchronous
= "ASYNCHRONOUS", *codimension
= "CODIMENSION",
375 *contiguous
= "CONTIGUOUS", *generic
= "GENERIC";
376 static const char *threadprivate
= "THREADPRIVATE";
377 static const char *omp_declare_target
= "OMP DECLARE TARGET";
378 static const char *oacc_declare_copyin
= "OACC DECLARE COPYIN";
379 static const char *oacc_declare_create
= "OACC DECLARE CREATE";
380 static const char *oacc_declare_deviceptr
= "OACC DECLARE DEVICEPTR";
381 static const char *oacc_declare_device_resident
=
382 "OACC DECLARE DEVICE_RESIDENT";
388 where
= &gfc_current_locus
;
390 if (attr
->pointer
&& attr
->intent
!= INTENT_UNKNOWN
)
394 standard
= GFC_STD_F2003
;
398 if (attr
->in_namelist
&& (attr
->allocatable
|| attr
->pointer
))
401 a2
= attr
->allocatable
? allocatable
: pointer
;
402 standard
= GFC_STD_F2003
;
406 /* Check for attributes not allowed in a BLOCK DATA. */
407 if (gfc_current_state () == COMP_BLOCK_DATA
)
411 if (attr
->in_namelist
)
413 if (attr
->allocatable
)
419 if (attr
->access
== ACCESS_PRIVATE
)
421 if (attr
->access
== ACCESS_PUBLIC
)
423 if (attr
->intent
!= INTENT_UNKNOWN
)
429 ("%s attribute not allowed in BLOCK DATA program unit at %L",
435 if (attr
->save
== SAVE_EXPLICIT
)
438 conf (in_common
, save
);
441 switch (attr
->flavor
)
449 a1
= gfc_code2string (flavors
, attr
->flavor
);
453 gfc_error ("Namelist group name at %L cannot have the "
454 "SAVE attribute", where
);
458 /* Conflicts between SAVE and PROCEDURE will be checked at
459 resolution stage, see "resolve_fl_procedure". */
466 if (attr
->dummy
&& ((attr
->function
|| attr
->subroutine
) &&
467 gfc_current_state () == COMP_CONTAINS
))
468 gfc_error_now ("internal procedure %qs at %L conflicts with "
469 "DUMMY argument", name
, where
);
472 conf (dummy
, intrinsic
);
473 conf (dummy
, threadprivate
);
474 conf (dummy
, omp_declare_target
);
475 conf (pointer
, target
);
476 conf (pointer
, intrinsic
);
477 conf (pointer
, elemental
);
478 conf (pointer
, codimension
);
479 conf (allocatable
, elemental
);
481 conf (target
, external
);
482 conf (target
, intrinsic
);
484 if (!attr
->if_source
)
485 conf (external
, dimension
); /* See Fortran 95's R504. */
487 conf (external
, intrinsic
);
488 conf (entry
, intrinsic
);
490 if ((attr
->if_source
== IFSRC_DECL
&& !attr
->procedure
) || attr
->contained
)
491 conf (external
, subroutine
);
493 if (attr
->proc_pointer
&& !gfc_notify_std (GFC_STD_F2003
,
494 "Procedure pointer at %C"))
497 conf (allocatable
, pointer
);
498 conf_std (allocatable
, dummy
, GFC_STD_F2003
);
499 conf_std (allocatable
, function
, GFC_STD_F2003
);
500 conf_std (allocatable
, result
, GFC_STD_F2003
);
501 conf (elemental
, recursive
);
503 conf (in_common
, dummy
);
504 conf (in_common
, allocatable
);
505 conf (in_common
, codimension
);
506 conf (in_common
, result
);
508 conf (in_equivalence
, use_assoc
);
509 conf (in_equivalence
, codimension
);
510 conf (in_equivalence
, dummy
);
511 conf (in_equivalence
, target
);
512 conf (in_equivalence
, pointer
);
513 conf (in_equivalence
, function
);
514 conf (in_equivalence
, result
);
515 conf (in_equivalence
, entry
);
516 conf (in_equivalence
, allocatable
);
517 conf (in_equivalence
, threadprivate
);
518 conf (in_equivalence
, omp_declare_target
);
519 conf (in_equivalence
, oacc_declare_create
);
520 conf (in_equivalence
, oacc_declare_copyin
);
521 conf (in_equivalence
, oacc_declare_deviceptr
);
522 conf (in_equivalence
, oacc_declare_device_resident
);
524 conf (dummy
, result
);
525 conf (entry
, result
);
526 conf (generic
, result
);
528 conf (function
, subroutine
);
530 if (!function
&& !subroutine
)
531 conf (is_bind_c
, dummy
);
533 conf (is_bind_c
, cray_pointer
);
534 conf (is_bind_c
, cray_pointee
);
535 conf (is_bind_c
, codimension
);
536 conf (is_bind_c
, allocatable
);
537 conf (is_bind_c
, elemental
);
539 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
540 Parameter conflict caught below. Also, value cannot be specified
541 for a dummy procedure. */
543 /* Cray pointer/pointee conflicts. */
544 conf (cray_pointer
, cray_pointee
);
545 conf (cray_pointer
, dimension
);
546 conf (cray_pointer
, codimension
);
547 conf (cray_pointer
, contiguous
);
548 conf (cray_pointer
, pointer
);
549 conf (cray_pointer
, target
);
550 conf (cray_pointer
, allocatable
);
551 conf (cray_pointer
, external
);
552 conf (cray_pointer
, intrinsic
);
553 conf (cray_pointer
, in_namelist
);
554 conf (cray_pointer
, function
);
555 conf (cray_pointer
, subroutine
);
556 conf (cray_pointer
, entry
);
558 conf (cray_pointee
, allocatable
);
559 conf (cray_pointee
, contiguous
);
560 conf (cray_pointee
, codimension
);
561 conf (cray_pointee
, intent
);
562 conf (cray_pointee
, optional
);
563 conf (cray_pointee
, dummy
);
564 conf (cray_pointee
, target
);
565 conf (cray_pointee
, intrinsic
);
566 conf (cray_pointee
, pointer
);
567 conf (cray_pointee
, entry
);
568 conf (cray_pointee
, in_common
);
569 conf (cray_pointee
, in_equivalence
);
570 conf (cray_pointee
, threadprivate
);
571 conf (cray_pointee
, omp_declare_target
);
572 conf (cray_pointee
, oacc_declare_create
);
573 conf (cray_pointee
, oacc_declare_copyin
);
574 conf (cray_pointee
, oacc_declare_deviceptr
);
575 conf (cray_pointee
, oacc_declare_device_resident
);
578 conf (data
, function
);
580 conf (data
, allocatable
);
582 conf (value
, pointer
)
583 conf (value
, allocatable
)
584 conf (value
, subroutine
)
585 conf (value
, function
)
586 conf (value
, volatile_
)
587 conf (value
, dimension
)
588 conf (value
, codimension
)
589 conf (value
, external
)
591 conf (codimension
, result
)
594 && (attr
->intent
== INTENT_OUT
|| attr
->intent
== INTENT_INOUT
))
597 a2
= attr
->intent
== INTENT_OUT
? intent_out
: intent_inout
;
601 conf (is_protected
, intrinsic
)
602 conf (is_protected
, in_common
)
604 conf (asynchronous
, intrinsic
)
605 conf (asynchronous
, external
)
607 conf (volatile_
, intrinsic
)
608 conf (volatile_
, external
)
610 if (attr
->volatile_
&& attr
->intent
== INTENT_IN
)
617 conf (procedure
, allocatable
)
618 conf (procedure
, dimension
)
619 conf (procedure
, codimension
)
620 conf (procedure
, intrinsic
)
621 conf (procedure
, target
)
622 conf (procedure
, value
)
623 conf (procedure
, volatile_
)
624 conf (procedure
, asynchronous
)
625 conf (procedure
, entry
)
627 conf (proc_pointer
, abstract
)
629 conf (entry
, omp_declare_target
)
630 conf (entry
, oacc_declare_create
)
631 conf (entry
, oacc_declare_copyin
)
632 conf (entry
, oacc_declare_deviceptr
)
633 conf (entry
, oacc_declare_device_resident
)
635 a1
= gfc_code2string (flavors
, attr
->flavor
);
637 if (attr
->in_namelist
638 && attr
->flavor
!= FL_VARIABLE
639 && attr
->flavor
!= FL_PROCEDURE
640 && attr
->flavor
!= FL_UNKNOWN
)
646 switch (attr
->flavor
)
656 conf2 (asynchronous
);
659 conf2 (is_protected
);
669 conf2 (threadprivate
);
670 conf2 (omp_declare_target
);
671 conf2 (oacc_declare_create
);
672 conf2 (oacc_declare_copyin
);
673 conf2 (oacc_declare_deviceptr
);
674 conf2 (oacc_declare_device_resident
);
676 if (attr
->access
== ACCESS_PUBLIC
|| attr
->access
== ACCESS_PRIVATE
)
678 a2
= attr
->access
== ACCESS_PUBLIC
? publik
: privat
;
679 gfc_error ("%s attribute applied to %s %s at %L", a2
, a1
,
686 gfc_error_now ("BIND(C) applied to %s %s at %L", a1
, name
, where
);
700 /* Conflicts with INTENT, SAVE and RESULT will be checked
701 at resolution stage, see "resolve_fl_procedure". */
703 if (attr
->subroutine
)
709 conf2 (asynchronous
);
714 if (!attr
->proc_pointer
)
715 conf2 (threadprivate
);
718 if (!attr
->proc_pointer
)
723 case PROC_ST_FUNCTION
:
734 conf2 (threadprivate
);
754 conf2 (threadprivate
);
756 conf2 (omp_declare_target
);
757 conf2 (oacc_declare_create
);
758 conf2 (oacc_declare_copyin
);
759 conf2 (oacc_declare_deviceptr
);
760 conf2 (oacc_declare_device_resident
);
762 if (attr
->intent
!= INTENT_UNKNOWN
)
779 conf2 (is_protected
);
785 conf2 (asynchronous
);
786 conf2 (threadprivate
);
802 gfc_error ("%s attribute conflicts with %s attribute at %L",
805 gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
806 a1
, a2
, name
, where
);
813 return gfc_notify_std (standard
, "%s attribute "
814 "with %s attribute at %L", a1
, a2
,
819 return gfc_notify_std (standard
, "%s attribute "
820 "with %s attribute in %qs at %L",
821 a1
, a2
, name
, where
);
830 /* Mark a symbol as referenced. */
833 gfc_set_sym_referenced (gfc_symbol
*sym
)
836 if (sym
->attr
.referenced
)
839 sym
->attr
.referenced
= 1;
841 /* Remember which order dummy variables are accessed in. */
843 sym
->dummy_order
= next_dummy_order
++;
847 /* Common subroutine called by attribute changing subroutines in order
848 to prevent them from changing a symbol that has been
849 use-associated. Returns zero if it is OK to change the symbol,
853 check_used (symbol_attribute
*attr
, const char *name
, locus
*where
)
856 if (attr
->use_assoc
== 0)
860 where
= &gfc_current_locus
;
863 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
866 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
873 /* Generate an error because of a duplicate attribute. */
876 duplicate_attr (const char *attr
, locus
*where
)
880 where
= &gfc_current_locus
;
882 gfc_error ("Duplicate %s attribute specified at %L", attr
, where
);
887 gfc_add_ext_attribute (symbol_attribute
*attr
, ext_attr_id_t ext_attr
,
888 locus
*where ATTRIBUTE_UNUSED
)
890 attr
->ext_attr
|= 1 << ext_attr
;
895 /* Called from decl.c (attr_decl1) to check attributes, when declared
899 gfc_add_attribute (symbol_attribute
*attr
, locus
*where
)
901 if (check_used (attr
, NULL
, where
))
904 return check_conflict (attr
, NULL
, where
);
909 gfc_add_allocatable (symbol_attribute
*attr
, locus
*where
)
912 if (check_used (attr
, NULL
, where
))
915 if (attr
->allocatable
)
917 duplicate_attr ("ALLOCATABLE", where
);
921 if (attr
->flavor
== FL_PROCEDURE
&& attr
->if_source
== IFSRC_IFBODY
922 && !gfc_find_state (COMP_INTERFACE
))
924 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
929 attr
->allocatable
= 1;
930 return check_conflict (attr
, NULL
, where
);
935 gfc_add_codimension (symbol_attribute
*attr
, const char *name
, locus
*where
)
938 if (check_used (attr
, name
, where
))
941 if (attr
->codimension
)
943 duplicate_attr ("CODIMENSION", where
);
947 if (attr
->flavor
== FL_PROCEDURE
&& attr
->if_source
== IFSRC_IFBODY
948 && !gfc_find_state (COMP_INTERFACE
))
950 gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
951 "at %L", name
, where
);
955 attr
->codimension
= 1;
956 return check_conflict (attr
, name
, where
);
961 gfc_add_dimension (symbol_attribute
*attr
, const char *name
, locus
*where
)
964 if (check_used (attr
, name
, where
))
969 duplicate_attr ("DIMENSION", where
);
973 if (attr
->flavor
== FL_PROCEDURE
&& attr
->if_source
== IFSRC_IFBODY
974 && !gfc_find_state (COMP_INTERFACE
))
976 gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
977 "at %L", name
, where
);
982 return check_conflict (attr
, name
, where
);
987 gfc_add_contiguous (symbol_attribute
*attr
, const char *name
, locus
*where
)
990 if (check_used (attr
, name
, where
))
993 attr
->contiguous
= 1;
994 return check_conflict (attr
, name
, where
);
999 gfc_add_external (symbol_attribute
*attr
, locus
*where
)
1002 if (check_used (attr
, NULL
, where
))
1007 duplicate_attr ("EXTERNAL", where
);
1011 if (attr
->pointer
&& attr
->if_source
!= IFSRC_IFBODY
)
1014 attr
->proc_pointer
= 1;
1019 return check_conflict (attr
, NULL
, where
);
1024 gfc_add_intrinsic (symbol_attribute
*attr
, locus
*where
)
1027 if (check_used (attr
, NULL
, where
))
1030 if (attr
->intrinsic
)
1032 duplicate_attr ("INTRINSIC", where
);
1036 attr
->intrinsic
= 1;
1038 return check_conflict (attr
, NULL
, where
);
1043 gfc_add_optional (symbol_attribute
*attr
, locus
*where
)
1046 if (check_used (attr
, NULL
, where
))
1051 duplicate_attr ("OPTIONAL", where
);
1056 return check_conflict (attr
, NULL
, where
);
1061 gfc_add_pointer (symbol_attribute
*attr
, locus
*where
)
1064 if (check_used (attr
, NULL
, where
))
1067 if (attr
->pointer
&& !(attr
->if_source
== IFSRC_IFBODY
1068 && !gfc_find_state (COMP_INTERFACE
)))
1070 duplicate_attr ("POINTER", where
);
1074 if (attr
->procedure
|| (attr
->external
&& attr
->if_source
!= IFSRC_IFBODY
)
1075 || (attr
->if_source
== IFSRC_IFBODY
1076 && !gfc_find_state (COMP_INTERFACE
)))
1077 attr
->proc_pointer
= 1;
1081 return check_conflict (attr
, NULL
, where
);
1086 gfc_add_cray_pointer (symbol_attribute
*attr
, locus
*where
)
1089 if (check_used (attr
, NULL
, where
))
1092 attr
->cray_pointer
= 1;
1093 return check_conflict (attr
, NULL
, where
);
1098 gfc_add_cray_pointee (symbol_attribute
*attr
, locus
*where
)
1101 if (check_used (attr
, NULL
, where
))
1104 if (attr
->cray_pointee
)
1106 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1107 " statements", where
);
1111 attr
->cray_pointee
= 1;
1112 return check_conflict (attr
, NULL
, where
);
1117 gfc_add_protected (symbol_attribute
*attr
, const char *name
, locus
*where
)
1119 if (check_used (attr
, name
, where
))
1122 if (attr
->is_protected
)
1124 if (!gfc_notify_std (GFC_STD_LEGACY
,
1125 "Duplicate PROTECTED attribute specified at %L",
1130 attr
->is_protected
= 1;
1131 return check_conflict (attr
, name
, where
);
1136 gfc_add_result (symbol_attribute
*attr
, const char *name
, locus
*where
)
1139 if (check_used (attr
, name
, where
))
1143 return check_conflict (attr
, name
, where
);
1148 gfc_add_save (symbol_attribute
*attr
, save_state s
, const char *name
,
1152 if (check_used (attr
, name
, where
))
1155 if (s
== SAVE_EXPLICIT
&& gfc_pure (NULL
))
1158 ("SAVE attribute at %L cannot be specified in a PURE procedure",
1163 if (s
== SAVE_EXPLICIT
)
1164 gfc_unset_implicit_pure (NULL
);
1166 if (s
== SAVE_EXPLICIT
&& attr
->save
== SAVE_EXPLICIT
)
1168 if (!gfc_notify_std (GFC_STD_LEGACY
,
1169 "Duplicate SAVE attribute specified at %L",
1175 return check_conflict (attr
, name
, where
);
1180 gfc_add_value (symbol_attribute
*attr
, const char *name
, locus
*where
)
1183 if (check_used (attr
, name
, where
))
1188 if (!gfc_notify_std (GFC_STD_LEGACY
,
1189 "Duplicate VALUE attribute specified at %L",
1195 return check_conflict (attr
, name
, where
);
1200 gfc_add_volatile (symbol_attribute
*attr
, const char *name
, locus
*where
)
1202 /* No check_used needed as 11.2.1 of the F2003 standard allows
1203 that the local identifier made accessible by a use statement can be
1204 given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1206 if (attr
->volatile_
&& attr
->volatile_ns
== gfc_current_ns
)
1207 if (!gfc_notify_std (GFC_STD_LEGACY
,
1208 "Duplicate VOLATILE attribute specified at %L",
1212 attr
->volatile_
= 1;
1213 attr
->volatile_ns
= gfc_current_ns
;
1214 return check_conflict (attr
, name
, where
);
1219 gfc_add_asynchronous (symbol_attribute
*attr
, const char *name
, locus
*where
)
1221 /* No check_used needed as 11.2.1 of the F2003 standard allows
1222 that the local identifier made accessible by a use statement can be
1223 given a ASYNCHRONOUS attribute. */
1225 if (attr
->asynchronous
&& attr
->asynchronous_ns
== gfc_current_ns
)
1226 if (!gfc_notify_std (GFC_STD_LEGACY
,
1227 "Duplicate ASYNCHRONOUS attribute specified at %L",
1231 attr
->asynchronous
= 1;
1232 attr
->asynchronous_ns
= gfc_current_ns
;
1233 return check_conflict (attr
, name
, where
);
1238 gfc_add_threadprivate (symbol_attribute
*attr
, const char *name
, locus
*where
)
1241 if (check_used (attr
, name
, where
))
1244 if (attr
->threadprivate
)
1246 duplicate_attr ("THREADPRIVATE", where
);
1250 attr
->threadprivate
= 1;
1251 return check_conflict (attr
, name
, where
);
1256 gfc_add_omp_declare_target (symbol_attribute
*attr
, const char *name
,
1260 if (check_used (attr
, name
, where
))
1263 if (attr
->omp_declare_target
)
1266 attr
->omp_declare_target
= 1;
1267 return check_conflict (attr
, name
, where
);
1272 gfc_add_oacc_declare_create (symbol_attribute
*attr
, const char *name
,
1275 if (check_used (attr
, name
, where
))
1278 if (attr
->oacc_declare_create
)
1281 attr
->oacc_declare_create
= 1;
1282 return check_conflict (attr
, name
, where
);
1287 gfc_add_oacc_declare_copyin (symbol_attribute
*attr
, const char *name
,
1290 if (check_used (attr
, name
, where
))
1293 if (attr
->oacc_declare_copyin
)
1296 attr
->oacc_declare_copyin
= 1;
1297 return check_conflict (attr
, name
, where
);
1302 gfc_add_oacc_declare_deviceptr (symbol_attribute
*attr
, const char *name
,
1305 if (check_used (attr
, name
, where
))
1308 if (attr
->oacc_declare_deviceptr
)
1311 attr
->oacc_declare_deviceptr
= 1;
1312 return check_conflict (attr
, name
, where
);
1317 gfc_add_oacc_declare_device_resident (symbol_attribute
*attr
, const char *name
,
1320 if (check_used (attr
, name
, where
))
1323 if (attr
->oacc_declare_device_resident
)
1326 attr
->oacc_declare_device_resident
= 1;
1327 return check_conflict (attr
, name
, where
);
1332 gfc_add_target (symbol_attribute
*attr
, locus
*where
)
1335 if (check_used (attr
, NULL
, where
))
1340 duplicate_attr ("TARGET", where
);
1345 return check_conflict (attr
, NULL
, where
);
1350 gfc_add_dummy (symbol_attribute
*attr
, const char *name
, locus
*where
)
1353 if (check_used (attr
, name
, where
))
1356 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1358 return check_conflict (attr
, name
, where
);
1363 gfc_add_in_common (symbol_attribute
*attr
, const char *name
, locus
*where
)
1366 if (check_used (attr
, name
, where
))
1369 /* Duplicate attribute already checked for. */
1370 attr
->in_common
= 1;
1371 return check_conflict (attr
, name
, where
);
1376 gfc_add_in_equivalence (symbol_attribute
*attr
, const char *name
, locus
*where
)
1379 /* Duplicate attribute already checked for. */
1380 attr
->in_equivalence
= 1;
1381 if (!check_conflict (attr
, name
, where
))
1384 if (attr
->flavor
== FL_VARIABLE
)
1387 return gfc_add_flavor (attr
, FL_VARIABLE
, name
, where
);
1392 gfc_add_data (symbol_attribute
*attr
, const char *name
, locus
*where
)
1395 if (check_used (attr
, name
, where
))
1399 return check_conflict (attr
, name
, where
);
1404 gfc_add_in_namelist (symbol_attribute
*attr
, const char *name
, locus
*where
)
1407 attr
->in_namelist
= 1;
1408 return check_conflict (attr
, name
, where
);
1413 gfc_add_sequence (symbol_attribute
*attr
, const char *name
, locus
*where
)
1416 if (check_used (attr
, name
, where
))
1420 return check_conflict (attr
, name
, where
);
1425 gfc_add_elemental (symbol_attribute
*attr
, locus
*where
)
1428 if (check_used (attr
, NULL
, where
))
1431 if (attr
->elemental
)
1433 duplicate_attr ("ELEMENTAL", where
);
1437 attr
->elemental
= 1;
1438 return check_conflict (attr
, NULL
, where
);
1443 gfc_add_pure (symbol_attribute
*attr
, locus
*where
)
1446 if (check_used (attr
, NULL
, where
))
1451 duplicate_attr ("PURE", where
);
1456 return check_conflict (attr
, NULL
, where
);
1461 gfc_add_recursive (symbol_attribute
*attr
, locus
*where
)
1464 if (check_used (attr
, NULL
, where
))
1467 if (attr
->recursive
)
1469 duplicate_attr ("RECURSIVE", where
);
1473 attr
->recursive
= 1;
1474 return check_conflict (attr
, NULL
, where
);
1479 gfc_add_entry (symbol_attribute
*attr
, const char *name
, locus
*where
)
1482 if (check_used (attr
, name
, where
))
1487 duplicate_attr ("ENTRY", where
);
1492 return check_conflict (attr
, name
, where
);
1497 gfc_add_function (symbol_attribute
*attr
, const char *name
, locus
*where
)
1500 if (attr
->flavor
!= FL_PROCEDURE
1501 && !gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
))
1505 return check_conflict (attr
, name
, where
);
1510 gfc_add_subroutine (symbol_attribute
*attr
, const char *name
, locus
*where
)
1513 if (attr
->flavor
!= FL_PROCEDURE
1514 && !gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
))
1517 attr
->subroutine
= 1;
1518 return check_conflict (attr
, name
, where
);
1523 gfc_add_generic (symbol_attribute
*attr
, const char *name
, locus
*where
)
1526 if (attr
->flavor
!= FL_PROCEDURE
1527 && !gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
))
1531 return check_conflict (attr
, name
, where
);
1536 gfc_add_proc (symbol_attribute
*attr
, const char *name
, locus
*where
)
1539 if (check_used (attr
, NULL
, where
))
1542 if (attr
->flavor
!= FL_PROCEDURE
1543 && !gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
))
1546 if (attr
->procedure
)
1548 duplicate_attr ("PROCEDURE", where
);
1552 attr
->procedure
= 1;
1554 return check_conflict (attr
, NULL
, where
);
1559 gfc_add_abstract (symbol_attribute
* attr
, locus
* where
)
1563 duplicate_attr ("ABSTRACT", where
);
1569 return check_conflict (attr
, NULL
, where
);
1573 /* Flavors are special because some flavors are not what Fortran
1574 considers attributes and can be reaffirmed multiple times. */
1577 gfc_add_flavor (symbol_attribute
*attr
, sym_flavor f
, const char *name
,
1581 if ((f
== FL_PROGRAM
|| f
== FL_BLOCK_DATA
|| f
== FL_MODULE
1582 || f
== FL_PARAMETER
|| f
== FL_LABEL
|| f
== FL_DERIVED
1583 || f
== FL_NAMELIST
) && check_used (attr
, name
, where
))
1586 if (attr
->flavor
== f
&& f
== FL_VARIABLE
)
1589 if (attr
->flavor
!= FL_UNKNOWN
)
1592 where
= &gfc_current_locus
;
1595 gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
1596 gfc_code2string (flavors
, attr
->flavor
), name
,
1597 gfc_code2string (flavors
, f
), where
);
1599 gfc_error ("%s attribute conflicts with %s attribute at %L",
1600 gfc_code2string (flavors
, attr
->flavor
),
1601 gfc_code2string (flavors
, f
), where
);
1608 return check_conflict (attr
, name
, where
);
1613 gfc_add_procedure (symbol_attribute
*attr
, procedure_type t
,
1614 const char *name
, locus
*where
)
1617 if (check_used (attr
, name
, where
))
1620 if (attr
->flavor
!= FL_PROCEDURE
1621 && !gfc_add_flavor (attr
, FL_PROCEDURE
, name
, where
))
1625 where
= &gfc_current_locus
;
1627 if (attr
->proc
!= PROC_UNKNOWN
&& !attr
->module_procedure
)
1629 if (attr
->proc
== PROC_ST_FUNCTION
&& t
== PROC_INTERNAL
1630 && !gfc_notification_std (GFC_STD_F2008
))
1631 gfc_error ("%s procedure at %L is already declared as %s "
1632 "procedure. \nF2008: A pointer function assignment "
1633 "is ambiguous if it is the first executable statement "
1634 "after the specification block. Please add any other "
1635 "kind of executable statement before it. FIXME",
1636 gfc_code2string (procedures
, t
), where
,
1637 gfc_code2string (procedures
, attr
->proc
));
1639 gfc_error ("%s procedure at %L is already declared as %s "
1640 "procedure", gfc_code2string (procedures
, t
), where
,
1641 gfc_code2string (procedures
, attr
->proc
));
1648 /* Statement functions are always scalar and functions. */
1649 if (t
== PROC_ST_FUNCTION
1650 && ((!attr
->function
&& !gfc_add_function (attr
, name
, where
))
1651 || attr
->dimension
))
1654 return check_conflict (attr
, name
, where
);
1659 gfc_add_intent (symbol_attribute
*attr
, sym_intent intent
, locus
*where
)
1662 if (check_used (attr
, NULL
, where
))
1665 if (attr
->intent
== INTENT_UNKNOWN
)
1667 attr
->intent
= intent
;
1668 return check_conflict (attr
, NULL
, where
);
1672 where
= &gfc_current_locus
;
1674 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1675 gfc_intent_string (attr
->intent
),
1676 gfc_intent_string (intent
), where
);
1682 /* No checks for use-association in public and private statements. */
1685 gfc_add_access (symbol_attribute
*attr
, gfc_access access
,
1686 const char *name
, locus
*where
)
1689 if (attr
->access
== ACCESS_UNKNOWN
1690 || (attr
->use_assoc
&& attr
->access
!= ACCESS_PRIVATE
))
1692 attr
->access
= access
;
1693 return check_conflict (attr
, name
, where
);
1697 where
= &gfc_current_locus
;
1698 gfc_error ("ACCESS specification at %L was already specified", where
);
1704 /* Set the is_bind_c field for the given symbol_attribute. */
1707 gfc_add_is_bind_c (symbol_attribute
*attr
, const char *name
, locus
*where
,
1708 int is_proc_lang_bind_spec
)
1711 if (is_proc_lang_bind_spec
== 0 && attr
->flavor
== FL_PROCEDURE
)
1712 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1713 "variables or common blocks", where
);
1714 else if (attr
->is_bind_c
)
1715 gfc_error_now ("Duplicate BIND attribute specified at %L", where
);
1717 attr
->is_bind_c
= 1;
1720 where
= &gfc_current_locus
;
1722 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) at %L", where
))
1725 return check_conflict (attr
, name
, where
);
1729 /* Set the extension field for the given symbol_attribute. */
1732 gfc_add_extension (symbol_attribute
*attr
, locus
*where
)
1735 where
= &gfc_current_locus
;
1737 if (attr
->extension
)
1738 gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where
);
1740 attr
->extension
= 1;
1742 if (!gfc_notify_std (GFC_STD_F2003
, "EXTENDS at %L", where
))
1750 gfc_add_explicit_interface (gfc_symbol
*sym
, ifsrc source
,
1751 gfc_formal_arglist
* formal
, locus
*where
)
1753 if (check_used (&sym
->attr
, sym
->name
, where
))
1756 /* Skip the following checks in the case of a module_procedures in a
1757 submodule since they will manifestly fail. */
1758 if (sym
->attr
.module_procedure
== 1
1759 && source
== IFSRC_DECL
)
1763 where
= &gfc_current_locus
;
1765 if (sym
->attr
.if_source
!= IFSRC_UNKNOWN
1766 && sym
->attr
.if_source
!= IFSRC_DECL
)
1768 gfc_error ("Symbol %qs at %L already has an explicit interface",
1773 if (source
== IFSRC_IFBODY
&& (sym
->attr
.dimension
|| sym
->attr
.allocatable
))
1775 gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
1776 "body", sym
->name
, where
);
1781 sym
->formal
= formal
;
1782 sym
->attr
.if_source
= source
;
1788 /* Add a type to a symbol. */
1791 gfc_add_type (gfc_symbol
*sym
, gfc_typespec
*ts
, locus
*where
)
1797 where
= &gfc_current_locus
;
1800 type
= sym
->result
->ts
.type
;
1802 type
= sym
->ts
.type
;
1804 if (sym
->attr
.result
&& type
== BT_UNKNOWN
&& sym
->ns
->proc_name
)
1805 type
= sym
->ns
->proc_name
->ts
.type
;
1807 if (type
!= BT_UNKNOWN
&& !(sym
->attr
.function
&& sym
->attr
.implicit_type
)
1808 && !(gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
1809 && gfc_state_stack
->previous
->previous
->state
== COMP_SUBMODULE
)
1810 && !sym
->attr
.module_procedure
)
1812 if (sym
->attr
.use_assoc
)
1813 gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
1814 "use-associated at %L", sym
->name
, where
, sym
->module
,
1817 gfc_error ("Symbol %qs at %L already has basic type of %s", sym
->name
,
1818 where
, gfc_basic_typename (type
));
1822 if (sym
->attr
.procedure
&& sym
->ts
.interface
)
1824 gfc_error ("Procedure %qs at %L may not have basic type of %s",
1825 sym
->name
, where
, gfc_basic_typename (ts
->type
));
1829 flavor
= sym
->attr
.flavor
;
1831 if (flavor
== FL_PROGRAM
|| flavor
== FL_BLOCK_DATA
|| flavor
== FL_MODULE
1832 || flavor
== FL_LABEL
1833 || (flavor
== FL_PROCEDURE
&& sym
->attr
.subroutine
)
1834 || flavor
== FL_DERIVED
|| flavor
== FL_NAMELIST
)
1836 gfc_error ("Symbol %qs at %L cannot have a type", sym
->name
, where
);
1845 /* Clears all attributes. */
1848 gfc_clear_attr (symbol_attribute
*attr
)
1850 memset (attr
, 0, sizeof (symbol_attribute
));
1854 /* Check for missing attributes in the new symbol. Currently does
1855 nothing, but it's not clear that it is unnecessary yet. */
1858 gfc_missing_attr (symbol_attribute
*attr ATTRIBUTE_UNUSED
,
1859 locus
*where ATTRIBUTE_UNUSED
)
1866 /* Copy an attribute to a symbol attribute, bit by bit. Some
1867 attributes have a lot of side-effects but cannot be present given
1868 where we are called from, so we ignore some bits. */
1871 gfc_copy_attr (symbol_attribute
*dest
, symbol_attribute
*src
, locus
*where
)
1873 int is_proc_lang_bind_spec
;
1875 /* In line with the other attributes, we only add bits but do not remove
1876 them; cf. also PR 41034. */
1877 dest
->ext_attr
|= src
->ext_attr
;
1879 if (src
->allocatable
&& !gfc_add_allocatable (dest
, where
))
1882 if (src
->dimension
&& !gfc_add_dimension (dest
, NULL
, where
))
1884 if (src
->codimension
&& !gfc_add_codimension (dest
, NULL
, where
))
1886 if (src
->contiguous
&& !gfc_add_contiguous (dest
, NULL
, where
))
1888 if (src
->optional
&& !gfc_add_optional (dest
, where
))
1890 if (src
->pointer
&& !gfc_add_pointer (dest
, where
))
1892 if (src
->is_protected
&& !gfc_add_protected (dest
, NULL
, where
))
1894 if (src
->save
&& !gfc_add_save (dest
, src
->save
, NULL
, where
))
1896 if (src
->value
&& !gfc_add_value (dest
, NULL
, where
))
1898 if (src
->volatile_
&& !gfc_add_volatile (dest
, NULL
, where
))
1900 if (src
->asynchronous
&& !gfc_add_asynchronous (dest
, NULL
, where
))
1902 if (src
->threadprivate
1903 && !gfc_add_threadprivate (dest
, NULL
, where
))
1905 if (src
->omp_declare_target
1906 && !gfc_add_omp_declare_target (dest
, NULL
, where
))
1908 if (src
->oacc_declare_create
1909 && !gfc_add_oacc_declare_create (dest
, NULL
, where
))
1911 if (src
->oacc_declare_copyin
1912 && !gfc_add_oacc_declare_copyin (dest
, NULL
, where
))
1914 if (src
->oacc_declare_deviceptr
1915 && !gfc_add_oacc_declare_deviceptr (dest
, NULL
, where
))
1917 if (src
->oacc_declare_device_resident
1918 && !gfc_add_oacc_declare_device_resident (dest
, NULL
, where
))
1920 if (src
->target
&& !gfc_add_target (dest
, where
))
1922 if (src
->dummy
&& !gfc_add_dummy (dest
, NULL
, where
))
1924 if (src
->result
&& !gfc_add_result (dest
, NULL
, where
))
1929 if (src
->in_namelist
&& !gfc_add_in_namelist (dest
, NULL
, where
))
1932 if (src
->in_common
&& !gfc_add_in_common (dest
, NULL
, where
))
1935 if (src
->generic
&& !gfc_add_generic (dest
, NULL
, where
))
1937 if (src
->function
&& !gfc_add_function (dest
, NULL
, where
))
1939 if (src
->subroutine
&& !gfc_add_subroutine (dest
, NULL
, where
))
1942 if (src
->sequence
&& !gfc_add_sequence (dest
, NULL
, where
))
1944 if (src
->elemental
&& !gfc_add_elemental (dest
, where
))
1946 if (src
->pure
&& !gfc_add_pure (dest
, where
))
1948 if (src
->recursive
&& !gfc_add_recursive (dest
, where
))
1951 if (src
->flavor
!= FL_UNKNOWN
1952 && !gfc_add_flavor (dest
, src
->flavor
, NULL
, where
))
1955 if (src
->intent
!= INTENT_UNKNOWN
1956 && !gfc_add_intent (dest
, src
->intent
, where
))
1959 if (src
->access
!= ACCESS_UNKNOWN
1960 && !gfc_add_access (dest
, src
->access
, NULL
, where
))
1963 if (!gfc_missing_attr (dest
, where
))
1966 if (src
->cray_pointer
&& !gfc_add_cray_pointer (dest
, where
))
1968 if (src
->cray_pointee
&& !gfc_add_cray_pointee (dest
, where
))
1971 is_proc_lang_bind_spec
= (src
->flavor
== FL_PROCEDURE
? 1 : 0);
1973 && !gfc_add_is_bind_c (dest
, NULL
, where
, is_proc_lang_bind_spec
))
1976 if (src
->is_c_interop
)
1977 dest
->is_c_interop
= 1;
1981 if (src
->external
&& !gfc_add_external (dest
, where
))
1983 if (src
->intrinsic
&& !gfc_add_intrinsic (dest
, where
))
1985 if (src
->proc_pointer
)
1986 dest
->proc_pointer
= 1;
1995 /* A function to generate a dummy argument symbol using that from the
1996 interface declaration. Can be used for the result symbol as well if
2000 gfc_copy_dummy_sym (gfc_symbol
**dsym
, gfc_symbol
*sym
, int result
)
2004 rc
= gfc_get_symbol (sym
->name
, NULL
, dsym
);
2008 if (!gfc_add_type (*dsym
, &(sym
->ts
), &gfc_current_locus
))
2011 if (!gfc_copy_attr (&(*dsym
)->attr
, &(sym
->attr
),
2012 &gfc_current_locus
))
2015 if ((*dsym
)->attr
.dimension
)
2016 (*dsym
)->as
= gfc_copy_array_spec (sym
->as
);
2018 (*dsym
)->attr
.class_ok
= sym
->attr
.class_ok
;
2020 if ((*dsym
) != NULL
&& !result
2021 && (!gfc_add_dummy(&(*dsym
)->attr
, (*dsym
)->name
, NULL
)
2022 || !gfc_missing_attr (&(*dsym
)->attr
, NULL
)))
2024 else if ((*dsym
) != NULL
&& result
2025 && (!gfc_add_result(&(*dsym
)->attr
, (*dsym
)->name
, NULL
)
2026 || !gfc_missing_attr (&(*dsym
)->attr
, NULL
)))
2033 /************** Component name management ************/
2035 /* Component names of a derived type form their own little namespaces
2036 that are separate from all other spaces. The space is composed of
2037 a singly linked list of gfc_component structures whose head is
2038 located in the parent symbol. */
2041 /* Add a component name to a symbol. The call fails if the name is
2042 already present. On success, the component pointer is modified to
2043 point to the additional component structure. */
2046 gfc_add_component (gfc_symbol
*sym
, const char *name
,
2047 gfc_component
**component
)
2049 gfc_component
*p
, *tail
;
2053 for (p
= sym
->components
; p
; p
= p
->next
)
2055 if (strcmp (p
->name
, name
) == 0)
2057 gfc_error ("Component %qs at %C already declared at %L",
2065 if (sym
->attr
.extension
2066 && gfc_find_component (sym
->components
->ts
.u
.derived
, name
, true, true))
2068 gfc_error ("Component %qs at %C already in the parent type "
2069 "at %L", name
, &sym
->components
->ts
.u
.derived
->declared_at
);
2073 /* Allocate a new component. */
2074 p
= gfc_get_component ();
2077 sym
->components
= p
;
2081 p
->name
= gfc_get_string (name
);
2082 p
->loc
= gfc_current_locus
;
2083 p
->ts
.type
= BT_UNKNOWN
;
2090 /* Recursive function to switch derived types of all symbol in a
2094 switch_types (gfc_symtree
*st
, gfc_symbol
*from
, gfc_symbol
*to
)
2102 if (sym
->ts
.type
== BT_DERIVED
&& sym
->ts
.u
.derived
== from
)
2103 sym
->ts
.u
.derived
= to
;
2105 switch_types (st
->left
, from
, to
);
2106 switch_types (st
->right
, from
, to
);
2110 /* This subroutine is called when a derived type is used in order to
2111 make the final determination about which version to use. The
2112 standard requires that a type be defined before it is 'used', but
2113 such types can appear in IMPLICIT statements before the actual
2114 definition. 'Using' in this context means declaring a variable to
2115 be that type or using the type constructor.
2117 If a type is used and the components haven't been defined, then we
2118 have to have a derived type in a parent unit. We find the node in
2119 the other namespace and point the symtree node in this namespace to
2120 that node. Further reference to this name point to the correct
2121 node. If we can't find the node in a parent namespace, then we have
2124 This subroutine takes a pointer to a symbol node and returns a
2125 pointer to the translated node or NULL for an error. Usually there
2126 is no translation and we return the node we were passed. */
2129 gfc_use_derived (gfc_symbol
*sym
)
2139 if (sym
->attr
.unlimited_polymorphic
)
2142 if (sym
->attr
.generic
)
2143 sym
= gfc_find_dt_in_generic (sym
);
2145 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
2146 return sym
; /* Already defined. */
2148 if (sym
->ns
->parent
== NULL
)
2151 if (gfc_find_symbol (sym
->name
, sym
->ns
->parent
, 1, &s
))
2153 gfc_error ("Symbol %qs at %C is ambiguous", sym
->name
);
2157 if (s
== NULL
|| s
->attr
.flavor
!= FL_DERIVED
)
2160 /* Get rid of symbol sym, translating all references to s. */
2161 for (i
= 0; i
< GFC_LETTERS
; i
++)
2163 t
= &sym
->ns
->default_type
[i
];
2164 if (t
->u
.derived
== sym
)
2168 st
= gfc_find_symtree (sym
->ns
->sym_root
, sym
->name
);
2173 /* Unlink from list of modified symbols. */
2174 gfc_commit_symbol (sym
);
2176 switch_types (sym
->ns
->sym_root
, sym
, s
);
2178 /* TODO: Also have to replace sym -> s in other lists like
2179 namelists, common lists and interface lists. */
2180 gfc_free_symbol (sym
);
2185 gfc_error ("Derived type %qs at %C is being used before it is defined",
2191 /* Given a derived type node and a component name, try to locate the
2192 component structure. Returns the NULL pointer if the component is
2193 not found or the components are private. If noaccess is set, no access
2197 gfc_find_component (gfc_symbol
*sym
, const char *name
,
2198 bool noaccess
, bool silent
)
2202 if (name
== NULL
|| sym
== NULL
)
2205 sym
= gfc_use_derived (sym
);
2210 for (p
= sym
->components
; p
; p
= p
->next
)
2211 if (strcmp (p
->name
, name
) == 0)
2214 if (p
&& sym
->attr
.use_assoc
&& !noaccess
)
2216 bool is_parent_comp
= sym
->attr
.extension
&& (p
== sym
->components
);
2217 if (p
->attr
.access
== ACCESS_PRIVATE
||
2218 (p
->attr
.access
!= ACCESS_PUBLIC
2219 && sym
->component_access
== ACCESS_PRIVATE
2220 && !is_parent_comp
))
2223 gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
2230 && sym
->attr
.extension
2231 && sym
->components
->ts
.type
== BT_DERIVED
)
2233 p
= gfc_find_component (sym
->components
->ts
.u
.derived
, name
,
2235 /* Do not overwrite the error. */
2240 if (p
== NULL
&& !silent
)
2241 gfc_error ("%qs at %C is not a member of the %qs structure",
2248 /* Given a symbol, free all of the component structures and everything
2252 free_components (gfc_component
*p
)
2260 gfc_free_array_spec (p
->as
);
2261 gfc_free_expr (p
->initializer
);
2269 /******************** Statement label management ********************/
2271 /* Comparison function for statement labels, used for managing the
2275 compare_st_labels (void *a1
, void *b1
)
2277 int a
= ((gfc_st_label
*) a1
)->value
;
2278 int b
= ((gfc_st_label
*) b1
)->value
;
2284 /* Free a single gfc_st_label structure, making sure the tree is not
2285 messed up. This function is called only when some parse error
2289 gfc_free_st_label (gfc_st_label
*label
)
2295 gfc_delete_bbt (&label
->ns
->st_labels
, label
, compare_st_labels
);
2297 if (label
->format
!= NULL
)
2298 gfc_free_expr (label
->format
);
2304 /* Free a whole tree of gfc_st_label structures. */
2307 free_st_labels (gfc_st_label
*label
)
2313 free_st_labels (label
->left
);
2314 free_st_labels (label
->right
);
2316 if (label
->format
!= NULL
)
2317 gfc_free_expr (label
->format
);
2322 /* Given a label number, search for and return a pointer to the label
2323 structure, creating it if it does not exist. */
2326 gfc_get_st_label (int labelno
)
2331 if (gfc_current_state () == COMP_DERIVED
)
2332 ns
= gfc_current_block ()->f2k_derived
;
2335 /* Find the namespace of the scoping unit:
2336 If we're in a BLOCK construct, jump to the parent namespace. */
2337 ns
= gfc_current_ns
;
2338 while (ns
->proc_name
&& ns
->proc_name
->attr
.flavor
== FL_LABEL
)
2342 /* First see if the label is already in this namespace. */
2346 if (lp
->value
== labelno
)
2349 if (lp
->value
< labelno
)
2355 lp
= XCNEW (gfc_st_label
);
2357 lp
->value
= labelno
;
2358 lp
->defined
= ST_LABEL_UNKNOWN
;
2359 lp
->referenced
= ST_LABEL_UNKNOWN
;
2362 gfc_insert_bbt (&ns
->st_labels
, lp
, compare_st_labels
);
2368 /* Called when a statement with a statement label is about to be
2369 accepted. We add the label to the list of the current namespace,
2370 making sure it hasn't been defined previously and referenced
2374 gfc_define_st_label (gfc_st_label
*lp
, gfc_sl_type type
, locus
*label_locus
)
2378 labelno
= lp
->value
;
2380 if (lp
->defined
!= ST_LABEL_UNKNOWN
)
2381 gfc_error ("Duplicate statement label %d at %L and %L", labelno
,
2382 &lp
->where
, label_locus
);
2385 lp
->where
= *label_locus
;
2389 case ST_LABEL_FORMAT
:
2390 if (lp
->referenced
== ST_LABEL_TARGET
2391 || lp
->referenced
== ST_LABEL_DO_TARGET
)
2392 gfc_error ("Label %d at %C already referenced as branch target",
2395 lp
->defined
= ST_LABEL_FORMAT
;
2399 case ST_LABEL_TARGET
:
2400 case ST_LABEL_DO_TARGET
:
2401 if (lp
->referenced
== ST_LABEL_FORMAT
)
2402 gfc_error ("Label %d at %C already referenced as a format label",
2407 if (lp
->referenced
== ST_LABEL_DO_TARGET
&& type
!= ST_LABEL_DO_TARGET
2408 && !gfc_notify_std (GFC_STD_F95_OBS
, "DO termination statement "
2409 "which is not END DO or CONTINUE with "
2410 "label %d at %C", labelno
))
2415 lp
->defined
= ST_LABEL_BAD_TARGET
;
2416 lp
->referenced
= ST_LABEL_BAD_TARGET
;
2422 /* Reference a label. Given a label and its type, see if that
2423 reference is consistent with what is known about that label,
2424 updating the unknown state. Returns false if something goes
2428 gfc_reference_st_label (gfc_st_label
*lp
, gfc_sl_type type
)
2430 gfc_sl_type label_type
;
2437 labelno
= lp
->value
;
2439 if (lp
->defined
!= ST_LABEL_UNKNOWN
)
2440 label_type
= lp
->defined
;
2443 label_type
= lp
->referenced
;
2444 lp
->where
= gfc_current_locus
;
2447 if (label_type
== ST_LABEL_FORMAT
2448 && (type
== ST_LABEL_TARGET
|| type
== ST_LABEL_DO_TARGET
))
2450 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno
);
2455 if ((label_type
== ST_LABEL_TARGET
|| label_type
== ST_LABEL_DO_TARGET
2456 || label_type
== ST_LABEL_BAD_TARGET
)
2457 && type
== ST_LABEL_FORMAT
)
2459 gfc_error ("Label %d at %C previously used as branch target", labelno
);
2464 if (lp
->referenced
== ST_LABEL_DO_TARGET
&& type
== ST_LABEL_DO_TARGET
2465 && !gfc_notify_std (GFC_STD_F95_OBS
, "Shared DO termination label %d "
2469 if (lp
->referenced
!= ST_LABEL_DO_TARGET
)
2470 lp
->referenced
= type
;
2478 /************** Symbol table management subroutines ****************/
2480 /* Basic details: Fortran 95 requires a potentially unlimited number
2481 of distinct namespaces when compiling a program unit. This case
2482 occurs during a compilation of internal subprograms because all of
2483 the internal subprograms must be read before we can start
2484 generating code for the host.
2486 Given the tricky nature of the Fortran grammar, we must be able to
2487 undo changes made to a symbol table if the current interpretation
2488 of a statement is found to be incorrect. Whenever a symbol is
2489 looked up, we make a copy of it and link to it. All of these
2490 symbols are kept in a vector so that we can commit or
2491 undo the changes at a later time.
2493 A symtree may point to a symbol node outside of its namespace. In
2494 this case, that symbol has been used as a host associated variable
2495 at some previous time. */
2497 /* Allocate a new namespace structure. Copies the implicit types from
2498 PARENT if PARENT_TYPES is set. */
2501 gfc_get_namespace (gfc_namespace
*parent
, int parent_types
)
2508 ns
= XCNEW (gfc_namespace
);
2509 ns
->sym_root
= NULL
;
2510 ns
->uop_root
= NULL
;
2511 ns
->tb_sym_root
= NULL
;
2512 ns
->finalizers
= NULL
;
2513 ns
->default_access
= ACCESS_UNKNOWN
;
2514 ns
->parent
= parent
;
2516 for (in
= GFC_INTRINSIC_BEGIN
; in
!= GFC_INTRINSIC_END
; in
++)
2518 ns
->operator_access
[in
] = ACCESS_UNKNOWN
;
2519 ns
->tb_op
[in
] = NULL
;
2522 /* Initialize default implicit types. */
2523 for (i
= 'a'; i
<= 'z'; i
++)
2525 ns
->set_flag
[i
- 'a'] = 0;
2526 ts
= &ns
->default_type
[i
- 'a'];
2528 if (parent_types
&& ns
->parent
!= NULL
)
2530 /* Copy parent settings. */
2531 *ts
= ns
->parent
->default_type
[i
- 'a'];
2535 if (flag_implicit_none
!= 0)
2541 if ('i' <= i
&& i
<= 'n')
2543 ts
->type
= BT_INTEGER
;
2544 ts
->kind
= gfc_default_integer_kind
;
2549 ts
->kind
= gfc_default_real_kind
;
2553 if (parent_types
&& ns
->parent
!= NULL
)
2554 ns
->has_implicit_none_export
= ns
->parent
->has_implicit_none_export
;
2562 /* Comparison function for symtree nodes. */
2565 compare_symtree (void *_st1
, void *_st2
)
2567 gfc_symtree
*st1
, *st2
;
2569 st1
= (gfc_symtree
*) _st1
;
2570 st2
= (gfc_symtree
*) _st2
;
2572 return strcmp (st1
->name
, st2
->name
);
2576 /* Allocate a new symtree node and associate it with the new symbol. */
2579 gfc_new_symtree (gfc_symtree
**root
, const char *name
)
2583 st
= XCNEW (gfc_symtree
);
2584 st
->name
= gfc_get_string (name
);
2586 gfc_insert_bbt (root
, st
, compare_symtree
);
2591 /* Delete a symbol from the tree. Does not free the symbol itself! */
2594 gfc_delete_symtree (gfc_symtree
**root
, const char *name
)
2596 gfc_symtree st
, *st0
;
2598 st0
= gfc_find_symtree (*root
, name
);
2600 st
.name
= gfc_get_string (name
);
2601 gfc_delete_bbt (root
, &st
, compare_symtree
);
2607 /* Given a root symtree node and a name, try to find the symbol within
2608 the namespace. Returns NULL if the symbol is not found. */
2611 gfc_find_symtree (gfc_symtree
*st
, const char *name
)
2617 c
= strcmp (name
, st
->name
);
2621 st
= (c
< 0) ? st
->left
: st
->right
;
2628 /* Return a symtree node with a name that is guaranteed to be unique
2629 within the namespace and corresponds to an illegal fortran name. */
2632 gfc_get_unique_symtree (gfc_namespace
*ns
)
2634 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2635 static int serial
= 0;
2637 sprintf (name
, "@%d", serial
++);
2638 return gfc_new_symtree (&ns
->sym_root
, name
);
2642 /* Given a name find a user operator node, creating it if it doesn't
2643 exist. These are much simpler than symbols because they can't be
2644 ambiguous with one another. */
2647 gfc_get_uop (const char *name
)
2651 gfc_namespace
*ns
= gfc_current_ns
;
2655 st
= gfc_find_symtree (ns
->uop_root
, name
);
2659 st
= gfc_new_symtree (&ns
->uop_root
, name
);
2661 uop
= st
->n
.uop
= XCNEW (gfc_user_op
);
2662 uop
->name
= gfc_get_string (name
);
2663 uop
->access
= ACCESS_UNKNOWN
;
2670 /* Given a name find the user operator node. Returns NULL if it does
2674 gfc_find_uop (const char *name
, gfc_namespace
*ns
)
2679 ns
= gfc_current_ns
;
2681 st
= gfc_find_symtree (ns
->uop_root
, name
);
2682 return (st
== NULL
) ? NULL
: st
->n
.uop
;
2686 /* Update a symbol's common_block field, and take care of the associated
2687 memory management. */
2690 set_symbol_common_block (gfc_symbol
*sym
, gfc_common_head
*common_block
)
2692 if (sym
->common_block
== common_block
)
2695 if (sym
->common_block
&& sym
->common_block
->name
[0] != '\0')
2697 sym
->common_block
->refs
--;
2698 if (sym
->common_block
->refs
== 0)
2699 free (sym
->common_block
);
2701 sym
->common_block
= common_block
;
2705 /* Remove a gfc_symbol structure and everything it points to. */
2708 gfc_free_symbol (gfc_symbol
*sym
)
2714 gfc_free_array_spec (sym
->as
);
2716 free_components (sym
->components
);
2718 gfc_free_expr (sym
->value
);
2720 gfc_free_namelist (sym
->namelist
);
2722 if (sym
->ns
!= sym
->formal_ns
)
2723 gfc_free_namespace (sym
->formal_ns
);
2725 if (!sym
->attr
.generic_copy
)
2726 gfc_free_interface (sym
->generic
);
2728 gfc_free_formal_arglist (sym
->formal
);
2730 gfc_free_namespace (sym
->f2k_derived
);
2732 set_symbol_common_block (sym
, NULL
);
2738 /* Decrease the reference counter and free memory when we reach zero. */
2741 gfc_release_symbol (gfc_symbol
*sym
)
2746 if (sym
->formal_ns
!= NULL
&& sym
->refs
== 2 && sym
->formal_ns
!= sym
->ns
2747 && (!sym
->attr
.entry
|| !sym
->module
))
2749 /* As formal_ns contains a reference to sym, delete formal_ns just
2750 before the deletion of sym. */
2751 gfc_namespace
*ns
= sym
->formal_ns
;
2752 sym
->formal_ns
= NULL
;
2753 gfc_free_namespace (ns
);
2760 gcc_assert (sym
->refs
== 0);
2761 gfc_free_symbol (sym
);
2765 /* Allocate and initialize a new symbol node. */
2768 gfc_new_symbol (const char *name
, gfc_namespace
*ns
)
2772 p
= XCNEW (gfc_symbol
);
2774 gfc_clear_ts (&p
->ts
);
2775 gfc_clear_attr (&p
->attr
);
2778 p
->declared_at
= gfc_current_locus
;
2780 if (strlen (name
) > GFC_MAX_SYMBOL_LEN
)
2781 gfc_internal_error ("new_symbol(): Symbol name too long");
2783 p
->name
= gfc_get_string (name
);
2785 /* Make sure flags for symbol being C bound are clear initially. */
2786 p
->attr
.is_bind_c
= 0;
2787 p
->attr
.is_iso_c
= 0;
2789 /* Clear the ptrs we may need. */
2790 p
->common_block
= NULL
;
2791 p
->f2k_derived
= NULL
;
2798 /* Generate an error if a symbol is ambiguous. */
2801 ambiguous_symbol (const char *name
, gfc_symtree
*st
)
2804 if (st
->n
.sym
->module
)
2805 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2806 "from module %qs", name
, st
->n
.sym
->name
, st
->n
.sym
->module
);
2808 gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
2809 "from current program unit", name
, st
->n
.sym
->name
);
2813 /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
2814 selector on the stack. If yes, replace it by the corresponding temporary. */
2817 select_type_insert_tmp (gfc_symtree
**st
)
2819 gfc_select_type_stack
*stack
= select_type_stack
;
2820 for (; stack
; stack
= stack
->prev
)
2821 if ((*st
)->n
.sym
== stack
->selector
&& stack
->tmp
)
2826 /* Look for a symtree in the current procedure -- that is, go up to
2827 parent namespaces but only if inside a BLOCK. Returns NULL if not found. */
2830 gfc_find_symtree_in_proc (const char* name
, gfc_namespace
* ns
)
2834 gfc_symtree
* st
= gfc_find_symtree (ns
->sym_root
, name
);
2838 if (!ns
->construct_entities
)
2847 /* Search for a symtree starting in the current namespace, resorting to
2848 any parent namespaces if requested by a nonzero parent_flag.
2849 Returns nonzero if the name is ambiguous. */
2852 gfc_find_sym_tree (const char *name
, gfc_namespace
*ns
, int parent_flag
,
2853 gfc_symtree
**result
)
2858 ns
= gfc_current_ns
;
2862 st
= gfc_find_symtree (ns
->sym_root
, name
);
2865 select_type_insert_tmp (&st
);
2868 /* Ambiguous generic interfaces are permitted, as long
2869 as the specific interfaces are different. */
2870 if (st
->ambiguous
&& !st
->n
.sym
->attr
.generic
)
2872 ambiguous_symbol (name
, st
);
2882 /* Don't escape an interface block. */
2883 if (ns
&& !ns
->has_import_set
2884 && ns
->proc_name
&& ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
2896 /* Same, but returns the symbol instead. */
2899 gfc_find_symbol (const char *name
, gfc_namespace
*ns
, int parent_flag
,
2900 gfc_symbol
**result
)
2905 i
= gfc_find_sym_tree (name
, ns
, parent_flag
, &st
);
2910 *result
= st
->n
.sym
;
2916 /* Tells whether there is only one set of changes in the stack. */
2919 single_undo_checkpoint_p (void)
2921 if (latest_undo_chgset
== &default_undo_chgset_var
)
2923 gcc_assert (latest_undo_chgset
->previous
== NULL
);
2928 gcc_assert (latest_undo_chgset
->previous
!= NULL
);
2933 /* Save symbol with the information necessary to back it out. */
2936 gfc_save_symbol_data (gfc_symbol
*sym
)
2941 if (!single_undo_checkpoint_p ())
2943 /* If there is more than one change set, look for the symbol in the
2944 current one. If it is found there, we can reuse it. */
2945 FOR_EACH_VEC_ELT (latest_undo_chgset
->syms
, i
, s
)
2948 gcc_assert (sym
->gfc_new
|| sym
->old_symbol
!= NULL
);
2952 else if (sym
->gfc_new
|| sym
->old_symbol
!= NULL
)
2955 s
= XCNEW (gfc_symbol
);
2957 sym
->old_symbol
= s
;
2960 latest_undo_chgset
->syms
.safe_push (sym
);
2964 /* Given a name, find a symbol, or create it if it does not exist yet
2965 in the current namespace. If the symbol is found we make sure that
2968 The integer return code indicates
2970 1 The symbol name was ambiguous
2971 2 The name meant to be established was already host associated.
2973 So if the return value is nonzero, then an error was issued. */
2976 gfc_get_sym_tree (const char *name
, gfc_namespace
*ns
, gfc_symtree
**result
,
2977 bool allow_subroutine
)
2982 /* This doesn't usually happen during resolution. */
2984 ns
= gfc_current_ns
;
2986 /* Try to find the symbol in ns. */
2987 st
= gfc_find_symtree (ns
->sym_root
, name
);
2989 if (st
== NULL
&& ns
->omp_udr_ns
)
2992 st
= gfc_find_symtree (ns
->sym_root
, name
);
2997 /* If not there, create a new symbol. */
2998 p
= gfc_new_symbol (name
, ns
);
3000 /* Add to the list of tentative symbols. */
3001 p
->old_symbol
= NULL
;
3004 latest_undo_chgset
->syms
.safe_push (p
);
3006 st
= gfc_new_symtree (&ns
->sym_root
, name
);
3013 /* Make sure the existing symbol is OK. Ambiguous
3014 generic interfaces are permitted, as long as the
3015 specific interfaces are different. */
3016 if (st
->ambiguous
&& !st
->n
.sym
->attr
.generic
)
3018 ambiguous_symbol (name
, st
);
3023 if (p
->ns
!= ns
&& (!p
->attr
.function
|| ns
->proc_name
!= p
)
3024 && !(allow_subroutine
&& p
->attr
.subroutine
)
3025 && !(ns
->proc_name
&& ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
3026 && (ns
->has_import_set
|| p
->attr
.imported
)))
3028 /* Symbol is from another namespace. */
3029 gfc_error ("Symbol %qs at %C has already been host associated",
3036 /* Copy in case this symbol is changed. */
3037 gfc_save_symbol_data (p
);
3046 gfc_get_symbol (const char *name
, gfc_namespace
*ns
, gfc_symbol
**result
)
3051 i
= gfc_get_sym_tree (name
, ns
, &st
, false);
3056 *result
= st
->n
.sym
;
3063 /* Subroutine that searches for a symbol, creating it if it doesn't
3064 exist, but tries to host-associate the symbol if possible. */
3067 gfc_get_ha_sym_tree (const char *name
, gfc_symtree
**result
)
3072 i
= gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
3076 gfc_save_symbol_data (st
->n
.sym
);
3081 i
= gfc_find_sym_tree (name
, gfc_current_ns
, 1, &st
);
3091 return gfc_get_sym_tree (name
, gfc_current_ns
, result
, false);
3096 gfc_get_ha_symbol (const char *name
, gfc_symbol
**result
)
3101 i
= gfc_get_ha_sym_tree (name
, &st
);
3104 *result
= st
->n
.sym
;
3112 /* Search for the symtree belonging to a gfc_common_head; we cannot use
3113 head->name as the common_root symtree's name might be mangled. */
3115 static gfc_symtree
*
3116 find_common_symtree (gfc_symtree
*st
, gfc_common_head
*head
)
3119 gfc_symtree
*result
;
3124 if (st
->n
.common
== head
)
3127 result
= find_common_symtree (st
->left
, head
);
3129 result
= find_common_symtree (st
->right
, head
);
3135 /* Clear the given storage, and make it the current change set for registering
3136 changed symbols. Its contents are freed after a call to
3137 gfc_restore_last_undo_checkpoint or gfc_drop_last_undo_checkpoint, but
3138 it is up to the caller to free the storage itself. It is usually a local
3139 variable, so there is nothing to do anyway. */
3142 gfc_new_undo_checkpoint (gfc_undo_change_set
&chg_syms
)
3144 chg_syms
.syms
= vNULL
;
3145 chg_syms
.tbps
= vNULL
;
3146 chg_syms
.previous
= latest_undo_chgset
;
3147 latest_undo_chgset
= &chg_syms
;
3151 /* Restore previous state of symbol. Just copy simple stuff. */
3154 restore_old_symbol (gfc_symbol
*p
)
3159 old
= p
->old_symbol
;
3161 p
->ts
.type
= old
->ts
.type
;
3162 p
->ts
.kind
= old
->ts
.kind
;
3164 p
->attr
= old
->attr
;
3166 if (p
->value
!= old
->value
)
3168 gcc_checking_assert (old
->value
== NULL
);
3169 gfc_free_expr (p
->value
);
3173 if (p
->as
!= old
->as
)
3176 gfc_free_array_spec (p
->as
);
3180 p
->generic
= old
->generic
;
3181 p
->component_access
= old
->component_access
;
3183 if (p
->namelist
!= NULL
&& old
->namelist
== NULL
)
3185 gfc_free_namelist (p
->namelist
);
3190 if (p
->namelist_tail
!= old
->namelist_tail
)
3192 gfc_free_namelist (old
->namelist_tail
->next
);
3193 old
->namelist_tail
->next
= NULL
;
3197 p
->namelist_tail
= old
->namelist_tail
;
3199 if (p
->formal
!= old
->formal
)
3201 gfc_free_formal_arglist (p
->formal
);
3202 p
->formal
= old
->formal
;
3205 set_symbol_common_block (p
, old
->common_block
);
3206 p
->common_head
= old
->common_head
;
3208 p
->old_symbol
= old
->old_symbol
;
3213 /* Frees the internal data of a gfc_undo_change_set structure. Doesn't free
3214 the structure itself. */
3217 free_undo_change_set_data (gfc_undo_change_set
&cs
)
3224 /* Given a change set pointer, free its target's contents and update it with
3225 the address of the previous change set. Note that only the contents are
3226 freed, not the target itself (the contents' container). It is not a problem
3227 as the latter will be a local variable usually. */
3230 pop_undo_change_set (gfc_undo_change_set
*&cs
)
3232 free_undo_change_set_data (*cs
);
3237 static void free_old_symbol (gfc_symbol
*sym
);
3240 /* Merges the current change set into the previous one. The changes themselves
3241 are left untouched; only one checkpoint is forgotten. */
3244 gfc_drop_last_undo_checkpoint (void)
3249 FOR_EACH_VEC_ELT (latest_undo_chgset
->syms
, i
, s
)
3251 /* No need to loop in this case. */
3252 if (s
->old_symbol
== NULL
)
3255 /* Remove the duplicate symbols. */
3256 FOR_EACH_VEC_ELT (latest_undo_chgset
->previous
->syms
, j
, t
)
3259 latest_undo_chgset
->previous
->syms
.unordered_remove (j
);
3261 /* S->OLD_SYMBOL is the backup symbol for S as it was at the
3262 last checkpoint. We drop that checkpoint, so S->OLD_SYMBOL
3263 shall contain from now on the backup symbol for S as it was
3264 at the checkpoint before. */
3265 if (s
->old_symbol
->gfc_new
)
3267 gcc_assert (s
->old_symbol
->old_symbol
== NULL
);
3268 s
->gfc_new
= s
->old_symbol
->gfc_new
;
3269 free_old_symbol (s
);
3272 restore_old_symbol (s
->old_symbol
);
3277 latest_undo_chgset
->previous
->syms
.safe_splice (latest_undo_chgset
->syms
);
3278 latest_undo_chgset
->previous
->tbps
.safe_splice (latest_undo_chgset
->tbps
);
3280 pop_undo_change_set (latest_undo_chgset
);
3284 /* Undoes all the changes made to symbols since the previous checkpoint.
3285 This subroutine is made simpler due to the fact that attributes are
3286 never removed once added. */
3289 gfc_restore_last_undo_checkpoint (void)
3294 FOR_EACH_VEC_ELT (latest_undo_chgset
->syms
, i
, p
)
3296 /* Symbol in a common block was new. Or was old and just put in common */
3298 && (p
->gfc_new
|| !p
->old_symbol
->common_block
))
3300 /* If the symbol was added to any common block, it
3301 needs to be removed to stop the resolver looking
3302 for a (possibly) dead symbol. */
3303 if (p
->common_block
->head
== p
&& !p
->common_next
)
3305 gfc_symtree st
, *st0
;
3306 st0
= find_common_symtree (p
->ns
->common_root
,
3310 st
.name
= st0
->name
;
3311 gfc_delete_bbt (&p
->ns
->common_root
, &st
, compare_symtree
);
3316 if (p
->common_block
->head
== p
)
3317 p
->common_block
->head
= p
->common_next
;
3320 gfc_symbol
*cparent
, *csym
;
3322 cparent
= p
->common_block
->head
;
3323 csym
= cparent
->common_next
;
3328 csym
= csym
->common_next
;
3331 gcc_assert(cparent
->common_next
== p
);
3332 cparent
->common_next
= csym
->common_next
;
3334 p
->common_next
= NULL
;
3338 /* The derived type is saved in the symtree with the first
3339 letter capitalized; the all lower-case version to the
3340 derived type contains its associated generic function. */
3341 if (p
->attr
.flavor
== FL_DERIVED
)
3342 gfc_delete_symtree (&p
->ns
->sym_root
, gfc_get_string ("%c%s",
3343 (char) TOUPPER ((unsigned char) p
->name
[0]),
3346 gfc_delete_symtree (&p
->ns
->sym_root
, p
->name
);
3348 gfc_release_symbol (p
);
3351 restore_old_symbol (p
);
3354 latest_undo_chgset
->syms
.truncate (0);
3355 latest_undo_chgset
->tbps
.truncate (0);
3357 if (!single_undo_checkpoint_p ())
3358 pop_undo_change_set (latest_undo_chgset
);
3362 /* Makes sure that there is only one set of changes; in other words we haven't
3363 forgotten to pair a call to gfc_new_checkpoint with a call to either
3364 gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint. */
3367 enforce_single_undo_checkpoint (void)
3369 gcc_checking_assert (single_undo_checkpoint_p ());
3373 /* Undoes all the changes made to symbols in the current statement. */
3376 gfc_undo_symbols (void)
3378 enforce_single_undo_checkpoint ();
3379 gfc_restore_last_undo_checkpoint ();
3383 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
3384 components of old_symbol that might need deallocation are the "allocatables"
3385 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
3386 namelist_tail. In case these differ between old_symbol and sym, it's just
3387 because sym->namelist has gotten a few more items. */
3390 free_old_symbol (gfc_symbol
*sym
)
3393 if (sym
->old_symbol
== NULL
)
3396 if (sym
->old_symbol
->as
!= sym
->as
)
3397 gfc_free_array_spec (sym
->old_symbol
->as
);
3399 if (sym
->old_symbol
->value
!= sym
->value
)
3400 gfc_free_expr (sym
->old_symbol
->value
);
3402 if (sym
->old_symbol
->formal
!= sym
->formal
)
3403 gfc_free_formal_arglist (sym
->old_symbol
->formal
);
3405 free (sym
->old_symbol
);
3406 sym
->old_symbol
= NULL
;
3410 /* Makes the changes made in the current statement permanent-- gets
3411 rid of undo information. */
3414 gfc_commit_symbols (void)
3417 gfc_typebound_proc
*tbp
;
3420 enforce_single_undo_checkpoint ();
3422 FOR_EACH_VEC_ELT (latest_undo_chgset
->syms
, i
, p
)
3426 free_old_symbol (p
);
3428 latest_undo_chgset
->syms
.truncate (0);
3430 FOR_EACH_VEC_ELT (latest_undo_chgset
->tbps
, i
, tbp
)
3432 latest_undo_chgset
->tbps
.truncate (0);
3436 /* Makes the changes made in one symbol permanent -- gets rid of undo
3440 gfc_commit_symbol (gfc_symbol
*sym
)
3445 enforce_single_undo_checkpoint ();
3447 FOR_EACH_VEC_ELT (latest_undo_chgset
->syms
, i
, p
)
3450 latest_undo_chgset
->syms
.unordered_remove (i
);
3457 free_old_symbol (sym
);
3461 /* Recursively free trees containing type-bound procedures. */
3464 free_tb_tree (gfc_symtree
*t
)
3469 free_tb_tree (t
->left
);
3470 free_tb_tree (t
->right
);
3472 /* TODO: Free type-bound procedure structs themselves; probably needs some
3473 sort of ref-counting mechanism. */
3479 /* Recursive function that deletes an entire tree and all the common
3480 head structures it points to. */
3483 free_common_tree (gfc_symtree
* common_tree
)
3485 if (common_tree
== NULL
)
3488 free_common_tree (common_tree
->left
);
3489 free_common_tree (common_tree
->right
);
3495 /* Recursive function that deletes an entire tree and all the common
3496 head structures it points to. */
3499 free_omp_udr_tree (gfc_symtree
* omp_udr_tree
)
3501 if (omp_udr_tree
== NULL
)
3504 free_omp_udr_tree (omp_udr_tree
->left
);
3505 free_omp_udr_tree (omp_udr_tree
->right
);
3507 gfc_free_omp_udr (omp_udr_tree
->n
.omp_udr
);
3508 free (omp_udr_tree
);
3512 /* Recursive function that deletes an entire tree and all the user
3513 operator nodes that it contains. */
3516 free_uop_tree (gfc_symtree
*uop_tree
)
3518 if (uop_tree
== NULL
)
3521 free_uop_tree (uop_tree
->left
);
3522 free_uop_tree (uop_tree
->right
);
3524 gfc_free_interface (uop_tree
->n
.uop
->op
);
3525 free (uop_tree
->n
.uop
);
3530 /* Recursive function that deletes an entire tree and all the symbols
3531 that it contains. */
3534 free_sym_tree (gfc_symtree
*sym_tree
)
3536 if (sym_tree
== NULL
)
3539 free_sym_tree (sym_tree
->left
);
3540 free_sym_tree (sym_tree
->right
);
3542 gfc_release_symbol (sym_tree
->n
.sym
);
3547 /* Free the derived type list. */
3550 gfc_free_dt_list (void)
3552 gfc_dt_list
*dt
, *n
;
3554 for (dt
= gfc_derived_types
; dt
; dt
= n
)
3560 gfc_derived_types
= NULL
;
3564 /* Free the gfc_equiv_info's. */
3567 gfc_free_equiv_infos (gfc_equiv_info
*s
)
3571 gfc_free_equiv_infos (s
->next
);
3576 /* Free the gfc_equiv_lists. */
3579 gfc_free_equiv_lists (gfc_equiv_list
*l
)
3583 gfc_free_equiv_lists (l
->next
);
3584 gfc_free_equiv_infos (l
->equiv
);
3589 /* Free a finalizer procedure list. */
3592 gfc_free_finalizer (gfc_finalizer
* el
)
3596 gfc_release_symbol (el
->proc_sym
);
3602 gfc_free_finalizer_list (gfc_finalizer
* list
)
3606 gfc_finalizer
* current
= list
;
3608 gfc_free_finalizer (current
);
3613 /* Create a new gfc_charlen structure and add it to a namespace.
3614 If 'old_cl' is given, the newly created charlen will be a copy of it. */
3617 gfc_new_charlen (gfc_namespace
*ns
, gfc_charlen
*old_cl
)
3620 cl
= gfc_get_charlen ();
3625 /* Put into namespace, but don't allow reject_statement
3626 to free it if old_cl is given. */
3627 gfc_charlen
**prev
= &ns
->cl_list
;
3628 cl
->next
= ns
->old_cl_list
;
3629 while (*prev
!= ns
->old_cl_list
)
3630 prev
= &(*prev
)->next
;
3632 ns
->old_cl_list
= cl
;
3633 cl
->length
= gfc_copy_expr (old_cl
->length
);
3634 cl
->length_from_typespec
= old_cl
->length_from_typespec
;
3635 cl
->backend_decl
= old_cl
->backend_decl
;
3636 cl
->passed_length
= old_cl
->passed_length
;
3637 cl
->resolved
= old_cl
->resolved
;
3641 /* Put into namespace. */
3642 cl
->next
= ns
->cl_list
;
3650 /* Free the charlen list from cl to end (end is not freed).
3651 Free the whole list if end is NULL. */
3654 gfc_free_charlen (gfc_charlen
*cl
, gfc_charlen
*end
)
3658 for (; cl
!= end
; cl
= cl2
)
3663 gfc_free_expr (cl
->length
);
3669 /* Free entry list structs. */
3672 free_entry_list (gfc_entry_list
*el
)
3674 gfc_entry_list
*next
;
3681 free_entry_list (next
);
3685 /* Free a namespace structure and everything below it. Interface
3686 lists associated with intrinsic operators are not freed. These are
3687 taken care of when a specific name is freed. */
3690 gfc_free_namespace (gfc_namespace
*ns
)
3692 gfc_namespace
*p
, *q
;
3701 gcc_assert (ns
->refs
== 0);
3703 gfc_free_statements (ns
->code
);
3705 free_sym_tree (ns
->sym_root
);
3706 free_uop_tree (ns
->uop_root
);
3707 free_common_tree (ns
->common_root
);
3708 free_omp_udr_tree (ns
->omp_udr_root
);
3709 free_tb_tree (ns
->tb_sym_root
);
3710 free_tb_tree (ns
->tb_uop_root
);
3711 gfc_free_finalizer_list (ns
->finalizers
);
3712 gfc_free_omp_declare_simd_list (ns
->omp_declare_simd
);
3713 gfc_free_charlen (ns
->cl_list
, NULL
);
3714 free_st_labels (ns
->st_labels
);
3716 free_entry_list (ns
->entries
);
3717 gfc_free_equiv (ns
->equiv
);
3718 gfc_free_equiv_lists (ns
->equiv_lists
);
3719 gfc_free_use_stmts (ns
->use_stmts
);
3721 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
3722 gfc_free_interface (ns
->op
[i
]);
3724 gfc_free_data (ns
->data
);
3728 /* Recursively free any contained namespaces. */
3733 gfc_free_namespace (q
);
3739 gfc_symbol_init_2 (void)
3742 gfc_current_ns
= gfc_get_namespace (NULL
, 0);
3747 gfc_symbol_done_2 (void)
3749 gfc_free_namespace (gfc_current_ns
);
3750 gfc_current_ns
= NULL
;
3751 gfc_free_dt_list ();
3753 enforce_single_undo_checkpoint ();
3754 free_undo_change_set_data (*latest_undo_chgset
);
3758 /* Count how many nodes a symtree has. */
3761 count_st_nodes (const gfc_symtree
*st
)
3767 nodes
= count_st_nodes (st
->left
);
3769 nodes
+= count_st_nodes (st
->right
);
3775 /* Convert symtree tree into symtree vector. */
3778 fill_st_vector (gfc_symtree
*st
, gfc_symtree
**st_vec
, unsigned node_cntr
)
3783 node_cntr
= fill_st_vector (st
->left
, st_vec
, node_cntr
);
3784 st_vec
[node_cntr
++] = st
;
3785 node_cntr
= fill_st_vector (st
->right
, st_vec
, node_cntr
);
3791 /* Traverse namespace. As the functions might modify the symtree, we store the
3792 symtree as a vector and operate on this vector. Note: We assume that
3793 sym_func or st_func never deletes nodes from the symtree - only adding is
3794 allowed. Additionally, newly added nodes are not traversed. */
3797 do_traverse_symtree (gfc_symtree
*st
, void (*st_func
) (gfc_symtree
*),
3798 void (*sym_func
) (gfc_symbol
*))
3800 gfc_symtree
**st_vec
;
3801 unsigned nodes
, i
, node_cntr
;
3803 gcc_assert ((st_func
&& !sym_func
) || (!st_func
&& sym_func
));
3804 nodes
= count_st_nodes (st
);
3805 st_vec
= XALLOCAVEC (gfc_symtree
*, nodes
);
3807 fill_st_vector (st
, st_vec
, node_cntr
);
3812 for (i
= 0; i
< nodes
; i
++)
3813 st_vec
[i
]->n
.sym
->mark
= 0;
3814 for (i
= 0; i
< nodes
; i
++)
3815 if (!st_vec
[i
]->n
.sym
->mark
)
3817 (*sym_func
) (st_vec
[i
]->n
.sym
);
3818 st_vec
[i
]->n
.sym
->mark
= 1;
3822 for (i
= 0; i
< nodes
; i
++)
3823 (*st_func
) (st_vec
[i
]);
3827 /* Recursively traverse the symtree nodes. */
3830 gfc_traverse_symtree (gfc_symtree
*st
, void (*st_func
) (gfc_symtree
*))
3832 do_traverse_symtree (st
, st_func
, NULL
);
3836 /* Call a given function for all symbols in the namespace. We take
3837 care that each gfc_symbol node is called exactly once. */
3840 gfc_traverse_ns (gfc_namespace
*ns
, void (*sym_func
) (gfc_symbol
*))
3842 do_traverse_symtree (ns
->sym_root
, NULL
, sym_func
);
3846 /* Return TRUE when name is the name of an intrinsic type. */
3849 gfc_is_intrinsic_typename (const char *name
)
3851 if (strcmp (name
, "integer") == 0
3852 || strcmp (name
, "real") == 0
3853 || strcmp (name
, "character") == 0
3854 || strcmp (name
, "logical") == 0
3855 || strcmp (name
, "complex") == 0
3856 || strcmp (name
, "doubleprecision") == 0
3857 || strcmp (name
, "doublecomplex") == 0)
3864 /* Return TRUE if the symbol is an automatic variable. */
3867 gfc_is_var_automatic (gfc_symbol
*sym
)
3869 /* Pointer and allocatable variables are never automatic. */
3870 if (sym
->attr
.pointer
|| sym
->attr
.allocatable
)
3872 /* Check for arrays with non-constant size. */
3873 if (sym
->attr
.dimension
&& sym
->as
3874 && !gfc_is_compile_time_shape (sym
->as
))
3876 /* Check for non-constant length character variables. */
3877 if (sym
->ts
.type
== BT_CHARACTER
3879 && !gfc_is_constant_expr (sym
->ts
.u
.cl
->length
))
3884 /* Given a symbol, mark it as SAVEd if it is allowed. */
3887 save_symbol (gfc_symbol
*sym
)
3890 if (sym
->attr
.use_assoc
)
3893 if (sym
->attr
.in_common
3896 || sym
->attr
.flavor
!= FL_VARIABLE
)
3898 /* Automatic objects are not saved. */
3899 if (gfc_is_var_automatic (sym
))
3901 gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
, &sym
->declared_at
);
3905 /* Mark those symbols which can be SAVEd as such. */
3908 gfc_save_all (gfc_namespace
*ns
)
3910 gfc_traverse_ns (ns
, save_symbol
);
3914 /* Make sure that no changes to symbols are pending. */
3917 gfc_enforce_clean_symbol_state(void)
3919 enforce_single_undo_checkpoint ();
3920 gcc_assert (latest_undo_chgset
->syms
.is_empty ());
3924 /************** Global symbol handling ************/
3927 /* Search a tree for the global symbol. */
3930 gfc_find_gsymbol (gfc_gsymbol
*symbol
, const char *name
)
3939 c
= strcmp (name
, symbol
->name
);
3943 symbol
= (c
< 0) ? symbol
->left
: symbol
->right
;
3950 /* Compare two global symbols. Used for managing the BB tree. */
3953 gsym_compare (void *_s1
, void *_s2
)
3955 gfc_gsymbol
*s1
, *s2
;
3957 s1
= (gfc_gsymbol
*) _s1
;
3958 s2
= (gfc_gsymbol
*) _s2
;
3959 return strcmp (s1
->name
, s2
->name
);
3963 /* Get a global symbol, creating it if it doesn't exist. */
3966 gfc_get_gsymbol (const char *name
)
3970 s
= gfc_find_gsymbol (gfc_gsym_root
, name
);
3974 s
= XCNEW (gfc_gsymbol
);
3975 s
->type
= GSYM_UNKNOWN
;
3976 s
->name
= gfc_get_string (name
);
3978 gfc_insert_bbt (&gfc_gsym_root
, s
, gsym_compare
);
3985 get_iso_c_binding_dt (int sym_id
)
3987 gfc_dt_list
*dt_list
;
3989 dt_list
= gfc_derived_types
;
3991 /* Loop through the derived types in the name list, searching for
3992 the desired symbol from iso_c_binding. Search the parent namespaces
3993 if necessary and requested to (parent_flag). */
3994 while (dt_list
!= NULL
)
3996 if (dt_list
->derived
->from_intmod
!= INTMOD_NONE
3997 && dt_list
->derived
->intmod_sym_id
== sym_id
)
3998 return dt_list
->derived
;
4000 dt_list
= dt_list
->next
;
4007 /* Verifies that the given derived type symbol, derived_sym, is interoperable
4008 with C. This is necessary for any derived type that is BIND(C) and for
4009 derived types that are parameters to functions that are BIND(C). All
4010 fields of the derived type are required to be interoperable, and are tested
4011 for such. If an error occurs, the errors are reported here, allowing for
4012 multiple errors to be handled for a single derived type. */
4015 verify_bind_c_derived_type (gfc_symbol
*derived_sym
)
4017 gfc_component
*curr_comp
= NULL
;
4018 bool is_c_interop
= false;
4021 if (derived_sym
== NULL
)
4022 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
4023 "unexpectedly NULL");
4025 /* If we've already looked at this derived symbol, do not look at it again
4026 so we don't repeat warnings/errors. */
4027 if (derived_sym
->ts
.is_c_interop
)
4030 /* The derived type must have the BIND attribute to be interoperable
4031 J3/04-007, Section 15.2.3. */
4032 if (derived_sym
->attr
.is_bind_c
!= 1)
4034 derived_sym
->ts
.is_c_interop
= 0;
4035 gfc_error_now ("Derived type %qs declared at %L must have the BIND "
4036 "attribute to be C interoperable", derived_sym
->name
,
4037 &(derived_sym
->declared_at
));
4041 curr_comp
= derived_sym
->components
;
4043 /* Fortran 2003 allows an empty derived type. C99 appears to disallow an
4044 empty struct. Section 15.2 in Fortran 2003 states: "The following
4045 subclauses define the conditions under which a Fortran entity is
4046 interoperable. If a Fortran entity is interoperable, an equivalent
4047 entity may be defined by means of C and the Fortran entity is said
4048 to be interoperable with the C entity. There does not have to be such
4049 an interoperating C entity."
4051 if (curr_comp
== NULL
)
4053 gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L is empty, "
4054 "and may be inaccessible by the C companion processor",
4055 derived_sym
->name
, &(derived_sym
->declared_at
));
4056 derived_sym
->ts
.is_c_interop
= 1;
4057 derived_sym
->attr
.is_bind_c
= 1;
4062 /* Initialize the derived type as being C interoperable.
4063 If we find an error in the components, this will be set false. */
4064 derived_sym
->ts
.is_c_interop
= 1;
4066 /* Loop through the list of components to verify that the kind of
4067 each is a C interoperable type. */
4070 /* The components cannot be pointers (fortran sense).
4071 J3/04-007, Section 15.2.3, C1505. */
4072 if (curr_comp
->attr
.pointer
!= 0)
4074 gfc_error ("Component %qs at %L cannot have the "
4075 "POINTER attribute because it is a member "
4076 "of the BIND(C) derived type %qs at %L",
4077 curr_comp
->name
, &(curr_comp
->loc
),
4078 derived_sym
->name
, &(derived_sym
->declared_at
));
4082 if (curr_comp
->attr
.proc_pointer
!= 0)
4084 gfc_error ("Procedure pointer component %qs at %L cannot be a member"
4085 " of the BIND(C) derived type %qs at %L", curr_comp
->name
,
4086 &curr_comp
->loc
, derived_sym
->name
,
4087 &derived_sym
->declared_at
);
4091 /* The components cannot be allocatable.
4092 J3/04-007, Section 15.2.3, C1505. */
4093 if (curr_comp
->attr
.allocatable
!= 0)
4095 gfc_error ("Component %qs at %L cannot have the "
4096 "ALLOCATABLE attribute because it is a member "
4097 "of the BIND(C) derived type %qs at %L",
4098 curr_comp
->name
, &(curr_comp
->loc
),
4099 derived_sym
->name
, &(derived_sym
->declared_at
));
4103 /* BIND(C) derived types must have interoperable components. */
4104 if (curr_comp
->ts
.type
== BT_DERIVED
4105 && curr_comp
->ts
.u
.derived
->ts
.is_iso_c
!= 1
4106 && curr_comp
->ts
.u
.derived
!= derived_sym
)
4108 /* This should be allowed; the draft says a derived-type can not
4109 have type parameters if it is has the BIND attribute. Type
4110 parameters seem to be for making parameterized derived types.
4111 There's no need to verify the type if it is c_ptr/c_funptr. */
4112 retval
= verify_bind_c_derived_type (curr_comp
->ts
.u
.derived
);
4116 /* Grab the typespec for the given component and test the kind. */
4117 is_c_interop
= gfc_verify_c_interop (&(curr_comp
->ts
));
4121 /* Report warning and continue since not fatal. The
4122 draft does specify a constraint that requires all fields
4123 to interoperate, but if the user says real(4), etc., it
4124 may interoperate with *something* in C, but the compiler
4125 most likely won't know exactly what. Further, it may not
4126 interoperate with the same data type(s) in C if the user
4127 recompiles with different flags (e.g., -m32 and -m64 on
4128 x86_64 and using integer(4) to claim interop with a
4130 if (derived_sym
->attr
.is_bind_c
== 1 && warn_c_binding_type
)
4131 /* If the derived type is bind(c), all fields must be
4133 gfc_warning (OPT_Wc_binding_type
,
4134 "Component %qs in derived type %qs at %L "
4135 "may not be C interoperable, even though "
4136 "derived type %qs is BIND(C)",
4137 curr_comp
->name
, derived_sym
->name
,
4138 &(curr_comp
->loc
), derived_sym
->name
);
4139 else if (warn_c_binding_type
)
4140 /* If derived type is param to bind(c) routine, or to one
4141 of the iso_c_binding procs, it must be interoperable, so
4142 all fields must interop too. */
4143 gfc_warning (OPT_Wc_binding_type
,
4144 "Component %qs in derived type %qs at %L "
4145 "may not be C interoperable",
4146 curr_comp
->name
, derived_sym
->name
,
4151 curr_comp
= curr_comp
->next
;
4152 } while (curr_comp
!= NULL
);
4155 /* Make sure we don't have conflicts with the attributes. */
4156 if (derived_sym
->attr
.access
== ACCESS_PRIVATE
)
4158 gfc_error ("Derived type %qs at %L cannot be declared with both "
4159 "PRIVATE and BIND(C) attributes", derived_sym
->name
,
4160 &(derived_sym
->declared_at
));
4164 if (derived_sym
->attr
.sequence
!= 0)
4166 gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
4167 "attribute because it is BIND(C)", derived_sym
->name
,
4168 &(derived_sym
->declared_at
));
4172 /* Mark the derived type as not being C interoperable if we found an
4173 error. If there were only warnings, proceed with the assumption
4174 it's interoperable. */
4176 derived_sym
->ts
.is_c_interop
= 0;
4182 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
4185 gen_special_c_interop_ptr (gfc_symbol
*tmp_sym
, gfc_symtree
*dt_symtree
)
4189 gcc_assert (tmp_sym
&& dt_symtree
&& dt_symtree
->n
.sym
);
4190 dt_symtree
->n
.sym
->attr
.referenced
= 1;
4192 tmp_sym
->attr
.is_c_interop
= 1;
4193 tmp_sym
->attr
.is_bind_c
= 1;
4194 tmp_sym
->ts
.is_c_interop
= 1;
4195 tmp_sym
->ts
.is_iso_c
= 1;
4196 tmp_sym
->ts
.type
= BT_DERIVED
;
4197 tmp_sym
->ts
.f90_type
= BT_VOID
;
4198 tmp_sym
->attr
.flavor
= FL_PARAMETER
;
4199 tmp_sym
->ts
.u
.derived
= dt_symtree
->n
.sym
;
4201 /* Set the c_address field of c_null_ptr and c_null_funptr to
4202 the value of NULL. */
4203 tmp_sym
->value
= gfc_get_expr ();
4204 tmp_sym
->value
->expr_type
= EXPR_STRUCTURE
;
4205 tmp_sym
->value
->ts
.type
= BT_DERIVED
;
4206 tmp_sym
->value
->ts
.f90_type
= BT_VOID
;
4207 tmp_sym
->value
->ts
.u
.derived
= tmp_sym
->ts
.u
.derived
;
4208 gfc_constructor_append_expr (&tmp_sym
->value
->value
.constructor
, NULL
, NULL
);
4209 c
= gfc_constructor_first (tmp_sym
->value
->value
.constructor
);
4210 c
->expr
= gfc_get_int_expr (gfc_index_integer_kind
, NULL
, 0);
4211 c
->expr
->ts
.is_iso_c
= 1;
4217 /* Add a formal argument, gfc_formal_arglist, to the
4218 end of the given list of arguments. Set the reference to the
4219 provided symbol, param_sym, in the argument. */
4222 add_formal_arg (gfc_formal_arglist
**head
,
4223 gfc_formal_arglist
**tail
,
4224 gfc_formal_arglist
*formal_arg
,
4225 gfc_symbol
*param_sym
)
4227 /* Put in list, either as first arg or at the tail (curr arg). */
4229 *head
= *tail
= formal_arg
;
4232 (*tail
)->next
= formal_arg
;
4233 (*tail
) = formal_arg
;
4236 (*tail
)->sym
= param_sym
;
4237 (*tail
)->next
= NULL
;
4243 /* Add a procedure interface to the given symbol (i.e., store a
4244 reference to the list of formal arguments). */
4247 add_proc_interface (gfc_symbol
*sym
, ifsrc source
, gfc_formal_arglist
*formal
)
4250 sym
->formal
= formal
;
4251 sym
->attr
.if_source
= source
;
4255 /* Copy the formal args from an existing symbol, src, into a new
4256 symbol, dest. New formal args are created, and the description of
4257 each arg is set according to the existing ones. This function is
4258 used when creating procedure declaration variables from a procedure
4259 declaration statement (see match_proc_decl()) to create the formal
4260 args based on the args of a given named interface.
4262 When an actual argument list is provided, skip the absent arguments.
4263 To be used together with gfc_se->ignore_optional. */
4266 gfc_copy_formal_args_intr (gfc_symbol
*dest
, gfc_intrinsic_sym
*src
,
4267 gfc_actual_arglist
*actual
)
4269 gfc_formal_arglist
*head
= NULL
;
4270 gfc_formal_arglist
*tail
= NULL
;
4271 gfc_formal_arglist
*formal_arg
= NULL
;
4272 gfc_intrinsic_arg
*curr_arg
= NULL
;
4273 gfc_formal_arglist
*formal_prev
= NULL
;
4274 gfc_actual_arglist
*act_arg
= actual
;
4275 /* Save current namespace so we can change it for formal args. */
4276 gfc_namespace
*parent_ns
= gfc_current_ns
;
4278 /* Create a new namespace, which will be the formal ns (namespace
4279 of the formal args). */
4280 gfc_current_ns
= gfc_get_namespace (parent_ns
, 0);
4281 gfc_current_ns
->proc_name
= dest
;
4283 for (curr_arg
= src
->formal
; curr_arg
; curr_arg
= curr_arg
->next
)
4285 /* Skip absent arguments. */
4288 gcc_assert (act_arg
!= NULL
);
4289 if (act_arg
->expr
== NULL
)
4291 act_arg
= act_arg
->next
;
4294 act_arg
= act_arg
->next
;
4296 formal_arg
= gfc_get_formal_arglist ();
4297 gfc_get_symbol (curr_arg
->name
, gfc_current_ns
, &(formal_arg
->sym
));
4299 /* May need to copy more info for the symbol. */
4300 formal_arg
->sym
->ts
= curr_arg
->ts
;
4301 formal_arg
->sym
->attr
.optional
= curr_arg
->optional
;
4302 formal_arg
->sym
->attr
.value
= curr_arg
->value
;
4303 formal_arg
->sym
->attr
.intent
= curr_arg
->intent
;
4304 formal_arg
->sym
->attr
.flavor
= FL_VARIABLE
;
4305 formal_arg
->sym
->attr
.dummy
= 1;
4307 if (formal_arg
->sym
->ts
.type
== BT_CHARACTER
)
4308 formal_arg
->sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4310 /* If this isn't the first arg, set up the next ptr. For the
4311 last arg built, the formal_arg->next will never get set to
4312 anything other than NULL. */
4313 if (formal_prev
!= NULL
)
4314 formal_prev
->next
= formal_arg
;
4316 formal_arg
->next
= NULL
;
4318 formal_prev
= formal_arg
;
4320 /* Add arg to list of formal args. */
4321 add_formal_arg (&head
, &tail
, formal_arg
, formal_arg
->sym
);
4323 /* Validate changes. */
4324 gfc_commit_symbol (formal_arg
->sym
);
4327 /* Add the interface to the symbol. */
4328 add_proc_interface (dest
, IFSRC_DECL
, head
);
4330 /* Store the formal namespace information. */
4331 if (dest
->formal
!= NULL
)
4332 /* The current ns should be that for the dest proc. */
4333 dest
->formal_ns
= gfc_current_ns
;
4334 /* Restore the current namespace to what it was on entry. */
4335 gfc_current_ns
= parent_ns
;
4340 std_for_isocbinding_symbol (int id
)
4344 #define NAMED_INTCST(a,b,c,d) \
4347 #include "iso-c-binding.def"
4350 #define NAMED_FUNCTION(a,b,c,d) \
4353 #define NAMED_SUBROUTINE(a,b,c,d) \
4356 #include "iso-c-binding.def"
4357 #undef NAMED_FUNCTION
4358 #undef NAMED_SUBROUTINE
4361 return GFC_STD_F2003
;
4365 /* Generate the given set of C interoperable kind objects, or all
4366 interoperable kinds. This function will only be given kind objects
4367 for valid iso_c_binding defined types because this is verified when
4368 the 'use' statement is parsed. If the user gives an 'only' clause,
4369 the specific kinds are looked up; if they don't exist, an error is
4370 reported. If the user does not give an 'only' clause, all
4371 iso_c_binding symbols are generated. If a list of specific kinds
4372 is given, it must have a NULL in the first empty spot to mark the
4373 end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
4374 point to the symtree for c_(fun)ptr. */
4377 generate_isocbinding_symbol (const char *mod_name
, iso_c_binding_symbol s
,
4378 const char *local_name
, gfc_symtree
*dt_symtree
,
4381 const char *const name
= (local_name
&& local_name
[0])
4382 ? local_name
: c_interop_kinds_table
[s
].name
;
4383 gfc_symtree
*tmp_symtree
;
4384 gfc_symbol
*tmp_sym
= NULL
;
4387 if (gfc_notification_std (std_for_isocbinding_symbol (s
)) == ERROR
)
4390 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
4392 && (!tmp_symtree
|| !tmp_symtree
->n
.sym
4393 || tmp_symtree
->n
.sym
->from_intmod
!= INTMOD_ISO_C_BINDING
4394 || tmp_symtree
->n
.sym
->intmod_sym_id
!= s
))
4397 /* Already exists in this scope so don't re-add it. */
4398 if (tmp_symtree
!= NULL
&& (tmp_sym
= tmp_symtree
->n
.sym
) != NULL
4399 && (!tmp_sym
->attr
.generic
4400 || (tmp_sym
= gfc_find_dt_in_generic (tmp_sym
)) != NULL
)
4401 && tmp_sym
->from_intmod
== INTMOD_ISO_C_BINDING
)
4403 if (tmp_sym
->attr
.flavor
== FL_DERIVED
4404 && !get_iso_c_binding_dt (tmp_sym
->intmod_sym_id
))
4406 gfc_dt_list
*dt_list
;
4407 dt_list
= gfc_get_dt_list ();
4408 dt_list
->derived
= tmp_sym
;
4409 dt_list
->next
= gfc_derived_types
;
4410 gfc_derived_types
= dt_list
;
4416 /* Create the sym tree in the current ns. */
4419 tmp_symtree
= gfc_get_unique_symtree (gfc_current_ns
);
4420 tmp_sym
= gfc_new_symbol (name
, gfc_current_ns
);
4422 /* Add to the list of tentative symbols. */
4423 latest_undo_chgset
->syms
.safe_push (tmp_sym
);
4424 tmp_sym
->old_symbol
= NULL
;
4426 tmp_sym
->gfc_new
= 1;
4428 tmp_symtree
->n
.sym
= tmp_sym
;
4433 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
4434 gcc_assert (tmp_symtree
);
4435 tmp_sym
= tmp_symtree
->n
.sym
;
4438 /* Say what module this symbol belongs to. */
4439 tmp_sym
->module
= gfc_get_string (mod_name
);
4440 tmp_sym
->from_intmod
= INTMOD_ISO_C_BINDING
;
4441 tmp_sym
->intmod_sym_id
= s
;
4442 tmp_sym
->attr
.is_iso_c
= 1;
4443 tmp_sym
->attr
.use_assoc
= 1;
4445 gcc_assert (dt_symtree
== NULL
|| s
== ISOCBINDING_NULL_FUNPTR
4446 || s
== ISOCBINDING_NULL_PTR
);
4451 #define NAMED_INTCST(a,b,c,d) case a :
4452 #define NAMED_REALCST(a,b,c,d) case a :
4453 #define NAMED_CMPXCST(a,b,c,d) case a :
4454 #define NAMED_LOGCST(a,b,c) case a :
4455 #define NAMED_CHARKNDCST(a,b,c) case a :
4456 #include "iso-c-binding.def"
4458 tmp_sym
->value
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
,
4459 c_interop_kinds_table
[s
].value
);
4461 /* Initialize an integer constant expression node. */
4462 tmp_sym
->attr
.flavor
= FL_PARAMETER
;
4463 tmp_sym
->ts
.type
= BT_INTEGER
;
4464 tmp_sym
->ts
.kind
= gfc_default_integer_kind
;
4466 /* Mark this type as a C interoperable one. */
4467 tmp_sym
->ts
.is_c_interop
= 1;
4468 tmp_sym
->ts
.is_iso_c
= 1;
4469 tmp_sym
->value
->ts
.is_c_interop
= 1;
4470 tmp_sym
->value
->ts
.is_iso_c
= 1;
4471 tmp_sym
->attr
.is_c_interop
= 1;
4473 /* Tell what f90 type this c interop kind is valid. */
4474 tmp_sym
->ts
.f90_type
= c_interop_kinds_table
[s
].f90_type
;
4479 #define NAMED_CHARCST(a,b,c) case a :
4480 #include "iso-c-binding.def"
4482 /* Initialize an integer constant expression node for the
4483 length of the character. */
4484 tmp_sym
->value
= gfc_get_character_expr (gfc_default_character_kind
,
4485 &gfc_current_locus
, NULL
, 1);
4486 tmp_sym
->value
->ts
.is_c_interop
= 1;
4487 tmp_sym
->value
->ts
.is_iso_c
= 1;
4488 tmp_sym
->value
->value
.character
.length
= 1;
4489 tmp_sym
->value
->value
.character
.string
[0]
4490 = (gfc_char_t
) c_interop_kinds_table
[s
].value
;
4491 tmp_sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
4492 tmp_sym
->ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
4495 /* May not need this in both attr and ts, but do need in
4496 attr for writing module file. */
4497 tmp_sym
->attr
.is_c_interop
= 1;
4499 tmp_sym
->attr
.flavor
= FL_PARAMETER
;
4500 tmp_sym
->ts
.type
= BT_CHARACTER
;
4502 /* Need to set it to the C_CHAR kind. */
4503 tmp_sym
->ts
.kind
= gfc_default_character_kind
;
4505 /* Mark this type as a C interoperable one. */
4506 tmp_sym
->ts
.is_c_interop
= 1;
4507 tmp_sym
->ts
.is_iso_c
= 1;
4509 /* Tell what f90 type this c interop kind is valid. */
4510 tmp_sym
->ts
.f90_type
= BT_CHARACTER
;
4514 case ISOCBINDING_PTR
:
4515 case ISOCBINDING_FUNPTR
:
4518 gfc_dt_list
**dt_list_ptr
= NULL
;
4519 gfc_component
*tmp_comp
= NULL
;
4521 /* Generate real derived type. */
4526 const char *hidden_name
;
4527 gfc_interface
*intr
, *head
;
4529 hidden_name
= gfc_get_string ("%c%s",
4530 (char) TOUPPER ((unsigned char)
4533 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
,
4535 gcc_assert (tmp_symtree
== NULL
);
4536 gfc_get_sym_tree (hidden_name
, gfc_current_ns
, &tmp_symtree
, false);
4537 dt_sym
= tmp_symtree
->n
.sym
;
4538 dt_sym
->name
= gfc_get_string (s
== ISOCBINDING_PTR
4539 ? "c_ptr" : "c_funptr");
4541 /* Generate an artificial generic function. */
4542 head
= tmp_sym
->generic
;
4543 intr
= gfc_get_interface ();
4545 intr
->where
= gfc_current_locus
;
4547 tmp_sym
->generic
= intr
;
4549 if (!tmp_sym
->attr
.generic
4550 && !gfc_add_generic (&tmp_sym
->attr
, tmp_sym
->name
, NULL
))
4553 if (!tmp_sym
->attr
.function
4554 && !gfc_add_function (&tmp_sym
->attr
, tmp_sym
->name
, NULL
))
4558 /* Say what module this symbol belongs to. */
4559 dt_sym
->module
= gfc_get_string (mod_name
);
4560 dt_sym
->from_intmod
= INTMOD_ISO_C_BINDING
;
4561 dt_sym
->intmod_sym_id
= s
;
4562 dt_sym
->attr
.use_assoc
= 1;
4564 /* Initialize an integer constant expression node. */
4565 dt_sym
->attr
.flavor
= FL_DERIVED
;
4566 dt_sym
->ts
.is_c_interop
= 1;
4567 dt_sym
->attr
.is_c_interop
= 1;
4568 dt_sym
->attr
.private_comp
= 1;
4569 dt_sym
->component_access
= ACCESS_PRIVATE
;
4570 dt_sym
->ts
.is_iso_c
= 1;
4571 dt_sym
->ts
.type
= BT_DERIVED
;
4572 dt_sym
->ts
.f90_type
= BT_VOID
;
4574 /* A derived type must have the bind attribute to be
4575 interoperable (J3/04-007, Section 15.2.3), even though
4576 the binding label is not used. */
4577 dt_sym
->attr
.is_bind_c
= 1;
4579 dt_sym
->attr
.referenced
= 1;
4580 dt_sym
->ts
.u
.derived
= dt_sym
;
4582 /* Add the symbol created for the derived type to the current ns. */
4583 dt_list_ptr
= &(gfc_derived_types
);
4584 while (*dt_list_ptr
!= NULL
&& (*dt_list_ptr
)->next
!= NULL
)
4585 dt_list_ptr
= &((*dt_list_ptr
)->next
);
4587 /* There is already at least one derived type in the list, so append
4588 the one we're currently building for c_ptr or c_funptr. */
4589 if (*dt_list_ptr
!= NULL
)
4590 dt_list_ptr
= &((*dt_list_ptr
)->next
);
4591 (*dt_list_ptr
) = gfc_get_dt_list ();
4592 (*dt_list_ptr
)->derived
= dt_sym
;
4593 (*dt_list_ptr
)->next
= NULL
;
4595 gfc_add_component (dt_sym
, "c_address", &tmp_comp
);
4596 if (tmp_comp
== NULL
)
4599 tmp_comp
->ts
.type
= BT_INTEGER
;
4601 /* Set this because the module will need to read/write this field. */
4602 tmp_comp
->ts
.f90_type
= BT_INTEGER
;
4604 /* The kinds for c_ptr and c_funptr are the same. */
4605 index
= get_c_kind ("c_ptr", c_interop_kinds_table
);
4606 tmp_comp
->ts
.kind
= c_interop_kinds_table
[index
].value
;
4607 tmp_comp
->attr
.access
= ACCESS_PRIVATE
;
4609 /* Mark the component as C interoperable. */
4610 tmp_comp
->ts
.is_c_interop
= 1;
4615 case ISOCBINDING_NULL_PTR
:
4616 case ISOCBINDING_NULL_FUNPTR
:
4617 gen_special_c_interop_ptr (tmp_sym
, dt_symtree
);
4623 gfc_commit_symbol (tmp_sym
);
4628 /* Check that a symbol is already typed. If strict is not set, an untyped
4629 symbol is acceptable for non-standard-conforming mode. */
4632 gfc_check_symbol_typed (gfc_symbol
* sym
, gfc_namespace
* ns
,
4633 bool strict
, locus where
)
4637 if (gfc_matching_prefix
)
4640 /* Check for the type and try to give it an implicit one. */
4641 if (sym
->ts
.type
== BT_UNKNOWN
4642 && !gfc_set_default_type (sym
, 0, ns
))
4646 gfc_error ("Symbol %qs is used before it is typed at %L",
4651 if (!gfc_notify_std (GFC_STD_GNU
, "Symbol %qs is used before"
4652 " it is typed at %L", sym
->name
, &where
))
4656 /* Everything is ok. */
4661 /* Construct a typebound-procedure structure. Those are stored in a tentative
4662 list and marked `error' until symbols are committed. */
4665 gfc_get_typebound_proc (gfc_typebound_proc
*tb0
)
4667 gfc_typebound_proc
*result
;
4669 result
= XCNEW (gfc_typebound_proc
);
4674 latest_undo_chgset
->tbps
.safe_push (result
);
4680 /* Get the super-type of a given derived type. */
4683 gfc_get_derived_super_type (gfc_symbol
* derived
)
4685 gcc_assert (derived
);
4687 if (derived
->attr
.generic
)
4688 derived
= gfc_find_dt_in_generic (derived
);
4690 if (!derived
->attr
.extension
)
4693 gcc_assert (derived
->components
);
4694 gcc_assert (derived
->components
->ts
.type
== BT_DERIVED
);
4695 gcc_assert (derived
->components
->ts
.u
.derived
);
4697 if (derived
->components
->ts
.u
.derived
->attr
.generic
)
4698 return gfc_find_dt_in_generic (derived
->components
->ts
.u
.derived
);
4700 return derived
->components
->ts
.u
.derived
;
4704 /* Get the ultimate super-type of a given derived type. */
4707 gfc_get_ultimate_derived_super_type (gfc_symbol
* derived
)
4709 if (!derived
->attr
.extension
)
4712 derived
= gfc_get_derived_super_type (derived
);
4714 if (derived
->attr
.extension
)
4715 return gfc_get_ultimate_derived_super_type (derived
);
4721 /* Check if a derived type t2 is an extension of (or equal to) a type t1. */
4724 gfc_type_is_extension_of (gfc_symbol
*t1
, gfc_symbol
*t2
)
4726 while (!gfc_compare_derived_types (t1
, t2
) && t2
->attr
.extension
)
4727 t2
= gfc_get_derived_super_type (t2
);
4728 return gfc_compare_derived_types (t1
, t2
);
4732 /* Check if two typespecs are type compatible (F03:5.1.1.2):
4733 If ts1 is nonpolymorphic, ts2 must be the same type.
4734 If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1. */
4737 gfc_type_compatible (gfc_typespec
*ts1
, gfc_typespec
*ts2
)
4739 bool is_class1
= (ts1
->type
== BT_CLASS
);
4740 bool is_class2
= (ts2
->type
== BT_CLASS
);
4741 bool is_derived1
= (ts1
->type
== BT_DERIVED
);
4742 bool is_derived2
= (ts2
->type
== BT_DERIVED
);
4745 && ts1
->u
.derived
->components
4746 && ((ts1
->u
.derived
->attr
.is_class
4747 && ts1
->u
.derived
->components
->ts
.u
.derived
->attr
4748 .unlimited_polymorphic
)
4749 || ts1
->u
.derived
->attr
.unlimited_polymorphic
))
4752 if (!is_derived1
&& !is_derived2
&& !is_class1
&& !is_class2
)
4753 return (ts1
->type
== ts2
->type
);
4755 if (is_derived1
&& is_derived2
)
4756 return gfc_compare_derived_types (ts1
->u
.derived
, ts2
->u
.derived
);
4758 if (is_derived1
&& is_class2
)
4759 return gfc_compare_derived_types (ts1
->u
.derived
,
4760 ts2
->u
.derived
->attr
.is_class
?
4761 ts2
->u
.derived
->components
->ts
.u
.derived
4763 if (is_class1
&& is_derived2
)
4764 return gfc_type_is_extension_of (ts1
->u
.derived
->attr
.is_class
?
4765 ts1
->u
.derived
->components
->ts
.u
.derived
4768 else if (is_class1
&& is_class2
)
4769 return gfc_type_is_extension_of (ts1
->u
.derived
->attr
.is_class
?
4770 ts1
->u
.derived
->components
->ts
.u
.derived
4772 ts2
->u
.derived
->attr
.is_class
?
4773 ts2
->u
.derived
->components
->ts
.u
.derived
4780 /* Find the parent-namespace of the current function. If we're inside
4781 BLOCK constructs, it may not be the current one. */
4784 gfc_find_proc_namespace (gfc_namespace
* ns
)
4786 while (ns
->construct_entities
)
4796 /* Check if an associate-variable should be translated as an `implicit' pointer
4797 internally (if it is associated to a variable and not an array with
4801 gfc_is_associate_pointer (gfc_symbol
* sym
)
4806 if (sym
->ts
.type
== BT_CLASS
)
4809 if (!sym
->assoc
->variable
)
4812 if (sym
->attr
.dimension
&& sym
->as
->type
!= AS_EXPLICIT
)
4820 gfc_find_dt_in_generic (gfc_symbol
*sym
)
4822 gfc_interface
*intr
= NULL
;
4824 if (!sym
|| sym
->attr
.flavor
== FL_DERIVED
)
4827 if (sym
->attr
.generic
)
4828 for (intr
= sym
->generic
; intr
; intr
= intr
->next
)
4829 if (intr
->sym
->attr
.flavor
== FL_DERIVED
)
4831 return intr
? intr
->sym
: NULL
;
4835 /* Get the dummy arguments from a procedure symbol. If it has been declared
4836 via a PROCEDURE statement with a named interface, ts.interface will be set
4837 and the arguments need to be taken from there. */
4839 gfc_formal_arglist
*
4840 gfc_sym_get_dummy_args (gfc_symbol
*sym
)
4842 gfc_formal_arglist
*dummies
;
4844 dummies
= sym
->formal
;
4845 if (dummies
== NULL
&& sym
->ts
.interface
!= NULL
)
4846 dummies
= sym
->ts
.interface
->formal
;