Fix ChangeLog
[official-gcc.git] / gcc / fortran / symbol.c
blobe4e43244d59ec4e4a8fddb9abd29de9037b42c95
1 /* Maintain binary trees of symbols.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "parse.h"
30 /* Strings for all symbol attributes. We use these for dumping the
31 parse tree, in error messages, and also when reading and writing
32 modules. */
34 const mstring flavors[] =
36 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
37 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
38 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
39 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
40 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
41 minit (NULL, -1)
44 const mstring procedures[] =
46 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
47 minit ("MODULE-PROC", PROC_MODULE),
48 minit ("INTERNAL-PROC", PROC_INTERNAL),
49 minit ("DUMMY-PROC", PROC_DUMMY),
50 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
51 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
52 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
53 minit (NULL, -1)
56 const mstring intents[] =
58 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
59 minit ("IN", INTENT_IN),
60 minit ("OUT", INTENT_OUT),
61 minit ("INOUT", INTENT_INOUT),
62 minit (NULL, -1)
65 const mstring access_types[] =
67 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
68 minit ("PUBLIC", ACCESS_PUBLIC),
69 minit ("PRIVATE", ACCESS_PRIVATE),
70 minit (NULL, -1)
73 const mstring ifsrc_types[] =
75 minit ("UNKNOWN", IFSRC_UNKNOWN),
76 minit ("DECL", IFSRC_DECL),
77 minit ("BODY", IFSRC_IFBODY),
78 minit ("USAGE", IFSRC_USAGE)
81 const mstring save_status[] =
83 minit ("UNKNOWN", SAVE_NONE),
84 minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
85 minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
88 /* This is to make sure the backend generates setup code in the correct
89 order. */
91 static int next_dummy_order = 1;
94 gfc_namespace *gfc_current_ns;
96 gfc_gsymbol *gfc_gsym_root = NULL;
98 static gfc_symbol *changed_syms = NULL;
100 gfc_dt_list *gfc_derived_types;
103 /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
105 /* The following static variable indicates whether a particular element has
106 been explicitly set or not. */
108 static int new_flag[GFC_LETTERS];
111 /* Handle a correctly parsed IMPLICIT NONE. */
113 void
114 gfc_set_implicit_none (void)
116 int i;
118 if (gfc_current_ns->seen_implicit_none)
120 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
121 return;
124 gfc_current_ns->seen_implicit_none = 1;
126 for (i = 0; i < GFC_LETTERS; i++)
128 gfc_clear_ts (&gfc_current_ns->default_type[i]);
129 gfc_current_ns->set_flag[i] = 1;
134 /* Reset the implicit range flags. */
136 void
137 gfc_clear_new_implicit (void)
139 int i;
141 for (i = 0; i < GFC_LETTERS; i++)
142 new_flag[i] = 0;
146 /* Prepare for a new implicit range. Sets flags in new_flag[]. */
149 gfc_add_new_implicit_range (int c1, int c2)
151 int i;
153 c1 -= 'a';
154 c2 -= 'a';
156 for (i = c1; i <= c2; i++)
158 if (new_flag[i])
160 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
161 i + 'A');
162 return FAILURE;
165 new_flag[i] = 1;
168 return SUCCESS;
172 /* Add a matched implicit range for gfc_set_implicit(). Check if merging
173 the new implicit types back into the existing types will work. */
176 gfc_merge_new_implicit (gfc_typespec *ts)
178 int i;
180 if (gfc_current_ns->seen_implicit_none)
182 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
183 return FAILURE;
186 for (i = 0; i < GFC_LETTERS; i++)
188 if (new_flag[i])
191 if (gfc_current_ns->set_flag[i])
193 gfc_error ("Letter %c already has an IMPLICIT type at %C",
194 i + 'A');
195 return FAILURE;
197 gfc_current_ns->default_type[i] = *ts;
198 gfc_current_ns->set_flag[i] = 1;
201 return SUCCESS;
205 /* Given a symbol, return a pointer to the typespec for its default type. */
207 gfc_typespec *
208 gfc_get_default_type (gfc_symbol *sym, gfc_namespace *ns)
210 char letter;
212 letter = sym->name[0];
214 if (gfc_option.flag_allow_leading_underscore && letter == '_')
215 gfc_internal_error ("Option -fallow-leading-underscore is for use only by "
216 "gfortran developers, and should not be used for "
217 "implicitly typed variables");
219 if (letter < 'a' || letter > 'z')
220 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
222 if (ns == NULL)
223 ns = gfc_current_ns;
225 return &ns->default_type[letter - 'a'];
229 /* Given a pointer to a symbol, set its type according to the first
230 letter of its name. Fails if the letter in question has no default
231 type. */
234 gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
236 gfc_typespec *ts;
238 if (sym->ts.type != BT_UNKNOWN)
239 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
241 ts = gfc_get_default_type (sym, ns);
243 if (ts->type == BT_UNKNOWN)
245 if (error_flag && !sym->attr.untyped)
247 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
248 sym->name, &sym->declared_at);
249 sym->attr.untyped = 1; /* Ensure we only give an error once. */
252 return FAILURE;
255 sym->ts = *ts;
256 sym->attr.implicit_type = 1;
258 if (sym->attr.is_bind_c == 1)
260 /* BIND(C) variables should not be implicitly declared. */
261 gfc_warning_now ("Implicitly declared BIND(C) variable '%s' at %L may "
262 "not be C interoperable", sym->name, &sym->declared_at);
263 sym->ts.f90_type = sym->ts.type;
266 if (sym->attr.dummy != 0)
268 if (sym->ns->proc_name != NULL
269 && (sym->ns->proc_name->attr.subroutine != 0
270 || sym->ns->proc_name->attr.function != 0)
271 && sym->ns->proc_name->attr.is_bind_c != 0)
273 /* Dummy args to a BIND(C) routine may not be interoperable if
274 they are implicitly typed. */
275 gfc_warning_now ("Implicity declared variable '%s' at %L may not "
276 "be C interoperable but it is a dummy argument to "
277 "the BIND(C) procedure '%s' at %L", sym->name,
278 &(sym->declared_at), sym->ns->proc_name->name,
279 &(sym->ns->proc_name->declared_at));
280 sym->ts.f90_type = sym->ts.type;
284 return SUCCESS;
288 /* This function is called from parse.c(parse_progunit) to check the
289 type of the function is not implicitly typed in the host namespace
290 and to implicitly type the function result, if necessary. */
292 void
293 gfc_check_function_type (gfc_namespace *ns)
295 gfc_symbol *proc = ns->proc_name;
297 if (!proc->attr.contained || proc->result->attr.implicit_type)
298 return;
300 if (proc->result->ts.type == BT_UNKNOWN)
302 if (gfc_set_default_type (proc->result, 0, gfc_current_ns)
303 == SUCCESS)
305 if (proc->result != proc)
307 proc->ts = proc->result->ts;
308 proc->as = gfc_copy_array_spec (proc->result->as);
309 proc->attr.dimension = proc->result->attr.dimension;
310 proc->attr.pointer = proc->result->attr.pointer;
311 proc->attr.allocatable = proc->result->attr.allocatable;
314 else
316 gfc_error ("Function result '%s' at %L has no IMPLICIT type",
317 proc->result->name, &proc->result->declared_at);
318 proc->result->attr.untyped = 1;
324 /******************** Symbol attribute stuff *********************/
326 /* This is a generic conflict-checker. We do this to avoid having a
327 single conflict in two places. */
329 #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
330 #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
331 #define conf_std(a, b, std) if (attr->a && attr->b)\
333 a1 = a;\
334 a2 = b;\
335 standard = std;\
336 goto conflict_std;\
339 static try
340 check_conflict (symbol_attribute *attr, const char *name, locus *where)
342 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
343 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
344 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
345 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
346 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
347 *private = "PRIVATE", *recursive = "RECURSIVE",
348 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
349 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
350 *function = "FUNCTION", *subroutine = "SUBROUTINE",
351 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
352 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
353 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
354 *volatile_ = "VOLATILE", *protected = "PROTECTED",
355 *is_bind_c = "BIND(C)", *procedure = "PROCEDURE";
356 static const char *threadprivate = "THREADPRIVATE";
358 const char *a1, *a2;
359 int standard;
361 if (where == NULL)
362 where = &gfc_current_locus;
364 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
366 a1 = pointer;
367 a2 = intent;
368 standard = GFC_STD_F2003;
369 goto conflict_std;
372 /* Check for attributes not allowed in a BLOCK DATA. */
373 if (gfc_current_state () == COMP_BLOCK_DATA)
375 a1 = NULL;
377 if (attr->in_namelist)
378 a1 = in_namelist;
379 if (attr->allocatable)
380 a1 = allocatable;
381 if (attr->external)
382 a1 = external;
383 if (attr->optional)
384 a1 = optional;
385 if (attr->access == ACCESS_PRIVATE)
386 a1 = private;
387 if (attr->access == ACCESS_PUBLIC)
388 a1 = public;
389 if (attr->intent != INTENT_UNKNOWN)
390 a1 = intent;
392 if (a1 != NULL)
394 gfc_error
395 ("%s attribute not allowed in BLOCK DATA program unit at %L",
396 a1, where);
397 return FAILURE;
401 if (attr->save == SAVE_EXPLICIT)
403 conf (dummy, save);
404 conf (in_common, save);
405 conf (result, save);
407 switch (attr->flavor)
409 case FL_PROGRAM:
410 case FL_BLOCK_DATA:
411 case FL_MODULE:
412 case FL_LABEL:
413 case FL_PROCEDURE:
414 case FL_DERIVED:
415 case FL_PARAMETER:
416 a1 = gfc_code2string (flavors, attr->flavor);
417 a2 = save;
418 goto conflict;
420 case FL_VARIABLE:
421 case FL_NAMELIST:
422 default:
423 break;
427 conf (dummy, entry);
428 conf (dummy, intrinsic);
429 conf (dummy, threadprivate);
430 conf (pointer, target);
431 conf (pointer, intrinsic);
432 conf (pointer, elemental);
433 conf (allocatable, elemental);
435 conf (target, external);
436 conf (target, intrinsic);
438 if (!attr->if_source)
439 conf (external, dimension); /* See Fortran 95's R504. */
441 conf (external, intrinsic);
442 conf (entry, intrinsic);
444 if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
446 conf (external, subroutine);
447 conf (external, function);
450 conf (allocatable, pointer);
451 conf_std (allocatable, dummy, GFC_STD_F2003);
452 conf_std (allocatable, function, GFC_STD_F2003);
453 conf_std (allocatable, result, GFC_STD_F2003);
454 conf (elemental, recursive);
456 conf (in_common, dummy);
457 conf (in_common, allocatable);
458 conf (in_common, result);
460 conf (dummy, result);
462 conf (in_equivalence, use_assoc);
463 conf (in_equivalence, dummy);
464 conf (in_equivalence, target);
465 conf (in_equivalence, pointer);
466 conf (in_equivalence, function);
467 conf (in_equivalence, result);
468 conf (in_equivalence, entry);
469 conf (in_equivalence, allocatable);
470 conf (in_equivalence, threadprivate);
472 conf (in_namelist, pointer);
473 conf (in_namelist, allocatable);
475 conf (entry, result);
477 conf (function, subroutine);
479 if (!function && !subroutine)
480 conf (is_bind_c, dummy);
482 conf (is_bind_c, cray_pointer);
483 conf (is_bind_c, cray_pointee);
484 conf (is_bind_c, allocatable);
485 conf (is_bind_c, elemental);
487 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
488 Parameter conflict caught below. Also, value cannot be specified
489 for a dummy procedure. */
491 /* Cray pointer/pointee conflicts. */
492 conf (cray_pointer, cray_pointee);
493 conf (cray_pointer, dimension);
494 conf (cray_pointer, pointer);
495 conf (cray_pointer, target);
496 conf (cray_pointer, allocatable);
497 conf (cray_pointer, external);
498 conf (cray_pointer, intrinsic);
499 conf (cray_pointer, in_namelist);
500 conf (cray_pointer, function);
501 conf (cray_pointer, subroutine);
502 conf (cray_pointer, entry);
504 conf (cray_pointee, allocatable);
505 conf (cray_pointee, intent);
506 conf (cray_pointee, optional);
507 conf (cray_pointee, dummy);
508 conf (cray_pointee, target);
509 conf (cray_pointee, intrinsic);
510 conf (cray_pointee, pointer);
511 conf (cray_pointee, entry);
512 conf (cray_pointee, in_common);
513 conf (cray_pointee, in_equivalence);
514 conf (cray_pointee, threadprivate);
516 conf (data, dummy);
517 conf (data, function);
518 conf (data, result);
519 conf (data, allocatable);
520 conf (data, use_assoc);
522 conf (value, pointer)
523 conf (value, allocatable)
524 conf (value, subroutine)
525 conf (value, function)
526 conf (value, volatile_)
527 conf (value, dimension)
528 conf (value, external)
530 if (attr->value
531 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
533 a1 = value;
534 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
535 goto conflict;
538 conf (protected, intrinsic)
539 conf (protected, external)
540 conf (protected, in_common)
542 conf (volatile_, intrinsic)
543 conf (volatile_, external)
545 if (attr->volatile_ && attr->intent == INTENT_IN)
547 a1 = volatile_;
548 a2 = intent_in;
549 goto conflict;
552 conf (procedure, allocatable)
553 conf (procedure, dimension)
554 conf (procedure, intrinsic)
555 conf (procedure, protected)
556 conf (procedure, target)
557 conf (procedure, value)
558 conf (procedure, volatile_)
559 conf (procedure, entry)
560 /* TODO: Implement procedure pointers. */
561 if (attr->procedure && attr->pointer)
563 gfc_error ("Fortran 2003: Procedure pointers at %L are "
564 "not yet implemented in gfortran", where);
565 return FAILURE;
568 a1 = gfc_code2string (flavors, attr->flavor);
570 if (attr->in_namelist
571 && attr->flavor != FL_VARIABLE
572 && attr->flavor != FL_PROCEDURE
573 && attr->flavor != FL_UNKNOWN)
575 a2 = in_namelist;
576 goto conflict;
579 switch (attr->flavor)
581 case FL_PROGRAM:
582 case FL_BLOCK_DATA:
583 case FL_MODULE:
584 case FL_LABEL:
585 conf2 (dimension);
586 conf2 (dummy);
587 conf2 (volatile_);
588 conf2 (pointer);
589 conf2 (protected);
590 conf2 (target);
591 conf2 (external);
592 conf2 (intrinsic);
593 conf2 (allocatable);
594 conf2 (result);
595 conf2 (in_namelist);
596 conf2 (optional);
597 conf2 (function);
598 conf2 (subroutine);
599 conf2 (threadprivate);
601 if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
603 a2 = attr->access == ACCESS_PUBLIC ? public : private;
604 gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
605 name, where);
606 return FAILURE;
609 if (attr->is_bind_c)
611 gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
612 return FAILURE;
615 break;
617 case FL_VARIABLE:
618 case FL_NAMELIST:
619 break;
621 case FL_PROCEDURE:
622 conf2 (intent);
624 if (attr->subroutine)
626 conf2 (pointer);
627 conf2 (target);
628 conf2 (allocatable);
629 conf2 (result);
630 conf2 (in_namelist);
631 conf2 (dimension);
632 conf2 (function);
633 conf2 (threadprivate);
636 switch (attr->proc)
638 case PROC_ST_FUNCTION:
639 conf2 (in_common);
640 conf2 (dummy);
641 break;
643 case PROC_MODULE:
644 conf2 (dummy);
645 break;
647 case PROC_DUMMY:
648 conf2 (result);
649 conf2 (in_common);
650 conf2 (threadprivate);
651 break;
653 default:
654 break;
657 break;
659 case FL_DERIVED:
660 conf2 (dummy);
661 conf2 (pointer);
662 conf2 (target);
663 conf2 (external);
664 conf2 (intrinsic);
665 conf2 (allocatable);
666 conf2 (optional);
667 conf2 (entry);
668 conf2 (function);
669 conf2 (subroutine);
670 conf2 (threadprivate);
672 if (attr->intent != INTENT_UNKNOWN)
674 a2 = intent;
675 goto conflict;
677 break;
679 case FL_PARAMETER:
680 conf2 (external);
681 conf2 (intrinsic);
682 conf2 (optional);
683 conf2 (allocatable);
684 conf2 (function);
685 conf2 (subroutine);
686 conf2 (entry);
687 conf2 (pointer);
688 conf2 (protected);
689 conf2 (target);
690 conf2 (dummy);
691 conf2 (in_common);
692 conf2 (value);
693 conf2 (volatile_);
694 conf2 (threadprivate);
695 conf2 (value);
696 conf2 (is_bind_c);
697 break;
699 default:
700 break;
703 return SUCCESS;
705 conflict:
706 if (name == NULL)
707 gfc_error ("%s attribute conflicts with %s attribute at %L",
708 a1, a2, where);
709 else
710 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
711 a1, a2, name, where);
713 return FAILURE;
715 conflict_std:
716 if (name == NULL)
718 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
719 "with %s attribute at %L", a1, a2,
720 where);
722 else
724 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
725 "with %s attribute in '%s' at %L",
726 a1, a2, name, where);
730 #undef conf
731 #undef conf2
732 #undef conf_std
735 /* Mark a symbol as referenced. */
737 void
738 gfc_set_sym_referenced (gfc_symbol *sym)
741 if (sym->attr.referenced)
742 return;
744 sym->attr.referenced = 1;
746 /* Remember which order dummy variables are accessed in. */
747 if (sym->attr.dummy)
748 sym->dummy_order = next_dummy_order++;
752 /* Common subroutine called by attribute changing subroutines in order
753 to prevent them from changing a symbol that has been
754 use-associated. Returns zero if it is OK to change the symbol,
755 nonzero if not. */
757 static int
758 check_used (symbol_attribute *attr, const char *name, locus *where)
761 if (attr->use_assoc == 0)
762 return 0;
764 if (where == NULL)
765 where = &gfc_current_locus;
767 if (name == NULL)
768 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
769 where);
770 else
771 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
772 name, where);
774 return 1;
778 /* Generate an error because of a duplicate attribute. */
780 static void
781 duplicate_attr (const char *attr, locus *where)
784 if (where == NULL)
785 where = &gfc_current_locus;
787 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
791 /* Called from decl.c (attr_decl1) to check attributes, when declared
792 separately. */
795 gfc_add_attribute (symbol_attribute *attr, locus *where)
798 if (check_used (attr, NULL, where))
799 return FAILURE;
801 return check_conflict (attr, NULL, where);
805 gfc_add_allocatable (symbol_attribute *attr, locus *where)
808 if (check_used (attr, NULL, where))
809 return FAILURE;
811 if (attr->allocatable)
813 duplicate_attr ("ALLOCATABLE", where);
814 return FAILURE;
817 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
818 && gfc_find_state (COMP_INTERFACE) == FAILURE)
820 gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
821 where);
822 return FAILURE;
825 attr->allocatable = 1;
826 return check_conflict (attr, NULL, where);
831 gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
834 if (check_used (attr, name, where))
835 return FAILURE;
837 if (attr->dimension)
839 duplicate_attr ("DIMENSION", where);
840 return FAILURE;
843 if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
844 && gfc_find_state (COMP_INTERFACE) == FAILURE)
846 gfc_error ("DIMENSION specified for '%s' outside its INTERFACE body "
847 "at %L", name, where);
848 return FAILURE;
851 attr->dimension = 1;
852 return check_conflict (attr, name, where);
857 gfc_add_external (symbol_attribute *attr, locus *where)
860 if (check_used (attr, NULL, where))
861 return FAILURE;
863 if (attr->external)
865 duplicate_attr ("EXTERNAL", where);
866 return FAILURE;
869 attr->external = 1;
871 return check_conflict (attr, NULL, where);
876 gfc_add_intrinsic (symbol_attribute *attr, locus *where)
879 if (check_used (attr, NULL, where))
880 return FAILURE;
882 if (attr->intrinsic)
884 duplicate_attr ("INTRINSIC", where);
885 return FAILURE;
888 attr->intrinsic = 1;
890 return check_conflict (attr, NULL, where);
895 gfc_add_optional (symbol_attribute *attr, locus *where)
898 if (check_used (attr, NULL, where))
899 return FAILURE;
901 if (attr->optional)
903 duplicate_attr ("OPTIONAL", where);
904 return FAILURE;
907 attr->optional = 1;
908 return check_conflict (attr, NULL, where);
913 gfc_add_pointer (symbol_attribute *attr, locus *where)
916 if (check_used (attr, NULL, where))
917 return FAILURE;
919 attr->pointer = 1;
920 return check_conflict (attr, NULL, where);
925 gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
928 if (check_used (attr, NULL, where))
929 return FAILURE;
931 attr->cray_pointer = 1;
932 return check_conflict (attr, NULL, where);
937 gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
940 if (check_used (attr, NULL, where))
941 return FAILURE;
943 if (attr->cray_pointee)
945 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
946 " statements", where);
947 return FAILURE;
950 attr->cray_pointee = 1;
951 return check_conflict (attr, NULL, where);
956 gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
958 if (check_used (attr, name, where))
959 return FAILURE;
961 if (attr->protected)
963 if (gfc_notify_std (GFC_STD_LEGACY,
964 "Duplicate PROTECTED attribute specified at %L",
965 where)
966 == FAILURE)
967 return FAILURE;
970 attr->protected = 1;
971 return check_conflict (attr, name, where);
976 gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
979 if (check_used (attr, name, where))
980 return FAILURE;
982 attr->result = 1;
983 return check_conflict (attr, name, where);
988 gfc_add_save (symbol_attribute *attr, const char *name, locus *where)
991 if (check_used (attr, name, where))
992 return FAILURE;
994 if (gfc_pure (NULL))
996 gfc_error
997 ("SAVE attribute at %L cannot be specified in a PURE procedure",
998 where);
999 return FAILURE;
1002 if (attr->save == SAVE_EXPLICIT)
1004 if (gfc_notify_std (GFC_STD_LEGACY,
1005 "Duplicate SAVE attribute specified at %L",
1006 where)
1007 == FAILURE)
1008 return FAILURE;
1011 attr->save = SAVE_EXPLICIT;
1012 return check_conflict (attr, name, where);
1017 gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1020 if (check_used (attr, name, where))
1021 return FAILURE;
1023 if (attr->value)
1025 if (gfc_notify_std (GFC_STD_LEGACY,
1026 "Duplicate VALUE attribute specified at %L",
1027 where)
1028 == FAILURE)
1029 return FAILURE;
1032 attr->value = 1;
1033 return check_conflict (attr, name, where);
1038 gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1040 /* No check_used needed as 11.2.1 of the F2003 standard allows
1041 that the local identifier made accessible by a use statement can be
1042 given a VOLATILE attribute. */
1044 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1045 if (gfc_notify_std (GFC_STD_LEGACY,
1046 "Duplicate VOLATILE attribute specified at %L", where)
1047 == FAILURE)
1048 return FAILURE;
1050 attr->volatile_ = 1;
1051 attr->volatile_ns = gfc_current_ns;
1052 return check_conflict (attr, name, where);
1057 gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1060 if (check_used (attr, name, where))
1061 return FAILURE;
1063 if (attr->threadprivate)
1065 duplicate_attr ("THREADPRIVATE", where);
1066 return FAILURE;
1069 attr->threadprivate = 1;
1070 return check_conflict (attr, name, where);
1075 gfc_add_target (symbol_attribute *attr, locus *where)
1078 if (check_used (attr, NULL, where))
1079 return FAILURE;
1081 if (attr->target)
1083 duplicate_attr ("TARGET", where);
1084 return FAILURE;
1087 attr->target = 1;
1088 return check_conflict (attr, NULL, where);
1093 gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1096 if (check_used (attr, name, where))
1097 return FAILURE;
1099 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1100 attr->dummy = 1;
1101 return check_conflict (attr, name, where);
1106 gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1109 if (check_used (attr, name, where))
1110 return FAILURE;
1112 /* Duplicate attribute already checked for. */
1113 attr->in_common = 1;
1114 if (check_conflict (attr, name, where) == FAILURE)
1115 return FAILURE;
1117 if (attr->flavor == FL_VARIABLE)
1118 return SUCCESS;
1120 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1125 gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1128 /* Duplicate attribute already checked for. */
1129 attr->in_equivalence = 1;
1130 if (check_conflict (attr, name, where) == FAILURE)
1131 return FAILURE;
1133 if (attr->flavor == FL_VARIABLE)
1134 return SUCCESS;
1136 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1141 gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1144 if (check_used (attr, name, where))
1145 return FAILURE;
1147 attr->data = 1;
1148 return check_conflict (attr, name, where);
1153 gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1156 attr->in_namelist = 1;
1157 return check_conflict (attr, name, where);
1162 gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1165 if (check_used (attr, name, where))
1166 return FAILURE;
1168 attr->sequence = 1;
1169 return check_conflict (attr, name, where);
1174 gfc_add_elemental (symbol_attribute *attr, locus *where)
1177 if (check_used (attr, NULL, where))
1178 return FAILURE;
1180 if (attr->elemental)
1182 duplicate_attr ("ELEMENTAL", where);
1183 return FAILURE;
1186 attr->elemental = 1;
1187 return check_conflict (attr, NULL, where);
1192 gfc_add_pure (symbol_attribute *attr, locus *where)
1195 if (check_used (attr, NULL, where))
1196 return FAILURE;
1198 if (attr->pure)
1200 duplicate_attr ("PURE", where);
1201 return FAILURE;
1204 attr->pure = 1;
1205 return check_conflict (attr, NULL, where);
1210 gfc_add_recursive (symbol_attribute *attr, locus *where)
1213 if (check_used (attr, NULL, where))
1214 return FAILURE;
1216 if (attr->recursive)
1218 duplicate_attr ("RECURSIVE", where);
1219 return FAILURE;
1222 attr->recursive = 1;
1223 return check_conflict (attr, NULL, where);
1228 gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1231 if (check_used (attr, name, where))
1232 return FAILURE;
1234 if (attr->entry)
1236 duplicate_attr ("ENTRY", where);
1237 return FAILURE;
1240 attr->entry = 1;
1241 return check_conflict (attr, name, where);
1246 gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1249 if (attr->flavor != FL_PROCEDURE
1250 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1251 return FAILURE;
1253 attr->function = 1;
1254 return check_conflict (attr, name, where);
1259 gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1262 if (attr->flavor != FL_PROCEDURE
1263 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1264 return FAILURE;
1266 attr->subroutine = 1;
1267 return check_conflict (attr, name, where);
1272 gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1275 if (attr->flavor != FL_PROCEDURE
1276 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1277 return FAILURE;
1279 attr->generic = 1;
1280 return check_conflict (attr, name, where);
1285 gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1288 if (check_used (attr, NULL, where))
1289 return FAILURE;
1291 if (attr->flavor != FL_PROCEDURE
1292 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1293 return FAILURE;
1295 if (attr->procedure)
1297 duplicate_attr ("PROCEDURE", where);
1298 return FAILURE;
1301 attr->procedure = 1;
1303 return check_conflict (attr, NULL, where);
1307 /* Flavors are special because some flavors are not what Fortran
1308 considers attributes and can be reaffirmed multiple times. */
1311 gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1312 locus *where)
1315 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1316 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
1317 || f == FL_NAMELIST) && check_used (attr, name, where))
1318 return FAILURE;
1320 if (attr->flavor == f && f == FL_VARIABLE)
1321 return SUCCESS;
1323 if (attr->flavor != FL_UNKNOWN)
1325 if (where == NULL)
1326 where = &gfc_current_locus;
1328 if (name)
1329 gfc_error ("%s attribute of '%s' conflicts with %s attribute at %L",
1330 gfc_code2string (flavors, attr->flavor), name,
1331 gfc_code2string (flavors, f), where);
1332 else
1333 gfc_error ("%s attribute conflicts with %s attribute at %L",
1334 gfc_code2string (flavors, attr->flavor),
1335 gfc_code2string (flavors, f), where);
1337 return FAILURE;
1340 attr->flavor = f;
1342 return check_conflict (attr, name, where);
1347 gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1348 const char *name, locus *where)
1351 if (check_used (attr, name, where))
1352 return FAILURE;
1354 if (attr->flavor != FL_PROCEDURE
1355 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1356 return FAILURE;
1358 if (where == NULL)
1359 where = &gfc_current_locus;
1361 if (attr->proc != PROC_UNKNOWN)
1363 gfc_error ("%s procedure at %L is already declared as %s procedure",
1364 gfc_code2string (procedures, t), where,
1365 gfc_code2string (procedures, attr->proc));
1367 return FAILURE;
1370 attr->proc = t;
1372 /* Statement functions are always scalar and functions. */
1373 if (t == PROC_ST_FUNCTION
1374 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1375 || attr->dimension))
1376 return FAILURE;
1378 return check_conflict (attr, name, where);
1383 gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1386 if (check_used (attr, NULL, where))
1387 return FAILURE;
1389 if (attr->intent == INTENT_UNKNOWN)
1391 attr->intent = intent;
1392 return check_conflict (attr, NULL, where);
1395 if (where == NULL)
1396 where = &gfc_current_locus;
1398 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1399 gfc_intent_string (attr->intent),
1400 gfc_intent_string (intent), where);
1402 return FAILURE;
1406 /* No checks for use-association in public and private statements. */
1409 gfc_add_access (symbol_attribute *attr, gfc_access access,
1410 const char *name, locus *where)
1413 if (attr->access == ACCESS_UNKNOWN)
1415 attr->access = access;
1416 return check_conflict (attr, name, where);
1419 if (where == NULL)
1420 where = &gfc_current_locus;
1421 gfc_error ("ACCESS specification at %L was already specified", where);
1423 return FAILURE;
1427 /* Set the is_bind_c field for the given symbol_attribute. */
1430 gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1431 int is_proc_lang_bind_spec)
1434 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1435 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1436 "variables or common blocks", where);
1437 else if (attr->is_bind_c)
1438 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1439 else
1440 attr->is_bind_c = 1;
1442 if (where == NULL)
1443 where = &gfc_current_locus;
1445 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BIND(C) at %L", where)
1446 == FAILURE)
1447 return FAILURE;
1449 return check_conflict (attr, name, where);
1454 gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1455 gfc_formal_arglist * formal, locus *where)
1458 if (check_used (&sym->attr, sym->name, where))
1459 return FAILURE;
1461 if (where == NULL)
1462 where = &gfc_current_locus;
1464 if (sym->attr.if_source != IFSRC_UNKNOWN
1465 && sym->attr.if_source != IFSRC_DECL)
1467 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1468 sym->name, where);
1469 return FAILURE;
1472 if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1474 gfc_error ("'%s' at %L has attributes specified outside its INTERFACE "
1475 "body", sym->name, where);
1476 return FAILURE;
1479 sym->formal = formal;
1480 sym->attr.if_source = source;
1482 return SUCCESS;
1486 /* Add a type to a symbol. */
1489 gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1491 sym_flavor flavor;
1493 if (where == NULL)
1494 where = &gfc_current_locus;
1496 if (sym->ts.type != BT_UNKNOWN)
1498 const char *msg = "Symbol '%s' at %L already has basic type of %s";
1499 if (!(sym->ts.type == ts->type
1500 && (sym->attr.flavor == FL_PROCEDURE || sym->attr.result))
1501 || gfc_notification_std (GFC_STD_GNU) == ERROR
1502 || pedantic)
1504 gfc_error (msg, sym->name, where, gfc_basic_typename (sym->ts.type));
1505 return FAILURE;
1507 else if (gfc_notify_std (GFC_STD_GNU, msg, sym->name, where,
1508 gfc_basic_typename (sym->ts.type)) == FAILURE)
1509 return FAILURE;
1512 flavor = sym->attr.flavor;
1514 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1515 || flavor == FL_LABEL
1516 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
1517 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1519 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1520 return FAILURE;
1523 sym->ts = *ts;
1524 return SUCCESS;
1528 /* Clears all attributes. */
1530 void
1531 gfc_clear_attr (symbol_attribute *attr)
1533 memset (attr, 0, sizeof (symbol_attribute));
1537 /* Check for missing attributes in the new symbol. Currently does
1538 nothing, but it's not clear that it is unnecessary yet. */
1541 gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1542 locus *where ATTRIBUTE_UNUSED)
1545 return SUCCESS;
1549 /* Copy an attribute to a symbol attribute, bit by bit. Some
1550 attributes have a lot of side-effects but cannot be present given
1551 where we are called from, so we ignore some bits. */
1554 gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
1556 int is_proc_lang_bind_spec;
1558 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1559 goto fail;
1561 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1562 goto fail;
1563 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1564 goto fail;
1565 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1566 goto fail;
1567 if (src->protected && gfc_add_protected (dest, NULL, where) == FAILURE)
1568 goto fail;
1569 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1570 goto fail;
1571 if (src->value && gfc_add_value (dest, NULL, where) == FAILURE)
1572 goto fail;
1573 if (src->volatile_ && gfc_add_volatile (dest, NULL, where) == FAILURE)
1574 goto fail;
1575 if (src->threadprivate
1576 && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
1577 goto fail;
1578 if (src->target && gfc_add_target (dest, where) == FAILURE)
1579 goto fail;
1580 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1581 goto fail;
1582 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1583 goto fail;
1584 if (src->entry)
1585 dest->entry = 1;
1587 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1588 goto fail;
1590 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1591 goto fail;
1593 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1594 goto fail;
1595 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1596 goto fail;
1597 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1598 goto fail;
1600 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1601 goto fail;
1602 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1603 goto fail;
1604 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1605 goto fail;
1606 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1607 goto fail;
1609 if (src->flavor != FL_UNKNOWN
1610 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1611 goto fail;
1613 if (src->intent != INTENT_UNKNOWN
1614 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1615 goto fail;
1617 if (src->access != ACCESS_UNKNOWN
1618 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1619 goto fail;
1621 if (gfc_missing_attr (dest, where) == FAILURE)
1622 goto fail;
1624 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1625 goto fail;
1626 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1627 goto fail;
1629 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1630 if (src->is_bind_c
1631 && gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec)
1632 != SUCCESS)
1633 return FAILURE;
1635 if (src->is_c_interop)
1636 dest->is_c_interop = 1;
1637 if (src->is_iso_c)
1638 dest->is_iso_c = 1;
1640 if (src->external && gfc_add_external (dest, where) == FAILURE)
1641 goto fail;
1642 if (src->intrinsic && gfc_add_intrinsic (dest, where) == FAILURE)
1643 goto fail;
1645 return SUCCESS;
1647 fail:
1648 return FAILURE;
1652 /************** Component name management ************/
1654 /* Component names of a derived type form their own little namespaces
1655 that are separate from all other spaces. The space is composed of
1656 a singly linked list of gfc_component structures whose head is
1657 located in the parent symbol. */
1660 /* Add a component name to a symbol. The call fails if the name is
1661 already present. On success, the component pointer is modified to
1662 point to the additional component structure. */
1665 gfc_add_component (gfc_symbol *sym, const char *name,
1666 gfc_component **component)
1668 gfc_component *p, *tail;
1670 tail = NULL;
1672 for (p = sym->components; p; p = p->next)
1674 if (strcmp (p->name, name) == 0)
1676 gfc_error ("Component '%s' at %C already declared at %L",
1677 name, &p->loc);
1678 return FAILURE;
1681 tail = p;
1684 /* Allocate a new component. */
1685 p = gfc_get_component ();
1687 if (tail == NULL)
1688 sym->components = p;
1689 else
1690 tail->next = p;
1692 p->name = gfc_get_string (name);
1693 p->loc = gfc_current_locus;
1695 *component = p;
1696 return SUCCESS;
1700 /* Recursive function to switch derived types of all symbol in a
1701 namespace. */
1703 static void
1704 switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
1706 gfc_symbol *sym;
1708 if (st == NULL)
1709 return;
1711 sym = st->n.sym;
1712 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1713 sym->ts.derived = to;
1715 switch_types (st->left, from, to);
1716 switch_types (st->right, from, to);
1720 /* This subroutine is called when a derived type is used in order to
1721 make the final determination about which version to use. The
1722 standard requires that a type be defined before it is 'used', but
1723 such types can appear in IMPLICIT statements before the actual
1724 definition. 'Using' in this context means declaring a variable to
1725 be that type or using the type constructor.
1727 If a type is used and the components haven't been defined, then we
1728 have to have a derived type in a parent unit. We find the node in
1729 the other namespace and point the symtree node in this namespace to
1730 that node. Further reference to this name point to the correct
1731 node. If we can't find the node in a parent namespace, then we have
1732 an error.
1734 This subroutine takes a pointer to a symbol node and returns a
1735 pointer to the translated node or NULL for an error. Usually there
1736 is no translation and we return the node we were passed. */
1738 gfc_symbol *
1739 gfc_use_derived (gfc_symbol *sym)
1741 gfc_symbol *s;
1742 gfc_typespec *t;
1743 gfc_symtree *st;
1744 int i;
1746 if (sym->components != NULL || sym->attr.zero_comp)
1747 return sym; /* Already defined. */
1749 if (sym->ns->parent == NULL)
1750 goto bad;
1752 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1754 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1755 return NULL;
1758 if (s == NULL || s->attr.flavor != FL_DERIVED)
1759 goto bad;
1761 /* Get rid of symbol sym, translating all references to s. */
1762 for (i = 0; i < GFC_LETTERS; i++)
1764 t = &sym->ns->default_type[i];
1765 if (t->derived == sym)
1766 t->derived = s;
1769 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1770 st->n.sym = s;
1772 s->refs++;
1774 /* Unlink from list of modified symbols. */
1775 gfc_commit_symbol (sym);
1777 switch_types (sym->ns->sym_root, sym, s);
1779 /* TODO: Also have to replace sym -> s in other lists like
1780 namelists, common lists and interface lists. */
1781 gfc_free_symbol (sym);
1783 return s;
1785 bad:
1786 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1787 sym->name);
1788 return NULL;
1792 /* Given a derived type node and a component name, try to locate the
1793 component structure. Returns the NULL pointer if the component is
1794 not found or the components are private. */
1796 gfc_component *
1797 gfc_find_component (gfc_symbol *sym, const char *name)
1799 gfc_component *p;
1801 if (name == NULL)
1802 return NULL;
1804 sym = gfc_use_derived (sym);
1806 if (sym == NULL)
1807 return NULL;
1809 for (p = sym->components; p; p = p->next)
1810 if (strcmp (p->name, name) == 0)
1811 break;
1813 if (p == NULL)
1814 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1815 name, sym->name);
1816 else
1818 if (sym->attr.use_assoc && (sym->component_access == ACCESS_PRIVATE
1819 || p->access == ACCESS_PRIVATE))
1821 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1822 name, sym->name);
1823 p = NULL;
1827 return p;
1831 /* Given a symbol, free all of the component structures and everything
1832 they point to. */
1834 static void
1835 free_components (gfc_component *p)
1837 gfc_component *q;
1839 for (; p; p = q)
1841 q = p->next;
1843 gfc_free_array_spec (p->as);
1844 gfc_free_expr (p->initializer);
1846 gfc_free (p);
1851 /* Set component attributes from a standard symbol attribute structure. */
1853 void
1854 gfc_set_component_attr (gfc_component *c, symbol_attribute *attr)
1857 c->dimension = attr->dimension;
1858 c->pointer = attr->pointer;
1859 c->allocatable = attr->allocatable;
1860 c->access = attr->access;
1864 /* Get a standard symbol attribute structure given the component
1865 structure. */
1867 void
1868 gfc_get_component_attr (symbol_attribute *attr, gfc_component *c)
1871 gfc_clear_attr (attr);
1872 attr->dimension = c->dimension;
1873 attr->pointer = c->pointer;
1874 attr->allocatable = c->allocatable;
1875 attr->access = c->access;
1879 /******************** Statement label management ********************/
1881 /* Comparison function for statement labels, used for managing the
1882 binary tree. */
1884 static int
1885 compare_st_labels (void *a1, void *b1)
1887 int a = ((gfc_st_label *) a1)->value;
1888 int b = ((gfc_st_label *) b1)->value;
1890 return (b - a);
1894 /* Free a single gfc_st_label structure, making sure the tree is not
1895 messed up. This function is called only when some parse error
1896 occurs. */
1898 void
1899 gfc_free_st_label (gfc_st_label *label)
1902 if (label == NULL)
1903 return;
1905 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
1907 if (label->format != NULL)
1908 gfc_free_expr (label->format);
1910 gfc_free (label);
1914 /* Free a whole tree of gfc_st_label structures. */
1916 static void
1917 free_st_labels (gfc_st_label *label)
1920 if (label == NULL)
1921 return;
1923 free_st_labels (label->left);
1924 free_st_labels (label->right);
1926 if (label->format != NULL)
1927 gfc_free_expr (label->format);
1928 gfc_free (label);
1932 /* Given a label number, search for and return a pointer to the label
1933 structure, creating it if it does not exist. */
1935 gfc_st_label *
1936 gfc_get_st_label (int labelno)
1938 gfc_st_label *lp;
1940 /* First see if the label is already in this namespace. */
1941 lp = gfc_current_ns->st_labels;
1942 while (lp)
1944 if (lp->value == labelno)
1945 return lp;
1947 if (lp->value < labelno)
1948 lp = lp->left;
1949 else
1950 lp = lp->right;
1953 lp = gfc_getmem (sizeof (gfc_st_label));
1955 lp->value = labelno;
1956 lp->defined = ST_LABEL_UNKNOWN;
1957 lp->referenced = ST_LABEL_UNKNOWN;
1959 gfc_insert_bbt (&gfc_current_ns->st_labels, lp, compare_st_labels);
1961 return lp;
1965 /* Called when a statement with a statement label is about to be
1966 accepted. We add the label to the list of the current namespace,
1967 making sure it hasn't been defined previously and referenced
1968 correctly. */
1970 void
1971 gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
1973 int labelno;
1975 labelno = lp->value;
1977 if (lp->defined != ST_LABEL_UNKNOWN)
1978 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1979 &lp->where, label_locus);
1980 else
1982 lp->where = *label_locus;
1984 switch (type)
1986 case ST_LABEL_FORMAT:
1987 if (lp->referenced == ST_LABEL_TARGET)
1988 gfc_error ("Label %d at %C already referenced as branch target",
1989 labelno);
1990 else
1991 lp->defined = ST_LABEL_FORMAT;
1993 break;
1995 case ST_LABEL_TARGET:
1996 if (lp->referenced == ST_LABEL_FORMAT)
1997 gfc_error ("Label %d at %C already referenced as a format label",
1998 labelno);
1999 else
2000 lp->defined = ST_LABEL_TARGET;
2002 break;
2004 default:
2005 lp->defined = ST_LABEL_BAD_TARGET;
2006 lp->referenced = ST_LABEL_BAD_TARGET;
2012 /* Reference a label. Given a label and its type, see if that
2013 reference is consistent with what is known about that label,
2014 updating the unknown state. Returns FAILURE if something goes
2015 wrong. */
2018 gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2020 gfc_sl_type label_type;
2021 int labelno;
2022 try rc;
2024 if (lp == NULL)
2025 return SUCCESS;
2027 labelno = lp->value;
2029 if (lp->defined != ST_LABEL_UNKNOWN)
2030 label_type = lp->defined;
2031 else
2033 label_type = lp->referenced;
2034 lp->where = gfc_current_locus;
2037 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
2039 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2040 rc = FAILURE;
2041 goto done;
2044 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
2045 && type == ST_LABEL_FORMAT)
2047 gfc_error ("Label %d at %C previously used as branch target", labelno);
2048 rc = FAILURE;
2049 goto done;
2052 lp->referenced = type;
2053 rc = SUCCESS;
2055 done:
2056 return rc;
2060 /*******A helper function for creating new expressions*************/
2063 gfc_expr *
2064 gfc_lval_expr_from_sym (gfc_symbol *sym)
2066 gfc_expr *lval;
2067 lval = gfc_get_expr ();
2068 lval->expr_type = EXPR_VARIABLE;
2069 lval->where = sym->declared_at;
2070 lval->ts = sym->ts;
2071 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
2073 /* It will always be a full array. */
2074 lval->rank = sym->as ? sym->as->rank : 0;
2075 if (lval->rank)
2077 lval->ref = gfc_get_ref ();
2078 lval->ref->type = REF_ARRAY;
2079 lval->ref->u.ar.type = AR_FULL;
2080 lval->ref->u.ar.dimen = lval->rank;
2081 lval->ref->u.ar.where = sym->declared_at;
2082 lval->ref->u.ar.as = sym->as;
2085 return lval;
2089 /************** Symbol table management subroutines ****************/
2091 /* Basic details: Fortran 95 requires a potentially unlimited number
2092 of distinct namespaces when compiling a program unit. This case
2093 occurs during a compilation of internal subprograms because all of
2094 the internal subprograms must be read before we can start
2095 generating code for the host.
2097 Given the tricky nature of the Fortran grammar, we must be able to
2098 undo changes made to a symbol table if the current interpretation
2099 of a statement is found to be incorrect. Whenever a symbol is
2100 looked up, we make a copy of it and link to it. All of these
2101 symbols are kept in a singly linked list so that we can commit or
2102 undo the changes at a later time.
2104 A symtree may point to a symbol node outside of its namespace. In
2105 this case, that symbol has been used as a host associated variable
2106 at some previous time. */
2108 /* Allocate a new namespace structure. Copies the implicit types from
2109 PARENT if PARENT_TYPES is set. */
2111 gfc_namespace *
2112 gfc_get_namespace (gfc_namespace *parent, int parent_types)
2114 gfc_namespace *ns;
2115 gfc_typespec *ts;
2116 gfc_intrinsic_op in;
2117 int i;
2119 ns = gfc_getmem (sizeof (gfc_namespace));
2120 ns->sym_root = NULL;
2121 ns->uop_root = NULL;
2122 ns->finalizers = NULL;
2123 ns->default_access = ACCESS_UNKNOWN;
2124 ns->parent = parent;
2126 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
2127 ns->operator_access[in] = ACCESS_UNKNOWN;
2129 /* Initialize default implicit types. */
2130 for (i = 'a'; i <= 'z'; i++)
2132 ns->set_flag[i - 'a'] = 0;
2133 ts = &ns->default_type[i - 'a'];
2135 if (parent_types && ns->parent != NULL)
2137 /* Copy parent settings. */
2138 *ts = ns->parent->default_type[i - 'a'];
2139 continue;
2142 if (gfc_option.flag_implicit_none != 0)
2144 gfc_clear_ts (ts);
2145 continue;
2148 if ('i' <= i && i <= 'n')
2150 ts->type = BT_INTEGER;
2151 ts->kind = gfc_default_integer_kind;
2153 else
2155 ts->type = BT_REAL;
2156 ts->kind = gfc_default_real_kind;
2160 ns->refs = 1;
2162 return ns;
2166 /* Comparison function for symtree nodes. */
2168 static int
2169 compare_symtree (void *_st1, void *_st2)
2171 gfc_symtree *st1, *st2;
2173 st1 = (gfc_symtree *) _st1;
2174 st2 = (gfc_symtree *) _st2;
2176 return strcmp (st1->name, st2->name);
2180 /* Allocate a new symtree node and associate it with the new symbol. */
2182 gfc_symtree *
2183 gfc_new_symtree (gfc_symtree **root, const char *name)
2185 gfc_symtree *st;
2187 st = gfc_getmem (sizeof (gfc_symtree));
2188 st->name = gfc_get_string (name);
2190 gfc_insert_bbt (root, st, compare_symtree);
2191 return st;
2195 /* Delete a symbol from the tree. Does not free the symbol itself! */
2197 void
2198 gfc_delete_symtree (gfc_symtree **root, const char *name)
2200 gfc_symtree st, *st0;
2202 st0 = gfc_find_symtree (*root, name);
2204 st.name = gfc_get_string (name);
2205 gfc_delete_bbt (root, &st, compare_symtree);
2207 gfc_free (st0);
2211 /* Given a root symtree node and a name, try to find the symbol within
2212 the namespace. Returns NULL if the symbol is not found. */
2214 gfc_symtree *
2215 gfc_find_symtree (gfc_symtree *st, const char *name)
2217 int c;
2219 while (st != NULL)
2221 c = strcmp (name, st->name);
2222 if (c == 0)
2223 return st;
2225 st = (c < 0) ? st->left : st->right;
2228 return NULL;
2232 /* Return a symtree node with a name that is guaranteed to be unique
2233 within the namespace and corresponds to an illegal fortran name. */
2235 gfc_symtree *
2236 gfc_get_unique_symtree (gfc_namespace *ns)
2238 char name[GFC_MAX_SYMBOL_LEN + 1];
2239 static int serial = 0;
2241 sprintf (name, "@%d", serial++);
2242 return gfc_new_symtree (&ns->sym_root, name);
2246 /* Given a name find a user operator node, creating it if it doesn't
2247 exist. These are much simpler than symbols because they can't be
2248 ambiguous with one another. */
2250 gfc_user_op *
2251 gfc_get_uop (const char *name)
2253 gfc_user_op *uop;
2254 gfc_symtree *st;
2256 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
2257 if (st != NULL)
2258 return st->n.uop;
2260 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
2262 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
2263 uop->name = gfc_get_string (name);
2264 uop->access = ACCESS_UNKNOWN;
2265 uop->ns = gfc_current_ns;
2267 return uop;
2271 /* Given a name find the user operator node. Returns NULL if it does
2272 not exist. */
2274 gfc_user_op *
2275 gfc_find_uop (const char *name, gfc_namespace *ns)
2277 gfc_symtree *st;
2279 if (ns == NULL)
2280 ns = gfc_current_ns;
2282 st = gfc_find_symtree (ns->uop_root, name);
2283 return (st == NULL) ? NULL : st->n.uop;
2287 /* Remove a gfc_symbol structure and everything it points to. */
2289 void
2290 gfc_free_symbol (gfc_symbol *sym)
2293 if (sym == NULL)
2294 return;
2296 gfc_free_array_spec (sym->as);
2298 free_components (sym->components);
2300 gfc_free_expr (sym->value);
2302 gfc_free_namelist (sym->namelist);
2304 gfc_free_namespace (sym->formal_ns);
2306 if (!sym->attr.generic_copy)
2307 gfc_free_interface (sym->generic);
2309 gfc_free_formal_arglist (sym->formal);
2311 gfc_free_namespace (sym->f2k_derived);
2313 gfc_free (sym);
2317 /* Allocate and initialize a new symbol node. */
2319 gfc_symbol *
2320 gfc_new_symbol (const char *name, gfc_namespace *ns)
2322 gfc_symbol *p;
2324 p = gfc_getmem (sizeof (gfc_symbol));
2326 gfc_clear_ts (&p->ts);
2327 gfc_clear_attr (&p->attr);
2328 p->ns = ns;
2330 p->declared_at = gfc_current_locus;
2332 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2333 gfc_internal_error ("new_symbol(): Symbol name too long");
2335 p->name = gfc_get_string (name);
2337 /* Make sure flags for symbol being C bound are clear initially. */
2338 p->attr.is_bind_c = 0;
2339 p->attr.is_iso_c = 0;
2340 /* Make sure the binding label field has a Nul char to start. */
2341 p->binding_label[0] = '\0';
2343 /* Clear the ptrs we may need. */
2344 p->common_block = NULL;
2345 p->f2k_derived = NULL;
2347 return p;
2351 /* Generate an error if a symbol is ambiguous. */
2353 static void
2354 ambiguous_symbol (const char *name, gfc_symtree *st)
2357 if (st->n.sym->module)
2358 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2359 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
2360 else
2361 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2362 "from current program unit", name, st->n.sym->name);
2366 /* Search for a symtree starting in the current namespace, resorting to
2367 any parent namespaces if requested by a nonzero parent_flag.
2368 Returns nonzero if the name is ambiguous. */
2371 gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2372 gfc_symtree **result)
2374 gfc_symtree *st;
2376 if (ns == NULL)
2377 ns = gfc_current_ns;
2381 st = gfc_find_symtree (ns->sym_root, name);
2382 if (st != NULL)
2384 *result = st;
2385 /* Ambiguous generic interfaces are permitted, as long
2386 as the specific interfaces are different. */
2387 if (st->ambiguous && !st->n.sym->attr.generic)
2389 ambiguous_symbol (name, st);
2390 return 1;
2393 return 0;
2396 if (!parent_flag)
2397 break;
2399 ns = ns->parent;
2401 while (ns != NULL);
2403 *result = NULL;
2404 return 0;
2408 /* Same, but returns the symbol instead. */
2411 gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
2412 gfc_symbol **result)
2414 gfc_symtree *st;
2415 int i;
2417 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2419 if (st == NULL)
2420 *result = NULL;
2421 else
2422 *result = st->n.sym;
2424 return i;
2428 /* Save symbol with the information necessary to back it out. */
2430 static void
2431 save_symbol_data (gfc_symbol *sym)
2434 if (sym->new || sym->old_symbol != NULL)
2435 return;
2437 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
2438 *(sym->old_symbol) = *sym;
2440 sym->tlink = changed_syms;
2441 changed_syms = sym;
2445 /* Given a name, find a symbol, or create it if it does not exist yet
2446 in the current namespace. If the symbol is found we make sure that
2447 it's OK.
2449 The integer return code indicates
2450 0 All OK
2451 1 The symbol name was ambiguous
2452 2 The name meant to be established was already host associated.
2454 So if the return value is nonzero, then an error was issued. */
2457 gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result)
2459 gfc_symtree *st;
2460 gfc_symbol *p;
2462 /* This doesn't usually happen during resolution. */
2463 if (ns == NULL)
2464 ns = gfc_current_ns;
2466 /* Try to find the symbol in ns. */
2467 st = gfc_find_symtree (ns->sym_root, name);
2469 if (st == NULL)
2471 /* If not there, create a new symbol. */
2472 p = gfc_new_symbol (name, ns);
2474 /* Add to the list of tentative symbols. */
2475 p->old_symbol = NULL;
2476 p->tlink = changed_syms;
2477 p->mark = 1;
2478 p->new = 1;
2479 changed_syms = p;
2481 st = gfc_new_symtree (&ns->sym_root, name);
2482 st->n.sym = p;
2483 p->refs++;
2486 else
2488 /* Make sure the existing symbol is OK. Ambiguous
2489 generic interfaces are permitted, as long as the
2490 specific interfaces are different. */
2491 if (st->ambiguous && !st->n.sym->attr.generic)
2493 ambiguous_symbol (name, st);
2494 return 1;
2497 p = st->n.sym;
2499 if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
2500 && !(ns->proc_name
2501 && ns->proc_name->attr.if_source == IFSRC_IFBODY
2502 && (ns->has_import_set || p->attr.imported)))
2504 /* Symbol is from another namespace. */
2505 gfc_error ("Symbol '%s' at %C has already been host associated",
2506 name);
2507 return 2;
2510 p->mark = 1;
2512 /* Copy in case this symbol is changed. */
2513 save_symbol_data (p);
2516 *result = st;
2517 return 0;
2522 gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
2524 gfc_symtree *st;
2525 int i;
2527 i = gfc_get_sym_tree (name, ns, &st);
2528 if (i != 0)
2529 return i;
2531 if (st)
2532 *result = st->n.sym;
2533 else
2534 *result = NULL;
2535 return i;
2539 /* Subroutine that searches for a symbol, creating it if it doesn't
2540 exist, but tries to host-associate the symbol if possible. */
2543 gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
2545 gfc_symtree *st;
2546 int i;
2548 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2549 if (st != NULL)
2551 save_symbol_data (st->n.sym);
2552 *result = st;
2553 return i;
2556 if (gfc_current_ns->parent != NULL)
2558 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2559 if (i)
2560 return i;
2562 if (st != NULL)
2564 *result = st;
2565 return 0;
2569 return gfc_get_sym_tree (name, gfc_current_ns, result);
2574 gfc_get_ha_symbol (const char *name, gfc_symbol **result)
2576 int i;
2577 gfc_symtree *st;
2579 i = gfc_get_ha_sym_tree (name, &st);
2581 if (st)
2582 *result = st->n.sym;
2583 else
2584 *result = NULL;
2586 return i;
2589 /* Return true if both symbols could refer to the same data object. Does
2590 not take account of aliasing due to equivalence statements. */
2593 gfc_symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym)
2595 /* Aliasing isn't possible if the symbols have different base types. */
2596 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2597 return 0;
2599 /* Pointers can point to other pointers, target objects and allocatable
2600 objects. Two allocatable objects cannot share the same storage. */
2601 if (lsym->attr.pointer
2602 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2603 return 1;
2604 if (lsym->attr.target && rsym->attr.pointer)
2605 return 1;
2606 if (lsym->attr.allocatable && rsym->attr.pointer)
2607 return 1;
2609 return 0;
2613 /* Undoes all the changes made to symbols in the current statement.
2614 This subroutine is made simpler due to the fact that attributes are
2615 never removed once added. */
2617 void
2618 gfc_undo_symbols (void)
2620 gfc_symbol *p, *q, *old;
2622 for (p = changed_syms; p; p = q)
2624 q = p->tlink;
2626 if (p->new)
2628 /* Symbol was new. */
2629 if (p->attr.in_common && p->common_block->head)
2631 /* If the symbol was added to any common block, it
2632 needs to be removed to stop the resolver looking
2633 for a (possibly) dead symbol. */
2635 if (p->common_block->head == p)
2636 p->common_block->head = p->common_next;
2637 else
2639 gfc_symbol *cparent, *csym;
2641 cparent = p->common_block->head;
2642 csym = cparent->common_next;
2644 while (csym != p)
2646 cparent = csym;
2647 csym = csym->common_next;
2650 gcc_assert(cparent->common_next == p);
2652 cparent->common_next = csym->common_next;
2656 gfc_delete_symtree (&p->ns->sym_root, p->name);
2658 p->refs--;
2659 if (p->refs < 0)
2660 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2661 if (p->refs == 0)
2662 gfc_free_symbol (p);
2663 continue;
2666 /* Restore previous state of symbol. Just copy simple stuff. */
2667 p->mark = 0;
2668 old = p->old_symbol;
2670 p->ts.type = old->ts.type;
2671 p->ts.kind = old->ts.kind;
2673 p->attr = old->attr;
2675 if (p->value != old->value)
2677 gfc_free_expr (old->value);
2678 p->value = NULL;
2681 if (p->as != old->as)
2683 if (p->as)
2684 gfc_free_array_spec (p->as);
2685 p->as = old->as;
2688 p->generic = old->generic;
2689 p->component_access = old->component_access;
2691 if (p->namelist != NULL && old->namelist == NULL)
2693 gfc_free_namelist (p->namelist);
2694 p->namelist = NULL;
2696 else
2698 if (p->namelist_tail != old->namelist_tail)
2700 gfc_free_namelist (old->namelist_tail);
2701 old->namelist_tail->next = NULL;
2705 p->namelist_tail = old->namelist_tail;
2707 if (p->formal != old->formal)
2709 gfc_free_formal_arglist (p->formal);
2710 p->formal = old->formal;
2713 gfc_free (p->old_symbol);
2714 p->old_symbol = NULL;
2715 p->tlink = NULL;
2718 changed_syms = NULL;
2722 /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2723 components of old_symbol that might need deallocation are the "allocatables"
2724 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2725 namelist_tail. In case these differ between old_symbol and sym, it's just
2726 because sym->namelist has gotten a few more items. */
2728 static void
2729 free_old_symbol (gfc_symbol *sym)
2732 if (sym->old_symbol == NULL)
2733 return;
2735 if (sym->old_symbol->as != sym->as)
2736 gfc_free_array_spec (sym->old_symbol->as);
2738 if (sym->old_symbol->value != sym->value)
2739 gfc_free_expr (sym->old_symbol->value);
2741 if (sym->old_symbol->formal != sym->formal)
2742 gfc_free_formal_arglist (sym->old_symbol->formal);
2744 gfc_free (sym->old_symbol);
2745 sym->old_symbol = NULL;
2749 /* Makes the changes made in the current statement permanent-- gets
2750 rid of undo information. */
2752 void
2753 gfc_commit_symbols (void)
2755 gfc_symbol *p, *q;
2757 for (p = changed_syms; p; p = q)
2759 q = p->tlink;
2760 p->tlink = NULL;
2761 p->mark = 0;
2762 p->new = 0;
2763 free_old_symbol (p);
2765 changed_syms = NULL;
2769 /* Makes the changes made in one symbol permanent -- gets rid of undo
2770 information. */
2772 void
2773 gfc_commit_symbol (gfc_symbol *sym)
2775 gfc_symbol *p;
2777 if (changed_syms == sym)
2778 changed_syms = sym->tlink;
2779 else
2781 for (p = changed_syms; p; p = p->tlink)
2782 if (p->tlink == sym)
2784 p->tlink = sym->tlink;
2785 break;
2789 sym->tlink = NULL;
2790 sym->mark = 0;
2791 sym->new = 0;
2793 free_old_symbol (sym);
2797 /* Recursive function that deletes an entire tree and all the common
2798 head structures it points to. */
2800 static void
2801 free_common_tree (gfc_symtree * common_tree)
2803 if (common_tree == NULL)
2804 return;
2806 free_common_tree (common_tree->left);
2807 free_common_tree (common_tree->right);
2809 gfc_free (common_tree);
2813 /* Recursive function that deletes an entire tree and all the user
2814 operator nodes that it contains. */
2816 static void
2817 free_uop_tree (gfc_symtree *uop_tree)
2820 if (uop_tree == NULL)
2821 return;
2823 free_uop_tree (uop_tree->left);
2824 free_uop_tree (uop_tree->right);
2826 gfc_free_interface (uop_tree->n.uop->operator);
2828 gfc_free (uop_tree->n.uop);
2829 gfc_free (uop_tree);
2833 /* Recursive function that deletes an entire tree and all the symbols
2834 that it contains. */
2836 static void
2837 free_sym_tree (gfc_symtree *sym_tree)
2839 gfc_namespace *ns;
2840 gfc_symbol *sym;
2842 if (sym_tree == NULL)
2843 return;
2845 free_sym_tree (sym_tree->left);
2846 free_sym_tree (sym_tree->right);
2848 sym = sym_tree->n.sym;
2850 sym->refs--;
2851 if (sym->refs < 0)
2852 gfc_internal_error ("free_sym_tree(): Negative refs");
2854 if (sym->formal_ns != NULL && sym->refs == 1)
2856 /* As formal_ns contains a reference to sym, delete formal_ns just
2857 before the deletion of sym. */
2858 ns = sym->formal_ns;
2859 sym->formal_ns = NULL;
2860 gfc_free_namespace (ns);
2862 else if (sym->refs == 0)
2864 /* Go ahead and delete the symbol. */
2865 gfc_free_symbol (sym);
2868 gfc_free (sym_tree);
2872 /* Free the derived type list. */
2874 static void
2875 gfc_free_dt_list (void)
2877 gfc_dt_list *dt, *n;
2879 for (dt = gfc_derived_types; dt; dt = n)
2881 n = dt->next;
2882 gfc_free (dt);
2885 gfc_derived_types = NULL;
2889 /* Free the gfc_equiv_info's. */
2891 static void
2892 gfc_free_equiv_infos (gfc_equiv_info *s)
2894 if (s == NULL)
2895 return;
2896 gfc_free_equiv_infos (s->next);
2897 gfc_free (s);
2901 /* Free the gfc_equiv_lists. */
2903 static void
2904 gfc_free_equiv_lists (gfc_equiv_list *l)
2906 if (l == NULL)
2907 return;
2908 gfc_free_equiv_lists (l->next);
2909 gfc_free_equiv_infos (l->equiv);
2910 gfc_free (l);
2914 /* Free a finalizer procedure list. */
2916 void
2917 gfc_free_finalizer (gfc_finalizer* el)
2919 if (el)
2921 --el->procedure->refs;
2922 if (!el->procedure->refs)
2923 gfc_free_symbol (el->procedure);
2925 gfc_free (el);
2929 static void
2930 gfc_free_finalizer_list (gfc_finalizer* list)
2932 while (list)
2934 gfc_finalizer* current = list;
2935 list = list->next;
2936 gfc_free_finalizer (current);
2941 /* Free a namespace structure and everything below it. Interface
2942 lists associated with intrinsic operators are not freed. These are
2943 taken care of when a specific name is freed. */
2945 void
2946 gfc_free_namespace (gfc_namespace *ns)
2948 gfc_charlen *cl, *cl2;
2949 gfc_namespace *p, *q;
2950 gfc_intrinsic_op i;
2952 if (ns == NULL)
2953 return;
2955 ns->refs--;
2956 if (ns->refs > 0)
2957 return;
2958 gcc_assert (ns->refs == 0);
2960 gfc_free_statements (ns->code);
2962 free_sym_tree (ns->sym_root);
2963 free_uop_tree (ns->uop_root);
2964 free_common_tree (ns->common_root);
2965 gfc_free_finalizer_list (ns->finalizers);
2967 for (cl = ns->cl_list; cl; cl = cl2)
2969 cl2 = cl->next;
2970 gfc_free_expr (cl->length);
2971 gfc_free (cl);
2974 free_st_labels (ns->st_labels);
2976 gfc_free_equiv (ns->equiv);
2977 gfc_free_equiv_lists (ns->equiv_lists);
2979 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2980 gfc_free_interface (ns->operator[i]);
2982 gfc_free_data (ns->data);
2983 p = ns->contained;
2984 gfc_free (ns);
2986 /* Recursively free any contained namespaces. */
2987 while (p != NULL)
2989 q = p;
2990 p = p->sibling;
2991 gfc_free_namespace (q);
2996 void
2997 gfc_symbol_init_2 (void)
3000 gfc_current_ns = gfc_get_namespace (NULL, 0);
3004 void
3005 gfc_symbol_done_2 (void)
3008 gfc_free_namespace (gfc_current_ns);
3009 gfc_current_ns = NULL;
3010 gfc_free_dt_list ();
3014 /* Clear mark bits from symbol nodes associated with a symtree node. */
3016 static void
3017 clear_sym_mark (gfc_symtree *st)
3020 st->n.sym->mark = 0;
3024 /* Recursively traverse the symtree nodes. */
3026 void
3027 gfc_traverse_symtree (gfc_symtree *st, void (*func) (gfc_symtree *))
3029 if (!st)
3030 return;
3032 gfc_traverse_symtree (st->left, func);
3033 (*func) (st);
3034 gfc_traverse_symtree (st->right, func);
3038 /* Recursive namespace traversal function. */
3040 static void
3041 traverse_ns (gfc_symtree *st, void (*func) (gfc_symbol *))
3044 if (st == NULL)
3045 return;
3047 traverse_ns (st->left, func);
3049 if (st->n.sym->mark == 0)
3050 (*func) (st->n.sym);
3051 st->n.sym->mark = 1;
3053 traverse_ns (st->right, func);
3057 /* Call a given function for all symbols in the namespace. We take
3058 care that each gfc_symbol node is called exactly once. */
3060 void
3061 gfc_traverse_ns (gfc_namespace *ns, void (*func) (gfc_symbol *))
3064 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
3066 traverse_ns (ns->sym_root, func);
3070 /* Return TRUE when name is the name of an intrinsic type. */
3072 bool
3073 gfc_is_intrinsic_typename (const char *name)
3075 if (strcmp (name, "integer") == 0
3076 || strcmp (name, "real") == 0
3077 || strcmp (name, "character") == 0
3078 || strcmp (name, "logical") == 0
3079 || strcmp (name, "complex") == 0
3080 || strcmp (name, "doubleprecision") == 0
3081 || strcmp (name, "doublecomplex") == 0)
3082 return true;
3083 else
3084 return false;
3088 /* Return TRUE if the symbol is an automatic variable. */
3090 static bool
3091 gfc_is_var_automatic (gfc_symbol *sym)
3093 /* Pointer and allocatable variables are never automatic. */
3094 if (sym->attr.pointer || sym->attr.allocatable)
3095 return false;
3096 /* Check for arrays with non-constant size. */
3097 if (sym->attr.dimension && sym->as
3098 && !gfc_is_compile_time_shape (sym->as))
3099 return true;
3100 /* Check for non-constant length character variables. */
3101 if (sym->ts.type == BT_CHARACTER
3102 && sym->ts.cl
3103 && !gfc_is_constant_expr (sym->ts.cl->length))
3104 return true;
3105 return false;
3108 /* Given a symbol, mark it as SAVEd if it is allowed. */
3110 static void
3111 save_symbol (gfc_symbol *sym)
3114 if (sym->attr.use_assoc)
3115 return;
3117 if (sym->attr.in_common
3118 || sym->attr.dummy
3119 || sym->attr.flavor != FL_VARIABLE)
3120 return;
3121 /* Automatic objects are not saved. */
3122 if (gfc_is_var_automatic (sym))
3123 return;
3124 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
3128 /* Mark those symbols which can be SAVEd as such. */
3130 void
3131 gfc_save_all (gfc_namespace *ns)
3134 gfc_traverse_ns (ns, save_symbol);
3138 #ifdef GFC_DEBUG
3139 /* Make sure that no changes to symbols are pending. */
3141 void
3142 gfc_symbol_state(void) {
3144 if (changed_syms != NULL)
3145 gfc_internal_error("Symbol changes still pending!");
3147 #endif
3150 /************** Global symbol handling ************/
3153 /* Search a tree for the global symbol. */
3155 gfc_gsymbol *
3156 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
3158 int c;
3160 if (symbol == NULL)
3161 return NULL;
3163 while (symbol)
3165 c = strcmp (name, symbol->name);
3166 if (!c)
3167 return symbol;
3169 symbol = (c < 0) ? symbol->left : symbol->right;
3172 return NULL;
3176 /* Compare two global symbols. Used for managing the BB tree. */
3178 static int
3179 gsym_compare (void *_s1, void *_s2)
3181 gfc_gsymbol *s1, *s2;
3183 s1 = (gfc_gsymbol *) _s1;
3184 s2 = (gfc_gsymbol *) _s2;
3185 return strcmp (s1->name, s2->name);
3189 /* Get a global symbol, creating it if it doesn't exist. */
3191 gfc_gsymbol *
3192 gfc_get_gsymbol (const char *name)
3194 gfc_gsymbol *s;
3196 s = gfc_find_gsymbol (gfc_gsym_root, name);
3197 if (s != NULL)
3198 return s;
3200 s = gfc_getmem (sizeof (gfc_gsymbol));
3201 s->type = GSYM_UNKNOWN;
3202 s->name = gfc_get_string (name);
3204 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
3206 return s;
3210 static gfc_symbol *
3211 get_iso_c_binding_dt (int sym_id)
3213 gfc_dt_list *dt_list;
3215 dt_list = gfc_derived_types;
3217 /* Loop through the derived types in the name list, searching for
3218 the desired symbol from iso_c_binding. Search the parent namespaces
3219 if necessary and requested to (parent_flag). */
3220 while (dt_list != NULL)
3222 if (dt_list->derived->from_intmod != INTMOD_NONE
3223 && dt_list->derived->intmod_sym_id == sym_id)
3224 return dt_list->derived;
3226 dt_list = dt_list->next;
3229 return NULL;
3233 /* Verifies that the given derived type symbol, derived_sym, is interoperable
3234 with C. This is necessary for any derived type that is BIND(C) and for
3235 derived types that are parameters to functions that are BIND(C). All
3236 fields of the derived type are required to be interoperable, and are tested
3237 for such. If an error occurs, the errors are reported here, allowing for
3238 multiple errors to be handled for a single derived type. */
3241 verify_bind_c_derived_type (gfc_symbol *derived_sym)
3243 gfc_component *curr_comp = NULL;
3244 try is_c_interop = FAILURE;
3245 try retval = SUCCESS;
3247 if (derived_sym == NULL)
3248 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
3249 "unexpectedly NULL");
3251 /* If we've already looked at this derived symbol, do not look at it again
3252 so we don't repeat warnings/errors. */
3253 if (derived_sym->ts.is_c_interop)
3254 return SUCCESS;
3256 /* The derived type must have the BIND attribute to be interoperable
3257 J3/04-007, Section 15.2.3. */
3258 if (derived_sym->attr.is_bind_c != 1)
3260 derived_sym->ts.is_c_interop = 0;
3261 gfc_error_now ("Derived type '%s' declared at %L must have the BIND "
3262 "attribute to be C interoperable", derived_sym->name,
3263 &(derived_sym->declared_at));
3264 retval = FAILURE;
3267 curr_comp = derived_sym->components;
3269 /* TODO: is this really an error? */
3270 if (curr_comp == NULL)
3272 gfc_error ("Derived type '%s' at %L is empty",
3273 derived_sym->name, &(derived_sym->declared_at));
3274 return FAILURE;
3277 /* Initialize the derived type as being C interoperable.
3278 If we find an error in the components, this will be set false. */
3279 derived_sym->ts.is_c_interop = 1;
3281 /* Loop through the list of components to verify that the kind of
3282 each is a C interoperable type. */
3285 /* The components cannot be pointers (fortran sense).
3286 J3/04-007, Section 15.2.3, C1505. */
3287 if (curr_comp->pointer != 0)
3289 gfc_error ("Component '%s' at %L cannot have the "
3290 "POINTER attribute because it is a member "
3291 "of the BIND(C) derived type '%s' at %L",
3292 curr_comp->name, &(curr_comp->loc),
3293 derived_sym->name, &(derived_sym->declared_at));
3294 retval = FAILURE;
3297 /* The components cannot be allocatable.
3298 J3/04-007, Section 15.2.3, C1505. */
3299 if (curr_comp->allocatable != 0)
3301 gfc_error ("Component '%s' at %L cannot have the "
3302 "ALLOCATABLE attribute because it is a member "
3303 "of the BIND(C) derived type '%s' at %L",
3304 curr_comp->name, &(curr_comp->loc),
3305 derived_sym->name, &(derived_sym->declared_at));
3306 retval = FAILURE;
3309 /* BIND(C) derived types must have interoperable components. */
3310 if (curr_comp->ts.type == BT_DERIVED
3311 && curr_comp->ts.derived->ts.is_iso_c != 1
3312 && curr_comp->ts.derived != derived_sym)
3314 /* This should be allowed; the draft says a derived-type can not
3315 have type parameters if it is has the BIND attribute. Type
3316 parameters seem to be for making parameterized derived types.
3317 There's no need to verify the type if it is c_ptr/c_funptr. */
3318 retval = verify_bind_c_derived_type (curr_comp->ts.derived);
3320 else
3322 /* Grab the typespec for the given component and test the kind. */
3323 is_c_interop = verify_c_interop (&(curr_comp->ts), curr_comp->name,
3324 &(curr_comp->loc));
3326 if (is_c_interop != SUCCESS)
3328 /* Report warning and continue since not fatal. The
3329 draft does specify a constraint that requires all fields
3330 to interoperate, but if the user says real(4), etc., it
3331 may interoperate with *something* in C, but the compiler
3332 most likely won't know exactly what. Further, it may not
3333 interoperate with the same data type(s) in C if the user
3334 recompiles with different flags (e.g., -m32 and -m64 on
3335 x86_64 and using integer(4) to claim interop with a
3336 C_LONG). */
3337 if (derived_sym->attr.is_bind_c == 1)
3338 /* If the derived type is bind(c), all fields must be
3339 interop. */
3340 gfc_warning ("Component '%s' in derived type '%s' at %L "
3341 "may not be C interoperable, even though "
3342 "derived type '%s' is BIND(C)",
3343 curr_comp->name, derived_sym->name,
3344 &(curr_comp->loc), derived_sym->name);
3345 else
3346 /* If derived type is param to bind(c) routine, or to one
3347 of the iso_c_binding procs, it must be interoperable, so
3348 all fields must interop too. */
3349 gfc_warning ("Component '%s' in derived type '%s' at %L "
3350 "may not be C interoperable",
3351 curr_comp->name, derived_sym->name,
3352 &(curr_comp->loc));
3356 curr_comp = curr_comp->next;
3357 } while (curr_comp != NULL);
3360 /* Make sure we don't have conflicts with the attributes. */
3361 if (derived_sym->attr.access == ACCESS_PRIVATE)
3363 gfc_error ("Derived type '%s' at %L cannot be declared with both "
3364 "PRIVATE and BIND(C) attributes", derived_sym->name,
3365 &(derived_sym->declared_at));
3366 retval = FAILURE;
3369 if (derived_sym->attr.sequence != 0)
3371 gfc_error ("Derived type '%s' at %L cannot have the SEQUENCE "
3372 "attribute because it is BIND(C)", derived_sym->name,
3373 &(derived_sym->declared_at));
3374 retval = FAILURE;
3377 /* Mark the derived type as not being C interoperable if we found an
3378 error. If there were only warnings, proceed with the assumption
3379 it's interoperable. */
3380 if (retval == FAILURE)
3381 derived_sym->ts.is_c_interop = 0;
3383 return retval;
3387 /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
3389 static try
3390 gen_special_c_interop_ptr (int ptr_id, const char *ptr_name,
3391 const char *module_name)
3393 gfc_symtree *tmp_symtree;
3394 gfc_symbol *tmp_sym;
3396 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, ptr_name);
3398 if (tmp_symtree != NULL)
3399 tmp_sym = tmp_symtree->n.sym;
3400 else
3402 tmp_sym = NULL;
3403 gfc_internal_error ("gen_special_c_interop_ptr(): Unable to "
3404 "create symbol for %s", ptr_name);
3407 /* Set up the symbol's important fields. Save attr required so we can
3408 initialize the ptr to NULL. */
3409 tmp_sym->attr.save = SAVE_EXPLICIT;
3410 tmp_sym->ts.is_c_interop = 1;
3411 tmp_sym->attr.is_c_interop = 1;
3412 tmp_sym->ts.is_iso_c = 1;
3413 tmp_sym->ts.type = BT_DERIVED;
3415 /* The c_ptr and c_funptr derived types will provide the
3416 definition for c_null_ptr and c_null_funptr, respectively. */
3417 if (ptr_id == ISOCBINDING_NULL_PTR)
3418 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_PTR);
3419 else
3420 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3421 if (tmp_sym->ts.derived == NULL)
3423 /* This can occur if the user forgot to declare c_ptr or
3424 c_funptr and they're trying to use one of the procedures
3425 that has arg(s) of the missing type. In this case, a
3426 regular version of the thing should have been put in the
3427 current ns. */
3428 generate_isocbinding_symbol (module_name, ptr_id == ISOCBINDING_NULL_PTR
3429 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR,
3430 (const char *) (ptr_id == ISOCBINDING_NULL_PTR
3431 ? "_gfortran_iso_c_binding_c_ptr"
3432 : "_gfortran_iso_c_binding_c_funptr"));
3434 tmp_sym->ts.derived =
3435 get_iso_c_binding_dt (ptr_id == ISOCBINDING_NULL_PTR
3436 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR);
3439 /* Module name is some mangled version of iso_c_binding. */
3440 tmp_sym->module = gfc_get_string (module_name);
3442 /* Say it's from the iso_c_binding module. */
3443 tmp_sym->attr.is_iso_c = 1;
3445 tmp_sym->attr.use_assoc = 1;
3446 tmp_sym->attr.is_bind_c = 1;
3447 /* Set the binding_label. */
3448 sprintf (tmp_sym->binding_label, "%s_%s", module_name, tmp_sym->name);
3450 /* Set the c_address field of c_null_ptr and c_null_funptr to
3451 the value of NULL. */
3452 tmp_sym->value = gfc_get_expr ();
3453 tmp_sym->value->expr_type = EXPR_STRUCTURE;
3454 tmp_sym->value->ts.type = BT_DERIVED;
3455 tmp_sym->value->ts.derived = tmp_sym->ts.derived;
3456 /* Create a constructor with no expr, that way we can recognize if the user
3457 tries to call the structure constructor for one of the iso_c_binding
3458 derived types during resolution (resolve_structure_cons). */
3459 tmp_sym->value->value.constructor = gfc_get_constructor ();
3460 /* Must declare c_null_ptr and c_null_funptr as having the
3461 PARAMETER attribute so they can be used in init expressions. */
3462 tmp_sym->attr.flavor = FL_PARAMETER;
3464 return SUCCESS;
3468 /* Add a formal argument, gfc_formal_arglist, to the
3469 end of the given list of arguments. Set the reference to the
3470 provided symbol, param_sym, in the argument. */
3472 static void
3473 add_formal_arg (gfc_formal_arglist **head,
3474 gfc_formal_arglist **tail,
3475 gfc_formal_arglist *formal_arg,
3476 gfc_symbol *param_sym)
3478 /* Put in list, either as first arg or at the tail (curr arg). */
3479 if (*head == NULL)
3480 *head = *tail = formal_arg;
3481 else
3483 (*tail)->next = formal_arg;
3484 (*tail) = formal_arg;
3487 (*tail)->sym = param_sym;
3488 (*tail)->next = NULL;
3490 return;
3494 /* Generates a symbol representing the CPTR argument to an
3495 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3496 CPTR and add it to the provided argument list. */
3498 static void
3499 gen_cptr_param (gfc_formal_arglist **head,
3500 gfc_formal_arglist **tail,
3501 const char *module_name,
3502 gfc_namespace *ns, const char *c_ptr_name,
3503 int iso_c_sym_id)
3505 gfc_symbol *param_sym = NULL;
3506 gfc_symbol *c_ptr_sym = NULL;
3507 gfc_symtree *param_symtree = NULL;
3508 gfc_formal_arglist *formal_arg = NULL;
3509 const char *c_ptr_in;
3510 const char *c_ptr_type = NULL;
3512 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3513 c_ptr_type = "_gfortran_iso_c_binding_c_funptr";
3514 else
3515 c_ptr_type = "_gfortran_iso_c_binding_c_ptr";
3517 if(c_ptr_name == NULL)
3518 c_ptr_in = "gfc_cptr__";
3519 else
3520 c_ptr_in = c_ptr_name;
3521 gfc_get_sym_tree (c_ptr_in, ns, &param_symtree);
3522 if (param_symtree != NULL)
3523 param_sym = param_symtree->n.sym;
3524 else
3525 gfc_internal_error ("gen_cptr_param(): Unable to "
3526 "create symbol for %s", c_ptr_in);
3528 /* Set up the appropriate fields for the new c_ptr param sym. */
3529 param_sym->refs++;
3530 param_sym->attr.flavor = FL_DERIVED;
3531 param_sym->ts.type = BT_DERIVED;
3532 param_sym->attr.intent = INTENT_IN;
3533 param_sym->attr.dummy = 1;
3535 /* This will pass the ptr to the iso_c routines as a (void *). */
3536 param_sym->attr.value = 1;
3537 param_sym->attr.use_assoc = 1;
3539 /* Get the symbol for c_ptr or c_funptr, no matter what it's name is
3540 (user renamed). */
3541 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3542 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3543 else
3544 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_PTR);
3545 if (c_ptr_sym == NULL)
3547 /* This can happen if the user did not define c_ptr but they are
3548 trying to use one of the iso_c_binding functions that need it. */
3549 if (iso_c_sym_id == ISOCBINDING_F_PROCPOINTER)
3550 generate_isocbinding_symbol (module_name, ISOCBINDING_FUNPTR,
3551 (const char *)c_ptr_type);
3552 else
3553 generate_isocbinding_symbol (module_name, ISOCBINDING_PTR,
3554 (const char *)c_ptr_type);
3556 gfc_get_ha_symbol (c_ptr_type, &(c_ptr_sym));
3559 param_sym->ts.derived = c_ptr_sym;
3560 param_sym->module = gfc_get_string (module_name);
3562 /* Make new formal arg. */
3563 formal_arg = gfc_get_formal_arglist ();
3564 /* Add arg to list of formal args (the CPTR arg). */
3565 add_formal_arg (head, tail, formal_arg, param_sym);
3569 /* Generates a symbol representing the FPTR argument to an
3570 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3571 FPTR and add it to the provided argument list. */
3573 static void
3574 gen_fptr_param (gfc_formal_arglist **head,
3575 gfc_formal_arglist **tail,
3576 const char *module_name,
3577 gfc_namespace *ns, const char *f_ptr_name)
3579 gfc_symbol *param_sym = NULL;
3580 gfc_symtree *param_symtree = NULL;
3581 gfc_formal_arglist *formal_arg = NULL;
3582 const char *f_ptr_out = "gfc_fptr__";
3584 if (f_ptr_name != NULL)
3585 f_ptr_out = f_ptr_name;
3587 gfc_get_sym_tree (f_ptr_out, ns, &param_symtree);
3588 if (param_symtree != NULL)
3589 param_sym = param_symtree->n.sym;
3590 else
3591 gfc_internal_error ("generateFPtrParam(): Unable to "
3592 "create symbol for %s", f_ptr_out);
3594 /* Set up the necessary fields for the fptr output param sym. */
3595 param_sym->refs++;
3596 param_sym->attr.pointer = 1;
3597 param_sym->attr.dummy = 1;
3598 param_sym->attr.use_assoc = 1;
3600 /* ISO C Binding type to allow any pointer type as actual param. */
3601 param_sym->ts.type = BT_VOID;
3602 param_sym->module = gfc_get_string (module_name);
3604 /* Make the arg. */
3605 formal_arg = gfc_get_formal_arglist ();
3606 /* Add arg to list of formal args. */
3607 add_formal_arg (head, tail, formal_arg, param_sym);
3611 /* Generates a symbol representing the optional SHAPE argument for the
3612 iso_c_binding c_f_pointer() procedure. Also, create a
3613 gfc_formal_arglist for the SHAPE and add it to the provided
3614 argument list. */
3616 static void
3617 gen_shape_param (gfc_formal_arglist **head,
3618 gfc_formal_arglist **tail,
3619 const char *module_name,
3620 gfc_namespace *ns, const char *shape_param_name)
3622 gfc_symbol *param_sym = NULL;
3623 gfc_symtree *param_symtree = NULL;
3624 gfc_formal_arglist *formal_arg = NULL;
3625 const char *shape_param = "gfc_shape_array__";
3626 int i;
3628 if (shape_param_name != NULL)
3629 shape_param = shape_param_name;
3631 gfc_get_sym_tree (shape_param, ns, &param_symtree);
3632 if (param_symtree != NULL)
3633 param_sym = param_symtree->n.sym;
3634 else
3635 gfc_internal_error ("generateShapeParam(): Unable to "
3636 "create symbol for %s", shape_param);
3638 /* Set up the necessary fields for the shape input param sym. */
3639 param_sym->refs++;
3640 param_sym->attr.dummy = 1;
3641 param_sym->attr.use_assoc = 1;
3643 /* Integer array, rank 1, describing the shape of the object. Make it's
3644 type BT_VOID initially so we can accept any type/kind combination of
3645 integer. During gfc_iso_c_sub_interface (resolve.c), we'll make it
3646 of BT_INTEGER type. */
3647 param_sym->ts.type = BT_VOID;
3649 /* Initialize the kind to default integer. However, it will be overridden
3650 during resolution to match the kind of the SHAPE parameter given as
3651 the actual argument (to allow for any valid integer kind). */
3652 param_sym->ts.kind = gfc_default_integer_kind;
3653 param_sym->as = gfc_get_array_spec ();
3655 /* Clear out the dimension info for the array. */
3656 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3658 param_sym->as->lower[i] = NULL;
3659 param_sym->as->upper[i] = NULL;
3661 param_sym->as->rank = 1;
3662 param_sym->as->lower[0] = gfc_int_expr (1);
3664 /* The extent is unknown until we get it. The length give us
3665 the rank the incoming pointer. */
3666 param_sym->as->type = AS_ASSUMED_SHAPE;
3668 /* The arg is also optional; it is required iff the second arg
3669 (fptr) is to an array, otherwise, it's ignored. */
3670 param_sym->attr.optional = 1;
3671 param_sym->attr.intent = INTENT_IN;
3672 param_sym->attr.dimension = 1;
3673 param_sym->module = gfc_get_string (module_name);
3675 /* Make the arg. */
3676 formal_arg = gfc_get_formal_arglist ();
3677 /* Add arg to list of formal args. */
3678 add_formal_arg (head, tail, formal_arg, param_sym);
3681 /* Add a procedure interface to the given symbol (i.e., store a
3682 reference to the list of formal arguments). */
3684 static void
3685 add_proc_interface (gfc_symbol *sym, ifsrc source,
3686 gfc_formal_arglist *formal)
3689 sym->formal = formal;
3690 sym->attr.if_source = source;
3693 /* Copy the formal args from an existing symbol, src, into a new
3694 symbol, dest. New formal args are created, and the description of
3695 each arg is set according to the existing ones. This function is
3696 used when creating procedure declaration variables from a procedure
3697 declaration statement (see match_proc_decl()) to create the formal
3698 args based on the args of a given named interface. */
3700 void
3701 copy_formal_args (gfc_symbol *dest, gfc_symbol *src)
3703 gfc_formal_arglist *head = NULL;
3704 gfc_formal_arglist *tail = NULL;
3705 gfc_formal_arglist *formal_arg = NULL;
3706 gfc_formal_arglist *curr_arg = NULL;
3707 gfc_formal_arglist *formal_prev = NULL;
3708 /* Save current namespace so we can change it for formal args. */
3709 gfc_namespace *parent_ns = gfc_current_ns;
3711 /* Create a new namespace, which will be the formal ns (namespace
3712 of the formal args). */
3713 gfc_current_ns = gfc_get_namespace (parent_ns, 0);
3714 gfc_current_ns->proc_name = dest;
3716 for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
3718 formal_arg = gfc_get_formal_arglist ();
3719 gfc_get_symbol (curr_arg->sym->name, gfc_current_ns, &(formal_arg->sym));
3721 /* May need to copy more info for the symbol. */
3722 formal_arg->sym->attr = curr_arg->sym->attr;
3723 formal_arg->sym->ts = curr_arg->sym->ts;
3724 formal_arg->sym->as = gfc_copy_array_spec (curr_arg->sym->as);
3726 /* If this isn't the first arg, set up the next ptr. For the
3727 last arg built, the formal_arg->next will never get set to
3728 anything other than NULL. */
3729 if (formal_prev != NULL)
3730 formal_prev->next = formal_arg;
3731 else
3732 formal_arg->next = NULL;
3734 formal_prev = formal_arg;
3736 /* Add arg to list of formal args. */
3737 add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
3740 /* Add the interface to the symbol. */
3741 add_proc_interface (dest, IFSRC_DECL, head);
3743 /* Store the formal namespace information. */
3744 if (dest->formal != NULL)
3745 /* The current ns should be that for the dest proc. */
3746 dest->formal_ns = gfc_current_ns;
3747 /* Restore the current namespace to what it was on entry. */
3748 gfc_current_ns = parent_ns;
3751 /* Builds the parameter list for the iso_c_binding procedure
3752 c_f_pointer or c_f_procpointer. The old_sym typically refers to a
3753 generic version of either the c_f_pointer or c_f_procpointer
3754 functions. The new_proc_sym represents a "resolved" version of the
3755 symbol. The functions are resolved to match the types of their
3756 parameters; for example, c_f_pointer(cptr, fptr) would resolve to
3757 something similar to c_f_pointer_i4 if the type of data object fptr
3758 pointed to was a default integer. The actual name of the resolved
3759 procedure symbol is further mangled with the module name, etc., but
3760 the idea holds true. */
3762 static void
3763 build_formal_args (gfc_symbol *new_proc_sym,
3764 gfc_symbol *old_sym, int add_optional_arg)
3766 gfc_formal_arglist *head = NULL, *tail = NULL;
3767 gfc_namespace *parent_ns = NULL;
3769 parent_ns = gfc_current_ns;
3770 /* Create a new namespace, which will be the formal ns (namespace
3771 of the formal args). */
3772 gfc_current_ns = gfc_get_namespace(parent_ns, 0);
3773 gfc_current_ns->proc_name = new_proc_sym;
3775 /* Generate the params. */
3776 if ((old_sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3777 (old_sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3779 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3780 gfc_current_ns, "cptr", old_sym->intmod_sym_id);
3781 gen_fptr_param (&head, &tail, (const char *) new_proc_sym->module,
3782 gfc_current_ns, "fptr");
3784 /* If we're dealing with c_f_pointer, it has an optional third arg. */
3785 if (old_sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3787 gen_shape_param (&head, &tail,
3788 (const char *) new_proc_sym->module,
3789 gfc_current_ns, "shape");
3792 else if (old_sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
3794 /* c_associated has one required arg and one optional; both
3795 are c_ptrs. */
3796 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3797 gfc_current_ns, "c_ptr_1", ISOCBINDING_ASSOCIATED);
3798 if (add_optional_arg)
3800 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3801 gfc_current_ns, "c_ptr_2", ISOCBINDING_ASSOCIATED);
3802 /* The last param is optional so mark it as such. */
3803 tail->sym->attr.optional = 1;
3807 /* Add the interface (store formal args to new_proc_sym). */
3808 add_proc_interface (new_proc_sym, IFSRC_DECL, head);
3810 /* Set up the formal_ns pointer to the one created for the
3811 new procedure so it'll get cleaned up during gfc_free_symbol(). */
3812 new_proc_sym->formal_ns = gfc_current_ns;
3814 gfc_current_ns = parent_ns;
3817 static int
3818 std_for_isocbinding_symbol (int id)
3820 switch (id)
3822 #define NAMED_INTCST(a,b,c,d) \
3823 case a:\
3824 return d;
3825 #include "iso-c-binding.def"
3826 #undef NAMED_INTCST
3827 default:
3828 return GFC_STD_F2003;
3832 /* Generate the given set of C interoperable kind objects, or all
3833 interoperable kinds. This function will only be given kind objects
3834 for valid iso_c_binding defined types because this is verified when
3835 the 'use' statement is parsed. If the user gives an 'only' clause,
3836 the specific kinds are looked up; if they don't exist, an error is
3837 reported. If the user does not give an 'only' clause, all
3838 iso_c_binding symbols are generated. If a list of specific kinds
3839 is given, it must have a NULL in the first empty spot to mark the
3840 end of the list. */
3843 void
3844 generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
3845 const char *local_name)
3847 const char *const name = (local_name && local_name[0]) ? local_name
3848 : c_interop_kinds_table[s].name;
3849 gfc_symtree *tmp_symtree = NULL;
3850 gfc_symbol *tmp_sym = NULL;
3851 gfc_dt_list **dt_list_ptr = NULL;
3852 gfc_component *tmp_comp = NULL;
3853 char comp_name[(GFC_MAX_SYMBOL_LEN * 2) + 1];
3854 int index;
3856 if (gfc_notification_std (std_for_isocbinding_symbol (s)) == FAILURE)
3857 return;
3858 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
3860 /* Already exists in this scope so don't re-add it.
3861 TODO: we should probably check that it's really the same symbol. */
3862 if (tmp_symtree != NULL)
3863 return;
3865 /* Create the sym tree in the current ns. */
3866 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree);
3867 if (tmp_symtree)
3868 tmp_sym = tmp_symtree->n.sym;
3869 else
3870 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3871 "create symbol");
3873 /* Say what module this symbol belongs to. */
3874 tmp_sym->module = gfc_get_string (mod_name);
3875 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
3876 tmp_sym->intmod_sym_id = s;
3878 switch (s)
3881 #define NAMED_INTCST(a,b,c,d) case a :
3882 #define NAMED_REALCST(a,b,c) case a :
3883 #define NAMED_CMPXCST(a,b,c) case a :
3884 #define NAMED_LOGCST(a,b,c) case a :
3885 #define NAMED_CHARKNDCST(a,b,c) case a :
3886 #include "iso-c-binding.def"
3888 tmp_sym->value = gfc_int_expr (c_interop_kinds_table[s].value);
3890 /* Initialize an integer constant expression node. */
3891 tmp_sym->attr.flavor = FL_PARAMETER;
3892 tmp_sym->ts.type = BT_INTEGER;
3893 tmp_sym->ts.kind = gfc_default_integer_kind;
3895 /* Mark this type as a C interoperable one. */
3896 tmp_sym->ts.is_c_interop = 1;
3897 tmp_sym->ts.is_iso_c = 1;
3898 tmp_sym->value->ts.is_c_interop = 1;
3899 tmp_sym->value->ts.is_iso_c = 1;
3900 tmp_sym->attr.is_c_interop = 1;
3902 /* Tell what f90 type this c interop kind is valid. */
3903 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
3905 /* Say it's from the iso_c_binding module. */
3906 tmp_sym->attr.is_iso_c = 1;
3908 /* Make it use associated. */
3909 tmp_sym->attr.use_assoc = 1;
3910 break;
3913 #define NAMED_CHARCST(a,b,c) case a :
3914 #include "iso-c-binding.def"
3916 /* Initialize an integer constant expression node for the
3917 length of the character. */
3918 tmp_sym->value = gfc_get_expr ();
3919 tmp_sym->value->expr_type = EXPR_CONSTANT;
3920 tmp_sym->value->ts.type = BT_CHARACTER;
3921 tmp_sym->value->ts.kind = gfc_default_character_kind;
3922 tmp_sym->value->where = gfc_current_locus;
3923 tmp_sym->value->ts.is_c_interop = 1;
3924 tmp_sym->value->ts.is_iso_c = 1;
3925 tmp_sym->value->value.character.length = 1;
3926 tmp_sym->value->value.character.string = gfc_get_wide_string (2);
3927 tmp_sym->value->value.character.string[0]
3928 = (gfc_char_t) c_interop_kinds_table[s].value;
3929 tmp_sym->value->value.character.string[1] = '\0';
3930 tmp_sym->ts.cl = gfc_get_charlen ();
3931 tmp_sym->ts.cl->length = gfc_int_expr (1);
3933 /* May not need this in both attr and ts, but do need in
3934 attr for writing module file. */
3935 tmp_sym->attr.is_c_interop = 1;
3937 tmp_sym->attr.flavor = FL_PARAMETER;
3938 tmp_sym->ts.type = BT_CHARACTER;
3940 /* Need to set it to the C_CHAR kind. */
3941 tmp_sym->ts.kind = gfc_default_character_kind;
3943 /* Mark this type as a C interoperable one. */
3944 tmp_sym->ts.is_c_interop = 1;
3945 tmp_sym->ts.is_iso_c = 1;
3947 /* Tell what f90 type this c interop kind is valid. */
3948 tmp_sym->ts.f90_type = BT_CHARACTER;
3950 /* Say it's from the iso_c_binding module. */
3951 tmp_sym->attr.is_iso_c = 1;
3953 /* Make it use associated. */
3954 tmp_sym->attr.use_assoc = 1;
3955 break;
3957 case ISOCBINDING_PTR:
3958 case ISOCBINDING_FUNPTR:
3960 /* Initialize an integer constant expression node. */
3961 tmp_sym->attr.flavor = FL_DERIVED;
3962 tmp_sym->ts.is_c_interop = 1;
3963 tmp_sym->attr.is_c_interop = 1;
3964 tmp_sym->attr.is_iso_c = 1;
3965 tmp_sym->ts.is_iso_c = 1;
3966 tmp_sym->ts.type = BT_DERIVED;
3968 /* A derived type must have the bind attribute to be
3969 interoperable (J3/04-007, Section 15.2.3), even though
3970 the binding label is not used. */
3971 tmp_sym->attr.is_bind_c = 1;
3973 tmp_sym->attr.referenced = 1;
3975 tmp_sym->ts.derived = tmp_sym;
3977 /* Add the symbol created for the derived type to the current ns. */
3978 dt_list_ptr = &(gfc_derived_types);
3979 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
3980 dt_list_ptr = &((*dt_list_ptr)->next);
3982 /* There is already at least one derived type in the list, so append
3983 the one we're currently building for c_ptr or c_funptr. */
3984 if (*dt_list_ptr != NULL)
3985 dt_list_ptr = &((*dt_list_ptr)->next);
3986 (*dt_list_ptr) = gfc_get_dt_list ();
3987 (*dt_list_ptr)->derived = tmp_sym;
3988 (*dt_list_ptr)->next = NULL;
3990 /* Set up the component of the derived type, which will be
3991 an integer with kind equal to c_ptr_size. Mangle the name of
3992 the field for the c_address to prevent the curious user from
3993 trying to access it from Fortran. */
3994 sprintf (comp_name, "__%s_%s", tmp_sym->name, "c_address");
3995 gfc_add_component (tmp_sym, comp_name, &tmp_comp);
3996 if (tmp_comp == NULL)
3997 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3998 "create component for c_address");
4000 tmp_comp->ts.type = BT_INTEGER;
4002 /* Set this because the module will need to read/write this field. */
4003 tmp_comp->ts.f90_type = BT_INTEGER;
4005 /* The kinds for c_ptr and c_funptr are the same. */
4006 index = get_c_kind ("c_ptr", c_interop_kinds_table);
4007 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
4009 tmp_comp->pointer = 0;
4010 tmp_comp->dimension = 0;
4012 /* Mark the component as C interoperable. */
4013 tmp_comp->ts.is_c_interop = 1;
4015 /* Make it use associated (iso_c_binding module). */
4016 tmp_sym->attr.use_assoc = 1;
4017 break;
4019 case ISOCBINDING_NULL_PTR:
4020 case ISOCBINDING_NULL_FUNPTR:
4021 gen_special_c_interop_ptr (s, name, mod_name);
4022 break;
4024 case ISOCBINDING_F_POINTER:
4025 case ISOCBINDING_ASSOCIATED:
4026 case ISOCBINDING_LOC:
4027 case ISOCBINDING_FUNLOC:
4028 case ISOCBINDING_F_PROCPOINTER:
4030 tmp_sym->attr.proc = PROC_MODULE;
4032 /* Use the procedure's name as it is in the iso_c_binding module for
4033 setting the binding label in case the user renamed the symbol. */
4034 sprintf (tmp_sym->binding_label, "%s_%s", mod_name,
4035 c_interop_kinds_table[s].name);
4036 tmp_sym->attr.is_iso_c = 1;
4037 if (s == ISOCBINDING_F_POINTER || s == ISOCBINDING_F_PROCPOINTER)
4038 tmp_sym->attr.subroutine = 1;
4039 else
4041 /* TODO! This needs to be finished more for the expr of the
4042 function or something!
4043 This may not need to be here, because trying to do c_loc
4044 as an external. */
4045 if (s == ISOCBINDING_ASSOCIATED)
4047 tmp_sym->attr.function = 1;
4048 tmp_sym->ts.type = BT_LOGICAL;
4049 tmp_sym->ts.kind = gfc_default_logical_kind;
4050 tmp_sym->result = tmp_sym;
4052 else
4054 /* Here, we're taking the simple approach. We're defining
4055 c_loc as an external identifier so the compiler will put
4056 what we expect on the stack for the address we want the
4057 C address of. */
4058 tmp_sym->ts.type = BT_DERIVED;
4059 if (s == ISOCBINDING_LOC)
4060 tmp_sym->ts.derived =
4061 get_iso_c_binding_dt (ISOCBINDING_PTR);
4062 else
4063 tmp_sym->ts.derived =
4064 get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
4066 if (tmp_sym->ts.derived == NULL)
4068 /* Create the necessary derived type so we can continue
4069 processing the file. */
4070 generate_isocbinding_symbol
4071 (mod_name, s == ISOCBINDING_FUNLOC
4072 ? ISOCBINDING_FUNPTR : ISOCBINDING_PTR,
4073 (const char *)(s == ISOCBINDING_FUNLOC
4074 ? "_gfortran_iso_c_binding_c_funptr"
4075 : "_gfortran_iso_c_binding_c_ptr"));
4076 tmp_sym->ts.derived =
4077 get_iso_c_binding_dt (s == ISOCBINDING_FUNLOC
4078 ? ISOCBINDING_FUNPTR
4079 : ISOCBINDING_PTR);
4082 /* The function result is itself (no result clause). */
4083 tmp_sym->result = tmp_sym;
4084 tmp_sym->attr.external = 1;
4085 tmp_sym->attr.use_assoc = 0;
4086 tmp_sym->attr.if_source = IFSRC_UNKNOWN;
4087 tmp_sym->attr.proc = PROC_UNKNOWN;
4091 tmp_sym->attr.flavor = FL_PROCEDURE;
4092 tmp_sym->attr.contained = 0;
4094 /* Try using this builder routine, with the new and old symbols
4095 both being the generic iso_c proc sym being created. This
4096 will create the formal args (and the new namespace for them).
4097 Don't build an arg list for c_loc because we're going to treat
4098 c_loc as an external procedure. */
4099 if (s != ISOCBINDING_LOC && s != ISOCBINDING_FUNLOC)
4100 /* The 1 says to add any optional args, if applicable. */
4101 build_formal_args (tmp_sym, tmp_sym, 1);
4103 /* Set this after setting up the symbol, to prevent error messages. */
4104 tmp_sym->attr.use_assoc = 1;
4106 /* This symbol will not be referenced directly. It will be
4107 resolved to the implementation for the given f90 kind. */
4108 tmp_sym->attr.referenced = 0;
4110 break;
4112 default:
4113 gcc_unreachable ();
4118 /* Creates a new symbol based off of an old iso_c symbol, with a new
4119 binding label. This function can be used to create a new,
4120 resolved, version of a procedure symbol for c_f_pointer or
4121 c_f_procpointer that is based on the generic symbols. A new
4122 parameter list is created for the new symbol using
4123 build_formal_args(). The add_optional_flag specifies whether the
4124 to add the optional SHAPE argument. The new symbol is
4125 returned. */
4127 gfc_symbol *
4128 get_iso_c_sym (gfc_symbol *old_sym, char *new_name,
4129 char *new_binding_label, int add_optional_arg)
4131 gfc_symtree *new_symtree = NULL;
4133 /* See if we have a symbol by that name already available, looking
4134 through any parent namespaces. */
4135 gfc_find_sym_tree (new_name, gfc_current_ns, 1, &new_symtree);
4136 if (new_symtree != NULL)
4137 /* Return the existing symbol. */
4138 return new_symtree->n.sym;
4140 /* Create the symtree/symbol, with attempted host association. */
4141 gfc_get_ha_sym_tree (new_name, &new_symtree);
4142 if (new_symtree == NULL)
4143 gfc_internal_error ("get_iso_c_sym(): Unable to create "
4144 "symtree for '%s'", new_name);
4146 /* Now fill in the fields of the resolved symbol with the old sym. */
4147 strcpy (new_symtree->n.sym->binding_label, new_binding_label);
4148 new_symtree->n.sym->attr = old_sym->attr;
4149 new_symtree->n.sym->ts = old_sym->ts;
4150 new_symtree->n.sym->module = gfc_get_string (old_sym->module);
4151 new_symtree->n.sym->from_intmod = old_sym->from_intmod;
4152 new_symtree->n.sym->intmod_sym_id = old_sym->intmod_sym_id;
4153 /* Build the formal arg list. */
4154 build_formal_args (new_symtree->n.sym, old_sym, add_optional_arg);
4156 gfc_commit_symbol (new_symtree->n.sym);
4158 return new_symtree->n.sym;