* lib/tsan-dg.exp (tsan_init): Set trivial testcase
[official-gcc.git] / gcc / fortran / module.c
blob52fdebe340cc8b1b3f6542de28f23b0a2468f4b1
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2014 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 "tree.h"
77 #include "stringpool.h"
78 #include "scanner.h"
79 #include <zlib.h>
81 #define MODULE_EXTENSION ".mod"
83 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
84 recognized. */
85 #define MOD_VERSION "12"
88 /* Structure that describes a position within a module file. */
90 typedef struct
92 int column, line;
93 long pos;
95 module_locus;
97 /* Structure for list of symbols of intrinsic modules. */
98 typedef struct
100 int id;
101 const char *name;
102 int value;
103 int standard;
105 intmod_sym;
108 typedef enum
110 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
112 pointer_t;
114 /* The fixup structure lists pointers to pointers that have to
115 be updated when a pointer value becomes known. */
117 typedef struct fixup_t
119 void **pointer;
120 struct fixup_t *next;
122 fixup_t;
125 /* Structure for holding extra info needed for pointers being read. */
127 enum gfc_rsym_state
129 UNUSED,
130 NEEDED,
131 USED
134 enum gfc_wsym_state
136 UNREFERENCED = 0,
137 NEEDS_WRITE,
138 WRITTEN
141 typedef struct pointer_info
143 BBT_HEADER (pointer_info);
144 int integer;
145 pointer_t type;
147 /* The first component of each member of the union is the pointer
148 being stored. */
150 fixup_t *fixup;
152 union
154 void *pointer; /* Member for doing pointer searches. */
156 struct
158 gfc_symbol *sym;
159 char *true_name, *module, *binding_label;
160 fixup_t *stfixup;
161 gfc_symtree *symtree;
162 enum gfc_rsym_state state;
163 int ns, referenced, renamed;
164 module_locus where;
166 rsym;
168 struct
170 gfc_symbol *sym;
171 enum gfc_wsym_state state;
173 wsym;
178 pointer_info;
180 #define gfc_get_pointer_info() XCNEW (pointer_info)
183 /* Local variables */
185 /* The gzFile for the module we're reading or writing. */
186 static gzFile module_fp;
189 /* The name of the module we're reading (USE'ing) or writing. */
190 static const char *module_name;
191 static gfc_use_list *module_list;
193 /* Content of module. */
194 static char* module_content;
196 static long module_pos;
197 static int module_line, module_column, only_flag;
198 static int prev_module_line, prev_module_column;
200 static enum
201 { IO_INPUT, IO_OUTPUT }
202 iomode;
204 static gfc_use_rename *gfc_rename_list;
205 static pointer_info *pi_root;
206 static int symbol_number; /* Counter for assigning symbol numbers */
208 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
209 static bool in_load_equiv;
213 /*****************************************************************/
215 /* Pointer/integer conversion. Pointers between structures are stored
216 as integers in the module file. The next couple of subroutines
217 handle this translation for reading and writing. */
219 /* Recursively free the tree of pointer structures. */
221 static void
222 free_pi_tree (pointer_info *p)
224 if (p == NULL)
225 return;
227 if (p->fixup != NULL)
228 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
230 free_pi_tree (p->left);
231 free_pi_tree (p->right);
233 if (iomode == IO_INPUT)
235 XDELETEVEC (p->u.rsym.true_name);
236 XDELETEVEC (p->u.rsym.module);
237 XDELETEVEC (p->u.rsym.binding_label);
240 free (p);
244 /* Compare pointers when searching by pointer. Used when writing a
245 module. */
247 static int
248 compare_pointers (void *_sn1, void *_sn2)
250 pointer_info *sn1, *sn2;
252 sn1 = (pointer_info *) _sn1;
253 sn2 = (pointer_info *) _sn2;
255 if (sn1->u.pointer < sn2->u.pointer)
256 return -1;
257 if (sn1->u.pointer > sn2->u.pointer)
258 return 1;
260 return 0;
264 /* Compare integers when searching by integer. Used when reading a
265 module. */
267 static int
268 compare_integers (void *_sn1, void *_sn2)
270 pointer_info *sn1, *sn2;
272 sn1 = (pointer_info *) _sn1;
273 sn2 = (pointer_info *) _sn2;
275 if (sn1->integer < sn2->integer)
276 return -1;
277 if (sn1->integer > sn2->integer)
278 return 1;
280 return 0;
284 /* Initialize the pointer_info tree. */
286 static void
287 init_pi_tree (void)
289 compare_fn compare;
290 pointer_info *p;
292 pi_root = NULL;
293 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
295 /* Pointer 0 is the NULL pointer. */
296 p = gfc_get_pointer_info ();
297 p->u.pointer = NULL;
298 p->integer = 0;
299 p->type = P_OTHER;
301 gfc_insert_bbt (&pi_root, p, compare);
303 /* Pointer 1 is the current namespace. */
304 p = gfc_get_pointer_info ();
305 p->u.pointer = gfc_current_ns;
306 p->integer = 1;
307 p->type = P_NAMESPACE;
309 gfc_insert_bbt (&pi_root, p, compare);
311 symbol_number = 2;
315 /* During module writing, call here with a pointer to something,
316 returning the pointer_info node. */
318 static pointer_info *
319 find_pointer (void *gp)
321 pointer_info *p;
323 p = pi_root;
324 while (p != NULL)
326 if (p->u.pointer == gp)
327 break;
328 p = (gp < p->u.pointer) ? p->left : p->right;
331 return p;
335 /* Given a pointer while writing, returns the pointer_info tree node,
336 creating it if it doesn't exist. */
338 static pointer_info *
339 get_pointer (void *gp)
341 pointer_info *p;
343 p = find_pointer (gp);
344 if (p != NULL)
345 return p;
347 /* Pointer doesn't have an integer. Give it one. */
348 p = gfc_get_pointer_info ();
350 p->u.pointer = gp;
351 p->integer = symbol_number++;
353 gfc_insert_bbt (&pi_root, p, compare_pointers);
355 return p;
359 /* Given an integer during reading, find it in the pointer_info tree,
360 creating the node if not found. */
362 static pointer_info *
363 get_integer (int integer)
365 pointer_info *p, t;
366 int c;
368 t.integer = integer;
370 p = pi_root;
371 while (p != NULL)
373 c = compare_integers (&t, p);
374 if (c == 0)
375 break;
377 p = (c < 0) ? p->left : p->right;
380 if (p != NULL)
381 return p;
383 p = gfc_get_pointer_info ();
384 p->integer = integer;
385 p->u.pointer = NULL;
387 gfc_insert_bbt (&pi_root, p, compare_integers);
389 return p;
393 /* Resolve any fixups using a known pointer. */
395 static void
396 resolve_fixups (fixup_t *f, void *gp)
398 fixup_t *next;
400 for (; f; f = next)
402 next = f->next;
403 *(f->pointer) = gp;
404 free (f);
409 /* Convert a string such that it starts with a lower-case character. Used
410 to convert the symtree name of a derived-type to the symbol name or to
411 the name of the associated generic function. */
413 static const char *
414 dt_lower_string (const char *name)
416 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
417 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
418 &name[1]);
419 return gfc_get_string (name);
423 /* Convert a string such that it starts with an upper-case character. Used to
424 return the symtree-name for a derived type; the symbol name itself and the
425 symtree/symbol name of the associated generic function start with a lower-
426 case character. */
428 static const char *
429 dt_upper_string (const char *name)
431 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
432 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
433 &name[1]);
434 return gfc_get_string (name);
437 /* Call here during module reading when we know what pointer to
438 associate with an integer. Any fixups that exist are resolved at
439 this time. */
441 static void
442 associate_integer_pointer (pointer_info *p, void *gp)
444 if (p->u.pointer != NULL)
445 gfc_internal_error ("associate_integer_pointer(): Already associated");
447 p->u.pointer = gp;
449 resolve_fixups (p->fixup, gp);
451 p->fixup = NULL;
455 /* During module reading, given an integer and a pointer to a pointer,
456 either store the pointer from an already-known value or create a
457 fixup structure in order to store things later. Returns zero if
458 the reference has been actually stored, or nonzero if the reference
459 must be fixed later (i.e., associate_integer_pointer must be called
460 sometime later. Returns the pointer_info structure. */
462 static pointer_info *
463 add_fixup (int integer, void *gp)
465 pointer_info *p;
466 fixup_t *f;
467 char **cp;
469 p = get_integer (integer);
471 if (p->integer == 0 || p->u.pointer != NULL)
473 cp = (char **) gp;
474 *cp = (char *) p->u.pointer;
476 else
478 f = XCNEW (fixup_t);
480 f->next = p->fixup;
481 p->fixup = f;
483 f->pointer = (void **) gp;
486 return p;
490 /*****************************************************************/
492 /* Parser related subroutines */
494 /* Free the rename list left behind by a USE statement. */
496 static void
497 free_rename (gfc_use_rename *list)
499 gfc_use_rename *next;
501 for (; list; list = next)
503 next = list->next;
504 free (list);
509 /* Match a USE statement. */
511 match
512 gfc_match_use (void)
514 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
515 gfc_use_rename *tail = NULL, *new_use;
516 interface_type type, type2;
517 gfc_intrinsic_op op;
518 match m;
519 gfc_use_list *use_list;
521 use_list = gfc_get_use_list ();
523 if (gfc_match (" , ") == MATCH_YES)
525 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
527 if (!gfc_notify_std (GFC_STD_F2003, "module "
528 "nature in USE statement at %C"))
529 goto cleanup;
531 if (strcmp (module_nature, "intrinsic") == 0)
532 use_list->intrinsic = true;
533 else
535 if (strcmp (module_nature, "non_intrinsic") == 0)
536 use_list->non_intrinsic = true;
537 else
539 gfc_error ("Module nature in USE statement at %C shall "
540 "be either INTRINSIC or NON_INTRINSIC");
541 goto cleanup;
545 else
547 /* Help output a better error message than "Unclassifiable
548 statement". */
549 gfc_match (" %n", module_nature);
550 if (strcmp (module_nature, "intrinsic") == 0
551 || strcmp (module_nature, "non_intrinsic") == 0)
552 gfc_error ("\"::\" was expected after module nature at %C "
553 "but was not found");
554 free (use_list);
555 return m;
558 else
560 m = gfc_match (" ::");
561 if (m == MATCH_YES &&
562 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
563 goto cleanup;
565 if (m != MATCH_YES)
567 m = gfc_match ("% ");
568 if (m != MATCH_YES)
570 free (use_list);
571 return m;
576 use_list->where = gfc_current_locus;
578 m = gfc_match_name (name);
579 if (m != MATCH_YES)
581 free (use_list);
582 return m;
585 use_list->module_name = gfc_get_string (name);
587 if (gfc_match_eos () == MATCH_YES)
588 goto done;
590 if (gfc_match_char (',') != MATCH_YES)
591 goto syntax;
593 if (gfc_match (" only :") == MATCH_YES)
594 use_list->only_flag = true;
596 if (gfc_match_eos () == MATCH_YES)
597 goto done;
599 for (;;)
601 /* Get a new rename struct and add it to the rename list. */
602 new_use = gfc_get_use_rename ();
603 new_use->where = gfc_current_locus;
604 new_use->found = 0;
606 if (use_list->rename == NULL)
607 use_list->rename = new_use;
608 else
609 tail->next = new_use;
610 tail = new_use;
612 /* See what kind of interface we're dealing with. Assume it is
613 not an operator. */
614 new_use->op = INTRINSIC_NONE;
615 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
616 goto cleanup;
618 switch (type)
620 case INTERFACE_NAMELESS:
621 gfc_error ("Missing generic specification in USE statement at %C");
622 goto cleanup;
624 case INTERFACE_USER_OP:
625 case INTERFACE_GENERIC:
626 m = gfc_match (" =>");
628 if (type == INTERFACE_USER_OP && m == MATCH_YES
629 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
630 "operators in USE statements at %C")))
631 goto cleanup;
633 if (type == INTERFACE_USER_OP)
634 new_use->op = INTRINSIC_USER;
636 if (use_list->only_flag)
638 if (m != MATCH_YES)
639 strcpy (new_use->use_name, name);
640 else
642 strcpy (new_use->local_name, name);
643 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
644 if (type != type2)
645 goto syntax;
646 if (m == MATCH_NO)
647 goto syntax;
648 if (m == MATCH_ERROR)
649 goto cleanup;
652 else
654 if (m != MATCH_YES)
655 goto syntax;
656 strcpy (new_use->local_name, name);
658 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
659 if (type != type2)
660 goto syntax;
661 if (m == MATCH_NO)
662 goto syntax;
663 if (m == MATCH_ERROR)
664 goto cleanup;
667 if (strcmp (new_use->use_name, use_list->module_name) == 0
668 || strcmp (new_use->local_name, use_list->module_name) == 0)
670 gfc_error ("The name '%s' at %C has already been used as "
671 "an external module name.", use_list->module_name);
672 goto cleanup;
674 break;
676 case INTERFACE_INTRINSIC_OP:
677 new_use->op = op;
678 break;
680 default:
681 gcc_unreachable ();
684 if (gfc_match_eos () == MATCH_YES)
685 break;
686 if (gfc_match_char (',') != MATCH_YES)
687 goto syntax;
690 done:
691 if (module_list)
693 gfc_use_list *last = module_list;
694 while (last->next)
695 last = last->next;
696 last->next = use_list;
698 else
699 module_list = use_list;
701 return MATCH_YES;
703 syntax:
704 gfc_syntax_error (ST_USE);
706 cleanup:
707 free_rename (use_list->rename);
708 free (use_list);
709 return MATCH_ERROR;
713 /* Given a name and a number, inst, return the inst name
714 under which to load this symbol. Returns NULL if this
715 symbol shouldn't be loaded. If inst is zero, returns
716 the number of instances of this name. If interface is
717 true, a user-defined operator is sought, otherwise only
718 non-operators are sought. */
720 static const char *
721 find_use_name_n (const char *name, int *inst, bool interface)
723 gfc_use_rename *u;
724 const char *low_name = NULL;
725 int i;
727 /* For derived types. */
728 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
729 low_name = dt_lower_string (name);
731 i = 0;
732 for (u = gfc_rename_list; u; u = u->next)
734 if ((!low_name && strcmp (u->use_name, name) != 0)
735 || (low_name && strcmp (u->use_name, low_name) != 0)
736 || (u->op == INTRINSIC_USER && !interface)
737 || (u->op != INTRINSIC_USER && interface))
738 continue;
739 if (++i == *inst)
740 break;
743 if (!*inst)
745 *inst = i;
746 return NULL;
749 if (u == NULL)
750 return only_flag ? NULL : name;
752 u->found = 1;
754 if (low_name)
756 if (u->local_name[0] == '\0')
757 return name;
758 return dt_upper_string (u->local_name);
761 return (u->local_name[0] != '\0') ? u->local_name : name;
765 /* Given a name, return the name under which to load this symbol.
766 Returns NULL if this symbol shouldn't be loaded. */
768 static const char *
769 find_use_name (const char *name, bool interface)
771 int i = 1;
772 return find_use_name_n (name, &i, interface);
776 /* Given a real name, return the number of use names associated with it. */
778 static int
779 number_use_names (const char *name, bool interface)
781 int i = 0;
782 find_use_name_n (name, &i, interface);
783 return i;
787 /* Try to find the operator in the current list. */
789 static gfc_use_rename *
790 find_use_operator (gfc_intrinsic_op op)
792 gfc_use_rename *u;
794 for (u = gfc_rename_list; u; u = u->next)
795 if (u->op == op)
796 return u;
798 return NULL;
802 /*****************************************************************/
804 /* The next couple of subroutines maintain a tree used to avoid a
805 brute-force search for a combination of true name and module name.
806 While symtree names, the name that a particular symbol is known by
807 can changed with USE statements, we still have to keep track of the
808 true names to generate the correct reference, and also avoid
809 loading the same real symbol twice in a program unit.
811 When we start reading, the true name tree is built and maintained
812 as symbols are read. The tree is searched as we load new symbols
813 to see if it already exists someplace in the namespace. */
815 typedef struct true_name
817 BBT_HEADER (true_name);
818 const char *name;
819 gfc_symbol *sym;
821 true_name;
823 static true_name *true_name_root;
826 /* Compare two true_name structures. */
828 static int
829 compare_true_names (void *_t1, void *_t2)
831 true_name *t1, *t2;
832 int c;
834 t1 = (true_name *) _t1;
835 t2 = (true_name *) _t2;
837 c = ((t1->sym->module > t2->sym->module)
838 - (t1->sym->module < t2->sym->module));
839 if (c != 0)
840 return c;
842 return strcmp (t1->name, t2->name);
846 /* Given a true name, search the true name tree to see if it exists
847 within the main namespace. */
849 static gfc_symbol *
850 find_true_name (const char *name, const char *module)
852 true_name t, *p;
853 gfc_symbol sym;
854 int c;
856 t.name = gfc_get_string (name);
857 if (module != NULL)
858 sym.module = gfc_get_string (module);
859 else
860 sym.module = NULL;
861 t.sym = &sym;
863 p = true_name_root;
864 while (p != NULL)
866 c = compare_true_names ((void *) (&t), (void *) p);
867 if (c == 0)
868 return p->sym;
870 p = (c < 0) ? p->left : p->right;
873 return NULL;
877 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
879 static void
880 add_true_name (gfc_symbol *sym)
882 true_name *t;
884 t = XCNEW (true_name);
885 t->sym = sym;
886 if (sym->attr.flavor == FL_DERIVED)
887 t->name = dt_upper_string (sym->name);
888 else
889 t->name = sym->name;
891 gfc_insert_bbt (&true_name_root, t, compare_true_names);
895 /* Recursive function to build the initial true name tree by
896 recursively traversing the current namespace. */
898 static void
899 build_tnt (gfc_symtree *st)
901 const char *name;
902 if (st == NULL)
903 return;
905 build_tnt (st->left);
906 build_tnt (st->right);
908 if (st->n.sym->attr.flavor == FL_DERIVED)
909 name = dt_upper_string (st->n.sym->name);
910 else
911 name = st->n.sym->name;
913 if (find_true_name (name, st->n.sym->module) != NULL)
914 return;
916 add_true_name (st->n.sym);
920 /* Initialize the true name tree with the current namespace. */
922 static void
923 init_true_name_tree (void)
925 true_name_root = NULL;
926 build_tnt (gfc_current_ns->sym_root);
930 /* Recursively free a true name tree node. */
932 static void
933 free_true_name (true_name *t)
935 if (t == NULL)
936 return;
937 free_true_name (t->left);
938 free_true_name (t->right);
940 free (t);
944 /*****************************************************************/
946 /* Module reading and writing. */
948 /* The following are versions similar to the ones in scanner.c, but
949 for dealing with compressed module files. */
951 static gzFile
952 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
953 bool module, bool system)
955 char *fullname;
956 gfc_directorylist *p;
957 gzFile f;
959 for (p = list; p; p = p->next)
961 if (module && !p->use_for_modules)
962 continue;
964 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
965 strcpy (fullname, p->path);
966 strcat (fullname, name);
968 f = gzopen (fullname, "r");
969 if (f != NULL)
971 if (gfc_cpp_makedep ())
972 gfc_cpp_add_dep (fullname, system);
974 return f;
978 return NULL;
981 static gzFile
982 gzopen_included_file (const char *name, bool include_cwd, bool module)
984 gzFile f = NULL;
986 if (IS_ABSOLUTE_PATH (name) || include_cwd)
988 f = gzopen (name, "r");
989 if (f && gfc_cpp_makedep ())
990 gfc_cpp_add_dep (name, false);
993 if (!f)
994 f = gzopen_included_file_1 (name, include_dirs, module, false);
996 return f;
999 static gzFile
1000 gzopen_intrinsic_module (const char* name)
1002 gzFile f = NULL;
1004 if (IS_ABSOLUTE_PATH (name))
1006 f = gzopen (name, "r");
1007 if (f && gfc_cpp_makedep ())
1008 gfc_cpp_add_dep (name, true);
1011 if (!f)
1012 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1014 return f;
1018 typedef enum
1020 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1022 atom_type;
1024 static atom_type last_atom;
1027 /* The name buffer must be at least as long as a symbol name. Right
1028 now it's not clear how we're going to store numeric constants--
1029 probably as a hexadecimal string, since this will allow the exact
1030 number to be preserved (this can't be done by a decimal
1031 representation). Worry about that later. TODO! */
1033 #define MAX_ATOM_SIZE 100
1035 static int atom_int;
1036 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1039 /* Report problems with a module. Error reporting is not very
1040 elaborate, since this sorts of errors shouldn't really happen.
1041 This subroutine never returns. */
1043 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1045 static void
1046 bad_module (const char *msgid)
1048 XDELETEVEC (module_content);
1049 module_content = NULL;
1051 switch (iomode)
1053 case IO_INPUT:
1054 gfc_fatal_error ("Reading module %s at line %d column %d: %s",
1055 module_name, module_line, module_column, msgid);
1056 break;
1057 case IO_OUTPUT:
1058 gfc_fatal_error ("Writing module %s at line %d column %d: %s",
1059 module_name, module_line, module_column, msgid);
1060 break;
1061 default:
1062 gfc_fatal_error ("Module %s at line %d column %d: %s",
1063 module_name, module_line, module_column, msgid);
1064 break;
1069 /* Set the module's input pointer. */
1071 static void
1072 set_module_locus (module_locus *m)
1074 module_column = m->column;
1075 module_line = m->line;
1076 module_pos = m->pos;
1080 /* Get the module's input pointer so that we can restore it later. */
1082 static void
1083 get_module_locus (module_locus *m)
1085 m->column = module_column;
1086 m->line = module_line;
1087 m->pos = module_pos;
1091 /* Get the next character in the module, updating our reckoning of
1092 where we are. */
1094 static int
1095 module_char (void)
1097 const char c = module_content[module_pos++];
1098 if (c == '\0')
1099 bad_module ("Unexpected EOF");
1101 prev_module_line = module_line;
1102 prev_module_column = module_column;
1104 if (c == '\n')
1106 module_line++;
1107 module_column = 0;
1110 module_column++;
1111 return c;
1114 /* Unget a character while remembering the line and column. Works for
1115 a single character only. */
1117 static void
1118 module_unget_char (void)
1120 module_line = prev_module_line;
1121 module_column = prev_module_column;
1122 module_pos--;
1125 /* Parse a string constant. The delimiter is guaranteed to be a
1126 single quote. */
1128 static void
1129 parse_string (void)
1131 int c;
1132 size_t cursz = 30;
1133 size_t len = 0;
1135 atom_string = XNEWVEC (char, cursz);
1137 for ( ; ; )
1139 c = module_char ();
1141 if (c == '\'')
1143 int c2 = module_char ();
1144 if (c2 != '\'')
1146 module_unget_char ();
1147 break;
1151 if (len >= cursz)
1153 cursz *= 2;
1154 atom_string = XRESIZEVEC (char, atom_string, cursz);
1156 atom_string[len] = c;
1157 len++;
1160 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1161 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1165 /* Parse a small integer. */
1167 static void
1168 parse_integer (int c)
1170 atom_int = c - '0';
1172 for (;;)
1174 c = module_char ();
1175 if (!ISDIGIT (c))
1177 module_unget_char ();
1178 break;
1181 atom_int = 10 * atom_int + c - '0';
1182 if (atom_int > 99999999)
1183 bad_module ("Integer overflow");
1189 /* Parse a name. */
1191 static void
1192 parse_name (int c)
1194 char *p;
1195 int len;
1197 p = atom_name;
1199 *p++ = c;
1200 len = 1;
1202 for (;;)
1204 c = module_char ();
1205 if (!ISALNUM (c) && c != '_' && c != '-')
1207 module_unget_char ();
1208 break;
1211 *p++ = c;
1212 if (++len > GFC_MAX_SYMBOL_LEN)
1213 bad_module ("Name too long");
1216 *p = '\0';
1221 /* Read the next atom in the module's input stream. */
1223 static atom_type
1224 parse_atom (void)
1226 int c;
1230 c = module_char ();
1232 while (c == ' ' || c == '\r' || c == '\n');
1234 switch (c)
1236 case '(':
1237 return ATOM_LPAREN;
1239 case ')':
1240 return ATOM_RPAREN;
1242 case '\'':
1243 parse_string ();
1244 return ATOM_STRING;
1246 case '0':
1247 case '1':
1248 case '2':
1249 case '3':
1250 case '4':
1251 case '5':
1252 case '6':
1253 case '7':
1254 case '8':
1255 case '9':
1256 parse_integer (c);
1257 return ATOM_INTEGER;
1259 case 'a':
1260 case 'b':
1261 case 'c':
1262 case 'd':
1263 case 'e':
1264 case 'f':
1265 case 'g':
1266 case 'h':
1267 case 'i':
1268 case 'j':
1269 case 'k':
1270 case 'l':
1271 case 'm':
1272 case 'n':
1273 case 'o':
1274 case 'p':
1275 case 'q':
1276 case 'r':
1277 case 's':
1278 case 't':
1279 case 'u':
1280 case 'v':
1281 case 'w':
1282 case 'x':
1283 case 'y':
1284 case 'z':
1285 case 'A':
1286 case 'B':
1287 case 'C':
1288 case 'D':
1289 case 'E':
1290 case 'F':
1291 case 'G':
1292 case 'H':
1293 case 'I':
1294 case 'J':
1295 case 'K':
1296 case 'L':
1297 case 'M':
1298 case 'N':
1299 case 'O':
1300 case 'P':
1301 case 'Q':
1302 case 'R':
1303 case 'S':
1304 case 'T':
1305 case 'U':
1306 case 'V':
1307 case 'W':
1308 case 'X':
1309 case 'Y':
1310 case 'Z':
1311 parse_name (c);
1312 return ATOM_NAME;
1314 default:
1315 bad_module ("Bad name");
1318 /* Not reached. */
1322 /* Peek at the next atom on the input. */
1324 static atom_type
1325 peek_atom (void)
1327 int c;
1331 c = module_char ();
1333 while (c == ' ' || c == '\r' || c == '\n');
1335 switch (c)
1337 case '(':
1338 module_unget_char ();
1339 return ATOM_LPAREN;
1341 case ')':
1342 module_unget_char ();
1343 return ATOM_RPAREN;
1345 case '\'':
1346 module_unget_char ();
1347 return ATOM_STRING;
1349 case '0':
1350 case '1':
1351 case '2':
1352 case '3':
1353 case '4':
1354 case '5':
1355 case '6':
1356 case '7':
1357 case '8':
1358 case '9':
1359 module_unget_char ();
1360 return ATOM_INTEGER;
1362 case 'a':
1363 case 'b':
1364 case 'c':
1365 case 'd':
1366 case 'e':
1367 case 'f':
1368 case 'g':
1369 case 'h':
1370 case 'i':
1371 case 'j':
1372 case 'k':
1373 case 'l':
1374 case 'm':
1375 case 'n':
1376 case 'o':
1377 case 'p':
1378 case 'q':
1379 case 'r':
1380 case 's':
1381 case 't':
1382 case 'u':
1383 case 'v':
1384 case 'w':
1385 case 'x':
1386 case 'y':
1387 case 'z':
1388 case 'A':
1389 case 'B':
1390 case 'C':
1391 case 'D':
1392 case 'E':
1393 case 'F':
1394 case 'G':
1395 case 'H':
1396 case 'I':
1397 case 'J':
1398 case 'K':
1399 case 'L':
1400 case 'M':
1401 case 'N':
1402 case 'O':
1403 case 'P':
1404 case 'Q':
1405 case 'R':
1406 case 'S':
1407 case 'T':
1408 case 'U':
1409 case 'V':
1410 case 'W':
1411 case 'X':
1412 case 'Y':
1413 case 'Z':
1414 module_unget_char ();
1415 return ATOM_NAME;
1417 default:
1418 bad_module ("Bad name");
1423 /* Read the next atom from the input, requiring that it be a
1424 particular kind. */
1426 static void
1427 require_atom (atom_type type)
1429 atom_type t;
1430 const char *p;
1431 int column, line;
1433 column = module_column;
1434 line = module_line;
1436 t = parse_atom ();
1437 if (t != type)
1439 switch (type)
1441 case ATOM_NAME:
1442 p = _("Expected name");
1443 break;
1444 case ATOM_LPAREN:
1445 p = _("Expected left parenthesis");
1446 break;
1447 case ATOM_RPAREN:
1448 p = _("Expected right parenthesis");
1449 break;
1450 case ATOM_INTEGER:
1451 p = _("Expected integer");
1452 break;
1453 case ATOM_STRING:
1454 p = _("Expected string");
1455 break;
1456 default:
1457 gfc_internal_error ("require_atom(): bad atom type required");
1460 module_column = column;
1461 module_line = line;
1462 bad_module (p);
1467 /* Given a pointer to an mstring array, require that the current input
1468 be one of the strings in the array. We return the enum value. */
1470 static int
1471 find_enum (const mstring *m)
1473 int i;
1475 i = gfc_string2code (m, atom_name);
1476 if (i >= 0)
1477 return i;
1479 bad_module ("find_enum(): Enum not found");
1481 /* Not reached. */
1485 /* Read a string. The caller is responsible for freeing. */
1487 static char*
1488 read_string (void)
1490 char* p;
1491 require_atom (ATOM_STRING);
1492 p = atom_string;
1493 atom_string = NULL;
1494 return p;
1498 /**************** Module output subroutines ***************************/
1500 /* Output a character to a module file. */
1502 static void
1503 write_char (char out)
1505 if (gzputc (module_fp, out) == EOF)
1506 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1508 if (out != '\n')
1509 module_column++;
1510 else
1512 module_column = 1;
1513 module_line++;
1518 /* Write an atom to a module. The line wrapping isn't perfect, but it
1519 should work most of the time. This isn't that big of a deal, since
1520 the file really isn't meant to be read by people anyway. */
1522 static void
1523 write_atom (atom_type atom, const void *v)
1525 char buffer[20];
1526 int i, len;
1527 const char *p;
1529 switch (atom)
1531 case ATOM_STRING:
1532 case ATOM_NAME:
1533 p = (const char *) v;
1534 break;
1536 case ATOM_LPAREN:
1537 p = "(";
1538 break;
1540 case ATOM_RPAREN:
1541 p = ")";
1542 break;
1544 case ATOM_INTEGER:
1545 i = *((const int *) v);
1546 if (i < 0)
1547 gfc_internal_error ("write_atom(): Writing negative integer");
1549 sprintf (buffer, "%d", i);
1550 p = buffer;
1551 break;
1553 default:
1554 gfc_internal_error ("write_atom(): Trying to write dab atom");
1558 if(p == NULL || *p == '\0')
1559 len = 0;
1560 else
1561 len = strlen (p);
1563 if (atom != ATOM_RPAREN)
1565 if (module_column + len > 72)
1566 write_char ('\n');
1567 else
1570 if (last_atom != ATOM_LPAREN && module_column != 1)
1571 write_char (' ');
1575 if (atom == ATOM_STRING)
1576 write_char ('\'');
1578 while (p != NULL && *p)
1580 if (atom == ATOM_STRING && *p == '\'')
1581 write_char ('\'');
1582 write_char (*p++);
1585 if (atom == ATOM_STRING)
1586 write_char ('\'');
1588 last_atom = atom;
1593 /***************** Mid-level I/O subroutines *****************/
1595 /* These subroutines let their caller read or write atoms without
1596 caring about which of the two is actually happening. This lets a
1597 subroutine concentrate on the actual format of the data being
1598 written. */
1600 static void mio_expr (gfc_expr **);
1601 pointer_info *mio_symbol_ref (gfc_symbol **);
1602 pointer_info *mio_interface_rest (gfc_interface **);
1603 static void mio_symtree_ref (gfc_symtree **);
1605 /* Read or write an enumerated value. On writing, we return the input
1606 value for the convenience of callers. We avoid using an integer
1607 pointer because enums are sometimes inside bitfields. */
1609 static int
1610 mio_name (int t, const mstring *m)
1612 if (iomode == IO_OUTPUT)
1613 write_atom (ATOM_NAME, gfc_code2string (m, t));
1614 else
1616 require_atom (ATOM_NAME);
1617 t = find_enum (m);
1620 return t;
1623 /* Specialization of mio_name. */
1625 #define DECL_MIO_NAME(TYPE) \
1626 static inline TYPE \
1627 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1629 return (TYPE) mio_name ((int) t, m); \
1631 #define MIO_NAME(TYPE) mio_name_##TYPE
1633 static void
1634 mio_lparen (void)
1636 if (iomode == IO_OUTPUT)
1637 write_atom (ATOM_LPAREN, NULL);
1638 else
1639 require_atom (ATOM_LPAREN);
1643 static void
1644 mio_rparen (void)
1646 if (iomode == IO_OUTPUT)
1647 write_atom (ATOM_RPAREN, NULL);
1648 else
1649 require_atom (ATOM_RPAREN);
1653 static void
1654 mio_integer (int *ip)
1656 if (iomode == IO_OUTPUT)
1657 write_atom (ATOM_INTEGER, ip);
1658 else
1660 require_atom (ATOM_INTEGER);
1661 *ip = atom_int;
1666 /* Read or write a gfc_intrinsic_op value. */
1668 static void
1669 mio_intrinsic_op (gfc_intrinsic_op* op)
1671 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1672 if (iomode == IO_OUTPUT)
1674 int converted = (int) *op;
1675 write_atom (ATOM_INTEGER, &converted);
1677 else
1679 require_atom (ATOM_INTEGER);
1680 *op = (gfc_intrinsic_op) atom_int;
1685 /* Read or write a character pointer that points to a string on the heap. */
1687 static const char *
1688 mio_allocated_string (const char *s)
1690 if (iomode == IO_OUTPUT)
1692 write_atom (ATOM_STRING, s);
1693 return s;
1695 else
1697 require_atom (ATOM_STRING);
1698 return atom_string;
1703 /* Functions for quoting and unquoting strings. */
1705 static char *
1706 quote_string (const gfc_char_t *s, const size_t slength)
1708 const gfc_char_t *p;
1709 char *res, *q;
1710 size_t len = 0, i;
1712 /* Calculate the length we'll need: a backslash takes two ("\\"),
1713 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1714 for (p = s, i = 0; i < slength; p++, i++)
1716 if (*p == '\\')
1717 len += 2;
1718 else if (!gfc_wide_is_printable (*p))
1719 len += 10;
1720 else
1721 len++;
1724 q = res = XCNEWVEC (char, len + 1);
1725 for (p = s, i = 0; i < slength; p++, i++)
1727 if (*p == '\\')
1728 *q++ = '\\', *q++ = '\\';
1729 else if (!gfc_wide_is_printable (*p))
1731 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1732 (unsigned HOST_WIDE_INT) *p);
1733 q += 10;
1735 else
1736 *q++ = (unsigned char) *p;
1739 res[len] = '\0';
1740 return res;
1743 static gfc_char_t *
1744 unquote_string (const char *s)
1746 size_t len, i;
1747 const char *p;
1748 gfc_char_t *res;
1750 for (p = s, len = 0; *p; p++, len++)
1752 if (*p != '\\')
1753 continue;
1755 if (p[1] == '\\')
1756 p++;
1757 else if (p[1] == 'U')
1758 p += 9; /* That is a "\U????????". */
1759 else
1760 gfc_internal_error ("unquote_string(): got bad string");
1763 res = gfc_get_wide_string (len + 1);
1764 for (i = 0, p = s; i < len; i++, p++)
1766 gcc_assert (*p);
1768 if (*p != '\\')
1769 res[i] = (unsigned char) *p;
1770 else if (p[1] == '\\')
1772 res[i] = (unsigned char) '\\';
1773 p++;
1775 else
1777 /* We read the 8-digits hexadecimal constant that follows. */
1778 int j;
1779 unsigned n;
1780 gfc_char_t c = 0;
1782 gcc_assert (p[1] == 'U');
1783 for (j = 0; j < 8; j++)
1785 c = c << 4;
1786 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1787 c += n;
1790 res[i] = c;
1791 p += 9;
1795 res[len] = '\0';
1796 return res;
1800 /* Read or write a character pointer that points to a wide string on the
1801 heap, performing quoting/unquoting of nonprintable characters using the
1802 form \U???????? (where each ? is a hexadecimal digit).
1803 Length is the length of the string, only known and used in output mode. */
1805 static const gfc_char_t *
1806 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1808 if (iomode == IO_OUTPUT)
1810 char *quoted = quote_string (s, length);
1811 write_atom (ATOM_STRING, quoted);
1812 free (quoted);
1813 return s;
1815 else
1817 gfc_char_t *unquoted;
1819 require_atom (ATOM_STRING);
1820 unquoted = unquote_string (atom_string);
1821 free (atom_string);
1822 return unquoted;
1827 /* Read or write a string that is in static memory. */
1829 static void
1830 mio_pool_string (const char **stringp)
1832 /* TODO: one could write the string only once, and refer to it via a
1833 fixup pointer. */
1835 /* As a special case we have to deal with a NULL string. This
1836 happens for the 'module' member of 'gfc_symbol's that are not in a
1837 module. We read / write these as the empty string. */
1838 if (iomode == IO_OUTPUT)
1840 const char *p = *stringp == NULL ? "" : *stringp;
1841 write_atom (ATOM_STRING, p);
1843 else
1845 require_atom (ATOM_STRING);
1846 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1847 free (atom_string);
1852 /* Read or write a string that is inside of some already-allocated
1853 structure. */
1855 static void
1856 mio_internal_string (char *string)
1858 if (iomode == IO_OUTPUT)
1859 write_atom (ATOM_STRING, string);
1860 else
1862 require_atom (ATOM_STRING);
1863 strcpy (string, atom_string);
1864 free (atom_string);
1869 typedef enum
1870 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1871 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1872 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1873 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1874 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1875 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1876 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1877 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1878 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1879 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1880 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY
1882 ab_attribute;
1884 static const mstring attr_bits[] =
1886 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1887 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1888 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1889 minit ("DIMENSION", AB_DIMENSION),
1890 minit ("CODIMENSION", AB_CODIMENSION),
1891 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1892 minit ("EXTERNAL", AB_EXTERNAL),
1893 minit ("INTRINSIC", AB_INTRINSIC),
1894 minit ("OPTIONAL", AB_OPTIONAL),
1895 minit ("POINTER", AB_POINTER),
1896 minit ("VOLATILE", AB_VOLATILE),
1897 minit ("TARGET", AB_TARGET),
1898 minit ("THREADPRIVATE", AB_THREADPRIVATE),
1899 minit ("DUMMY", AB_DUMMY),
1900 minit ("RESULT", AB_RESULT),
1901 minit ("DATA", AB_DATA),
1902 minit ("IN_NAMELIST", AB_IN_NAMELIST),
1903 minit ("IN_COMMON", AB_IN_COMMON),
1904 minit ("FUNCTION", AB_FUNCTION),
1905 minit ("SUBROUTINE", AB_SUBROUTINE),
1906 minit ("SEQUENCE", AB_SEQUENCE),
1907 minit ("ELEMENTAL", AB_ELEMENTAL),
1908 minit ("PURE", AB_PURE),
1909 minit ("RECURSIVE", AB_RECURSIVE),
1910 minit ("GENERIC", AB_GENERIC),
1911 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
1912 minit ("CRAY_POINTER", AB_CRAY_POINTER),
1913 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
1914 minit ("IS_BIND_C", AB_IS_BIND_C),
1915 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
1916 minit ("IS_ISO_C", AB_IS_ISO_C),
1917 minit ("VALUE", AB_VALUE),
1918 minit ("ALLOC_COMP", AB_ALLOC_COMP),
1919 minit ("COARRAY_COMP", AB_COARRAY_COMP),
1920 minit ("LOCK_COMP", AB_LOCK_COMP),
1921 minit ("POINTER_COMP", AB_POINTER_COMP),
1922 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
1923 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
1924 minit ("ZERO_COMP", AB_ZERO_COMP),
1925 minit ("PROTECTED", AB_PROTECTED),
1926 minit ("ABSTRACT", AB_ABSTRACT),
1927 minit ("IS_CLASS", AB_IS_CLASS),
1928 minit ("PROCEDURE", AB_PROCEDURE),
1929 minit ("PROC_POINTER", AB_PROC_POINTER),
1930 minit ("VTYPE", AB_VTYPE),
1931 minit ("VTAB", AB_VTAB),
1932 minit ("CLASS_POINTER", AB_CLASS_POINTER),
1933 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
1934 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
1935 minit (NULL, -1)
1938 /* For binding attributes. */
1939 static const mstring binding_passing[] =
1941 minit ("PASS", 0),
1942 minit ("NOPASS", 1),
1943 minit (NULL, -1)
1945 static const mstring binding_overriding[] =
1947 minit ("OVERRIDABLE", 0),
1948 minit ("NON_OVERRIDABLE", 1),
1949 minit ("DEFERRED", 2),
1950 minit (NULL, -1)
1952 static const mstring binding_generic[] =
1954 minit ("SPECIFIC", 0),
1955 minit ("GENERIC", 1),
1956 minit (NULL, -1)
1958 static const mstring binding_ppc[] =
1960 minit ("NO_PPC", 0),
1961 minit ("PPC", 1),
1962 minit (NULL, -1)
1965 /* Specialization of mio_name. */
1966 DECL_MIO_NAME (ab_attribute)
1967 DECL_MIO_NAME (ar_type)
1968 DECL_MIO_NAME (array_type)
1969 DECL_MIO_NAME (bt)
1970 DECL_MIO_NAME (expr_t)
1971 DECL_MIO_NAME (gfc_access)
1972 DECL_MIO_NAME (gfc_intrinsic_op)
1973 DECL_MIO_NAME (ifsrc)
1974 DECL_MIO_NAME (save_state)
1975 DECL_MIO_NAME (procedure_type)
1976 DECL_MIO_NAME (ref_type)
1977 DECL_MIO_NAME (sym_flavor)
1978 DECL_MIO_NAME (sym_intent)
1979 #undef DECL_MIO_NAME
1981 /* Symbol attributes are stored in list with the first three elements
1982 being the enumerated fields, while the remaining elements (if any)
1983 indicate the individual attribute bits. The access field is not
1984 saved-- it controls what symbols are exported when a module is
1985 written. */
1987 static void
1988 mio_symbol_attribute (symbol_attribute *attr)
1990 atom_type t;
1991 unsigned ext_attr,extension_level;
1993 mio_lparen ();
1995 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
1996 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
1997 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
1998 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
1999 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2001 ext_attr = attr->ext_attr;
2002 mio_integer ((int *) &ext_attr);
2003 attr->ext_attr = ext_attr;
2005 extension_level = attr->extension;
2006 mio_integer ((int *) &extension_level);
2007 attr->extension = extension_level;
2009 if (iomode == IO_OUTPUT)
2011 if (attr->allocatable)
2012 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2013 if (attr->artificial)
2014 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2015 if (attr->asynchronous)
2016 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2017 if (attr->dimension)
2018 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2019 if (attr->codimension)
2020 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2021 if (attr->contiguous)
2022 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2023 if (attr->external)
2024 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2025 if (attr->intrinsic)
2026 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2027 if (attr->optional)
2028 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2029 if (attr->pointer)
2030 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2031 if (attr->class_pointer)
2032 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2033 if (attr->is_protected)
2034 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2035 if (attr->value)
2036 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2037 if (attr->volatile_)
2038 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2039 if (attr->target)
2040 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2041 if (attr->threadprivate)
2042 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2043 if (attr->dummy)
2044 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2045 if (attr->result)
2046 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2047 /* We deliberately don't preserve the "entry" flag. */
2049 if (attr->data)
2050 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2051 if (attr->in_namelist)
2052 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2053 if (attr->in_common)
2054 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2056 if (attr->function)
2057 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2058 if (attr->subroutine)
2059 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2060 if (attr->generic)
2061 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2062 if (attr->abstract)
2063 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2065 if (attr->sequence)
2066 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2067 if (attr->elemental)
2068 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2069 if (attr->pure)
2070 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2071 if (attr->implicit_pure)
2072 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2073 if (attr->unlimited_polymorphic)
2074 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2075 if (attr->recursive)
2076 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2077 if (attr->always_explicit)
2078 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2079 if (attr->cray_pointer)
2080 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2081 if (attr->cray_pointee)
2082 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2083 if (attr->is_bind_c)
2084 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2085 if (attr->is_c_interop)
2086 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2087 if (attr->is_iso_c)
2088 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2089 if (attr->alloc_comp)
2090 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2091 if (attr->pointer_comp)
2092 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2093 if (attr->proc_pointer_comp)
2094 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2095 if (attr->private_comp)
2096 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2097 if (attr->coarray_comp)
2098 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2099 if (attr->lock_comp)
2100 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2101 if (attr->zero_comp)
2102 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2103 if (attr->is_class)
2104 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2105 if (attr->procedure)
2106 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2107 if (attr->proc_pointer)
2108 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2109 if (attr->vtype)
2110 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2111 if (attr->vtab)
2112 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2114 mio_rparen ();
2117 else
2119 for (;;)
2121 t = parse_atom ();
2122 if (t == ATOM_RPAREN)
2123 break;
2124 if (t != ATOM_NAME)
2125 bad_module ("Expected attribute bit name");
2127 switch ((ab_attribute) find_enum (attr_bits))
2129 case AB_ALLOCATABLE:
2130 attr->allocatable = 1;
2131 break;
2132 case AB_ARTIFICIAL:
2133 attr->artificial = 1;
2134 break;
2135 case AB_ASYNCHRONOUS:
2136 attr->asynchronous = 1;
2137 break;
2138 case AB_DIMENSION:
2139 attr->dimension = 1;
2140 break;
2141 case AB_CODIMENSION:
2142 attr->codimension = 1;
2143 break;
2144 case AB_CONTIGUOUS:
2145 attr->contiguous = 1;
2146 break;
2147 case AB_EXTERNAL:
2148 attr->external = 1;
2149 break;
2150 case AB_INTRINSIC:
2151 attr->intrinsic = 1;
2152 break;
2153 case AB_OPTIONAL:
2154 attr->optional = 1;
2155 break;
2156 case AB_POINTER:
2157 attr->pointer = 1;
2158 break;
2159 case AB_CLASS_POINTER:
2160 attr->class_pointer = 1;
2161 break;
2162 case AB_PROTECTED:
2163 attr->is_protected = 1;
2164 break;
2165 case AB_VALUE:
2166 attr->value = 1;
2167 break;
2168 case AB_VOLATILE:
2169 attr->volatile_ = 1;
2170 break;
2171 case AB_TARGET:
2172 attr->target = 1;
2173 break;
2174 case AB_THREADPRIVATE:
2175 attr->threadprivate = 1;
2176 break;
2177 case AB_DUMMY:
2178 attr->dummy = 1;
2179 break;
2180 case AB_RESULT:
2181 attr->result = 1;
2182 break;
2183 case AB_DATA:
2184 attr->data = 1;
2185 break;
2186 case AB_IN_NAMELIST:
2187 attr->in_namelist = 1;
2188 break;
2189 case AB_IN_COMMON:
2190 attr->in_common = 1;
2191 break;
2192 case AB_FUNCTION:
2193 attr->function = 1;
2194 break;
2195 case AB_SUBROUTINE:
2196 attr->subroutine = 1;
2197 break;
2198 case AB_GENERIC:
2199 attr->generic = 1;
2200 break;
2201 case AB_ABSTRACT:
2202 attr->abstract = 1;
2203 break;
2204 case AB_SEQUENCE:
2205 attr->sequence = 1;
2206 break;
2207 case AB_ELEMENTAL:
2208 attr->elemental = 1;
2209 break;
2210 case AB_PURE:
2211 attr->pure = 1;
2212 break;
2213 case AB_IMPLICIT_PURE:
2214 attr->implicit_pure = 1;
2215 break;
2216 case AB_UNLIMITED_POLY:
2217 attr->unlimited_polymorphic = 1;
2218 break;
2219 case AB_RECURSIVE:
2220 attr->recursive = 1;
2221 break;
2222 case AB_ALWAYS_EXPLICIT:
2223 attr->always_explicit = 1;
2224 break;
2225 case AB_CRAY_POINTER:
2226 attr->cray_pointer = 1;
2227 break;
2228 case AB_CRAY_POINTEE:
2229 attr->cray_pointee = 1;
2230 break;
2231 case AB_IS_BIND_C:
2232 attr->is_bind_c = 1;
2233 break;
2234 case AB_IS_C_INTEROP:
2235 attr->is_c_interop = 1;
2236 break;
2237 case AB_IS_ISO_C:
2238 attr->is_iso_c = 1;
2239 break;
2240 case AB_ALLOC_COMP:
2241 attr->alloc_comp = 1;
2242 break;
2243 case AB_COARRAY_COMP:
2244 attr->coarray_comp = 1;
2245 break;
2246 case AB_LOCK_COMP:
2247 attr->lock_comp = 1;
2248 break;
2249 case AB_POINTER_COMP:
2250 attr->pointer_comp = 1;
2251 break;
2252 case AB_PROC_POINTER_COMP:
2253 attr->proc_pointer_comp = 1;
2254 break;
2255 case AB_PRIVATE_COMP:
2256 attr->private_comp = 1;
2257 break;
2258 case AB_ZERO_COMP:
2259 attr->zero_comp = 1;
2260 break;
2261 case AB_IS_CLASS:
2262 attr->is_class = 1;
2263 break;
2264 case AB_PROCEDURE:
2265 attr->procedure = 1;
2266 break;
2267 case AB_PROC_POINTER:
2268 attr->proc_pointer = 1;
2269 break;
2270 case AB_VTYPE:
2271 attr->vtype = 1;
2272 break;
2273 case AB_VTAB:
2274 attr->vtab = 1;
2275 break;
2282 static const mstring bt_types[] = {
2283 minit ("INTEGER", BT_INTEGER),
2284 minit ("REAL", BT_REAL),
2285 minit ("COMPLEX", BT_COMPLEX),
2286 minit ("LOGICAL", BT_LOGICAL),
2287 minit ("CHARACTER", BT_CHARACTER),
2288 minit ("DERIVED", BT_DERIVED),
2289 minit ("CLASS", BT_CLASS),
2290 minit ("PROCEDURE", BT_PROCEDURE),
2291 minit ("UNKNOWN", BT_UNKNOWN),
2292 minit ("VOID", BT_VOID),
2293 minit ("ASSUMED", BT_ASSUMED),
2294 minit (NULL, -1)
2298 static void
2299 mio_charlen (gfc_charlen **clp)
2301 gfc_charlen *cl;
2303 mio_lparen ();
2305 if (iomode == IO_OUTPUT)
2307 cl = *clp;
2308 if (cl != NULL)
2309 mio_expr (&cl->length);
2311 else
2313 if (peek_atom () != ATOM_RPAREN)
2315 cl = gfc_new_charlen (gfc_current_ns, NULL);
2316 mio_expr (&cl->length);
2317 *clp = cl;
2321 mio_rparen ();
2325 /* See if a name is a generated name. */
2327 static int
2328 check_unique_name (const char *name)
2330 return *name == '@';
2334 static void
2335 mio_typespec (gfc_typespec *ts)
2337 mio_lparen ();
2339 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2341 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2342 mio_integer (&ts->kind);
2343 else
2344 mio_symbol_ref (&ts->u.derived);
2346 mio_symbol_ref (&ts->interface);
2348 /* Add info for C interop and is_iso_c. */
2349 mio_integer (&ts->is_c_interop);
2350 mio_integer (&ts->is_iso_c);
2352 /* If the typespec is for an identifier either from iso_c_binding, or
2353 a constant that was initialized to an identifier from it, use the
2354 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2355 if (ts->is_iso_c)
2356 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2357 else
2358 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2360 if (ts->type != BT_CHARACTER)
2362 /* ts->u.cl is only valid for BT_CHARACTER. */
2363 mio_lparen ();
2364 mio_rparen ();
2366 else
2367 mio_charlen (&ts->u.cl);
2369 /* So as not to disturb the existing API, use an ATOM_NAME to
2370 transmit deferred characteristic for characters (F2003). */
2371 if (iomode == IO_OUTPUT)
2373 if (ts->type == BT_CHARACTER && ts->deferred)
2374 write_atom (ATOM_NAME, "DEFERRED_CL");
2376 else if (peek_atom () != ATOM_RPAREN)
2378 if (parse_atom () != ATOM_NAME)
2379 bad_module ("Expected string");
2380 ts->deferred = 1;
2383 mio_rparen ();
2387 static const mstring array_spec_types[] = {
2388 minit ("EXPLICIT", AS_EXPLICIT),
2389 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2390 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2391 minit ("DEFERRED", AS_DEFERRED),
2392 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2393 minit (NULL, -1)
2397 static void
2398 mio_array_spec (gfc_array_spec **asp)
2400 gfc_array_spec *as;
2401 int i;
2403 mio_lparen ();
2405 if (iomode == IO_OUTPUT)
2407 int rank;
2409 if (*asp == NULL)
2410 goto done;
2411 as = *asp;
2413 /* mio_integer expects nonnegative values. */
2414 rank = as->rank > 0 ? as->rank : 0;
2415 mio_integer (&rank);
2417 else
2419 if (peek_atom () == ATOM_RPAREN)
2421 *asp = NULL;
2422 goto done;
2425 *asp = as = gfc_get_array_spec ();
2426 mio_integer (&as->rank);
2429 mio_integer (&as->corank);
2430 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2432 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2433 as->rank = -1;
2434 if (iomode == IO_INPUT && as->corank)
2435 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2437 if (as->rank + as->corank > 0)
2438 for (i = 0; i < as->rank + as->corank; i++)
2440 mio_expr (&as->lower[i]);
2441 mio_expr (&as->upper[i]);
2444 done:
2445 mio_rparen ();
2449 /* Given a pointer to an array reference structure (which lives in a
2450 gfc_ref structure), find the corresponding array specification
2451 structure. Storing the pointer in the ref structure doesn't quite
2452 work when loading from a module. Generating code for an array
2453 reference also needs more information than just the array spec. */
2455 static const mstring array_ref_types[] = {
2456 minit ("FULL", AR_FULL),
2457 minit ("ELEMENT", AR_ELEMENT),
2458 minit ("SECTION", AR_SECTION),
2459 minit (NULL, -1)
2463 static void
2464 mio_array_ref (gfc_array_ref *ar)
2466 int i;
2468 mio_lparen ();
2469 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2470 mio_integer (&ar->dimen);
2472 switch (ar->type)
2474 case AR_FULL:
2475 break;
2477 case AR_ELEMENT:
2478 for (i = 0; i < ar->dimen; i++)
2479 mio_expr (&ar->start[i]);
2481 break;
2483 case AR_SECTION:
2484 for (i = 0; i < ar->dimen; i++)
2486 mio_expr (&ar->start[i]);
2487 mio_expr (&ar->end[i]);
2488 mio_expr (&ar->stride[i]);
2491 break;
2493 case AR_UNKNOWN:
2494 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2497 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2498 we can't call mio_integer directly. Instead loop over each element
2499 and cast it to/from an integer. */
2500 if (iomode == IO_OUTPUT)
2502 for (i = 0; i < ar->dimen; i++)
2504 int tmp = (int)ar->dimen_type[i];
2505 write_atom (ATOM_INTEGER, &tmp);
2508 else
2510 for (i = 0; i < ar->dimen; i++)
2512 require_atom (ATOM_INTEGER);
2513 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2517 if (iomode == IO_INPUT)
2519 ar->where = gfc_current_locus;
2521 for (i = 0; i < ar->dimen; i++)
2522 ar->c_where[i] = gfc_current_locus;
2525 mio_rparen ();
2529 /* Saves or restores a pointer. The pointer is converted back and
2530 forth from an integer. We return the pointer_info pointer so that
2531 the caller can take additional action based on the pointer type. */
2533 static pointer_info *
2534 mio_pointer_ref (void *gp)
2536 pointer_info *p;
2538 if (iomode == IO_OUTPUT)
2540 p = get_pointer (*((char **) gp));
2541 write_atom (ATOM_INTEGER, &p->integer);
2543 else
2545 require_atom (ATOM_INTEGER);
2546 p = add_fixup (atom_int, gp);
2549 return p;
2553 /* Save and load references to components that occur within
2554 expressions. We have to describe these references by a number and
2555 by name. The number is necessary for forward references during
2556 reading, and the name is necessary if the symbol already exists in
2557 the namespace and is not loaded again. */
2559 static void
2560 mio_component_ref (gfc_component **cp)
2562 pointer_info *p;
2564 p = mio_pointer_ref (cp);
2565 if (p->type == P_UNKNOWN)
2566 p->type = P_COMPONENT;
2570 static void mio_namespace_ref (gfc_namespace **nsp);
2571 static void mio_formal_arglist (gfc_formal_arglist **formal);
2572 static void mio_typebound_proc (gfc_typebound_proc** proc);
2574 static void
2575 mio_component (gfc_component *c, int vtype)
2577 pointer_info *p;
2578 int n;
2580 mio_lparen ();
2582 if (iomode == IO_OUTPUT)
2584 p = get_pointer (c);
2585 mio_integer (&p->integer);
2587 else
2589 mio_integer (&n);
2590 p = get_integer (n);
2591 associate_integer_pointer (p, c);
2594 if (p->type == P_UNKNOWN)
2595 p->type = P_COMPONENT;
2597 mio_pool_string (&c->name);
2598 mio_typespec (&c->ts);
2599 mio_array_spec (&c->as);
2601 mio_symbol_attribute (&c->attr);
2602 if (c->ts.type == BT_CLASS)
2603 c->attr.class_ok = 1;
2604 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2606 if (!vtype || strcmp (c->name, "_final") == 0
2607 || strcmp (c->name, "_hash") == 0)
2608 mio_expr (&c->initializer);
2610 if (c->attr.proc_pointer)
2611 mio_typebound_proc (&c->tb);
2613 mio_rparen ();
2617 static void
2618 mio_component_list (gfc_component **cp, int vtype)
2620 gfc_component *c, *tail;
2622 mio_lparen ();
2624 if (iomode == IO_OUTPUT)
2626 for (c = *cp; c; c = c->next)
2627 mio_component (c, vtype);
2629 else
2631 *cp = NULL;
2632 tail = NULL;
2634 for (;;)
2636 if (peek_atom () == ATOM_RPAREN)
2637 break;
2639 c = gfc_get_component ();
2640 mio_component (c, vtype);
2642 if (tail == NULL)
2643 *cp = c;
2644 else
2645 tail->next = c;
2647 tail = c;
2651 mio_rparen ();
2655 static void
2656 mio_actual_arg (gfc_actual_arglist *a)
2658 mio_lparen ();
2659 mio_pool_string (&a->name);
2660 mio_expr (&a->expr);
2661 mio_rparen ();
2665 static void
2666 mio_actual_arglist (gfc_actual_arglist **ap)
2668 gfc_actual_arglist *a, *tail;
2670 mio_lparen ();
2672 if (iomode == IO_OUTPUT)
2674 for (a = *ap; a; a = a->next)
2675 mio_actual_arg (a);
2678 else
2680 tail = NULL;
2682 for (;;)
2684 if (peek_atom () != ATOM_LPAREN)
2685 break;
2687 a = gfc_get_actual_arglist ();
2689 if (tail == NULL)
2690 *ap = a;
2691 else
2692 tail->next = a;
2694 tail = a;
2695 mio_actual_arg (a);
2699 mio_rparen ();
2703 /* Read and write formal argument lists. */
2705 static void
2706 mio_formal_arglist (gfc_formal_arglist **formal)
2708 gfc_formal_arglist *f, *tail;
2710 mio_lparen ();
2712 if (iomode == IO_OUTPUT)
2714 for (f = *formal; f; f = f->next)
2715 mio_symbol_ref (&f->sym);
2717 else
2719 *formal = tail = NULL;
2721 while (peek_atom () != ATOM_RPAREN)
2723 f = gfc_get_formal_arglist ();
2724 mio_symbol_ref (&f->sym);
2726 if (*formal == NULL)
2727 *formal = f;
2728 else
2729 tail->next = f;
2731 tail = f;
2735 mio_rparen ();
2739 /* Save or restore a reference to a symbol node. */
2741 pointer_info *
2742 mio_symbol_ref (gfc_symbol **symp)
2744 pointer_info *p;
2746 p = mio_pointer_ref (symp);
2747 if (p->type == P_UNKNOWN)
2748 p->type = P_SYMBOL;
2750 if (iomode == IO_OUTPUT)
2752 if (p->u.wsym.state == UNREFERENCED)
2753 p->u.wsym.state = NEEDS_WRITE;
2755 else
2757 if (p->u.rsym.state == UNUSED)
2758 p->u.rsym.state = NEEDED;
2760 return p;
2764 /* Save or restore a reference to a symtree node. */
2766 static void
2767 mio_symtree_ref (gfc_symtree **stp)
2769 pointer_info *p;
2770 fixup_t *f;
2772 if (iomode == IO_OUTPUT)
2773 mio_symbol_ref (&(*stp)->n.sym);
2774 else
2776 require_atom (ATOM_INTEGER);
2777 p = get_integer (atom_int);
2779 /* An unused equivalence member; make a symbol and a symtree
2780 for it. */
2781 if (in_load_equiv && p->u.rsym.symtree == NULL)
2783 /* Since this is not used, it must have a unique name. */
2784 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2786 /* Make the symbol. */
2787 if (p->u.rsym.sym == NULL)
2789 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2790 gfc_current_ns);
2791 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2794 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2795 p->u.rsym.symtree->n.sym->refs++;
2796 p->u.rsym.referenced = 1;
2798 /* If the symbol is PRIVATE and in COMMON, load_commons will
2799 generate a fixup symbol, which must be associated. */
2800 if (p->fixup)
2801 resolve_fixups (p->fixup, p->u.rsym.sym);
2802 p->fixup = NULL;
2805 if (p->type == P_UNKNOWN)
2806 p->type = P_SYMBOL;
2808 if (p->u.rsym.state == UNUSED)
2809 p->u.rsym.state = NEEDED;
2811 if (p->u.rsym.symtree != NULL)
2813 *stp = p->u.rsym.symtree;
2815 else
2817 f = XCNEW (fixup_t);
2819 f->next = p->u.rsym.stfixup;
2820 p->u.rsym.stfixup = f;
2822 f->pointer = (void **) stp;
2828 static void
2829 mio_iterator (gfc_iterator **ip)
2831 gfc_iterator *iter;
2833 mio_lparen ();
2835 if (iomode == IO_OUTPUT)
2837 if (*ip == NULL)
2838 goto done;
2840 else
2842 if (peek_atom () == ATOM_RPAREN)
2844 *ip = NULL;
2845 goto done;
2848 *ip = gfc_get_iterator ();
2851 iter = *ip;
2853 mio_expr (&iter->var);
2854 mio_expr (&iter->start);
2855 mio_expr (&iter->end);
2856 mio_expr (&iter->step);
2858 done:
2859 mio_rparen ();
2863 static void
2864 mio_constructor (gfc_constructor_base *cp)
2866 gfc_constructor *c;
2868 mio_lparen ();
2870 if (iomode == IO_OUTPUT)
2872 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2874 mio_lparen ();
2875 mio_expr (&c->expr);
2876 mio_iterator (&c->iterator);
2877 mio_rparen ();
2880 else
2882 while (peek_atom () != ATOM_RPAREN)
2884 c = gfc_constructor_append_expr (cp, NULL, NULL);
2886 mio_lparen ();
2887 mio_expr (&c->expr);
2888 mio_iterator (&c->iterator);
2889 mio_rparen ();
2893 mio_rparen ();
2897 static const mstring ref_types[] = {
2898 minit ("ARRAY", REF_ARRAY),
2899 minit ("COMPONENT", REF_COMPONENT),
2900 minit ("SUBSTRING", REF_SUBSTRING),
2901 minit (NULL, -1)
2905 static void
2906 mio_ref (gfc_ref **rp)
2908 gfc_ref *r;
2910 mio_lparen ();
2912 r = *rp;
2913 r->type = MIO_NAME (ref_type) (r->type, ref_types);
2915 switch (r->type)
2917 case REF_ARRAY:
2918 mio_array_ref (&r->u.ar);
2919 break;
2921 case REF_COMPONENT:
2922 mio_symbol_ref (&r->u.c.sym);
2923 mio_component_ref (&r->u.c.component);
2924 break;
2926 case REF_SUBSTRING:
2927 mio_expr (&r->u.ss.start);
2928 mio_expr (&r->u.ss.end);
2929 mio_charlen (&r->u.ss.length);
2930 break;
2933 mio_rparen ();
2937 static void
2938 mio_ref_list (gfc_ref **rp)
2940 gfc_ref *ref, *head, *tail;
2942 mio_lparen ();
2944 if (iomode == IO_OUTPUT)
2946 for (ref = *rp; ref; ref = ref->next)
2947 mio_ref (&ref);
2949 else
2951 head = tail = NULL;
2953 while (peek_atom () != ATOM_RPAREN)
2955 if (head == NULL)
2956 head = tail = gfc_get_ref ();
2957 else
2959 tail->next = gfc_get_ref ();
2960 tail = tail->next;
2963 mio_ref (&tail);
2966 *rp = head;
2969 mio_rparen ();
2973 /* Read and write an integer value. */
2975 static void
2976 mio_gmp_integer (mpz_t *integer)
2978 char *p;
2980 if (iomode == IO_INPUT)
2982 if (parse_atom () != ATOM_STRING)
2983 bad_module ("Expected integer string");
2985 mpz_init (*integer);
2986 if (mpz_set_str (*integer, atom_string, 10))
2987 bad_module ("Error converting integer");
2989 free (atom_string);
2991 else
2993 p = mpz_get_str (NULL, 10, *integer);
2994 write_atom (ATOM_STRING, p);
2995 free (p);
3000 static void
3001 mio_gmp_real (mpfr_t *real)
3003 mp_exp_t exponent;
3004 char *p;
3006 if (iomode == IO_INPUT)
3008 if (parse_atom () != ATOM_STRING)
3009 bad_module ("Expected real string");
3011 mpfr_init (*real);
3012 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3013 free (atom_string);
3015 else
3017 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3019 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3021 write_atom (ATOM_STRING, p);
3022 free (p);
3023 return;
3026 atom_string = XCNEWVEC (char, strlen (p) + 20);
3028 sprintf (atom_string, "0.%s@%ld", p, exponent);
3030 /* Fix negative numbers. */
3031 if (atom_string[2] == '-')
3033 atom_string[0] = '-';
3034 atom_string[1] = '0';
3035 atom_string[2] = '.';
3038 write_atom (ATOM_STRING, atom_string);
3040 free (atom_string);
3041 free (p);
3046 /* Save and restore the shape of an array constructor. */
3048 static void
3049 mio_shape (mpz_t **pshape, int rank)
3051 mpz_t *shape;
3052 atom_type t;
3053 int n;
3055 /* A NULL shape is represented by (). */
3056 mio_lparen ();
3058 if (iomode == IO_OUTPUT)
3060 shape = *pshape;
3061 if (!shape)
3063 mio_rparen ();
3064 return;
3067 else
3069 t = peek_atom ();
3070 if (t == ATOM_RPAREN)
3072 *pshape = NULL;
3073 mio_rparen ();
3074 return;
3077 shape = gfc_get_shape (rank);
3078 *pshape = shape;
3081 for (n = 0; n < rank; n++)
3082 mio_gmp_integer (&shape[n]);
3084 mio_rparen ();
3088 static const mstring expr_types[] = {
3089 minit ("OP", EXPR_OP),
3090 minit ("FUNCTION", EXPR_FUNCTION),
3091 minit ("CONSTANT", EXPR_CONSTANT),
3092 minit ("VARIABLE", EXPR_VARIABLE),
3093 minit ("SUBSTRING", EXPR_SUBSTRING),
3094 minit ("STRUCTURE", EXPR_STRUCTURE),
3095 minit ("ARRAY", EXPR_ARRAY),
3096 minit ("NULL", EXPR_NULL),
3097 minit ("COMPCALL", EXPR_COMPCALL),
3098 minit (NULL, -1)
3101 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3102 generic operators, not in expressions. INTRINSIC_USER is also
3103 replaced by the correct function name by the time we see it. */
3105 static const mstring intrinsics[] =
3107 minit ("UPLUS", INTRINSIC_UPLUS),
3108 minit ("UMINUS", INTRINSIC_UMINUS),
3109 minit ("PLUS", INTRINSIC_PLUS),
3110 minit ("MINUS", INTRINSIC_MINUS),
3111 minit ("TIMES", INTRINSIC_TIMES),
3112 minit ("DIVIDE", INTRINSIC_DIVIDE),
3113 minit ("POWER", INTRINSIC_POWER),
3114 minit ("CONCAT", INTRINSIC_CONCAT),
3115 minit ("AND", INTRINSIC_AND),
3116 minit ("OR", INTRINSIC_OR),
3117 minit ("EQV", INTRINSIC_EQV),
3118 minit ("NEQV", INTRINSIC_NEQV),
3119 minit ("EQ_SIGN", INTRINSIC_EQ),
3120 minit ("EQ", INTRINSIC_EQ_OS),
3121 minit ("NE_SIGN", INTRINSIC_NE),
3122 minit ("NE", INTRINSIC_NE_OS),
3123 minit ("GT_SIGN", INTRINSIC_GT),
3124 minit ("GT", INTRINSIC_GT_OS),
3125 minit ("GE_SIGN", INTRINSIC_GE),
3126 minit ("GE", INTRINSIC_GE_OS),
3127 minit ("LT_SIGN", INTRINSIC_LT),
3128 minit ("LT", INTRINSIC_LT_OS),
3129 minit ("LE_SIGN", INTRINSIC_LE),
3130 minit ("LE", INTRINSIC_LE_OS),
3131 minit ("NOT", INTRINSIC_NOT),
3132 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3133 minit (NULL, -1)
3137 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3139 static void
3140 fix_mio_expr (gfc_expr *e)
3142 gfc_symtree *ns_st = NULL;
3143 const char *fname;
3145 if (iomode != IO_OUTPUT)
3146 return;
3148 if (e->symtree)
3150 /* If this is a symtree for a symbol that came from a contained module
3151 namespace, it has a unique name and we should look in the current
3152 namespace to see if the required, non-contained symbol is available
3153 yet. If so, the latter should be written. */
3154 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3156 const char *name = e->symtree->n.sym->name;
3157 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3158 name = dt_upper_string (name);
3159 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3162 /* On the other hand, if the existing symbol is the module name or the
3163 new symbol is a dummy argument, do not do the promotion. */
3164 if (ns_st && ns_st->n.sym
3165 && ns_st->n.sym->attr.flavor != FL_MODULE
3166 && !e->symtree->n.sym->attr.dummy)
3167 e->symtree = ns_st;
3169 else if (e->expr_type == EXPR_FUNCTION && e->value.function.name)
3171 gfc_symbol *sym;
3173 /* In some circumstances, a function used in an initialization
3174 expression, in one use associated module, can fail to be
3175 coupled to its symtree when used in a specification
3176 expression in another module. */
3177 fname = e->value.function.esym ? e->value.function.esym->name
3178 : e->value.function.isym->name;
3179 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3181 if (e->symtree)
3182 return;
3184 /* This is probably a reference to a private procedure from another
3185 module. To prevent a segfault, make a generic with no specific
3186 instances. If this module is used, without the required
3187 specific coming from somewhere, the appropriate error message
3188 is issued. */
3189 gfc_get_symbol (fname, gfc_current_ns, &sym);
3190 sym->attr.flavor = FL_PROCEDURE;
3191 sym->attr.generic = 1;
3192 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3193 gfc_commit_symbol (sym);
3198 /* Read and write expressions. The form "()" is allowed to indicate a
3199 NULL expression. */
3201 static void
3202 mio_expr (gfc_expr **ep)
3204 gfc_expr *e;
3205 atom_type t;
3206 int flag;
3208 mio_lparen ();
3210 if (iomode == IO_OUTPUT)
3212 if (*ep == NULL)
3214 mio_rparen ();
3215 return;
3218 e = *ep;
3219 MIO_NAME (expr_t) (e->expr_type, expr_types);
3221 else
3223 t = parse_atom ();
3224 if (t == ATOM_RPAREN)
3226 *ep = NULL;
3227 return;
3230 if (t != ATOM_NAME)
3231 bad_module ("Expected expression type");
3233 e = *ep = gfc_get_expr ();
3234 e->where = gfc_current_locus;
3235 e->expr_type = (expr_t) find_enum (expr_types);
3238 mio_typespec (&e->ts);
3239 mio_integer (&e->rank);
3241 fix_mio_expr (e);
3243 switch (e->expr_type)
3245 case EXPR_OP:
3246 e->value.op.op
3247 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3249 switch (e->value.op.op)
3251 case INTRINSIC_UPLUS:
3252 case INTRINSIC_UMINUS:
3253 case INTRINSIC_NOT:
3254 case INTRINSIC_PARENTHESES:
3255 mio_expr (&e->value.op.op1);
3256 break;
3258 case INTRINSIC_PLUS:
3259 case INTRINSIC_MINUS:
3260 case INTRINSIC_TIMES:
3261 case INTRINSIC_DIVIDE:
3262 case INTRINSIC_POWER:
3263 case INTRINSIC_CONCAT:
3264 case INTRINSIC_AND:
3265 case INTRINSIC_OR:
3266 case INTRINSIC_EQV:
3267 case INTRINSIC_NEQV:
3268 case INTRINSIC_EQ:
3269 case INTRINSIC_EQ_OS:
3270 case INTRINSIC_NE:
3271 case INTRINSIC_NE_OS:
3272 case INTRINSIC_GT:
3273 case INTRINSIC_GT_OS:
3274 case INTRINSIC_GE:
3275 case INTRINSIC_GE_OS:
3276 case INTRINSIC_LT:
3277 case INTRINSIC_LT_OS:
3278 case INTRINSIC_LE:
3279 case INTRINSIC_LE_OS:
3280 mio_expr (&e->value.op.op1);
3281 mio_expr (&e->value.op.op2);
3282 break;
3284 default:
3285 bad_module ("Bad operator");
3288 break;
3290 case EXPR_FUNCTION:
3291 mio_symtree_ref (&e->symtree);
3292 mio_actual_arglist (&e->value.function.actual);
3294 if (iomode == IO_OUTPUT)
3296 e->value.function.name
3297 = mio_allocated_string (e->value.function.name);
3298 if (e->value.function.esym)
3299 flag = 1;
3300 else if (e->ref)
3301 flag = 2;
3302 else
3303 flag = 0;
3304 mio_integer (&flag);
3305 switch (flag)
3307 case 1:
3308 mio_symbol_ref (&e->value.function.esym);
3309 break;
3310 case 2:
3311 mio_ref_list (&e->ref);
3312 break;
3313 default:
3314 write_atom (ATOM_STRING, e->value.function.isym->name);
3317 else
3319 require_atom (ATOM_STRING);
3320 e->value.function.name = gfc_get_string (atom_string);
3321 free (atom_string);
3323 mio_integer (&flag);
3324 switch (flag)
3326 case 1:
3327 mio_symbol_ref (&e->value.function.esym);
3328 break;
3329 case 2:
3330 mio_ref_list (&e->ref);
3331 break;
3332 default:
3333 require_atom (ATOM_STRING);
3334 e->value.function.isym = gfc_find_function (atom_string);
3335 free (atom_string);
3339 break;
3341 case EXPR_VARIABLE:
3342 mio_symtree_ref (&e->symtree);
3343 mio_ref_list (&e->ref);
3344 break;
3346 case EXPR_SUBSTRING:
3347 e->value.character.string
3348 = CONST_CAST (gfc_char_t *,
3349 mio_allocated_wide_string (e->value.character.string,
3350 e->value.character.length));
3351 mio_ref_list (&e->ref);
3352 break;
3354 case EXPR_STRUCTURE:
3355 case EXPR_ARRAY:
3356 mio_constructor (&e->value.constructor);
3357 mio_shape (&e->shape, e->rank);
3358 break;
3360 case EXPR_CONSTANT:
3361 switch (e->ts.type)
3363 case BT_INTEGER:
3364 mio_gmp_integer (&e->value.integer);
3365 break;
3367 case BT_REAL:
3368 gfc_set_model_kind (e->ts.kind);
3369 mio_gmp_real (&e->value.real);
3370 break;
3372 case BT_COMPLEX:
3373 gfc_set_model_kind (e->ts.kind);
3374 mio_gmp_real (&mpc_realref (e->value.complex));
3375 mio_gmp_real (&mpc_imagref (e->value.complex));
3376 break;
3378 case BT_LOGICAL:
3379 mio_integer (&e->value.logical);
3380 break;
3382 case BT_CHARACTER:
3383 mio_integer (&e->value.character.length);
3384 e->value.character.string
3385 = CONST_CAST (gfc_char_t *,
3386 mio_allocated_wide_string (e->value.character.string,
3387 e->value.character.length));
3388 break;
3390 default:
3391 bad_module ("Bad type in constant expression");
3394 break;
3396 case EXPR_NULL:
3397 break;
3399 case EXPR_COMPCALL:
3400 case EXPR_PPC:
3401 gcc_unreachable ();
3402 break;
3405 mio_rparen ();
3409 /* Read and write namelists. */
3411 static void
3412 mio_namelist (gfc_symbol *sym)
3414 gfc_namelist *n, *m;
3415 const char *check_name;
3417 mio_lparen ();
3419 if (iomode == IO_OUTPUT)
3421 for (n = sym->namelist; n; n = n->next)
3422 mio_symbol_ref (&n->sym);
3424 else
3426 /* This departure from the standard is flagged as an error.
3427 It does, in fact, work correctly. TODO: Allow it
3428 conditionally? */
3429 if (sym->attr.flavor == FL_NAMELIST)
3431 check_name = find_use_name (sym->name, false);
3432 if (check_name && strcmp (check_name, sym->name) != 0)
3433 gfc_error ("Namelist %s cannot be renamed by USE "
3434 "association to %s", sym->name, check_name);
3437 m = NULL;
3438 while (peek_atom () != ATOM_RPAREN)
3440 n = gfc_get_namelist ();
3441 mio_symbol_ref (&n->sym);
3443 if (sym->namelist == NULL)
3444 sym->namelist = n;
3445 else
3446 m->next = n;
3448 m = n;
3450 sym->namelist_tail = m;
3453 mio_rparen ();
3457 /* Save/restore lists of gfc_interface structures. When loading an
3458 interface, we are really appending to the existing list of
3459 interfaces. Checking for duplicate and ambiguous interfaces has to
3460 be done later when all symbols have been loaded. */
3462 pointer_info *
3463 mio_interface_rest (gfc_interface **ip)
3465 gfc_interface *tail, *p;
3466 pointer_info *pi = NULL;
3468 if (iomode == IO_OUTPUT)
3470 if (ip != NULL)
3471 for (p = *ip; p; p = p->next)
3472 mio_symbol_ref (&p->sym);
3474 else
3476 if (*ip == NULL)
3477 tail = NULL;
3478 else
3480 tail = *ip;
3481 while (tail->next)
3482 tail = tail->next;
3485 for (;;)
3487 if (peek_atom () == ATOM_RPAREN)
3488 break;
3490 p = gfc_get_interface ();
3491 p->where = gfc_current_locus;
3492 pi = mio_symbol_ref (&p->sym);
3494 if (tail == NULL)
3495 *ip = p;
3496 else
3497 tail->next = p;
3499 tail = p;
3503 mio_rparen ();
3504 return pi;
3508 /* Save/restore a nameless operator interface. */
3510 static void
3511 mio_interface (gfc_interface **ip)
3513 mio_lparen ();
3514 mio_interface_rest (ip);
3518 /* Save/restore a named operator interface. */
3520 static void
3521 mio_symbol_interface (const char **name, const char **module,
3522 gfc_interface **ip)
3524 mio_lparen ();
3525 mio_pool_string (name);
3526 mio_pool_string (module);
3527 mio_interface_rest (ip);
3531 static void
3532 mio_namespace_ref (gfc_namespace **nsp)
3534 gfc_namespace *ns;
3535 pointer_info *p;
3537 p = mio_pointer_ref (nsp);
3539 if (p->type == P_UNKNOWN)
3540 p->type = P_NAMESPACE;
3542 if (iomode == IO_INPUT && p->integer != 0)
3544 ns = (gfc_namespace *) p->u.pointer;
3545 if (ns == NULL)
3547 ns = gfc_get_namespace (NULL, 0);
3548 associate_integer_pointer (p, ns);
3550 else
3551 ns->refs++;
3556 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3558 static gfc_namespace* current_f2k_derived;
3560 static void
3561 mio_typebound_proc (gfc_typebound_proc** proc)
3563 int flag;
3564 int overriding_flag;
3566 if (iomode == IO_INPUT)
3568 *proc = gfc_get_typebound_proc (NULL);
3569 (*proc)->where = gfc_current_locus;
3571 gcc_assert (*proc);
3573 mio_lparen ();
3575 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3577 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3578 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3579 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3580 overriding_flag = mio_name (overriding_flag, binding_overriding);
3581 (*proc)->deferred = ((overriding_flag & 2) != 0);
3582 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3583 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3585 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3586 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3587 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3589 mio_pool_string (&((*proc)->pass_arg));
3591 flag = (int) (*proc)->pass_arg_num;
3592 mio_integer (&flag);
3593 (*proc)->pass_arg_num = (unsigned) flag;
3595 if ((*proc)->is_generic)
3597 gfc_tbp_generic* g;
3598 int iop;
3600 mio_lparen ();
3602 if (iomode == IO_OUTPUT)
3603 for (g = (*proc)->u.generic; g; g = g->next)
3605 iop = (int) g->is_operator;
3606 mio_integer (&iop);
3607 mio_allocated_string (g->specific_st->name);
3609 else
3611 (*proc)->u.generic = NULL;
3612 while (peek_atom () != ATOM_RPAREN)
3614 gfc_symtree** sym_root;
3616 g = gfc_get_tbp_generic ();
3617 g->specific = NULL;
3619 mio_integer (&iop);
3620 g->is_operator = (bool) iop;
3622 require_atom (ATOM_STRING);
3623 sym_root = &current_f2k_derived->tb_sym_root;
3624 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3625 free (atom_string);
3627 g->next = (*proc)->u.generic;
3628 (*proc)->u.generic = g;
3632 mio_rparen ();
3634 else if (!(*proc)->ppc)
3635 mio_symtree_ref (&(*proc)->u.specific);
3637 mio_rparen ();
3640 /* Walker-callback function for this purpose. */
3641 static void
3642 mio_typebound_symtree (gfc_symtree* st)
3644 if (iomode == IO_OUTPUT && !st->n.tb)
3645 return;
3647 if (iomode == IO_OUTPUT)
3649 mio_lparen ();
3650 mio_allocated_string (st->name);
3652 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3654 mio_typebound_proc (&st->n.tb);
3655 mio_rparen ();
3658 /* IO a full symtree (in all depth). */
3659 static void
3660 mio_full_typebound_tree (gfc_symtree** root)
3662 mio_lparen ();
3664 if (iomode == IO_OUTPUT)
3665 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3666 else
3668 while (peek_atom () == ATOM_LPAREN)
3670 gfc_symtree* st;
3672 mio_lparen ();
3674 require_atom (ATOM_STRING);
3675 st = gfc_get_tbp_symtree (root, atom_string);
3676 free (atom_string);
3678 mio_typebound_symtree (st);
3682 mio_rparen ();
3685 static void
3686 mio_finalizer (gfc_finalizer **f)
3688 if (iomode == IO_OUTPUT)
3690 gcc_assert (*f);
3691 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3692 mio_symtree_ref (&(*f)->proc_tree);
3694 else
3696 *f = gfc_get_finalizer ();
3697 (*f)->where = gfc_current_locus; /* Value should not matter. */
3698 (*f)->next = NULL;
3700 mio_symtree_ref (&(*f)->proc_tree);
3701 (*f)->proc_sym = NULL;
3705 static void
3706 mio_f2k_derived (gfc_namespace *f2k)
3708 current_f2k_derived = f2k;
3710 /* Handle the list of finalizer procedures. */
3711 mio_lparen ();
3712 if (iomode == IO_OUTPUT)
3714 gfc_finalizer *f;
3715 for (f = f2k->finalizers; f; f = f->next)
3716 mio_finalizer (&f);
3718 else
3720 f2k->finalizers = NULL;
3721 while (peek_atom () != ATOM_RPAREN)
3723 gfc_finalizer *cur = NULL;
3724 mio_finalizer (&cur);
3725 cur->next = f2k->finalizers;
3726 f2k->finalizers = cur;
3729 mio_rparen ();
3731 /* Handle type-bound procedures. */
3732 mio_full_typebound_tree (&f2k->tb_sym_root);
3734 /* Type-bound user operators. */
3735 mio_full_typebound_tree (&f2k->tb_uop_root);
3737 /* Type-bound intrinsic operators. */
3738 mio_lparen ();
3739 if (iomode == IO_OUTPUT)
3741 int op;
3742 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3744 gfc_intrinsic_op realop;
3746 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3747 continue;
3749 mio_lparen ();
3750 realop = (gfc_intrinsic_op) op;
3751 mio_intrinsic_op (&realop);
3752 mio_typebound_proc (&f2k->tb_op[op]);
3753 mio_rparen ();
3756 else
3757 while (peek_atom () != ATOM_RPAREN)
3759 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3761 mio_lparen ();
3762 mio_intrinsic_op (&op);
3763 mio_typebound_proc (&f2k->tb_op[op]);
3764 mio_rparen ();
3766 mio_rparen ();
3769 static void
3770 mio_full_f2k_derived (gfc_symbol *sym)
3772 mio_lparen ();
3774 if (iomode == IO_OUTPUT)
3776 if (sym->f2k_derived)
3777 mio_f2k_derived (sym->f2k_derived);
3779 else
3781 if (peek_atom () != ATOM_RPAREN)
3783 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3784 mio_f2k_derived (sym->f2k_derived);
3786 else
3787 gcc_assert (!sym->f2k_derived);
3790 mio_rparen ();
3794 /* Unlike most other routines, the address of the symbol node is already
3795 fixed on input and the name/module has already been filled in.
3796 If you update the symbol format here, don't forget to update read_module
3797 as well (look for "seek to the symbol's component list"). */
3799 static void
3800 mio_symbol (gfc_symbol *sym)
3802 int intmod = INTMOD_NONE;
3804 mio_lparen ();
3806 mio_symbol_attribute (&sym->attr);
3808 /* Note that components are always saved, even if they are supposed
3809 to be private. Component access is checked during searching. */
3810 mio_component_list (&sym->components, sym->attr.vtype);
3811 if (sym->components != NULL)
3812 sym->component_access
3813 = MIO_NAME (gfc_access) (sym->component_access, access_types);
3815 mio_typespec (&sym->ts);
3816 if (sym->ts.type == BT_CLASS)
3817 sym->attr.class_ok = 1;
3819 if (iomode == IO_OUTPUT)
3820 mio_namespace_ref (&sym->formal_ns);
3821 else
3823 mio_namespace_ref (&sym->formal_ns);
3824 if (sym->formal_ns)
3825 sym->formal_ns->proc_name = sym;
3828 /* Save/restore common block links. */
3829 mio_symbol_ref (&sym->common_next);
3831 mio_formal_arglist (&sym->formal);
3833 if (sym->attr.flavor == FL_PARAMETER)
3834 mio_expr (&sym->value);
3836 mio_array_spec (&sym->as);
3838 mio_symbol_ref (&sym->result);
3840 if (sym->attr.cray_pointee)
3841 mio_symbol_ref (&sym->cp_pointer);
3843 /* Load/save the f2k_derived namespace of a derived-type symbol. */
3844 mio_full_f2k_derived (sym);
3846 mio_namelist (sym);
3848 /* Add the fields that say whether this is from an intrinsic module,
3849 and if so, what symbol it is within the module. */
3850 /* mio_integer (&(sym->from_intmod)); */
3851 if (iomode == IO_OUTPUT)
3853 intmod = sym->from_intmod;
3854 mio_integer (&intmod);
3856 else
3858 mio_integer (&intmod);
3859 sym->from_intmod = (intmod_id) intmod;
3862 mio_integer (&(sym->intmod_sym_id));
3864 if (sym->attr.flavor == FL_DERIVED)
3865 mio_integer (&(sym->hash_value));
3867 mio_rparen ();
3871 /************************* Top level subroutines *************************/
3873 /* Given a root symtree node and a symbol, try to find a symtree that
3874 references the symbol that is not a unique name. */
3876 static gfc_symtree *
3877 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
3879 gfc_symtree *s = NULL;
3881 if (st == NULL)
3882 return s;
3884 s = find_symtree_for_symbol (st->right, sym);
3885 if (s != NULL)
3886 return s;
3887 s = find_symtree_for_symbol (st->left, sym);
3888 if (s != NULL)
3889 return s;
3891 if (st->n.sym == sym && !check_unique_name (st->name))
3892 return st;
3894 return s;
3898 /* A recursive function to look for a specific symbol by name and by
3899 module. Whilst several symtrees might point to one symbol, its
3900 is sufficient for the purposes here than one exist. Note that
3901 generic interfaces are distinguished as are symbols that have been
3902 renamed in another module. */
3903 static gfc_symtree *
3904 find_symbol (gfc_symtree *st, const char *name,
3905 const char *module, int generic)
3907 int c;
3908 gfc_symtree *retval, *s;
3910 if (st == NULL || st->n.sym == NULL)
3911 return NULL;
3913 c = strcmp (name, st->n.sym->name);
3914 if (c == 0 && st->n.sym->module
3915 && strcmp (module, st->n.sym->module) == 0
3916 && !check_unique_name (st->name))
3918 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
3920 /* Detect symbols that are renamed by use association in another
3921 module by the absence of a symtree and null attr.use_rename,
3922 since the latter is not transmitted in the module file. */
3923 if (((!generic && !st->n.sym->attr.generic)
3924 || (generic && st->n.sym->attr.generic))
3925 && !(s == NULL && !st->n.sym->attr.use_rename))
3926 return st;
3929 retval = find_symbol (st->left, name, module, generic);
3931 if (retval == NULL)
3932 retval = find_symbol (st->right, name, module, generic);
3934 return retval;
3938 /* Skip a list between balanced left and right parens.
3939 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
3940 have been already parsed by hand, and the remaining of the content is to be
3941 skipped here. The default value is 0 (balanced parens). */
3943 static void
3944 skip_list (int nest_level = 0)
3946 int level;
3948 level = nest_level;
3951 switch (parse_atom ())
3953 case ATOM_LPAREN:
3954 level++;
3955 break;
3957 case ATOM_RPAREN:
3958 level--;
3959 break;
3961 case ATOM_STRING:
3962 free (atom_string);
3963 break;
3965 case ATOM_NAME:
3966 case ATOM_INTEGER:
3967 break;
3970 while (level > 0);
3974 /* Load operator interfaces from the module. Interfaces are unusual
3975 in that they attach themselves to existing symbols. */
3977 static void
3978 load_operator_interfaces (void)
3980 const char *p;
3981 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
3982 gfc_user_op *uop;
3983 pointer_info *pi = NULL;
3984 int n, i;
3986 mio_lparen ();
3988 while (peek_atom () != ATOM_RPAREN)
3990 mio_lparen ();
3992 mio_internal_string (name);
3993 mio_internal_string (module);
3995 n = number_use_names (name, true);
3996 n = n ? n : 1;
3998 for (i = 1; i <= n; i++)
4000 /* Decide if we need to load this one or not. */
4001 p = find_use_name_n (name, &i, true);
4003 if (p == NULL)
4005 while (parse_atom () != ATOM_RPAREN);
4006 continue;
4009 if (i == 1)
4011 uop = gfc_get_uop (p);
4012 pi = mio_interface_rest (&uop->op);
4014 else
4016 if (gfc_find_uop (p, NULL))
4017 continue;
4018 uop = gfc_get_uop (p);
4019 uop->op = gfc_get_interface ();
4020 uop->op->where = gfc_current_locus;
4021 add_fixup (pi->integer, &uop->op->sym);
4026 mio_rparen ();
4030 /* Load interfaces from the module. Interfaces are unusual in that
4031 they attach themselves to existing symbols. */
4033 static void
4034 load_generic_interfaces (void)
4036 const char *p;
4037 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4038 gfc_symbol *sym;
4039 gfc_interface *generic = NULL, *gen = NULL;
4040 int n, i, renamed;
4041 bool ambiguous_set = false;
4043 mio_lparen ();
4045 while (peek_atom () != ATOM_RPAREN)
4047 mio_lparen ();
4049 mio_internal_string (name);
4050 mio_internal_string (module);
4052 n = number_use_names (name, false);
4053 renamed = n ? 1 : 0;
4054 n = n ? n : 1;
4056 for (i = 1; i <= n; i++)
4058 gfc_symtree *st;
4059 /* Decide if we need to load this one or not. */
4060 p = find_use_name_n (name, &i, false);
4062 st = find_symbol (gfc_current_ns->sym_root,
4063 name, module_name, 1);
4065 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4067 /* Skip the specific names for these cases. */
4068 while (i == 1 && parse_atom () != ATOM_RPAREN);
4070 continue;
4073 /* If the symbol exists already and is being USEd without being
4074 in an ONLY clause, do not load a new symtree(11.3.2). */
4075 if (!only_flag && st)
4076 sym = st->n.sym;
4078 if (!sym)
4080 if (st)
4082 sym = st->n.sym;
4083 if (strcmp (st->name, p) != 0)
4085 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4086 st->n.sym = sym;
4087 sym->refs++;
4091 /* Since we haven't found a valid generic interface, we had
4092 better make one. */
4093 if (!sym)
4095 gfc_get_symbol (p, NULL, &sym);
4096 sym->name = gfc_get_string (name);
4097 sym->module = module_name;
4098 sym->attr.flavor = FL_PROCEDURE;
4099 sym->attr.generic = 1;
4100 sym->attr.use_assoc = 1;
4103 else
4105 /* Unless sym is a generic interface, this reference
4106 is ambiguous. */
4107 if (st == NULL)
4108 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4110 sym = st->n.sym;
4112 if (st && !sym->attr.generic
4113 && !st->ambiguous
4114 && sym->module
4115 && strcmp (module, sym->module))
4117 ambiguous_set = true;
4118 st->ambiguous = 1;
4122 sym->attr.use_only = only_flag;
4123 sym->attr.use_rename = renamed;
4125 if (i == 1)
4127 mio_interface_rest (&sym->generic);
4128 generic = sym->generic;
4130 else if (!sym->generic)
4132 sym->generic = generic;
4133 sym->attr.generic_copy = 1;
4136 /* If a procedure that is not generic has generic interfaces
4137 that include itself, it is generic! We need to take care
4138 to retain symbols ambiguous that were already so. */
4139 if (sym->attr.use_assoc
4140 && !sym->attr.generic
4141 && sym->attr.flavor == FL_PROCEDURE)
4143 for (gen = generic; gen; gen = gen->next)
4145 if (gen->sym == sym)
4147 sym->attr.generic = 1;
4148 if (ambiguous_set)
4149 st->ambiguous = 0;
4150 break;
4158 mio_rparen ();
4162 /* Load common blocks. */
4164 static void
4165 load_commons (void)
4167 char name[GFC_MAX_SYMBOL_LEN + 1];
4168 gfc_common_head *p;
4170 mio_lparen ();
4172 while (peek_atom () != ATOM_RPAREN)
4174 int flags;
4175 char* label;
4176 mio_lparen ();
4177 mio_internal_string (name);
4179 p = gfc_get_common (name, 1);
4181 mio_symbol_ref (&p->head);
4182 mio_integer (&flags);
4183 if (flags & 1)
4184 p->saved = 1;
4185 if (flags & 2)
4186 p->threadprivate = 1;
4187 p->use_assoc = 1;
4189 /* Get whether this was a bind(c) common or not. */
4190 mio_integer (&p->is_bind_c);
4191 /* Get the binding label. */
4192 label = read_string ();
4193 if (strlen (label))
4194 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4195 XDELETEVEC (label);
4197 mio_rparen ();
4200 mio_rparen ();
4204 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4205 so that unused variables are not loaded and so that the expression can
4206 be safely freed. */
4208 static void
4209 load_equiv (void)
4211 gfc_equiv *head, *tail, *end, *eq;
4212 bool unused;
4214 mio_lparen ();
4215 in_load_equiv = true;
4217 end = gfc_current_ns->equiv;
4218 while (end != NULL && end->next != NULL)
4219 end = end->next;
4221 while (peek_atom () != ATOM_RPAREN) {
4222 mio_lparen ();
4223 head = tail = NULL;
4225 while(peek_atom () != ATOM_RPAREN)
4227 if (head == NULL)
4228 head = tail = gfc_get_equiv ();
4229 else
4231 tail->eq = gfc_get_equiv ();
4232 tail = tail->eq;
4235 mio_pool_string (&tail->module);
4236 mio_expr (&tail->expr);
4239 /* Unused equivalence members have a unique name. In addition, it
4240 must be checked that the symbols are from the same module. */
4241 unused = true;
4242 for (eq = head; eq; eq = eq->eq)
4244 if (eq->expr->symtree->n.sym->module
4245 && head->expr->symtree->n.sym->module
4246 && strcmp (head->expr->symtree->n.sym->module,
4247 eq->expr->symtree->n.sym->module) == 0
4248 && !check_unique_name (eq->expr->symtree->name))
4250 unused = false;
4251 break;
4255 if (unused)
4257 for (eq = head; eq; eq = head)
4259 head = eq->eq;
4260 gfc_free_expr (eq->expr);
4261 free (eq);
4265 if (end == NULL)
4266 gfc_current_ns->equiv = head;
4267 else
4268 end->next = head;
4270 if (head != NULL)
4271 end = head;
4273 mio_rparen ();
4276 mio_rparen ();
4277 in_load_equiv = false;
4281 /* This function loads the sym_root of f2k_derived with the extensions to
4282 the derived type. */
4283 static void
4284 load_derived_extensions (void)
4286 int symbol, j;
4287 gfc_symbol *derived;
4288 gfc_symbol *dt;
4289 gfc_symtree *st;
4290 pointer_info *info;
4291 char name[GFC_MAX_SYMBOL_LEN + 1];
4292 char module[GFC_MAX_SYMBOL_LEN + 1];
4293 const char *p;
4295 mio_lparen ();
4296 while (peek_atom () != ATOM_RPAREN)
4298 mio_lparen ();
4299 mio_integer (&symbol);
4300 info = get_integer (symbol);
4301 derived = info->u.rsym.sym;
4303 /* This one is not being loaded. */
4304 if (!info || !derived)
4306 while (peek_atom () != ATOM_RPAREN)
4307 skip_list ();
4308 continue;
4311 gcc_assert (derived->attr.flavor == FL_DERIVED);
4312 if (derived->f2k_derived == NULL)
4313 derived->f2k_derived = gfc_get_namespace (NULL, 0);
4315 while (peek_atom () != ATOM_RPAREN)
4317 mio_lparen ();
4318 mio_internal_string (name);
4319 mio_internal_string (module);
4321 /* Only use one use name to find the symbol. */
4322 j = 1;
4323 p = find_use_name_n (name, &j, false);
4324 if (p)
4326 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4327 dt = st->n.sym;
4328 st = gfc_find_symtree (derived->f2k_derived->sym_root, name);
4329 if (st == NULL)
4331 /* Only use the real name in f2k_derived to ensure a single
4332 symtree. */
4333 st = gfc_new_symtree (&derived->f2k_derived->sym_root, name);
4334 st->n.sym = dt;
4335 st->n.sym->refs++;
4338 mio_rparen ();
4340 mio_rparen ();
4342 mio_rparen ();
4346 /* Recursive function to traverse the pointer_info tree and load a
4347 needed symbol. We return nonzero if we load a symbol and stop the
4348 traversal, because the act of loading can alter the tree. */
4350 static int
4351 load_needed (pointer_info *p)
4353 gfc_namespace *ns;
4354 pointer_info *q;
4355 gfc_symbol *sym;
4356 int rv;
4358 rv = 0;
4359 if (p == NULL)
4360 return rv;
4362 rv |= load_needed (p->left);
4363 rv |= load_needed (p->right);
4365 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4366 return rv;
4368 p->u.rsym.state = USED;
4370 set_module_locus (&p->u.rsym.where);
4372 sym = p->u.rsym.sym;
4373 if (sym == NULL)
4375 q = get_integer (p->u.rsym.ns);
4377 ns = (gfc_namespace *) q->u.pointer;
4378 if (ns == NULL)
4380 /* Create an interface namespace if necessary. These are
4381 the namespaces that hold the formal parameters of module
4382 procedures. */
4384 ns = gfc_get_namespace (NULL, 0);
4385 associate_integer_pointer (q, ns);
4388 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4389 doesn't go pear-shaped if the symbol is used. */
4390 if (!ns->proc_name)
4391 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4392 1, &ns->proc_name);
4394 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4395 sym->name = dt_lower_string (p->u.rsym.true_name);
4396 sym->module = gfc_get_string (p->u.rsym.module);
4397 if (p->u.rsym.binding_label)
4398 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4399 (p->u.rsym.binding_label));
4401 associate_integer_pointer (p, sym);
4404 mio_symbol (sym);
4405 sym->attr.use_assoc = 1;
4407 /* Mark as only or rename for later diagnosis for explicitly imported
4408 but not used warnings; don't mark internal symbols such as __vtab,
4409 __def_init etc. Only mark them if they have been explicitly loaded. */
4411 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4413 gfc_use_rename *u;
4415 /* Search the use/rename list for the variable; if the variable is
4416 found, mark it. */
4417 for (u = gfc_rename_list; u; u = u->next)
4419 if (strcmp (u->use_name, sym->name) == 0)
4421 sym->attr.use_only = 1;
4422 break;
4427 if (p->u.rsym.renamed)
4428 sym->attr.use_rename = 1;
4430 return 1;
4434 /* Recursive function for cleaning up things after a module has been read. */
4436 static void
4437 read_cleanup (pointer_info *p)
4439 gfc_symtree *st;
4440 pointer_info *q;
4442 if (p == NULL)
4443 return;
4445 read_cleanup (p->left);
4446 read_cleanup (p->right);
4448 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4450 gfc_namespace *ns;
4451 /* Add hidden symbols to the symtree. */
4452 q = get_integer (p->u.rsym.ns);
4453 ns = (gfc_namespace *) q->u.pointer;
4455 if (!p->u.rsym.sym->attr.vtype
4456 && !p->u.rsym.sym->attr.vtab)
4457 st = gfc_get_unique_symtree (ns);
4458 else
4460 /* There is no reason to use 'unique_symtrees' for vtabs or
4461 vtypes - their name is fine for a symtree and reduces the
4462 namespace pollution. */
4463 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4464 if (!st)
4465 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4468 st->n.sym = p->u.rsym.sym;
4469 st->n.sym->refs++;
4471 /* Fixup any symtree references. */
4472 p->u.rsym.symtree = st;
4473 resolve_fixups (p->u.rsym.stfixup, st);
4474 p->u.rsym.stfixup = NULL;
4477 /* Free unused symbols. */
4478 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4479 gfc_free_symbol (p->u.rsym.sym);
4483 /* It is not quite enough to check for ambiguity in the symbols by
4484 the loaded symbol and the new symbol not being identical. */
4485 static bool
4486 check_for_ambiguous (gfc_symbol *st_sym, pointer_info *info)
4488 gfc_symbol *rsym;
4489 module_locus locus;
4490 symbol_attribute attr;
4492 if (gfc_current_ns->proc_name && st_sym->name == gfc_current_ns->proc_name->name)
4494 gfc_error ("'%s' of module '%s', imported at %C, is also the name of the "
4495 "current program unit", st_sym->name, module_name);
4496 return true;
4499 rsym = info->u.rsym.sym;
4500 if (st_sym == rsym)
4501 return false;
4503 if (st_sym->attr.vtab || st_sym->attr.vtype)
4504 return false;
4506 /* If the existing symbol is generic from a different module and
4507 the new symbol is generic there can be no ambiguity. */
4508 if (st_sym->attr.generic
4509 && st_sym->module
4510 && st_sym->module != module_name)
4512 /* The new symbol's attributes have not yet been read. Since
4513 we need attr.generic, read it directly. */
4514 get_module_locus (&locus);
4515 set_module_locus (&info->u.rsym.where);
4516 mio_lparen ();
4517 attr.generic = 0;
4518 mio_symbol_attribute (&attr);
4519 set_module_locus (&locus);
4520 if (attr.generic)
4521 return false;
4524 return true;
4528 /* Read a module file. */
4530 static void
4531 read_module (void)
4533 module_locus operator_interfaces, user_operators, extensions;
4534 const char *p;
4535 char name[GFC_MAX_SYMBOL_LEN + 1];
4536 int i;
4537 int ambiguous, j, nuse, symbol;
4538 pointer_info *info, *q;
4539 gfc_use_rename *u = NULL;
4540 gfc_symtree *st;
4541 gfc_symbol *sym;
4543 get_module_locus (&operator_interfaces); /* Skip these for now. */
4544 skip_list ();
4546 get_module_locus (&user_operators);
4547 skip_list ();
4548 skip_list ();
4550 /* Skip commons, equivalences and derived type extensions for now. */
4551 skip_list ();
4552 skip_list ();
4554 get_module_locus (&extensions);
4555 skip_list ();
4557 mio_lparen ();
4559 /* Create the fixup nodes for all the symbols. */
4561 while (peek_atom () != ATOM_RPAREN)
4563 char* bind_label;
4564 require_atom (ATOM_INTEGER);
4565 info = get_integer (atom_int);
4567 info->type = P_SYMBOL;
4568 info->u.rsym.state = UNUSED;
4570 info->u.rsym.true_name = read_string ();
4571 info->u.rsym.module = read_string ();
4572 bind_label = read_string ();
4573 if (strlen (bind_label))
4574 info->u.rsym.binding_label = bind_label;
4575 else
4576 XDELETEVEC (bind_label);
4578 require_atom (ATOM_INTEGER);
4579 info->u.rsym.ns = atom_int;
4581 get_module_locus (&info->u.rsym.where);
4583 /* See if the symbol has already been loaded by a previous module.
4584 If so, we reference the existing symbol and prevent it from
4585 being loaded again. This should not happen if the symbol being
4586 read is an index for an assumed shape dummy array (ns != 1). */
4588 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
4590 if (sym == NULL
4591 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
4593 skip_list ();
4594 continue;
4597 info->u.rsym.state = USED;
4598 info->u.rsym.sym = sym;
4599 /* The current symbol has already been loaded, so we can avoid loading
4600 it again. However, if it is a derived type, some of its components
4601 can be used in expressions in the module. To avoid the module loading
4602 failing, we need to associate the module's component pointer indexes
4603 with the existing symbol's component pointers. */
4604 if (sym->attr.flavor == FL_DERIVED)
4606 gfc_component *c;
4608 /* First seek to the symbol's component list. */
4609 mio_lparen (); /* symbol opening. */
4610 skip_list (); /* skip symbol attribute. */
4612 mio_lparen (); /* component list opening. */
4613 for (c = sym->components; c; c = c->next)
4615 pointer_info *p;
4616 const char *comp_name;
4617 int n;
4619 mio_lparen (); /* component opening. */
4620 mio_integer (&n);
4621 p = get_integer (n);
4622 if (p->u.pointer == NULL)
4623 associate_integer_pointer (p, c);
4624 mio_pool_string (&comp_name);
4625 gcc_assert (comp_name == c->name);
4626 skip_list (1); /* component end. */
4628 mio_rparen (); /* component list closing. */
4630 skip_list (1); /* symbol end. */
4632 else
4633 skip_list ();
4635 /* Some symbols do not have a namespace (eg. formal arguments),
4636 so the automatic "unique symtree" mechanism must be suppressed
4637 by marking them as referenced. */
4638 q = get_integer (info->u.rsym.ns);
4639 if (q->u.pointer == NULL)
4641 info->u.rsym.referenced = 1;
4642 continue;
4645 /* If possible recycle the symtree that references the symbol.
4646 If a symtree is not found and the module does not import one,
4647 a unique-name symtree is found by read_cleanup. */
4648 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
4649 if (st != NULL)
4651 info->u.rsym.symtree = st;
4652 info->u.rsym.referenced = 1;
4656 mio_rparen ();
4658 /* Parse the symtree lists. This lets us mark which symbols need to
4659 be loaded. Renaming is also done at this point by replacing the
4660 symtree name. */
4662 mio_lparen ();
4664 while (peek_atom () != ATOM_RPAREN)
4666 mio_internal_string (name);
4667 mio_integer (&ambiguous);
4668 mio_integer (&symbol);
4670 info = get_integer (symbol);
4672 /* See how many use names there are. If none, go through the start
4673 of the loop at least once. */
4674 nuse = number_use_names (name, false);
4675 info->u.rsym.renamed = nuse ? 1 : 0;
4677 if (nuse == 0)
4678 nuse = 1;
4680 for (j = 1; j <= nuse; j++)
4682 /* Get the jth local name for this symbol. */
4683 p = find_use_name_n (name, &j, false);
4685 if (p == NULL && strcmp (name, module_name) == 0)
4686 p = name;
4688 /* Exception: Always import vtabs & vtypes. */
4689 if (p == NULL && name[0] == '_'
4690 && (strncmp (name, "__vtab_", 5) == 0
4691 || strncmp (name, "__vtype_", 6) == 0))
4692 p = name;
4694 /* Skip symtree nodes not in an ONLY clause, unless there
4695 is an existing symtree loaded from another USE statement. */
4696 if (p == NULL)
4698 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4699 if (st != NULL
4700 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
4701 && st->n.sym->module != NULL
4702 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
4704 info->u.rsym.symtree = st;
4705 info->u.rsym.sym = st->n.sym;
4707 continue;
4710 /* If a symbol of the same name and module exists already,
4711 this symbol, which is not in an ONLY clause, must not be
4712 added to the namespace(11.3.2). Note that find_symbol
4713 only returns the first occurrence that it finds. */
4714 if (!only_flag && !info->u.rsym.renamed
4715 && strcmp (name, module_name) != 0
4716 && find_symbol (gfc_current_ns->sym_root, name,
4717 module_name, 0))
4718 continue;
4720 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4722 if (st != NULL)
4724 /* Check for ambiguous symbols. */
4725 if (check_for_ambiguous (st->n.sym, info))
4726 st->ambiguous = 1;
4727 else
4728 info->u.rsym.symtree = st;
4730 else
4732 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4734 /* Create a symtree node in the current namespace for this
4735 symbol. */
4736 st = check_unique_name (p)
4737 ? gfc_get_unique_symtree (gfc_current_ns)
4738 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
4739 st->ambiguous = ambiguous;
4741 sym = info->u.rsym.sym;
4743 /* Create a symbol node if it doesn't already exist. */
4744 if (sym == NULL)
4746 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
4747 gfc_current_ns);
4748 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
4749 sym = info->u.rsym.sym;
4750 sym->module = gfc_get_string (info->u.rsym.module);
4752 if (info->u.rsym.binding_label)
4753 sym->binding_label =
4754 IDENTIFIER_POINTER (get_identifier
4755 (info->u.rsym.binding_label));
4758 st->n.sym = sym;
4759 st->n.sym->refs++;
4761 if (strcmp (name, p) != 0)
4762 sym->attr.use_rename = 1;
4764 if (name[0] != '_'
4765 || (strncmp (name, "__vtab_", 5) != 0
4766 && strncmp (name, "__vtype_", 6) != 0))
4767 sym->attr.use_only = only_flag;
4769 /* Store the symtree pointing to this symbol. */
4770 info->u.rsym.symtree = st;
4772 if (info->u.rsym.state == UNUSED)
4773 info->u.rsym.state = NEEDED;
4774 info->u.rsym.referenced = 1;
4779 mio_rparen ();
4781 /* Load intrinsic operator interfaces. */
4782 set_module_locus (&operator_interfaces);
4783 mio_lparen ();
4785 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
4787 if (i == INTRINSIC_USER)
4788 continue;
4790 if (only_flag)
4792 u = find_use_operator ((gfc_intrinsic_op) i);
4794 if (u == NULL)
4796 skip_list ();
4797 continue;
4800 u->found = 1;
4803 mio_interface (&gfc_current_ns->op[i]);
4804 if (u && !gfc_current_ns->op[i])
4805 u->found = 0;
4808 mio_rparen ();
4810 /* Load generic and user operator interfaces. These must follow the
4811 loading of symtree because otherwise symbols can be marked as
4812 ambiguous. */
4814 set_module_locus (&user_operators);
4816 load_operator_interfaces ();
4817 load_generic_interfaces ();
4819 load_commons ();
4820 load_equiv ();
4822 /* At this point, we read those symbols that are needed but haven't
4823 been loaded yet. If one symbol requires another, the other gets
4824 marked as NEEDED if its previous state was UNUSED. */
4826 while (load_needed (pi_root));
4828 /* Make sure all elements of the rename-list were found in the module. */
4830 for (u = gfc_rename_list; u; u = u->next)
4832 if (u->found)
4833 continue;
4835 if (u->op == INTRINSIC_NONE)
4837 gfc_error ("Symbol '%s' referenced at %L not found in module '%s'",
4838 u->use_name, &u->where, module_name);
4839 continue;
4842 if (u->op == INTRINSIC_USER)
4844 gfc_error ("User operator '%s' referenced at %L not found "
4845 "in module '%s'", u->use_name, &u->where, module_name);
4846 continue;
4849 gfc_error ("Intrinsic operator '%s' referenced at %L not found "
4850 "in module '%s'", gfc_op2string (u->op), &u->where,
4851 module_name);
4854 /* Now we should be in a position to fill f2k_derived with derived type
4855 extensions, since everything has been loaded. */
4856 set_module_locus (&extensions);
4857 load_derived_extensions ();
4859 /* Clean up symbol nodes that were never loaded, create references
4860 to hidden symbols. */
4862 read_cleanup (pi_root);
4866 /* Given an access type that is specific to an entity and the default
4867 access, return nonzero if the entity is publicly accessible. If the
4868 element is declared as PUBLIC, then it is public; if declared
4869 PRIVATE, then private, and otherwise it is public unless the default
4870 access in this context has been declared PRIVATE. */
4872 static bool
4873 check_access (gfc_access specific_access, gfc_access default_access)
4875 if (specific_access == ACCESS_PUBLIC)
4876 return TRUE;
4877 if (specific_access == ACCESS_PRIVATE)
4878 return FALSE;
4880 if (gfc_option.flag_module_private)
4881 return default_access == ACCESS_PUBLIC;
4882 else
4883 return default_access != ACCESS_PRIVATE;
4887 bool
4888 gfc_check_symbol_access (gfc_symbol *sym)
4890 if (sym->attr.vtab || sym->attr.vtype)
4891 return true;
4892 else
4893 return check_access (sym->attr.access, sym->ns->default_access);
4897 /* A structure to remember which commons we've already written. */
4899 struct written_common
4901 BBT_HEADER(written_common);
4902 const char *name, *label;
4905 static struct written_common *written_commons = NULL;
4907 /* Comparison function used for balancing the binary tree. */
4909 static int
4910 compare_written_commons (void *a1, void *b1)
4912 const char *aname = ((struct written_common *) a1)->name;
4913 const char *alabel = ((struct written_common *) a1)->label;
4914 const char *bname = ((struct written_common *) b1)->name;
4915 const char *blabel = ((struct written_common *) b1)->label;
4916 int c = strcmp (aname, bname);
4918 return (c != 0 ? c : strcmp (alabel, blabel));
4921 /* Free a list of written commons. */
4923 static void
4924 free_written_common (struct written_common *w)
4926 if (!w)
4927 return;
4929 if (w->left)
4930 free_written_common (w->left);
4931 if (w->right)
4932 free_written_common (w->right);
4934 free (w);
4937 /* Write a common block to the module -- recursive helper function. */
4939 static void
4940 write_common_0 (gfc_symtree *st, bool this_module)
4942 gfc_common_head *p;
4943 const char * name;
4944 int flags;
4945 const char *label;
4946 struct written_common *w;
4947 bool write_me = true;
4949 if (st == NULL)
4950 return;
4952 write_common_0 (st->left, this_module);
4954 /* We will write out the binding label, or "" if no label given. */
4955 name = st->n.common->name;
4956 p = st->n.common;
4957 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
4959 /* Check if we've already output this common. */
4960 w = written_commons;
4961 while (w)
4963 int c = strcmp (name, w->name);
4964 c = (c != 0 ? c : strcmp (label, w->label));
4965 if (c == 0)
4966 write_me = false;
4968 w = (c < 0) ? w->left : w->right;
4971 if (this_module && p->use_assoc)
4972 write_me = false;
4974 if (write_me)
4976 /* Write the common to the module. */
4977 mio_lparen ();
4978 mio_pool_string (&name);
4980 mio_symbol_ref (&p->head);
4981 flags = p->saved ? 1 : 0;
4982 if (p->threadprivate)
4983 flags |= 2;
4984 mio_integer (&flags);
4986 /* Write out whether the common block is bind(c) or not. */
4987 mio_integer (&(p->is_bind_c));
4989 mio_pool_string (&label);
4990 mio_rparen ();
4992 /* Record that we have written this common. */
4993 w = XCNEW (struct written_common);
4994 w->name = p->name;
4995 w->label = label;
4996 gfc_insert_bbt (&written_commons, w, compare_written_commons);
4999 write_common_0 (st->right, this_module);
5003 /* Write a common, by initializing the list of written commons, calling
5004 the recursive function write_common_0() and cleaning up afterwards. */
5006 static void
5007 write_common (gfc_symtree *st)
5009 written_commons = NULL;
5010 write_common_0 (st, true);
5011 write_common_0 (st, false);
5012 free_written_common (written_commons);
5013 written_commons = NULL;
5017 /* Write the blank common block to the module. */
5019 static void
5020 write_blank_common (void)
5022 const char * name = BLANK_COMMON_NAME;
5023 int saved;
5024 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5025 this, but it hasn't been checked. Just making it so for now. */
5026 int is_bind_c = 0;
5028 if (gfc_current_ns->blank_common.head == NULL)
5029 return;
5031 mio_lparen ();
5033 mio_pool_string (&name);
5035 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5036 saved = gfc_current_ns->blank_common.saved;
5037 mio_integer (&saved);
5039 /* Write out whether the common block is bind(c) or not. */
5040 mio_integer (&is_bind_c);
5042 /* Write out an empty binding label. */
5043 write_atom (ATOM_STRING, "");
5045 mio_rparen ();
5049 /* Write equivalences to the module. */
5051 static void
5052 write_equiv (void)
5054 gfc_equiv *eq, *e;
5055 int num;
5057 num = 0;
5058 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5060 mio_lparen ();
5062 for (e = eq; e; e = e->eq)
5064 if (e->module == NULL)
5065 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5066 mio_allocated_string (e->module);
5067 mio_expr (&e->expr);
5070 num++;
5071 mio_rparen ();
5076 /* Write derived type extensions to the module. */
5078 static void
5079 write_dt_extensions (gfc_symtree *st)
5081 if (!gfc_check_symbol_access (st->n.sym))
5082 return;
5083 if (!(st->n.sym->ns && st->n.sym->ns->proc_name
5084 && st->n.sym->ns->proc_name->attr.flavor == FL_MODULE))
5085 return;
5087 mio_lparen ();
5088 mio_pool_string (&st->name);
5089 if (st->n.sym->module != NULL)
5090 mio_pool_string (&st->n.sym->module);
5091 else
5093 char name[GFC_MAX_SYMBOL_LEN + 1];
5094 if (iomode == IO_OUTPUT)
5095 strcpy (name, module_name);
5096 mio_internal_string (name);
5097 if (iomode == IO_INPUT)
5098 module_name = gfc_get_string (name);
5100 mio_rparen ();
5103 static void
5104 write_derived_extensions (gfc_symtree *st)
5106 if (!((st->n.sym->attr.flavor == FL_DERIVED)
5107 && (st->n.sym->f2k_derived != NULL)
5108 && (st->n.sym->f2k_derived->sym_root != NULL)))
5109 return;
5111 mio_lparen ();
5112 mio_symbol_ref (&(st->n.sym));
5113 gfc_traverse_symtree (st->n.sym->f2k_derived->sym_root,
5114 write_dt_extensions);
5115 mio_rparen ();
5119 /* Write a symbol to the module. */
5121 static void
5122 write_symbol (int n, gfc_symbol *sym)
5124 const char *label;
5126 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5127 gfc_internal_error ("write_symbol(): bad module symbol '%s'", sym->name);
5129 mio_integer (&n);
5131 if (sym->attr.flavor == FL_DERIVED)
5133 const char *name;
5134 name = dt_upper_string (sym->name);
5135 mio_pool_string (&name);
5137 else
5138 mio_pool_string (&sym->name);
5140 mio_pool_string (&sym->module);
5141 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5143 label = sym->binding_label;
5144 mio_pool_string (&label);
5146 else
5147 write_atom (ATOM_STRING, "");
5149 mio_pointer_ref (&sym->ns);
5151 mio_symbol (sym);
5152 write_char ('\n');
5156 /* Recursive traversal function to write the initial set of symbols to
5157 the module. We check to see if the symbol should be written
5158 according to the access specification. */
5160 static void
5161 write_symbol0 (gfc_symtree *st)
5163 gfc_symbol *sym;
5164 pointer_info *p;
5165 bool dont_write = false;
5167 if (st == NULL)
5168 return;
5170 write_symbol0 (st->left);
5172 sym = st->n.sym;
5173 if (sym->module == NULL)
5174 sym->module = module_name;
5176 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5177 && !sym->attr.subroutine && !sym->attr.function)
5178 dont_write = true;
5180 if (!gfc_check_symbol_access (sym))
5181 dont_write = true;
5183 if (!dont_write)
5185 p = get_pointer (sym);
5186 if (p->type == P_UNKNOWN)
5187 p->type = P_SYMBOL;
5189 if (p->u.wsym.state != WRITTEN)
5191 write_symbol (p->integer, sym);
5192 p->u.wsym.state = WRITTEN;
5196 write_symbol0 (st->right);
5200 /* Type for the temporary tree used when writing secondary symbols. */
5202 struct sorted_pointer_info
5204 BBT_HEADER (sorted_pointer_info);
5206 pointer_info *p;
5209 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5211 /* Recursively traverse the temporary tree, free its contents. */
5213 static void
5214 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5216 if (!p)
5217 return;
5219 free_sorted_pointer_info_tree (p->left);
5220 free_sorted_pointer_info_tree (p->right);
5222 free (p);
5225 /* Comparison function for the temporary tree. */
5227 static int
5228 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5230 sorted_pointer_info *spi1, *spi2;
5231 spi1 = (sorted_pointer_info *)_spi1;
5232 spi2 = (sorted_pointer_info *)_spi2;
5234 if (spi1->p->integer < spi2->p->integer)
5235 return -1;
5236 if (spi1->p->integer > spi2->p->integer)
5237 return 1;
5238 return 0;
5242 /* Finds the symbols that need to be written and collects them in the
5243 sorted_pi tree so that they can be traversed in an order
5244 independent of memory addresses. */
5246 static void
5247 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5249 if (!p)
5250 return;
5252 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5254 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5255 sp->p = p;
5257 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5260 find_symbols_to_write (tree, p->left);
5261 find_symbols_to_write (tree, p->right);
5265 /* Recursive function that traverses the tree of symbols that need to be
5266 written and writes them in order. */
5268 static void
5269 write_symbol1_recursion (sorted_pointer_info *sp)
5271 if (!sp)
5272 return;
5274 write_symbol1_recursion (sp->left);
5276 pointer_info *p1 = sp->p;
5277 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5279 p1->u.wsym.state = WRITTEN;
5280 write_symbol (p1->integer, p1->u.wsym.sym);
5281 p1->u.wsym.sym->attr.public_used = 1;
5283 write_symbol1_recursion (sp->right);
5287 /* Write the secondary set of symbols to the module file. These are
5288 symbols that were not public yet are needed by the public symbols
5289 or another dependent symbol. The act of writing a symbol can add
5290 symbols to the pointer_info tree, so we return nonzero if a symbol
5291 was written and pass that information upwards. The caller will
5292 then call this function again until nothing was written. It uses
5293 the utility functions and a temporary tree to ensure a reproducible
5294 ordering of the symbol output and thus the module file. */
5296 static int
5297 write_symbol1 (pointer_info *p)
5299 if (!p)
5300 return 0;
5302 /* Put symbols that need to be written into a tree sorted on the
5303 integer field. */
5305 sorted_pointer_info *spi_root = NULL;
5306 find_symbols_to_write (&spi_root, p);
5308 /* No symbols to write, return. */
5309 if (!spi_root)
5310 return 0;
5312 /* Otherwise, write and free the tree again. */
5313 write_symbol1_recursion (spi_root);
5314 free_sorted_pointer_info_tree (spi_root);
5316 return 1;
5320 /* Write operator interfaces associated with a symbol. */
5322 static void
5323 write_operator (gfc_user_op *uop)
5325 static char nullstring[] = "";
5326 const char *p = nullstring;
5328 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5329 return;
5331 mio_symbol_interface (&uop->name, &p, &uop->op);
5335 /* Write generic interfaces from the namespace sym_root. */
5337 static void
5338 write_generic (gfc_symtree *st)
5340 gfc_symbol *sym;
5342 if (st == NULL)
5343 return;
5345 write_generic (st->left);
5347 sym = st->n.sym;
5348 if (sym && !check_unique_name (st->name)
5349 && sym->generic && gfc_check_symbol_access (sym))
5351 if (!sym->module)
5352 sym->module = module_name;
5354 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5357 write_generic (st->right);
5361 static void
5362 write_symtree (gfc_symtree *st)
5364 gfc_symbol *sym;
5365 pointer_info *p;
5367 sym = st->n.sym;
5369 /* A symbol in an interface body must not be visible in the
5370 module file. */
5371 if (sym->ns != gfc_current_ns
5372 && sym->ns->proc_name
5373 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5374 return;
5376 if (!gfc_check_symbol_access (sym)
5377 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5378 && !sym->attr.subroutine && !sym->attr.function))
5379 return;
5381 if (check_unique_name (st->name))
5382 return;
5384 p = find_pointer (sym);
5385 if (p == NULL)
5386 gfc_internal_error ("write_symtree(): Symbol not written");
5388 mio_pool_string (&st->name);
5389 mio_integer (&st->ambiguous);
5390 mio_integer (&p->integer);
5394 static void
5395 write_module (void)
5397 int i;
5399 /* Write the operator interfaces. */
5400 mio_lparen ();
5402 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5404 if (i == INTRINSIC_USER)
5405 continue;
5407 mio_interface (check_access (gfc_current_ns->operator_access[i],
5408 gfc_current_ns->default_access)
5409 ? &gfc_current_ns->op[i] : NULL);
5412 mio_rparen ();
5413 write_char ('\n');
5414 write_char ('\n');
5416 mio_lparen ();
5417 gfc_traverse_user_op (gfc_current_ns, write_operator);
5418 mio_rparen ();
5419 write_char ('\n');
5420 write_char ('\n');
5422 mio_lparen ();
5423 write_generic (gfc_current_ns->sym_root);
5424 mio_rparen ();
5425 write_char ('\n');
5426 write_char ('\n');
5428 mio_lparen ();
5429 write_blank_common ();
5430 write_common (gfc_current_ns->common_root);
5431 mio_rparen ();
5432 write_char ('\n');
5433 write_char ('\n');
5435 mio_lparen ();
5436 write_equiv ();
5437 mio_rparen ();
5438 write_char ('\n');
5439 write_char ('\n');
5441 mio_lparen ();
5442 gfc_traverse_symtree (gfc_current_ns->sym_root,
5443 write_derived_extensions);
5444 mio_rparen ();
5445 write_char ('\n');
5446 write_char ('\n');
5448 /* Write symbol information. First we traverse all symbols in the
5449 primary namespace, writing those that need to be written.
5450 Sometimes writing one symbol will cause another to need to be
5451 written. A list of these symbols ends up on the write stack, and
5452 we end by popping the bottom of the stack and writing the symbol
5453 until the stack is empty. */
5455 mio_lparen ();
5457 write_symbol0 (gfc_current_ns->sym_root);
5458 while (write_symbol1 (pi_root))
5459 /* Nothing. */;
5461 mio_rparen ();
5463 write_char ('\n');
5464 write_char ('\n');
5466 mio_lparen ();
5467 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5468 mio_rparen ();
5472 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5473 true on success, false on failure. */
5475 static bool
5476 read_crc32_from_module_file (const char* filename, uLong* crc)
5478 FILE *file;
5479 char buf[4];
5480 unsigned int val;
5482 /* Open the file in binary mode. */
5483 if ((file = fopen (filename, "rb")) == NULL)
5484 return false;
5486 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5487 file. See RFC 1952. */
5488 if (fseek (file, -8, SEEK_END) != 0)
5490 fclose (file);
5491 return false;
5494 /* Read the CRC32. */
5495 if (fread (buf, 1, 4, file) != 4)
5497 fclose (file);
5498 return false;
5501 /* Close the file. */
5502 fclose (file);
5504 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5505 + ((buf[3] & 0xFF) << 24);
5506 *crc = val;
5508 /* For debugging, the CRC value printed in hexadecimal should match
5509 the CRC printed by "zcat -l -v filename".
5510 printf("CRC of file %s is %x\n", filename, val); */
5512 return true;
5516 /* Given module, dump it to disk. If there was an error while
5517 processing the module, dump_flag will be set to zero and we delete
5518 the module file, even if it was already there. */
5520 void
5521 gfc_dump_module (const char *name, int dump_flag)
5523 int n;
5524 char *filename, *filename_tmp;
5525 uLong crc, crc_old;
5527 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5528 if (gfc_option.module_dir != NULL)
5530 n += strlen (gfc_option.module_dir);
5531 filename = (char *) alloca (n);
5532 strcpy (filename, gfc_option.module_dir);
5533 strcat (filename, name);
5535 else
5537 filename = (char *) alloca (n);
5538 strcpy (filename, name);
5540 strcat (filename, MODULE_EXTENSION);
5542 /* Name of the temporary file used to write the module. */
5543 filename_tmp = (char *) alloca (n + 1);
5544 strcpy (filename_tmp, filename);
5545 strcat (filename_tmp, "0");
5547 /* There was an error while processing the module. We delete the
5548 module file, even if it was already there. */
5549 if (!dump_flag)
5551 unlink (filename);
5552 return;
5555 if (gfc_cpp_makedep ())
5556 gfc_cpp_add_target (filename);
5558 /* Write the module to the temporary file. */
5559 module_fp = gzopen (filename_tmp, "w");
5560 if (module_fp == NULL)
5561 gfc_fatal_error ("Can't open module file '%s' for writing at %C: %s",
5562 filename_tmp, xstrerror (errno));
5564 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
5565 MOD_VERSION, gfc_source_file);
5567 /* Write the module itself. */
5568 iomode = IO_OUTPUT;
5569 module_name = gfc_get_string (name);
5571 init_pi_tree ();
5573 write_module ();
5575 free_pi_tree (pi_root);
5576 pi_root = NULL;
5578 write_char ('\n');
5580 if (gzclose (module_fp))
5581 gfc_fatal_error ("Error writing module file '%s' for writing: %s",
5582 filename_tmp, xstrerror (errno));
5584 /* Read the CRC32 from the gzip trailers of the module files and
5585 compare. */
5586 if (!read_crc32_from_module_file (filename_tmp, &crc)
5587 || !read_crc32_from_module_file (filename, &crc_old)
5588 || crc_old != crc)
5590 /* Module file have changed, replace the old one. */
5591 if (rename (filename_tmp, filename))
5592 gfc_fatal_error ("Can't rename module file '%s' to '%s': %s",
5593 filename_tmp, filename, xstrerror (errno));
5595 else
5597 if (unlink (filename_tmp))
5598 gfc_fatal_error ("Can't delete temporary module file '%s': %s",
5599 filename_tmp, xstrerror (errno));
5604 static void
5605 create_intrinsic_function (const char *name, int id,
5606 const char *modname, intmod_id module,
5607 bool subroutine, gfc_symbol *result_type)
5609 gfc_intrinsic_sym *isym;
5610 gfc_symtree *tmp_symtree;
5611 gfc_symbol *sym;
5613 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5614 if (tmp_symtree)
5616 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5617 return;
5618 gfc_error ("Symbol '%s' already declared", name);
5621 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5622 sym = tmp_symtree->n.sym;
5624 if (subroutine)
5626 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5627 isym = gfc_intrinsic_subroutine_by_id (isym_id);
5628 sym->attr.subroutine = 1;
5630 else
5632 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5633 isym = gfc_intrinsic_function_by_id (isym_id);
5635 sym->attr.function = 1;
5636 if (result_type)
5638 sym->ts.type = BT_DERIVED;
5639 sym->ts.u.derived = result_type;
5640 sym->ts.is_c_interop = 1;
5641 isym->ts.f90_type = BT_VOID;
5642 isym->ts.type = BT_DERIVED;
5643 isym->ts.f90_type = BT_VOID;
5644 isym->ts.u.derived = result_type;
5645 isym->ts.is_c_interop = 1;
5648 gcc_assert (isym);
5650 sym->attr.flavor = FL_PROCEDURE;
5651 sym->attr.intrinsic = 1;
5653 sym->module = gfc_get_string (modname);
5654 sym->attr.use_assoc = 1;
5655 sym->from_intmod = module;
5656 sym->intmod_sym_id = id;
5660 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
5661 the current namespace for all named constants, pointer types, and
5662 procedures in the module unless the only clause was used or a rename
5663 list was provided. */
5665 static void
5666 import_iso_c_binding_module (void)
5668 gfc_symbol *mod_sym = NULL, *return_type;
5669 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
5670 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
5671 const char *iso_c_module_name = "__iso_c_binding";
5672 gfc_use_rename *u;
5673 int i;
5674 bool want_c_ptr = false, want_c_funptr = false;
5676 /* Look only in the current namespace. */
5677 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
5679 if (mod_symtree == NULL)
5681 /* symtree doesn't already exist in current namespace. */
5682 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
5683 false);
5685 if (mod_symtree != NULL)
5686 mod_sym = mod_symtree->n.sym;
5687 else
5688 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
5689 "create symbol for %s", iso_c_module_name);
5691 mod_sym->attr.flavor = FL_MODULE;
5692 mod_sym->attr.intrinsic = 1;
5693 mod_sym->module = gfc_get_string (iso_c_module_name);
5694 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
5697 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
5698 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
5699 need C_(FUN)PTR. */
5700 for (u = gfc_rename_list; u; u = u->next)
5702 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
5703 u->use_name) == 0)
5704 want_c_ptr = true;
5705 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
5706 u->use_name) == 0)
5707 want_c_ptr = true;
5708 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
5709 u->use_name) == 0)
5710 want_c_funptr = true;
5711 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
5712 u->use_name) == 0)
5713 want_c_funptr = true;
5714 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
5715 u->use_name) == 0)
5717 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5718 (iso_c_binding_symbol)
5719 ISOCBINDING_PTR,
5720 u->local_name[0] ? u->local_name
5721 : u->use_name,
5722 NULL, false);
5724 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
5725 u->use_name) == 0)
5727 c_funptr
5728 = generate_isocbinding_symbol (iso_c_module_name,
5729 (iso_c_binding_symbol)
5730 ISOCBINDING_FUNPTR,
5731 u->local_name[0] ? u->local_name
5732 : u->use_name,
5733 NULL, false);
5737 if ((want_c_ptr || !only_flag) && !c_ptr)
5738 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5739 (iso_c_binding_symbol)
5740 ISOCBINDING_PTR,
5741 NULL, NULL, only_flag);
5742 if ((want_c_funptr || !only_flag) && !c_funptr)
5743 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
5744 (iso_c_binding_symbol)
5745 ISOCBINDING_FUNPTR,
5746 NULL, NULL, only_flag);
5748 /* Generate the symbols for the named constants representing
5749 the kinds for intrinsic data types. */
5750 for (i = 0; i < ISOCBINDING_NUMBER; i++)
5752 bool found = false;
5753 for (u = gfc_rename_list; u; u = u->next)
5754 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
5756 bool not_in_std;
5757 const char *name;
5758 u->found = 1;
5759 found = true;
5761 switch (i)
5763 #define NAMED_FUNCTION(a,b,c,d) \
5764 case a: \
5765 not_in_std = (gfc_option.allow_std & d) == 0; \
5766 name = b; \
5767 break;
5768 #define NAMED_SUBROUTINE(a,b,c,d) \
5769 case a: \
5770 not_in_std = (gfc_option.allow_std & d) == 0; \
5771 name = b; \
5772 break;
5773 #define NAMED_INTCST(a,b,c,d) \
5774 case a: \
5775 not_in_std = (gfc_option.allow_std & d) == 0; \
5776 name = b; \
5777 break;
5778 #define NAMED_REALCST(a,b,c,d) \
5779 case a: \
5780 not_in_std = (gfc_option.allow_std & d) == 0; \
5781 name = b; \
5782 break;
5783 #define NAMED_CMPXCST(a,b,c,d) \
5784 case a: \
5785 not_in_std = (gfc_option.allow_std & d) == 0; \
5786 name = b; \
5787 break;
5788 #include "iso-c-binding.def"
5789 default:
5790 not_in_std = false;
5791 name = "";
5794 if (not_in_std)
5796 gfc_error ("The symbol '%s', referenced at %L, is not "
5797 "in the selected standard", name, &u->where);
5798 continue;
5801 switch (i)
5803 #define NAMED_FUNCTION(a,b,c,d) \
5804 case a: \
5805 if (a == ISOCBINDING_LOC) \
5806 return_type = c_ptr->n.sym; \
5807 else if (a == ISOCBINDING_FUNLOC) \
5808 return_type = c_funptr->n.sym; \
5809 else \
5810 return_type = NULL; \
5811 create_intrinsic_function (u->local_name[0] \
5812 ? u->local_name : u->use_name, \
5813 a, iso_c_module_name, \
5814 INTMOD_ISO_C_BINDING, false, \
5815 return_type); \
5816 break;
5817 #define NAMED_SUBROUTINE(a,b,c,d) \
5818 case a: \
5819 create_intrinsic_function (u->local_name[0] ? u->local_name \
5820 : u->use_name, \
5821 a, iso_c_module_name, \
5822 INTMOD_ISO_C_BINDING, true, NULL); \
5823 break;
5824 #include "iso-c-binding.def"
5826 case ISOCBINDING_PTR:
5827 case ISOCBINDING_FUNPTR:
5828 /* Already handled above. */
5829 break;
5830 default:
5831 if (i == ISOCBINDING_NULL_PTR)
5832 tmp_symtree = c_ptr;
5833 else if (i == ISOCBINDING_NULL_FUNPTR)
5834 tmp_symtree = c_funptr;
5835 else
5836 tmp_symtree = NULL;
5837 generate_isocbinding_symbol (iso_c_module_name,
5838 (iso_c_binding_symbol) i,
5839 u->local_name[0]
5840 ? u->local_name : u->use_name,
5841 tmp_symtree, false);
5845 if (!found && !only_flag)
5847 /* Skip, if the symbol is not in the enabled standard. */
5848 switch (i)
5850 #define NAMED_FUNCTION(a,b,c,d) \
5851 case a: \
5852 if ((gfc_option.allow_std & d) == 0) \
5853 continue; \
5854 break;
5855 #define NAMED_SUBROUTINE(a,b,c,d) \
5856 case a: \
5857 if ((gfc_option.allow_std & d) == 0) \
5858 continue; \
5859 break;
5860 #define NAMED_INTCST(a,b,c,d) \
5861 case a: \
5862 if ((gfc_option.allow_std & d) == 0) \
5863 continue; \
5864 break;
5865 #define NAMED_REALCST(a,b,c,d) \
5866 case a: \
5867 if ((gfc_option.allow_std & d) == 0) \
5868 continue; \
5869 break;
5870 #define NAMED_CMPXCST(a,b,c,d) \
5871 case a: \
5872 if ((gfc_option.allow_std & d) == 0) \
5873 continue; \
5874 break;
5875 #include "iso-c-binding.def"
5876 default:
5877 ; /* Not GFC_STD_* versioned. */
5880 switch (i)
5882 #define NAMED_FUNCTION(a,b,c,d) \
5883 case a: \
5884 if (a == ISOCBINDING_LOC) \
5885 return_type = c_ptr->n.sym; \
5886 else if (a == ISOCBINDING_FUNLOC) \
5887 return_type = c_funptr->n.sym; \
5888 else \
5889 return_type = NULL; \
5890 create_intrinsic_function (b, a, iso_c_module_name, \
5891 INTMOD_ISO_C_BINDING, false, \
5892 return_type); \
5893 break;
5894 #define NAMED_SUBROUTINE(a,b,c,d) \
5895 case a: \
5896 create_intrinsic_function (b, a, iso_c_module_name, \
5897 INTMOD_ISO_C_BINDING, true, NULL); \
5898 break;
5899 #include "iso-c-binding.def"
5901 case ISOCBINDING_PTR:
5902 case ISOCBINDING_FUNPTR:
5903 /* Already handled above. */
5904 break;
5905 default:
5906 if (i == ISOCBINDING_NULL_PTR)
5907 tmp_symtree = c_ptr;
5908 else if (i == ISOCBINDING_NULL_FUNPTR)
5909 tmp_symtree = c_funptr;
5910 else
5911 tmp_symtree = NULL;
5912 generate_isocbinding_symbol (iso_c_module_name,
5913 (iso_c_binding_symbol) i, NULL,
5914 tmp_symtree, false);
5919 for (u = gfc_rename_list; u; u = u->next)
5921 if (u->found)
5922 continue;
5924 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
5925 "module ISO_C_BINDING", u->use_name, &u->where);
5930 /* Add an integer named constant from a given module. */
5932 static void
5933 create_int_parameter (const char *name, int value, const char *modname,
5934 intmod_id module, int id)
5936 gfc_symtree *tmp_symtree;
5937 gfc_symbol *sym;
5939 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5940 if (tmp_symtree != NULL)
5942 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5943 return;
5944 else
5945 gfc_error ("Symbol '%s' already declared", name);
5948 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5949 sym = tmp_symtree->n.sym;
5951 sym->module = gfc_get_string (modname);
5952 sym->attr.flavor = FL_PARAMETER;
5953 sym->ts.type = BT_INTEGER;
5954 sym->ts.kind = gfc_default_integer_kind;
5955 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
5956 sym->attr.use_assoc = 1;
5957 sym->from_intmod = module;
5958 sym->intmod_sym_id = id;
5962 /* Value is already contained by the array constructor, but not
5963 yet the shape. */
5965 static void
5966 create_int_parameter_array (const char *name, int size, gfc_expr *value,
5967 const char *modname, intmod_id module, int id)
5969 gfc_symtree *tmp_symtree;
5970 gfc_symbol *sym;
5972 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5973 if (tmp_symtree != NULL)
5975 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5976 return;
5977 else
5978 gfc_error ("Symbol '%s' already declared", name);
5981 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5982 sym = tmp_symtree->n.sym;
5984 sym->module = gfc_get_string (modname);
5985 sym->attr.flavor = FL_PARAMETER;
5986 sym->ts.type = BT_INTEGER;
5987 sym->ts.kind = gfc_default_integer_kind;
5988 sym->attr.use_assoc = 1;
5989 sym->from_intmod = module;
5990 sym->intmod_sym_id = id;
5991 sym->attr.dimension = 1;
5992 sym->as = gfc_get_array_spec ();
5993 sym->as->rank = 1;
5994 sym->as->type = AS_EXPLICIT;
5995 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5996 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
5998 sym->value = value;
5999 sym->value->shape = gfc_get_shape (1);
6000 mpz_init_set_ui (sym->value->shape[0], size);
6004 /* Add an derived type for a given module. */
6006 static void
6007 create_derived_type (const char *name, const char *modname,
6008 intmod_id module, int id)
6010 gfc_symtree *tmp_symtree;
6011 gfc_symbol *sym, *dt_sym;
6012 gfc_interface *intr, *head;
6014 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6015 if (tmp_symtree != NULL)
6017 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6018 return;
6019 else
6020 gfc_error ("Symbol '%s' already declared", name);
6023 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6024 sym = tmp_symtree->n.sym;
6025 sym->module = gfc_get_string (modname);
6026 sym->from_intmod = module;
6027 sym->intmod_sym_id = id;
6028 sym->attr.flavor = FL_PROCEDURE;
6029 sym->attr.function = 1;
6030 sym->attr.generic = 1;
6032 gfc_get_sym_tree (dt_upper_string (sym->name),
6033 gfc_current_ns, &tmp_symtree, false);
6034 dt_sym = tmp_symtree->n.sym;
6035 dt_sym->name = gfc_get_string (sym->name);
6036 dt_sym->attr.flavor = FL_DERIVED;
6037 dt_sym->attr.private_comp = 1;
6038 dt_sym->attr.zero_comp = 1;
6039 dt_sym->attr.use_assoc = 1;
6040 dt_sym->module = gfc_get_string (modname);
6041 dt_sym->from_intmod = module;
6042 dt_sym->intmod_sym_id = id;
6044 head = sym->generic;
6045 intr = gfc_get_interface ();
6046 intr->sym = dt_sym;
6047 intr->where = gfc_current_locus;
6048 intr->next = head;
6049 sym->generic = intr;
6050 sym->attr.if_source = IFSRC_DECL;
6054 /* Read the contents of the module file into a temporary buffer. */
6056 static void
6057 read_module_to_tmpbuf ()
6059 /* We don't know the uncompressed size, so enlarge the buffer as
6060 needed. */
6061 int cursz = 4096;
6062 int rsize = cursz;
6063 int len = 0;
6065 module_content = XNEWVEC (char, cursz);
6067 while (1)
6069 int nread = gzread (module_fp, module_content + len, rsize);
6070 len += nread;
6071 if (nread < rsize)
6072 break;
6073 cursz *= 2;
6074 module_content = XRESIZEVEC (char, module_content, cursz);
6075 rsize = cursz - len;
6078 module_content = XRESIZEVEC (char, module_content, len + 1);
6079 module_content[len] = '\0';
6081 module_pos = 0;
6085 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6087 static void
6088 use_iso_fortran_env_module (void)
6090 static char mod[] = "iso_fortran_env";
6091 gfc_use_rename *u;
6092 gfc_symbol *mod_sym;
6093 gfc_symtree *mod_symtree;
6094 gfc_expr *expr;
6095 int i, j;
6097 intmod_sym symbol[] = {
6098 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6099 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6100 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6101 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6102 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6103 #include "iso-fortran-env.def"
6104 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6106 i = 0;
6107 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6108 #include "iso-fortran-env.def"
6110 /* Generate the symbol for the module itself. */
6111 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6112 if (mod_symtree == NULL)
6114 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6115 gcc_assert (mod_symtree);
6116 mod_sym = mod_symtree->n.sym;
6118 mod_sym->attr.flavor = FL_MODULE;
6119 mod_sym->attr.intrinsic = 1;
6120 mod_sym->module = gfc_get_string (mod);
6121 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6123 else
6124 if (!mod_symtree->n.sym->attr.intrinsic)
6125 gfc_error ("Use of intrinsic module '%s' at %C conflicts with "
6126 "non-intrinsic module name used previously", mod);
6128 /* Generate the symbols for the module integer named constants. */
6130 for (i = 0; symbol[i].name; i++)
6132 bool found = false;
6133 for (u = gfc_rename_list; u; u = u->next)
6135 if (strcmp (symbol[i].name, u->use_name) == 0)
6137 found = true;
6138 u->found = 1;
6140 if (!gfc_notify_std (symbol[i].standard, "The symbol '%s', "
6141 "referenced at %L, is not in the selected "
6142 "standard", symbol[i].name, &u->where))
6143 continue;
6145 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6146 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6147 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6148 "constant from intrinsic module "
6149 "ISO_FORTRAN_ENV at %L is incompatible with "
6150 "option %s", &u->where,
6151 gfc_option.flag_default_integer
6152 ? "-fdefault-integer-8"
6153 : "-fdefault-real-8");
6154 switch (symbol[i].id)
6156 #define NAMED_INTCST(a,b,c,d) \
6157 case a:
6158 #include "iso-fortran-env.def"
6159 create_int_parameter (u->local_name[0] ? u->local_name
6160 : u->use_name,
6161 symbol[i].value, mod,
6162 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6163 break;
6165 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6166 case a:\
6167 expr = gfc_get_array_expr (BT_INTEGER, \
6168 gfc_default_integer_kind,\
6169 NULL); \
6170 for (j = 0; KINDS[j].kind != 0; j++) \
6171 gfc_constructor_append_expr (&expr->value.constructor, \
6172 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6173 KINDS[j].kind), NULL); \
6174 create_int_parameter_array (u->local_name[0] ? u->local_name \
6175 : u->use_name, \
6176 j, expr, mod, \
6177 INTMOD_ISO_FORTRAN_ENV, \
6178 symbol[i].id); \
6179 break;
6180 #include "iso-fortran-env.def"
6182 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6183 case a:
6184 #include "iso-fortran-env.def"
6185 create_derived_type (u->local_name[0] ? u->local_name
6186 : u->use_name,
6187 mod, INTMOD_ISO_FORTRAN_ENV,
6188 symbol[i].id);
6189 break;
6191 #define NAMED_FUNCTION(a,b,c,d) \
6192 case a:
6193 #include "iso-fortran-env.def"
6194 create_intrinsic_function (u->local_name[0] ? u->local_name
6195 : u->use_name,
6196 symbol[i].id, mod,
6197 INTMOD_ISO_FORTRAN_ENV, false,
6198 NULL);
6199 break;
6201 default:
6202 gcc_unreachable ();
6207 if (!found && !only_flag)
6209 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6210 continue;
6212 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6213 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6214 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6215 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6216 "incompatible with option %s",
6217 gfc_option.flag_default_integer
6218 ? "-fdefault-integer-8" : "-fdefault-real-8");
6220 switch (symbol[i].id)
6222 #define NAMED_INTCST(a,b,c,d) \
6223 case a:
6224 #include "iso-fortran-env.def"
6225 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6226 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6227 break;
6229 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6230 case a:\
6231 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6232 NULL); \
6233 for (j = 0; KINDS[j].kind != 0; j++) \
6234 gfc_constructor_append_expr (&expr->value.constructor, \
6235 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6236 KINDS[j].kind), NULL); \
6237 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6238 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6239 break;
6240 #include "iso-fortran-env.def"
6242 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6243 case a:
6244 #include "iso-fortran-env.def"
6245 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6246 symbol[i].id);
6247 break;
6249 #define NAMED_FUNCTION(a,b,c,d) \
6250 case a:
6251 #include "iso-fortran-env.def"
6252 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6253 INTMOD_ISO_FORTRAN_ENV, false,
6254 NULL);
6255 break;
6257 default:
6258 gcc_unreachable ();
6263 for (u = gfc_rename_list; u; u = u->next)
6265 if (u->found)
6266 continue;
6268 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
6269 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6274 /* Process a USE directive. */
6276 static void
6277 gfc_use_module (gfc_use_list *module)
6279 char *filename;
6280 gfc_state_data *p;
6281 int c, line, start;
6282 gfc_symtree *mod_symtree;
6283 gfc_use_list *use_stmt;
6284 locus old_locus = gfc_current_locus;
6286 gfc_current_locus = module->where;
6287 module_name = module->module_name;
6288 gfc_rename_list = module->rename;
6289 only_flag = module->only_flag;
6291 filename = XALLOCAVEC (char, strlen (module_name) + strlen (MODULE_EXTENSION)
6292 + 1);
6293 strcpy (filename, module_name);
6294 strcat (filename, MODULE_EXTENSION);
6296 /* First, try to find an non-intrinsic module, unless the USE statement
6297 specified that the module is intrinsic. */
6298 module_fp = NULL;
6299 if (!module->intrinsic)
6300 module_fp = gzopen_included_file (filename, true, true);
6302 /* Then, see if it's an intrinsic one, unless the USE statement
6303 specified that the module is non-intrinsic. */
6304 if (module_fp == NULL && !module->non_intrinsic)
6306 if (strcmp (module_name, "iso_fortran_env") == 0
6307 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6308 "intrinsic module at %C"))
6310 use_iso_fortran_env_module ();
6311 free_rename (module->rename);
6312 module->rename = NULL;
6313 gfc_current_locus = old_locus;
6314 module->intrinsic = true;
6315 return;
6318 if (strcmp (module_name, "iso_c_binding") == 0
6319 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6321 import_iso_c_binding_module();
6322 free_rename (module->rename);
6323 module->rename = NULL;
6324 gfc_current_locus = old_locus;
6325 module->intrinsic = true;
6326 return;
6329 module_fp = gzopen_intrinsic_module (filename);
6331 if (module_fp == NULL && module->intrinsic)
6332 gfc_fatal_error ("Can't find an intrinsic module named '%s' at %C",
6333 module_name);
6336 if (module_fp == NULL)
6337 gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
6338 filename, xstrerror (errno));
6340 /* Check that we haven't already USEd an intrinsic module with the
6341 same name. */
6343 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6344 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6345 gfc_error ("Use of non-intrinsic module '%s' at %C conflicts with "
6346 "intrinsic module name used previously", module_name);
6348 iomode = IO_INPUT;
6349 module_line = 1;
6350 module_column = 1;
6351 start = 0;
6353 read_module_to_tmpbuf ();
6354 gzclose (module_fp);
6356 /* Skip the first line of the module, after checking that this is
6357 a gfortran module file. */
6358 line = 0;
6359 while (line < 1)
6361 c = module_char ();
6362 if (c == EOF)
6363 bad_module ("Unexpected end of module");
6364 if (start++ < 3)
6365 parse_name (c);
6366 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6367 || (start == 2 && strcmp (atom_name, " module") != 0))
6368 gfc_fatal_error ("File '%s' opened at %C is not a GNU Fortran"
6369 " module file", filename);
6370 if (start == 3)
6372 if (strcmp (atom_name, " version") != 0
6373 || module_char () != ' '
6374 || parse_atom () != ATOM_STRING
6375 || strcmp (atom_string, MOD_VERSION))
6376 gfc_fatal_error ("Cannot read module file '%s' opened at %C,"
6377 " because it was created by a different"
6378 " version of GNU Fortran", filename);
6380 free (atom_string);
6383 if (c == '\n')
6384 line++;
6387 /* Make sure we're not reading the same module that we may be building. */
6388 for (p = gfc_state_stack; p; p = p->previous)
6389 if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
6390 gfc_fatal_error ("Can't USE the same module we're building!");
6392 init_pi_tree ();
6393 init_true_name_tree ();
6395 read_module ();
6397 free_true_name (true_name_root);
6398 true_name_root = NULL;
6400 free_pi_tree (pi_root);
6401 pi_root = NULL;
6403 XDELETEVEC (module_content);
6404 module_content = NULL;
6406 use_stmt = gfc_get_use_list ();
6407 *use_stmt = *module;
6408 use_stmt->next = gfc_current_ns->use_stmts;
6409 gfc_current_ns->use_stmts = use_stmt;
6411 gfc_current_locus = old_locus;
6415 /* Remove duplicated intrinsic operators from the rename list. */
6417 static void
6418 rename_list_remove_duplicate (gfc_use_rename *list)
6420 gfc_use_rename *seek, *last;
6422 for (; list; list = list->next)
6423 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6425 last = list;
6426 for (seek = list->next; seek; seek = last->next)
6428 if (list->op == seek->op)
6430 last->next = seek->next;
6431 free (seek);
6433 else
6434 last = seek;
6440 /* Process all USE directives. */
6442 void
6443 gfc_use_modules (void)
6445 gfc_use_list *next, *seek, *last;
6447 for (next = module_list; next; next = next->next)
6449 bool non_intrinsic = next->non_intrinsic;
6450 bool intrinsic = next->intrinsic;
6451 bool neither = !non_intrinsic && !intrinsic;
6453 for (seek = next->next; seek; seek = seek->next)
6455 if (next->module_name != seek->module_name)
6456 continue;
6458 if (seek->non_intrinsic)
6459 non_intrinsic = true;
6460 else if (seek->intrinsic)
6461 intrinsic = true;
6462 else
6463 neither = true;
6466 if (intrinsic && neither && !non_intrinsic)
6468 char *filename;
6469 FILE *fp;
6471 filename = XALLOCAVEC (char,
6472 strlen (next->module_name)
6473 + strlen (MODULE_EXTENSION) + 1);
6474 strcpy (filename, next->module_name);
6475 strcat (filename, MODULE_EXTENSION);
6476 fp = gfc_open_included_file (filename, true, true);
6477 if (fp != NULL)
6479 non_intrinsic = true;
6480 fclose (fp);
6484 last = next;
6485 for (seek = next->next; seek; seek = last->next)
6487 if (next->module_name != seek->module_name)
6489 last = seek;
6490 continue;
6493 if ((!next->intrinsic && !seek->intrinsic)
6494 || (next->intrinsic && seek->intrinsic)
6495 || !non_intrinsic)
6497 if (!seek->only_flag)
6498 next->only_flag = false;
6499 if (seek->rename)
6501 gfc_use_rename *r = seek->rename;
6502 while (r->next)
6503 r = r->next;
6504 r->next = next->rename;
6505 next->rename = seek->rename;
6507 last->next = seek->next;
6508 free (seek);
6510 else
6511 last = seek;
6515 for (; module_list; module_list = next)
6517 next = module_list->next;
6518 rename_list_remove_duplicate (module_list->rename);
6519 gfc_use_module (module_list);
6520 free (module_list);
6522 gfc_rename_list = NULL;
6526 void
6527 gfc_free_use_stmts (gfc_use_list *use_stmts)
6529 gfc_use_list *next;
6530 for (; use_stmts; use_stmts = next)
6532 gfc_use_rename *next_rename;
6534 for (; use_stmts->rename; use_stmts->rename = next_rename)
6536 next_rename = use_stmts->rename->next;
6537 free (use_stmts->rename);
6539 next = use_stmts->next;
6540 free (use_stmts);
6545 void
6546 gfc_module_init_2 (void)
6548 last_atom = ATOM_LPAREN;
6549 gfc_rename_list = NULL;
6550 module_list = NULL;
6554 void
6555 gfc_module_done_2 (void)
6557 free_rename (gfc_rename_list);
6558 gfc_rename_list = NULL;