2015-01-20 Jeff Law <law@redhat.com>
[official-gcc.git] / gcc / fortran / module.c
blobc47489aeec3dedad424973aae0985450d9389d18
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "gfortran.h"
71 #include "arith.h"
72 #include "match.h"
73 #include "parse.h" /* FIXME */
74 #include "constructor.h"
75 #include "cpp.h"
76 #include "hash-set.h"
77 #include "machmode.h"
78 #include "vec.h"
79 #include "double-int.h"
80 #include "input.h"
81 #include "alias.h"
82 #include "symtab.h"
83 #include "options.h"
84 #include "wide-int.h"
85 #include "inchash.h"
86 #include "tree.h"
87 #include "stringpool.h"
88 #include "scanner.h"
89 #include <zlib.h>
91 #define MODULE_EXTENSION ".mod"
93 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
94 recognized. */
95 #define MOD_VERSION "13"
98 /* Structure that describes a position within a module file. */
100 typedef struct
102 int column, line;
103 long pos;
105 module_locus;
107 /* Structure for list of symbols of intrinsic modules. */
108 typedef struct
110 int id;
111 const char *name;
112 int value;
113 int standard;
115 intmod_sym;
118 typedef enum
120 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
122 pointer_t;
124 /* The fixup structure lists pointers to pointers that have to
125 be updated when a pointer value becomes known. */
127 typedef struct fixup_t
129 void **pointer;
130 struct fixup_t *next;
132 fixup_t;
135 /* Structure for holding extra info needed for pointers being read. */
137 enum gfc_rsym_state
139 UNUSED,
140 NEEDED,
141 USED
144 enum gfc_wsym_state
146 UNREFERENCED = 0,
147 NEEDS_WRITE,
148 WRITTEN
151 typedef struct pointer_info
153 BBT_HEADER (pointer_info);
154 int integer;
155 pointer_t type;
157 /* The first component of each member of the union is the pointer
158 being stored. */
160 fixup_t *fixup;
162 union
164 void *pointer; /* Member for doing pointer searches. */
166 struct
168 gfc_symbol *sym;
169 char *true_name, *module, *binding_label;
170 fixup_t *stfixup;
171 gfc_symtree *symtree;
172 enum gfc_rsym_state state;
173 int ns, referenced, renamed;
174 module_locus where;
176 rsym;
178 struct
180 gfc_symbol *sym;
181 enum gfc_wsym_state state;
183 wsym;
188 pointer_info;
190 #define gfc_get_pointer_info() XCNEW (pointer_info)
193 /* Local variables */
195 /* The gzFile for the module we're reading or writing. */
196 static gzFile module_fp;
199 /* The name of the module we're reading (USE'ing) or writing. */
200 static const char *module_name;
201 static gfc_use_list *module_list;
203 /* If we're reading an intrinsic module, this is its ID. */
204 static intmod_id current_intmod;
206 /* Content of module. */
207 static char* module_content;
209 static long module_pos;
210 static int module_line, module_column, only_flag;
211 static int prev_module_line, prev_module_column;
213 static enum
214 { IO_INPUT, IO_OUTPUT }
215 iomode;
217 static gfc_use_rename *gfc_rename_list;
218 static pointer_info *pi_root;
219 static int symbol_number; /* Counter for assigning symbol numbers */
221 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
222 static bool in_load_equiv;
226 /*****************************************************************/
228 /* Pointer/integer conversion. Pointers between structures are stored
229 as integers in the module file. The next couple of subroutines
230 handle this translation for reading and writing. */
232 /* Recursively free the tree of pointer structures. */
234 static void
235 free_pi_tree (pointer_info *p)
237 if (p == NULL)
238 return;
240 if (p->fixup != NULL)
241 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
243 free_pi_tree (p->left);
244 free_pi_tree (p->right);
246 if (iomode == IO_INPUT)
248 XDELETEVEC (p->u.rsym.true_name);
249 XDELETEVEC (p->u.rsym.module);
250 XDELETEVEC (p->u.rsym.binding_label);
253 free (p);
257 /* Compare pointers when searching by pointer. Used when writing a
258 module. */
260 static int
261 compare_pointers (void *_sn1, void *_sn2)
263 pointer_info *sn1, *sn2;
265 sn1 = (pointer_info *) _sn1;
266 sn2 = (pointer_info *) _sn2;
268 if (sn1->u.pointer < sn2->u.pointer)
269 return -1;
270 if (sn1->u.pointer > sn2->u.pointer)
271 return 1;
273 return 0;
277 /* Compare integers when searching by integer. Used when reading a
278 module. */
280 static int
281 compare_integers (void *_sn1, void *_sn2)
283 pointer_info *sn1, *sn2;
285 sn1 = (pointer_info *) _sn1;
286 sn2 = (pointer_info *) _sn2;
288 if (sn1->integer < sn2->integer)
289 return -1;
290 if (sn1->integer > sn2->integer)
291 return 1;
293 return 0;
297 /* Initialize the pointer_info tree. */
299 static void
300 init_pi_tree (void)
302 compare_fn compare;
303 pointer_info *p;
305 pi_root = NULL;
306 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
308 /* Pointer 0 is the NULL pointer. */
309 p = gfc_get_pointer_info ();
310 p->u.pointer = NULL;
311 p->integer = 0;
312 p->type = P_OTHER;
314 gfc_insert_bbt (&pi_root, p, compare);
316 /* Pointer 1 is the current namespace. */
317 p = gfc_get_pointer_info ();
318 p->u.pointer = gfc_current_ns;
319 p->integer = 1;
320 p->type = P_NAMESPACE;
322 gfc_insert_bbt (&pi_root, p, compare);
324 symbol_number = 2;
328 /* During module writing, call here with a pointer to something,
329 returning the pointer_info node. */
331 static pointer_info *
332 find_pointer (void *gp)
334 pointer_info *p;
336 p = pi_root;
337 while (p != NULL)
339 if (p->u.pointer == gp)
340 break;
341 p = (gp < p->u.pointer) ? p->left : p->right;
344 return p;
348 /* Given a pointer while writing, returns the pointer_info tree node,
349 creating it if it doesn't exist. */
351 static pointer_info *
352 get_pointer (void *gp)
354 pointer_info *p;
356 p = find_pointer (gp);
357 if (p != NULL)
358 return p;
360 /* Pointer doesn't have an integer. Give it one. */
361 p = gfc_get_pointer_info ();
363 p->u.pointer = gp;
364 p->integer = symbol_number++;
366 gfc_insert_bbt (&pi_root, p, compare_pointers);
368 return p;
372 /* Given an integer during reading, find it in the pointer_info tree,
373 creating the node if not found. */
375 static pointer_info *
376 get_integer (int integer)
378 pointer_info *p, t;
379 int c;
381 t.integer = integer;
383 p = pi_root;
384 while (p != NULL)
386 c = compare_integers (&t, p);
387 if (c == 0)
388 break;
390 p = (c < 0) ? p->left : p->right;
393 if (p != NULL)
394 return p;
396 p = gfc_get_pointer_info ();
397 p->integer = integer;
398 p->u.pointer = NULL;
400 gfc_insert_bbt (&pi_root, p, compare_integers);
402 return p;
406 /* Resolve any fixups using a known pointer. */
408 static void
409 resolve_fixups (fixup_t *f, void *gp)
411 fixup_t *next;
413 for (; f; f = next)
415 next = f->next;
416 *(f->pointer) = gp;
417 free (f);
422 /* Convert a string such that it starts with a lower-case character. Used
423 to convert the symtree name of a derived-type to the symbol name or to
424 the name of the associated generic function. */
426 static const char *
427 dt_lower_string (const char *name)
429 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
430 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
431 &name[1]);
432 return gfc_get_string (name);
436 /* Convert a string such that it starts with an upper-case character. Used to
437 return the symtree-name for a derived type; the symbol name itself and the
438 symtree/symbol name of the associated generic function start with a lower-
439 case character. */
441 static const char *
442 dt_upper_string (const char *name)
444 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
445 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
446 &name[1]);
447 return gfc_get_string (name);
450 /* Call here during module reading when we know what pointer to
451 associate with an integer. Any fixups that exist are resolved at
452 this time. */
454 static void
455 associate_integer_pointer (pointer_info *p, void *gp)
457 if (p->u.pointer != NULL)
458 gfc_internal_error ("associate_integer_pointer(): Already associated");
460 p->u.pointer = gp;
462 resolve_fixups (p->fixup, gp);
464 p->fixup = NULL;
468 /* During module reading, given an integer and a pointer to a pointer,
469 either store the pointer from an already-known value or create a
470 fixup structure in order to store things later. Returns zero if
471 the reference has been actually stored, or nonzero if the reference
472 must be fixed later (i.e., associate_integer_pointer must be called
473 sometime later. Returns the pointer_info structure. */
475 static pointer_info *
476 add_fixup (int integer, void *gp)
478 pointer_info *p;
479 fixup_t *f;
480 char **cp;
482 p = get_integer (integer);
484 if (p->integer == 0 || p->u.pointer != NULL)
486 cp = (char **) gp;
487 *cp = (char *) p->u.pointer;
489 else
491 f = XCNEW (fixup_t);
493 f->next = p->fixup;
494 p->fixup = f;
496 f->pointer = (void **) gp;
499 return p;
503 /*****************************************************************/
505 /* Parser related subroutines */
507 /* Free the rename list left behind by a USE statement. */
509 static void
510 free_rename (gfc_use_rename *list)
512 gfc_use_rename *next;
514 for (; list; list = next)
516 next = list->next;
517 free (list);
522 /* Match a USE statement. */
524 match
525 gfc_match_use (void)
527 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
528 gfc_use_rename *tail = NULL, *new_use;
529 interface_type type, type2;
530 gfc_intrinsic_op op;
531 match m;
532 gfc_use_list *use_list;
534 use_list = gfc_get_use_list ();
536 if (gfc_match (" , ") == MATCH_YES)
538 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
540 if (!gfc_notify_std (GFC_STD_F2003, "module "
541 "nature in USE statement at %C"))
542 goto cleanup;
544 if (strcmp (module_nature, "intrinsic") == 0)
545 use_list->intrinsic = true;
546 else
548 if (strcmp (module_nature, "non_intrinsic") == 0)
549 use_list->non_intrinsic = true;
550 else
552 gfc_error ("Module nature in USE statement at %C shall "
553 "be either INTRINSIC or NON_INTRINSIC");
554 goto cleanup;
558 else
560 /* Help output a better error message than "Unclassifiable
561 statement". */
562 gfc_match (" %n", module_nature);
563 if (strcmp (module_nature, "intrinsic") == 0
564 || strcmp (module_nature, "non_intrinsic") == 0)
565 gfc_error ("\"::\" was expected after module nature at %C "
566 "but was not found");
567 free (use_list);
568 return m;
571 else
573 m = gfc_match (" ::");
574 if (m == MATCH_YES &&
575 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
576 goto cleanup;
578 if (m != MATCH_YES)
580 m = gfc_match ("% ");
581 if (m != MATCH_YES)
583 free (use_list);
584 return m;
589 use_list->where = gfc_current_locus;
591 m = gfc_match_name (name);
592 if (m != MATCH_YES)
594 free (use_list);
595 return m;
598 use_list->module_name = gfc_get_string (name);
600 if (gfc_match_eos () == MATCH_YES)
601 goto done;
603 if (gfc_match_char (',') != MATCH_YES)
604 goto syntax;
606 if (gfc_match (" only :") == MATCH_YES)
607 use_list->only_flag = true;
609 if (gfc_match_eos () == MATCH_YES)
610 goto done;
612 for (;;)
614 /* Get a new rename struct and add it to the rename list. */
615 new_use = gfc_get_use_rename ();
616 new_use->where = gfc_current_locus;
617 new_use->found = 0;
619 if (use_list->rename == NULL)
620 use_list->rename = new_use;
621 else
622 tail->next = new_use;
623 tail = new_use;
625 /* See what kind of interface we're dealing with. Assume it is
626 not an operator. */
627 new_use->op = INTRINSIC_NONE;
628 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
629 goto cleanup;
631 switch (type)
633 case INTERFACE_NAMELESS:
634 gfc_error ("Missing generic specification in USE statement at %C");
635 goto cleanup;
637 case INTERFACE_USER_OP:
638 case INTERFACE_GENERIC:
639 m = gfc_match (" =>");
641 if (type == INTERFACE_USER_OP && m == MATCH_YES
642 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
643 "operators in USE statements at %C")))
644 goto cleanup;
646 if (type == INTERFACE_USER_OP)
647 new_use->op = INTRINSIC_USER;
649 if (use_list->only_flag)
651 if (m != MATCH_YES)
652 strcpy (new_use->use_name, name);
653 else
655 strcpy (new_use->local_name, name);
656 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
657 if (type != type2)
658 goto syntax;
659 if (m == MATCH_NO)
660 goto syntax;
661 if (m == MATCH_ERROR)
662 goto cleanup;
665 else
667 if (m != MATCH_YES)
668 goto syntax;
669 strcpy (new_use->local_name, name);
671 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
672 if (type != type2)
673 goto syntax;
674 if (m == MATCH_NO)
675 goto syntax;
676 if (m == MATCH_ERROR)
677 goto cleanup;
680 if (strcmp (new_use->use_name, use_list->module_name) == 0
681 || strcmp (new_use->local_name, use_list->module_name) == 0)
683 gfc_error ("The name %qs at %C has already been used as "
684 "an external module name.", use_list->module_name);
685 goto cleanup;
687 break;
689 case INTERFACE_INTRINSIC_OP:
690 new_use->op = op;
691 break;
693 default:
694 gcc_unreachable ();
697 if (gfc_match_eos () == MATCH_YES)
698 break;
699 if (gfc_match_char (',') != MATCH_YES)
700 goto syntax;
703 done:
704 if (module_list)
706 gfc_use_list *last = module_list;
707 while (last->next)
708 last = last->next;
709 last->next = use_list;
711 else
712 module_list = use_list;
714 return MATCH_YES;
716 syntax:
717 gfc_syntax_error (ST_USE);
719 cleanup:
720 free_rename (use_list->rename);
721 free (use_list);
722 return MATCH_ERROR;
726 /* Given a name and a number, inst, return the inst name
727 under which to load this symbol. Returns NULL if this
728 symbol shouldn't be loaded. If inst is zero, returns
729 the number of instances of this name. If interface is
730 true, a user-defined operator is sought, otherwise only
731 non-operators are sought. */
733 static const char *
734 find_use_name_n (const char *name, int *inst, bool interface)
736 gfc_use_rename *u;
737 const char *low_name = NULL;
738 int i;
740 /* For derived types. */
741 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
742 low_name = dt_lower_string (name);
744 i = 0;
745 for (u = gfc_rename_list; u; u = u->next)
747 if ((!low_name && strcmp (u->use_name, name) != 0)
748 || (low_name && strcmp (u->use_name, low_name) != 0)
749 || (u->op == INTRINSIC_USER && !interface)
750 || (u->op != INTRINSIC_USER && interface))
751 continue;
752 if (++i == *inst)
753 break;
756 if (!*inst)
758 *inst = i;
759 return NULL;
762 if (u == NULL)
763 return only_flag ? NULL : name;
765 u->found = 1;
767 if (low_name)
769 if (u->local_name[0] == '\0')
770 return name;
771 return dt_upper_string (u->local_name);
774 return (u->local_name[0] != '\0') ? u->local_name : name;
778 /* Given a name, return the name under which to load this symbol.
779 Returns NULL if this symbol shouldn't be loaded. */
781 static const char *
782 find_use_name (const char *name, bool interface)
784 int i = 1;
785 return find_use_name_n (name, &i, interface);
789 /* Given a real name, return the number of use names associated with it. */
791 static int
792 number_use_names (const char *name, bool interface)
794 int i = 0;
795 find_use_name_n (name, &i, interface);
796 return i;
800 /* Try to find the operator in the current list. */
802 static gfc_use_rename *
803 find_use_operator (gfc_intrinsic_op op)
805 gfc_use_rename *u;
807 for (u = gfc_rename_list; u; u = u->next)
808 if (u->op == op)
809 return u;
811 return NULL;
815 /*****************************************************************/
817 /* The next couple of subroutines maintain a tree used to avoid a
818 brute-force search for a combination of true name and module name.
819 While symtree names, the name that a particular symbol is known by
820 can changed with USE statements, we still have to keep track of the
821 true names to generate the correct reference, and also avoid
822 loading the same real symbol twice in a program unit.
824 When we start reading, the true name tree is built and maintained
825 as symbols are read. The tree is searched as we load new symbols
826 to see if it already exists someplace in the namespace. */
828 typedef struct true_name
830 BBT_HEADER (true_name);
831 const char *name;
832 gfc_symbol *sym;
834 true_name;
836 static true_name *true_name_root;
839 /* Compare two true_name structures. */
841 static int
842 compare_true_names (void *_t1, void *_t2)
844 true_name *t1, *t2;
845 int c;
847 t1 = (true_name *) _t1;
848 t2 = (true_name *) _t2;
850 c = ((t1->sym->module > t2->sym->module)
851 - (t1->sym->module < t2->sym->module));
852 if (c != 0)
853 return c;
855 return strcmp (t1->name, t2->name);
859 /* Given a true name, search the true name tree to see if it exists
860 within the main namespace. */
862 static gfc_symbol *
863 find_true_name (const char *name, const char *module)
865 true_name t, *p;
866 gfc_symbol sym;
867 int c;
869 t.name = gfc_get_string (name);
870 if (module != NULL)
871 sym.module = gfc_get_string (module);
872 else
873 sym.module = NULL;
874 t.sym = &sym;
876 p = true_name_root;
877 while (p != NULL)
879 c = compare_true_names ((void *) (&t), (void *) p);
880 if (c == 0)
881 return p->sym;
883 p = (c < 0) ? p->left : p->right;
886 return NULL;
890 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
892 static void
893 add_true_name (gfc_symbol *sym)
895 true_name *t;
897 t = XCNEW (true_name);
898 t->sym = sym;
899 if (sym->attr.flavor == FL_DERIVED)
900 t->name = dt_upper_string (sym->name);
901 else
902 t->name = sym->name;
904 gfc_insert_bbt (&true_name_root, t, compare_true_names);
908 /* Recursive function to build the initial true name tree by
909 recursively traversing the current namespace. */
911 static void
912 build_tnt (gfc_symtree *st)
914 const char *name;
915 if (st == NULL)
916 return;
918 build_tnt (st->left);
919 build_tnt (st->right);
921 if (st->n.sym->attr.flavor == FL_DERIVED)
922 name = dt_upper_string (st->n.sym->name);
923 else
924 name = st->n.sym->name;
926 if (find_true_name (name, st->n.sym->module) != NULL)
927 return;
929 add_true_name (st->n.sym);
933 /* Initialize the true name tree with the current namespace. */
935 static void
936 init_true_name_tree (void)
938 true_name_root = NULL;
939 build_tnt (gfc_current_ns->sym_root);
943 /* Recursively free a true name tree node. */
945 static void
946 free_true_name (true_name *t)
948 if (t == NULL)
949 return;
950 free_true_name (t->left);
951 free_true_name (t->right);
953 free (t);
957 /*****************************************************************/
959 /* Module reading and writing. */
961 /* The following are versions similar to the ones in scanner.c, but
962 for dealing with compressed module files. */
964 static gzFile
965 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
966 bool module, bool system)
968 char *fullname;
969 gfc_directorylist *p;
970 gzFile f;
972 for (p = list; p; p = p->next)
974 if (module && !p->use_for_modules)
975 continue;
977 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
978 strcpy (fullname, p->path);
979 strcat (fullname, name);
981 f = gzopen (fullname, "r");
982 if (f != NULL)
984 if (gfc_cpp_makedep ())
985 gfc_cpp_add_dep (fullname, system);
987 return f;
991 return NULL;
994 static gzFile
995 gzopen_included_file (const char *name, bool include_cwd, bool module)
997 gzFile f = NULL;
999 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1001 f = gzopen (name, "r");
1002 if (f && gfc_cpp_makedep ())
1003 gfc_cpp_add_dep (name, false);
1006 if (!f)
1007 f = gzopen_included_file_1 (name, include_dirs, module, false);
1009 return f;
1012 static gzFile
1013 gzopen_intrinsic_module (const char* name)
1015 gzFile f = NULL;
1017 if (IS_ABSOLUTE_PATH (name))
1019 f = gzopen (name, "r");
1020 if (f && gfc_cpp_makedep ())
1021 gfc_cpp_add_dep (name, true);
1024 if (!f)
1025 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1027 return f;
1031 typedef enum
1033 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1035 atom_type;
1037 static atom_type last_atom;
1040 /* The name buffer must be at least as long as a symbol name. Right
1041 now it's not clear how we're going to store numeric constants--
1042 probably as a hexadecimal string, since this will allow the exact
1043 number to be preserved (this can't be done by a decimal
1044 representation). Worry about that later. TODO! */
1046 #define MAX_ATOM_SIZE 100
1048 static int atom_int;
1049 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1052 /* Report problems with a module. Error reporting is not very
1053 elaborate, since this sorts of errors shouldn't really happen.
1054 This subroutine never returns. */
1056 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1058 static void
1059 bad_module (const char *msgid)
1061 XDELETEVEC (module_content);
1062 module_content = NULL;
1064 switch (iomode)
1066 case IO_INPUT:
1067 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1068 module_name, module_line, module_column, msgid);
1069 break;
1070 case IO_OUTPUT:
1071 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1072 module_name, module_line, module_column, msgid);
1073 break;
1074 default:
1075 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1076 module_name, module_line, module_column, msgid);
1077 break;
1082 /* Set the module's input pointer. */
1084 static void
1085 set_module_locus (module_locus *m)
1087 module_column = m->column;
1088 module_line = m->line;
1089 module_pos = m->pos;
1093 /* Get the module's input pointer so that we can restore it later. */
1095 static void
1096 get_module_locus (module_locus *m)
1098 m->column = module_column;
1099 m->line = module_line;
1100 m->pos = module_pos;
1104 /* Get the next character in the module, updating our reckoning of
1105 where we are. */
1107 static int
1108 module_char (void)
1110 const char c = module_content[module_pos++];
1111 if (c == '\0')
1112 bad_module ("Unexpected EOF");
1114 prev_module_line = module_line;
1115 prev_module_column = module_column;
1117 if (c == '\n')
1119 module_line++;
1120 module_column = 0;
1123 module_column++;
1124 return c;
1127 /* Unget a character while remembering the line and column. Works for
1128 a single character only. */
1130 static void
1131 module_unget_char (void)
1133 module_line = prev_module_line;
1134 module_column = prev_module_column;
1135 module_pos--;
1138 /* Parse a string constant. The delimiter is guaranteed to be a
1139 single quote. */
1141 static void
1142 parse_string (void)
1144 int c;
1145 size_t cursz = 30;
1146 size_t len = 0;
1148 atom_string = XNEWVEC (char, cursz);
1150 for ( ; ; )
1152 c = module_char ();
1154 if (c == '\'')
1156 int c2 = module_char ();
1157 if (c2 != '\'')
1159 module_unget_char ();
1160 break;
1164 if (len >= cursz)
1166 cursz *= 2;
1167 atom_string = XRESIZEVEC (char, atom_string, cursz);
1169 atom_string[len] = c;
1170 len++;
1173 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1174 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1178 /* Parse a small integer. */
1180 static void
1181 parse_integer (int c)
1183 atom_int = c - '0';
1185 for (;;)
1187 c = module_char ();
1188 if (!ISDIGIT (c))
1190 module_unget_char ();
1191 break;
1194 atom_int = 10 * atom_int + c - '0';
1195 if (atom_int > 99999999)
1196 bad_module ("Integer overflow");
1202 /* Parse a name. */
1204 static void
1205 parse_name (int c)
1207 char *p;
1208 int len;
1210 p = atom_name;
1212 *p++ = c;
1213 len = 1;
1215 for (;;)
1217 c = module_char ();
1218 if (!ISALNUM (c) && c != '_' && c != '-')
1220 module_unget_char ();
1221 break;
1224 *p++ = c;
1225 if (++len > GFC_MAX_SYMBOL_LEN)
1226 bad_module ("Name too long");
1229 *p = '\0';
1234 /* Read the next atom in the module's input stream. */
1236 static atom_type
1237 parse_atom (void)
1239 int c;
1243 c = module_char ();
1245 while (c == ' ' || c == '\r' || c == '\n');
1247 switch (c)
1249 case '(':
1250 return ATOM_LPAREN;
1252 case ')':
1253 return ATOM_RPAREN;
1255 case '\'':
1256 parse_string ();
1257 return ATOM_STRING;
1259 case '0':
1260 case '1':
1261 case '2':
1262 case '3':
1263 case '4':
1264 case '5':
1265 case '6':
1266 case '7':
1267 case '8':
1268 case '9':
1269 parse_integer (c);
1270 return ATOM_INTEGER;
1272 case 'a':
1273 case 'b':
1274 case 'c':
1275 case 'd':
1276 case 'e':
1277 case 'f':
1278 case 'g':
1279 case 'h':
1280 case 'i':
1281 case 'j':
1282 case 'k':
1283 case 'l':
1284 case 'm':
1285 case 'n':
1286 case 'o':
1287 case 'p':
1288 case 'q':
1289 case 'r':
1290 case 's':
1291 case 't':
1292 case 'u':
1293 case 'v':
1294 case 'w':
1295 case 'x':
1296 case 'y':
1297 case 'z':
1298 case 'A':
1299 case 'B':
1300 case 'C':
1301 case 'D':
1302 case 'E':
1303 case 'F':
1304 case 'G':
1305 case 'H':
1306 case 'I':
1307 case 'J':
1308 case 'K':
1309 case 'L':
1310 case 'M':
1311 case 'N':
1312 case 'O':
1313 case 'P':
1314 case 'Q':
1315 case 'R':
1316 case 'S':
1317 case 'T':
1318 case 'U':
1319 case 'V':
1320 case 'W':
1321 case 'X':
1322 case 'Y':
1323 case 'Z':
1324 parse_name (c);
1325 return ATOM_NAME;
1327 default:
1328 bad_module ("Bad name");
1331 /* Not reached. */
1335 /* Peek at the next atom on the input. */
1337 static atom_type
1338 peek_atom (void)
1340 int c;
1344 c = module_char ();
1346 while (c == ' ' || c == '\r' || c == '\n');
1348 switch (c)
1350 case '(':
1351 module_unget_char ();
1352 return ATOM_LPAREN;
1354 case ')':
1355 module_unget_char ();
1356 return ATOM_RPAREN;
1358 case '\'':
1359 module_unget_char ();
1360 return ATOM_STRING;
1362 case '0':
1363 case '1':
1364 case '2':
1365 case '3':
1366 case '4':
1367 case '5':
1368 case '6':
1369 case '7':
1370 case '8':
1371 case '9':
1372 module_unget_char ();
1373 return ATOM_INTEGER;
1375 case 'a':
1376 case 'b':
1377 case 'c':
1378 case 'd':
1379 case 'e':
1380 case 'f':
1381 case 'g':
1382 case 'h':
1383 case 'i':
1384 case 'j':
1385 case 'k':
1386 case 'l':
1387 case 'm':
1388 case 'n':
1389 case 'o':
1390 case 'p':
1391 case 'q':
1392 case 'r':
1393 case 's':
1394 case 't':
1395 case 'u':
1396 case 'v':
1397 case 'w':
1398 case 'x':
1399 case 'y':
1400 case 'z':
1401 case 'A':
1402 case 'B':
1403 case 'C':
1404 case 'D':
1405 case 'E':
1406 case 'F':
1407 case 'G':
1408 case 'H':
1409 case 'I':
1410 case 'J':
1411 case 'K':
1412 case 'L':
1413 case 'M':
1414 case 'N':
1415 case 'O':
1416 case 'P':
1417 case 'Q':
1418 case 'R':
1419 case 'S':
1420 case 'T':
1421 case 'U':
1422 case 'V':
1423 case 'W':
1424 case 'X':
1425 case 'Y':
1426 case 'Z':
1427 module_unget_char ();
1428 return ATOM_NAME;
1430 default:
1431 bad_module ("Bad name");
1436 /* Read the next atom from the input, requiring that it be a
1437 particular kind. */
1439 static void
1440 require_atom (atom_type type)
1442 atom_type t;
1443 const char *p;
1444 int column, line;
1446 column = module_column;
1447 line = module_line;
1449 t = parse_atom ();
1450 if (t != type)
1452 switch (type)
1454 case ATOM_NAME:
1455 p = _("Expected name");
1456 break;
1457 case ATOM_LPAREN:
1458 p = _("Expected left parenthesis");
1459 break;
1460 case ATOM_RPAREN:
1461 p = _("Expected right parenthesis");
1462 break;
1463 case ATOM_INTEGER:
1464 p = _("Expected integer");
1465 break;
1466 case ATOM_STRING:
1467 p = _("Expected string");
1468 break;
1469 default:
1470 gfc_internal_error ("require_atom(): bad atom type required");
1473 module_column = column;
1474 module_line = line;
1475 bad_module (p);
1480 /* Given a pointer to an mstring array, require that the current input
1481 be one of the strings in the array. We return the enum value. */
1483 static int
1484 find_enum (const mstring *m)
1486 int i;
1488 i = gfc_string2code (m, atom_name);
1489 if (i >= 0)
1490 return i;
1492 bad_module ("find_enum(): Enum not found");
1494 /* Not reached. */
1498 /* Read a string. The caller is responsible for freeing. */
1500 static char*
1501 read_string (void)
1503 char* p;
1504 require_atom (ATOM_STRING);
1505 p = atom_string;
1506 atom_string = NULL;
1507 return p;
1511 /**************** Module output subroutines ***************************/
1513 /* Output a character to a module file. */
1515 static void
1516 write_char (char out)
1518 if (gzputc (module_fp, out) == EOF)
1519 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1521 if (out != '\n')
1522 module_column++;
1523 else
1525 module_column = 1;
1526 module_line++;
1531 /* Write an atom to a module. The line wrapping isn't perfect, but it
1532 should work most of the time. This isn't that big of a deal, since
1533 the file really isn't meant to be read by people anyway. */
1535 static void
1536 write_atom (atom_type atom, const void *v)
1538 char buffer[20];
1539 int i, len;
1540 const char *p;
1542 switch (atom)
1544 case ATOM_STRING:
1545 case ATOM_NAME:
1546 p = (const char *) v;
1547 break;
1549 case ATOM_LPAREN:
1550 p = "(";
1551 break;
1553 case ATOM_RPAREN:
1554 p = ")";
1555 break;
1557 case ATOM_INTEGER:
1558 i = *((const int *) v);
1559 if (i < 0)
1560 gfc_internal_error ("write_atom(): Writing negative integer");
1562 sprintf (buffer, "%d", i);
1563 p = buffer;
1564 break;
1566 default:
1567 gfc_internal_error ("write_atom(): Trying to write dab atom");
1571 if(p == NULL || *p == '\0')
1572 len = 0;
1573 else
1574 len = strlen (p);
1576 if (atom != ATOM_RPAREN)
1578 if (module_column + len > 72)
1579 write_char ('\n');
1580 else
1583 if (last_atom != ATOM_LPAREN && module_column != 1)
1584 write_char (' ');
1588 if (atom == ATOM_STRING)
1589 write_char ('\'');
1591 while (p != NULL && *p)
1593 if (atom == ATOM_STRING && *p == '\'')
1594 write_char ('\'');
1595 write_char (*p++);
1598 if (atom == ATOM_STRING)
1599 write_char ('\'');
1601 last_atom = atom;
1606 /***************** Mid-level I/O subroutines *****************/
1608 /* These subroutines let their caller read or write atoms without
1609 caring about which of the two is actually happening. This lets a
1610 subroutine concentrate on the actual format of the data being
1611 written. */
1613 static void mio_expr (gfc_expr **);
1614 pointer_info *mio_symbol_ref (gfc_symbol **);
1615 pointer_info *mio_interface_rest (gfc_interface **);
1616 static void mio_symtree_ref (gfc_symtree **);
1618 /* Read or write an enumerated value. On writing, we return the input
1619 value for the convenience of callers. We avoid using an integer
1620 pointer because enums are sometimes inside bitfields. */
1622 static int
1623 mio_name (int t, const mstring *m)
1625 if (iomode == IO_OUTPUT)
1626 write_atom (ATOM_NAME, gfc_code2string (m, t));
1627 else
1629 require_atom (ATOM_NAME);
1630 t = find_enum (m);
1633 return t;
1636 /* Specialization of mio_name. */
1638 #define DECL_MIO_NAME(TYPE) \
1639 static inline TYPE \
1640 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1642 return (TYPE) mio_name ((int) t, m); \
1644 #define MIO_NAME(TYPE) mio_name_##TYPE
1646 static void
1647 mio_lparen (void)
1649 if (iomode == IO_OUTPUT)
1650 write_atom (ATOM_LPAREN, NULL);
1651 else
1652 require_atom (ATOM_LPAREN);
1656 static void
1657 mio_rparen (void)
1659 if (iomode == IO_OUTPUT)
1660 write_atom (ATOM_RPAREN, NULL);
1661 else
1662 require_atom (ATOM_RPAREN);
1666 static void
1667 mio_integer (int *ip)
1669 if (iomode == IO_OUTPUT)
1670 write_atom (ATOM_INTEGER, ip);
1671 else
1673 require_atom (ATOM_INTEGER);
1674 *ip = atom_int;
1679 /* Read or write a gfc_intrinsic_op value. */
1681 static void
1682 mio_intrinsic_op (gfc_intrinsic_op* op)
1684 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1685 if (iomode == IO_OUTPUT)
1687 int converted = (int) *op;
1688 write_atom (ATOM_INTEGER, &converted);
1690 else
1692 require_atom (ATOM_INTEGER);
1693 *op = (gfc_intrinsic_op) atom_int;
1698 /* Read or write a character pointer that points to a string on the heap. */
1700 static const char *
1701 mio_allocated_string (const char *s)
1703 if (iomode == IO_OUTPUT)
1705 write_atom (ATOM_STRING, s);
1706 return s;
1708 else
1710 require_atom (ATOM_STRING);
1711 return atom_string;
1716 /* Functions for quoting and unquoting strings. */
1718 static char *
1719 quote_string (const gfc_char_t *s, const size_t slength)
1721 const gfc_char_t *p;
1722 char *res, *q;
1723 size_t len = 0, i;
1725 /* Calculate the length we'll need: a backslash takes two ("\\"),
1726 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1727 for (p = s, i = 0; i < slength; p++, i++)
1729 if (*p == '\\')
1730 len += 2;
1731 else if (!gfc_wide_is_printable (*p))
1732 len += 10;
1733 else
1734 len++;
1737 q = res = XCNEWVEC (char, len + 1);
1738 for (p = s, i = 0; i < slength; p++, i++)
1740 if (*p == '\\')
1741 *q++ = '\\', *q++ = '\\';
1742 else if (!gfc_wide_is_printable (*p))
1744 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1745 (unsigned HOST_WIDE_INT) *p);
1746 q += 10;
1748 else
1749 *q++ = (unsigned char) *p;
1752 res[len] = '\0';
1753 return res;
1756 static gfc_char_t *
1757 unquote_string (const char *s)
1759 size_t len, i;
1760 const char *p;
1761 gfc_char_t *res;
1763 for (p = s, len = 0; *p; p++, len++)
1765 if (*p != '\\')
1766 continue;
1768 if (p[1] == '\\')
1769 p++;
1770 else if (p[1] == 'U')
1771 p += 9; /* That is a "\U????????". */
1772 else
1773 gfc_internal_error ("unquote_string(): got bad string");
1776 res = gfc_get_wide_string (len + 1);
1777 for (i = 0, p = s; i < len; i++, p++)
1779 gcc_assert (*p);
1781 if (*p != '\\')
1782 res[i] = (unsigned char) *p;
1783 else if (p[1] == '\\')
1785 res[i] = (unsigned char) '\\';
1786 p++;
1788 else
1790 /* We read the 8-digits hexadecimal constant that follows. */
1791 int j;
1792 unsigned n;
1793 gfc_char_t c = 0;
1795 gcc_assert (p[1] == 'U');
1796 for (j = 0; j < 8; j++)
1798 c = c << 4;
1799 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1800 c += n;
1803 res[i] = c;
1804 p += 9;
1808 res[len] = '\0';
1809 return res;
1813 /* Read or write a character pointer that points to a wide string on the
1814 heap, performing quoting/unquoting of nonprintable characters using the
1815 form \U???????? (where each ? is a hexadecimal digit).
1816 Length is the length of the string, only known and used in output mode. */
1818 static const gfc_char_t *
1819 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1821 if (iomode == IO_OUTPUT)
1823 char *quoted = quote_string (s, length);
1824 write_atom (ATOM_STRING, quoted);
1825 free (quoted);
1826 return s;
1828 else
1830 gfc_char_t *unquoted;
1832 require_atom (ATOM_STRING);
1833 unquoted = unquote_string (atom_string);
1834 free (atom_string);
1835 return unquoted;
1840 /* Read or write a string that is in static memory. */
1842 static void
1843 mio_pool_string (const char **stringp)
1845 /* TODO: one could write the string only once, and refer to it via a
1846 fixup pointer. */
1848 /* As a special case we have to deal with a NULL string. This
1849 happens for the 'module' member of 'gfc_symbol's that are not in a
1850 module. We read / write these as the empty string. */
1851 if (iomode == IO_OUTPUT)
1853 const char *p = *stringp == NULL ? "" : *stringp;
1854 write_atom (ATOM_STRING, p);
1856 else
1858 require_atom (ATOM_STRING);
1859 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1860 free (atom_string);
1865 /* Read or write a string that is inside of some already-allocated
1866 structure. */
1868 static void
1869 mio_internal_string (char *string)
1871 if (iomode == IO_OUTPUT)
1872 write_atom (ATOM_STRING, string);
1873 else
1875 require_atom (ATOM_STRING);
1876 strcpy (string, atom_string);
1877 free (atom_string);
1882 typedef enum
1883 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1884 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1885 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1886 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1887 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1888 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1889 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1890 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1891 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1892 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1893 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET
1895 ab_attribute;
1897 static const mstring attr_bits[] =
1899 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1900 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1901 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1902 minit ("DIMENSION", AB_DIMENSION),
1903 minit ("CODIMENSION", AB_CODIMENSION),
1904 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1905 minit ("EXTERNAL", AB_EXTERNAL),
1906 minit ("INTRINSIC", AB_INTRINSIC),
1907 minit ("OPTIONAL", AB_OPTIONAL),
1908 minit ("POINTER", AB_POINTER),
1909 minit ("VOLATILE", AB_VOLATILE),
1910 minit ("TARGET", AB_TARGET),
1911 minit ("THREADPRIVATE", AB_THREADPRIVATE),
1912 minit ("DUMMY", AB_DUMMY),
1913 minit ("RESULT", AB_RESULT),
1914 minit ("DATA", AB_DATA),
1915 minit ("IN_NAMELIST", AB_IN_NAMELIST),
1916 minit ("IN_COMMON", AB_IN_COMMON),
1917 minit ("FUNCTION", AB_FUNCTION),
1918 minit ("SUBROUTINE", AB_SUBROUTINE),
1919 minit ("SEQUENCE", AB_SEQUENCE),
1920 minit ("ELEMENTAL", AB_ELEMENTAL),
1921 minit ("PURE", AB_PURE),
1922 minit ("RECURSIVE", AB_RECURSIVE),
1923 minit ("GENERIC", AB_GENERIC),
1924 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
1925 minit ("CRAY_POINTER", AB_CRAY_POINTER),
1926 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
1927 minit ("IS_BIND_C", AB_IS_BIND_C),
1928 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
1929 minit ("IS_ISO_C", AB_IS_ISO_C),
1930 minit ("VALUE", AB_VALUE),
1931 minit ("ALLOC_COMP", AB_ALLOC_COMP),
1932 minit ("COARRAY_COMP", AB_COARRAY_COMP),
1933 minit ("LOCK_COMP", AB_LOCK_COMP),
1934 minit ("POINTER_COMP", AB_POINTER_COMP),
1935 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
1936 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
1937 minit ("ZERO_COMP", AB_ZERO_COMP),
1938 minit ("PROTECTED", AB_PROTECTED),
1939 minit ("ABSTRACT", AB_ABSTRACT),
1940 minit ("IS_CLASS", AB_IS_CLASS),
1941 minit ("PROCEDURE", AB_PROCEDURE),
1942 minit ("PROC_POINTER", AB_PROC_POINTER),
1943 minit ("VTYPE", AB_VTYPE),
1944 minit ("VTAB", AB_VTAB),
1945 minit ("CLASS_POINTER", AB_CLASS_POINTER),
1946 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
1947 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
1948 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
1949 minit (NULL, -1)
1952 /* For binding attributes. */
1953 static const mstring binding_passing[] =
1955 minit ("PASS", 0),
1956 minit ("NOPASS", 1),
1957 minit (NULL, -1)
1959 static const mstring binding_overriding[] =
1961 minit ("OVERRIDABLE", 0),
1962 minit ("NON_OVERRIDABLE", 1),
1963 minit ("DEFERRED", 2),
1964 minit (NULL, -1)
1966 static const mstring binding_generic[] =
1968 minit ("SPECIFIC", 0),
1969 minit ("GENERIC", 1),
1970 minit (NULL, -1)
1972 static const mstring binding_ppc[] =
1974 minit ("NO_PPC", 0),
1975 minit ("PPC", 1),
1976 minit (NULL, -1)
1979 /* Specialization of mio_name. */
1980 DECL_MIO_NAME (ab_attribute)
1981 DECL_MIO_NAME (ar_type)
1982 DECL_MIO_NAME (array_type)
1983 DECL_MIO_NAME (bt)
1984 DECL_MIO_NAME (expr_t)
1985 DECL_MIO_NAME (gfc_access)
1986 DECL_MIO_NAME (gfc_intrinsic_op)
1987 DECL_MIO_NAME (ifsrc)
1988 DECL_MIO_NAME (save_state)
1989 DECL_MIO_NAME (procedure_type)
1990 DECL_MIO_NAME (ref_type)
1991 DECL_MIO_NAME (sym_flavor)
1992 DECL_MIO_NAME (sym_intent)
1993 #undef DECL_MIO_NAME
1995 /* Symbol attributes are stored in list with the first three elements
1996 being the enumerated fields, while the remaining elements (if any)
1997 indicate the individual attribute bits. The access field is not
1998 saved-- it controls what symbols are exported when a module is
1999 written. */
2001 static void
2002 mio_symbol_attribute (symbol_attribute *attr)
2004 atom_type t;
2005 unsigned ext_attr,extension_level;
2007 mio_lparen ();
2009 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2010 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2011 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2012 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2013 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2015 ext_attr = attr->ext_attr;
2016 mio_integer ((int *) &ext_attr);
2017 attr->ext_attr = ext_attr;
2019 extension_level = attr->extension;
2020 mio_integer ((int *) &extension_level);
2021 attr->extension = extension_level;
2023 if (iomode == IO_OUTPUT)
2025 if (attr->allocatable)
2026 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2027 if (attr->artificial)
2028 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2029 if (attr->asynchronous)
2030 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2031 if (attr->dimension)
2032 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2033 if (attr->codimension)
2034 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2035 if (attr->contiguous)
2036 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2037 if (attr->external)
2038 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2039 if (attr->intrinsic)
2040 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2041 if (attr->optional)
2042 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2043 if (attr->pointer)
2044 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2045 if (attr->class_pointer)
2046 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2047 if (attr->is_protected)
2048 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2049 if (attr->value)
2050 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2051 if (attr->volatile_)
2052 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2053 if (attr->target)
2054 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2055 if (attr->threadprivate)
2056 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2057 if (attr->dummy)
2058 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2059 if (attr->result)
2060 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2061 /* We deliberately don't preserve the "entry" flag. */
2063 if (attr->data)
2064 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2065 if (attr->in_namelist)
2066 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2067 if (attr->in_common)
2068 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2070 if (attr->function)
2071 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2072 if (attr->subroutine)
2073 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2074 if (attr->generic)
2075 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2076 if (attr->abstract)
2077 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2079 if (attr->sequence)
2080 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2081 if (attr->elemental)
2082 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2083 if (attr->pure)
2084 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2085 if (attr->implicit_pure)
2086 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2087 if (attr->unlimited_polymorphic)
2088 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2089 if (attr->recursive)
2090 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2091 if (attr->always_explicit)
2092 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2093 if (attr->cray_pointer)
2094 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2095 if (attr->cray_pointee)
2096 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2097 if (attr->is_bind_c)
2098 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2099 if (attr->is_c_interop)
2100 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2101 if (attr->is_iso_c)
2102 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2103 if (attr->alloc_comp)
2104 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2105 if (attr->pointer_comp)
2106 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2107 if (attr->proc_pointer_comp)
2108 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2109 if (attr->private_comp)
2110 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2111 if (attr->coarray_comp)
2112 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2113 if (attr->lock_comp)
2114 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2115 if (attr->zero_comp)
2116 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2117 if (attr->is_class)
2118 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2119 if (attr->procedure)
2120 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2121 if (attr->proc_pointer)
2122 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2123 if (attr->vtype)
2124 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2125 if (attr->vtab)
2126 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2127 if (attr->omp_declare_target)
2128 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2130 mio_rparen ();
2133 else
2135 for (;;)
2137 t = parse_atom ();
2138 if (t == ATOM_RPAREN)
2139 break;
2140 if (t != ATOM_NAME)
2141 bad_module ("Expected attribute bit name");
2143 switch ((ab_attribute) find_enum (attr_bits))
2145 case AB_ALLOCATABLE:
2146 attr->allocatable = 1;
2147 break;
2148 case AB_ARTIFICIAL:
2149 attr->artificial = 1;
2150 break;
2151 case AB_ASYNCHRONOUS:
2152 attr->asynchronous = 1;
2153 break;
2154 case AB_DIMENSION:
2155 attr->dimension = 1;
2156 break;
2157 case AB_CODIMENSION:
2158 attr->codimension = 1;
2159 break;
2160 case AB_CONTIGUOUS:
2161 attr->contiguous = 1;
2162 break;
2163 case AB_EXTERNAL:
2164 attr->external = 1;
2165 break;
2166 case AB_INTRINSIC:
2167 attr->intrinsic = 1;
2168 break;
2169 case AB_OPTIONAL:
2170 attr->optional = 1;
2171 break;
2172 case AB_POINTER:
2173 attr->pointer = 1;
2174 break;
2175 case AB_CLASS_POINTER:
2176 attr->class_pointer = 1;
2177 break;
2178 case AB_PROTECTED:
2179 attr->is_protected = 1;
2180 break;
2181 case AB_VALUE:
2182 attr->value = 1;
2183 break;
2184 case AB_VOLATILE:
2185 attr->volatile_ = 1;
2186 break;
2187 case AB_TARGET:
2188 attr->target = 1;
2189 break;
2190 case AB_THREADPRIVATE:
2191 attr->threadprivate = 1;
2192 break;
2193 case AB_DUMMY:
2194 attr->dummy = 1;
2195 break;
2196 case AB_RESULT:
2197 attr->result = 1;
2198 break;
2199 case AB_DATA:
2200 attr->data = 1;
2201 break;
2202 case AB_IN_NAMELIST:
2203 attr->in_namelist = 1;
2204 break;
2205 case AB_IN_COMMON:
2206 attr->in_common = 1;
2207 break;
2208 case AB_FUNCTION:
2209 attr->function = 1;
2210 break;
2211 case AB_SUBROUTINE:
2212 attr->subroutine = 1;
2213 break;
2214 case AB_GENERIC:
2215 attr->generic = 1;
2216 break;
2217 case AB_ABSTRACT:
2218 attr->abstract = 1;
2219 break;
2220 case AB_SEQUENCE:
2221 attr->sequence = 1;
2222 break;
2223 case AB_ELEMENTAL:
2224 attr->elemental = 1;
2225 break;
2226 case AB_PURE:
2227 attr->pure = 1;
2228 break;
2229 case AB_IMPLICIT_PURE:
2230 attr->implicit_pure = 1;
2231 break;
2232 case AB_UNLIMITED_POLY:
2233 attr->unlimited_polymorphic = 1;
2234 break;
2235 case AB_RECURSIVE:
2236 attr->recursive = 1;
2237 break;
2238 case AB_ALWAYS_EXPLICIT:
2239 attr->always_explicit = 1;
2240 break;
2241 case AB_CRAY_POINTER:
2242 attr->cray_pointer = 1;
2243 break;
2244 case AB_CRAY_POINTEE:
2245 attr->cray_pointee = 1;
2246 break;
2247 case AB_IS_BIND_C:
2248 attr->is_bind_c = 1;
2249 break;
2250 case AB_IS_C_INTEROP:
2251 attr->is_c_interop = 1;
2252 break;
2253 case AB_IS_ISO_C:
2254 attr->is_iso_c = 1;
2255 break;
2256 case AB_ALLOC_COMP:
2257 attr->alloc_comp = 1;
2258 break;
2259 case AB_COARRAY_COMP:
2260 attr->coarray_comp = 1;
2261 break;
2262 case AB_LOCK_COMP:
2263 attr->lock_comp = 1;
2264 break;
2265 case AB_POINTER_COMP:
2266 attr->pointer_comp = 1;
2267 break;
2268 case AB_PROC_POINTER_COMP:
2269 attr->proc_pointer_comp = 1;
2270 break;
2271 case AB_PRIVATE_COMP:
2272 attr->private_comp = 1;
2273 break;
2274 case AB_ZERO_COMP:
2275 attr->zero_comp = 1;
2276 break;
2277 case AB_IS_CLASS:
2278 attr->is_class = 1;
2279 break;
2280 case AB_PROCEDURE:
2281 attr->procedure = 1;
2282 break;
2283 case AB_PROC_POINTER:
2284 attr->proc_pointer = 1;
2285 break;
2286 case AB_VTYPE:
2287 attr->vtype = 1;
2288 break;
2289 case AB_VTAB:
2290 attr->vtab = 1;
2291 break;
2292 case AB_OMP_DECLARE_TARGET:
2293 attr->omp_declare_target = 1;
2294 break;
2301 static const mstring bt_types[] = {
2302 minit ("INTEGER", BT_INTEGER),
2303 minit ("REAL", BT_REAL),
2304 minit ("COMPLEX", BT_COMPLEX),
2305 minit ("LOGICAL", BT_LOGICAL),
2306 minit ("CHARACTER", BT_CHARACTER),
2307 minit ("DERIVED", BT_DERIVED),
2308 minit ("CLASS", BT_CLASS),
2309 minit ("PROCEDURE", BT_PROCEDURE),
2310 minit ("UNKNOWN", BT_UNKNOWN),
2311 minit ("VOID", BT_VOID),
2312 minit ("ASSUMED", BT_ASSUMED),
2313 minit (NULL, -1)
2317 static void
2318 mio_charlen (gfc_charlen **clp)
2320 gfc_charlen *cl;
2322 mio_lparen ();
2324 if (iomode == IO_OUTPUT)
2326 cl = *clp;
2327 if (cl != NULL)
2328 mio_expr (&cl->length);
2330 else
2332 if (peek_atom () != ATOM_RPAREN)
2334 cl = gfc_new_charlen (gfc_current_ns, NULL);
2335 mio_expr (&cl->length);
2336 *clp = cl;
2340 mio_rparen ();
2344 /* See if a name is a generated name. */
2346 static int
2347 check_unique_name (const char *name)
2349 return *name == '@';
2353 static void
2354 mio_typespec (gfc_typespec *ts)
2356 mio_lparen ();
2358 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2360 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2361 mio_integer (&ts->kind);
2362 else
2363 mio_symbol_ref (&ts->u.derived);
2365 mio_symbol_ref (&ts->interface);
2367 /* Add info for C interop and is_iso_c. */
2368 mio_integer (&ts->is_c_interop);
2369 mio_integer (&ts->is_iso_c);
2371 /* If the typespec is for an identifier either from iso_c_binding, or
2372 a constant that was initialized to an identifier from it, use the
2373 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2374 if (ts->is_iso_c)
2375 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2376 else
2377 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2379 if (ts->type != BT_CHARACTER)
2381 /* ts->u.cl is only valid for BT_CHARACTER. */
2382 mio_lparen ();
2383 mio_rparen ();
2385 else
2386 mio_charlen (&ts->u.cl);
2388 /* So as not to disturb the existing API, use an ATOM_NAME to
2389 transmit deferred characteristic for characters (F2003). */
2390 if (iomode == IO_OUTPUT)
2392 if (ts->type == BT_CHARACTER && ts->deferred)
2393 write_atom (ATOM_NAME, "DEFERRED_CL");
2395 else if (peek_atom () != ATOM_RPAREN)
2397 if (parse_atom () != ATOM_NAME)
2398 bad_module ("Expected string");
2399 ts->deferred = 1;
2402 mio_rparen ();
2406 static const mstring array_spec_types[] = {
2407 minit ("EXPLICIT", AS_EXPLICIT),
2408 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2409 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2410 minit ("DEFERRED", AS_DEFERRED),
2411 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2412 minit (NULL, -1)
2416 static void
2417 mio_array_spec (gfc_array_spec **asp)
2419 gfc_array_spec *as;
2420 int i;
2422 mio_lparen ();
2424 if (iomode == IO_OUTPUT)
2426 int rank;
2428 if (*asp == NULL)
2429 goto done;
2430 as = *asp;
2432 /* mio_integer expects nonnegative values. */
2433 rank = as->rank > 0 ? as->rank : 0;
2434 mio_integer (&rank);
2436 else
2438 if (peek_atom () == ATOM_RPAREN)
2440 *asp = NULL;
2441 goto done;
2444 *asp = as = gfc_get_array_spec ();
2445 mio_integer (&as->rank);
2448 mio_integer (&as->corank);
2449 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2451 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2452 as->rank = -1;
2453 if (iomode == IO_INPUT && as->corank)
2454 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2456 if (as->rank + as->corank > 0)
2457 for (i = 0; i < as->rank + as->corank; i++)
2459 mio_expr (&as->lower[i]);
2460 mio_expr (&as->upper[i]);
2463 done:
2464 mio_rparen ();
2468 /* Given a pointer to an array reference structure (which lives in a
2469 gfc_ref structure), find the corresponding array specification
2470 structure. Storing the pointer in the ref structure doesn't quite
2471 work when loading from a module. Generating code for an array
2472 reference also needs more information than just the array spec. */
2474 static const mstring array_ref_types[] = {
2475 minit ("FULL", AR_FULL),
2476 minit ("ELEMENT", AR_ELEMENT),
2477 minit ("SECTION", AR_SECTION),
2478 minit (NULL, -1)
2482 static void
2483 mio_array_ref (gfc_array_ref *ar)
2485 int i;
2487 mio_lparen ();
2488 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2489 mio_integer (&ar->dimen);
2491 switch (ar->type)
2493 case AR_FULL:
2494 break;
2496 case AR_ELEMENT:
2497 for (i = 0; i < ar->dimen; i++)
2498 mio_expr (&ar->start[i]);
2500 break;
2502 case AR_SECTION:
2503 for (i = 0; i < ar->dimen; i++)
2505 mio_expr (&ar->start[i]);
2506 mio_expr (&ar->end[i]);
2507 mio_expr (&ar->stride[i]);
2510 break;
2512 case AR_UNKNOWN:
2513 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2516 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2517 we can't call mio_integer directly. Instead loop over each element
2518 and cast it to/from an integer. */
2519 if (iomode == IO_OUTPUT)
2521 for (i = 0; i < ar->dimen; i++)
2523 int tmp = (int)ar->dimen_type[i];
2524 write_atom (ATOM_INTEGER, &tmp);
2527 else
2529 for (i = 0; i < ar->dimen; i++)
2531 require_atom (ATOM_INTEGER);
2532 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2536 if (iomode == IO_INPUT)
2538 ar->where = gfc_current_locus;
2540 for (i = 0; i < ar->dimen; i++)
2541 ar->c_where[i] = gfc_current_locus;
2544 mio_rparen ();
2548 /* Saves or restores a pointer. The pointer is converted back and
2549 forth from an integer. We return the pointer_info pointer so that
2550 the caller can take additional action based on the pointer type. */
2552 static pointer_info *
2553 mio_pointer_ref (void *gp)
2555 pointer_info *p;
2557 if (iomode == IO_OUTPUT)
2559 p = get_pointer (*((char **) gp));
2560 write_atom (ATOM_INTEGER, &p->integer);
2562 else
2564 require_atom (ATOM_INTEGER);
2565 p = add_fixup (atom_int, gp);
2568 return p;
2572 /* Save and load references to components that occur within
2573 expressions. We have to describe these references by a number and
2574 by name. The number is necessary for forward references during
2575 reading, and the name is necessary if the symbol already exists in
2576 the namespace and is not loaded again. */
2578 static void
2579 mio_component_ref (gfc_component **cp)
2581 pointer_info *p;
2583 p = mio_pointer_ref (cp);
2584 if (p->type == P_UNKNOWN)
2585 p->type = P_COMPONENT;
2589 static void mio_namespace_ref (gfc_namespace **nsp);
2590 static void mio_formal_arglist (gfc_formal_arglist **formal);
2591 static void mio_typebound_proc (gfc_typebound_proc** proc);
2593 static void
2594 mio_component (gfc_component *c, int vtype)
2596 pointer_info *p;
2597 int n;
2599 mio_lparen ();
2601 if (iomode == IO_OUTPUT)
2603 p = get_pointer (c);
2604 mio_integer (&p->integer);
2606 else
2608 mio_integer (&n);
2609 p = get_integer (n);
2610 associate_integer_pointer (p, c);
2613 if (p->type == P_UNKNOWN)
2614 p->type = P_COMPONENT;
2616 mio_pool_string (&c->name);
2617 mio_typespec (&c->ts);
2618 mio_array_spec (&c->as);
2620 mio_symbol_attribute (&c->attr);
2621 if (c->ts.type == BT_CLASS)
2622 c->attr.class_ok = 1;
2623 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2625 if (!vtype || strcmp (c->name, "_final") == 0
2626 || strcmp (c->name, "_hash") == 0)
2627 mio_expr (&c->initializer);
2629 if (c->attr.proc_pointer)
2630 mio_typebound_proc (&c->tb);
2632 mio_rparen ();
2636 static void
2637 mio_component_list (gfc_component **cp, int vtype)
2639 gfc_component *c, *tail;
2641 mio_lparen ();
2643 if (iomode == IO_OUTPUT)
2645 for (c = *cp; c; c = c->next)
2646 mio_component (c, vtype);
2648 else
2650 *cp = NULL;
2651 tail = NULL;
2653 for (;;)
2655 if (peek_atom () == ATOM_RPAREN)
2656 break;
2658 c = gfc_get_component ();
2659 mio_component (c, vtype);
2661 if (tail == NULL)
2662 *cp = c;
2663 else
2664 tail->next = c;
2666 tail = c;
2670 mio_rparen ();
2674 static void
2675 mio_actual_arg (gfc_actual_arglist *a)
2677 mio_lparen ();
2678 mio_pool_string (&a->name);
2679 mio_expr (&a->expr);
2680 mio_rparen ();
2684 static void
2685 mio_actual_arglist (gfc_actual_arglist **ap)
2687 gfc_actual_arglist *a, *tail;
2689 mio_lparen ();
2691 if (iomode == IO_OUTPUT)
2693 for (a = *ap; a; a = a->next)
2694 mio_actual_arg (a);
2697 else
2699 tail = NULL;
2701 for (;;)
2703 if (peek_atom () != ATOM_LPAREN)
2704 break;
2706 a = gfc_get_actual_arglist ();
2708 if (tail == NULL)
2709 *ap = a;
2710 else
2711 tail->next = a;
2713 tail = a;
2714 mio_actual_arg (a);
2718 mio_rparen ();
2722 /* Read and write formal argument lists. */
2724 static void
2725 mio_formal_arglist (gfc_formal_arglist **formal)
2727 gfc_formal_arglist *f, *tail;
2729 mio_lparen ();
2731 if (iomode == IO_OUTPUT)
2733 for (f = *formal; f; f = f->next)
2734 mio_symbol_ref (&f->sym);
2736 else
2738 *formal = tail = NULL;
2740 while (peek_atom () != ATOM_RPAREN)
2742 f = gfc_get_formal_arglist ();
2743 mio_symbol_ref (&f->sym);
2745 if (*formal == NULL)
2746 *formal = f;
2747 else
2748 tail->next = f;
2750 tail = f;
2754 mio_rparen ();
2758 /* Save or restore a reference to a symbol node. */
2760 pointer_info *
2761 mio_symbol_ref (gfc_symbol **symp)
2763 pointer_info *p;
2765 p = mio_pointer_ref (symp);
2766 if (p->type == P_UNKNOWN)
2767 p->type = P_SYMBOL;
2769 if (iomode == IO_OUTPUT)
2771 if (p->u.wsym.state == UNREFERENCED)
2772 p->u.wsym.state = NEEDS_WRITE;
2774 else
2776 if (p->u.rsym.state == UNUSED)
2777 p->u.rsym.state = NEEDED;
2779 return p;
2783 /* Save or restore a reference to a symtree node. */
2785 static void
2786 mio_symtree_ref (gfc_symtree **stp)
2788 pointer_info *p;
2789 fixup_t *f;
2791 if (iomode == IO_OUTPUT)
2792 mio_symbol_ref (&(*stp)->n.sym);
2793 else
2795 require_atom (ATOM_INTEGER);
2796 p = get_integer (atom_int);
2798 /* An unused equivalence member; make a symbol and a symtree
2799 for it. */
2800 if (in_load_equiv && p->u.rsym.symtree == NULL)
2802 /* Since this is not used, it must have a unique name. */
2803 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2805 /* Make the symbol. */
2806 if (p->u.rsym.sym == NULL)
2808 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2809 gfc_current_ns);
2810 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2813 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2814 p->u.rsym.symtree->n.sym->refs++;
2815 p->u.rsym.referenced = 1;
2817 /* If the symbol is PRIVATE and in COMMON, load_commons will
2818 generate a fixup symbol, which must be associated. */
2819 if (p->fixup)
2820 resolve_fixups (p->fixup, p->u.rsym.sym);
2821 p->fixup = NULL;
2824 if (p->type == P_UNKNOWN)
2825 p->type = P_SYMBOL;
2827 if (p->u.rsym.state == UNUSED)
2828 p->u.rsym.state = NEEDED;
2830 if (p->u.rsym.symtree != NULL)
2832 *stp = p->u.rsym.symtree;
2834 else
2836 f = XCNEW (fixup_t);
2838 f->next = p->u.rsym.stfixup;
2839 p->u.rsym.stfixup = f;
2841 f->pointer = (void **) stp;
2847 static void
2848 mio_iterator (gfc_iterator **ip)
2850 gfc_iterator *iter;
2852 mio_lparen ();
2854 if (iomode == IO_OUTPUT)
2856 if (*ip == NULL)
2857 goto done;
2859 else
2861 if (peek_atom () == ATOM_RPAREN)
2863 *ip = NULL;
2864 goto done;
2867 *ip = gfc_get_iterator ();
2870 iter = *ip;
2872 mio_expr (&iter->var);
2873 mio_expr (&iter->start);
2874 mio_expr (&iter->end);
2875 mio_expr (&iter->step);
2877 done:
2878 mio_rparen ();
2882 static void
2883 mio_constructor (gfc_constructor_base *cp)
2885 gfc_constructor *c;
2887 mio_lparen ();
2889 if (iomode == IO_OUTPUT)
2891 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2893 mio_lparen ();
2894 mio_expr (&c->expr);
2895 mio_iterator (&c->iterator);
2896 mio_rparen ();
2899 else
2901 while (peek_atom () != ATOM_RPAREN)
2903 c = gfc_constructor_append_expr (cp, NULL, NULL);
2905 mio_lparen ();
2906 mio_expr (&c->expr);
2907 mio_iterator (&c->iterator);
2908 mio_rparen ();
2912 mio_rparen ();
2916 static const mstring ref_types[] = {
2917 minit ("ARRAY", REF_ARRAY),
2918 minit ("COMPONENT", REF_COMPONENT),
2919 minit ("SUBSTRING", REF_SUBSTRING),
2920 minit (NULL, -1)
2924 static void
2925 mio_ref (gfc_ref **rp)
2927 gfc_ref *r;
2929 mio_lparen ();
2931 r = *rp;
2932 r->type = MIO_NAME (ref_type) (r->type, ref_types);
2934 switch (r->type)
2936 case REF_ARRAY:
2937 mio_array_ref (&r->u.ar);
2938 break;
2940 case REF_COMPONENT:
2941 mio_symbol_ref (&r->u.c.sym);
2942 mio_component_ref (&r->u.c.component);
2943 break;
2945 case REF_SUBSTRING:
2946 mio_expr (&r->u.ss.start);
2947 mio_expr (&r->u.ss.end);
2948 mio_charlen (&r->u.ss.length);
2949 break;
2952 mio_rparen ();
2956 static void
2957 mio_ref_list (gfc_ref **rp)
2959 gfc_ref *ref, *head, *tail;
2961 mio_lparen ();
2963 if (iomode == IO_OUTPUT)
2965 for (ref = *rp; ref; ref = ref->next)
2966 mio_ref (&ref);
2968 else
2970 head = tail = NULL;
2972 while (peek_atom () != ATOM_RPAREN)
2974 if (head == NULL)
2975 head = tail = gfc_get_ref ();
2976 else
2978 tail->next = gfc_get_ref ();
2979 tail = tail->next;
2982 mio_ref (&tail);
2985 *rp = head;
2988 mio_rparen ();
2992 /* Read and write an integer value. */
2994 static void
2995 mio_gmp_integer (mpz_t *integer)
2997 char *p;
2999 if (iomode == IO_INPUT)
3001 if (parse_atom () != ATOM_STRING)
3002 bad_module ("Expected integer string");
3004 mpz_init (*integer);
3005 if (mpz_set_str (*integer, atom_string, 10))
3006 bad_module ("Error converting integer");
3008 free (atom_string);
3010 else
3012 p = mpz_get_str (NULL, 10, *integer);
3013 write_atom (ATOM_STRING, p);
3014 free (p);
3019 static void
3020 mio_gmp_real (mpfr_t *real)
3022 mp_exp_t exponent;
3023 char *p;
3025 if (iomode == IO_INPUT)
3027 if (parse_atom () != ATOM_STRING)
3028 bad_module ("Expected real string");
3030 mpfr_init (*real);
3031 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3032 free (atom_string);
3034 else
3036 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3038 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3040 write_atom (ATOM_STRING, p);
3041 free (p);
3042 return;
3045 atom_string = XCNEWVEC (char, strlen (p) + 20);
3047 sprintf (atom_string, "0.%s@%ld", p, exponent);
3049 /* Fix negative numbers. */
3050 if (atom_string[2] == '-')
3052 atom_string[0] = '-';
3053 atom_string[1] = '0';
3054 atom_string[2] = '.';
3057 write_atom (ATOM_STRING, atom_string);
3059 free (atom_string);
3060 free (p);
3065 /* Save and restore the shape of an array constructor. */
3067 static void
3068 mio_shape (mpz_t **pshape, int rank)
3070 mpz_t *shape;
3071 atom_type t;
3072 int n;
3074 /* A NULL shape is represented by (). */
3075 mio_lparen ();
3077 if (iomode == IO_OUTPUT)
3079 shape = *pshape;
3080 if (!shape)
3082 mio_rparen ();
3083 return;
3086 else
3088 t = peek_atom ();
3089 if (t == ATOM_RPAREN)
3091 *pshape = NULL;
3092 mio_rparen ();
3093 return;
3096 shape = gfc_get_shape (rank);
3097 *pshape = shape;
3100 for (n = 0; n < rank; n++)
3101 mio_gmp_integer (&shape[n]);
3103 mio_rparen ();
3107 static const mstring expr_types[] = {
3108 minit ("OP", EXPR_OP),
3109 minit ("FUNCTION", EXPR_FUNCTION),
3110 minit ("CONSTANT", EXPR_CONSTANT),
3111 minit ("VARIABLE", EXPR_VARIABLE),
3112 minit ("SUBSTRING", EXPR_SUBSTRING),
3113 minit ("STRUCTURE", EXPR_STRUCTURE),
3114 minit ("ARRAY", EXPR_ARRAY),
3115 minit ("NULL", EXPR_NULL),
3116 minit ("COMPCALL", EXPR_COMPCALL),
3117 minit (NULL, -1)
3120 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3121 generic operators, not in expressions. INTRINSIC_USER is also
3122 replaced by the correct function name by the time we see it. */
3124 static const mstring intrinsics[] =
3126 minit ("UPLUS", INTRINSIC_UPLUS),
3127 minit ("UMINUS", INTRINSIC_UMINUS),
3128 minit ("PLUS", INTRINSIC_PLUS),
3129 minit ("MINUS", INTRINSIC_MINUS),
3130 minit ("TIMES", INTRINSIC_TIMES),
3131 minit ("DIVIDE", INTRINSIC_DIVIDE),
3132 minit ("POWER", INTRINSIC_POWER),
3133 minit ("CONCAT", INTRINSIC_CONCAT),
3134 minit ("AND", INTRINSIC_AND),
3135 minit ("OR", INTRINSIC_OR),
3136 minit ("EQV", INTRINSIC_EQV),
3137 minit ("NEQV", INTRINSIC_NEQV),
3138 minit ("EQ_SIGN", INTRINSIC_EQ),
3139 minit ("EQ", INTRINSIC_EQ_OS),
3140 minit ("NE_SIGN", INTRINSIC_NE),
3141 minit ("NE", INTRINSIC_NE_OS),
3142 minit ("GT_SIGN", INTRINSIC_GT),
3143 minit ("GT", INTRINSIC_GT_OS),
3144 minit ("GE_SIGN", INTRINSIC_GE),
3145 minit ("GE", INTRINSIC_GE_OS),
3146 minit ("LT_SIGN", INTRINSIC_LT),
3147 minit ("LT", INTRINSIC_LT_OS),
3148 minit ("LE_SIGN", INTRINSIC_LE),
3149 minit ("LE", INTRINSIC_LE_OS),
3150 minit ("NOT", INTRINSIC_NOT),
3151 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3152 minit ("USER", INTRINSIC_USER),
3153 minit (NULL, -1)
3157 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3159 static void
3160 fix_mio_expr (gfc_expr *e)
3162 gfc_symtree *ns_st = NULL;
3163 const char *fname;
3165 if (iomode != IO_OUTPUT)
3166 return;
3168 if (e->symtree)
3170 /* If this is a symtree for a symbol that came from a contained module
3171 namespace, it has a unique name and we should look in the current
3172 namespace to see if the required, non-contained symbol is available
3173 yet. If so, the latter should be written. */
3174 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3176 const char *name = e->symtree->n.sym->name;
3177 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3178 name = dt_upper_string (name);
3179 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3182 /* On the other hand, if the existing symbol is the module name or the
3183 new symbol is a dummy argument, do not do the promotion. */
3184 if (ns_st && ns_st->n.sym
3185 && ns_st->n.sym->attr.flavor != FL_MODULE
3186 && !e->symtree->n.sym->attr.dummy)
3187 e->symtree = ns_st;
3189 else if (e->expr_type == EXPR_FUNCTION
3190 && (e->value.function.name || e->value.function.isym))
3192 gfc_symbol *sym;
3194 /* In some circumstances, a function used in an initialization
3195 expression, in one use associated module, can fail to be
3196 coupled to its symtree when used in a specification
3197 expression in another module. */
3198 fname = e->value.function.esym ? e->value.function.esym->name
3199 : e->value.function.isym->name;
3200 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3202 if (e->symtree)
3203 return;
3205 /* This is probably a reference to a private procedure from another
3206 module. To prevent a segfault, make a generic with no specific
3207 instances. If this module is used, without the required
3208 specific coming from somewhere, the appropriate error message
3209 is issued. */
3210 gfc_get_symbol (fname, gfc_current_ns, &sym);
3211 sym->attr.flavor = FL_PROCEDURE;
3212 sym->attr.generic = 1;
3213 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3214 gfc_commit_symbol (sym);
3219 /* Read and write expressions. The form "()" is allowed to indicate a
3220 NULL expression. */
3222 static void
3223 mio_expr (gfc_expr **ep)
3225 gfc_expr *e;
3226 atom_type t;
3227 int flag;
3229 mio_lparen ();
3231 if (iomode == IO_OUTPUT)
3233 if (*ep == NULL)
3235 mio_rparen ();
3236 return;
3239 e = *ep;
3240 MIO_NAME (expr_t) (e->expr_type, expr_types);
3242 else
3244 t = parse_atom ();
3245 if (t == ATOM_RPAREN)
3247 *ep = NULL;
3248 return;
3251 if (t != ATOM_NAME)
3252 bad_module ("Expected expression type");
3254 e = *ep = gfc_get_expr ();
3255 e->where = gfc_current_locus;
3256 e->expr_type = (expr_t) find_enum (expr_types);
3259 mio_typespec (&e->ts);
3260 mio_integer (&e->rank);
3262 fix_mio_expr (e);
3264 switch (e->expr_type)
3266 case EXPR_OP:
3267 e->value.op.op
3268 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3270 switch (e->value.op.op)
3272 case INTRINSIC_UPLUS:
3273 case INTRINSIC_UMINUS:
3274 case INTRINSIC_NOT:
3275 case INTRINSIC_PARENTHESES:
3276 mio_expr (&e->value.op.op1);
3277 break;
3279 case INTRINSIC_PLUS:
3280 case INTRINSIC_MINUS:
3281 case INTRINSIC_TIMES:
3282 case INTRINSIC_DIVIDE:
3283 case INTRINSIC_POWER:
3284 case INTRINSIC_CONCAT:
3285 case INTRINSIC_AND:
3286 case INTRINSIC_OR:
3287 case INTRINSIC_EQV:
3288 case INTRINSIC_NEQV:
3289 case INTRINSIC_EQ:
3290 case INTRINSIC_EQ_OS:
3291 case INTRINSIC_NE:
3292 case INTRINSIC_NE_OS:
3293 case INTRINSIC_GT:
3294 case INTRINSIC_GT_OS:
3295 case INTRINSIC_GE:
3296 case INTRINSIC_GE_OS:
3297 case INTRINSIC_LT:
3298 case INTRINSIC_LT_OS:
3299 case INTRINSIC_LE:
3300 case INTRINSIC_LE_OS:
3301 mio_expr (&e->value.op.op1);
3302 mio_expr (&e->value.op.op2);
3303 break;
3305 case INTRINSIC_USER:
3306 /* INTRINSIC_USER should not appear in resolved expressions,
3307 though for UDRs we need to stream unresolved ones. */
3308 if (iomode == IO_OUTPUT)
3309 write_atom (ATOM_STRING, e->value.op.uop->name);
3310 else
3312 char *name = read_string ();
3313 const char *uop_name = find_use_name (name, true);
3314 if (uop_name == NULL)
3316 size_t len = strlen (name);
3317 char *name2 = XCNEWVEC (char, len + 2);
3318 memcpy (name2, name, len);
3319 name2[len] = ' ';
3320 name2[len + 1] = '\0';
3321 free (name);
3322 uop_name = name = name2;
3324 e->value.op.uop = gfc_get_uop (uop_name);
3325 free (name);
3327 mio_expr (&e->value.op.op1);
3328 mio_expr (&e->value.op.op2);
3329 break;
3331 default:
3332 bad_module ("Bad operator");
3335 break;
3337 case EXPR_FUNCTION:
3338 mio_symtree_ref (&e->symtree);
3339 mio_actual_arglist (&e->value.function.actual);
3341 if (iomode == IO_OUTPUT)
3343 e->value.function.name
3344 = mio_allocated_string (e->value.function.name);
3345 if (e->value.function.esym)
3346 flag = 1;
3347 else if (e->ref)
3348 flag = 2;
3349 else if (e->value.function.isym == NULL)
3350 flag = 3;
3351 else
3352 flag = 0;
3353 mio_integer (&flag);
3354 switch (flag)
3356 case 1:
3357 mio_symbol_ref (&e->value.function.esym);
3358 break;
3359 case 2:
3360 mio_ref_list (&e->ref);
3361 break;
3362 case 3:
3363 break;
3364 default:
3365 write_atom (ATOM_STRING, e->value.function.isym->name);
3368 else
3370 require_atom (ATOM_STRING);
3371 if (atom_string[0] == '\0')
3372 e->value.function.name = NULL;
3373 else
3374 e->value.function.name = gfc_get_string (atom_string);
3375 free (atom_string);
3377 mio_integer (&flag);
3378 switch (flag)
3380 case 1:
3381 mio_symbol_ref (&e->value.function.esym);
3382 break;
3383 case 2:
3384 mio_ref_list (&e->ref);
3385 break;
3386 case 3:
3387 break;
3388 default:
3389 require_atom (ATOM_STRING);
3390 e->value.function.isym = gfc_find_function (atom_string);
3391 free (atom_string);
3395 break;
3397 case EXPR_VARIABLE:
3398 mio_symtree_ref (&e->symtree);
3399 mio_ref_list (&e->ref);
3400 break;
3402 case EXPR_SUBSTRING:
3403 e->value.character.string
3404 = CONST_CAST (gfc_char_t *,
3405 mio_allocated_wide_string (e->value.character.string,
3406 e->value.character.length));
3407 mio_ref_list (&e->ref);
3408 break;
3410 case EXPR_STRUCTURE:
3411 case EXPR_ARRAY:
3412 mio_constructor (&e->value.constructor);
3413 mio_shape (&e->shape, e->rank);
3414 break;
3416 case EXPR_CONSTANT:
3417 switch (e->ts.type)
3419 case BT_INTEGER:
3420 mio_gmp_integer (&e->value.integer);
3421 break;
3423 case BT_REAL:
3424 gfc_set_model_kind (e->ts.kind);
3425 mio_gmp_real (&e->value.real);
3426 break;
3428 case BT_COMPLEX:
3429 gfc_set_model_kind (e->ts.kind);
3430 mio_gmp_real (&mpc_realref (e->value.complex));
3431 mio_gmp_real (&mpc_imagref (e->value.complex));
3432 break;
3434 case BT_LOGICAL:
3435 mio_integer (&e->value.logical);
3436 break;
3438 case BT_CHARACTER:
3439 mio_integer (&e->value.character.length);
3440 e->value.character.string
3441 = CONST_CAST (gfc_char_t *,
3442 mio_allocated_wide_string (e->value.character.string,
3443 e->value.character.length));
3444 break;
3446 default:
3447 bad_module ("Bad type in constant expression");
3450 break;
3452 case EXPR_NULL:
3453 break;
3455 case EXPR_COMPCALL:
3456 case EXPR_PPC:
3457 gcc_unreachable ();
3458 break;
3461 mio_rparen ();
3465 /* Read and write namelists. */
3467 static void
3468 mio_namelist (gfc_symbol *sym)
3470 gfc_namelist *n, *m;
3471 const char *check_name;
3473 mio_lparen ();
3475 if (iomode == IO_OUTPUT)
3477 for (n = sym->namelist; n; n = n->next)
3478 mio_symbol_ref (&n->sym);
3480 else
3482 /* This departure from the standard is flagged as an error.
3483 It does, in fact, work correctly. TODO: Allow it
3484 conditionally? */
3485 if (sym->attr.flavor == FL_NAMELIST)
3487 check_name = find_use_name (sym->name, false);
3488 if (check_name && strcmp (check_name, sym->name) != 0)
3489 gfc_error ("Namelist %s cannot be renamed by USE "
3490 "association to %s", sym->name, check_name);
3493 m = NULL;
3494 while (peek_atom () != ATOM_RPAREN)
3496 n = gfc_get_namelist ();
3497 mio_symbol_ref (&n->sym);
3499 if (sym->namelist == NULL)
3500 sym->namelist = n;
3501 else
3502 m->next = n;
3504 m = n;
3506 sym->namelist_tail = m;
3509 mio_rparen ();
3513 /* Save/restore lists of gfc_interface structures. When loading an
3514 interface, we are really appending to the existing list of
3515 interfaces. Checking for duplicate and ambiguous interfaces has to
3516 be done later when all symbols have been loaded. */
3518 pointer_info *
3519 mio_interface_rest (gfc_interface **ip)
3521 gfc_interface *tail, *p;
3522 pointer_info *pi = NULL;
3524 if (iomode == IO_OUTPUT)
3526 if (ip != NULL)
3527 for (p = *ip; p; p = p->next)
3528 mio_symbol_ref (&p->sym);
3530 else
3532 if (*ip == NULL)
3533 tail = NULL;
3534 else
3536 tail = *ip;
3537 while (tail->next)
3538 tail = tail->next;
3541 for (;;)
3543 if (peek_atom () == ATOM_RPAREN)
3544 break;
3546 p = gfc_get_interface ();
3547 p->where = gfc_current_locus;
3548 pi = mio_symbol_ref (&p->sym);
3550 if (tail == NULL)
3551 *ip = p;
3552 else
3553 tail->next = p;
3555 tail = p;
3559 mio_rparen ();
3560 return pi;
3564 /* Save/restore a nameless operator interface. */
3566 static void
3567 mio_interface (gfc_interface **ip)
3569 mio_lparen ();
3570 mio_interface_rest (ip);
3574 /* Save/restore a named operator interface. */
3576 static void
3577 mio_symbol_interface (const char **name, const char **module,
3578 gfc_interface **ip)
3580 mio_lparen ();
3581 mio_pool_string (name);
3582 mio_pool_string (module);
3583 mio_interface_rest (ip);
3587 static void
3588 mio_namespace_ref (gfc_namespace **nsp)
3590 gfc_namespace *ns;
3591 pointer_info *p;
3593 p = mio_pointer_ref (nsp);
3595 if (p->type == P_UNKNOWN)
3596 p->type = P_NAMESPACE;
3598 if (iomode == IO_INPUT && p->integer != 0)
3600 ns = (gfc_namespace *) p->u.pointer;
3601 if (ns == NULL)
3603 ns = gfc_get_namespace (NULL, 0);
3604 associate_integer_pointer (p, ns);
3606 else
3607 ns->refs++;
3612 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3614 static gfc_namespace* current_f2k_derived;
3616 static void
3617 mio_typebound_proc (gfc_typebound_proc** proc)
3619 int flag;
3620 int overriding_flag;
3622 if (iomode == IO_INPUT)
3624 *proc = gfc_get_typebound_proc (NULL);
3625 (*proc)->where = gfc_current_locus;
3627 gcc_assert (*proc);
3629 mio_lparen ();
3631 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3633 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3634 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3635 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3636 overriding_flag = mio_name (overriding_flag, binding_overriding);
3637 (*proc)->deferred = ((overriding_flag & 2) != 0);
3638 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3639 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3641 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3642 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3643 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3645 mio_pool_string (&((*proc)->pass_arg));
3647 flag = (int) (*proc)->pass_arg_num;
3648 mio_integer (&flag);
3649 (*proc)->pass_arg_num = (unsigned) flag;
3651 if ((*proc)->is_generic)
3653 gfc_tbp_generic* g;
3654 int iop;
3656 mio_lparen ();
3658 if (iomode == IO_OUTPUT)
3659 for (g = (*proc)->u.generic; g; g = g->next)
3661 iop = (int) g->is_operator;
3662 mio_integer (&iop);
3663 mio_allocated_string (g->specific_st->name);
3665 else
3667 (*proc)->u.generic = NULL;
3668 while (peek_atom () != ATOM_RPAREN)
3670 gfc_symtree** sym_root;
3672 g = gfc_get_tbp_generic ();
3673 g->specific = NULL;
3675 mio_integer (&iop);
3676 g->is_operator = (bool) iop;
3678 require_atom (ATOM_STRING);
3679 sym_root = &current_f2k_derived->tb_sym_root;
3680 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3681 free (atom_string);
3683 g->next = (*proc)->u.generic;
3684 (*proc)->u.generic = g;
3688 mio_rparen ();
3690 else if (!(*proc)->ppc)
3691 mio_symtree_ref (&(*proc)->u.specific);
3693 mio_rparen ();
3696 /* Walker-callback function for this purpose. */
3697 static void
3698 mio_typebound_symtree (gfc_symtree* st)
3700 if (iomode == IO_OUTPUT && !st->n.tb)
3701 return;
3703 if (iomode == IO_OUTPUT)
3705 mio_lparen ();
3706 mio_allocated_string (st->name);
3708 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3710 mio_typebound_proc (&st->n.tb);
3711 mio_rparen ();
3714 /* IO a full symtree (in all depth). */
3715 static void
3716 mio_full_typebound_tree (gfc_symtree** root)
3718 mio_lparen ();
3720 if (iomode == IO_OUTPUT)
3721 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3722 else
3724 while (peek_atom () == ATOM_LPAREN)
3726 gfc_symtree* st;
3728 mio_lparen ();
3730 require_atom (ATOM_STRING);
3731 st = gfc_get_tbp_symtree (root, atom_string);
3732 free (atom_string);
3734 mio_typebound_symtree (st);
3738 mio_rparen ();
3741 static void
3742 mio_finalizer (gfc_finalizer **f)
3744 if (iomode == IO_OUTPUT)
3746 gcc_assert (*f);
3747 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3748 mio_symtree_ref (&(*f)->proc_tree);
3750 else
3752 *f = gfc_get_finalizer ();
3753 (*f)->where = gfc_current_locus; /* Value should not matter. */
3754 (*f)->next = NULL;
3756 mio_symtree_ref (&(*f)->proc_tree);
3757 (*f)->proc_sym = NULL;
3761 static void
3762 mio_f2k_derived (gfc_namespace *f2k)
3764 current_f2k_derived = f2k;
3766 /* Handle the list of finalizer procedures. */
3767 mio_lparen ();
3768 if (iomode == IO_OUTPUT)
3770 gfc_finalizer *f;
3771 for (f = f2k->finalizers; f; f = f->next)
3772 mio_finalizer (&f);
3774 else
3776 f2k->finalizers = NULL;
3777 while (peek_atom () != ATOM_RPAREN)
3779 gfc_finalizer *cur = NULL;
3780 mio_finalizer (&cur);
3781 cur->next = f2k->finalizers;
3782 f2k->finalizers = cur;
3785 mio_rparen ();
3787 /* Handle type-bound procedures. */
3788 mio_full_typebound_tree (&f2k->tb_sym_root);
3790 /* Type-bound user operators. */
3791 mio_full_typebound_tree (&f2k->tb_uop_root);
3793 /* Type-bound intrinsic operators. */
3794 mio_lparen ();
3795 if (iomode == IO_OUTPUT)
3797 int op;
3798 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3800 gfc_intrinsic_op realop;
3802 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3803 continue;
3805 mio_lparen ();
3806 realop = (gfc_intrinsic_op) op;
3807 mio_intrinsic_op (&realop);
3808 mio_typebound_proc (&f2k->tb_op[op]);
3809 mio_rparen ();
3812 else
3813 while (peek_atom () != ATOM_RPAREN)
3815 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3817 mio_lparen ();
3818 mio_intrinsic_op (&op);
3819 mio_typebound_proc (&f2k->tb_op[op]);
3820 mio_rparen ();
3822 mio_rparen ();
3825 static void
3826 mio_full_f2k_derived (gfc_symbol *sym)
3828 mio_lparen ();
3830 if (iomode == IO_OUTPUT)
3832 if (sym->f2k_derived)
3833 mio_f2k_derived (sym->f2k_derived);
3835 else
3837 if (peek_atom () != ATOM_RPAREN)
3839 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3840 mio_f2k_derived (sym->f2k_derived);
3842 else
3843 gcc_assert (!sym->f2k_derived);
3846 mio_rparen ();
3849 static const mstring omp_declare_simd_clauses[] =
3851 minit ("INBRANCH", 0),
3852 minit ("NOTINBRANCH", 1),
3853 minit ("SIMDLEN", 2),
3854 minit ("UNIFORM", 3),
3855 minit ("LINEAR", 4),
3856 minit ("ALIGNED", 5),
3857 minit (NULL, -1)
3860 /* Handle !$omp declare simd. */
3862 static void
3863 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
3865 if (iomode == IO_OUTPUT)
3867 if (*odsp == NULL)
3868 return;
3870 else if (peek_atom () != ATOM_LPAREN)
3871 return;
3873 gfc_omp_declare_simd *ods = *odsp;
3875 mio_lparen ();
3876 if (iomode == IO_OUTPUT)
3878 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
3879 if (ods->clauses)
3881 gfc_omp_namelist *n;
3883 if (ods->clauses->inbranch)
3884 mio_name (0, omp_declare_simd_clauses);
3885 if (ods->clauses->notinbranch)
3886 mio_name (1, omp_declare_simd_clauses);
3887 if (ods->clauses->simdlen_expr)
3889 mio_name (2, omp_declare_simd_clauses);
3890 mio_expr (&ods->clauses->simdlen_expr);
3892 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
3894 mio_name (3, omp_declare_simd_clauses);
3895 mio_symbol_ref (&n->sym);
3897 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
3899 mio_name (4, omp_declare_simd_clauses);
3900 mio_symbol_ref (&n->sym);
3901 mio_expr (&n->expr);
3903 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
3905 mio_name (5, omp_declare_simd_clauses);
3906 mio_symbol_ref (&n->sym);
3907 mio_expr (&n->expr);
3911 else
3913 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
3915 require_atom (ATOM_NAME);
3916 *odsp = ods = gfc_get_omp_declare_simd ();
3917 ods->where = gfc_current_locus;
3918 ods->proc_name = ns->proc_name;
3919 if (peek_atom () == ATOM_NAME)
3921 ods->clauses = gfc_get_omp_clauses ();
3922 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
3923 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
3924 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
3926 while (peek_atom () == ATOM_NAME)
3928 gfc_omp_namelist *n;
3929 int t = mio_name (0, omp_declare_simd_clauses);
3931 switch (t)
3933 case 0: ods->clauses->inbranch = true; break;
3934 case 1: ods->clauses->notinbranch = true; break;
3935 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
3936 case 3:
3937 case 4:
3938 case 5:
3939 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
3940 ptrs[t - 3] = &n->next;
3941 mio_symbol_ref (&n->sym);
3942 if (t != 3)
3943 mio_expr (&n->expr);
3944 break;
3949 mio_omp_declare_simd (ns, &ods->next);
3951 mio_rparen ();
3955 static const mstring omp_declare_reduction_stmt[] =
3957 minit ("ASSIGN", 0),
3958 minit ("CALL", 1),
3959 minit (NULL, -1)
3963 static void
3964 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
3965 gfc_namespace *ns, bool is_initializer)
3967 if (iomode == IO_OUTPUT)
3969 if ((*sym1)->module == NULL)
3971 (*sym1)->module = module_name;
3972 (*sym2)->module = module_name;
3974 mio_symbol_ref (sym1);
3975 mio_symbol_ref (sym2);
3976 if (ns->code->op == EXEC_ASSIGN)
3978 mio_name (0, omp_declare_reduction_stmt);
3979 mio_expr (&ns->code->expr1);
3980 mio_expr (&ns->code->expr2);
3982 else
3984 int flag;
3985 mio_name (1, omp_declare_reduction_stmt);
3986 mio_symtree_ref (&ns->code->symtree);
3987 mio_actual_arglist (&ns->code->ext.actual);
3989 flag = ns->code->resolved_isym != NULL;
3990 mio_integer (&flag);
3991 if (flag)
3992 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
3993 else
3994 mio_symbol_ref (&ns->code->resolved_sym);
3997 else
3999 pointer_info *p1 = mio_symbol_ref (sym1);
4000 pointer_info *p2 = mio_symbol_ref (sym2);
4001 gfc_symbol *sym;
4002 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4003 gcc_assert (p1->u.rsym.sym == NULL);
4004 /* Add hidden symbols to the symtree. */
4005 pointer_info *q = get_integer (p1->u.rsym.ns);
4006 q->u.pointer = (void *) ns;
4007 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4008 sym->ts = udr->ts;
4009 sym->module = gfc_get_string (p1->u.rsym.module);
4010 associate_integer_pointer (p1, sym);
4011 sym->attr.omp_udr_artificial_var = 1;
4012 gcc_assert (p2->u.rsym.sym == NULL);
4013 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4014 sym->ts = udr->ts;
4015 sym->module = gfc_get_string (p2->u.rsym.module);
4016 associate_integer_pointer (p2, sym);
4017 sym->attr.omp_udr_artificial_var = 1;
4018 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4020 ns->code = gfc_get_code (EXEC_ASSIGN);
4021 mio_expr (&ns->code->expr1);
4022 mio_expr (&ns->code->expr2);
4024 else
4026 int flag;
4027 ns->code = gfc_get_code (EXEC_CALL);
4028 mio_symtree_ref (&ns->code->symtree);
4029 mio_actual_arglist (&ns->code->ext.actual);
4031 mio_integer (&flag);
4032 if (flag)
4034 require_atom (ATOM_STRING);
4035 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4036 free (atom_string);
4038 else
4039 mio_symbol_ref (&ns->code->resolved_sym);
4041 ns->code->loc = gfc_current_locus;
4042 ns->omp_udr_ns = 1;
4047 /* Unlike most other routines, the address of the symbol node is already
4048 fixed on input and the name/module has already been filled in.
4049 If you update the symbol format here, don't forget to update read_module
4050 as well (look for "seek to the symbol's component list"). */
4052 static void
4053 mio_symbol (gfc_symbol *sym)
4055 int intmod = INTMOD_NONE;
4057 mio_lparen ();
4059 mio_symbol_attribute (&sym->attr);
4061 /* Note that components are always saved, even if they are supposed
4062 to be private. Component access is checked during searching. */
4063 mio_component_list (&sym->components, sym->attr.vtype);
4064 if (sym->components != NULL)
4065 sym->component_access
4066 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4068 mio_typespec (&sym->ts);
4069 if (sym->ts.type == BT_CLASS)
4070 sym->attr.class_ok = 1;
4072 if (iomode == IO_OUTPUT)
4073 mio_namespace_ref (&sym->formal_ns);
4074 else
4076 mio_namespace_ref (&sym->formal_ns);
4077 if (sym->formal_ns)
4078 sym->formal_ns->proc_name = sym;
4081 /* Save/restore common block links. */
4082 mio_symbol_ref (&sym->common_next);
4084 mio_formal_arglist (&sym->formal);
4086 if (sym->attr.flavor == FL_PARAMETER)
4087 mio_expr (&sym->value);
4089 mio_array_spec (&sym->as);
4091 mio_symbol_ref (&sym->result);
4093 if (sym->attr.cray_pointee)
4094 mio_symbol_ref (&sym->cp_pointer);
4096 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4097 mio_full_f2k_derived (sym);
4099 mio_namelist (sym);
4101 /* Add the fields that say whether this is from an intrinsic module,
4102 and if so, what symbol it is within the module. */
4103 /* mio_integer (&(sym->from_intmod)); */
4104 if (iomode == IO_OUTPUT)
4106 intmod = sym->from_intmod;
4107 mio_integer (&intmod);
4109 else
4111 mio_integer (&intmod);
4112 if (current_intmod)
4113 sym->from_intmod = current_intmod;
4114 else
4115 sym->from_intmod = (intmod_id) intmod;
4118 mio_integer (&(sym->intmod_sym_id));
4120 if (sym->attr.flavor == FL_DERIVED)
4121 mio_integer (&(sym->hash_value));
4123 if (sym->formal_ns
4124 && sym->formal_ns->proc_name == sym
4125 && sym->formal_ns->entries == NULL)
4126 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4128 mio_rparen ();
4132 /************************* Top level subroutines *************************/
4134 /* Given a root symtree node and a symbol, try to find a symtree that
4135 references the symbol that is not a unique name. */
4137 static gfc_symtree *
4138 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
4140 gfc_symtree *s = NULL;
4142 if (st == NULL)
4143 return s;
4145 s = find_symtree_for_symbol (st->right, sym);
4146 if (s != NULL)
4147 return s;
4148 s = find_symtree_for_symbol (st->left, sym);
4149 if (s != NULL)
4150 return s;
4152 if (st->n.sym == sym && !check_unique_name (st->name))
4153 return st;
4155 return s;
4159 /* A recursive function to look for a specific symbol by name and by
4160 module. Whilst several symtrees might point to one symbol, its
4161 is sufficient for the purposes here than one exist. Note that
4162 generic interfaces are distinguished as are symbols that have been
4163 renamed in another module. */
4164 static gfc_symtree *
4165 find_symbol (gfc_symtree *st, const char *name,
4166 const char *module, int generic)
4168 int c;
4169 gfc_symtree *retval, *s;
4171 if (st == NULL || st->n.sym == NULL)
4172 return NULL;
4174 c = strcmp (name, st->n.sym->name);
4175 if (c == 0 && st->n.sym->module
4176 && strcmp (module, st->n.sym->module) == 0
4177 && !check_unique_name (st->name))
4179 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4181 /* Detect symbols that are renamed by use association in another
4182 module by the absence of a symtree and null attr.use_rename,
4183 since the latter is not transmitted in the module file. */
4184 if (((!generic && !st->n.sym->attr.generic)
4185 || (generic && st->n.sym->attr.generic))
4186 && !(s == NULL && !st->n.sym->attr.use_rename))
4187 return st;
4190 retval = find_symbol (st->left, name, module, generic);
4192 if (retval == NULL)
4193 retval = find_symbol (st->right, name, module, generic);
4195 return retval;
4199 /* Skip a list between balanced left and right parens.
4200 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4201 have been already parsed by hand, and the remaining of the content is to be
4202 skipped here. The default value is 0 (balanced parens). */
4204 static void
4205 skip_list (int nest_level = 0)
4207 int level;
4209 level = nest_level;
4212 switch (parse_atom ())
4214 case ATOM_LPAREN:
4215 level++;
4216 break;
4218 case ATOM_RPAREN:
4219 level--;
4220 break;
4222 case ATOM_STRING:
4223 free (atom_string);
4224 break;
4226 case ATOM_NAME:
4227 case ATOM_INTEGER:
4228 break;
4231 while (level > 0);
4235 /* Load operator interfaces from the module. Interfaces are unusual
4236 in that they attach themselves to existing symbols. */
4238 static void
4239 load_operator_interfaces (void)
4241 const char *p;
4242 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4243 gfc_user_op *uop;
4244 pointer_info *pi = NULL;
4245 int n, i;
4247 mio_lparen ();
4249 while (peek_atom () != ATOM_RPAREN)
4251 mio_lparen ();
4253 mio_internal_string (name);
4254 mio_internal_string (module);
4256 n = number_use_names (name, true);
4257 n = n ? n : 1;
4259 for (i = 1; i <= n; i++)
4261 /* Decide if we need to load this one or not. */
4262 p = find_use_name_n (name, &i, true);
4264 if (p == NULL)
4266 while (parse_atom () != ATOM_RPAREN);
4267 continue;
4270 if (i == 1)
4272 uop = gfc_get_uop (p);
4273 pi = mio_interface_rest (&uop->op);
4275 else
4277 if (gfc_find_uop (p, NULL))
4278 continue;
4279 uop = gfc_get_uop (p);
4280 uop->op = gfc_get_interface ();
4281 uop->op->where = gfc_current_locus;
4282 add_fixup (pi->integer, &uop->op->sym);
4287 mio_rparen ();
4291 /* Load interfaces from the module. Interfaces are unusual in that
4292 they attach themselves to existing symbols. */
4294 static void
4295 load_generic_interfaces (void)
4297 const char *p;
4298 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4299 gfc_symbol *sym;
4300 gfc_interface *generic = NULL, *gen = NULL;
4301 int n, i, renamed;
4302 bool ambiguous_set = false;
4304 mio_lparen ();
4306 while (peek_atom () != ATOM_RPAREN)
4308 mio_lparen ();
4310 mio_internal_string (name);
4311 mio_internal_string (module);
4313 n = number_use_names (name, false);
4314 renamed = n ? 1 : 0;
4315 n = n ? n : 1;
4317 for (i = 1; i <= n; i++)
4319 gfc_symtree *st;
4320 /* Decide if we need to load this one or not. */
4321 p = find_use_name_n (name, &i, false);
4323 st = find_symbol (gfc_current_ns->sym_root,
4324 name, module_name, 1);
4326 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4328 /* Skip the specific names for these cases. */
4329 while (i == 1 && parse_atom () != ATOM_RPAREN);
4331 continue;
4334 /* If the symbol exists already and is being USEd without being
4335 in an ONLY clause, do not load a new symtree(11.3.2). */
4336 if (!only_flag && st)
4337 sym = st->n.sym;
4339 if (!sym)
4341 if (st)
4343 sym = st->n.sym;
4344 if (strcmp (st->name, p) != 0)
4346 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4347 st->n.sym = sym;
4348 sym->refs++;
4352 /* Since we haven't found a valid generic interface, we had
4353 better make one. */
4354 if (!sym)
4356 gfc_get_symbol (p, NULL, &sym);
4357 sym->name = gfc_get_string (name);
4358 sym->module = module_name;
4359 sym->attr.flavor = FL_PROCEDURE;
4360 sym->attr.generic = 1;
4361 sym->attr.use_assoc = 1;
4364 else
4366 /* Unless sym is a generic interface, this reference
4367 is ambiguous. */
4368 if (st == NULL)
4369 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4371 sym = st->n.sym;
4373 if (st && !sym->attr.generic
4374 && !st->ambiguous
4375 && sym->module
4376 && strcmp (module, sym->module))
4378 ambiguous_set = true;
4379 st->ambiguous = 1;
4383 sym->attr.use_only = only_flag;
4384 sym->attr.use_rename = renamed;
4386 if (i == 1)
4388 mio_interface_rest (&sym->generic);
4389 generic = sym->generic;
4391 else if (!sym->generic)
4393 sym->generic = generic;
4394 sym->attr.generic_copy = 1;
4397 /* If a procedure that is not generic has generic interfaces
4398 that include itself, it is generic! We need to take care
4399 to retain symbols ambiguous that were already so. */
4400 if (sym->attr.use_assoc
4401 && !sym->attr.generic
4402 && sym->attr.flavor == FL_PROCEDURE)
4404 for (gen = generic; gen; gen = gen->next)
4406 if (gen->sym == sym)
4408 sym->attr.generic = 1;
4409 if (ambiguous_set)
4410 st->ambiguous = 0;
4411 break;
4419 mio_rparen ();
4423 /* Load common blocks. */
4425 static void
4426 load_commons (void)
4428 char name[GFC_MAX_SYMBOL_LEN + 1];
4429 gfc_common_head *p;
4431 mio_lparen ();
4433 while (peek_atom () != ATOM_RPAREN)
4435 int flags;
4436 char* label;
4437 mio_lparen ();
4438 mio_internal_string (name);
4440 p = gfc_get_common (name, 1);
4442 mio_symbol_ref (&p->head);
4443 mio_integer (&flags);
4444 if (flags & 1)
4445 p->saved = 1;
4446 if (flags & 2)
4447 p->threadprivate = 1;
4448 p->use_assoc = 1;
4450 /* Get whether this was a bind(c) common or not. */
4451 mio_integer (&p->is_bind_c);
4452 /* Get the binding label. */
4453 label = read_string ();
4454 if (strlen (label))
4455 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4456 XDELETEVEC (label);
4458 mio_rparen ();
4461 mio_rparen ();
4465 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4466 so that unused variables are not loaded and so that the expression can
4467 be safely freed. */
4469 static void
4470 load_equiv (void)
4472 gfc_equiv *head, *tail, *end, *eq;
4473 bool unused;
4475 mio_lparen ();
4476 in_load_equiv = true;
4478 end = gfc_current_ns->equiv;
4479 while (end != NULL && end->next != NULL)
4480 end = end->next;
4482 while (peek_atom () != ATOM_RPAREN) {
4483 mio_lparen ();
4484 head = tail = NULL;
4486 while(peek_atom () != ATOM_RPAREN)
4488 if (head == NULL)
4489 head = tail = gfc_get_equiv ();
4490 else
4492 tail->eq = gfc_get_equiv ();
4493 tail = tail->eq;
4496 mio_pool_string (&tail->module);
4497 mio_expr (&tail->expr);
4500 /* Unused equivalence members have a unique name. In addition, it
4501 must be checked that the symbols are from the same module. */
4502 unused = true;
4503 for (eq = head; eq; eq = eq->eq)
4505 if (eq->expr->symtree->n.sym->module
4506 && head->expr->symtree->n.sym->module
4507 && strcmp (head->expr->symtree->n.sym->module,
4508 eq->expr->symtree->n.sym->module) == 0
4509 && !check_unique_name (eq->expr->symtree->name))
4511 unused = false;
4512 break;
4516 if (unused)
4518 for (eq = head; eq; eq = head)
4520 head = eq->eq;
4521 gfc_free_expr (eq->expr);
4522 free (eq);
4526 if (end == NULL)
4527 gfc_current_ns->equiv = head;
4528 else
4529 end->next = head;
4531 if (head != NULL)
4532 end = head;
4534 mio_rparen ();
4537 mio_rparen ();
4538 in_load_equiv = false;
4542 /* This function loads the sym_root of f2k_derived with the extensions to
4543 the derived type. */
4544 static void
4545 load_derived_extensions (void)
4547 int symbol, j;
4548 gfc_symbol *derived;
4549 gfc_symbol *dt;
4550 gfc_symtree *st;
4551 pointer_info *info;
4552 char name[GFC_MAX_SYMBOL_LEN + 1];
4553 char module[GFC_MAX_SYMBOL_LEN + 1];
4554 const char *p;
4556 mio_lparen ();
4557 while (peek_atom () != ATOM_RPAREN)
4559 mio_lparen ();
4560 mio_integer (&symbol);
4561 info = get_integer (symbol);
4562 derived = info->u.rsym.sym;
4564 /* This one is not being loaded. */
4565 if (!info || !derived)
4567 while (peek_atom () != ATOM_RPAREN)
4568 skip_list ();
4569 continue;
4572 gcc_assert (derived->attr.flavor == FL_DERIVED);
4573 if (derived->f2k_derived == NULL)
4574 derived->f2k_derived = gfc_get_namespace (NULL, 0);
4576 while (peek_atom () != ATOM_RPAREN)
4578 mio_lparen ();
4579 mio_internal_string (name);
4580 mio_internal_string (module);
4582 /* Only use one use name to find the symbol. */
4583 j = 1;
4584 p = find_use_name_n (name, &j, false);
4585 if (p)
4587 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4588 dt = st->n.sym;
4589 st = gfc_find_symtree (derived->f2k_derived->sym_root, name);
4590 if (st == NULL)
4592 /* Only use the real name in f2k_derived to ensure a single
4593 symtree. */
4594 st = gfc_new_symtree (&derived->f2k_derived->sym_root, name);
4595 st->n.sym = dt;
4596 st->n.sym->refs++;
4599 mio_rparen ();
4601 mio_rparen ();
4603 mio_rparen ();
4607 /* This function loads OpenMP user defined reductions. */
4608 static void
4609 load_omp_udrs (void)
4611 mio_lparen ();
4612 while (peek_atom () != ATOM_RPAREN)
4614 const char *name, *newname;
4615 char *altname;
4616 gfc_typespec ts;
4617 gfc_symtree *st;
4618 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4620 mio_lparen ();
4621 mio_pool_string (&name);
4622 mio_typespec (&ts);
4623 if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
4625 const char *p = name + sizeof ("operator ") - 1;
4626 if (strcmp (p, "+") == 0)
4627 rop = OMP_REDUCTION_PLUS;
4628 else if (strcmp (p, "*") == 0)
4629 rop = OMP_REDUCTION_TIMES;
4630 else if (strcmp (p, "-") == 0)
4631 rop = OMP_REDUCTION_MINUS;
4632 else if (strcmp (p, ".and.") == 0)
4633 rop = OMP_REDUCTION_AND;
4634 else if (strcmp (p, ".or.") == 0)
4635 rop = OMP_REDUCTION_OR;
4636 else if (strcmp (p, ".eqv.") == 0)
4637 rop = OMP_REDUCTION_EQV;
4638 else if (strcmp (p, ".neqv.") == 0)
4639 rop = OMP_REDUCTION_NEQV;
4641 altname = NULL;
4642 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4644 size_t len = strlen (name + 1);
4645 altname = XALLOCAVEC (char, len);
4646 gcc_assert (name[len] == '.');
4647 memcpy (altname, name + 1, len - 1);
4648 altname[len - 1] = '\0';
4650 newname = name;
4651 if (rop == OMP_REDUCTION_USER)
4652 newname = find_use_name (altname ? altname : name, !!altname);
4653 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4654 newname = NULL;
4655 if (newname == NULL)
4657 skip_list (1);
4658 continue;
4660 if (altname && newname != altname)
4662 size_t len = strlen (newname);
4663 altname = XALLOCAVEC (char, len + 3);
4664 altname[0] = '.';
4665 memcpy (altname + 1, newname, len);
4666 altname[len + 1] = '.';
4667 altname[len + 2] = '\0';
4668 name = gfc_get_string (altname);
4670 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4671 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4672 if (udr)
4674 require_atom (ATOM_INTEGER);
4675 pointer_info *p = get_integer (atom_int);
4676 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4678 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4679 "module %s at %L",
4680 p->u.rsym.module, &gfc_current_locus);
4681 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4682 "%s at %L",
4683 udr->omp_out->module, &udr->where);
4685 skip_list (1);
4686 continue;
4688 udr = gfc_get_omp_udr ();
4689 udr->name = name;
4690 udr->rop = rop;
4691 udr->ts = ts;
4692 udr->where = gfc_current_locus;
4693 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4694 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4695 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4696 false);
4697 if (peek_atom () != ATOM_RPAREN)
4699 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4700 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4701 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4702 udr->initializer_ns, true);
4704 if (st)
4706 udr->next = st->n.omp_udr;
4707 st->n.omp_udr = udr;
4709 else
4711 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4712 st->n.omp_udr = udr;
4714 mio_rparen ();
4716 mio_rparen ();
4720 /* Recursive function to traverse the pointer_info tree and load a
4721 needed symbol. We return nonzero if we load a symbol and stop the
4722 traversal, because the act of loading can alter the tree. */
4724 static int
4725 load_needed (pointer_info *p)
4727 gfc_namespace *ns;
4728 pointer_info *q;
4729 gfc_symbol *sym;
4730 int rv;
4732 rv = 0;
4733 if (p == NULL)
4734 return rv;
4736 rv |= load_needed (p->left);
4737 rv |= load_needed (p->right);
4739 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4740 return rv;
4742 p->u.rsym.state = USED;
4744 set_module_locus (&p->u.rsym.where);
4746 sym = p->u.rsym.sym;
4747 if (sym == NULL)
4749 q = get_integer (p->u.rsym.ns);
4751 ns = (gfc_namespace *) q->u.pointer;
4752 if (ns == NULL)
4754 /* Create an interface namespace if necessary. These are
4755 the namespaces that hold the formal parameters of module
4756 procedures. */
4758 ns = gfc_get_namespace (NULL, 0);
4759 associate_integer_pointer (q, ns);
4762 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4763 doesn't go pear-shaped if the symbol is used. */
4764 if (!ns->proc_name)
4765 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4766 1, &ns->proc_name);
4768 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4769 sym->name = dt_lower_string (p->u.rsym.true_name);
4770 sym->module = gfc_get_string (p->u.rsym.module);
4771 if (p->u.rsym.binding_label)
4772 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4773 (p->u.rsym.binding_label));
4775 associate_integer_pointer (p, sym);
4778 mio_symbol (sym);
4779 sym->attr.use_assoc = 1;
4781 /* Mark as only or rename for later diagnosis for explicitly imported
4782 but not used warnings; don't mark internal symbols such as __vtab,
4783 __def_init etc. Only mark them if they have been explicitly loaded. */
4785 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4787 gfc_use_rename *u;
4789 /* Search the use/rename list for the variable; if the variable is
4790 found, mark it. */
4791 for (u = gfc_rename_list; u; u = u->next)
4793 if (strcmp (u->use_name, sym->name) == 0)
4795 sym->attr.use_only = 1;
4796 break;
4801 if (p->u.rsym.renamed)
4802 sym->attr.use_rename = 1;
4804 return 1;
4808 /* Recursive function for cleaning up things after a module has been read. */
4810 static void
4811 read_cleanup (pointer_info *p)
4813 gfc_symtree *st;
4814 pointer_info *q;
4816 if (p == NULL)
4817 return;
4819 read_cleanup (p->left);
4820 read_cleanup (p->right);
4822 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4824 gfc_namespace *ns;
4825 /* Add hidden symbols to the symtree. */
4826 q = get_integer (p->u.rsym.ns);
4827 ns = (gfc_namespace *) q->u.pointer;
4829 if (!p->u.rsym.sym->attr.vtype
4830 && !p->u.rsym.sym->attr.vtab)
4831 st = gfc_get_unique_symtree (ns);
4832 else
4834 /* There is no reason to use 'unique_symtrees' for vtabs or
4835 vtypes - their name is fine for a symtree and reduces the
4836 namespace pollution. */
4837 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4838 if (!st)
4839 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4842 st->n.sym = p->u.rsym.sym;
4843 st->n.sym->refs++;
4845 /* Fixup any symtree references. */
4846 p->u.rsym.symtree = st;
4847 resolve_fixups (p->u.rsym.stfixup, st);
4848 p->u.rsym.stfixup = NULL;
4851 /* Free unused symbols. */
4852 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4853 gfc_free_symbol (p->u.rsym.sym);
4857 /* It is not quite enough to check for ambiguity in the symbols by
4858 the loaded symbol and the new symbol not being identical. */
4859 static bool
4860 check_for_ambiguous (gfc_symbol *st_sym, pointer_info *info)
4862 gfc_symbol *rsym;
4863 module_locus locus;
4864 symbol_attribute attr;
4866 if (gfc_current_ns->proc_name && st_sym->name == gfc_current_ns->proc_name->name)
4868 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4869 "current program unit", st_sym->name, module_name);
4870 return true;
4873 rsym = info->u.rsym.sym;
4874 if (st_sym == rsym)
4875 return false;
4877 if (st_sym->attr.vtab || st_sym->attr.vtype)
4878 return false;
4880 /* If the existing symbol is generic from a different module and
4881 the new symbol is generic there can be no ambiguity. */
4882 if (st_sym->attr.generic
4883 && st_sym->module
4884 && st_sym->module != module_name)
4886 /* The new symbol's attributes have not yet been read. Since
4887 we need attr.generic, read it directly. */
4888 get_module_locus (&locus);
4889 set_module_locus (&info->u.rsym.where);
4890 mio_lparen ();
4891 attr.generic = 0;
4892 mio_symbol_attribute (&attr);
4893 set_module_locus (&locus);
4894 if (attr.generic)
4895 return false;
4898 return true;
4902 /* Read a module file. */
4904 static void
4905 read_module (void)
4907 module_locus operator_interfaces, user_operators, extensions, omp_udrs;
4908 const char *p;
4909 char name[GFC_MAX_SYMBOL_LEN + 1];
4910 int i;
4911 int ambiguous, j, nuse, symbol;
4912 pointer_info *info, *q;
4913 gfc_use_rename *u = NULL;
4914 gfc_symtree *st;
4915 gfc_symbol *sym;
4917 get_module_locus (&operator_interfaces); /* Skip these for now. */
4918 skip_list ();
4920 get_module_locus (&user_operators);
4921 skip_list ();
4922 skip_list ();
4924 /* Skip commons, equivalences and derived type extensions for now. */
4925 skip_list ();
4926 skip_list ();
4928 get_module_locus (&extensions);
4929 skip_list ();
4931 /* Skip OpenMP UDRs. */
4932 get_module_locus (&omp_udrs);
4933 skip_list ();
4935 mio_lparen ();
4937 /* Create the fixup nodes for all the symbols. */
4939 while (peek_atom () != ATOM_RPAREN)
4941 char* bind_label;
4942 require_atom (ATOM_INTEGER);
4943 info = get_integer (atom_int);
4945 info->type = P_SYMBOL;
4946 info->u.rsym.state = UNUSED;
4948 info->u.rsym.true_name = read_string ();
4949 info->u.rsym.module = read_string ();
4950 bind_label = read_string ();
4951 if (strlen (bind_label))
4952 info->u.rsym.binding_label = bind_label;
4953 else
4954 XDELETEVEC (bind_label);
4956 require_atom (ATOM_INTEGER);
4957 info->u.rsym.ns = atom_int;
4959 get_module_locus (&info->u.rsym.where);
4961 /* See if the symbol has already been loaded by a previous module.
4962 If so, we reference the existing symbol and prevent it from
4963 being loaded again. This should not happen if the symbol being
4964 read is an index for an assumed shape dummy array (ns != 1). */
4966 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
4968 if (sym == NULL
4969 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
4971 skip_list ();
4972 continue;
4975 info->u.rsym.state = USED;
4976 info->u.rsym.sym = sym;
4977 /* The current symbol has already been loaded, so we can avoid loading
4978 it again. However, if it is a derived type, some of its components
4979 can be used in expressions in the module. To avoid the module loading
4980 failing, we need to associate the module's component pointer indexes
4981 with the existing symbol's component pointers. */
4982 if (sym->attr.flavor == FL_DERIVED)
4984 gfc_component *c;
4986 /* First seek to the symbol's component list. */
4987 mio_lparen (); /* symbol opening. */
4988 skip_list (); /* skip symbol attribute. */
4990 mio_lparen (); /* component list opening. */
4991 for (c = sym->components; c; c = c->next)
4993 pointer_info *p;
4994 const char *comp_name;
4995 int n;
4997 mio_lparen (); /* component opening. */
4998 mio_integer (&n);
4999 p = get_integer (n);
5000 if (p->u.pointer == NULL)
5001 associate_integer_pointer (p, c);
5002 mio_pool_string (&comp_name);
5003 gcc_assert (comp_name == c->name);
5004 skip_list (1); /* component end. */
5006 mio_rparen (); /* component list closing. */
5008 skip_list (1); /* symbol end. */
5010 else
5011 skip_list ();
5013 /* Some symbols do not have a namespace (eg. formal arguments),
5014 so the automatic "unique symtree" mechanism must be suppressed
5015 by marking them as referenced. */
5016 q = get_integer (info->u.rsym.ns);
5017 if (q->u.pointer == NULL)
5019 info->u.rsym.referenced = 1;
5020 continue;
5023 /* If possible recycle the symtree that references the symbol.
5024 If a symtree is not found and the module does not import one,
5025 a unique-name symtree is found by read_cleanup. */
5026 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
5027 if (st != NULL)
5029 info->u.rsym.symtree = st;
5030 info->u.rsym.referenced = 1;
5034 mio_rparen ();
5036 /* Parse the symtree lists. This lets us mark which symbols need to
5037 be loaded. Renaming is also done at this point by replacing the
5038 symtree name. */
5040 mio_lparen ();
5042 while (peek_atom () != ATOM_RPAREN)
5044 mio_internal_string (name);
5045 mio_integer (&ambiguous);
5046 mio_integer (&symbol);
5048 info = get_integer (symbol);
5050 /* See how many use names there are. If none, go through the start
5051 of the loop at least once. */
5052 nuse = number_use_names (name, false);
5053 info->u.rsym.renamed = nuse ? 1 : 0;
5055 if (nuse == 0)
5056 nuse = 1;
5058 for (j = 1; j <= nuse; j++)
5060 /* Get the jth local name for this symbol. */
5061 p = find_use_name_n (name, &j, false);
5063 if (p == NULL && strcmp (name, module_name) == 0)
5064 p = name;
5066 /* Exception: Always import vtabs & vtypes. */
5067 if (p == NULL && name[0] == '_'
5068 && (strncmp (name, "__vtab_", 5) == 0
5069 || strncmp (name, "__vtype_", 6) == 0))
5070 p = name;
5072 /* Skip symtree nodes not in an ONLY clause, unless there
5073 is an existing symtree loaded from another USE statement. */
5074 if (p == NULL)
5076 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5077 if (st != NULL
5078 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5079 && st->n.sym->module != NULL
5080 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5082 info->u.rsym.symtree = st;
5083 info->u.rsym.sym = st->n.sym;
5085 continue;
5088 /* If a symbol of the same name and module exists already,
5089 this symbol, which is not in an ONLY clause, must not be
5090 added to the namespace(11.3.2). Note that find_symbol
5091 only returns the first occurrence that it finds. */
5092 if (!only_flag && !info->u.rsym.renamed
5093 && strcmp (name, module_name) != 0
5094 && find_symbol (gfc_current_ns->sym_root, name,
5095 module_name, 0))
5096 continue;
5098 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5100 if (st != NULL)
5102 /* Check for ambiguous symbols. */
5103 if (check_for_ambiguous (st->n.sym, info))
5104 st->ambiguous = 1;
5105 else
5106 info->u.rsym.symtree = st;
5108 else
5110 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5112 /* Create a symtree node in the current namespace for this
5113 symbol. */
5114 st = check_unique_name (p)
5115 ? gfc_get_unique_symtree (gfc_current_ns)
5116 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5117 st->ambiguous = ambiguous;
5119 sym = info->u.rsym.sym;
5121 /* Create a symbol node if it doesn't already exist. */
5122 if (sym == NULL)
5124 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5125 gfc_current_ns);
5126 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
5127 sym = info->u.rsym.sym;
5128 sym->module = gfc_get_string (info->u.rsym.module);
5130 if (info->u.rsym.binding_label)
5131 sym->binding_label =
5132 IDENTIFIER_POINTER (get_identifier
5133 (info->u.rsym.binding_label));
5136 st->n.sym = sym;
5137 st->n.sym->refs++;
5139 if (strcmp (name, p) != 0)
5140 sym->attr.use_rename = 1;
5142 if (name[0] != '_'
5143 || (strncmp (name, "__vtab_", 5) != 0
5144 && strncmp (name, "__vtype_", 6) != 0))
5145 sym->attr.use_only = only_flag;
5147 /* Store the symtree pointing to this symbol. */
5148 info->u.rsym.symtree = st;
5150 if (info->u.rsym.state == UNUSED)
5151 info->u.rsym.state = NEEDED;
5152 info->u.rsym.referenced = 1;
5157 mio_rparen ();
5159 /* Load intrinsic operator interfaces. */
5160 set_module_locus (&operator_interfaces);
5161 mio_lparen ();
5163 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5165 if (i == INTRINSIC_USER)
5166 continue;
5168 if (only_flag)
5170 u = find_use_operator ((gfc_intrinsic_op) i);
5172 if (u == NULL)
5174 skip_list ();
5175 continue;
5178 u->found = 1;
5181 mio_interface (&gfc_current_ns->op[i]);
5182 if (u && !gfc_current_ns->op[i])
5183 u->found = 0;
5186 mio_rparen ();
5188 /* Load generic and user operator interfaces. These must follow the
5189 loading of symtree because otherwise symbols can be marked as
5190 ambiguous. */
5192 set_module_locus (&user_operators);
5194 load_operator_interfaces ();
5195 load_generic_interfaces ();
5197 load_commons ();
5198 load_equiv ();
5200 /* Load OpenMP user defined reductions. */
5201 set_module_locus (&omp_udrs);
5202 load_omp_udrs ();
5204 /* At this point, we read those symbols that are needed but haven't
5205 been loaded yet. If one symbol requires another, the other gets
5206 marked as NEEDED if its previous state was UNUSED. */
5208 while (load_needed (pi_root));
5210 /* Make sure all elements of the rename-list were found in the module. */
5212 for (u = gfc_rename_list; u; u = u->next)
5214 if (u->found)
5215 continue;
5217 if (u->op == INTRINSIC_NONE)
5219 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5220 u->use_name, &u->where, module_name);
5221 continue;
5224 if (u->op == INTRINSIC_USER)
5226 gfc_error ("User operator %qs referenced at %L not found "
5227 "in module %qs", u->use_name, &u->where, module_name);
5228 continue;
5231 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5232 "in module %qs", gfc_op2string (u->op), &u->where,
5233 module_name);
5236 /* Now we should be in a position to fill f2k_derived with derived type
5237 extensions, since everything has been loaded. */
5238 set_module_locus (&extensions);
5239 load_derived_extensions ();
5241 /* Clean up symbol nodes that were never loaded, create references
5242 to hidden symbols. */
5244 read_cleanup (pi_root);
5248 /* Given an access type that is specific to an entity and the default
5249 access, return nonzero if the entity is publicly accessible. If the
5250 element is declared as PUBLIC, then it is public; if declared
5251 PRIVATE, then private, and otherwise it is public unless the default
5252 access in this context has been declared PRIVATE. */
5254 static bool
5255 check_access (gfc_access specific_access, gfc_access default_access)
5257 if (specific_access == ACCESS_PUBLIC)
5258 return TRUE;
5259 if (specific_access == ACCESS_PRIVATE)
5260 return FALSE;
5262 if (flag_module_private)
5263 return default_access == ACCESS_PUBLIC;
5264 else
5265 return default_access != ACCESS_PRIVATE;
5269 bool
5270 gfc_check_symbol_access (gfc_symbol *sym)
5272 if (sym->attr.vtab || sym->attr.vtype)
5273 return true;
5274 else
5275 return check_access (sym->attr.access, sym->ns->default_access);
5279 /* A structure to remember which commons we've already written. */
5281 struct written_common
5283 BBT_HEADER(written_common);
5284 const char *name, *label;
5287 static struct written_common *written_commons = NULL;
5289 /* Comparison function used for balancing the binary tree. */
5291 static int
5292 compare_written_commons (void *a1, void *b1)
5294 const char *aname = ((struct written_common *) a1)->name;
5295 const char *alabel = ((struct written_common *) a1)->label;
5296 const char *bname = ((struct written_common *) b1)->name;
5297 const char *blabel = ((struct written_common *) b1)->label;
5298 int c = strcmp (aname, bname);
5300 return (c != 0 ? c : strcmp (alabel, blabel));
5303 /* Free a list of written commons. */
5305 static void
5306 free_written_common (struct written_common *w)
5308 if (!w)
5309 return;
5311 if (w->left)
5312 free_written_common (w->left);
5313 if (w->right)
5314 free_written_common (w->right);
5316 free (w);
5319 /* Write a common block to the module -- recursive helper function. */
5321 static void
5322 write_common_0 (gfc_symtree *st, bool this_module)
5324 gfc_common_head *p;
5325 const char * name;
5326 int flags;
5327 const char *label;
5328 struct written_common *w;
5329 bool write_me = true;
5331 if (st == NULL)
5332 return;
5334 write_common_0 (st->left, this_module);
5336 /* We will write out the binding label, or "" if no label given. */
5337 name = st->n.common->name;
5338 p = st->n.common;
5339 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5341 /* Check if we've already output this common. */
5342 w = written_commons;
5343 while (w)
5345 int c = strcmp (name, w->name);
5346 c = (c != 0 ? c : strcmp (label, w->label));
5347 if (c == 0)
5348 write_me = false;
5350 w = (c < 0) ? w->left : w->right;
5353 if (this_module && p->use_assoc)
5354 write_me = false;
5356 if (write_me)
5358 /* Write the common to the module. */
5359 mio_lparen ();
5360 mio_pool_string (&name);
5362 mio_symbol_ref (&p->head);
5363 flags = p->saved ? 1 : 0;
5364 if (p->threadprivate)
5365 flags |= 2;
5366 mio_integer (&flags);
5368 /* Write out whether the common block is bind(c) or not. */
5369 mio_integer (&(p->is_bind_c));
5371 mio_pool_string (&label);
5372 mio_rparen ();
5374 /* Record that we have written this common. */
5375 w = XCNEW (struct written_common);
5376 w->name = p->name;
5377 w->label = label;
5378 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5381 write_common_0 (st->right, this_module);
5385 /* Write a common, by initializing the list of written commons, calling
5386 the recursive function write_common_0() and cleaning up afterwards. */
5388 static void
5389 write_common (gfc_symtree *st)
5391 written_commons = NULL;
5392 write_common_0 (st, true);
5393 write_common_0 (st, false);
5394 free_written_common (written_commons);
5395 written_commons = NULL;
5399 /* Write the blank common block to the module. */
5401 static void
5402 write_blank_common (void)
5404 const char * name = BLANK_COMMON_NAME;
5405 int saved;
5406 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5407 this, but it hasn't been checked. Just making it so for now. */
5408 int is_bind_c = 0;
5410 if (gfc_current_ns->blank_common.head == NULL)
5411 return;
5413 mio_lparen ();
5415 mio_pool_string (&name);
5417 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5418 saved = gfc_current_ns->blank_common.saved;
5419 mio_integer (&saved);
5421 /* Write out whether the common block is bind(c) or not. */
5422 mio_integer (&is_bind_c);
5424 /* Write out an empty binding label. */
5425 write_atom (ATOM_STRING, "");
5427 mio_rparen ();
5431 /* Write equivalences to the module. */
5433 static void
5434 write_equiv (void)
5436 gfc_equiv *eq, *e;
5437 int num;
5439 num = 0;
5440 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5442 mio_lparen ();
5444 for (e = eq; e; e = e->eq)
5446 if (e->module == NULL)
5447 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5448 mio_allocated_string (e->module);
5449 mio_expr (&e->expr);
5452 num++;
5453 mio_rparen ();
5458 /* Write derived type extensions to the module. */
5460 static void
5461 write_dt_extensions (gfc_symtree *st)
5463 if (!gfc_check_symbol_access (st->n.sym))
5464 return;
5465 if (!(st->n.sym->ns && st->n.sym->ns->proc_name
5466 && st->n.sym->ns->proc_name->attr.flavor == FL_MODULE))
5467 return;
5469 mio_lparen ();
5470 mio_pool_string (&st->name);
5471 if (st->n.sym->module != NULL)
5472 mio_pool_string (&st->n.sym->module);
5473 else
5475 char name[GFC_MAX_SYMBOL_LEN + 1];
5476 if (iomode == IO_OUTPUT)
5477 strcpy (name, module_name);
5478 mio_internal_string (name);
5479 if (iomode == IO_INPUT)
5480 module_name = gfc_get_string (name);
5482 mio_rparen ();
5485 static void
5486 write_derived_extensions (gfc_symtree *st)
5488 if (!((st->n.sym->attr.flavor == FL_DERIVED)
5489 && (st->n.sym->f2k_derived != NULL)
5490 && (st->n.sym->f2k_derived->sym_root != NULL)))
5491 return;
5493 mio_lparen ();
5494 mio_symbol_ref (&(st->n.sym));
5495 gfc_traverse_symtree (st->n.sym->f2k_derived->sym_root,
5496 write_dt_extensions);
5497 mio_rparen ();
5501 /* Write a symbol to the module. */
5503 static void
5504 write_symbol (int n, gfc_symbol *sym)
5506 const char *label;
5508 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5509 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5511 mio_integer (&n);
5513 if (sym->attr.flavor == FL_DERIVED)
5515 const char *name;
5516 name = dt_upper_string (sym->name);
5517 mio_pool_string (&name);
5519 else
5520 mio_pool_string (&sym->name);
5522 mio_pool_string (&sym->module);
5523 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5525 label = sym->binding_label;
5526 mio_pool_string (&label);
5528 else
5529 write_atom (ATOM_STRING, "");
5531 mio_pointer_ref (&sym->ns);
5533 mio_symbol (sym);
5534 write_char ('\n');
5538 /* Recursive traversal function to write the initial set of symbols to
5539 the module. We check to see if the symbol should be written
5540 according to the access specification. */
5542 static void
5543 write_symbol0 (gfc_symtree *st)
5545 gfc_symbol *sym;
5546 pointer_info *p;
5547 bool dont_write = false;
5549 if (st == NULL)
5550 return;
5552 write_symbol0 (st->left);
5554 sym = st->n.sym;
5555 if (sym->module == NULL)
5556 sym->module = module_name;
5558 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5559 && !sym->attr.subroutine && !sym->attr.function)
5560 dont_write = true;
5562 if (!gfc_check_symbol_access (sym))
5563 dont_write = true;
5565 if (!dont_write)
5567 p = get_pointer (sym);
5568 if (p->type == P_UNKNOWN)
5569 p->type = P_SYMBOL;
5571 if (p->u.wsym.state != WRITTEN)
5573 write_symbol (p->integer, sym);
5574 p->u.wsym.state = WRITTEN;
5578 write_symbol0 (st->right);
5582 static void
5583 write_omp_udr (gfc_omp_udr *udr)
5585 switch (udr->rop)
5587 case OMP_REDUCTION_USER:
5588 /* Non-operators can't be used outside of the module. */
5589 if (udr->name[0] != '.')
5590 return;
5591 else
5593 gfc_symtree *st;
5594 size_t len = strlen (udr->name + 1);
5595 char *name = XALLOCAVEC (char, len);
5596 memcpy (name, udr->name, len - 1);
5597 name[len - 1] = '\0';
5598 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5599 /* If corresponding user operator is private, don't write
5600 the UDR. */
5601 if (st != NULL)
5603 gfc_user_op *uop = st->n.uop;
5604 if (!check_access (uop->access, uop->ns->default_access))
5605 return;
5608 break;
5609 case OMP_REDUCTION_PLUS:
5610 case OMP_REDUCTION_MINUS:
5611 case OMP_REDUCTION_TIMES:
5612 case OMP_REDUCTION_AND:
5613 case OMP_REDUCTION_OR:
5614 case OMP_REDUCTION_EQV:
5615 case OMP_REDUCTION_NEQV:
5616 /* If corresponding operator is private, don't write the UDR. */
5617 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5618 gfc_current_ns->default_access))
5619 return;
5620 break;
5621 default:
5622 break;
5624 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5626 /* If derived type is private, don't write the UDR. */
5627 if (!gfc_check_symbol_access (udr->ts.u.derived))
5628 return;
5631 mio_lparen ();
5632 mio_pool_string (&udr->name);
5633 mio_typespec (&udr->ts);
5634 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5635 if (udr->initializer_ns)
5636 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5637 udr->initializer_ns, true);
5638 mio_rparen ();
5642 static void
5643 write_omp_udrs (gfc_symtree *st)
5645 if (st == NULL)
5646 return;
5648 write_omp_udrs (st->left);
5649 gfc_omp_udr *udr;
5650 for (udr = st->n.omp_udr; udr; udr = udr->next)
5651 write_omp_udr (udr);
5652 write_omp_udrs (st->right);
5656 /* Type for the temporary tree used when writing secondary symbols. */
5658 struct sorted_pointer_info
5660 BBT_HEADER (sorted_pointer_info);
5662 pointer_info *p;
5665 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5667 /* Recursively traverse the temporary tree, free its contents. */
5669 static void
5670 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5672 if (!p)
5673 return;
5675 free_sorted_pointer_info_tree (p->left);
5676 free_sorted_pointer_info_tree (p->right);
5678 free (p);
5681 /* Comparison function for the temporary tree. */
5683 static int
5684 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5686 sorted_pointer_info *spi1, *spi2;
5687 spi1 = (sorted_pointer_info *)_spi1;
5688 spi2 = (sorted_pointer_info *)_spi2;
5690 if (spi1->p->integer < spi2->p->integer)
5691 return -1;
5692 if (spi1->p->integer > spi2->p->integer)
5693 return 1;
5694 return 0;
5698 /* Finds the symbols that need to be written and collects them in the
5699 sorted_pi tree so that they can be traversed in an order
5700 independent of memory addresses. */
5702 static void
5703 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5705 if (!p)
5706 return;
5708 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5710 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5711 sp->p = p;
5713 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5716 find_symbols_to_write (tree, p->left);
5717 find_symbols_to_write (tree, p->right);
5721 /* Recursive function that traverses the tree of symbols that need to be
5722 written and writes them in order. */
5724 static void
5725 write_symbol1_recursion (sorted_pointer_info *sp)
5727 if (!sp)
5728 return;
5730 write_symbol1_recursion (sp->left);
5732 pointer_info *p1 = sp->p;
5733 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5735 p1->u.wsym.state = WRITTEN;
5736 write_symbol (p1->integer, p1->u.wsym.sym);
5737 p1->u.wsym.sym->attr.public_used = 1;
5739 write_symbol1_recursion (sp->right);
5743 /* Write the secondary set of symbols to the module file. These are
5744 symbols that were not public yet are needed by the public symbols
5745 or another dependent symbol. The act of writing a symbol can add
5746 symbols to the pointer_info tree, so we return nonzero if a symbol
5747 was written and pass that information upwards. The caller will
5748 then call this function again until nothing was written. It uses
5749 the utility functions and a temporary tree to ensure a reproducible
5750 ordering of the symbol output and thus the module file. */
5752 static int
5753 write_symbol1 (pointer_info *p)
5755 if (!p)
5756 return 0;
5758 /* Put symbols that need to be written into a tree sorted on the
5759 integer field. */
5761 sorted_pointer_info *spi_root = NULL;
5762 find_symbols_to_write (&spi_root, p);
5764 /* No symbols to write, return. */
5765 if (!spi_root)
5766 return 0;
5768 /* Otherwise, write and free the tree again. */
5769 write_symbol1_recursion (spi_root);
5770 free_sorted_pointer_info_tree (spi_root);
5772 return 1;
5776 /* Write operator interfaces associated with a symbol. */
5778 static void
5779 write_operator (gfc_user_op *uop)
5781 static char nullstring[] = "";
5782 const char *p = nullstring;
5784 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5785 return;
5787 mio_symbol_interface (&uop->name, &p, &uop->op);
5791 /* Write generic interfaces from the namespace sym_root. */
5793 static void
5794 write_generic (gfc_symtree *st)
5796 gfc_symbol *sym;
5798 if (st == NULL)
5799 return;
5801 write_generic (st->left);
5803 sym = st->n.sym;
5804 if (sym && !check_unique_name (st->name)
5805 && sym->generic && gfc_check_symbol_access (sym))
5807 if (!sym->module)
5808 sym->module = module_name;
5810 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5813 write_generic (st->right);
5817 static void
5818 write_symtree (gfc_symtree *st)
5820 gfc_symbol *sym;
5821 pointer_info *p;
5823 sym = st->n.sym;
5825 /* A symbol in an interface body must not be visible in the
5826 module file. */
5827 if (sym->ns != gfc_current_ns
5828 && sym->ns->proc_name
5829 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5830 return;
5832 if (!gfc_check_symbol_access (sym)
5833 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5834 && !sym->attr.subroutine && !sym->attr.function))
5835 return;
5837 if (check_unique_name (st->name))
5838 return;
5840 p = find_pointer (sym);
5841 if (p == NULL)
5842 gfc_internal_error ("write_symtree(): Symbol not written");
5844 mio_pool_string (&st->name);
5845 mio_integer (&st->ambiguous);
5846 mio_integer (&p->integer);
5850 static void
5851 write_module (void)
5853 int i;
5855 /* Write the operator interfaces. */
5856 mio_lparen ();
5858 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5860 if (i == INTRINSIC_USER)
5861 continue;
5863 mio_interface (check_access (gfc_current_ns->operator_access[i],
5864 gfc_current_ns->default_access)
5865 ? &gfc_current_ns->op[i] : NULL);
5868 mio_rparen ();
5869 write_char ('\n');
5870 write_char ('\n');
5872 mio_lparen ();
5873 gfc_traverse_user_op (gfc_current_ns, write_operator);
5874 mio_rparen ();
5875 write_char ('\n');
5876 write_char ('\n');
5878 mio_lparen ();
5879 write_generic (gfc_current_ns->sym_root);
5880 mio_rparen ();
5881 write_char ('\n');
5882 write_char ('\n');
5884 mio_lparen ();
5885 write_blank_common ();
5886 write_common (gfc_current_ns->common_root);
5887 mio_rparen ();
5888 write_char ('\n');
5889 write_char ('\n');
5891 mio_lparen ();
5892 write_equiv ();
5893 mio_rparen ();
5894 write_char ('\n');
5895 write_char ('\n');
5897 mio_lparen ();
5898 gfc_traverse_symtree (gfc_current_ns->sym_root,
5899 write_derived_extensions);
5900 mio_rparen ();
5901 write_char ('\n');
5902 write_char ('\n');
5904 mio_lparen ();
5905 write_omp_udrs (gfc_current_ns->omp_udr_root);
5906 mio_rparen ();
5907 write_char ('\n');
5908 write_char ('\n');
5910 /* Write symbol information. First we traverse all symbols in the
5911 primary namespace, writing those that need to be written.
5912 Sometimes writing one symbol will cause another to need to be
5913 written. A list of these symbols ends up on the write stack, and
5914 we end by popping the bottom of the stack and writing the symbol
5915 until the stack is empty. */
5917 mio_lparen ();
5919 write_symbol0 (gfc_current_ns->sym_root);
5920 while (write_symbol1 (pi_root))
5921 /* Nothing. */;
5923 mio_rparen ();
5925 write_char ('\n');
5926 write_char ('\n');
5928 mio_lparen ();
5929 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5930 mio_rparen ();
5934 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5935 true on success, false on failure. */
5937 static bool
5938 read_crc32_from_module_file (const char* filename, uLong* crc)
5940 FILE *file;
5941 char buf[4];
5942 unsigned int val;
5944 /* Open the file in binary mode. */
5945 if ((file = fopen (filename, "rb")) == NULL)
5946 return false;
5948 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5949 file. See RFC 1952. */
5950 if (fseek (file, -8, SEEK_END) != 0)
5952 fclose (file);
5953 return false;
5956 /* Read the CRC32. */
5957 if (fread (buf, 1, 4, file) != 4)
5959 fclose (file);
5960 return false;
5963 /* Close the file. */
5964 fclose (file);
5966 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5967 + ((buf[3] & 0xFF) << 24);
5968 *crc = val;
5970 /* For debugging, the CRC value printed in hexadecimal should match
5971 the CRC printed by "zcat -l -v filename".
5972 printf("CRC of file %s is %x\n", filename, val); */
5974 return true;
5978 /* Given module, dump it to disk. If there was an error while
5979 processing the module, dump_flag will be set to zero and we delete
5980 the module file, even if it was already there. */
5982 void
5983 gfc_dump_module (const char *name, int dump_flag)
5985 int n;
5986 char *filename, *filename_tmp;
5987 uLong crc, crc_old;
5989 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5990 if (gfc_option.module_dir != NULL)
5992 n += strlen (gfc_option.module_dir);
5993 filename = (char *) alloca (n);
5994 strcpy (filename, gfc_option.module_dir);
5995 strcat (filename, name);
5997 else
5999 filename = (char *) alloca (n);
6000 strcpy (filename, name);
6002 strcat (filename, MODULE_EXTENSION);
6004 /* Name of the temporary file used to write the module. */
6005 filename_tmp = (char *) alloca (n + 1);
6006 strcpy (filename_tmp, filename);
6007 strcat (filename_tmp, "0");
6009 /* There was an error while processing the module. We delete the
6010 module file, even if it was already there. */
6011 if (!dump_flag)
6013 remove (filename);
6014 return;
6017 if (gfc_cpp_makedep ())
6018 gfc_cpp_add_target (filename);
6020 /* Write the module to the temporary file. */
6021 module_fp = gzopen (filename_tmp, "w");
6022 if (module_fp == NULL)
6023 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6024 filename_tmp, xstrerror (errno));
6026 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6027 MOD_VERSION, gfc_source_file);
6029 /* Write the module itself. */
6030 iomode = IO_OUTPUT;
6031 module_name = gfc_get_string (name);
6033 init_pi_tree ();
6035 write_module ();
6037 free_pi_tree (pi_root);
6038 pi_root = NULL;
6040 write_char ('\n');
6042 if (gzclose (module_fp))
6043 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6044 filename_tmp, xstrerror (errno));
6046 /* Read the CRC32 from the gzip trailers of the module files and
6047 compare. */
6048 if (!read_crc32_from_module_file (filename_tmp, &crc)
6049 || !read_crc32_from_module_file (filename, &crc_old)
6050 || crc_old != crc)
6052 /* Module file have changed, replace the old one. */
6053 if (remove (filename) && errno != ENOENT)
6054 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6055 xstrerror (errno));
6056 if (rename (filename_tmp, filename))
6057 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6058 filename_tmp, filename, xstrerror (errno));
6060 else
6062 if (remove (filename_tmp))
6063 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6064 filename_tmp, xstrerror (errno));
6069 static void
6070 create_intrinsic_function (const char *name, int id,
6071 const char *modname, intmod_id module,
6072 bool subroutine, gfc_symbol *result_type)
6074 gfc_intrinsic_sym *isym;
6075 gfc_symtree *tmp_symtree;
6076 gfc_symbol *sym;
6078 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6079 if (tmp_symtree)
6081 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6082 return;
6083 gfc_error ("Symbol %qs already declared", name);
6086 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6087 sym = tmp_symtree->n.sym;
6089 if (subroutine)
6091 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6092 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6093 sym->attr.subroutine = 1;
6095 else
6097 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6098 isym = gfc_intrinsic_function_by_id (isym_id);
6100 sym->attr.function = 1;
6101 if (result_type)
6103 sym->ts.type = BT_DERIVED;
6104 sym->ts.u.derived = result_type;
6105 sym->ts.is_c_interop = 1;
6106 isym->ts.f90_type = BT_VOID;
6107 isym->ts.type = BT_DERIVED;
6108 isym->ts.f90_type = BT_VOID;
6109 isym->ts.u.derived = result_type;
6110 isym->ts.is_c_interop = 1;
6113 gcc_assert (isym);
6115 sym->attr.flavor = FL_PROCEDURE;
6116 sym->attr.intrinsic = 1;
6118 sym->module = gfc_get_string (modname);
6119 sym->attr.use_assoc = 1;
6120 sym->from_intmod = module;
6121 sym->intmod_sym_id = id;
6125 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6126 the current namespace for all named constants, pointer types, and
6127 procedures in the module unless the only clause was used or a rename
6128 list was provided. */
6130 static void
6131 import_iso_c_binding_module (void)
6133 gfc_symbol *mod_sym = NULL, *return_type;
6134 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6135 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6136 const char *iso_c_module_name = "__iso_c_binding";
6137 gfc_use_rename *u;
6138 int i;
6139 bool want_c_ptr = false, want_c_funptr = false;
6141 /* Look only in the current namespace. */
6142 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6144 if (mod_symtree == NULL)
6146 /* symtree doesn't already exist in current namespace. */
6147 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6148 false);
6150 if (mod_symtree != NULL)
6151 mod_sym = mod_symtree->n.sym;
6152 else
6153 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6154 "create symbol for %s", iso_c_module_name);
6156 mod_sym->attr.flavor = FL_MODULE;
6157 mod_sym->attr.intrinsic = 1;
6158 mod_sym->module = gfc_get_string (iso_c_module_name);
6159 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6162 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6163 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6164 need C_(FUN)PTR. */
6165 for (u = gfc_rename_list; u; u = u->next)
6167 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6168 u->use_name) == 0)
6169 want_c_ptr = true;
6170 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6171 u->use_name) == 0)
6172 want_c_ptr = true;
6173 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6174 u->use_name) == 0)
6175 want_c_funptr = true;
6176 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6177 u->use_name) == 0)
6178 want_c_funptr = true;
6179 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6180 u->use_name) == 0)
6182 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6183 (iso_c_binding_symbol)
6184 ISOCBINDING_PTR,
6185 u->local_name[0] ? u->local_name
6186 : u->use_name,
6187 NULL, false);
6189 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6190 u->use_name) == 0)
6192 c_funptr
6193 = generate_isocbinding_symbol (iso_c_module_name,
6194 (iso_c_binding_symbol)
6195 ISOCBINDING_FUNPTR,
6196 u->local_name[0] ? u->local_name
6197 : u->use_name,
6198 NULL, false);
6202 if ((want_c_ptr || !only_flag) && !c_ptr)
6203 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6204 (iso_c_binding_symbol)
6205 ISOCBINDING_PTR,
6206 NULL, NULL, only_flag);
6207 if ((want_c_funptr || !only_flag) && !c_funptr)
6208 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6209 (iso_c_binding_symbol)
6210 ISOCBINDING_FUNPTR,
6211 NULL, NULL, only_flag);
6213 /* Generate the symbols for the named constants representing
6214 the kinds for intrinsic data types. */
6215 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6217 bool found = false;
6218 for (u = gfc_rename_list; u; u = u->next)
6219 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6221 bool not_in_std;
6222 const char *name;
6223 u->found = 1;
6224 found = true;
6226 switch (i)
6228 #define NAMED_FUNCTION(a,b,c,d) \
6229 case a: \
6230 not_in_std = (gfc_option.allow_std & d) == 0; \
6231 name = b; \
6232 break;
6233 #define NAMED_SUBROUTINE(a,b,c,d) \
6234 case a: \
6235 not_in_std = (gfc_option.allow_std & d) == 0; \
6236 name = b; \
6237 break;
6238 #define NAMED_INTCST(a,b,c,d) \
6239 case a: \
6240 not_in_std = (gfc_option.allow_std & d) == 0; \
6241 name = b; \
6242 break;
6243 #define NAMED_REALCST(a,b,c,d) \
6244 case a: \
6245 not_in_std = (gfc_option.allow_std & d) == 0; \
6246 name = b; \
6247 break;
6248 #define NAMED_CMPXCST(a,b,c,d) \
6249 case a: \
6250 not_in_std = (gfc_option.allow_std & d) == 0; \
6251 name = b; \
6252 break;
6253 #include "iso-c-binding.def"
6254 default:
6255 not_in_std = false;
6256 name = "";
6259 if (not_in_std)
6261 gfc_error ("The symbol %qs, referenced at %L, is not "
6262 "in the selected standard", name, &u->where);
6263 continue;
6266 switch (i)
6268 #define NAMED_FUNCTION(a,b,c,d) \
6269 case a: \
6270 if (a == ISOCBINDING_LOC) \
6271 return_type = c_ptr->n.sym; \
6272 else if (a == ISOCBINDING_FUNLOC) \
6273 return_type = c_funptr->n.sym; \
6274 else \
6275 return_type = NULL; \
6276 create_intrinsic_function (u->local_name[0] \
6277 ? u->local_name : u->use_name, \
6278 a, iso_c_module_name, \
6279 INTMOD_ISO_C_BINDING, false, \
6280 return_type); \
6281 break;
6282 #define NAMED_SUBROUTINE(a,b,c,d) \
6283 case a: \
6284 create_intrinsic_function (u->local_name[0] ? u->local_name \
6285 : u->use_name, \
6286 a, iso_c_module_name, \
6287 INTMOD_ISO_C_BINDING, true, NULL); \
6288 break;
6289 #include "iso-c-binding.def"
6291 case ISOCBINDING_PTR:
6292 case ISOCBINDING_FUNPTR:
6293 /* Already handled above. */
6294 break;
6295 default:
6296 if (i == ISOCBINDING_NULL_PTR)
6297 tmp_symtree = c_ptr;
6298 else if (i == ISOCBINDING_NULL_FUNPTR)
6299 tmp_symtree = c_funptr;
6300 else
6301 tmp_symtree = NULL;
6302 generate_isocbinding_symbol (iso_c_module_name,
6303 (iso_c_binding_symbol) i,
6304 u->local_name[0]
6305 ? u->local_name : u->use_name,
6306 tmp_symtree, false);
6310 if (!found && !only_flag)
6312 /* Skip, if the symbol is not in the enabled standard. */
6313 switch (i)
6315 #define NAMED_FUNCTION(a,b,c,d) \
6316 case a: \
6317 if ((gfc_option.allow_std & d) == 0) \
6318 continue; \
6319 break;
6320 #define NAMED_SUBROUTINE(a,b,c,d) \
6321 case a: \
6322 if ((gfc_option.allow_std & d) == 0) \
6323 continue; \
6324 break;
6325 #define NAMED_INTCST(a,b,c,d) \
6326 case a: \
6327 if ((gfc_option.allow_std & d) == 0) \
6328 continue; \
6329 break;
6330 #define NAMED_REALCST(a,b,c,d) \
6331 case a: \
6332 if ((gfc_option.allow_std & d) == 0) \
6333 continue; \
6334 break;
6335 #define NAMED_CMPXCST(a,b,c,d) \
6336 case a: \
6337 if ((gfc_option.allow_std & d) == 0) \
6338 continue; \
6339 break;
6340 #include "iso-c-binding.def"
6341 default:
6342 ; /* Not GFC_STD_* versioned. */
6345 switch (i)
6347 #define NAMED_FUNCTION(a,b,c,d) \
6348 case a: \
6349 if (a == ISOCBINDING_LOC) \
6350 return_type = c_ptr->n.sym; \
6351 else if (a == ISOCBINDING_FUNLOC) \
6352 return_type = c_funptr->n.sym; \
6353 else \
6354 return_type = NULL; \
6355 create_intrinsic_function (b, a, iso_c_module_name, \
6356 INTMOD_ISO_C_BINDING, false, \
6357 return_type); \
6358 break;
6359 #define NAMED_SUBROUTINE(a,b,c,d) \
6360 case a: \
6361 create_intrinsic_function (b, a, iso_c_module_name, \
6362 INTMOD_ISO_C_BINDING, true, NULL); \
6363 break;
6364 #include "iso-c-binding.def"
6366 case ISOCBINDING_PTR:
6367 case ISOCBINDING_FUNPTR:
6368 /* Already handled above. */
6369 break;
6370 default:
6371 if (i == ISOCBINDING_NULL_PTR)
6372 tmp_symtree = c_ptr;
6373 else if (i == ISOCBINDING_NULL_FUNPTR)
6374 tmp_symtree = c_funptr;
6375 else
6376 tmp_symtree = NULL;
6377 generate_isocbinding_symbol (iso_c_module_name,
6378 (iso_c_binding_symbol) i, NULL,
6379 tmp_symtree, false);
6384 for (u = gfc_rename_list; u; u = u->next)
6386 if (u->found)
6387 continue;
6389 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6390 "module ISO_C_BINDING", u->use_name, &u->where);
6395 /* Add an integer named constant from a given module. */
6397 static void
6398 create_int_parameter (const char *name, int value, const char *modname,
6399 intmod_id module, int id)
6401 gfc_symtree *tmp_symtree;
6402 gfc_symbol *sym;
6404 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6405 if (tmp_symtree != NULL)
6407 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6408 return;
6409 else
6410 gfc_error ("Symbol %qs already declared", name);
6413 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6414 sym = tmp_symtree->n.sym;
6416 sym->module = gfc_get_string (modname);
6417 sym->attr.flavor = FL_PARAMETER;
6418 sym->ts.type = BT_INTEGER;
6419 sym->ts.kind = gfc_default_integer_kind;
6420 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6421 sym->attr.use_assoc = 1;
6422 sym->from_intmod = module;
6423 sym->intmod_sym_id = id;
6427 /* Value is already contained by the array constructor, but not
6428 yet the shape. */
6430 static void
6431 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6432 const char *modname, intmod_id module, int id)
6434 gfc_symtree *tmp_symtree;
6435 gfc_symbol *sym;
6437 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6438 if (tmp_symtree != NULL)
6440 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6441 return;
6442 else
6443 gfc_error ("Symbol %qs already declared", name);
6446 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6447 sym = tmp_symtree->n.sym;
6449 sym->module = gfc_get_string (modname);
6450 sym->attr.flavor = FL_PARAMETER;
6451 sym->ts.type = BT_INTEGER;
6452 sym->ts.kind = gfc_default_integer_kind;
6453 sym->attr.use_assoc = 1;
6454 sym->from_intmod = module;
6455 sym->intmod_sym_id = id;
6456 sym->attr.dimension = 1;
6457 sym->as = gfc_get_array_spec ();
6458 sym->as->rank = 1;
6459 sym->as->type = AS_EXPLICIT;
6460 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6461 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6463 sym->value = value;
6464 sym->value->shape = gfc_get_shape (1);
6465 mpz_init_set_ui (sym->value->shape[0], size);
6469 /* Add an derived type for a given module. */
6471 static void
6472 create_derived_type (const char *name, const char *modname,
6473 intmod_id module, int id)
6475 gfc_symtree *tmp_symtree;
6476 gfc_symbol *sym, *dt_sym;
6477 gfc_interface *intr, *head;
6479 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6480 if (tmp_symtree != NULL)
6482 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6483 return;
6484 else
6485 gfc_error ("Symbol %qs already declared", name);
6488 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6489 sym = tmp_symtree->n.sym;
6490 sym->module = gfc_get_string (modname);
6491 sym->from_intmod = module;
6492 sym->intmod_sym_id = id;
6493 sym->attr.flavor = FL_PROCEDURE;
6494 sym->attr.function = 1;
6495 sym->attr.generic = 1;
6497 gfc_get_sym_tree (dt_upper_string (sym->name),
6498 gfc_current_ns, &tmp_symtree, false);
6499 dt_sym = tmp_symtree->n.sym;
6500 dt_sym->name = gfc_get_string (sym->name);
6501 dt_sym->attr.flavor = FL_DERIVED;
6502 dt_sym->attr.private_comp = 1;
6503 dt_sym->attr.zero_comp = 1;
6504 dt_sym->attr.use_assoc = 1;
6505 dt_sym->module = gfc_get_string (modname);
6506 dt_sym->from_intmod = module;
6507 dt_sym->intmod_sym_id = id;
6509 head = sym->generic;
6510 intr = gfc_get_interface ();
6511 intr->sym = dt_sym;
6512 intr->where = gfc_current_locus;
6513 intr->next = head;
6514 sym->generic = intr;
6515 sym->attr.if_source = IFSRC_DECL;
6519 /* Read the contents of the module file into a temporary buffer. */
6521 static void
6522 read_module_to_tmpbuf ()
6524 /* We don't know the uncompressed size, so enlarge the buffer as
6525 needed. */
6526 int cursz = 4096;
6527 int rsize = cursz;
6528 int len = 0;
6530 module_content = XNEWVEC (char, cursz);
6532 while (1)
6534 int nread = gzread (module_fp, module_content + len, rsize);
6535 len += nread;
6536 if (nread < rsize)
6537 break;
6538 cursz *= 2;
6539 module_content = XRESIZEVEC (char, module_content, cursz);
6540 rsize = cursz - len;
6543 module_content = XRESIZEVEC (char, module_content, len + 1);
6544 module_content[len] = '\0';
6546 module_pos = 0;
6550 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6552 static void
6553 use_iso_fortran_env_module (void)
6555 static char mod[] = "iso_fortran_env";
6556 gfc_use_rename *u;
6557 gfc_symbol *mod_sym;
6558 gfc_symtree *mod_symtree;
6559 gfc_expr *expr;
6560 int i, j;
6562 intmod_sym symbol[] = {
6563 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6564 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6565 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6566 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6567 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6568 #include "iso-fortran-env.def"
6569 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6571 i = 0;
6572 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6573 #include "iso-fortran-env.def"
6575 /* Generate the symbol for the module itself. */
6576 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6577 if (mod_symtree == NULL)
6579 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6580 gcc_assert (mod_symtree);
6581 mod_sym = mod_symtree->n.sym;
6583 mod_sym->attr.flavor = FL_MODULE;
6584 mod_sym->attr.intrinsic = 1;
6585 mod_sym->module = gfc_get_string (mod);
6586 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6588 else
6589 if (!mod_symtree->n.sym->attr.intrinsic)
6590 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6591 "non-intrinsic module name used previously", mod);
6593 /* Generate the symbols for the module integer named constants. */
6595 for (i = 0; symbol[i].name; i++)
6597 bool found = false;
6598 for (u = gfc_rename_list; u; u = u->next)
6600 if (strcmp (symbol[i].name, u->use_name) == 0)
6602 found = true;
6603 u->found = 1;
6605 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6606 "referenced at %L, is not in the selected "
6607 "standard", symbol[i].name, &u->where))
6608 continue;
6610 if ((flag_default_integer || flag_default_real)
6611 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6612 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6613 "constant from intrinsic module "
6614 "ISO_FORTRAN_ENV at %L is incompatible with "
6615 "option %qs", &u->where,
6616 flag_default_integer
6617 ? "-fdefault-integer-8"
6618 : "-fdefault-real-8");
6619 switch (symbol[i].id)
6621 #define NAMED_INTCST(a,b,c,d) \
6622 case a:
6623 #include "iso-fortran-env.def"
6624 create_int_parameter (u->local_name[0] ? u->local_name
6625 : u->use_name,
6626 symbol[i].value, mod,
6627 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6628 break;
6630 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6631 case a:\
6632 expr = gfc_get_array_expr (BT_INTEGER, \
6633 gfc_default_integer_kind,\
6634 NULL); \
6635 for (j = 0; KINDS[j].kind != 0; j++) \
6636 gfc_constructor_append_expr (&expr->value.constructor, \
6637 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6638 KINDS[j].kind), NULL); \
6639 create_int_parameter_array (u->local_name[0] ? u->local_name \
6640 : u->use_name, \
6641 j, expr, mod, \
6642 INTMOD_ISO_FORTRAN_ENV, \
6643 symbol[i].id); \
6644 break;
6645 #include "iso-fortran-env.def"
6647 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6648 case a:
6649 #include "iso-fortran-env.def"
6650 create_derived_type (u->local_name[0] ? u->local_name
6651 : u->use_name,
6652 mod, INTMOD_ISO_FORTRAN_ENV,
6653 symbol[i].id);
6654 break;
6656 #define NAMED_FUNCTION(a,b,c,d) \
6657 case a:
6658 #include "iso-fortran-env.def"
6659 create_intrinsic_function (u->local_name[0] ? u->local_name
6660 : u->use_name,
6661 symbol[i].id, mod,
6662 INTMOD_ISO_FORTRAN_ENV, false,
6663 NULL);
6664 break;
6666 default:
6667 gcc_unreachable ();
6672 if (!found && !only_flag)
6674 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6675 continue;
6677 if ((flag_default_integer || flag_default_real)
6678 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6679 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6680 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6681 "incompatible with option %s",
6682 flag_default_integer
6683 ? "-fdefault-integer-8" : "-fdefault-real-8");
6685 switch (symbol[i].id)
6687 #define NAMED_INTCST(a,b,c,d) \
6688 case a:
6689 #include "iso-fortran-env.def"
6690 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6691 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6692 break;
6694 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6695 case a:\
6696 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6697 NULL); \
6698 for (j = 0; KINDS[j].kind != 0; j++) \
6699 gfc_constructor_append_expr (&expr->value.constructor, \
6700 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6701 KINDS[j].kind), NULL); \
6702 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6703 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6704 break;
6705 #include "iso-fortran-env.def"
6707 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6708 case a:
6709 #include "iso-fortran-env.def"
6710 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6711 symbol[i].id);
6712 break;
6714 #define NAMED_FUNCTION(a,b,c,d) \
6715 case a:
6716 #include "iso-fortran-env.def"
6717 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6718 INTMOD_ISO_FORTRAN_ENV, false,
6719 NULL);
6720 break;
6722 default:
6723 gcc_unreachable ();
6728 for (u = gfc_rename_list; u; u = u->next)
6730 if (u->found)
6731 continue;
6733 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6734 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6739 /* Process a USE directive. */
6741 static void
6742 gfc_use_module (gfc_use_list *module)
6744 char *filename;
6745 gfc_state_data *p;
6746 int c, line, start;
6747 gfc_symtree *mod_symtree;
6748 gfc_use_list *use_stmt;
6749 locus old_locus = gfc_current_locus;
6751 gfc_current_locus = module->where;
6752 module_name = module->module_name;
6753 gfc_rename_list = module->rename;
6754 only_flag = module->only_flag;
6755 current_intmod = INTMOD_NONE;
6757 if (!only_flag)
6758 gfc_warning_now (OPT_Wuse_without_only,
6759 "USE statement at %C has no ONLY qualifier");
6761 filename = XALLOCAVEC (char, strlen (module_name) + strlen (MODULE_EXTENSION)
6762 + 1);
6763 strcpy (filename, module_name);
6764 strcat (filename, MODULE_EXTENSION);
6766 /* First, try to find an non-intrinsic module, unless the USE statement
6767 specified that the module is intrinsic. */
6768 module_fp = NULL;
6769 if (!module->intrinsic)
6770 module_fp = gzopen_included_file (filename, true, true);
6772 /* Then, see if it's an intrinsic one, unless the USE statement
6773 specified that the module is non-intrinsic. */
6774 if (module_fp == NULL && !module->non_intrinsic)
6776 if (strcmp (module_name, "iso_fortran_env") == 0
6777 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6778 "intrinsic module at %C"))
6780 use_iso_fortran_env_module ();
6781 free_rename (module->rename);
6782 module->rename = NULL;
6783 gfc_current_locus = old_locus;
6784 module->intrinsic = true;
6785 return;
6788 if (strcmp (module_name, "iso_c_binding") == 0
6789 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6791 import_iso_c_binding_module();
6792 free_rename (module->rename);
6793 module->rename = NULL;
6794 gfc_current_locus = old_locus;
6795 module->intrinsic = true;
6796 return;
6799 module_fp = gzopen_intrinsic_module (filename);
6801 if (module_fp == NULL && module->intrinsic)
6802 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6803 module_name);
6805 /* Check for the IEEE modules, so we can mark their symbols
6806 accordingly when we read them. */
6807 if (strcmp (module_name, "ieee_features") == 0
6808 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
6810 current_intmod = INTMOD_IEEE_FEATURES;
6812 else if (strcmp (module_name, "ieee_exceptions") == 0
6813 && gfc_notify_std (GFC_STD_F2003,
6814 "IEEE_EXCEPTIONS module at %C"))
6816 current_intmod = INTMOD_IEEE_EXCEPTIONS;
6818 else if (strcmp (module_name, "ieee_arithmetic") == 0
6819 && gfc_notify_std (GFC_STD_F2003,
6820 "IEEE_ARITHMETIC module at %C"))
6822 current_intmod = INTMOD_IEEE_ARITHMETIC;
6826 if (module_fp == NULL)
6827 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6828 filename, xstrerror (errno));
6830 /* Check that we haven't already USEd an intrinsic module with the
6831 same name. */
6833 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6834 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6835 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6836 "intrinsic module name used previously", module_name);
6838 iomode = IO_INPUT;
6839 module_line = 1;
6840 module_column = 1;
6841 start = 0;
6843 read_module_to_tmpbuf ();
6844 gzclose (module_fp);
6846 /* Skip the first line of the module, after checking that this is
6847 a gfortran module file. */
6848 line = 0;
6849 while (line < 1)
6851 c = module_char ();
6852 if (c == EOF)
6853 bad_module ("Unexpected end of module");
6854 if (start++ < 3)
6855 parse_name (c);
6856 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6857 || (start == 2 && strcmp (atom_name, " module") != 0))
6858 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6859 " module file", filename);
6860 if (start == 3)
6862 if (strcmp (atom_name, " version") != 0
6863 || module_char () != ' '
6864 || parse_atom () != ATOM_STRING
6865 || strcmp (atom_string, MOD_VERSION))
6866 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6867 " because it was created by a different"
6868 " version of GNU Fortran", filename);
6870 free (atom_string);
6873 if (c == '\n')
6874 line++;
6877 /* Make sure we're not reading the same module that we may be building. */
6878 for (p = gfc_state_stack; p; p = p->previous)
6879 if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
6880 gfc_fatal_error ("Can't USE the same module we're building!");
6882 init_pi_tree ();
6883 init_true_name_tree ();
6885 read_module ();
6887 free_true_name (true_name_root);
6888 true_name_root = NULL;
6890 free_pi_tree (pi_root);
6891 pi_root = NULL;
6893 XDELETEVEC (module_content);
6894 module_content = NULL;
6896 use_stmt = gfc_get_use_list ();
6897 *use_stmt = *module;
6898 use_stmt->next = gfc_current_ns->use_stmts;
6899 gfc_current_ns->use_stmts = use_stmt;
6901 gfc_current_locus = old_locus;
6905 /* Remove duplicated intrinsic operators from the rename list. */
6907 static void
6908 rename_list_remove_duplicate (gfc_use_rename *list)
6910 gfc_use_rename *seek, *last;
6912 for (; list; list = list->next)
6913 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6915 last = list;
6916 for (seek = list->next; seek; seek = last->next)
6918 if (list->op == seek->op)
6920 last->next = seek->next;
6921 free (seek);
6923 else
6924 last = seek;
6930 /* Process all USE directives. */
6932 void
6933 gfc_use_modules (void)
6935 gfc_use_list *next, *seek, *last;
6937 for (next = module_list; next; next = next->next)
6939 bool non_intrinsic = next->non_intrinsic;
6940 bool intrinsic = next->intrinsic;
6941 bool neither = !non_intrinsic && !intrinsic;
6943 for (seek = next->next; seek; seek = seek->next)
6945 if (next->module_name != seek->module_name)
6946 continue;
6948 if (seek->non_intrinsic)
6949 non_intrinsic = true;
6950 else if (seek->intrinsic)
6951 intrinsic = true;
6952 else
6953 neither = true;
6956 if (intrinsic && neither && !non_intrinsic)
6958 char *filename;
6959 FILE *fp;
6961 filename = XALLOCAVEC (char,
6962 strlen (next->module_name)
6963 + strlen (MODULE_EXTENSION) + 1);
6964 strcpy (filename, next->module_name);
6965 strcat (filename, MODULE_EXTENSION);
6966 fp = gfc_open_included_file (filename, true, true);
6967 if (fp != NULL)
6969 non_intrinsic = true;
6970 fclose (fp);
6974 last = next;
6975 for (seek = next->next; seek; seek = last->next)
6977 if (next->module_name != seek->module_name)
6979 last = seek;
6980 continue;
6983 if ((!next->intrinsic && !seek->intrinsic)
6984 || (next->intrinsic && seek->intrinsic)
6985 || !non_intrinsic)
6987 if (!seek->only_flag)
6988 next->only_flag = false;
6989 if (seek->rename)
6991 gfc_use_rename *r = seek->rename;
6992 while (r->next)
6993 r = r->next;
6994 r->next = next->rename;
6995 next->rename = seek->rename;
6997 last->next = seek->next;
6998 free (seek);
7000 else
7001 last = seek;
7005 for (; module_list; module_list = next)
7007 next = module_list->next;
7008 rename_list_remove_duplicate (module_list->rename);
7009 gfc_use_module (module_list);
7010 free (module_list);
7012 gfc_rename_list = NULL;
7016 void
7017 gfc_free_use_stmts (gfc_use_list *use_stmts)
7019 gfc_use_list *next;
7020 for (; use_stmts; use_stmts = next)
7022 gfc_use_rename *next_rename;
7024 for (; use_stmts->rename; use_stmts->rename = next_rename)
7026 next_rename = use_stmts->rename->next;
7027 free (use_stmts->rename);
7029 next = use_stmts->next;
7030 free (use_stmts);
7035 void
7036 gfc_module_init_2 (void)
7038 last_atom = ATOM_LPAREN;
7039 gfc_rename_list = NULL;
7040 module_list = NULL;
7044 void
7045 gfc_module_done_2 (void)
7047 free_rename (gfc_rename_list);
7048 gfc_rename_list = NULL;