2015-07-17 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / module.c
blobdb1d33928112798b37805b08e98aeed94486d2cb
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2015 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/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "gfortran.h"
71 #include "arith.h"
72 #include "match.h"
73 #include "parse.h" /* FIXME */
74 #include "constructor.h"
75 #include "cpp.h"
76 #include "alias.h"
77 #include "tree.h"
78 #include "options.h"
79 #include "stringpool.h"
80 #include "scanner.h"
81 #include <zlib.h>
83 #define MODULE_EXTENSION ".mod"
84 #define SUBMODULE_EXTENSION ".smod"
86 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
87 recognized. */
88 #define MOD_VERSION "14"
91 /* Structure that describes a position within a module file. */
93 typedef struct
95 int column, line;
96 long pos;
98 module_locus;
100 /* Structure for list of symbols of intrinsic modules. */
101 typedef struct
103 int id;
104 const char *name;
105 int value;
106 int standard;
108 intmod_sym;
111 typedef enum
113 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
115 pointer_t;
117 /* The fixup structure lists pointers to pointers that have to
118 be updated when a pointer value becomes known. */
120 typedef struct fixup_t
122 void **pointer;
123 struct fixup_t *next;
125 fixup_t;
128 /* Structure for holding extra info needed for pointers being read. */
130 enum gfc_rsym_state
132 UNUSED,
133 NEEDED,
134 USED
137 enum gfc_wsym_state
139 UNREFERENCED = 0,
140 NEEDS_WRITE,
141 WRITTEN
144 typedef struct pointer_info
146 BBT_HEADER (pointer_info);
147 int integer;
148 pointer_t type;
150 /* The first component of each member of the union is the pointer
151 being stored. */
153 fixup_t *fixup;
155 union
157 void *pointer; /* Member for doing pointer searches. */
159 struct
161 gfc_symbol *sym;
162 char *true_name, *module, *binding_label;
163 fixup_t *stfixup;
164 gfc_symtree *symtree;
165 enum gfc_rsym_state state;
166 int ns, referenced, renamed;
167 module_locus where;
169 rsym;
171 struct
173 gfc_symbol *sym;
174 enum gfc_wsym_state state;
176 wsym;
181 pointer_info;
183 #define gfc_get_pointer_info() XCNEW (pointer_info)
186 /* Local variables */
188 /* The gzFile for the module we're reading or writing. */
189 static gzFile module_fp;
192 /* The name of the module we're reading (USE'ing) or writing. */
193 static const char *module_name;
194 /* The name of the .smod file that the submodule will write to. */
195 static const char *submodule_name;
196 static gfc_use_list *module_list;
198 /* If we're reading an intrinsic module, this is its ID. */
199 static intmod_id current_intmod;
201 /* Content of module. */
202 static char* module_content;
204 static long module_pos;
205 static int module_line, module_column, only_flag;
206 static int prev_module_line, prev_module_column;
208 static enum
209 { IO_INPUT, IO_OUTPUT }
210 iomode;
212 static gfc_use_rename *gfc_rename_list;
213 static pointer_info *pi_root;
214 static int symbol_number; /* Counter for assigning symbol numbers */
216 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
217 static bool in_load_equiv;
221 /*****************************************************************/
223 /* Pointer/integer conversion. Pointers between structures are stored
224 as integers in the module file. The next couple of subroutines
225 handle this translation for reading and writing. */
227 /* Recursively free the tree of pointer structures. */
229 static void
230 free_pi_tree (pointer_info *p)
232 if (p == NULL)
233 return;
235 if (p->fixup != NULL)
236 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
238 free_pi_tree (p->left);
239 free_pi_tree (p->right);
241 if (iomode == IO_INPUT)
243 XDELETEVEC (p->u.rsym.true_name);
244 XDELETEVEC (p->u.rsym.module);
245 XDELETEVEC (p->u.rsym.binding_label);
248 free (p);
252 /* Compare pointers when searching by pointer. Used when writing a
253 module. */
255 static int
256 compare_pointers (void *_sn1, void *_sn2)
258 pointer_info *sn1, *sn2;
260 sn1 = (pointer_info *) _sn1;
261 sn2 = (pointer_info *) _sn2;
263 if (sn1->u.pointer < sn2->u.pointer)
264 return -1;
265 if (sn1->u.pointer > sn2->u.pointer)
266 return 1;
268 return 0;
272 /* Compare integers when searching by integer. Used when reading a
273 module. */
275 static int
276 compare_integers (void *_sn1, void *_sn2)
278 pointer_info *sn1, *sn2;
280 sn1 = (pointer_info *) _sn1;
281 sn2 = (pointer_info *) _sn2;
283 if (sn1->integer < sn2->integer)
284 return -1;
285 if (sn1->integer > sn2->integer)
286 return 1;
288 return 0;
292 /* Initialize the pointer_info tree. */
294 static void
295 init_pi_tree (void)
297 compare_fn compare;
298 pointer_info *p;
300 pi_root = NULL;
301 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
303 /* Pointer 0 is the NULL pointer. */
304 p = gfc_get_pointer_info ();
305 p->u.pointer = NULL;
306 p->integer = 0;
307 p->type = P_OTHER;
309 gfc_insert_bbt (&pi_root, p, compare);
311 /* Pointer 1 is the current namespace. */
312 p = gfc_get_pointer_info ();
313 p->u.pointer = gfc_current_ns;
314 p->integer = 1;
315 p->type = P_NAMESPACE;
317 gfc_insert_bbt (&pi_root, p, compare);
319 symbol_number = 2;
323 /* During module writing, call here with a pointer to something,
324 returning the pointer_info node. */
326 static pointer_info *
327 find_pointer (void *gp)
329 pointer_info *p;
331 p = pi_root;
332 while (p != NULL)
334 if (p->u.pointer == gp)
335 break;
336 p = (gp < p->u.pointer) ? p->left : p->right;
339 return p;
343 /* Given a pointer while writing, returns the pointer_info tree node,
344 creating it if it doesn't exist. */
346 static pointer_info *
347 get_pointer (void *gp)
349 pointer_info *p;
351 p = find_pointer (gp);
352 if (p != NULL)
353 return p;
355 /* Pointer doesn't have an integer. Give it one. */
356 p = gfc_get_pointer_info ();
358 p->u.pointer = gp;
359 p->integer = symbol_number++;
361 gfc_insert_bbt (&pi_root, p, compare_pointers);
363 return p;
367 /* Given an integer during reading, find it in the pointer_info tree,
368 creating the node if not found. */
370 static pointer_info *
371 get_integer (int integer)
373 pointer_info *p, t;
374 int c;
376 t.integer = integer;
378 p = pi_root;
379 while (p != NULL)
381 c = compare_integers (&t, p);
382 if (c == 0)
383 break;
385 p = (c < 0) ? p->left : p->right;
388 if (p != NULL)
389 return p;
391 p = gfc_get_pointer_info ();
392 p->integer = integer;
393 p->u.pointer = NULL;
395 gfc_insert_bbt (&pi_root, p, compare_integers);
397 return p;
401 /* Resolve any fixups using a known pointer. */
403 static void
404 resolve_fixups (fixup_t *f, void *gp)
406 fixup_t *next;
408 for (; f; f = next)
410 next = f->next;
411 *(f->pointer) = gp;
412 free (f);
417 /* Convert a string such that it starts with a lower-case character. Used
418 to convert the symtree name of a derived-type to the symbol name or to
419 the name of the associated generic function. */
421 static const char *
422 dt_lower_string (const char *name)
424 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
425 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
426 &name[1]);
427 return gfc_get_string (name);
431 /* Convert a string such that it starts with an upper-case character. Used to
432 return the symtree-name for a derived type; the symbol name itself and the
433 symtree/symbol name of the associated generic function start with a lower-
434 case character. */
436 static const char *
437 dt_upper_string (const char *name)
439 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
440 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
441 &name[1]);
442 return gfc_get_string (name);
445 /* Call here during module reading when we know what pointer to
446 associate with an integer. Any fixups that exist are resolved at
447 this time. */
449 static void
450 associate_integer_pointer (pointer_info *p, void *gp)
452 if (p->u.pointer != NULL)
453 gfc_internal_error ("associate_integer_pointer(): Already associated");
455 p->u.pointer = gp;
457 resolve_fixups (p->fixup, gp);
459 p->fixup = NULL;
463 /* During module reading, given an integer and a pointer to a pointer,
464 either store the pointer from an already-known value or create a
465 fixup structure in order to store things later. Returns zero if
466 the reference has been actually stored, or nonzero if the reference
467 must be fixed later (i.e., associate_integer_pointer must be called
468 sometime later. Returns the pointer_info structure. */
470 static pointer_info *
471 add_fixup (int integer, void *gp)
473 pointer_info *p;
474 fixup_t *f;
475 char **cp;
477 p = get_integer (integer);
479 if (p->integer == 0 || p->u.pointer != NULL)
481 cp = (char **) gp;
482 *cp = (char *) p->u.pointer;
484 else
486 f = XCNEW (fixup_t);
488 f->next = p->fixup;
489 p->fixup = f;
491 f->pointer = (void **) gp;
494 return p;
498 /*****************************************************************/
500 /* Parser related subroutines */
502 /* Free the rename list left behind by a USE statement. */
504 static void
505 free_rename (gfc_use_rename *list)
507 gfc_use_rename *next;
509 for (; list; list = next)
511 next = list->next;
512 free (list);
517 /* Match a USE statement. */
519 match
520 gfc_match_use (void)
522 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
523 gfc_use_rename *tail = NULL, *new_use;
524 interface_type type, type2;
525 gfc_intrinsic_op op;
526 match m;
527 gfc_use_list *use_list;
529 use_list = gfc_get_use_list ();
531 if (gfc_match (" , ") == MATCH_YES)
533 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
535 if (!gfc_notify_std (GFC_STD_F2003, "module "
536 "nature in USE statement at %C"))
537 goto cleanup;
539 if (strcmp (module_nature, "intrinsic") == 0)
540 use_list->intrinsic = true;
541 else
543 if (strcmp (module_nature, "non_intrinsic") == 0)
544 use_list->non_intrinsic = true;
545 else
547 gfc_error ("Module nature in USE statement at %C shall "
548 "be either INTRINSIC or NON_INTRINSIC");
549 goto cleanup;
553 else
555 /* Help output a better error message than "Unclassifiable
556 statement". */
557 gfc_match (" %n", module_nature);
558 if (strcmp (module_nature, "intrinsic") == 0
559 || strcmp (module_nature, "non_intrinsic") == 0)
560 gfc_error ("\"::\" was expected after module nature at %C "
561 "but was not found");
562 free (use_list);
563 return m;
566 else
568 m = gfc_match (" ::");
569 if (m == MATCH_YES &&
570 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
571 goto cleanup;
573 if (m != MATCH_YES)
575 m = gfc_match ("% ");
576 if (m != MATCH_YES)
578 free (use_list);
579 return m;
584 use_list->where = gfc_current_locus;
586 m = gfc_match_name (name);
587 if (m != MATCH_YES)
589 free (use_list);
590 return m;
593 use_list->module_name = gfc_get_string (name);
595 if (gfc_match_eos () == MATCH_YES)
596 goto done;
598 if (gfc_match_char (',') != MATCH_YES)
599 goto syntax;
601 if (gfc_match (" only :") == MATCH_YES)
602 use_list->only_flag = true;
604 if (gfc_match_eos () == MATCH_YES)
605 goto done;
607 for (;;)
609 /* Get a new rename struct and add it to the rename list. */
610 new_use = gfc_get_use_rename ();
611 new_use->where = gfc_current_locus;
612 new_use->found = 0;
614 if (use_list->rename == NULL)
615 use_list->rename = new_use;
616 else
617 tail->next = new_use;
618 tail = new_use;
620 /* See what kind of interface we're dealing with. Assume it is
621 not an operator. */
622 new_use->op = INTRINSIC_NONE;
623 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
624 goto cleanup;
626 switch (type)
628 case INTERFACE_NAMELESS:
629 gfc_error ("Missing generic specification in USE statement at %C");
630 goto cleanup;
632 case INTERFACE_USER_OP:
633 case INTERFACE_GENERIC:
634 m = gfc_match (" =>");
636 if (type == INTERFACE_USER_OP && m == MATCH_YES
637 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
638 "operators in USE statements at %C")))
639 goto cleanup;
641 if (type == INTERFACE_USER_OP)
642 new_use->op = INTRINSIC_USER;
644 if (use_list->only_flag)
646 if (m != MATCH_YES)
647 strcpy (new_use->use_name, name);
648 else
650 strcpy (new_use->local_name, name);
651 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
652 if (type != type2)
653 goto syntax;
654 if (m == MATCH_NO)
655 goto syntax;
656 if (m == MATCH_ERROR)
657 goto cleanup;
660 else
662 if (m != MATCH_YES)
663 goto syntax;
664 strcpy (new_use->local_name, name);
666 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
667 if (type != type2)
668 goto syntax;
669 if (m == MATCH_NO)
670 goto syntax;
671 if (m == MATCH_ERROR)
672 goto cleanup;
675 if (strcmp (new_use->use_name, use_list->module_name) == 0
676 || strcmp (new_use->local_name, use_list->module_name) == 0)
678 gfc_error ("The name %qs at %C has already been used as "
679 "an external module name.", use_list->module_name);
680 goto cleanup;
682 break;
684 case INTERFACE_INTRINSIC_OP:
685 new_use->op = op;
686 break;
688 default:
689 gcc_unreachable ();
692 if (gfc_match_eos () == MATCH_YES)
693 break;
694 if (gfc_match_char (',') != MATCH_YES)
695 goto syntax;
698 done:
699 if (module_list)
701 gfc_use_list *last = module_list;
702 while (last->next)
703 last = last->next;
704 last->next = use_list;
706 else
707 module_list = use_list;
709 return MATCH_YES;
711 syntax:
712 gfc_syntax_error (ST_USE);
714 cleanup:
715 free_rename (use_list->rename);
716 free (use_list);
717 return MATCH_ERROR;
721 /* Match a SUBMODULE statement.
723 According to F2008:11.2.3.2, "The submodule identifier is the
724 ordered pair whose first element is the ancestor module name and
725 whose second element is the submodule name. 'Submodule_name' is
726 used for the submodule filename and uses '@' as a separator, whilst
727 the name of the symbol for the module uses '.' as a a separator.
728 The reasons for these choices are:
729 (i) To follow another leading brand in the submodule filenames;
730 (ii) Since '.' is not particularly visible in the filenames; and
731 (iii) The linker does not permit '@' in mnemonics. */
733 match
734 gfc_match_submodule (void)
736 match m;
737 char name[GFC_MAX_SYMBOL_LEN + 1];
738 gfc_use_list *use_list;
740 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
741 return MATCH_ERROR;
743 gfc_new_block = NULL;
744 gcc_assert (module_list == NULL);
746 if (gfc_match_char ('(') != MATCH_YES)
747 goto syntax;
749 while (1)
751 m = gfc_match (" %n", name);
752 if (m != MATCH_YES)
753 goto syntax;
755 use_list = gfc_get_use_list ();
756 use_list->where = gfc_current_locus;
758 if (module_list)
760 gfc_use_list *last = module_list;
761 while (last->next)
762 last = last->next;
763 last->next = use_list;
764 use_list->module_name
765 = gfc_get_string ("%s.%s", module_list->module_name, name);
766 use_list->submodule_name
767 = gfc_get_string ("%s@%s", module_list->module_name, name);
769 else
771 module_list = use_list;
772 use_list->module_name = gfc_get_string (name);
773 use_list->submodule_name = use_list->module_name;
776 if (gfc_match_char (')') == MATCH_YES)
777 break;
779 if (gfc_match_char (':') != MATCH_YES)
780 goto syntax;
783 m = gfc_match (" %s%t", &gfc_new_block);
784 if (m != MATCH_YES)
785 goto syntax;
787 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
788 gfc_new_block->name);
790 gfc_new_block->name = gfc_get_string ("%s.%s",
791 module_list->module_name,
792 gfc_new_block->name);
794 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
795 gfc_new_block->name, NULL))
796 return MATCH_ERROR;
798 /* Just retain the ultimate .(s)mod file for reading, since it
799 contains all the information in its ancestors. */
800 use_list = module_list;
801 for (; module_list->next; use_list = use_list->next)
803 module_list = use_list->next;
804 free (use_list);
807 return MATCH_YES;
809 syntax:
810 gfc_error ("Syntax error in SUBMODULE statement at %C");
811 return MATCH_ERROR;
815 /* Given a name and a number, inst, return the inst name
816 under which to load this symbol. Returns NULL if this
817 symbol shouldn't be loaded. If inst is zero, returns
818 the number of instances of this name. If interface is
819 true, a user-defined operator is sought, otherwise only
820 non-operators are sought. */
822 static const char *
823 find_use_name_n (const char *name, int *inst, bool interface)
825 gfc_use_rename *u;
826 const char *low_name = NULL;
827 int i;
829 /* For derived types. */
830 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
831 low_name = dt_lower_string (name);
833 i = 0;
834 for (u = gfc_rename_list; u; u = u->next)
836 if ((!low_name && strcmp (u->use_name, name) != 0)
837 || (low_name && strcmp (u->use_name, low_name) != 0)
838 || (u->op == INTRINSIC_USER && !interface)
839 || (u->op != INTRINSIC_USER && interface))
840 continue;
841 if (++i == *inst)
842 break;
845 if (!*inst)
847 *inst = i;
848 return NULL;
851 if (u == NULL)
852 return only_flag ? NULL : name;
854 u->found = 1;
856 if (low_name)
858 if (u->local_name[0] == '\0')
859 return name;
860 return dt_upper_string (u->local_name);
863 return (u->local_name[0] != '\0') ? u->local_name : name;
867 /* Given a name, return the name under which to load this symbol.
868 Returns NULL if this symbol shouldn't be loaded. */
870 static const char *
871 find_use_name (const char *name, bool interface)
873 int i = 1;
874 return find_use_name_n (name, &i, interface);
878 /* Given a real name, return the number of use names associated with it. */
880 static int
881 number_use_names (const char *name, bool interface)
883 int i = 0;
884 find_use_name_n (name, &i, interface);
885 return i;
889 /* Try to find the operator in the current list. */
891 static gfc_use_rename *
892 find_use_operator (gfc_intrinsic_op op)
894 gfc_use_rename *u;
896 for (u = gfc_rename_list; u; u = u->next)
897 if (u->op == op)
898 return u;
900 return NULL;
904 /*****************************************************************/
906 /* The next couple of subroutines maintain a tree used to avoid a
907 brute-force search for a combination of true name and module name.
908 While symtree names, the name that a particular symbol is known by
909 can changed with USE statements, we still have to keep track of the
910 true names to generate the correct reference, and also avoid
911 loading the same real symbol twice in a program unit.
913 When we start reading, the true name tree is built and maintained
914 as symbols are read. The tree is searched as we load new symbols
915 to see if it already exists someplace in the namespace. */
917 typedef struct true_name
919 BBT_HEADER (true_name);
920 const char *name;
921 gfc_symbol *sym;
923 true_name;
925 static true_name *true_name_root;
928 /* Compare two true_name structures. */
930 static int
931 compare_true_names (void *_t1, void *_t2)
933 true_name *t1, *t2;
934 int c;
936 t1 = (true_name *) _t1;
937 t2 = (true_name *) _t2;
939 c = ((t1->sym->module > t2->sym->module)
940 - (t1->sym->module < t2->sym->module));
941 if (c != 0)
942 return c;
944 return strcmp (t1->name, t2->name);
948 /* Given a true name, search the true name tree to see if it exists
949 within the main namespace. */
951 static gfc_symbol *
952 find_true_name (const char *name, const char *module)
954 true_name t, *p;
955 gfc_symbol sym;
956 int c;
958 t.name = gfc_get_string (name);
959 if (module != NULL)
960 sym.module = gfc_get_string (module);
961 else
962 sym.module = NULL;
963 t.sym = &sym;
965 p = true_name_root;
966 while (p != NULL)
968 c = compare_true_names ((void *) (&t), (void *) p);
969 if (c == 0)
970 return p->sym;
972 p = (c < 0) ? p->left : p->right;
975 return NULL;
979 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
981 static void
982 add_true_name (gfc_symbol *sym)
984 true_name *t;
986 t = XCNEW (true_name);
987 t->sym = sym;
988 if (sym->attr.flavor == FL_DERIVED)
989 t->name = dt_upper_string (sym->name);
990 else
991 t->name = sym->name;
993 gfc_insert_bbt (&true_name_root, t, compare_true_names);
997 /* Recursive function to build the initial true name tree by
998 recursively traversing the current namespace. */
1000 static void
1001 build_tnt (gfc_symtree *st)
1003 const char *name;
1004 if (st == NULL)
1005 return;
1007 build_tnt (st->left);
1008 build_tnt (st->right);
1010 if (st->n.sym->attr.flavor == FL_DERIVED)
1011 name = dt_upper_string (st->n.sym->name);
1012 else
1013 name = st->n.sym->name;
1015 if (find_true_name (name, st->n.sym->module) != NULL)
1016 return;
1018 add_true_name (st->n.sym);
1022 /* Initialize the true name tree with the current namespace. */
1024 static void
1025 init_true_name_tree (void)
1027 true_name_root = NULL;
1028 build_tnt (gfc_current_ns->sym_root);
1032 /* Recursively free a true name tree node. */
1034 static void
1035 free_true_name (true_name *t)
1037 if (t == NULL)
1038 return;
1039 free_true_name (t->left);
1040 free_true_name (t->right);
1042 free (t);
1046 /*****************************************************************/
1048 /* Module reading and writing. */
1050 /* The following are versions similar to the ones in scanner.c, but
1051 for dealing with compressed module files. */
1053 static gzFile
1054 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1055 bool module, bool system)
1057 char *fullname;
1058 gfc_directorylist *p;
1059 gzFile f;
1061 for (p = list; p; p = p->next)
1063 if (module && !p->use_for_modules)
1064 continue;
1066 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
1067 strcpy (fullname, p->path);
1068 strcat (fullname, name);
1070 f = gzopen (fullname, "r");
1071 if (f != NULL)
1073 if (gfc_cpp_makedep ())
1074 gfc_cpp_add_dep (fullname, system);
1076 return f;
1080 return NULL;
1083 static gzFile
1084 gzopen_included_file (const char *name, bool include_cwd, bool module)
1086 gzFile f = NULL;
1088 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1090 f = gzopen (name, "r");
1091 if (f && gfc_cpp_makedep ())
1092 gfc_cpp_add_dep (name, false);
1095 if (!f)
1096 f = gzopen_included_file_1 (name, include_dirs, module, false);
1098 return f;
1101 static gzFile
1102 gzopen_intrinsic_module (const char* name)
1104 gzFile f = NULL;
1106 if (IS_ABSOLUTE_PATH (name))
1108 f = gzopen (name, "r");
1109 if (f && gfc_cpp_makedep ())
1110 gfc_cpp_add_dep (name, true);
1113 if (!f)
1114 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1116 return f;
1120 typedef enum
1122 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1124 atom_type;
1126 static atom_type last_atom;
1129 /* The name buffer must be at least as long as a symbol name. Right
1130 now it's not clear how we're going to store numeric constants--
1131 probably as a hexadecimal string, since this will allow the exact
1132 number to be preserved (this can't be done by a decimal
1133 representation). Worry about that later. TODO! */
1135 #define MAX_ATOM_SIZE 100
1137 static int atom_int;
1138 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1141 /* Report problems with a module. Error reporting is not very
1142 elaborate, since this sorts of errors shouldn't really happen.
1143 This subroutine never returns. */
1145 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1147 static void
1148 bad_module (const char *msgid)
1150 XDELETEVEC (module_content);
1151 module_content = NULL;
1153 switch (iomode)
1155 case IO_INPUT:
1156 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1157 module_name, module_line, module_column, msgid);
1158 break;
1159 case IO_OUTPUT:
1160 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1161 module_name, module_line, module_column, msgid);
1162 break;
1163 default:
1164 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1165 module_name, module_line, module_column, msgid);
1166 break;
1171 /* Set the module's input pointer. */
1173 static void
1174 set_module_locus (module_locus *m)
1176 module_column = m->column;
1177 module_line = m->line;
1178 module_pos = m->pos;
1182 /* Get the module's input pointer so that we can restore it later. */
1184 static void
1185 get_module_locus (module_locus *m)
1187 m->column = module_column;
1188 m->line = module_line;
1189 m->pos = module_pos;
1193 /* Get the next character in the module, updating our reckoning of
1194 where we are. */
1196 static int
1197 module_char (void)
1199 const char c = module_content[module_pos++];
1200 if (c == '\0')
1201 bad_module ("Unexpected EOF");
1203 prev_module_line = module_line;
1204 prev_module_column = module_column;
1206 if (c == '\n')
1208 module_line++;
1209 module_column = 0;
1212 module_column++;
1213 return c;
1216 /* Unget a character while remembering the line and column. Works for
1217 a single character only. */
1219 static void
1220 module_unget_char (void)
1222 module_line = prev_module_line;
1223 module_column = prev_module_column;
1224 module_pos--;
1227 /* Parse a string constant. The delimiter is guaranteed to be a
1228 single quote. */
1230 static void
1231 parse_string (void)
1233 int c;
1234 size_t cursz = 30;
1235 size_t len = 0;
1237 atom_string = XNEWVEC (char, cursz);
1239 for ( ; ; )
1241 c = module_char ();
1243 if (c == '\'')
1245 int c2 = module_char ();
1246 if (c2 != '\'')
1248 module_unget_char ();
1249 break;
1253 if (len >= cursz)
1255 cursz *= 2;
1256 atom_string = XRESIZEVEC (char, atom_string, cursz);
1258 atom_string[len] = c;
1259 len++;
1262 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1263 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1267 /* Parse a small integer. */
1269 static void
1270 parse_integer (int c)
1272 atom_int = c - '0';
1274 for (;;)
1276 c = module_char ();
1277 if (!ISDIGIT (c))
1279 module_unget_char ();
1280 break;
1283 atom_int = 10 * atom_int + c - '0';
1284 if (atom_int > 99999999)
1285 bad_module ("Integer overflow");
1291 /* Parse a name. */
1293 static void
1294 parse_name (int c)
1296 char *p;
1297 int len;
1299 p = atom_name;
1301 *p++ = c;
1302 len = 1;
1304 for (;;)
1306 c = module_char ();
1307 if (!ISALNUM (c) && c != '_' && c != '-')
1309 module_unget_char ();
1310 break;
1313 *p++ = c;
1314 if (++len > GFC_MAX_SYMBOL_LEN)
1315 bad_module ("Name too long");
1318 *p = '\0';
1323 /* Read the next atom in the module's input stream. */
1325 static atom_type
1326 parse_atom (void)
1328 int c;
1332 c = module_char ();
1334 while (c == ' ' || c == '\r' || c == '\n');
1336 switch (c)
1338 case '(':
1339 return ATOM_LPAREN;
1341 case ')':
1342 return ATOM_RPAREN;
1344 case '\'':
1345 parse_string ();
1346 return ATOM_STRING;
1348 case '0':
1349 case '1':
1350 case '2':
1351 case '3':
1352 case '4':
1353 case '5':
1354 case '6':
1355 case '7':
1356 case '8':
1357 case '9':
1358 parse_integer (c);
1359 return ATOM_INTEGER;
1361 case 'a':
1362 case 'b':
1363 case 'c':
1364 case 'd':
1365 case 'e':
1366 case 'f':
1367 case 'g':
1368 case 'h':
1369 case 'i':
1370 case 'j':
1371 case 'k':
1372 case 'l':
1373 case 'm':
1374 case 'n':
1375 case 'o':
1376 case 'p':
1377 case 'q':
1378 case 'r':
1379 case 's':
1380 case 't':
1381 case 'u':
1382 case 'v':
1383 case 'w':
1384 case 'x':
1385 case 'y':
1386 case 'z':
1387 case 'A':
1388 case 'B':
1389 case 'C':
1390 case 'D':
1391 case 'E':
1392 case 'F':
1393 case 'G':
1394 case 'H':
1395 case 'I':
1396 case 'J':
1397 case 'K':
1398 case 'L':
1399 case 'M':
1400 case 'N':
1401 case 'O':
1402 case 'P':
1403 case 'Q':
1404 case 'R':
1405 case 'S':
1406 case 'T':
1407 case 'U':
1408 case 'V':
1409 case 'W':
1410 case 'X':
1411 case 'Y':
1412 case 'Z':
1413 parse_name (c);
1414 return ATOM_NAME;
1416 default:
1417 bad_module ("Bad name");
1420 /* Not reached. */
1424 /* Peek at the next atom on the input. */
1426 static atom_type
1427 peek_atom (void)
1429 int c;
1433 c = module_char ();
1435 while (c == ' ' || c == '\r' || c == '\n');
1437 switch (c)
1439 case '(':
1440 module_unget_char ();
1441 return ATOM_LPAREN;
1443 case ')':
1444 module_unget_char ();
1445 return ATOM_RPAREN;
1447 case '\'':
1448 module_unget_char ();
1449 return ATOM_STRING;
1451 case '0':
1452 case '1':
1453 case '2':
1454 case '3':
1455 case '4':
1456 case '5':
1457 case '6':
1458 case '7':
1459 case '8':
1460 case '9':
1461 module_unget_char ();
1462 return ATOM_INTEGER;
1464 case 'a':
1465 case 'b':
1466 case 'c':
1467 case 'd':
1468 case 'e':
1469 case 'f':
1470 case 'g':
1471 case 'h':
1472 case 'i':
1473 case 'j':
1474 case 'k':
1475 case 'l':
1476 case 'm':
1477 case 'n':
1478 case 'o':
1479 case 'p':
1480 case 'q':
1481 case 'r':
1482 case 's':
1483 case 't':
1484 case 'u':
1485 case 'v':
1486 case 'w':
1487 case 'x':
1488 case 'y':
1489 case 'z':
1490 case 'A':
1491 case 'B':
1492 case 'C':
1493 case 'D':
1494 case 'E':
1495 case 'F':
1496 case 'G':
1497 case 'H':
1498 case 'I':
1499 case 'J':
1500 case 'K':
1501 case 'L':
1502 case 'M':
1503 case 'N':
1504 case 'O':
1505 case 'P':
1506 case 'Q':
1507 case 'R':
1508 case 'S':
1509 case 'T':
1510 case 'U':
1511 case 'V':
1512 case 'W':
1513 case 'X':
1514 case 'Y':
1515 case 'Z':
1516 module_unget_char ();
1517 return ATOM_NAME;
1519 default:
1520 bad_module ("Bad name");
1525 /* Read the next atom from the input, requiring that it be a
1526 particular kind. */
1528 static void
1529 require_atom (atom_type type)
1531 atom_type t;
1532 const char *p;
1533 int column, line;
1535 column = module_column;
1536 line = module_line;
1538 t = parse_atom ();
1539 if (t != type)
1541 switch (type)
1543 case ATOM_NAME:
1544 p = _("Expected name");
1545 break;
1546 case ATOM_LPAREN:
1547 p = _("Expected left parenthesis");
1548 break;
1549 case ATOM_RPAREN:
1550 p = _("Expected right parenthesis");
1551 break;
1552 case ATOM_INTEGER:
1553 p = _("Expected integer");
1554 break;
1555 case ATOM_STRING:
1556 p = _("Expected string");
1557 break;
1558 default:
1559 gfc_internal_error ("require_atom(): bad atom type required");
1562 module_column = column;
1563 module_line = line;
1564 bad_module (p);
1569 /* Given a pointer to an mstring array, require that the current input
1570 be one of the strings in the array. We return the enum value. */
1572 static int
1573 find_enum (const mstring *m)
1575 int i;
1577 i = gfc_string2code (m, atom_name);
1578 if (i >= 0)
1579 return i;
1581 bad_module ("find_enum(): Enum not found");
1583 /* Not reached. */
1587 /* Read a string. The caller is responsible for freeing. */
1589 static char*
1590 read_string (void)
1592 char* p;
1593 require_atom (ATOM_STRING);
1594 p = atom_string;
1595 atom_string = NULL;
1596 return p;
1600 /**************** Module output subroutines ***************************/
1602 /* Output a character to a module file. */
1604 static void
1605 write_char (char out)
1607 if (gzputc (module_fp, out) == EOF)
1608 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1610 if (out != '\n')
1611 module_column++;
1612 else
1614 module_column = 1;
1615 module_line++;
1620 /* Write an atom to a module. The line wrapping isn't perfect, but it
1621 should work most of the time. This isn't that big of a deal, since
1622 the file really isn't meant to be read by people anyway. */
1624 static void
1625 write_atom (atom_type atom, const void *v)
1627 char buffer[20];
1629 /* Workaround -Wmaybe-uninitialized false positive during
1630 profiledbootstrap by initializing them. */
1631 int i = 0, len;
1632 const char *p;
1634 switch (atom)
1636 case ATOM_STRING:
1637 case ATOM_NAME:
1638 p = (const char *) v;
1639 break;
1641 case ATOM_LPAREN:
1642 p = "(";
1643 break;
1645 case ATOM_RPAREN:
1646 p = ")";
1647 break;
1649 case ATOM_INTEGER:
1650 i = *((const int *) v);
1651 if (i < 0)
1652 gfc_internal_error ("write_atom(): Writing negative integer");
1654 sprintf (buffer, "%d", i);
1655 p = buffer;
1656 break;
1658 default:
1659 gfc_internal_error ("write_atom(): Trying to write dab atom");
1663 if(p == NULL || *p == '\0')
1664 len = 0;
1665 else
1666 len = strlen (p);
1668 if (atom != ATOM_RPAREN)
1670 if (module_column + len > 72)
1671 write_char ('\n');
1672 else
1675 if (last_atom != ATOM_LPAREN && module_column != 1)
1676 write_char (' ');
1680 if (atom == ATOM_STRING)
1681 write_char ('\'');
1683 while (p != NULL && *p)
1685 if (atom == ATOM_STRING && *p == '\'')
1686 write_char ('\'');
1687 write_char (*p++);
1690 if (atom == ATOM_STRING)
1691 write_char ('\'');
1693 last_atom = atom;
1698 /***************** Mid-level I/O subroutines *****************/
1700 /* These subroutines let their caller read or write atoms without
1701 caring about which of the two is actually happening. This lets a
1702 subroutine concentrate on the actual format of the data being
1703 written. */
1705 static void mio_expr (gfc_expr **);
1706 pointer_info *mio_symbol_ref (gfc_symbol **);
1707 pointer_info *mio_interface_rest (gfc_interface **);
1708 static void mio_symtree_ref (gfc_symtree **);
1710 /* Read or write an enumerated value. On writing, we return the input
1711 value for the convenience of callers. We avoid using an integer
1712 pointer because enums are sometimes inside bitfields. */
1714 static int
1715 mio_name (int t, const mstring *m)
1717 if (iomode == IO_OUTPUT)
1718 write_atom (ATOM_NAME, gfc_code2string (m, t));
1719 else
1721 require_atom (ATOM_NAME);
1722 t = find_enum (m);
1725 return t;
1728 /* Specialization of mio_name. */
1730 #define DECL_MIO_NAME(TYPE) \
1731 static inline TYPE \
1732 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1734 return (TYPE) mio_name ((int) t, m); \
1736 #define MIO_NAME(TYPE) mio_name_##TYPE
1738 static void
1739 mio_lparen (void)
1741 if (iomode == IO_OUTPUT)
1742 write_atom (ATOM_LPAREN, NULL);
1743 else
1744 require_atom (ATOM_LPAREN);
1748 static void
1749 mio_rparen (void)
1751 if (iomode == IO_OUTPUT)
1752 write_atom (ATOM_RPAREN, NULL);
1753 else
1754 require_atom (ATOM_RPAREN);
1758 static void
1759 mio_integer (int *ip)
1761 if (iomode == IO_OUTPUT)
1762 write_atom (ATOM_INTEGER, ip);
1763 else
1765 require_atom (ATOM_INTEGER);
1766 *ip = atom_int;
1771 /* Read or write a gfc_intrinsic_op value. */
1773 static void
1774 mio_intrinsic_op (gfc_intrinsic_op* op)
1776 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1777 if (iomode == IO_OUTPUT)
1779 int converted = (int) *op;
1780 write_atom (ATOM_INTEGER, &converted);
1782 else
1784 require_atom (ATOM_INTEGER);
1785 *op = (gfc_intrinsic_op) atom_int;
1790 /* Read or write a character pointer that points to a string on the heap. */
1792 static const char *
1793 mio_allocated_string (const char *s)
1795 if (iomode == IO_OUTPUT)
1797 write_atom (ATOM_STRING, s);
1798 return s;
1800 else
1802 require_atom (ATOM_STRING);
1803 return atom_string;
1808 /* Functions for quoting and unquoting strings. */
1810 static char *
1811 quote_string (const gfc_char_t *s, const size_t slength)
1813 const gfc_char_t *p;
1814 char *res, *q;
1815 size_t len = 0, i;
1817 /* Calculate the length we'll need: a backslash takes two ("\\"),
1818 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1819 for (p = s, i = 0; i < slength; p++, i++)
1821 if (*p == '\\')
1822 len += 2;
1823 else if (!gfc_wide_is_printable (*p))
1824 len += 10;
1825 else
1826 len++;
1829 q = res = XCNEWVEC (char, len + 1);
1830 for (p = s, i = 0; i < slength; p++, i++)
1832 if (*p == '\\')
1833 *q++ = '\\', *q++ = '\\';
1834 else if (!gfc_wide_is_printable (*p))
1836 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1837 (unsigned HOST_WIDE_INT) *p);
1838 q += 10;
1840 else
1841 *q++ = (unsigned char) *p;
1844 res[len] = '\0';
1845 return res;
1848 static gfc_char_t *
1849 unquote_string (const char *s)
1851 size_t len, i;
1852 const char *p;
1853 gfc_char_t *res;
1855 for (p = s, len = 0; *p; p++, len++)
1857 if (*p != '\\')
1858 continue;
1860 if (p[1] == '\\')
1861 p++;
1862 else if (p[1] == 'U')
1863 p += 9; /* That is a "\U????????". */
1864 else
1865 gfc_internal_error ("unquote_string(): got bad string");
1868 res = gfc_get_wide_string (len + 1);
1869 for (i = 0, p = s; i < len; i++, p++)
1871 gcc_assert (*p);
1873 if (*p != '\\')
1874 res[i] = (unsigned char) *p;
1875 else if (p[1] == '\\')
1877 res[i] = (unsigned char) '\\';
1878 p++;
1880 else
1882 /* We read the 8-digits hexadecimal constant that follows. */
1883 int j;
1884 unsigned n;
1885 gfc_char_t c = 0;
1887 gcc_assert (p[1] == 'U');
1888 for (j = 0; j < 8; j++)
1890 c = c << 4;
1891 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1892 c += n;
1895 res[i] = c;
1896 p += 9;
1900 res[len] = '\0';
1901 return res;
1905 /* Read or write a character pointer that points to a wide string on the
1906 heap, performing quoting/unquoting of nonprintable characters using the
1907 form \U???????? (where each ? is a hexadecimal digit).
1908 Length is the length of the string, only known and used in output mode. */
1910 static const gfc_char_t *
1911 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1913 if (iomode == IO_OUTPUT)
1915 char *quoted = quote_string (s, length);
1916 write_atom (ATOM_STRING, quoted);
1917 free (quoted);
1918 return s;
1920 else
1922 gfc_char_t *unquoted;
1924 require_atom (ATOM_STRING);
1925 unquoted = unquote_string (atom_string);
1926 free (atom_string);
1927 return unquoted;
1932 /* Read or write a string that is in static memory. */
1934 static void
1935 mio_pool_string (const char **stringp)
1937 /* TODO: one could write the string only once, and refer to it via a
1938 fixup pointer. */
1940 /* As a special case we have to deal with a NULL string. This
1941 happens for the 'module' member of 'gfc_symbol's that are not in a
1942 module. We read / write these as the empty string. */
1943 if (iomode == IO_OUTPUT)
1945 const char *p = *stringp == NULL ? "" : *stringp;
1946 write_atom (ATOM_STRING, p);
1948 else
1950 require_atom (ATOM_STRING);
1951 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1952 free (atom_string);
1957 /* Read or write a string that is inside of some already-allocated
1958 structure. */
1960 static void
1961 mio_internal_string (char *string)
1963 if (iomode == IO_OUTPUT)
1964 write_atom (ATOM_STRING, string);
1965 else
1967 require_atom (ATOM_STRING);
1968 strcpy (string, atom_string);
1969 free (atom_string);
1974 typedef enum
1975 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1976 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1977 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1978 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1979 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1980 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1981 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1982 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1983 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1984 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1985 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
1986 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE
1988 ab_attribute;
1990 static const mstring attr_bits[] =
1992 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1993 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1994 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1995 minit ("DIMENSION", AB_DIMENSION),
1996 minit ("CODIMENSION", AB_CODIMENSION),
1997 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1998 minit ("EXTERNAL", AB_EXTERNAL),
1999 minit ("INTRINSIC", AB_INTRINSIC),
2000 minit ("OPTIONAL", AB_OPTIONAL),
2001 minit ("POINTER", AB_POINTER),
2002 minit ("VOLATILE", AB_VOLATILE),
2003 minit ("TARGET", AB_TARGET),
2004 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2005 minit ("DUMMY", AB_DUMMY),
2006 minit ("RESULT", AB_RESULT),
2007 minit ("DATA", AB_DATA),
2008 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2009 minit ("IN_COMMON", AB_IN_COMMON),
2010 minit ("FUNCTION", AB_FUNCTION),
2011 minit ("SUBROUTINE", AB_SUBROUTINE),
2012 minit ("SEQUENCE", AB_SEQUENCE),
2013 minit ("ELEMENTAL", AB_ELEMENTAL),
2014 minit ("PURE", AB_PURE),
2015 minit ("RECURSIVE", AB_RECURSIVE),
2016 minit ("GENERIC", AB_GENERIC),
2017 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2018 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2019 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2020 minit ("IS_BIND_C", AB_IS_BIND_C),
2021 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2022 minit ("IS_ISO_C", AB_IS_ISO_C),
2023 minit ("VALUE", AB_VALUE),
2024 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2025 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2026 minit ("LOCK_COMP", AB_LOCK_COMP),
2027 minit ("POINTER_COMP", AB_POINTER_COMP),
2028 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2029 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2030 minit ("ZERO_COMP", AB_ZERO_COMP),
2031 minit ("PROTECTED", AB_PROTECTED),
2032 minit ("ABSTRACT", AB_ABSTRACT),
2033 minit ("IS_CLASS", AB_IS_CLASS),
2034 minit ("PROCEDURE", AB_PROCEDURE),
2035 minit ("PROC_POINTER", AB_PROC_POINTER),
2036 minit ("VTYPE", AB_VTYPE),
2037 minit ("VTAB", AB_VTAB),
2038 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2039 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2040 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2041 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2042 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2043 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2044 minit (NULL, -1)
2047 /* For binding attributes. */
2048 static const mstring binding_passing[] =
2050 minit ("PASS", 0),
2051 minit ("NOPASS", 1),
2052 minit (NULL, -1)
2054 static const mstring binding_overriding[] =
2056 minit ("OVERRIDABLE", 0),
2057 minit ("NON_OVERRIDABLE", 1),
2058 minit ("DEFERRED", 2),
2059 minit (NULL, -1)
2061 static const mstring binding_generic[] =
2063 minit ("SPECIFIC", 0),
2064 minit ("GENERIC", 1),
2065 minit (NULL, -1)
2067 static const mstring binding_ppc[] =
2069 minit ("NO_PPC", 0),
2070 minit ("PPC", 1),
2071 minit (NULL, -1)
2074 /* Specialization of mio_name. */
2075 DECL_MIO_NAME (ab_attribute)
2076 DECL_MIO_NAME (ar_type)
2077 DECL_MIO_NAME (array_type)
2078 DECL_MIO_NAME (bt)
2079 DECL_MIO_NAME (expr_t)
2080 DECL_MIO_NAME (gfc_access)
2081 DECL_MIO_NAME (gfc_intrinsic_op)
2082 DECL_MIO_NAME (ifsrc)
2083 DECL_MIO_NAME (save_state)
2084 DECL_MIO_NAME (procedure_type)
2085 DECL_MIO_NAME (ref_type)
2086 DECL_MIO_NAME (sym_flavor)
2087 DECL_MIO_NAME (sym_intent)
2088 #undef DECL_MIO_NAME
2090 /* Symbol attributes are stored in list with the first three elements
2091 being the enumerated fields, while the remaining elements (if any)
2092 indicate the individual attribute bits. The access field is not
2093 saved-- it controls what symbols are exported when a module is
2094 written. */
2096 static void
2097 mio_symbol_attribute (symbol_attribute *attr)
2099 atom_type t;
2100 unsigned ext_attr,extension_level;
2102 mio_lparen ();
2104 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2105 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2106 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2107 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2108 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2110 ext_attr = attr->ext_attr;
2111 mio_integer ((int *) &ext_attr);
2112 attr->ext_attr = ext_attr;
2114 extension_level = attr->extension;
2115 mio_integer ((int *) &extension_level);
2116 attr->extension = extension_level;
2118 if (iomode == IO_OUTPUT)
2120 if (attr->allocatable)
2121 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2122 if (attr->artificial)
2123 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2124 if (attr->asynchronous)
2125 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2126 if (attr->dimension)
2127 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2128 if (attr->codimension)
2129 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2130 if (attr->contiguous)
2131 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2132 if (attr->external)
2133 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2134 if (attr->intrinsic)
2135 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2136 if (attr->optional)
2137 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2138 if (attr->pointer)
2139 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2140 if (attr->class_pointer)
2141 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2142 if (attr->is_protected)
2143 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2144 if (attr->value)
2145 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2146 if (attr->volatile_)
2147 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2148 if (attr->target)
2149 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2150 if (attr->threadprivate)
2151 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2152 if (attr->dummy)
2153 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2154 if (attr->result)
2155 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2156 /* We deliberately don't preserve the "entry" flag. */
2158 if (attr->data)
2159 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2160 if (attr->in_namelist)
2161 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2162 if (attr->in_common)
2163 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2165 if (attr->function)
2166 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2167 if (attr->subroutine)
2168 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2169 if (attr->generic)
2170 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2171 if (attr->abstract)
2172 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2174 if (attr->sequence)
2175 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2176 if (attr->elemental)
2177 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2178 if (attr->pure)
2179 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2180 if (attr->implicit_pure)
2181 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2182 if (attr->unlimited_polymorphic)
2183 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2184 if (attr->recursive)
2185 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2186 if (attr->always_explicit)
2187 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2188 if (attr->cray_pointer)
2189 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2190 if (attr->cray_pointee)
2191 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2192 if (attr->is_bind_c)
2193 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2194 if (attr->is_c_interop)
2195 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2196 if (attr->is_iso_c)
2197 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2198 if (attr->alloc_comp)
2199 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2200 if (attr->pointer_comp)
2201 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2202 if (attr->proc_pointer_comp)
2203 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2204 if (attr->private_comp)
2205 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2206 if (attr->coarray_comp)
2207 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2208 if (attr->lock_comp)
2209 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2210 if (attr->zero_comp)
2211 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2212 if (attr->is_class)
2213 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2214 if (attr->procedure)
2215 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2216 if (attr->proc_pointer)
2217 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2218 if (attr->vtype)
2219 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2220 if (attr->vtab)
2221 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2222 if (attr->omp_declare_target)
2223 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2224 if (attr->array_outer_dependency)
2225 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2226 if (attr->module_procedure)
2227 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2229 mio_rparen ();
2232 else
2234 for (;;)
2236 t = parse_atom ();
2237 if (t == ATOM_RPAREN)
2238 break;
2239 if (t != ATOM_NAME)
2240 bad_module ("Expected attribute bit name");
2242 switch ((ab_attribute) find_enum (attr_bits))
2244 case AB_ALLOCATABLE:
2245 attr->allocatable = 1;
2246 break;
2247 case AB_ARTIFICIAL:
2248 attr->artificial = 1;
2249 break;
2250 case AB_ASYNCHRONOUS:
2251 attr->asynchronous = 1;
2252 break;
2253 case AB_DIMENSION:
2254 attr->dimension = 1;
2255 break;
2256 case AB_CODIMENSION:
2257 attr->codimension = 1;
2258 break;
2259 case AB_CONTIGUOUS:
2260 attr->contiguous = 1;
2261 break;
2262 case AB_EXTERNAL:
2263 attr->external = 1;
2264 break;
2265 case AB_INTRINSIC:
2266 attr->intrinsic = 1;
2267 break;
2268 case AB_OPTIONAL:
2269 attr->optional = 1;
2270 break;
2271 case AB_POINTER:
2272 attr->pointer = 1;
2273 break;
2274 case AB_CLASS_POINTER:
2275 attr->class_pointer = 1;
2276 break;
2277 case AB_PROTECTED:
2278 attr->is_protected = 1;
2279 break;
2280 case AB_VALUE:
2281 attr->value = 1;
2282 break;
2283 case AB_VOLATILE:
2284 attr->volatile_ = 1;
2285 break;
2286 case AB_TARGET:
2287 attr->target = 1;
2288 break;
2289 case AB_THREADPRIVATE:
2290 attr->threadprivate = 1;
2291 break;
2292 case AB_DUMMY:
2293 attr->dummy = 1;
2294 break;
2295 case AB_RESULT:
2296 attr->result = 1;
2297 break;
2298 case AB_DATA:
2299 attr->data = 1;
2300 break;
2301 case AB_IN_NAMELIST:
2302 attr->in_namelist = 1;
2303 break;
2304 case AB_IN_COMMON:
2305 attr->in_common = 1;
2306 break;
2307 case AB_FUNCTION:
2308 attr->function = 1;
2309 break;
2310 case AB_SUBROUTINE:
2311 attr->subroutine = 1;
2312 break;
2313 case AB_GENERIC:
2314 attr->generic = 1;
2315 break;
2316 case AB_ABSTRACT:
2317 attr->abstract = 1;
2318 break;
2319 case AB_SEQUENCE:
2320 attr->sequence = 1;
2321 break;
2322 case AB_ELEMENTAL:
2323 attr->elemental = 1;
2324 break;
2325 case AB_PURE:
2326 attr->pure = 1;
2327 break;
2328 case AB_IMPLICIT_PURE:
2329 attr->implicit_pure = 1;
2330 break;
2331 case AB_UNLIMITED_POLY:
2332 attr->unlimited_polymorphic = 1;
2333 break;
2334 case AB_RECURSIVE:
2335 attr->recursive = 1;
2336 break;
2337 case AB_ALWAYS_EXPLICIT:
2338 attr->always_explicit = 1;
2339 break;
2340 case AB_CRAY_POINTER:
2341 attr->cray_pointer = 1;
2342 break;
2343 case AB_CRAY_POINTEE:
2344 attr->cray_pointee = 1;
2345 break;
2346 case AB_IS_BIND_C:
2347 attr->is_bind_c = 1;
2348 break;
2349 case AB_IS_C_INTEROP:
2350 attr->is_c_interop = 1;
2351 break;
2352 case AB_IS_ISO_C:
2353 attr->is_iso_c = 1;
2354 break;
2355 case AB_ALLOC_COMP:
2356 attr->alloc_comp = 1;
2357 break;
2358 case AB_COARRAY_COMP:
2359 attr->coarray_comp = 1;
2360 break;
2361 case AB_LOCK_COMP:
2362 attr->lock_comp = 1;
2363 break;
2364 case AB_POINTER_COMP:
2365 attr->pointer_comp = 1;
2366 break;
2367 case AB_PROC_POINTER_COMP:
2368 attr->proc_pointer_comp = 1;
2369 break;
2370 case AB_PRIVATE_COMP:
2371 attr->private_comp = 1;
2372 break;
2373 case AB_ZERO_COMP:
2374 attr->zero_comp = 1;
2375 break;
2376 case AB_IS_CLASS:
2377 attr->is_class = 1;
2378 break;
2379 case AB_PROCEDURE:
2380 attr->procedure = 1;
2381 break;
2382 case AB_PROC_POINTER:
2383 attr->proc_pointer = 1;
2384 break;
2385 case AB_VTYPE:
2386 attr->vtype = 1;
2387 break;
2388 case AB_VTAB:
2389 attr->vtab = 1;
2390 break;
2391 case AB_OMP_DECLARE_TARGET:
2392 attr->omp_declare_target = 1;
2393 break;
2394 case AB_ARRAY_OUTER_DEPENDENCY:
2395 attr->array_outer_dependency =1;
2396 break;
2397 case AB_MODULE_PROCEDURE:
2398 attr->module_procedure =1;
2399 break;
2406 static const mstring bt_types[] = {
2407 minit ("INTEGER", BT_INTEGER),
2408 minit ("REAL", BT_REAL),
2409 minit ("COMPLEX", BT_COMPLEX),
2410 minit ("LOGICAL", BT_LOGICAL),
2411 minit ("CHARACTER", BT_CHARACTER),
2412 minit ("DERIVED", BT_DERIVED),
2413 minit ("CLASS", BT_CLASS),
2414 minit ("PROCEDURE", BT_PROCEDURE),
2415 minit ("UNKNOWN", BT_UNKNOWN),
2416 minit ("VOID", BT_VOID),
2417 minit ("ASSUMED", BT_ASSUMED),
2418 minit (NULL, -1)
2422 static void
2423 mio_charlen (gfc_charlen **clp)
2425 gfc_charlen *cl;
2427 mio_lparen ();
2429 if (iomode == IO_OUTPUT)
2431 cl = *clp;
2432 if (cl != NULL)
2433 mio_expr (&cl->length);
2435 else
2437 if (peek_atom () != ATOM_RPAREN)
2439 cl = gfc_new_charlen (gfc_current_ns, NULL);
2440 mio_expr (&cl->length);
2441 *clp = cl;
2445 mio_rparen ();
2449 /* See if a name is a generated name. */
2451 static int
2452 check_unique_name (const char *name)
2454 return *name == '@';
2458 static void
2459 mio_typespec (gfc_typespec *ts)
2461 mio_lparen ();
2463 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2465 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2466 mio_integer (&ts->kind);
2467 else
2468 mio_symbol_ref (&ts->u.derived);
2470 mio_symbol_ref (&ts->interface);
2472 /* Add info for C interop and is_iso_c. */
2473 mio_integer (&ts->is_c_interop);
2474 mio_integer (&ts->is_iso_c);
2476 /* If the typespec is for an identifier either from iso_c_binding, or
2477 a constant that was initialized to an identifier from it, use the
2478 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2479 if (ts->is_iso_c)
2480 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2481 else
2482 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2484 if (ts->type != BT_CHARACTER)
2486 /* ts->u.cl is only valid for BT_CHARACTER. */
2487 mio_lparen ();
2488 mio_rparen ();
2490 else
2491 mio_charlen (&ts->u.cl);
2493 /* So as not to disturb the existing API, use an ATOM_NAME to
2494 transmit deferred characteristic for characters (F2003). */
2495 if (iomode == IO_OUTPUT)
2497 if (ts->type == BT_CHARACTER && ts->deferred)
2498 write_atom (ATOM_NAME, "DEFERRED_CL");
2500 else if (peek_atom () != ATOM_RPAREN)
2502 if (parse_atom () != ATOM_NAME)
2503 bad_module ("Expected string");
2504 ts->deferred = 1;
2507 mio_rparen ();
2511 static const mstring array_spec_types[] = {
2512 minit ("EXPLICIT", AS_EXPLICIT),
2513 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2514 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2515 minit ("DEFERRED", AS_DEFERRED),
2516 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2517 minit (NULL, -1)
2521 static void
2522 mio_array_spec (gfc_array_spec **asp)
2524 gfc_array_spec *as;
2525 int i;
2527 mio_lparen ();
2529 if (iomode == IO_OUTPUT)
2531 int rank;
2533 if (*asp == NULL)
2534 goto done;
2535 as = *asp;
2537 /* mio_integer expects nonnegative values. */
2538 rank = as->rank > 0 ? as->rank : 0;
2539 mio_integer (&rank);
2541 else
2543 if (peek_atom () == ATOM_RPAREN)
2545 *asp = NULL;
2546 goto done;
2549 *asp = as = gfc_get_array_spec ();
2550 mio_integer (&as->rank);
2553 mio_integer (&as->corank);
2554 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2556 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2557 as->rank = -1;
2558 if (iomode == IO_INPUT && as->corank)
2559 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2561 if (as->rank + as->corank > 0)
2562 for (i = 0; i < as->rank + as->corank; i++)
2564 mio_expr (&as->lower[i]);
2565 mio_expr (&as->upper[i]);
2568 done:
2569 mio_rparen ();
2573 /* Given a pointer to an array reference structure (which lives in a
2574 gfc_ref structure), find the corresponding array specification
2575 structure. Storing the pointer in the ref structure doesn't quite
2576 work when loading from a module. Generating code for an array
2577 reference also needs more information than just the array spec. */
2579 static const mstring array_ref_types[] = {
2580 minit ("FULL", AR_FULL),
2581 minit ("ELEMENT", AR_ELEMENT),
2582 minit ("SECTION", AR_SECTION),
2583 minit (NULL, -1)
2587 static void
2588 mio_array_ref (gfc_array_ref *ar)
2590 int i;
2592 mio_lparen ();
2593 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2594 mio_integer (&ar->dimen);
2596 switch (ar->type)
2598 case AR_FULL:
2599 break;
2601 case AR_ELEMENT:
2602 for (i = 0; i < ar->dimen; i++)
2603 mio_expr (&ar->start[i]);
2605 break;
2607 case AR_SECTION:
2608 for (i = 0; i < ar->dimen; i++)
2610 mio_expr (&ar->start[i]);
2611 mio_expr (&ar->end[i]);
2612 mio_expr (&ar->stride[i]);
2615 break;
2617 case AR_UNKNOWN:
2618 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2621 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2622 we can't call mio_integer directly. Instead loop over each element
2623 and cast it to/from an integer. */
2624 if (iomode == IO_OUTPUT)
2626 for (i = 0; i < ar->dimen; i++)
2628 int tmp = (int)ar->dimen_type[i];
2629 write_atom (ATOM_INTEGER, &tmp);
2632 else
2634 for (i = 0; i < ar->dimen; i++)
2636 require_atom (ATOM_INTEGER);
2637 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2641 if (iomode == IO_INPUT)
2643 ar->where = gfc_current_locus;
2645 for (i = 0; i < ar->dimen; i++)
2646 ar->c_where[i] = gfc_current_locus;
2649 mio_rparen ();
2653 /* Saves or restores a pointer. The pointer is converted back and
2654 forth from an integer. We return the pointer_info pointer so that
2655 the caller can take additional action based on the pointer type. */
2657 static pointer_info *
2658 mio_pointer_ref (void *gp)
2660 pointer_info *p;
2662 if (iomode == IO_OUTPUT)
2664 p = get_pointer (*((char **) gp));
2665 write_atom (ATOM_INTEGER, &p->integer);
2667 else
2669 require_atom (ATOM_INTEGER);
2670 p = add_fixup (atom_int, gp);
2673 return p;
2677 /* Save and load references to components that occur within
2678 expressions. We have to describe these references by a number and
2679 by name. The number is necessary for forward references during
2680 reading, and the name is necessary if the symbol already exists in
2681 the namespace and is not loaded again. */
2683 static void
2684 mio_component_ref (gfc_component **cp)
2686 pointer_info *p;
2688 p = mio_pointer_ref (cp);
2689 if (p->type == P_UNKNOWN)
2690 p->type = P_COMPONENT;
2694 static void mio_namespace_ref (gfc_namespace **nsp);
2695 static void mio_formal_arglist (gfc_formal_arglist **formal);
2696 static void mio_typebound_proc (gfc_typebound_proc** proc);
2698 static void
2699 mio_component (gfc_component *c, int vtype)
2701 pointer_info *p;
2702 int n;
2704 mio_lparen ();
2706 if (iomode == IO_OUTPUT)
2708 p = get_pointer (c);
2709 mio_integer (&p->integer);
2711 else
2713 mio_integer (&n);
2714 p = get_integer (n);
2715 associate_integer_pointer (p, c);
2718 if (p->type == P_UNKNOWN)
2719 p->type = P_COMPONENT;
2721 mio_pool_string (&c->name);
2722 mio_typespec (&c->ts);
2723 mio_array_spec (&c->as);
2725 mio_symbol_attribute (&c->attr);
2726 if (c->ts.type == BT_CLASS)
2727 c->attr.class_ok = 1;
2728 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2730 if (!vtype || strcmp (c->name, "_final") == 0
2731 || strcmp (c->name, "_hash") == 0)
2732 mio_expr (&c->initializer);
2734 if (c->attr.proc_pointer)
2735 mio_typebound_proc (&c->tb);
2737 mio_rparen ();
2741 static void
2742 mio_component_list (gfc_component **cp, int vtype)
2744 gfc_component *c, *tail;
2746 mio_lparen ();
2748 if (iomode == IO_OUTPUT)
2750 for (c = *cp; c; c = c->next)
2751 mio_component (c, vtype);
2753 else
2755 *cp = NULL;
2756 tail = NULL;
2758 for (;;)
2760 if (peek_atom () == ATOM_RPAREN)
2761 break;
2763 c = gfc_get_component ();
2764 mio_component (c, vtype);
2766 if (tail == NULL)
2767 *cp = c;
2768 else
2769 tail->next = c;
2771 tail = c;
2775 mio_rparen ();
2779 static void
2780 mio_actual_arg (gfc_actual_arglist *a)
2782 mio_lparen ();
2783 mio_pool_string (&a->name);
2784 mio_expr (&a->expr);
2785 mio_rparen ();
2789 static void
2790 mio_actual_arglist (gfc_actual_arglist **ap)
2792 gfc_actual_arglist *a, *tail;
2794 mio_lparen ();
2796 if (iomode == IO_OUTPUT)
2798 for (a = *ap; a; a = a->next)
2799 mio_actual_arg (a);
2802 else
2804 tail = NULL;
2806 for (;;)
2808 if (peek_atom () != ATOM_LPAREN)
2809 break;
2811 a = gfc_get_actual_arglist ();
2813 if (tail == NULL)
2814 *ap = a;
2815 else
2816 tail->next = a;
2818 tail = a;
2819 mio_actual_arg (a);
2823 mio_rparen ();
2827 /* Read and write formal argument lists. */
2829 static void
2830 mio_formal_arglist (gfc_formal_arglist **formal)
2832 gfc_formal_arglist *f, *tail;
2834 mio_lparen ();
2836 if (iomode == IO_OUTPUT)
2838 for (f = *formal; f; f = f->next)
2839 mio_symbol_ref (&f->sym);
2841 else
2843 *formal = tail = NULL;
2845 while (peek_atom () != ATOM_RPAREN)
2847 f = gfc_get_formal_arglist ();
2848 mio_symbol_ref (&f->sym);
2850 if (*formal == NULL)
2851 *formal = f;
2852 else
2853 tail->next = f;
2855 tail = f;
2859 mio_rparen ();
2863 /* Save or restore a reference to a symbol node. */
2865 pointer_info *
2866 mio_symbol_ref (gfc_symbol **symp)
2868 pointer_info *p;
2870 p = mio_pointer_ref (symp);
2871 if (p->type == P_UNKNOWN)
2872 p->type = P_SYMBOL;
2874 if (iomode == IO_OUTPUT)
2876 if (p->u.wsym.state == UNREFERENCED)
2877 p->u.wsym.state = NEEDS_WRITE;
2879 else
2881 if (p->u.rsym.state == UNUSED)
2882 p->u.rsym.state = NEEDED;
2884 return p;
2888 /* Save or restore a reference to a symtree node. */
2890 static void
2891 mio_symtree_ref (gfc_symtree **stp)
2893 pointer_info *p;
2894 fixup_t *f;
2896 if (iomode == IO_OUTPUT)
2897 mio_symbol_ref (&(*stp)->n.sym);
2898 else
2900 require_atom (ATOM_INTEGER);
2901 p = get_integer (atom_int);
2903 /* An unused equivalence member; make a symbol and a symtree
2904 for it. */
2905 if (in_load_equiv && p->u.rsym.symtree == NULL)
2907 /* Since this is not used, it must have a unique name. */
2908 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2910 /* Make the symbol. */
2911 if (p->u.rsym.sym == NULL)
2913 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2914 gfc_current_ns);
2915 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2918 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2919 p->u.rsym.symtree->n.sym->refs++;
2920 p->u.rsym.referenced = 1;
2922 /* If the symbol is PRIVATE and in COMMON, load_commons will
2923 generate a fixup symbol, which must be associated. */
2924 if (p->fixup)
2925 resolve_fixups (p->fixup, p->u.rsym.sym);
2926 p->fixup = NULL;
2929 if (p->type == P_UNKNOWN)
2930 p->type = P_SYMBOL;
2932 if (p->u.rsym.state == UNUSED)
2933 p->u.rsym.state = NEEDED;
2935 if (p->u.rsym.symtree != NULL)
2937 *stp = p->u.rsym.symtree;
2939 else
2941 f = XCNEW (fixup_t);
2943 f->next = p->u.rsym.stfixup;
2944 p->u.rsym.stfixup = f;
2946 f->pointer = (void **) stp;
2952 static void
2953 mio_iterator (gfc_iterator **ip)
2955 gfc_iterator *iter;
2957 mio_lparen ();
2959 if (iomode == IO_OUTPUT)
2961 if (*ip == NULL)
2962 goto done;
2964 else
2966 if (peek_atom () == ATOM_RPAREN)
2968 *ip = NULL;
2969 goto done;
2972 *ip = gfc_get_iterator ();
2975 iter = *ip;
2977 mio_expr (&iter->var);
2978 mio_expr (&iter->start);
2979 mio_expr (&iter->end);
2980 mio_expr (&iter->step);
2982 done:
2983 mio_rparen ();
2987 static void
2988 mio_constructor (gfc_constructor_base *cp)
2990 gfc_constructor *c;
2992 mio_lparen ();
2994 if (iomode == IO_OUTPUT)
2996 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2998 mio_lparen ();
2999 mio_expr (&c->expr);
3000 mio_iterator (&c->iterator);
3001 mio_rparen ();
3004 else
3006 while (peek_atom () != ATOM_RPAREN)
3008 c = gfc_constructor_append_expr (cp, NULL, NULL);
3010 mio_lparen ();
3011 mio_expr (&c->expr);
3012 mio_iterator (&c->iterator);
3013 mio_rparen ();
3017 mio_rparen ();
3021 static const mstring ref_types[] = {
3022 minit ("ARRAY", REF_ARRAY),
3023 minit ("COMPONENT", REF_COMPONENT),
3024 minit ("SUBSTRING", REF_SUBSTRING),
3025 minit (NULL, -1)
3029 static void
3030 mio_ref (gfc_ref **rp)
3032 gfc_ref *r;
3034 mio_lparen ();
3036 r = *rp;
3037 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3039 switch (r->type)
3041 case REF_ARRAY:
3042 mio_array_ref (&r->u.ar);
3043 break;
3045 case REF_COMPONENT:
3046 mio_symbol_ref (&r->u.c.sym);
3047 mio_component_ref (&r->u.c.component);
3048 break;
3050 case REF_SUBSTRING:
3051 mio_expr (&r->u.ss.start);
3052 mio_expr (&r->u.ss.end);
3053 mio_charlen (&r->u.ss.length);
3054 break;
3057 mio_rparen ();
3061 static void
3062 mio_ref_list (gfc_ref **rp)
3064 gfc_ref *ref, *head, *tail;
3066 mio_lparen ();
3068 if (iomode == IO_OUTPUT)
3070 for (ref = *rp; ref; ref = ref->next)
3071 mio_ref (&ref);
3073 else
3075 head = tail = NULL;
3077 while (peek_atom () != ATOM_RPAREN)
3079 if (head == NULL)
3080 head = tail = gfc_get_ref ();
3081 else
3083 tail->next = gfc_get_ref ();
3084 tail = tail->next;
3087 mio_ref (&tail);
3090 *rp = head;
3093 mio_rparen ();
3097 /* Read and write an integer value. */
3099 static void
3100 mio_gmp_integer (mpz_t *integer)
3102 char *p;
3104 if (iomode == IO_INPUT)
3106 if (parse_atom () != ATOM_STRING)
3107 bad_module ("Expected integer string");
3109 mpz_init (*integer);
3110 if (mpz_set_str (*integer, atom_string, 10))
3111 bad_module ("Error converting integer");
3113 free (atom_string);
3115 else
3117 p = mpz_get_str (NULL, 10, *integer);
3118 write_atom (ATOM_STRING, p);
3119 free (p);
3124 static void
3125 mio_gmp_real (mpfr_t *real)
3127 mp_exp_t exponent;
3128 char *p;
3130 if (iomode == IO_INPUT)
3132 if (parse_atom () != ATOM_STRING)
3133 bad_module ("Expected real string");
3135 mpfr_init (*real);
3136 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3137 free (atom_string);
3139 else
3141 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3143 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3145 write_atom (ATOM_STRING, p);
3146 free (p);
3147 return;
3150 atom_string = XCNEWVEC (char, strlen (p) + 20);
3152 sprintf (atom_string, "0.%s@%ld", p, exponent);
3154 /* Fix negative numbers. */
3155 if (atom_string[2] == '-')
3157 atom_string[0] = '-';
3158 atom_string[1] = '0';
3159 atom_string[2] = '.';
3162 write_atom (ATOM_STRING, atom_string);
3164 free (atom_string);
3165 free (p);
3170 /* Save and restore the shape of an array constructor. */
3172 static void
3173 mio_shape (mpz_t **pshape, int rank)
3175 mpz_t *shape;
3176 atom_type t;
3177 int n;
3179 /* A NULL shape is represented by (). */
3180 mio_lparen ();
3182 if (iomode == IO_OUTPUT)
3184 shape = *pshape;
3185 if (!shape)
3187 mio_rparen ();
3188 return;
3191 else
3193 t = peek_atom ();
3194 if (t == ATOM_RPAREN)
3196 *pshape = NULL;
3197 mio_rparen ();
3198 return;
3201 shape = gfc_get_shape (rank);
3202 *pshape = shape;
3205 for (n = 0; n < rank; n++)
3206 mio_gmp_integer (&shape[n]);
3208 mio_rparen ();
3212 static const mstring expr_types[] = {
3213 minit ("OP", EXPR_OP),
3214 minit ("FUNCTION", EXPR_FUNCTION),
3215 minit ("CONSTANT", EXPR_CONSTANT),
3216 minit ("VARIABLE", EXPR_VARIABLE),
3217 minit ("SUBSTRING", EXPR_SUBSTRING),
3218 minit ("STRUCTURE", EXPR_STRUCTURE),
3219 minit ("ARRAY", EXPR_ARRAY),
3220 minit ("NULL", EXPR_NULL),
3221 minit ("COMPCALL", EXPR_COMPCALL),
3222 minit (NULL, -1)
3225 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3226 generic operators, not in expressions. INTRINSIC_USER is also
3227 replaced by the correct function name by the time we see it. */
3229 static const mstring intrinsics[] =
3231 minit ("UPLUS", INTRINSIC_UPLUS),
3232 minit ("UMINUS", INTRINSIC_UMINUS),
3233 minit ("PLUS", INTRINSIC_PLUS),
3234 minit ("MINUS", INTRINSIC_MINUS),
3235 minit ("TIMES", INTRINSIC_TIMES),
3236 minit ("DIVIDE", INTRINSIC_DIVIDE),
3237 minit ("POWER", INTRINSIC_POWER),
3238 minit ("CONCAT", INTRINSIC_CONCAT),
3239 minit ("AND", INTRINSIC_AND),
3240 minit ("OR", INTRINSIC_OR),
3241 minit ("EQV", INTRINSIC_EQV),
3242 minit ("NEQV", INTRINSIC_NEQV),
3243 minit ("EQ_SIGN", INTRINSIC_EQ),
3244 minit ("EQ", INTRINSIC_EQ_OS),
3245 minit ("NE_SIGN", INTRINSIC_NE),
3246 minit ("NE", INTRINSIC_NE_OS),
3247 minit ("GT_SIGN", INTRINSIC_GT),
3248 minit ("GT", INTRINSIC_GT_OS),
3249 minit ("GE_SIGN", INTRINSIC_GE),
3250 minit ("GE", INTRINSIC_GE_OS),
3251 minit ("LT_SIGN", INTRINSIC_LT),
3252 minit ("LT", INTRINSIC_LT_OS),
3253 minit ("LE_SIGN", INTRINSIC_LE),
3254 minit ("LE", INTRINSIC_LE_OS),
3255 minit ("NOT", INTRINSIC_NOT),
3256 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3257 minit ("USER", INTRINSIC_USER),
3258 minit (NULL, -1)
3262 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3264 static void
3265 fix_mio_expr (gfc_expr *e)
3267 gfc_symtree *ns_st = NULL;
3268 const char *fname;
3270 if (iomode != IO_OUTPUT)
3271 return;
3273 if (e->symtree)
3275 /* If this is a symtree for a symbol that came from a contained module
3276 namespace, it has a unique name and we should look in the current
3277 namespace to see if the required, non-contained symbol is available
3278 yet. If so, the latter should be written. */
3279 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3281 const char *name = e->symtree->n.sym->name;
3282 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3283 name = dt_upper_string (name);
3284 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3287 /* On the other hand, if the existing symbol is the module name or the
3288 new symbol is a dummy argument, do not do the promotion. */
3289 if (ns_st && ns_st->n.sym
3290 && ns_st->n.sym->attr.flavor != FL_MODULE
3291 && !e->symtree->n.sym->attr.dummy)
3292 e->symtree = ns_st;
3294 else if (e->expr_type == EXPR_FUNCTION
3295 && (e->value.function.name || e->value.function.isym))
3297 gfc_symbol *sym;
3299 /* In some circumstances, a function used in an initialization
3300 expression, in one use associated module, can fail to be
3301 coupled to its symtree when used in a specification
3302 expression in another module. */
3303 fname = e->value.function.esym ? e->value.function.esym->name
3304 : e->value.function.isym->name;
3305 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3307 if (e->symtree)
3308 return;
3310 /* This is probably a reference to a private procedure from another
3311 module. To prevent a segfault, make a generic with no specific
3312 instances. If this module is used, without the required
3313 specific coming from somewhere, the appropriate error message
3314 is issued. */
3315 gfc_get_symbol (fname, gfc_current_ns, &sym);
3316 sym->attr.flavor = FL_PROCEDURE;
3317 sym->attr.generic = 1;
3318 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3319 gfc_commit_symbol (sym);
3324 /* Read and write expressions. The form "()" is allowed to indicate a
3325 NULL expression. */
3327 static void
3328 mio_expr (gfc_expr **ep)
3330 gfc_expr *e;
3331 atom_type t;
3332 int flag;
3334 mio_lparen ();
3336 if (iomode == IO_OUTPUT)
3338 if (*ep == NULL)
3340 mio_rparen ();
3341 return;
3344 e = *ep;
3345 MIO_NAME (expr_t) (e->expr_type, expr_types);
3347 else
3349 t = parse_atom ();
3350 if (t == ATOM_RPAREN)
3352 *ep = NULL;
3353 return;
3356 if (t != ATOM_NAME)
3357 bad_module ("Expected expression type");
3359 e = *ep = gfc_get_expr ();
3360 e->where = gfc_current_locus;
3361 e->expr_type = (expr_t) find_enum (expr_types);
3364 mio_typespec (&e->ts);
3365 mio_integer (&e->rank);
3367 fix_mio_expr (e);
3369 switch (e->expr_type)
3371 case EXPR_OP:
3372 e->value.op.op
3373 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3375 switch (e->value.op.op)
3377 case INTRINSIC_UPLUS:
3378 case INTRINSIC_UMINUS:
3379 case INTRINSIC_NOT:
3380 case INTRINSIC_PARENTHESES:
3381 mio_expr (&e->value.op.op1);
3382 break;
3384 case INTRINSIC_PLUS:
3385 case INTRINSIC_MINUS:
3386 case INTRINSIC_TIMES:
3387 case INTRINSIC_DIVIDE:
3388 case INTRINSIC_POWER:
3389 case INTRINSIC_CONCAT:
3390 case INTRINSIC_AND:
3391 case INTRINSIC_OR:
3392 case INTRINSIC_EQV:
3393 case INTRINSIC_NEQV:
3394 case INTRINSIC_EQ:
3395 case INTRINSIC_EQ_OS:
3396 case INTRINSIC_NE:
3397 case INTRINSIC_NE_OS:
3398 case INTRINSIC_GT:
3399 case INTRINSIC_GT_OS:
3400 case INTRINSIC_GE:
3401 case INTRINSIC_GE_OS:
3402 case INTRINSIC_LT:
3403 case INTRINSIC_LT_OS:
3404 case INTRINSIC_LE:
3405 case INTRINSIC_LE_OS:
3406 mio_expr (&e->value.op.op1);
3407 mio_expr (&e->value.op.op2);
3408 break;
3410 case INTRINSIC_USER:
3411 /* INTRINSIC_USER should not appear in resolved expressions,
3412 though for UDRs we need to stream unresolved ones. */
3413 if (iomode == IO_OUTPUT)
3414 write_atom (ATOM_STRING, e->value.op.uop->name);
3415 else
3417 char *name = read_string ();
3418 const char *uop_name = find_use_name (name, true);
3419 if (uop_name == NULL)
3421 size_t len = strlen (name);
3422 char *name2 = XCNEWVEC (char, len + 2);
3423 memcpy (name2, name, len);
3424 name2[len] = ' ';
3425 name2[len + 1] = '\0';
3426 free (name);
3427 uop_name = name = name2;
3429 e->value.op.uop = gfc_get_uop (uop_name);
3430 free (name);
3432 mio_expr (&e->value.op.op1);
3433 mio_expr (&e->value.op.op2);
3434 break;
3436 default:
3437 bad_module ("Bad operator");
3440 break;
3442 case EXPR_FUNCTION:
3443 mio_symtree_ref (&e->symtree);
3444 mio_actual_arglist (&e->value.function.actual);
3446 if (iomode == IO_OUTPUT)
3448 e->value.function.name
3449 = mio_allocated_string (e->value.function.name);
3450 if (e->value.function.esym)
3451 flag = 1;
3452 else if (e->ref)
3453 flag = 2;
3454 else if (e->value.function.isym == NULL)
3455 flag = 3;
3456 else
3457 flag = 0;
3458 mio_integer (&flag);
3459 switch (flag)
3461 case 1:
3462 mio_symbol_ref (&e->value.function.esym);
3463 break;
3464 case 2:
3465 mio_ref_list (&e->ref);
3466 break;
3467 case 3:
3468 break;
3469 default:
3470 write_atom (ATOM_STRING, e->value.function.isym->name);
3473 else
3475 require_atom (ATOM_STRING);
3476 if (atom_string[0] == '\0')
3477 e->value.function.name = NULL;
3478 else
3479 e->value.function.name = gfc_get_string (atom_string);
3480 free (atom_string);
3482 mio_integer (&flag);
3483 switch (flag)
3485 case 1:
3486 mio_symbol_ref (&e->value.function.esym);
3487 break;
3488 case 2:
3489 mio_ref_list (&e->ref);
3490 break;
3491 case 3:
3492 break;
3493 default:
3494 require_atom (ATOM_STRING);
3495 e->value.function.isym = gfc_find_function (atom_string);
3496 free (atom_string);
3500 break;
3502 case EXPR_VARIABLE:
3503 mio_symtree_ref (&e->symtree);
3504 mio_ref_list (&e->ref);
3505 break;
3507 case EXPR_SUBSTRING:
3508 e->value.character.string
3509 = CONST_CAST (gfc_char_t *,
3510 mio_allocated_wide_string (e->value.character.string,
3511 e->value.character.length));
3512 mio_ref_list (&e->ref);
3513 break;
3515 case EXPR_STRUCTURE:
3516 case EXPR_ARRAY:
3517 mio_constructor (&e->value.constructor);
3518 mio_shape (&e->shape, e->rank);
3519 break;
3521 case EXPR_CONSTANT:
3522 switch (e->ts.type)
3524 case BT_INTEGER:
3525 mio_gmp_integer (&e->value.integer);
3526 break;
3528 case BT_REAL:
3529 gfc_set_model_kind (e->ts.kind);
3530 mio_gmp_real (&e->value.real);
3531 break;
3533 case BT_COMPLEX:
3534 gfc_set_model_kind (e->ts.kind);
3535 mio_gmp_real (&mpc_realref (e->value.complex));
3536 mio_gmp_real (&mpc_imagref (e->value.complex));
3537 break;
3539 case BT_LOGICAL:
3540 mio_integer (&e->value.logical);
3541 break;
3543 case BT_CHARACTER:
3544 mio_integer (&e->value.character.length);
3545 e->value.character.string
3546 = CONST_CAST (gfc_char_t *,
3547 mio_allocated_wide_string (e->value.character.string,
3548 e->value.character.length));
3549 break;
3551 default:
3552 bad_module ("Bad type in constant expression");
3555 break;
3557 case EXPR_NULL:
3558 break;
3560 case EXPR_COMPCALL:
3561 case EXPR_PPC:
3562 gcc_unreachable ();
3563 break;
3566 mio_rparen ();
3570 /* Read and write namelists. */
3572 static void
3573 mio_namelist (gfc_symbol *sym)
3575 gfc_namelist *n, *m;
3576 const char *check_name;
3578 mio_lparen ();
3580 if (iomode == IO_OUTPUT)
3582 for (n = sym->namelist; n; n = n->next)
3583 mio_symbol_ref (&n->sym);
3585 else
3587 /* This departure from the standard is flagged as an error.
3588 It does, in fact, work correctly. TODO: Allow it
3589 conditionally? */
3590 if (sym->attr.flavor == FL_NAMELIST)
3592 check_name = find_use_name (sym->name, false);
3593 if (check_name && strcmp (check_name, sym->name) != 0)
3594 gfc_error ("Namelist %s cannot be renamed by USE "
3595 "association to %s", sym->name, check_name);
3598 m = NULL;
3599 while (peek_atom () != ATOM_RPAREN)
3601 n = gfc_get_namelist ();
3602 mio_symbol_ref (&n->sym);
3604 if (sym->namelist == NULL)
3605 sym->namelist = n;
3606 else
3607 m->next = n;
3609 m = n;
3611 sym->namelist_tail = m;
3614 mio_rparen ();
3618 /* Save/restore lists of gfc_interface structures. When loading an
3619 interface, we are really appending to the existing list of
3620 interfaces. Checking for duplicate and ambiguous interfaces has to
3621 be done later when all symbols have been loaded. */
3623 pointer_info *
3624 mio_interface_rest (gfc_interface **ip)
3626 gfc_interface *tail, *p;
3627 pointer_info *pi = NULL;
3629 if (iomode == IO_OUTPUT)
3631 if (ip != NULL)
3632 for (p = *ip; p; p = p->next)
3633 mio_symbol_ref (&p->sym);
3635 else
3637 if (*ip == NULL)
3638 tail = NULL;
3639 else
3641 tail = *ip;
3642 while (tail->next)
3643 tail = tail->next;
3646 for (;;)
3648 if (peek_atom () == ATOM_RPAREN)
3649 break;
3651 p = gfc_get_interface ();
3652 p->where = gfc_current_locus;
3653 pi = mio_symbol_ref (&p->sym);
3655 if (tail == NULL)
3656 *ip = p;
3657 else
3658 tail->next = p;
3660 tail = p;
3664 mio_rparen ();
3665 return pi;
3669 /* Save/restore a nameless operator interface. */
3671 static void
3672 mio_interface (gfc_interface **ip)
3674 mio_lparen ();
3675 mio_interface_rest (ip);
3679 /* Save/restore a named operator interface. */
3681 static void
3682 mio_symbol_interface (const char **name, const char **module,
3683 gfc_interface **ip)
3685 mio_lparen ();
3686 mio_pool_string (name);
3687 mio_pool_string (module);
3688 mio_interface_rest (ip);
3692 static void
3693 mio_namespace_ref (gfc_namespace **nsp)
3695 gfc_namespace *ns;
3696 pointer_info *p;
3698 p = mio_pointer_ref (nsp);
3700 if (p->type == P_UNKNOWN)
3701 p->type = P_NAMESPACE;
3703 if (iomode == IO_INPUT && p->integer != 0)
3705 ns = (gfc_namespace *) p->u.pointer;
3706 if (ns == NULL)
3708 ns = gfc_get_namespace (NULL, 0);
3709 associate_integer_pointer (p, ns);
3711 else
3712 ns->refs++;
3717 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3719 static gfc_namespace* current_f2k_derived;
3721 static void
3722 mio_typebound_proc (gfc_typebound_proc** proc)
3724 int flag;
3725 int overriding_flag;
3727 if (iomode == IO_INPUT)
3729 *proc = gfc_get_typebound_proc (NULL);
3730 (*proc)->where = gfc_current_locus;
3732 gcc_assert (*proc);
3734 mio_lparen ();
3736 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3738 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3739 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3740 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3741 overriding_flag = mio_name (overriding_flag, binding_overriding);
3742 (*proc)->deferred = ((overriding_flag & 2) != 0);
3743 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3744 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3746 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3747 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3748 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3750 mio_pool_string (&((*proc)->pass_arg));
3752 flag = (int) (*proc)->pass_arg_num;
3753 mio_integer (&flag);
3754 (*proc)->pass_arg_num = (unsigned) flag;
3756 if ((*proc)->is_generic)
3758 gfc_tbp_generic* g;
3759 int iop;
3761 mio_lparen ();
3763 if (iomode == IO_OUTPUT)
3764 for (g = (*proc)->u.generic; g; g = g->next)
3766 iop = (int) g->is_operator;
3767 mio_integer (&iop);
3768 mio_allocated_string (g->specific_st->name);
3770 else
3772 (*proc)->u.generic = NULL;
3773 while (peek_atom () != ATOM_RPAREN)
3775 gfc_symtree** sym_root;
3777 g = gfc_get_tbp_generic ();
3778 g->specific = NULL;
3780 mio_integer (&iop);
3781 g->is_operator = (bool) iop;
3783 require_atom (ATOM_STRING);
3784 sym_root = &current_f2k_derived->tb_sym_root;
3785 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3786 free (atom_string);
3788 g->next = (*proc)->u.generic;
3789 (*proc)->u.generic = g;
3793 mio_rparen ();
3795 else if (!(*proc)->ppc)
3796 mio_symtree_ref (&(*proc)->u.specific);
3798 mio_rparen ();
3801 /* Walker-callback function for this purpose. */
3802 static void
3803 mio_typebound_symtree (gfc_symtree* st)
3805 if (iomode == IO_OUTPUT && !st->n.tb)
3806 return;
3808 if (iomode == IO_OUTPUT)
3810 mio_lparen ();
3811 mio_allocated_string (st->name);
3813 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3815 mio_typebound_proc (&st->n.tb);
3816 mio_rparen ();
3819 /* IO a full symtree (in all depth). */
3820 static void
3821 mio_full_typebound_tree (gfc_symtree** root)
3823 mio_lparen ();
3825 if (iomode == IO_OUTPUT)
3826 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3827 else
3829 while (peek_atom () == ATOM_LPAREN)
3831 gfc_symtree* st;
3833 mio_lparen ();
3835 require_atom (ATOM_STRING);
3836 st = gfc_get_tbp_symtree (root, atom_string);
3837 free (atom_string);
3839 mio_typebound_symtree (st);
3843 mio_rparen ();
3846 static void
3847 mio_finalizer (gfc_finalizer **f)
3849 if (iomode == IO_OUTPUT)
3851 gcc_assert (*f);
3852 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3853 mio_symtree_ref (&(*f)->proc_tree);
3855 else
3857 *f = gfc_get_finalizer ();
3858 (*f)->where = gfc_current_locus; /* Value should not matter. */
3859 (*f)->next = NULL;
3861 mio_symtree_ref (&(*f)->proc_tree);
3862 (*f)->proc_sym = NULL;
3866 static void
3867 mio_f2k_derived (gfc_namespace *f2k)
3869 current_f2k_derived = f2k;
3871 /* Handle the list of finalizer procedures. */
3872 mio_lparen ();
3873 if (iomode == IO_OUTPUT)
3875 gfc_finalizer *f;
3876 for (f = f2k->finalizers; f; f = f->next)
3877 mio_finalizer (&f);
3879 else
3881 f2k->finalizers = NULL;
3882 while (peek_atom () != ATOM_RPAREN)
3884 gfc_finalizer *cur = NULL;
3885 mio_finalizer (&cur);
3886 cur->next = f2k->finalizers;
3887 f2k->finalizers = cur;
3890 mio_rparen ();
3892 /* Handle type-bound procedures. */
3893 mio_full_typebound_tree (&f2k->tb_sym_root);
3895 /* Type-bound user operators. */
3896 mio_full_typebound_tree (&f2k->tb_uop_root);
3898 /* Type-bound intrinsic operators. */
3899 mio_lparen ();
3900 if (iomode == IO_OUTPUT)
3902 int op;
3903 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3905 gfc_intrinsic_op realop;
3907 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3908 continue;
3910 mio_lparen ();
3911 realop = (gfc_intrinsic_op) op;
3912 mio_intrinsic_op (&realop);
3913 mio_typebound_proc (&f2k->tb_op[op]);
3914 mio_rparen ();
3917 else
3918 while (peek_atom () != ATOM_RPAREN)
3920 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3922 mio_lparen ();
3923 mio_intrinsic_op (&op);
3924 mio_typebound_proc (&f2k->tb_op[op]);
3925 mio_rparen ();
3927 mio_rparen ();
3930 static void
3931 mio_full_f2k_derived (gfc_symbol *sym)
3933 mio_lparen ();
3935 if (iomode == IO_OUTPUT)
3937 if (sym->f2k_derived)
3938 mio_f2k_derived (sym->f2k_derived);
3940 else
3942 if (peek_atom () != ATOM_RPAREN)
3944 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3945 mio_f2k_derived (sym->f2k_derived);
3947 else
3948 gcc_assert (!sym->f2k_derived);
3951 mio_rparen ();
3954 static const mstring omp_declare_simd_clauses[] =
3956 minit ("INBRANCH", 0),
3957 minit ("NOTINBRANCH", 1),
3958 minit ("SIMDLEN", 2),
3959 minit ("UNIFORM", 3),
3960 minit ("LINEAR", 4),
3961 minit ("ALIGNED", 5),
3962 minit (NULL, -1)
3965 /* Handle !$omp declare simd. */
3967 static void
3968 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
3970 if (iomode == IO_OUTPUT)
3972 if (*odsp == NULL)
3973 return;
3975 else if (peek_atom () != ATOM_LPAREN)
3976 return;
3978 gfc_omp_declare_simd *ods = *odsp;
3980 mio_lparen ();
3981 if (iomode == IO_OUTPUT)
3983 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
3984 if (ods->clauses)
3986 gfc_omp_namelist *n;
3988 if (ods->clauses->inbranch)
3989 mio_name (0, omp_declare_simd_clauses);
3990 if (ods->clauses->notinbranch)
3991 mio_name (1, omp_declare_simd_clauses);
3992 if (ods->clauses->simdlen_expr)
3994 mio_name (2, omp_declare_simd_clauses);
3995 mio_expr (&ods->clauses->simdlen_expr);
3997 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
3999 mio_name (3, omp_declare_simd_clauses);
4000 mio_symbol_ref (&n->sym);
4002 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4004 mio_name (4, omp_declare_simd_clauses);
4005 mio_symbol_ref (&n->sym);
4006 mio_expr (&n->expr);
4008 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4010 mio_name (5, omp_declare_simd_clauses);
4011 mio_symbol_ref (&n->sym);
4012 mio_expr (&n->expr);
4016 else
4018 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4020 require_atom (ATOM_NAME);
4021 *odsp = ods = gfc_get_omp_declare_simd ();
4022 ods->where = gfc_current_locus;
4023 ods->proc_name = ns->proc_name;
4024 if (peek_atom () == ATOM_NAME)
4026 ods->clauses = gfc_get_omp_clauses ();
4027 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4028 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4029 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4031 while (peek_atom () == ATOM_NAME)
4033 gfc_omp_namelist *n;
4034 int t = mio_name (0, omp_declare_simd_clauses);
4036 switch (t)
4038 case 0: ods->clauses->inbranch = true; break;
4039 case 1: ods->clauses->notinbranch = true; break;
4040 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4041 case 3:
4042 case 4:
4043 case 5:
4044 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4045 ptrs[t - 3] = &n->next;
4046 mio_symbol_ref (&n->sym);
4047 if (t != 3)
4048 mio_expr (&n->expr);
4049 break;
4054 mio_omp_declare_simd (ns, &ods->next);
4056 mio_rparen ();
4060 static const mstring omp_declare_reduction_stmt[] =
4062 minit ("ASSIGN", 0),
4063 minit ("CALL", 1),
4064 minit (NULL, -1)
4068 static void
4069 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4070 gfc_namespace *ns, bool is_initializer)
4072 if (iomode == IO_OUTPUT)
4074 if ((*sym1)->module == NULL)
4076 (*sym1)->module = module_name;
4077 (*sym2)->module = module_name;
4079 mio_symbol_ref (sym1);
4080 mio_symbol_ref (sym2);
4081 if (ns->code->op == EXEC_ASSIGN)
4083 mio_name (0, omp_declare_reduction_stmt);
4084 mio_expr (&ns->code->expr1);
4085 mio_expr (&ns->code->expr2);
4087 else
4089 int flag;
4090 mio_name (1, omp_declare_reduction_stmt);
4091 mio_symtree_ref (&ns->code->symtree);
4092 mio_actual_arglist (&ns->code->ext.actual);
4094 flag = ns->code->resolved_isym != NULL;
4095 mio_integer (&flag);
4096 if (flag)
4097 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4098 else
4099 mio_symbol_ref (&ns->code->resolved_sym);
4102 else
4104 pointer_info *p1 = mio_symbol_ref (sym1);
4105 pointer_info *p2 = mio_symbol_ref (sym2);
4106 gfc_symbol *sym;
4107 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4108 gcc_assert (p1->u.rsym.sym == NULL);
4109 /* Add hidden symbols to the symtree. */
4110 pointer_info *q = get_integer (p1->u.rsym.ns);
4111 q->u.pointer = (void *) ns;
4112 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4113 sym->ts = udr->ts;
4114 sym->module = gfc_get_string (p1->u.rsym.module);
4115 associate_integer_pointer (p1, sym);
4116 sym->attr.omp_udr_artificial_var = 1;
4117 gcc_assert (p2->u.rsym.sym == NULL);
4118 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4119 sym->ts = udr->ts;
4120 sym->module = gfc_get_string (p2->u.rsym.module);
4121 associate_integer_pointer (p2, sym);
4122 sym->attr.omp_udr_artificial_var = 1;
4123 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4125 ns->code = gfc_get_code (EXEC_ASSIGN);
4126 mio_expr (&ns->code->expr1);
4127 mio_expr (&ns->code->expr2);
4129 else
4131 int flag;
4132 ns->code = gfc_get_code (EXEC_CALL);
4133 mio_symtree_ref (&ns->code->symtree);
4134 mio_actual_arglist (&ns->code->ext.actual);
4136 mio_integer (&flag);
4137 if (flag)
4139 require_atom (ATOM_STRING);
4140 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4141 free (atom_string);
4143 else
4144 mio_symbol_ref (&ns->code->resolved_sym);
4146 ns->code->loc = gfc_current_locus;
4147 ns->omp_udr_ns = 1;
4152 /* Unlike most other routines, the address of the symbol node is already
4153 fixed on input and the name/module has already been filled in.
4154 If you update the symbol format here, don't forget to update read_module
4155 as well (look for "seek to the symbol's component list"). */
4157 static void
4158 mio_symbol (gfc_symbol *sym)
4160 int intmod = INTMOD_NONE;
4162 mio_lparen ();
4164 mio_symbol_attribute (&sym->attr);
4166 /* Note that components are always saved, even if they are supposed
4167 to be private. Component access is checked during searching. */
4168 mio_component_list (&sym->components, sym->attr.vtype);
4169 if (sym->components != NULL)
4170 sym->component_access
4171 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4173 mio_typespec (&sym->ts);
4174 if (sym->ts.type == BT_CLASS)
4175 sym->attr.class_ok = 1;
4177 if (iomode == IO_OUTPUT)
4178 mio_namespace_ref (&sym->formal_ns);
4179 else
4181 mio_namespace_ref (&sym->formal_ns);
4182 if (sym->formal_ns)
4183 sym->formal_ns->proc_name = sym;
4186 /* Save/restore common block links. */
4187 mio_symbol_ref (&sym->common_next);
4189 mio_formal_arglist (&sym->formal);
4191 if (sym->attr.flavor == FL_PARAMETER)
4192 mio_expr (&sym->value);
4194 mio_array_spec (&sym->as);
4196 mio_symbol_ref (&sym->result);
4198 if (sym->attr.cray_pointee)
4199 mio_symbol_ref (&sym->cp_pointer);
4201 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4202 mio_full_f2k_derived (sym);
4204 mio_namelist (sym);
4206 /* Add the fields that say whether this is from an intrinsic module,
4207 and if so, what symbol it is within the module. */
4208 /* mio_integer (&(sym->from_intmod)); */
4209 if (iomode == IO_OUTPUT)
4211 intmod = sym->from_intmod;
4212 mio_integer (&intmod);
4214 else
4216 mio_integer (&intmod);
4217 if (current_intmod)
4218 sym->from_intmod = current_intmod;
4219 else
4220 sym->from_intmod = (intmod_id) intmod;
4223 mio_integer (&(sym->intmod_sym_id));
4225 if (sym->attr.flavor == FL_DERIVED)
4226 mio_integer (&(sym->hash_value));
4228 if (sym->formal_ns
4229 && sym->formal_ns->proc_name == sym
4230 && sym->formal_ns->entries == NULL)
4231 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4233 mio_rparen ();
4237 /************************* Top level subroutines *************************/
4239 /* Given a root symtree node and a symbol, try to find a symtree that
4240 references the symbol that is not a unique name. */
4242 static gfc_symtree *
4243 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
4245 gfc_symtree *s = NULL;
4247 if (st == NULL)
4248 return s;
4250 s = find_symtree_for_symbol (st->right, sym);
4251 if (s != NULL)
4252 return s;
4253 s = find_symtree_for_symbol (st->left, sym);
4254 if (s != NULL)
4255 return s;
4257 if (st->n.sym == sym && !check_unique_name (st->name))
4258 return st;
4260 return s;
4264 /* A recursive function to look for a specific symbol by name and by
4265 module. Whilst several symtrees might point to one symbol, its
4266 is sufficient for the purposes here than one exist. Note that
4267 generic interfaces are distinguished as are symbols that have been
4268 renamed in another module. */
4269 static gfc_symtree *
4270 find_symbol (gfc_symtree *st, const char *name,
4271 const char *module, int generic)
4273 int c;
4274 gfc_symtree *retval, *s;
4276 if (st == NULL || st->n.sym == NULL)
4277 return NULL;
4279 c = strcmp (name, st->n.sym->name);
4280 if (c == 0 && st->n.sym->module
4281 && strcmp (module, st->n.sym->module) == 0
4282 && !check_unique_name (st->name))
4284 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4286 /* Detect symbols that are renamed by use association in another
4287 module by the absence of a symtree and null attr.use_rename,
4288 since the latter is not transmitted in the module file. */
4289 if (((!generic && !st->n.sym->attr.generic)
4290 || (generic && st->n.sym->attr.generic))
4291 && !(s == NULL && !st->n.sym->attr.use_rename))
4292 return st;
4295 retval = find_symbol (st->left, name, module, generic);
4297 if (retval == NULL)
4298 retval = find_symbol (st->right, name, module, generic);
4300 return retval;
4304 /* Skip a list between balanced left and right parens.
4305 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4306 have been already parsed by hand, and the remaining of the content is to be
4307 skipped here. The default value is 0 (balanced parens). */
4309 static void
4310 skip_list (int nest_level = 0)
4312 int level;
4314 level = nest_level;
4317 switch (parse_atom ())
4319 case ATOM_LPAREN:
4320 level++;
4321 break;
4323 case ATOM_RPAREN:
4324 level--;
4325 break;
4327 case ATOM_STRING:
4328 free (atom_string);
4329 break;
4331 case ATOM_NAME:
4332 case ATOM_INTEGER:
4333 break;
4336 while (level > 0);
4340 /* Load operator interfaces from the module. Interfaces are unusual
4341 in that they attach themselves to existing symbols. */
4343 static void
4344 load_operator_interfaces (void)
4346 const char *p;
4347 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4348 gfc_user_op *uop;
4349 pointer_info *pi = NULL;
4350 int n, i;
4352 mio_lparen ();
4354 while (peek_atom () != ATOM_RPAREN)
4356 mio_lparen ();
4358 mio_internal_string (name);
4359 mio_internal_string (module);
4361 n = number_use_names (name, true);
4362 n = n ? n : 1;
4364 for (i = 1; i <= n; i++)
4366 /* Decide if we need to load this one or not. */
4367 p = find_use_name_n (name, &i, true);
4369 if (p == NULL)
4371 while (parse_atom () != ATOM_RPAREN);
4372 continue;
4375 if (i == 1)
4377 uop = gfc_get_uop (p);
4378 pi = mio_interface_rest (&uop->op);
4380 else
4382 if (gfc_find_uop (p, NULL))
4383 continue;
4384 uop = gfc_get_uop (p);
4385 uop->op = gfc_get_interface ();
4386 uop->op->where = gfc_current_locus;
4387 add_fixup (pi->integer, &uop->op->sym);
4392 mio_rparen ();
4396 /* Load interfaces from the module. Interfaces are unusual in that
4397 they attach themselves to existing symbols. */
4399 static void
4400 load_generic_interfaces (void)
4402 const char *p;
4403 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4404 gfc_symbol *sym;
4405 gfc_interface *generic = NULL, *gen = NULL;
4406 int n, i, renamed;
4407 bool ambiguous_set = false;
4409 mio_lparen ();
4411 while (peek_atom () != ATOM_RPAREN)
4413 mio_lparen ();
4415 mio_internal_string (name);
4416 mio_internal_string (module);
4418 n = number_use_names (name, false);
4419 renamed = n ? 1 : 0;
4420 n = n ? n : 1;
4422 for (i = 1; i <= n; i++)
4424 gfc_symtree *st;
4425 /* Decide if we need to load this one or not. */
4426 p = find_use_name_n (name, &i, false);
4428 st = find_symbol (gfc_current_ns->sym_root,
4429 name, module_name, 1);
4431 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4433 /* Skip the specific names for these cases. */
4434 while (i == 1 && parse_atom () != ATOM_RPAREN);
4436 continue;
4439 /* If the symbol exists already and is being USEd without being
4440 in an ONLY clause, do not load a new symtree(11.3.2). */
4441 if (!only_flag && st)
4442 sym = st->n.sym;
4444 if (!sym)
4446 if (st)
4448 sym = st->n.sym;
4449 if (strcmp (st->name, p) != 0)
4451 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4452 st->n.sym = sym;
4453 sym->refs++;
4457 /* Since we haven't found a valid generic interface, we had
4458 better make one. */
4459 if (!sym)
4461 gfc_get_symbol (p, NULL, &sym);
4462 sym->name = gfc_get_string (name);
4463 sym->module = module_name;
4464 sym->attr.flavor = FL_PROCEDURE;
4465 sym->attr.generic = 1;
4466 sym->attr.use_assoc = 1;
4469 else
4471 /* Unless sym is a generic interface, this reference
4472 is ambiguous. */
4473 if (st == NULL)
4474 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4476 sym = st->n.sym;
4478 if (st && !sym->attr.generic
4479 && !st->ambiguous
4480 && sym->module
4481 && strcmp (module, sym->module))
4483 ambiguous_set = true;
4484 st->ambiguous = 1;
4488 sym->attr.use_only = only_flag;
4489 sym->attr.use_rename = renamed;
4491 if (i == 1)
4493 mio_interface_rest (&sym->generic);
4494 generic = sym->generic;
4496 else if (!sym->generic)
4498 sym->generic = generic;
4499 sym->attr.generic_copy = 1;
4502 /* If a procedure that is not generic has generic interfaces
4503 that include itself, it is generic! We need to take care
4504 to retain symbols ambiguous that were already so. */
4505 if (sym->attr.use_assoc
4506 && !sym->attr.generic
4507 && sym->attr.flavor == FL_PROCEDURE)
4509 for (gen = generic; gen; gen = gen->next)
4511 if (gen->sym == sym)
4513 sym->attr.generic = 1;
4514 if (ambiguous_set)
4515 st->ambiguous = 0;
4516 break;
4524 mio_rparen ();
4528 /* Load common blocks. */
4530 static void
4531 load_commons (void)
4533 char name[GFC_MAX_SYMBOL_LEN + 1];
4534 gfc_common_head *p;
4536 mio_lparen ();
4538 while (peek_atom () != ATOM_RPAREN)
4540 int flags;
4541 char* label;
4542 mio_lparen ();
4543 mio_internal_string (name);
4545 p = gfc_get_common (name, 1);
4547 mio_symbol_ref (&p->head);
4548 mio_integer (&flags);
4549 if (flags & 1)
4550 p->saved = 1;
4551 if (flags & 2)
4552 p->threadprivate = 1;
4553 p->use_assoc = 1;
4555 /* Get whether this was a bind(c) common or not. */
4556 mio_integer (&p->is_bind_c);
4557 /* Get the binding label. */
4558 label = read_string ();
4559 if (strlen (label))
4560 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4561 XDELETEVEC (label);
4563 mio_rparen ();
4566 mio_rparen ();
4570 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4571 so that unused variables are not loaded and so that the expression can
4572 be safely freed. */
4574 static void
4575 load_equiv (void)
4577 gfc_equiv *head, *tail, *end, *eq, *equiv;
4578 bool duplicate;
4580 mio_lparen ();
4581 in_load_equiv = true;
4583 end = gfc_current_ns->equiv;
4584 while (end != NULL && end->next != NULL)
4585 end = end->next;
4587 while (peek_atom () != ATOM_RPAREN) {
4588 mio_lparen ();
4589 head = tail = NULL;
4591 while(peek_atom () != ATOM_RPAREN)
4593 if (head == NULL)
4594 head = tail = gfc_get_equiv ();
4595 else
4597 tail->eq = gfc_get_equiv ();
4598 tail = tail->eq;
4601 mio_pool_string (&tail->module);
4602 mio_expr (&tail->expr);
4605 /* Check for duplicate equivalences being loaded from different modules */
4606 duplicate = false;
4607 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
4609 if (equiv->module && head->module
4610 && strcmp (equiv->module, head->module) == 0)
4612 duplicate = true;
4613 break;
4617 if (duplicate)
4619 for (eq = head; eq; eq = head)
4621 head = eq->eq;
4622 gfc_free_expr (eq->expr);
4623 free (eq);
4627 if (end == NULL)
4628 gfc_current_ns->equiv = head;
4629 else
4630 end->next = head;
4632 if (head != NULL)
4633 end = head;
4635 mio_rparen ();
4638 mio_rparen ();
4639 in_load_equiv = false;
4643 /* This function loads OpenMP user defined reductions. */
4644 static void
4645 load_omp_udrs (void)
4647 mio_lparen ();
4648 while (peek_atom () != ATOM_RPAREN)
4650 const char *name, *newname;
4651 char *altname;
4652 gfc_typespec ts;
4653 gfc_symtree *st;
4654 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4656 mio_lparen ();
4657 mio_pool_string (&name);
4658 mio_typespec (&ts);
4659 if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
4661 const char *p = name + sizeof ("operator ") - 1;
4662 if (strcmp (p, "+") == 0)
4663 rop = OMP_REDUCTION_PLUS;
4664 else if (strcmp (p, "*") == 0)
4665 rop = OMP_REDUCTION_TIMES;
4666 else if (strcmp (p, "-") == 0)
4667 rop = OMP_REDUCTION_MINUS;
4668 else if (strcmp (p, ".and.") == 0)
4669 rop = OMP_REDUCTION_AND;
4670 else if (strcmp (p, ".or.") == 0)
4671 rop = OMP_REDUCTION_OR;
4672 else if (strcmp (p, ".eqv.") == 0)
4673 rop = OMP_REDUCTION_EQV;
4674 else if (strcmp (p, ".neqv.") == 0)
4675 rop = OMP_REDUCTION_NEQV;
4677 altname = NULL;
4678 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4680 size_t len = strlen (name + 1);
4681 altname = XALLOCAVEC (char, len);
4682 gcc_assert (name[len] == '.');
4683 memcpy (altname, name + 1, len - 1);
4684 altname[len - 1] = '\0';
4686 newname = name;
4687 if (rop == OMP_REDUCTION_USER)
4688 newname = find_use_name (altname ? altname : name, !!altname);
4689 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4690 newname = NULL;
4691 if (newname == NULL)
4693 skip_list (1);
4694 continue;
4696 if (altname && newname != altname)
4698 size_t len = strlen (newname);
4699 altname = XALLOCAVEC (char, len + 3);
4700 altname[0] = '.';
4701 memcpy (altname + 1, newname, len);
4702 altname[len + 1] = '.';
4703 altname[len + 2] = '\0';
4704 name = gfc_get_string (altname);
4706 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4707 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4708 if (udr)
4710 require_atom (ATOM_INTEGER);
4711 pointer_info *p = get_integer (atom_int);
4712 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4714 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4715 "module %s at %L",
4716 p->u.rsym.module, &gfc_current_locus);
4717 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4718 "%s at %L",
4719 udr->omp_out->module, &udr->where);
4721 skip_list (1);
4722 continue;
4724 udr = gfc_get_omp_udr ();
4725 udr->name = name;
4726 udr->rop = rop;
4727 udr->ts = ts;
4728 udr->where = gfc_current_locus;
4729 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4730 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4731 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4732 false);
4733 if (peek_atom () != ATOM_RPAREN)
4735 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4736 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4737 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4738 udr->initializer_ns, true);
4740 if (st)
4742 udr->next = st->n.omp_udr;
4743 st->n.omp_udr = udr;
4745 else
4747 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4748 st->n.omp_udr = udr;
4750 mio_rparen ();
4752 mio_rparen ();
4756 /* Recursive function to traverse the pointer_info tree and load a
4757 needed symbol. We return nonzero if we load a symbol and stop the
4758 traversal, because the act of loading can alter the tree. */
4760 static int
4761 load_needed (pointer_info *p)
4763 gfc_namespace *ns;
4764 pointer_info *q;
4765 gfc_symbol *sym;
4766 int rv;
4768 rv = 0;
4769 if (p == NULL)
4770 return rv;
4772 rv |= load_needed (p->left);
4773 rv |= load_needed (p->right);
4775 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4776 return rv;
4778 p->u.rsym.state = USED;
4780 set_module_locus (&p->u.rsym.where);
4782 sym = p->u.rsym.sym;
4783 if (sym == NULL)
4785 q = get_integer (p->u.rsym.ns);
4787 ns = (gfc_namespace *) q->u.pointer;
4788 if (ns == NULL)
4790 /* Create an interface namespace if necessary. These are
4791 the namespaces that hold the formal parameters of module
4792 procedures. */
4794 ns = gfc_get_namespace (NULL, 0);
4795 associate_integer_pointer (q, ns);
4798 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4799 doesn't go pear-shaped if the symbol is used. */
4800 if (!ns->proc_name)
4801 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4802 1, &ns->proc_name);
4804 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4805 sym->name = dt_lower_string (p->u.rsym.true_name);
4806 sym->module = gfc_get_string (p->u.rsym.module);
4807 if (p->u.rsym.binding_label)
4808 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4809 (p->u.rsym.binding_label));
4811 associate_integer_pointer (p, sym);
4814 mio_symbol (sym);
4815 sym->attr.use_assoc = 1;
4817 /* Mark as only or rename for later diagnosis for explicitly imported
4818 but not used warnings; don't mark internal symbols such as __vtab,
4819 __def_init etc. Only mark them if they have been explicitly loaded. */
4821 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4823 gfc_use_rename *u;
4825 /* Search the use/rename list for the variable; if the variable is
4826 found, mark it. */
4827 for (u = gfc_rename_list; u; u = u->next)
4829 if (strcmp (u->use_name, sym->name) == 0)
4831 sym->attr.use_only = 1;
4832 break;
4837 if (p->u.rsym.renamed)
4838 sym->attr.use_rename = 1;
4840 return 1;
4844 /* Recursive function for cleaning up things after a module has been read. */
4846 static void
4847 read_cleanup (pointer_info *p)
4849 gfc_symtree *st;
4850 pointer_info *q;
4852 if (p == NULL)
4853 return;
4855 read_cleanup (p->left);
4856 read_cleanup (p->right);
4858 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4860 gfc_namespace *ns;
4861 /* Add hidden symbols to the symtree. */
4862 q = get_integer (p->u.rsym.ns);
4863 ns = (gfc_namespace *) q->u.pointer;
4865 if (!p->u.rsym.sym->attr.vtype
4866 && !p->u.rsym.sym->attr.vtab)
4867 st = gfc_get_unique_symtree (ns);
4868 else
4870 /* There is no reason to use 'unique_symtrees' for vtabs or
4871 vtypes - their name is fine for a symtree and reduces the
4872 namespace pollution. */
4873 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4874 if (!st)
4875 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4878 st->n.sym = p->u.rsym.sym;
4879 st->n.sym->refs++;
4881 /* Fixup any symtree references. */
4882 p->u.rsym.symtree = st;
4883 resolve_fixups (p->u.rsym.stfixup, st);
4884 p->u.rsym.stfixup = NULL;
4887 /* Free unused symbols. */
4888 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4889 gfc_free_symbol (p->u.rsym.sym);
4893 /* It is not quite enough to check for ambiguity in the symbols by
4894 the loaded symbol and the new symbol not being identical. */
4895 static bool
4896 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
4898 gfc_symbol *rsym;
4899 module_locus locus;
4900 symbol_attribute attr;
4901 gfc_symbol *st_sym;
4903 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
4905 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4906 "current program unit", st->name, module_name);
4907 return true;
4910 st_sym = st->n.sym;
4911 rsym = info->u.rsym.sym;
4912 if (st_sym == rsym)
4913 return false;
4915 if (st_sym->attr.vtab || st_sym->attr.vtype)
4916 return false;
4918 /* If the existing symbol is generic from a different module and
4919 the new symbol is generic there can be no ambiguity. */
4920 if (st_sym->attr.generic
4921 && st_sym->module
4922 && st_sym->module != module_name)
4924 /* The new symbol's attributes have not yet been read. Since
4925 we need attr.generic, read it directly. */
4926 get_module_locus (&locus);
4927 set_module_locus (&info->u.rsym.where);
4928 mio_lparen ();
4929 attr.generic = 0;
4930 mio_symbol_attribute (&attr);
4931 set_module_locus (&locus);
4932 if (attr.generic)
4933 return false;
4936 return true;
4940 /* Read a module file. */
4942 static void
4943 read_module (void)
4945 module_locus operator_interfaces, user_operators, omp_udrs;
4946 const char *p;
4947 char name[GFC_MAX_SYMBOL_LEN + 1];
4948 int i;
4949 /* Workaround -Wmaybe-uninitialized false positive during
4950 profiledbootstrap by initializing them. */
4951 int ambiguous = 0, j, nuse, symbol = 0;
4952 pointer_info *info, *q;
4953 gfc_use_rename *u = NULL;
4954 gfc_symtree *st;
4955 gfc_symbol *sym;
4957 get_module_locus (&operator_interfaces); /* Skip these for now. */
4958 skip_list ();
4960 get_module_locus (&user_operators);
4961 skip_list ();
4962 skip_list ();
4964 /* Skip commons and equivalences for now. */
4965 skip_list ();
4966 skip_list ();
4968 /* Skip OpenMP UDRs. */
4969 get_module_locus (&omp_udrs);
4970 skip_list ();
4972 mio_lparen ();
4974 /* Create the fixup nodes for all the symbols. */
4976 while (peek_atom () != ATOM_RPAREN)
4978 char* bind_label;
4979 require_atom (ATOM_INTEGER);
4980 info = get_integer (atom_int);
4982 info->type = P_SYMBOL;
4983 info->u.rsym.state = UNUSED;
4985 info->u.rsym.true_name = read_string ();
4986 info->u.rsym.module = read_string ();
4987 bind_label = read_string ();
4988 if (strlen (bind_label))
4989 info->u.rsym.binding_label = bind_label;
4990 else
4991 XDELETEVEC (bind_label);
4993 require_atom (ATOM_INTEGER);
4994 info->u.rsym.ns = atom_int;
4996 get_module_locus (&info->u.rsym.where);
4998 /* See if the symbol has already been loaded by a previous module.
4999 If so, we reference the existing symbol and prevent it from
5000 being loaded again. This should not happen if the symbol being
5001 read is an index for an assumed shape dummy array (ns != 1). */
5003 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5005 if (sym == NULL
5006 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5008 skip_list ();
5009 continue;
5012 info->u.rsym.state = USED;
5013 info->u.rsym.sym = sym;
5014 /* The current symbol has already been loaded, so we can avoid loading
5015 it again. However, if it is a derived type, some of its components
5016 can be used in expressions in the module. To avoid the module loading
5017 failing, we need to associate the module's component pointer indexes
5018 with the existing symbol's component pointers. */
5019 if (sym->attr.flavor == FL_DERIVED)
5021 gfc_component *c;
5023 /* First seek to the symbol's component list. */
5024 mio_lparen (); /* symbol opening. */
5025 skip_list (); /* skip symbol attribute. */
5027 mio_lparen (); /* component list opening. */
5028 for (c = sym->components; c; c = c->next)
5030 pointer_info *p;
5031 const char *comp_name;
5032 int n;
5034 mio_lparen (); /* component opening. */
5035 mio_integer (&n);
5036 p = get_integer (n);
5037 if (p->u.pointer == NULL)
5038 associate_integer_pointer (p, c);
5039 mio_pool_string (&comp_name);
5040 gcc_assert (comp_name == c->name);
5041 skip_list (1); /* component end. */
5043 mio_rparen (); /* component list closing. */
5045 skip_list (1); /* symbol end. */
5047 else
5048 skip_list ();
5050 /* Some symbols do not have a namespace (eg. formal arguments),
5051 so the automatic "unique symtree" mechanism must be suppressed
5052 by marking them as referenced. */
5053 q = get_integer (info->u.rsym.ns);
5054 if (q->u.pointer == NULL)
5056 info->u.rsym.referenced = 1;
5057 continue;
5060 /* If possible recycle the symtree that references the symbol.
5061 If a symtree is not found and the module does not import one,
5062 a unique-name symtree is found by read_cleanup. */
5063 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
5064 if (st != NULL)
5066 info->u.rsym.symtree = st;
5067 info->u.rsym.referenced = 1;
5071 mio_rparen ();
5073 /* Parse the symtree lists. This lets us mark which symbols need to
5074 be loaded. Renaming is also done at this point by replacing the
5075 symtree name. */
5077 mio_lparen ();
5079 while (peek_atom () != ATOM_RPAREN)
5081 mio_internal_string (name);
5082 mio_integer (&ambiguous);
5083 mio_integer (&symbol);
5085 info = get_integer (symbol);
5087 /* See how many use names there are. If none, go through the start
5088 of the loop at least once. */
5089 nuse = number_use_names (name, false);
5090 info->u.rsym.renamed = nuse ? 1 : 0;
5092 if (nuse == 0)
5093 nuse = 1;
5095 for (j = 1; j <= nuse; j++)
5097 /* Get the jth local name for this symbol. */
5098 p = find_use_name_n (name, &j, false);
5100 if (p == NULL && strcmp (name, module_name) == 0)
5101 p = name;
5103 /* Exception: Always import vtabs & vtypes. */
5104 if (p == NULL && name[0] == '_'
5105 && (strncmp (name, "__vtab_", 5) == 0
5106 || strncmp (name, "__vtype_", 6) == 0))
5107 p = name;
5109 /* Skip symtree nodes not in an ONLY clause, unless there
5110 is an existing symtree loaded from another USE statement. */
5111 if (p == NULL)
5113 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5114 if (st != NULL
5115 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5116 && st->n.sym->module != NULL
5117 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5119 info->u.rsym.symtree = st;
5120 info->u.rsym.sym = st->n.sym;
5122 continue;
5125 /* If a symbol of the same name and module exists already,
5126 this symbol, which is not in an ONLY clause, must not be
5127 added to the namespace(11.3.2). Note that find_symbol
5128 only returns the first occurrence that it finds. */
5129 if (!only_flag && !info->u.rsym.renamed
5130 && strcmp (name, module_name) != 0
5131 && find_symbol (gfc_current_ns->sym_root, name,
5132 module_name, 0))
5133 continue;
5135 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5137 if (st != NULL)
5139 /* Check for ambiguous symbols. */
5140 if (check_for_ambiguous (st, info))
5141 st->ambiguous = 1;
5142 else
5143 info->u.rsym.symtree = st;
5145 else
5147 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5149 /* Create a symtree node in the current namespace for this
5150 symbol. */
5151 st = check_unique_name (p)
5152 ? gfc_get_unique_symtree (gfc_current_ns)
5153 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5154 st->ambiguous = ambiguous;
5156 sym = info->u.rsym.sym;
5158 /* Create a symbol node if it doesn't already exist. */
5159 if (sym == NULL)
5161 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5162 gfc_current_ns);
5163 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
5164 sym = info->u.rsym.sym;
5165 sym->module = gfc_get_string (info->u.rsym.module);
5167 if (info->u.rsym.binding_label)
5168 sym->binding_label =
5169 IDENTIFIER_POINTER (get_identifier
5170 (info->u.rsym.binding_label));
5173 st->n.sym = sym;
5174 st->n.sym->refs++;
5176 if (strcmp (name, p) != 0)
5177 sym->attr.use_rename = 1;
5179 if (name[0] != '_'
5180 || (strncmp (name, "__vtab_", 5) != 0
5181 && strncmp (name, "__vtype_", 6) != 0))
5182 sym->attr.use_only = only_flag;
5184 /* Store the symtree pointing to this symbol. */
5185 info->u.rsym.symtree = st;
5187 if (info->u.rsym.state == UNUSED)
5188 info->u.rsym.state = NEEDED;
5189 info->u.rsym.referenced = 1;
5194 mio_rparen ();
5196 /* Load intrinsic operator interfaces. */
5197 set_module_locus (&operator_interfaces);
5198 mio_lparen ();
5200 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5202 if (i == INTRINSIC_USER)
5203 continue;
5205 if (only_flag)
5207 u = find_use_operator ((gfc_intrinsic_op) i);
5209 if (u == NULL)
5211 skip_list ();
5212 continue;
5215 u->found = 1;
5218 mio_interface (&gfc_current_ns->op[i]);
5219 if (u && !gfc_current_ns->op[i])
5220 u->found = 0;
5223 mio_rparen ();
5225 /* Load generic and user operator interfaces. These must follow the
5226 loading of symtree because otherwise symbols can be marked as
5227 ambiguous. */
5229 set_module_locus (&user_operators);
5231 load_operator_interfaces ();
5232 load_generic_interfaces ();
5234 load_commons ();
5235 load_equiv ();
5237 /* Load OpenMP user defined reductions. */
5238 set_module_locus (&omp_udrs);
5239 load_omp_udrs ();
5241 /* At this point, we read those symbols that are needed but haven't
5242 been loaded yet. If one symbol requires another, the other gets
5243 marked as NEEDED if its previous state was UNUSED. */
5245 while (load_needed (pi_root));
5247 /* Make sure all elements of the rename-list were found in the module. */
5249 for (u = gfc_rename_list; u; u = u->next)
5251 if (u->found)
5252 continue;
5254 if (u->op == INTRINSIC_NONE)
5256 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5257 u->use_name, &u->where, module_name);
5258 continue;
5261 if (u->op == INTRINSIC_USER)
5263 gfc_error ("User operator %qs referenced at %L not found "
5264 "in module %qs", u->use_name, &u->where, module_name);
5265 continue;
5268 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5269 "in module %qs", gfc_op2string (u->op), &u->where,
5270 module_name);
5273 /* Clean up symbol nodes that were never loaded, create references
5274 to hidden symbols. */
5276 read_cleanup (pi_root);
5280 /* Given an access type that is specific to an entity and the default
5281 access, return nonzero if the entity is publicly accessible. If the
5282 element is declared as PUBLIC, then it is public; if declared
5283 PRIVATE, then private, and otherwise it is public unless the default
5284 access in this context has been declared PRIVATE. */
5286 static bool
5287 check_access (gfc_access specific_access, gfc_access default_access)
5289 if (specific_access == ACCESS_PUBLIC)
5290 return TRUE;
5291 if (specific_access == ACCESS_PRIVATE)
5292 return FALSE;
5294 if (flag_module_private)
5295 return default_access == ACCESS_PUBLIC;
5296 else
5297 return default_access != ACCESS_PRIVATE;
5301 bool
5302 gfc_check_symbol_access (gfc_symbol *sym)
5304 if (sym->attr.vtab || sym->attr.vtype)
5305 return true;
5306 else
5307 return check_access (sym->attr.access, sym->ns->default_access);
5311 /* A structure to remember which commons we've already written. */
5313 struct written_common
5315 BBT_HEADER(written_common);
5316 const char *name, *label;
5319 static struct written_common *written_commons = NULL;
5321 /* Comparison function used for balancing the binary tree. */
5323 static int
5324 compare_written_commons (void *a1, void *b1)
5326 const char *aname = ((struct written_common *) a1)->name;
5327 const char *alabel = ((struct written_common *) a1)->label;
5328 const char *bname = ((struct written_common *) b1)->name;
5329 const char *blabel = ((struct written_common *) b1)->label;
5330 int c = strcmp (aname, bname);
5332 return (c != 0 ? c : strcmp (alabel, blabel));
5335 /* Free a list of written commons. */
5337 static void
5338 free_written_common (struct written_common *w)
5340 if (!w)
5341 return;
5343 if (w->left)
5344 free_written_common (w->left);
5345 if (w->right)
5346 free_written_common (w->right);
5348 free (w);
5351 /* Write a common block to the module -- recursive helper function. */
5353 static void
5354 write_common_0 (gfc_symtree *st, bool this_module)
5356 gfc_common_head *p;
5357 const char * name;
5358 int flags;
5359 const char *label;
5360 struct written_common *w;
5361 bool write_me = true;
5363 if (st == NULL)
5364 return;
5366 write_common_0 (st->left, this_module);
5368 /* We will write out the binding label, or "" if no label given. */
5369 name = st->n.common->name;
5370 p = st->n.common;
5371 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5373 /* Check if we've already output this common. */
5374 w = written_commons;
5375 while (w)
5377 int c = strcmp (name, w->name);
5378 c = (c != 0 ? c : strcmp (label, w->label));
5379 if (c == 0)
5380 write_me = false;
5382 w = (c < 0) ? w->left : w->right;
5385 if (this_module && p->use_assoc)
5386 write_me = false;
5388 if (write_me)
5390 /* Write the common to the module. */
5391 mio_lparen ();
5392 mio_pool_string (&name);
5394 mio_symbol_ref (&p->head);
5395 flags = p->saved ? 1 : 0;
5396 if (p->threadprivate)
5397 flags |= 2;
5398 mio_integer (&flags);
5400 /* Write out whether the common block is bind(c) or not. */
5401 mio_integer (&(p->is_bind_c));
5403 mio_pool_string (&label);
5404 mio_rparen ();
5406 /* Record that we have written this common. */
5407 w = XCNEW (struct written_common);
5408 w->name = p->name;
5409 w->label = label;
5410 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5413 write_common_0 (st->right, this_module);
5417 /* Write a common, by initializing the list of written commons, calling
5418 the recursive function write_common_0() and cleaning up afterwards. */
5420 static void
5421 write_common (gfc_symtree *st)
5423 written_commons = NULL;
5424 write_common_0 (st, true);
5425 write_common_0 (st, false);
5426 free_written_common (written_commons);
5427 written_commons = NULL;
5431 /* Write the blank common block to the module. */
5433 static void
5434 write_blank_common (void)
5436 const char * name = BLANK_COMMON_NAME;
5437 int saved;
5438 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5439 this, but it hasn't been checked. Just making it so for now. */
5440 int is_bind_c = 0;
5442 if (gfc_current_ns->blank_common.head == NULL)
5443 return;
5445 mio_lparen ();
5447 mio_pool_string (&name);
5449 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5450 saved = gfc_current_ns->blank_common.saved;
5451 mio_integer (&saved);
5453 /* Write out whether the common block is bind(c) or not. */
5454 mio_integer (&is_bind_c);
5456 /* Write out an empty binding label. */
5457 write_atom (ATOM_STRING, "");
5459 mio_rparen ();
5463 /* Write equivalences to the module. */
5465 static void
5466 write_equiv (void)
5468 gfc_equiv *eq, *e;
5469 int num;
5471 num = 0;
5472 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5474 mio_lparen ();
5476 for (e = eq; e; e = e->eq)
5478 if (e->module == NULL)
5479 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5480 mio_allocated_string (e->module);
5481 mio_expr (&e->expr);
5484 num++;
5485 mio_rparen ();
5490 /* Write a symbol to the module. */
5492 static void
5493 write_symbol (int n, gfc_symbol *sym)
5495 const char *label;
5497 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5498 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5500 mio_integer (&n);
5502 if (sym->attr.flavor == FL_DERIVED)
5504 const char *name;
5505 name = dt_upper_string (sym->name);
5506 mio_pool_string (&name);
5508 else
5509 mio_pool_string (&sym->name);
5511 mio_pool_string (&sym->module);
5512 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5514 label = sym->binding_label;
5515 mio_pool_string (&label);
5517 else
5518 write_atom (ATOM_STRING, "");
5520 mio_pointer_ref (&sym->ns);
5522 mio_symbol (sym);
5523 write_char ('\n');
5527 /* Recursive traversal function to write the initial set of symbols to
5528 the module. We check to see if the symbol should be written
5529 according to the access specification. */
5531 static void
5532 write_symbol0 (gfc_symtree *st)
5534 gfc_symbol *sym;
5535 pointer_info *p;
5536 bool dont_write = false;
5538 if (st == NULL)
5539 return;
5541 write_symbol0 (st->left);
5543 sym = st->n.sym;
5544 if (sym->module == NULL)
5545 sym->module = module_name;
5547 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5548 && !sym->attr.subroutine && !sym->attr.function)
5549 dont_write = true;
5551 if (!gfc_check_symbol_access (sym))
5552 dont_write = true;
5554 if (!dont_write)
5556 p = get_pointer (sym);
5557 if (p->type == P_UNKNOWN)
5558 p->type = P_SYMBOL;
5560 if (p->u.wsym.state != WRITTEN)
5562 write_symbol (p->integer, sym);
5563 p->u.wsym.state = WRITTEN;
5567 write_symbol0 (st->right);
5571 static void
5572 write_omp_udr (gfc_omp_udr *udr)
5574 switch (udr->rop)
5576 case OMP_REDUCTION_USER:
5577 /* Non-operators can't be used outside of the module. */
5578 if (udr->name[0] != '.')
5579 return;
5580 else
5582 gfc_symtree *st;
5583 size_t len = strlen (udr->name + 1);
5584 char *name = XALLOCAVEC (char, len);
5585 memcpy (name, udr->name, len - 1);
5586 name[len - 1] = '\0';
5587 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5588 /* If corresponding user operator is private, don't write
5589 the UDR. */
5590 if (st != NULL)
5592 gfc_user_op *uop = st->n.uop;
5593 if (!check_access (uop->access, uop->ns->default_access))
5594 return;
5597 break;
5598 case OMP_REDUCTION_PLUS:
5599 case OMP_REDUCTION_MINUS:
5600 case OMP_REDUCTION_TIMES:
5601 case OMP_REDUCTION_AND:
5602 case OMP_REDUCTION_OR:
5603 case OMP_REDUCTION_EQV:
5604 case OMP_REDUCTION_NEQV:
5605 /* If corresponding operator is private, don't write the UDR. */
5606 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5607 gfc_current_ns->default_access))
5608 return;
5609 break;
5610 default:
5611 break;
5613 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5615 /* If derived type is private, don't write the UDR. */
5616 if (!gfc_check_symbol_access (udr->ts.u.derived))
5617 return;
5620 mio_lparen ();
5621 mio_pool_string (&udr->name);
5622 mio_typespec (&udr->ts);
5623 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5624 if (udr->initializer_ns)
5625 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5626 udr->initializer_ns, true);
5627 mio_rparen ();
5631 static void
5632 write_omp_udrs (gfc_symtree *st)
5634 if (st == NULL)
5635 return;
5637 write_omp_udrs (st->left);
5638 gfc_omp_udr *udr;
5639 for (udr = st->n.omp_udr; udr; udr = udr->next)
5640 write_omp_udr (udr);
5641 write_omp_udrs (st->right);
5645 /* Type for the temporary tree used when writing secondary symbols. */
5647 struct sorted_pointer_info
5649 BBT_HEADER (sorted_pointer_info);
5651 pointer_info *p;
5654 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5656 /* Recursively traverse the temporary tree, free its contents. */
5658 static void
5659 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5661 if (!p)
5662 return;
5664 free_sorted_pointer_info_tree (p->left);
5665 free_sorted_pointer_info_tree (p->right);
5667 free (p);
5670 /* Comparison function for the temporary tree. */
5672 static int
5673 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5675 sorted_pointer_info *spi1, *spi2;
5676 spi1 = (sorted_pointer_info *)_spi1;
5677 spi2 = (sorted_pointer_info *)_spi2;
5679 if (spi1->p->integer < spi2->p->integer)
5680 return -1;
5681 if (spi1->p->integer > spi2->p->integer)
5682 return 1;
5683 return 0;
5687 /* Finds the symbols that need to be written and collects them in the
5688 sorted_pi tree so that they can be traversed in an order
5689 independent of memory addresses. */
5691 static void
5692 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5694 if (!p)
5695 return;
5697 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5699 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5700 sp->p = p;
5702 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5705 find_symbols_to_write (tree, p->left);
5706 find_symbols_to_write (tree, p->right);
5710 /* Recursive function that traverses the tree of symbols that need to be
5711 written and writes them in order. */
5713 static void
5714 write_symbol1_recursion (sorted_pointer_info *sp)
5716 if (!sp)
5717 return;
5719 write_symbol1_recursion (sp->left);
5721 pointer_info *p1 = sp->p;
5722 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5724 p1->u.wsym.state = WRITTEN;
5725 write_symbol (p1->integer, p1->u.wsym.sym);
5726 p1->u.wsym.sym->attr.public_used = 1;
5728 write_symbol1_recursion (sp->right);
5732 /* Write the secondary set of symbols to the module file. These are
5733 symbols that were not public yet are needed by the public symbols
5734 or another dependent symbol. The act of writing a symbol can add
5735 symbols to the pointer_info tree, so we return nonzero if a symbol
5736 was written and pass that information upwards. The caller will
5737 then call this function again until nothing was written. It uses
5738 the utility functions and a temporary tree to ensure a reproducible
5739 ordering of the symbol output and thus the module file. */
5741 static int
5742 write_symbol1 (pointer_info *p)
5744 if (!p)
5745 return 0;
5747 /* Put symbols that need to be written into a tree sorted on the
5748 integer field. */
5750 sorted_pointer_info *spi_root = NULL;
5751 find_symbols_to_write (&spi_root, p);
5753 /* No symbols to write, return. */
5754 if (!spi_root)
5755 return 0;
5757 /* Otherwise, write and free the tree again. */
5758 write_symbol1_recursion (spi_root);
5759 free_sorted_pointer_info_tree (spi_root);
5761 return 1;
5765 /* Write operator interfaces associated with a symbol. */
5767 static void
5768 write_operator (gfc_user_op *uop)
5770 static char nullstring[] = "";
5771 const char *p = nullstring;
5773 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5774 return;
5776 mio_symbol_interface (&uop->name, &p, &uop->op);
5780 /* Write generic interfaces from the namespace sym_root. */
5782 static void
5783 write_generic (gfc_symtree *st)
5785 gfc_symbol *sym;
5787 if (st == NULL)
5788 return;
5790 write_generic (st->left);
5792 sym = st->n.sym;
5793 if (sym && !check_unique_name (st->name)
5794 && sym->generic && gfc_check_symbol_access (sym))
5796 if (!sym->module)
5797 sym->module = module_name;
5799 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5802 write_generic (st->right);
5806 static void
5807 write_symtree (gfc_symtree *st)
5809 gfc_symbol *sym;
5810 pointer_info *p;
5812 sym = st->n.sym;
5814 /* A symbol in an interface body must not be visible in the
5815 module file. */
5816 if (sym->ns != gfc_current_ns
5817 && sym->ns->proc_name
5818 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5819 return;
5821 if (!gfc_check_symbol_access (sym)
5822 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5823 && !sym->attr.subroutine && !sym->attr.function))
5824 return;
5826 if (check_unique_name (st->name))
5827 return;
5829 p = find_pointer (sym);
5830 if (p == NULL)
5831 gfc_internal_error ("write_symtree(): Symbol not written");
5833 mio_pool_string (&st->name);
5834 mio_integer (&st->ambiguous);
5835 mio_integer (&p->integer);
5839 static void
5840 write_module (void)
5842 int i;
5844 /* Write the operator interfaces. */
5845 mio_lparen ();
5847 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5849 if (i == INTRINSIC_USER)
5850 continue;
5852 mio_interface (check_access (gfc_current_ns->operator_access[i],
5853 gfc_current_ns->default_access)
5854 ? &gfc_current_ns->op[i] : NULL);
5857 mio_rparen ();
5858 write_char ('\n');
5859 write_char ('\n');
5861 mio_lparen ();
5862 gfc_traverse_user_op (gfc_current_ns, write_operator);
5863 mio_rparen ();
5864 write_char ('\n');
5865 write_char ('\n');
5867 mio_lparen ();
5868 write_generic (gfc_current_ns->sym_root);
5869 mio_rparen ();
5870 write_char ('\n');
5871 write_char ('\n');
5873 mio_lparen ();
5874 write_blank_common ();
5875 write_common (gfc_current_ns->common_root);
5876 mio_rparen ();
5877 write_char ('\n');
5878 write_char ('\n');
5880 mio_lparen ();
5881 write_equiv ();
5882 mio_rparen ();
5883 write_char ('\n');
5884 write_char ('\n');
5886 mio_lparen ();
5887 write_omp_udrs (gfc_current_ns->omp_udr_root);
5888 mio_rparen ();
5889 write_char ('\n');
5890 write_char ('\n');
5892 /* Write symbol information. First we traverse all symbols in the
5893 primary namespace, writing those that need to be written.
5894 Sometimes writing one symbol will cause another to need to be
5895 written. A list of these symbols ends up on the write stack, and
5896 we end by popping the bottom of the stack and writing the symbol
5897 until the stack is empty. */
5899 mio_lparen ();
5901 write_symbol0 (gfc_current_ns->sym_root);
5902 while (write_symbol1 (pi_root))
5903 /* Nothing. */;
5905 mio_rparen ();
5907 write_char ('\n');
5908 write_char ('\n');
5910 mio_lparen ();
5911 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5912 mio_rparen ();
5916 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5917 true on success, false on failure. */
5919 static bool
5920 read_crc32_from_module_file (const char* filename, uLong* crc)
5922 FILE *file;
5923 char buf[4];
5924 unsigned int val;
5926 /* Open the file in binary mode. */
5927 if ((file = fopen (filename, "rb")) == NULL)
5928 return false;
5930 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5931 file. See RFC 1952. */
5932 if (fseek (file, -8, SEEK_END) != 0)
5934 fclose (file);
5935 return false;
5938 /* Read the CRC32. */
5939 if (fread (buf, 1, 4, file) != 4)
5941 fclose (file);
5942 return false;
5945 /* Close the file. */
5946 fclose (file);
5948 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5949 + ((buf[3] & 0xFF) << 24);
5950 *crc = val;
5952 /* For debugging, the CRC value printed in hexadecimal should match
5953 the CRC printed by "zcat -l -v filename".
5954 printf("CRC of file %s is %x\n", filename, val); */
5956 return true;
5960 /* Given module, dump it to disk. If there was an error while
5961 processing the module, dump_flag will be set to zero and we delete
5962 the module file, even if it was already there. */
5964 void
5965 gfc_dump_module (const char *name, int dump_flag)
5967 int n;
5968 char *filename, *filename_tmp;
5969 uLong crc, crc_old;
5971 module_name = gfc_get_string (name);
5973 if (gfc_state_stack->state == COMP_SUBMODULE)
5975 name = submodule_name;
5976 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
5978 else
5979 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5981 if (gfc_option.module_dir != NULL)
5983 n += strlen (gfc_option.module_dir);
5984 filename = (char *) alloca (n);
5985 strcpy (filename, gfc_option.module_dir);
5986 strcat (filename, name);
5988 else
5990 filename = (char *) alloca (n);
5991 strcpy (filename, name);
5994 if (gfc_state_stack->state == COMP_SUBMODULE)
5995 strcat (filename, SUBMODULE_EXTENSION);
5996 else
5997 strcat (filename, MODULE_EXTENSION);
5999 /* Name of the temporary file used to write the module. */
6000 filename_tmp = (char *) alloca (n + 1);
6001 strcpy (filename_tmp, filename);
6002 strcat (filename_tmp, "0");
6004 /* There was an error while processing the module. We delete the
6005 module file, even if it was already there. */
6006 if (!dump_flag)
6008 remove (filename);
6009 return;
6012 if (gfc_cpp_makedep ())
6013 gfc_cpp_add_target (filename);
6015 /* Write the module to the temporary file. */
6016 module_fp = gzopen (filename_tmp, "w");
6017 if (module_fp == NULL)
6018 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6019 filename_tmp, xstrerror (errno));
6021 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6022 MOD_VERSION, gfc_source_file);
6024 /* Write the module itself. */
6025 iomode = IO_OUTPUT;
6027 init_pi_tree ();
6029 write_module ();
6031 free_pi_tree (pi_root);
6032 pi_root = NULL;
6034 write_char ('\n');
6036 if (gzclose (module_fp))
6037 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6038 filename_tmp, xstrerror (errno));
6040 /* Read the CRC32 from the gzip trailers of the module files and
6041 compare. */
6042 if (!read_crc32_from_module_file (filename_tmp, &crc)
6043 || !read_crc32_from_module_file (filename, &crc_old)
6044 || crc_old != crc)
6046 /* Module file have changed, replace the old one. */
6047 if (remove (filename) && errno != ENOENT)
6048 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6049 xstrerror (errno));
6050 if (rename (filename_tmp, filename))
6051 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6052 filename_tmp, filename, xstrerror (errno));
6054 else
6056 if (remove (filename_tmp))
6057 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6058 filename_tmp, xstrerror (errno));
6063 static void
6064 create_intrinsic_function (const char *name, int id,
6065 const char *modname, intmod_id module,
6066 bool subroutine, gfc_symbol *result_type)
6068 gfc_intrinsic_sym *isym;
6069 gfc_symtree *tmp_symtree;
6070 gfc_symbol *sym;
6072 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6073 if (tmp_symtree)
6075 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6076 return;
6077 gfc_error ("Symbol %qs already declared", name);
6080 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6081 sym = tmp_symtree->n.sym;
6083 if (subroutine)
6085 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6086 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6087 sym->attr.subroutine = 1;
6089 else
6091 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6092 isym = gfc_intrinsic_function_by_id (isym_id);
6094 sym->attr.function = 1;
6095 if (result_type)
6097 sym->ts.type = BT_DERIVED;
6098 sym->ts.u.derived = result_type;
6099 sym->ts.is_c_interop = 1;
6100 isym->ts.f90_type = BT_VOID;
6101 isym->ts.type = BT_DERIVED;
6102 isym->ts.f90_type = BT_VOID;
6103 isym->ts.u.derived = result_type;
6104 isym->ts.is_c_interop = 1;
6107 gcc_assert (isym);
6109 sym->attr.flavor = FL_PROCEDURE;
6110 sym->attr.intrinsic = 1;
6112 sym->module = gfc_get_string (modname);
6113 sym->attr.use_assoc = 1;
6114 sym->from_intmod = module;
6115 sym->intmod_sym_id = id;
6119 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6120 the current namespace for all named constants, pointer types, and
6121 procedures in the module unless the only clause was used or a rename
6122 list was provided. */
6124 static void
6125 import_iso_c_binding_module (void)
6127 gfc_symbol *mod_sym = NULL, *return_type;
6128 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6129 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6130 const char *iso_c_module_name = "__iso_c_binding";
6131 gfc_use_rename *u;
6132 int i;
6133 bool want_c_ptr = false, want_c_funptr = false;
6135 /* Look only in the current namespace. */
6136 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6138 if (mod_symtree == NULL)
6140 /* symtree doesn't already exist in current namespace. */
6141 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6142 false);
6144 if (mod_symtree != NULL)
6145 mod_sym = mod_symtree->n.sym;
6146 else
6147 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6148 "create symbol for %s", iso_c_module_name);
6150 mod_sym->attr.flavor = FL_MODULE;
6151 mod_sym->attr.intrinsic = 1;
6152 mod_sym->module = gfc_get_string (iso_c_module_name);
6153 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6156 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6157 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6158 need C_(FUN)PTR. */
6159 for (u = gfc_rename_list; u; u = u->next)
6161 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6162 u->use_name) == 0)
6163 want_c_ptr = true;
6164 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6165 u->use_name) == 0)
6166 want_c_ptr = true;
6167 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6168 u->use_name) == 0)
6169 want_c_funptr = true;
6170 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6171 u->use_name) == 0)
6172 want_c_funptr = true;
6173 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6174 u->use_name) == 0)
6176 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6177 (iso_c_binding_symbol)
6178 ISOCBINDING_PTR,
6179 u->local_name[0] ? u->local_name
6180 : u->use_name,
6181 NULL, false);
6183 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6184 u->use_name) == 0)
6186 c_funptr
6187 = generate_isocbinding_symbol (iso_c_module_name,
6188 (iso_c_binding_symbol)
6189 ISOCBINDING_FUNPTR,
6190 u->local_name[0] ? u->local_name
6191 : u->use_name,
6192 NULL, false);
6196 if ((want_c_ptr || !only_flag) && !c_ptr)
6197 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6198 (iso_c_binding_symbol)
6199 ISOCBINDING_PTR,
6200 NULL, NULL, only_flag);
6201 if ((want_c_funptr || !only_flag) && !c_funptr)
6202 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6203 (iso_c_binding_symbol)
6204 ISOCBINDING_FUNPTR,
6205 NULL, NULL, only_flag);
6207 /* Generate the symbols for the named constants representing
6208 the kinds for intrinsic data types. */
6209 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6211 bool found = false;
6212 for (u = gfc_rename_list; u; u = u->next)
6213 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6215 bool not_in_std;
6216 const char *name;
6217 u->found = 1;
6218 found = true;
6220 switch (i)
6222 #define NAMED_FUNCTION(a,b,c,d) \
6223 case a: \
6224 not_in_std = (gfc_option.allow_std & d) == 0; \
6225 name = b; \
6226 break;
6227 #define NAMED_SUBROUTINE(a,b,c,d) \
6228 case a: \
6229 not_in_std = (gfc_option.allow_std & d) == 0; \
6230 name = b; \
6231 break;
6232 #define NAMED_INTCST(a,b,c,d) \
6233 case a: \
6234 not_in_std = (gfc_option.allow_std & d) == 0; \
6235 name = b; \
6236 break;
6237 #define NAMED_REALCST(a,b,c,d) \
6238 case a: \
6239 not_in_std = (gfc_option.allow_std & d) == 0; \
6240 name = b; \
6241 break;
6242 #define NAMED_CMPXCST(a,b,c,d) \
6243 case a: \
6244 not_in_std = (gfc_option.allow_std & d) == 0; \
6245 name = b; \
6246 break;
6247 #include "iso-c-binding.def"
6248 default:
6249 not_in_std = false;
6250 name = "";
6253 if (not_in_std)
6255 gfc_error ("The symbol %qs, referenced at %L, is not "
6256 "in the selected standard", name, &u->where);
6257 continue;
6260 switch (i)
6262 #define NAMED_FUNCTION(a,b,c,d) \
6263 case a: \
6264 if (a == ISOCBINDING_LOC) \
6265 return_type = c_ptr->n.sym; \
6266 else if (a == ISOCBINDING_FUNLOC) \
6267 return_type = c_funptr->n.sym; \
6268 else \
6269 return_type = NULL; \
6270 create_intrinsic_function (u->local_name[0] \
6271 ? u->local_name : u->use_name, \
6272 a, iso_c_module_name, \
6273 INTMOD_ISO_C_BINDING, false, \
6274 return_type); \
6275 break;
6276 #define NAMED_SUBROUTINE(a,b,c,d) \
6277 case a: \
6278 create_intrinsic_function (u->local_name[0] ? u->local_name \
6279 : u->use_name, \
6280 a, iso_c_module_name, \
6281 INTMOD_ISO_C_BINDING, true, NULL); \
6282 break;
6283 #include "iso-c-binding.def"
6285 case ISOCBINDING_PTR:
6286 case ISOCBINDING_FUNPTR:
6287 /* Already handled above. */
6288 break;
6289 default:
6290 if (i == ISOCBINDING_NULL_PTR)
6291 tmp_symtree = c_ptr;
6292 else if (i == ISOCBINDING_NULL_FUNPTR)
6293 tmp_symtree = c_funptr;
6294 else
6295 tmp_symtree = NULL;
6296 generate_isocbinding_symbol (iso_c_module_name,
6297 (iso_c_binding_symbol) i,
6298 u->local_name[0]
6299 ? u->local_name : u->use_name,
6300 tmp_symtree, false);
6304 if (!found && !only_flag)
6306 /* Skip, if the symbol is not in the enabled standard. */
6307 switch (i)
6309 #define NAMED_FUNCTION(a,b,c,d) \
6310 case a: \
6311 if ((gfc_option.allow_std & d) == 0) \
6312 continue; \
6313 break;
6314 #define NAMED_SUBROUTINE(a,b,c,d) \
6315 case a: \
6316 if ((gfc_option.allow_std & d) == 0) \
6317 continue; \
6318 break;
6319 #define NAMED_INTCST(a,b,c,d) \
6320 case a: \
6321 if ((gfc_option.allow_std & d) == 0) \
6322 continue; \
6323 break;
6324 #define NAMED_REALCST(a,b,c,d) \
6325 case a: \
6326 if ((gfc_option.allow_std & d) == 0) \
6327 continue; \
6328 break;
6329 #define NAMED_CMPXCST(a,b,c,d) \
6330 case a: \
6331 if ((gfc_option.allow_std & d) == 0) \
6332 continue; \
6333 break;
6334 #include "iso-c-binding.def"
6335 default:
6336 ; /* Not GFC_STD_* versioned. */
6339 switch (i)
6341 #define NAMED_FUNCTION(a,b,c,d) \
6342 case a: \
6343 if (a == ISOCBINDING_LOC) \
6344 return_type = c_ptr->n.sym; \
6345 else if (a == ISOCBINDING_FUNLOC) \
6346 return_type = c_funptr->n.sym; \
6347 else \
6348 return_type = NULL; \
6349 create_intrinsic_function (b, a, iso_c_module_name, \
6350 INTMOD_ISO_C_BINDING, false, \
6351 return_type); \
6352 break;
6353 #define NAMED_SUBROUTINE(a,b,c,d) \
6354 case a: \
6355 create_intrinsic_function (b, a, iso_c_module_name, \
6356 INTMOD_ISO_C_BINDING, true, NULL); \
6357 break;
6358 #include "iso-c-binding.def"
6360 case ISOCBINDING_PTR:
6361 case ISOCBINDING_FUNPTR:
6362 /* Already handled above. */
6363 break;
6364 default:
6365 if (i == ISOCBINDING_NULL_PTR)
6366 tmp_symtree = c_ptr;
6367 else if (i == ISOCBINDING_NULL_FUNPTR)
6368 tmp_symtree = c_funptr;
6369 else
6370 tmp_symtree = NULL;
6371 generate_isocbinding_symbol (iso_c_module_name,
6372 (iso_c_binding_symbol) i, NULL,
6373 tmp_symtree, false);
6378 for (u = gfc_rename_list; u; u = u->next)
6380 if (u->found)
6381 continue;
6383 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6384 "module ISO_C_BINDING", u->use_name, &u->where);
6389 /* Add an integer named constant from a given module. */
6391 static void
6392 create_int_parameter (const char *name, int value, const char *modname,
6393 intmod_id module, int id)
6395 gfc_symtree *tmp_symtree;
6396 gfc_symbol *sym;
6398 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6399 if (tmp_symtree != NULL)
6401 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6402 return;
6403 else
6404 gfc_error ("Symbol %qs already declared", name);
6407 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6408 sym = tmp_symtree->n.sym;
6410 sym->module = gfc_get_string (modname);
6411 sym->attr.flavor = FL_PARAMETER;
6412 sym->ts.type = BT_INTEGER;
6413 sym->ts.kind = gfc_default_integer_kind;
6414 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6415 sym->attr.use_assoc = 1;
6416 sym->from_intmod = module;
6417 sym->intmod_sym_id = id;
6421 /* Value is already contained by the array constructor, but not
6422 yet the shape. */
6424 static void
6425 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6426 const char *modname, intmod_id module, int id)
6428 gfc_symtree *tmp_symtree;
6429 gfc_symbol *sym;
6431 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6432 if (tmp_symtree != NULL)
6434 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6435 return;
6436 else
6437 gfc_error ("Symbol %qs already declared", name);
6440 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6441 sym = tmp_symtree->n.sym;
6443 sym->module = gfc_get_string (modname);
6444 sym->attr.flavor = FL_PARAMETER;
6445 sym->ts.type = BT_INTEGER;
6446 sym->ts.kind = gfc_default_integer_kind;
6447 sym->attr.use_assoc = 1;
6448 sym->from_intmod = module;
6449 sym->intmod_sym_id = id;
6450 sym->attr.dimension = 1;
6451 sym->as = gfc_get_array_spec ();
6452 sym->as->rank = 1;
6453 sym->as->type = AS_EXPLICIT;
6454 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6455 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6457 sym->value = value;
6458 sym->value->shape = gfc_get_shape (1);
6459 mpz_init_set_ui (sym->value->shape[0], size);
6463 /* Add an derived type for a given module. */
6465 static void
6466 create_derived_type (const char *name, const char *modname,
6467 intmod_id module, int id)
6469 gfc_symtree *tmp_symtree;
6470 gfc_symbol *sym, *dt_sym;
6471 gfc_interface *intr, *head;
6473 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6474 if (tmp_symtree != NULL)
6476 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6477 return;
6478 else
6479 gfc_error ("Symbol %qs already declared", name);
6482 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6483 sym = tmp_symtree->n.sym;
6484 sym->module = gfc_get_string (modname);
6485 sym->from_intmod = module;
6486 sym->intmod_sym_id = id;
6487 sym->attr.flavor = FL_PROCEDURE;
6488 sym->attr.function = 1;
6489 sym->attr.generic = 1;
6491 gfc_get_sym_tree (dt_upper_string (sym->name),
6492 gfc_current_ns, &tmp_symtree, false);
6493 dt_sym = tmp_symtree->n.sym;
6494 dt_sym->name = gfc_get_string (sym->name);
6495 dt_sym->attr.flavor = FL_DERIVED;
6496 dt_sym->attr.private_comp = 1;
6497 dt_sym->attr.zero_comp = 1;
6498 dt_sym->attr.use_assoc = 1;
6499 dt_sym->module = gfc_get_string (modname);
6500 dt_sym->from_intmod = module;
6501 dt_sym->intmod_sym_id = id;
6503 head = sym->generic;
6504 intr = gfc_get_interface ();
6505 intr->sym = dt_sym;
6506 intr->where = gfc_current_locus;
6507 intr->next = head;
6508 sym->generic = intr;
6509 sym->attr.if_source = IFSRC_DECL;
6513 /* Read the contents of the module file into a temporary buffer. */
6515 static void
6516 read_module_to_tmpbuf ()
6518 /* We don't know the uncompressed size, so enlarge the buffer as
6519 needed. */
6520 int cursz = 4096;
6521 int rsize = cursz;
6522 int len = 0;
6524 module_content = XNEWVEC (char, cursz);
6526 while (1)
6528 int nread = gzread (module_fp, module_content + len, rsize);
6529 len += nread;
6530 if (nread < rsize)
6531 break;
6532 cursz *= 2;
6533 module_content = XRESIZEVEC (char, module_content, cursz);
6534 rsize = cursz - len;
6537 module_content = XRESIZEVEC (char, module_content, len + 1);
6538 module_content[len] = '\0';
6540 module_pos = 0;
6544 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6546 static void
6547 use_iso_fortran_env_module (void)
6549 static char mod[] = "iso_fortran_env";
6550 gfc_use_rename *u;
6551 gfc_symbol *mod_sym;
6552 gfc_symtree *mod_symtree;
6553 gfc_expr *expr;
6554 int i, j;
6556 intmod_sym symbol[] = {
6557 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6558 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6559 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6560 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6561 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6562 #include "iso-fortran-env.def"
6563 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6565 i = 0;
6566 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6567 #include "iso-fortran-env.def"
6569 /* Generate the symbol for the module itself. */
6570 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6571 if (mod_symtree == NULL)
6573 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6574 gcc_assert (mod_symtree);
6575 mod_sym = mod_symtree->n.sym;
6577 mod_sym->attr.flavor = FL_MODULE;
6578 mod_sym->attr.intrinsic = 1;
6579 mod_sym->module = gfc_get_string (mod);
6580 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6582 else
6583 if (!mod_symtree->n.sym->attr.intrinsic)
6584 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6585 "non-intrinsic module name used previously", mod);
6587 /* Generate the symbols for the module integer named constants. */
6589 for (i = 0; symbol[i].name; i++)
6591 bool found = false;
6592 for (u = gfc_rename_list; u; u = u->next)
6594 if (strcmp (symbol[i].name, u->use_name) == 0)
6596 found = true;
6597 u->found = 1;
6599 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6600 "referenced at %L, is not in the selected "
6601 "standard", symbol[i].name, &u->where))
6602 continue;
6604 if ((flag_default_integer || flag_default_real)
6605 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6606 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
6607 "constant from intrinsic module "
6608 "ISO_FORTRAN_ENV at %L is incompatible with "
6609 "option %qs", &u->where,
6610 flag_default_integer
6611 ? "-fdefault-integer-8"
6612 : "-fdefault-real-8");
6613 switch (symbol[i].id)
6615 #define NAMED_INTCST(a,b,c,d) \
6616 case a:
6617 #include "iso-fortran-env.def"
6618 create_int_parameter (u->local_name[0] ? u->local_name
6619 : u->use_name,
6620 symbol[i].value, mod,
6621 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6622 break;
6624 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6625 case a:\
6626 expr = gfc_get_array_expr (BT_INTEGER, \
6627 gfc_default_integer_kind,\
6628 NULL); \
6629 for (j = 0; KINDS[j].kind != 0; j++) \
6630 gfc_constructor_append_expr (&expr->value.constructor, \
6631 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6632 KINDS[j].kind), NULL); \
6633 create_int_parameter_array (u->local_name[0] ? u->local_name \
6634 : u->use_name, \
6635 j, expr, mod, \
6636 INTMOD_ISO_FORTRAN_ENV, \
6637 symbol[i].id); \
6638 break;
6639 #include "iso-fortran-env.def"
6641 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6642 case a:
6643 #include "iso-fortran-env.def"
6644 create_derived_type (u->local_name[0] ? u->local_name
6645 : u->use_name,
6646 mod, INTMOD_ISO_FORTRAN_ENV,
6647 symbol[i].id);
6648 break;
6650 #define NAMED_FUNCTION(a,b,c,d) \
6651 case a:
6652 #include "iso-fortran-env.def"
6653 create_intrinsic_function (u->local_name[0] ? u->local_name
6654 : u->use_name,
6655 symbol[i].id, mod,
6656 INTMOD_ISO_FORTRAN_ENV, false,
6657 NULL);
6658 break;
6660 default:
6661 gcc_unreachable ();
6666 if (!found && !only_flag)
6668 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6669 continue;
6671 if ((flag_default_integer || flag_default_real)
6672 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6673 gfc_warning_now (0,
6674 "Use of the NUMERIC_STORAGE_SIZE named constant "
6675 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6676 "incompatible with option %s",
6677 flag_default_integer
6678 ? "-fdefault-integer-8" : "-fdefault-real-8");
6680 switch (symbol[i].id)
6682 #define NAMED_INTCST(a,b,c,d) \
6683 case a:
6684 #include "iso-fortran-env.def"
6685 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6686 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6687 break;
6689 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6690 case a:\
6691 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6692 NULL); \
6693 for (j = 0; KINDS[j].kind != 0; j++) \
6694 gfc_constructor_append_expr (&expr->value.constructor, \
6695 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6696 KINDS[j].kind), NULL); \
6697 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6698 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6699 break;
6700 #include "iso-fortran-env.def"
6702 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6703 case a:
6704 #include "iso-fortran-env.def"
6705 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6706 symbol[i].id);
6707 break;
6709 #define NAMED_FUNCTION(a,b,c,d) \
6710 case a:
6711 #include "iso-fortran-env.def"
6712 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6713 INTMOD_ISO_FORTRAN_ENV, false,
6714 NULL);
6715 break;
6717 default:
6718 gcc_unreachable ();
6723 for (u = gfc_rename_list; u; u = u->next)
6725 if (u->found)
6726 continue;
6728 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6729 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6734 /* Process a USE directive. */
6736 static void
6737 gfc_use_module (gfc_use_list *module)
6739 char *filename;
6740 gfc_state_data *p;
6741 int c, line, start;
6742 gfc_symtree *mod_symtree;
6743 gfc_use_list *use_stmt;
6744 locus old_locus = gfc_current_locus;
6746 gfc_current_locus = module->where;
6747 module_name = module->module_name;
6748 gfc_rename_list = module->rename;
6749 only_flag = module->only_flag;
6750 current_intmod = INTMOD_NONE;
6752 if (!only_flag)
6753 gfc_warning_now (OPT_Wuse_without_only,
6754 "USE statement at %C has no ONLY qualifier");
6756 if (gfc_state_stack->state == COMP_MODULE
6757 || module->submodule_name == NULL
6758 || strcmp (module_name, module->submodule_name) == 0)
6760 filename = XALLOCAVEC (char, strlen (module_name)
6761 + strlen (MODULE_EXTENSION) + 1);
6762 strcpy (filename, module_name);
6763 strcat (filename, MODULE_EXTENSION);
6765 else
6767 filename = XALLOCAVEC (char, strlen (module->submodule_name)
6768 + strlen (SUBMODULE_EXTENSION) + 1);
6769 strcpy (filename, module->submodule_name);
6770 strcat (filename, SUBMODULE_EXTENSION);
6773 /* First, try to find an non-intrinsic module, unless the USE statement
6774 specified that the module is intrinsic. */
6775 module_fp = NULL;
6776 if (!module->intrinsic)
6777 module_fp = gzopen_included_file (filename, true, true);
6779 /* Then, see if it's an intrinsic one, unless the USE statement
6780 specified that the module is non-intrinsic. */
6781 if (module_fp == NULL && !module->non_intrinsic)
6783 if (strcmp (module_name, "iso_fortran_env") == 0
6784 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6785 "intrinsic module at %C"))
6787 use_iso_fortran_env_module ();
6788 free_rename (module->rename);
6789 module->rename = NULL;
6790 gfc_current_locus = old_locus;
6791 module->intrinsic = true;
6792 return;
6795 if (strcmp (module_name, "iso_c_binding") == 0
6796 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6798 import_iso_c_binding_module();
6799 free_rename (module->rename);
6800 module->rename = NULL;
6801 gfc_current_locus = old_locus;
6802 module->intrinsic = true;
6803 return;
6806 module_fp = gzopen_intrinsic_module (filename);
6808 if (module_fp == NULL && module->intrinsic)
6809 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6810 module_name);
6812 /* Check for the IEEE modules, so we can mark their symbols
6813 accordingly when we read them. */
6814 if (strcmp (module_name, "ieee_features") == 0
6815 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
6817 current_intmod = INTMOD_IEEE_FEATURES;
6819 else if (strcmp (module_name, "ieee_exceptions") == 0
6820 && gfc_notify_std (GFC_STD_F2003,
6821 "IEEE_EXCEPTIONS module at %C"))
6823 current_intmod = INTMOD_IEEE_EXCEPTIONS;
6825 else if (strcmp (module_name, "ieee_arithmetic") == 0
6826 && gfc_notify_std (GFC_STD_F2003,
6827 "IEEE_ARITHMETIC module at %C"))
6829 current_intmod = INTMOD_IEEE_ARITHMETIC;
6833 if (module_fp == NULL)
6834 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6835 filename, xstrerror (errno));
6837 /* Check that we haven't already USEd an intrinsic module with the
6838 same name. */
6840 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6841 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6842 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6843 "intrinsic module name used previously", module_name);
6845 iomode = IO_INPUT;
6846 module_line = 1;
6847 module_column = 1;
6848 start = 0;
6850 read_module_to_tmpbuf ();
6851 gzclose (module_fp);
6853 /* Skip the first line of the module, after checking that this is
6854 a gfortran module file. */
6855 line = 0;
6856 while (line < 1)
6858 c = module_char ();
6859 if (c == EOF)
6860 bad_module ("Unexpected end of module");
6861 if (start++ < 3)
6862 parse_name (c);
6863 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6864 || (start == 2 && strcmp (atom_name, " module") != 0))
6865 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6866 " module file", filename);
6867 if (start == 3)
6869 if (strcmp (atom_name, " version") != 0
6870 || module_char () != ' '
6871 || parse_atom () != ATOM_STRING
6872 || strcmp (atom_string, MOD_VERSION))
6873 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6874 " because it was created by a different"
6875 " version of GNU Fortran", filename);
6877 free (atom_string);
6880 if (c == '\n')
6881 line++;
6884 /* Make sure we're not reading the same module that we may be building. */
6885 for (p = gfc_state_stack; p; p = p->previous)
6886 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
6887 && strcmp (p->sym->name, module_name) == 0)
6888 gfc_fatal_error ("Can't USE the same %smodule we're building!",
6889 p->state == COMP_SUBMODULE ? "sub" : "");
6891 init_pi_tree ();
6892 init_true_name_tree ();
6894 read_module ();
6896 free_true_name (true_name_root);
6897 true_name_root = NULL;
6899 free_pi_tree (pi_root);
6900 pi_root = NULL;
6902 XDELETEVEC (module_content);
6903 module_content = NULL;
6905 use_stmt = gfc_get_use_list ();
6906 *use_stmt = *module;
6907 use_stmt->next = gfc_current_ns->use_stmts;
6908 gfc_current_ns->use_stmts = use_stmt;
6910 gfc_current_locus = old_locus;
6914 /* Remove duplicated intrinsic operators from the rename list. */
6916 static void
6917 rename_list_remove_duplicate (gfc_use_rename *list)
6919 gfc_use_rename *seek, *last;
6921 for (; list; list = list->next)
6922 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6924 last = list;
6925 for (seek = list->next; seek; seek = last->next)
6927 if (list->op == seek->op)
6929 last->next = seek->next;
6930 free (seek);
6932 else
6933 last = seek;
6939 /* Process all USE directives. */
6941 void
6942 gfc_use_modules (void)
6944 gfc_use_list *next, *seek, *last;
6946 for (next = module_list; next; next = next->next)
6948 bool non_intrinsic = next->non_intrinsic;
6949 bool intrinsic = next->intrinsic;
6950 bool neither = !non_intrinsic && !intrinsic;
6952 for (seek = next->next; seek; seek = seek->next)
6954 if (next->module_name != seek->module_name)
6955 continue;
6957 if (seek->non_intrinsic)
6958 non_intrinsic = true;
6959 else if (seek->intrinsic)
6960 intrinsic = true;
6961 else
6962 neither = true;
6965 if (intrinsic && neither && !non_intrinsic)
6967 char *filename;
6968 FILE *fp;
6970 filename = XALLOCAVEC (char,
6971 strlen (next->module_name)
6972 + strlen (MODULE_EXTENSION) + 1);
6973 strcpy (filename, next->module_name);
6974 strcat (filename, MODULE_EXTENSION);
6975 fp = gfc_open_included_file (filename, true, true);
6976 if (fp != NULL)
6978 non_intrinsic = true;
6979 fclose (fp);
6983 last = next;
6984 for (seek = next->next; seek; seek = last->next)
6986 if (next->module_name != seek->module_name)
6988 last = seek;
6989 continue;
6992 if ((!next->intrinsic && !seek->intrinsic)
6993 || (next->intrinsic && seek->intrinsic)
6994 || !non_intrinsic)
6996 if (!seek->only_flag)
6997 next->only_flag = false;
6998 if (seek->rename)
7000 gfc_use_rename *r = seek->rename;
7001 while (r->next)
7002 r = r->next;
7003 r->next = next->rename;
7004 next->rename = seek->rename;
7006 last->next = seek->next;
7007 free (seek);
7009 else
7010 last = seek;
7014 for (; module_list; module_list = next)
7016 next = module_list->next;
7017 rename_list_remove_duplicate (module_list->rename);
7018 gfc_use_module (module_list);
7019 free (module_list);
7021 gfc_rename_list = NULL;
7025 void
7026 gfc_free_use_stmts (gfc_use_list *use_stmts)
7028 gfc_use_list *next;
7029 for (; use_stmts; use_stmts = next)
7031 gfc_use_rename *next_rename;
7033 for (; use_stmts->rename; use_stmts->rename = next_rename)
7035 next_rename = use_stmts->rename->next;
7036 free (use_stmts->rename);
7038 next = use_stmts->next;
7039 free (use_stmts);
7044 void
7045 gfc_module_init_2 (void)
7047 last_atom = ATOM_LPAREN;
7048 gfc_rename_list = NULL;
7049 module_list = NULL;
7053 void
7054 gfc_module_done_2 (void)
7056 free_rename (gfc_rename_list);
7057 gfc_rename_list = NULL;