remove more useless typedefs
[official-gcc.git] / gcc / fortran / module.c
blob621ef36d170299547bd01a7f70dfb497ec3a1a1e
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "gfortran.h"
71 #include "arith.h"
72 #include "match.h"
73 #include "parse.h" /* FIXME */
74 #include "constructor.h"
75 #include "cpp.h"
76 #include "alias.h"
77 #include "tree.h"
78 #include "options.h"
79 #include "stringpool.h"
80 #include "scanner.h"
81 #include <zlib.h>
83 #define MODULE_EXTENSION ".mod"
84 #define SUBMODULE_EXTENSION ".smod"
86 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
87 recognized. */
88 #define MOD_VERSION "14"
91 /* Structure that describes a position within a module file. */
93 typedef struct
95 int column, line;
96 long pos;
98 module_locus;
100 /* Structure for list of symbols of intrinsic modules. */
101 typedef struct
103 int id;
104 const char *name;
105 int value;
106 int standard;
108 intmod_sym;
111 typedef enum
113 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
115 pointer_t;
117 /* The fixup structure lists pointers to pointers that have to
118 be updated when a pointer value becomes known. */
120 typedef struct fixup_t
122 void **pointer;
123 struct fixup_t *next;
125 fixup_t;
128 /* Structure for holding extra info needed for pointers being read. */
130 enum gfc_rsym_state
132 UNUSED,
133 NEEDED,
134 USED
137 enum gfc_wsym_state
139 UNREFERENCED = 0,
140 NEEDS_WRITE,
141 WRITTEN
144 typedef struct pointer_info
146 BBT_HEADER (pointer_info);
147 int integer;
148 pointer_t type;
150 /* The first component of each member of the union is the pointer
151 being stored. */
153 fixup_t *fixup;
155 union
157 void *pointer; /* Member for doing pointer searches. */
159 struct
161 gfc_symbol *sym;
162 char *true_name, *module, *binding_label;
163 fixup_t *stfixup;
164 gfc_symtree *symtree;
165 enum gfc_rsym_state state;
166 int ns, referenced, renamed;
167 module_locus where;
169 rsym;
171 struct
173 gfc_symbol *sym;
174 enum gfc_wsym_state state;
176 wsym;
181 pointer_info;
183 #define gfc_get_pointer_info() XCNEW (pointer_info)
186 /* Local variables */
188 /* The gzFile for the module we're reading or writing. */
189 static gzFile module_fp;
192 /* The name of the module we're reading (USE'ing) or writing. */
193 static const char *module_name;
194 /* The name of the .smod file that the submodule will write to. */
195 static const char *submodule_name;
196 static gfc_use_list *module_list;
198 /* If we're reading an intrinsic module, this is its ID. */
199 static intmod_id current_intmod;
201 /* Content of module. */
202 static char* module_content;
204 static long module_pos;
205 static int module_line, module_column, only_flag;
206 static int prev_module_line, prev_module_column;
208 static enum
209 { IO_INPUT, IO_OUTPUT }
210 iomode;
212 static gfc_use_rename *gfc_rename_list;
213 static pointer_info *pi_root;
214 static int symbol_number; /* Counter for assigning symbol numbers */
216 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
217 static bool in_load_equiv;
221 /*****************************************************************/
223 /* Pointer/integer conversion. Pointers between structures are stored
224 as integers in the module file. The next couple of subroutines
225 handle this translation for reading and writing. */
227 /* Recursively free the tree of pointer structures. */
229 static void
230 free_pi_tree (pointer_info *p)
232 if (p == NULL)
233 return;
235 if (p->fixup != NULL)
236 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
238 free_pi_tree (p->left);
239 free_pi_tree (p->right);
241 if (iomode == IO_INPUT)
243 XDELETEVEC (p->u.rsym.true_name);
244 XDELETEVEC (p->u.rsym.module);
245 XDELETEVEC (p->u.rsym.binding_label);
248 free (p);
252 /* Compare pointers when searching by pointer. Used when writing a
253 module. */
255 static int
256 compare_pointers (void *_sn1, void *_sn2)
258 pointer_info *sn1, *sn2;
260 sn1 = (pointer_info *) _sn1;
261 sn2 = (pointer_info *) _sn2;
263 if (sn1->u.pointer < sn2->u.pointer)
264 return -1;
265 if (sn1->u.pointer > sn2->u.pointer)
266 return 1;
268 return 0;
272 /* Compare integers when searching by integer. Used when reading a
273 module. */
275 static int
276 compare_integers (void *_sn1, void *_sn2)
278 pointer_info *sn1, *sn2;
280 sn1 = (pointer_info *) _sn1;
281 sn2 = (pointer_info *) _sn2;
283 if (sn1->integer < sn2->integer)
284 return -1;
285 if (sn1->integer > sn2->integer)
286 return 1;
288 return 0;
292 /* Initialize the pointer_info tree. */
294 static void
295 init_pi_tree (void)
297 compare_fn compare;
298 pointer_info *p;
300 pi_root = NULL;
301 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
303 /* Pointer 0 is the NULL pointer. */
304 p = gfc_get_pointer_info ();
305 p->u.pointer = NULL;
306 p->integer = 0;
307 p->type = P_OTHER;
309 gfc_insert_bbt (&pi_root, p, compare);
311 /* Pointer 1 is the current namespace. */
312 p = gfc_get_pointer_info ();
313 p->u.pointer = gfc_current_ns;
314 p->integer = 1;
315 p->type = P_NAMESPACE;
317 gfc_insert_bbt (&pi_root, p, compare);
319 symbol_number = 2;
323 /* During module writing, call here with a pointer to something,
324 returning the pointer_info node. */
326 static pointer_info *
327 find_pointer (void *gp)
329 pointer_info *p;
331 p = pi_root;
332 while (p != NULL)
334 if (p->u.pointer == gp)
335 break;
336 p = (gp < p->u.pointer) ? p->left : p->right;
339 return p;
343 /* Given a pointer while writing, returns the pointer_info tree node,
344 creating it if it doesn't exist. */
346 static pointer_info *
347 get_pointer (void *gp)
349 pointer_info *p;
351 p = find_pointer (gp);
352 if (p != NULL)
353 return p;
355 /* Pointer doesn't have an integer. Give it one. */
356 p = gfc_get_pointer_info ();
358 p->u.pointer = gp;
359 p->integer = symbol_number++;
361 gfc_insert_bbt (&pi_root, p, compare_pointers);
363 return p;
367 /* Given an integer during reading, find it in the pointer_info tree,
368 creating the node if not found. */
370 static pointer_info *
371 get_integer (int integer)
373 pointer_info *p, t;
374 int c;
376 t.integer = integer;
378 p = pi_root;
379 while (p != NULL)
381 c = compare_integers (&t, p);
382 if (c == 0)
383 break;
385 p = (c < 0) ? p->left : p->right;
388 if (p != NULL)
389 return p;
391 p = gfc_get_pointer_info ();
392 p->integer = integer;
393 p->u.pointer = NULL;
395 gfc_insert_bbt (&pi_root, p, compare_integers);
397 return p;
401 /* Resolve any fixups using a known pointer. */
403 static void
404 resolve_fixups (fixup_t *f, void *gp)
406 fixup_t *next;
408 for (; f; f = next)
410 next = f->next;
411 *(f->pointer) = gp;
412 free (f);
417 /* Convert a string such that it starts with a lower-case character. Used
418 to convert the symtree name of a derived-type to the symbol name or to
419 the name of the associated generic function. */
421 static const char *
422 dt_lower_string (const char *name)
424 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
425 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
426 &name[1]);
427 return gfc_get_string (name);
431 /* Convert a string such that it starts with an upper-case character. Used to
432 return the symtree-name for a derived type; the symbol name itself and the
433 symtree/symbol name of the associated generic function start with a lower-
434 case character. */
436 static const char *
437 dt_upper_string (const char *name)
439 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
440 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
441 &name[1]);
442 return gfc_get_string (name);
445 /* Call here during module reading when we know what pointer to
446 associate with an integer. Any fixups that exist are resolved at
447 this time. */
449 static void
450 associate_integer_pointer (pointer_info *p, void *gp)
452 if (p->u.pointer != NULL)
453 gfc_internal_error ("associate_integer_pointer(): Already associated");
455 p->u.pointer = gp;
457 resolve_fixups (p->fixup, gp);
459 p->fixup = NULL;
463 /* During module reading, given an integer and a pointer to a pointer,
464 either store the pointer from an already-known value or create a
465 fixup structure in order to store things later. Returns zero if
466 the reference has been actually stored, or nonzero if the reference
467 must be fixed later (i.e., associate_integer_pointer must be called
468 sometime later. Returns the pointer_info structure. */
470 static pointer_info *
471 add_fixup (int integer, void *gp)
473 pointer_info *p;
474 fixup_t *f;
475 char **cp;
477 p = get_integer (integer);
479 if (p->integer == 0 || p->u.pointer != NULL)
481 cp = (char **) gp;
482 *cp = (char *) p->u.pointer;
484 else
486 f = XCNEW (fixup_t);
488 f->next = p->fixup;
489 p->fixup = f;
491 f->pointer = (void **) gp;
494 return p;
498 /*****************************************************************/
500 /* Parser related subroutines */
502 /* Free the rename list left behind by a USE statement. */
504 static void
505 free_rename (gfc_use_rename *list)
507 gfc_use_rename *next;
509 for (; list; list = next)
511 next = list->next;
512 free (list);
517 /* Match a USE statement. */
519 match
520 gfc_match_use (void)
522 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
523 gfc_use_rename *tail = NULL, *new_use;
524 interface_type type, type2;
525 gfc_intrinsic_op op;
526 match m;
527 gfc_use_list *use_list;
529 use_list = gfc_get_use_list ();
531 if (gfc_match (" , ") == MATCH_YES)
533 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
535 if (!gfc_notify_std (GFC_STD_F2003, "module "
536 "nature in USE statement at %C"))
537 goto cleanup;
539 if (strcmp (module_nature, "intrinsic") == 0)
540 use_list->intrinsic = true;
541 else
543 if (strcmp (module_nature, "non_intrinsic") == 0)
544 use_list->non_intrinsic = true;
545 else
547 gfc_error ("Module nature in USE statement at %C shall "
548 "be either INTRINSIC or NON_INTRINSIC");
549 goto cleanup;
553 else
555 /* Help output a better error message than "Unclassifiable
556 statement". */
557 gfc_match (" %n", module_nature);
558 if (strcmp (module_nature, "intrinsic") == 0
559 || strcmp (module_nature, "non_intrinsic") == 0)
560 gfc_error ("\"::\" was expected after module nature at %C "
561 "but was not found");
562 free (use_list);
563 return m;
566 else
568 m = gfc_match (" ::");
569 if (m == MATCH_YES &&
570 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
571 goto cleanup;
573 if (m != MATCH_YES)
575 m = gfc_match ("% ");
576 if (m != MATCH_YES)
578 free (use_list);
579 return m;
584 use_list->where = gfc_current_locus;
586 m = gfc_match_name (name);
587 if (m != MATCH_YES)
589 free (use_list);
590 return m;
593 use_list->module_name = gfc_get_string (name);
595 if (gfc_match_eos () == MATCH_YES)
596 goto done;
598 if (gfc_match_char (',') != MATCH_YES)
599 goto syntax;
601 if (gfc_match (" only :") == MATCH_YES)
602 use_list->only_flag = true;
604 if (gfc_match_eos () == MATCH_YES)
605 goto done;
607 for (;;)
609 /* Get a new rename struct and add it to the rename list. */
610 new_use = gfc_get_use_rename ();
611 new_use->where = gfc_current_locus;
612 new_use->found = 0;
614 if (use_list->rename == NULL)
615 use_list->rename = new_use;
616 else
617 tail->next = new_use;
618 tail = new_use;
620 /* See what kind of interface we're dealing with. Assume it is
621 not an operator. */
622 new_use->op = INTRINSIC_NONE;
623 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
624 goto cleanup;
626 switch (type)
628 case INTERFACE_NAMELESS:
629 gfc_error ("Missing generic specification in USE statement at %C");
630 goto cleanup;
632 case INTERFACE_USER_OP:
633 case INTERFACE_GENERIC:
634 m = gfc_match (" =>");
636 if (type == INTERFACE_USER_OP && m == MATCH_YES
637 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
638 "operators in USE statements at %C")))
639 goto cleanup;
641 if (type == INTERFACE_USER_OP)
642 new_use->op = INTRINSIC_USER;
644 if (use_list->only_flag)
646 if (m != MATCH_YES)
647 strcpy (new_use->use_name, name);
648 else
650 strcpy (new_use->local_name, name);
651 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
652 if (type != type2)
653 goto syntax;
654 if (m == MATCH_NO)
655 goto syntax;
656 if (m == MATCH_ERROR)
657 goto cleanup;
660 else
662 if (m != MATCH_YES)
663 goto syntax;
664 strcpy (new_use->local_name, name);
666 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
667 if (type != type2)
668 goto syntax;
669 if (m == MATCH_NO)
670 goto syntax;
671 if (m == MATCH_ERROR)
672 goto cleanup;
675 if (strcmp (new_use->use_name, use_list->module_name) == 0
676 || strcmp (new_use->local_name, use_list->module_name) == 0)
678 gfc_error ("The name %qs at %C has already been used as "
679 "an external module name.", use_list->module_name);
680 goto cleanup;
682 break;
684 case INTERFACE_INTRINSIC_OP:
685 new_use->op = op;
686 break;
688 default:
689 gcc_unreachable ();
692 if (gfc_match_eos () == MATCH_YES)
693 break;
694 if (gfc_match_char (',') != MATCH_YES)
695 goto syntax;
698 done:
699 if (module_list)
701 gfc_use_list *last = module_list;
702 while (last->next)
703 last = last->next;
704 last->next = use_list;
706 else
707 module_list = use_list;
709 return MATCH_YES;
711 syntax:
712 gfc_syntax_error (ST_USE);
714 cleanup:
715 free_rename (use_list->rename);
716 free (use_list);
717 return MATCH_ERROR;
721 /* Match a SUBMODULE statement.
723 According to F2008:11.2.3.2, "The submodule identifier is the
724 ordered pair whose first element is the ancestor module name and
725 whose second element is the submodule name. 'Submodule_name' is
726 used for the submodule filename and uses '@' as a separator, whilst
727 the name of the symbol for the module uses '.' as a a separator.
728 The reasons for these choices are:
729 (i) To follow another leading brand in the submodule filenames;
730 (ii) Since '.' is not particularly visible in the filenames; and
731 (iii) The linker does not permit '@' in mnemonics. */
733 match
734 gfc_match_submodule (void)
736 match m;
737 char name[GFC_MAX_SYMBOL_LEN + 1];
738 gfc_use_list *use_list;
740 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
741 return MATCH_ERROR;
743 gfc_new_block = NULL;
744 gcc_assert (module_list == NULL);
746 if (gfc_match_char ('(') != MATCH_YES)
747 goto syntax;
749 while (1)
751 m = gfc_match (" %n", name);
752 if (m != MATCH_YES)
753 goto syntax;
755 use_list = gfc_get_use_list ();
756 use_list->where = gfc_current_locus;
758 if (module_list)
760 gfc_use_list *last = module_list;
761 while (last->next)
762 last = last->next;
763 last->next = use_list;
764 use_list->module_name
765 = gfc_get_string ("%s.%s", module_list->module_name, name);
766 use_list->submodule_name
767 = gfc_get_string ("%s@%s", module_list->module_name, name);
769 else
771 module_list = use_list;
772 use_list->module_name = gfc_get_string (name);
773 use_list->submodule_name = use_list->module_name;
776 if (gfc_match_char (')') == MATCH_YES)
777 break;
779 if (gfc_match_char (':') != MATCH_YES)
780 goto syntax;
783 m = gfc_match (" %s%t", &gfc_new_block);
784 if (m != MATCH_YES)
785 goto syntax;
787 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
788 gfc_new_block->name);
790 gfc_new_block->name = gfc_get_string ("%s.%s",
791 module_list->module_name,
792 gfc_new_block->name);
794 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
795 gfc_new_block->name, NULL))
796 return MATCH_ERROR;
798 /* Just retain the ultimate .(s)mod file for reading, since it
799 contains all the information in its ancestors. */
800 use_list = module_list;
801 for (; module_list->next; use_list = use_list->next)
803 module_list = use_list->next;
804 free (use_list);
807 return MATCH_YES;
809 syntax:
810 gfc_error ("Syntax error in SUBMODULE statement at %C");
811 return MATCH_ERROR;
815 /* Given a name and a number, inst, return the inst name
816 under which to load this symbol. Returns NULL if this
817 symbol shouldn't be loaded. If inst is zero, returns
818 the number of instances of this name. If interface is
819 true, a user-defined operator is sought, otherwise only
820 non-operators are sought. */
822 static const char *
823 find_use_name_n (const char *name, int *inst, bool interface)
825 gfc_use_rename *u;
826 const char *low_name = NULL;
827 int i;
829 /* For derived types. */
830 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
831 low_name = dt_lower_string (name);
833 i = 0;
834 for (u = gfc_rename_list; u; u = u->next)
836 if ((!low_name && strcmp (u->use_name, name) != 0)
837 || (low_name && strcmp (u->use_name, low_name) != 0)
838 || (u->op == INTRINSIC_USER && !interface)
839 || (u->op != INTRINSIC_USER && interface))
840 continue;
841 if (++i == *inst)
842 break;
845 if (!*inst)
847 *inst = i;
848 return NULL;
851 if (u == NULL)
852 return only_flag ? NULL : name;
854 u->found = 1;
856 if (low_name)
858 if (u->local_name[0] == '\0')
859 return name;
860 return dt_upper_string (u->local_name);
863 return (u->local_name[0] != '\0') ? u->local_name : name;
867 /* Given a name, return the name under which to load this symbol.
868 Returns NULL if this symbol shouldn't be loaded. */
870 static const char *
871 find_use_name (const char *name, bool interface)
873 int i = 1;
874 return find_use_name_n (name, &i, interface);
878 /* Given a real name, return the number of use names associated with it. */
880 static int
881 number_use_names (const char *name, bool interface)
883 int i = 0;
884 find_use_name_n (name, &i, interface);
885 return i;
889 /* Try to find the operator in the current list. */
891 static gfc_use_rename *
892 find_use_operator (gfc_intrinsic_op op)
894 gfc_use_rename *u;
896 for (u = gfc_rename_list; u; u = u->next)
897 if (u->op == op)
898 return u;
900 return NULL;
904 /*****************************************************************/
906 /* The next couple of subroutines maintain a tree used to avoid a
907 brute-force search for a combination of true name and module name.
908 While symtree names, the name that a particular symbol is known by
909 can changed with USE statements, we still have to keep track of the
910 true names to generate the correct reference, and also avoid
911 loading the same real symbol twice in a program unit.
913 When we start reading, the true name tree is built and maintained
914 as symbols are read. The tree is searched as we load new symbols
915 to see if it already exists someplace in the namespace. */
917 typedef struct true_name
919 BBT_HEADER (true_name);
920 const char *name;
921 gfc_symbol *sym;
923 true_name;
925 static true_name *true_name_root;
928 /* Compare two true_name structures. */
930 static int
931 compare_true_names (void *_t1, void *_t2)
933 true_name *t1, *t2;
934 int c;
936 t1 = (true_name *) _t1;
937 t2 = (true_name *) _t2;
939 c = ((t1->sym->module > t2->sym->module)
940 - (t1->sym->module < t2->sym->module));
941 if (c != 0)
942 return c;
944 return strcmp (t1->name, t2->name);
948 /* Given a true name, search the true name tree to see if it exists
949 within the main namespace. */
951 static gfc_symbol *
952 find_true_name (const char *name, const char *module)
954 true_name t, *p;
955 gfc_symbol sym;
956 int c;
958 t.name = gfc_get_string (name);
959 if (module != NULL)
960 sym.module = gfc_get_string (module);
961 else
962 sym.module = NULL;
963 t.sym = &sym;
965 p = true_name_root;
966 while (p != NULL)
968 c = compare_true_names ((void *) (&t), (void *) p);
969 if (c == 0)
970 return p->sym;
972 p = (c < 0) ? p->left : p->right;
975 return NULL;
979 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
981 static void
982 add_true_name (gfc_symbol *sym)
984 true_name *t;
986 t = XCNEW (true_name);
987 t->sym = sym;
988 if (sym->attr.flavor == FL_DERIVED)
989 t->name = dt_upper_string (sym->name);
990 else
991 t->name = sym->name;
993 gfc_insert_bbt (&true_name_root, t, compare_true_names);
997 /* Recursive function to build the initial true name tree by
998 recursively traversing the current namespace. */
1000 static void
1001 build_tnt (gfc_symtree *st)
1003 const char *name;
1004 if (st == NULL)
1005 return;
1007 build_tnt (st->left);
1008 build_tnt (st->right);
1010 if (st->n.sym->attr.flavor == FL_DERIVED)
1011 name = dt_upper_string (st->n.sym->name);
1012 else
1013 name = st->n.sym->name;
1015 if (find_true_name (name, st->n.sym->module) != NULL)
1016 return;
1018 add_true_name (st->n.sym);
1022 /* Initialize the true name tree with the current namespace. */
1024 static void
1025 init_true_name_tree (void)
1027 true_name_root = NULL;
1028 build_tnt (gfc_current_ns->sym_root);
1032 /* Recursively free a true name tree node. */
1034 static void
1035 free_true_name (true_name *t)
1037 if (t == NULL)
1038 return;
1039 free_true_name (t->left);
1040 free_true_name (t->right);
1042 free (t);
1046 /*****************************************************************/
1048 /* Module reading and writing. */
1050 /* The following are versions similar to the ones in scanner.c, but
1051 for dealing with compressed module files. */
1053 static gzFile
1054 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1055 bool module, bool system)
1057 char *fullname;
1058 gfc_directorylist *p;
1059 gzFile f;
1061 for (p = list; p; p = p->next)
1063 if (module && !p->use_for_modules)
1064 continue;
1066 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
1067 strcpy (fullname, p->path);
1068 strcat (fullname, name);
1070 f = gzopen (fullname, "r");
1071 if (f != NULL)
1073 if (gfc_cpp_makedep ())
1074 gfc_cpp_add_dep (fullname, system);
1076 return f;
1080 return NULL;
1083 static gzFile
1084 gzopen_included_file (const char *name, bool include_cwd, bool module)
1086 gzFile f = NULL;
1088 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1090 f = gzopen (name, "r");
1091 if (f && gfc_cpp_makedep ())
1092 gfc_cpp_add_dep (name, false);
1095 if (!f)
1096 f = gzopen_included_file_1 (name, include_dirs, module, false);
1098 return f;
1101 static gzFile
1102 gzopen_intrinsic_module (const char* name)
1104 gzFile f = NULL;
1106 if (IS_ABSOLUTE_PATH (name))
1108 f = gzopen (name, "r");
1109 if (f && gfc_cpp_makedep ())
1110 gfc_cpp_add_dep (name, true);
1113 if (!f)
1114 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1116 return f;
1120 enum atom_type
1122 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1125 static atom_type last_atom;
1128 /* The name buffer must be at least as long as a symbol name. Right
1129 now it's not clear how we're going to store numeric constants--
1130 probably as a hexadecimal string, since this will allow the exact
1131 number to be preserved (this can't be done by a decimal
1132 representation). Worry about that later. TODO! */
1134 #define MAX_ATOM_SIZE 100
1136 static int atom_int;
1137 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1140 /* Report problems with a module. Error reporting is not very
1141 elaborate, since this sorts of errors shouldn't really happen.
1142 This subroutine never returns. */
1144 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1146 static void
1147 bad_module (const char *msgid)
1149 XDELETEVEC (module_content);
1150 module_content = NULL;
1152 switch (iomode)
1154 case IO_INPUT:
1155 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1156 module_name, module_line, module_column, msgid);
1157 break;
1158 case IO_OUTPUT:
1159 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1160 module_name, module_line, module_column, msgid);
1161 break;
1162 default:
1163 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1164 module_name, module_line, module_column, msgid);
1165 break;
1170 /* Set the module's input pointer. */
1172 static void
1173 set_module_locus (module_locus *m)
1175 module_column = m->column;
1176 module_line = m->line;
1177 module_pos = m->pos;
1181 /* Get the module's input pointer so that we can restore it later. */
1183 static void
1184 get_module_locus (module_locus *m)
1186 m->column = module_column;
1187 m->line = module_line;
1188 m->pos = module_pos;
1192 /* Get the next character in the module, updating our reckoning of
1193 where we are. */
1195 static int
1196 module_char (void)
1198 const char c = module_content[module_pos++];
1199 if (c == '\0')
1200 bad_module ("Unexpected EOF");
1202 prev_module_line = module_line;
1203 prev_module_column = module_column;
1205 if (c == '\n')
1207 module_line++;
1208 module_column = 0;
1211 module_column++;
1212 return c;
1215 /* Unget a character while remembering the line and column. Works for
1216 a single character only. */
1218 static void
1219 module_unget_char (void)
1221 module_line = prev_module_line;
1222 module_column = prev_module_column;
1223 module_pos--;
1226 /* Parse a string constant. The delimiter is guaranteed to be a
1227 single quote. */
1229 static void
1230 parse_string (void)
1232 int c;
1233 size_t cursz = 30;
1234 size_t len = 0;
1236 atom_string = XNEWVEC (char, cursz);
1238 for ( ; ; )
1240 c = module_char ();
1242 if (c == '\'')
1244 int c2 = module_char ();
1245 if (c2 != '\'')
1247 module_unget_char ();
1248 break;
1252 if (len >= cursz)
1254 cursz *= 2;
1255 atom_string = XRESIZEVEC (char, atom_string, cursz);
1257 atom_string[len] = c;
1258 len++;
1261 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1262 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1266 /* Parse a small integer. */
1268 static void
1269 parse_integer (int c)
1271 atom_int = c - '0';
1273 for (;;)
1275 c = module_char ();
1276 if (!ISDIGIT (c))
1278 module_unget_char ();
1279 break;
1282 atom_int = 10 * atom_int + c - '0';
1283 if (atom_int > 99999999)
1284 bad_module ("Integer overflow");
1290 /* Parse a name. */
1292 static void
1293 parse_name (int c)
1295 char *p;
1296 int len;
1298 p = atom_name;
1300 *p++ = c;
1301 len = 1;
1303 for (;;)
1305 c = module_char ();
1306 if (!ISALNUM (c) && c != '_' && c != '-')
1308 module_unget_char ();
1309 break;
1312 *p++ = c;
1313 if (++len > GFC_MAX_SYMBOL_LEN)
1314 bad_module ("Name too long");
1317 *p = '\0';
1322 /* Read the next atom in the module's input stream. */
1324 static atom_type
1325 parse_atom (void)
1327 int c;
1331 c = module_char ();
1333 while (c == ' ' || c == '\r' || c == '\n');
1335 switch (c)
1337 case '(':
1338 return ATOM_LPAREN;
1340 case ')':
1341 return ATOM_RPAREN;
1343 case '\'':
1344 parse_string ();
1345 return ATOM_STRING;
1347 case '0':
1348 case '1':
1349 case '2':
1350 case '3':
1351 case '4':
1352 case '5':
1353 case '6':
1354 case '7':
1355 case '8':
1356 case '9':
1357 parse_integer (c);
1358 return ATOM_INTEGER;
1360 case 'a':
1361 case 'b':
1362 case 'c':
1363 case 'd':
1364 case 'e':
1365 case 'f':
1366 case 'g':
1367 case 'h':
1368 case 'i':
1369 case 'j':
1370 case 'k':
1371 case 'l':
1372 case 'm':
1373 case 'n':
1374 case 'o':
1375 case 'p':
1376 case 'q':
1377 case 'r':
1378 case 's':
1379 case 't':
1380 case 'u':
1381 case 'v':
1382 case 'w':
1383 case 'x':
1384 case 'y':
1385 case 'z':
1386 case 'A':
1387 case 'B':
1388 case 'C':
1389 case 'D':
1390 case 'E':
1391 case 'F':
1392 case 'G':
1393 case 'H':
1394 case 'I':
1395 case 'J':
1396 case 'K':
1397 case 'L':
1398 case 'M':
1399 case 'N':
1400 case 'O':
1401 case 'P':
1402 case 'Q':
1403 case 'R':
1404 case 'S':
1405 case 'T':
1406 case 'U':
1407 case 'V':
1408 case 'W':
1409 case 'X':
1410 case 'Y':
1411 case 'Z':
1412 parse_name (c);
1413 return ATOM_NAME;
1415 default:
1416 bad_module ("Bad name");
1419 /* Not reached. */
1423 /* Peek at the next atom on the input. */
1425 static atom_type
1426 peek_atom (void)
1428 int c;
1432 c = module_char ();
1434 while (c == ' ' || c == '\r' || c == '\n');
1436 switch (c)
1438 case '(':
1439 module_unget_char ();
1440 return ATOM_LPAREN;
1442 case ')':
1443 module_unget_char ();
1444 return ATOM_RPAREN;
1446 case '\'':
1447 module_unget_char ();
1448 return ATOM_STRING;
1450 case '0':
1451 case '1':
1452 case '2':
1453 case '3':
1454 case '4':
1455 case '5':
1456 case '6':
1457 case '7':
1458 case '8':
1459 case '9':
1460 module_unget_char ();
1461 return ATOM_INTEGER;
1463 case 'a':
1464 case 'b':
1465 case 'c':
1466 case 'd':
1467 case 'e':
1468 case 'f':
1469 case 'g':
1470 case 'h':
1471 case 'i':
1472 case 'j':
1473 case 'k':
1474 case 'l':
1475 case 'm':
1476 case 'n':
1477 case 'o':
1478 case 'p':
1479 case 'q':
1480 case 'r':
1481 case 's':
1482 case 't':
1483 case 'u':
1484 case 'v':
1485 case 'w':
1486 case 'x':
1487 case 'y':
1488 case 'z':
1489 case 'A':
1490 case 'B':
1491 case 'C':
1492 case 'D':
1493 case 'E':
1494 case 'F':
1495 case 'G':
1496 case 'H':
1497 case 'I':
1498 case 'J':
1499 case 'K':
1500 case 'L':
1501 case 'M':
1502 case 'N':
1503 case 'O':
1504 case 'P':
1505 case 'Q':
1506 case 'R':
1507 case 'S':
1508 case 'T':
1509 case 'U':
1510 case 'V':
1511 case 'W':
1512 case 'X':
1513 case 'Y':
1514 case 'Z':
1515 module_unget_char ();
1516 return ATOM_NAME;
1518 default:
1519 bad_module ("Bad name");
1524 /* Read the next atom from the input, requiring that it be a
1525 particular kind. */
1527 static void
1528 require_atom (atom_type type)
1530 atom_type t;
1531 const char *p;
1532 int column, line;
1534 column = module_column;
1535 line = module_line;
1537 t = parse_atom ();
1538 if (t != type)
1540 switch (type)
1542 case ATOM_NAME:
1543 p = _("Expected name");
1544 break;
1545 case ATOM_LPAREN:
1546 p = _("Expected left parenthesis");
1547 break;
1548 case ATOM_RPAREN:
1549 p = _("Expected right parenthesis");
1550 break;
1551 case ATOM_INTEGER:
1552 p = _("Expected integer");
1553 break;
1554 case ATOM_STRING:
1555 p = _("Expected string");
1556 break;
1557 default:
1558 gfc_internal_error ("require_atom(): bad atom type required");
1561 module_column = column;
1562 module_line = line;
1563 bad_module (p);
1568 /* Given a pointer to an mstring array, require that the current input
1569 be one of the strings in the array. We return the enum value. */
1571 static int
1572 find_enum (const mstring *m)
1574 int i;
1576 i = gfc_string2code (m, atom_name);
1577 if (i >= 0)
1578 return i;
1580 bad_module ("find_enum(): Enum not found");
1582 /* Not reached. */
1586 /* Read a string. The caller is responsible for freeing. */
1588 static char*
1589 read_string (void)
1591 char* p;
1592 require_atom (ATOM_STRING);
1593 p = atom_string;
1594 atom_string = NULL;
1595 return p;
1599 /**************** Module output subroutines ***************************/
1601 /* Output a character to a module file. */
1603 static void
1604 write_char (char out)
1606 if (gzputc (module_fp, out) == EOF)
1607 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1609 if (out != '\n')
1610 module_column++;
1611 else
1613 module_column = 1;
1614 module_line++;
1619 /* Write an atom to a module. The line wrapping isn't perfect, but it
1620 should work most of the time. This isn't that big of a deal, since
1621 the file really isn't meant to be read by people anyway. */
1623 static void
1624 write_atom (atom_type atom, const void *v)
1626 char buffer[20];
1628 /* Workaround -Wmaybe-uninitialized false positive during
1629 profiledbootstrap by initializing them. */
1630 int i = 0, len;
1631 const char *p;
1633 switch (atom)
1635 case ATOM_STRING:
1636 case ATOM_NAME:
1637 p = (const char *) v;
1638 break;
1640 case ATOM_LPAREN:
1641 p = "(";
1642 break;
1644 case ATOM_RPAREN:
1645 p = ")";
1646 break;
1648 case ATOM_INTEGER:
1649 i = *((const int *) v);
1650 if (i < 0)
1651 gfc_internal_error ("write_atom(): Writing negative integer");
1653 sprintf (buffer, "%d", i);
1654 p = buffer;
1655 break;
1657 default:
1658 gfc_internal_error ("write_atom(): Trying to write dab atom");
1662 if(p == NULL || *p == '\0')
1663 len = 0;
1664 else
1665 len = strlen (p);
1667 if (atom != ATOM_RPAREN)
1669 if (module_column + len > 72)
1670 write_char ('\n');
1671 else
1674 if (last_atom != ATOM_LPAREN && module_column != 1)
1675 write_char (' ');
1679 if (atom == ATOM_STRING)
1680 write_char ('\'');
1682 while (p != NULL && *p)
1684 if (atom == ATOM_STRING && *p == '\'')
1685 write_char ('\'');
1686 write_char (*p++);
1689 if (atom == ATOM_STRING)
1690 write_char ('\'');
1692 last_atom = atom;
1697 /***************** Mid-level I/O subroutines *****************/
1699 /* These subroutines let their caller read or write atoms without
1700 caring about which of the two is actually happening. This lets a
1701 subroutine concentrate on the actual format of the data being
1702 written. */
1704 static void mio_expr (gfc_expr **);
1705 pointer_info *mio_symbol_ref (gfc_symbol **);
1706 pointer_info *mio_interface_rest (gfc_interface **);
1707 static void mio_symtree_ref (gfc_symtree **);
1709 /* Read or write an enumerated value. On writing, we return the input
1710 value for the convenience of callers. We avoid using an integer
1711 pointer because enums are sometimes inside bitfields. */
1713 static int
1714 mio_name (int t, const mstring *m)
1716 if (iomode == IO_OUTPUT)
1717 write_atom (ATOM_NAME, gfc_code2string (m, t));
1718 else
1720 require_atom (ATOM_NAME);
1721 t = find_enum (m);
1724 return t;
1727 /* Specialization of mio_name. */
1729 #define DECL_MIO_NAME(TYPE) \
1730 static inline TYPE \
1731 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1733 return (TYPE) mio_name ((int) t, m); \
1735 #define MIO_NAME(TYPE) mio_name_##TYPE
1737 static void
1738 mio_lparen (void)
1740 if (iomode == IO_OUTPUT)
1741 write_atom (ATOM_LPAREN, NULL);
1742 else
1743 require_atom (ATOM_LPAREN);
1747 static void
1748 mio_rparen (void)
1750 if (iomode == IO_OUTPUT)
1751 write_atom (ATOM_RPAREN, NULL);
1752 else
1753 require_atom (ATOM_RPAREN);
1757 static void
1758 mio_integer (int *ip)
1760 if (iomode == IO_OUTPUT)
1761 write_atom (ATOM_INTEGER, ip);
1762 else
1764 require_atom (ATOM_INTEGER);
1765 *ip = atom_int;
1770 /* Read or write a gfc_intrinsic_op value. */
1772 static void
1773 mio_intrinsic_op (gfc_intrinsic_op* op)
1775 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1776 if (iomode == IO_OUTPUT)
1778 int converted = (int) *op;
1779 write_atom (ATOM_INTEGER, &converted);
1781 else
1783 require_atom (ATOM_INTEGER);
1784 *op = (gfc_intrinsic_op) atom_int;
1789 /* Read or write a character pointer that points to a string on the heap. */
1791 static const char *
1792 mio_allocated_string (const char *s)
1794 if (iomode == IO_OUTPUT)
1796 write_atom (ATOM_STRING, s);
1797 return s;
1799 else
1801 require_atom (ATOM_STRING);
1802 return atom_string;
1807 /* Functions for quoting and unquoting strings. */
1809 static char *
1810 quote_string (const gfc_char_t *s, const size_t slength)
1812 const gfc_char_t *p;
1813 char *res, *q;
1814 size_t len = 0, i;
1816 /* Calculate the length we'll need: a backslash takes two ("\\"),
1817 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1818 for (p = s, i = 0; i < slength; p++, i++)
1820 if (*p == '\\')
1821 len += 2;
1822 else if (!gfc_wide_is_printable (*p))
1823 len += 10;
1824 else
1825 len++;
1828 q = res = XCNEWVEC (char, len + 1);
1829 for (p = s, i = 0; i < slength; p++, i++)
1831 if (*p == '\\')
1832 *q++ = '\\', *q++ = '\\';
1833 else if (!gfc_wide_is_printable (*p))
1835 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1836 (unsigned HOST_WIDE_INT) *p);
1837 q += 10;
1839 else
1840 *q++ = (unsigned char) *p;
1843 res[len] = '\0';
1844 return res;
1847 static gfc_char_t *
1848 unquote_string (const char *s)
1850 size_t len, i;
1851 const char *p;
1852 gfc_char_t *res;
1854 for (p = s, len = 0; *p; p++, len++)
1856 if (*p != '\\')
1857 continue;
1859 if (p[1] == '\\')
1860 p++;
1861 else if (p[1] == 'U')
1862 p += 9; /* That is a "\U????????". */
1863 else
1864 gfc_internal_error ("unquote_string(): got bad string");
1867 res = gfc_get_wide_string (len + 1);
1868 for (i = 0, p = s; i < len; i++, p++)
1870 gcc_assert (*p);
1872 if (*p != '\\')
1873 res[i] = (unsigned char) *p;
1874 else if (p[1] == '\\')
1876 res[i] = (unsigned char) '\\';
1877 p++;
1879 else
1881 /* We read the 8-digits hexadecimal constant that follows. */
1882 int j;
1883 unsigned n;
1884 gfc_char_t c = 0;
1886 gcc_assert (p[1] == 'U');
1887 for (j = 0; j < 8; j++)
1889 c = c << 4;
1890 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1891 c += n;
1894 res[i] = c;
1895 p += 9;
1899 res[len] = '\0';
1900 return res;
1904 /* Read or write a character pointer that points to a wide string on the
1905 heap, performing quoting/unquoting of nonprintable characters using the
1906 form \U???????? (where each ? is a hexadecimal digit).
1907 Length is the length of the string, only known and used in output mode. */
1909 static const gfc_char_t *
1910 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1912 if (iomode == IO_OUTPUT)
1914 char *quoted = quote_string (s, length);
1915 write_atom (ATOM_STRING, quoted);
1916 free (quoted);
1917 return s;
1919 else
1921 gfc_char_t *unquoted;
1923 require_atom (ATOM_STRING);
1924 unquoted = unquote_string (atom_string);
1925 free (atom_string);
1926 return unquoted;
1931 /* Read or write a string that is in static memory. */
1933 static void
1934 mio_pool_string (const char **stringp)
1936 /* TODO: one could write the string only once, and refer to it via a
1937 fixup pointer. */
1939 /* As a special case we have to deal with a NULL string. This
1940 happens for the 'module' member of 'gfc_symbol's that are not in a
1941 module. We read / write these as the empty string. */
1942 if (iomode == IO_OUTPUT)
1944 const char *p = *stringp == NULL ? "" : *stringp;
1945 write_atom (ATOM_STRING, p);
1947 else
1949 require_atom (ATOM_STRING);
1950 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1951 free (atom_string);
1956 /* Read or write a string that is inside of some already-allocated
1957 structure. */
1959 static void
1960 mio_internal_string (char *string)
1962 if (iomode == IO_OUTPUT)
1963 write_atom (ATOM_STRING, string);
1964 else
1966 require_atom (ATOM_STRING);
1967 strcpy (string, atom_string);
1968 free (atom_string);
1973 enum ab_attribute
1974 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1975 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1976 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1977 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1978 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1979 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1980 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1981 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1982 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1983 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1984 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
1985 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE
1988 static const mstring attr_bits[] =
1990 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1991 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1992 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1993 minit ("DIMENSION", AB_DIMENSION),
1994 minit ("CODIMENSION", AB_CODIMENSION),
1995 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1996 minit ("EXTERNAL", AB_EXTERNAL),
1997 minit ("INTRINSIC", AB_INTRINSIC),
1998 minit ("OPTIONAL", AB_OPTIONAL),
1999 minit ("POINTER", AB_POINTER),
2000 minit ("VOLATILE", AB_VOLATILE),
2001 minit ("TARGET", AB_TARGET),
2002 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2003 minit ("DUMMY", AB_DUMMY),
2004 minit ("RESULT", AB_RESULT),
2005 minit ("DATA", AB_DATA),
2006 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2007 minit ("IN_COMMON", AB_IN_COMMON),
2008 minit ("FUNCTION", AB_FUNCTION),
2009 minit ("SUBROUTINE", AB_SUBROUTINE),
2010 minit ("SEQUENCE", AB_SEQUENCE),
2011 minit ("ELEMENTAL", AB_ELEMENTAL),
2012 minit ("PURE", AB_PURE),
2013 minit ("RECURSIVE", AB_RECURSIVE),
2014 minit ("GENERIC", AB_GENERIC),
2015 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2016 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2017 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2018 minit ("IS_BIND_C", AB_IS_BIND_C),
2019 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2020 minit ("IS_ISO_C", AB_IS_ISO_C),
2021 minit ("VALUE", AB_VALUE),
2022 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2023 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2024 minit ("LOCK_COMP", AB_LOCK_COMP),
2025 minit ("POINTER_COMP", AB_POINTER_COMP),
2026 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2027 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2028 minit ("ZERO_COMP", AB_ZERO_COMP),
2029 minit ("PROTECTED", AB_PROTECTED),
2030 minit ("ABSTRACT", AB_ABSTRACT),
2031 minit ("IS_CLASS", AB_IS_CLASS),
2032 minit ("PROCEDURE", AB_PROCEDURE),
2033 minit ("PROC_POINTER", AB_PROC_POINTER),
2034 minit ("VTYPE", AB_VTYPE),
2035 minit ("VTAB", AB_VTAB),
2036 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2037 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2038 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2039 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2040 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2041 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2042 minit (NULL, -1)
2045 /* For binding attributes. */
2046 static const mstring binding_passing[] =
2048 minit ("PASS", 0),
2049 minit ("NOPASS", 1),
2050 minit (NULL, -1)
2052 static const mstring binding_overriding[] =
2054 minit ("OVERRIDABLE", 0),
2055 minit ("NON_OVERRIDABLE", 1),
2056 minit ("DEFERRED", 2),
2057 minit (NULL, -1)
2059 static const mstring binding_generic[] =
2061 minit ("SPECIFIC", 0),
2062 minit ("GENERIC", 1),
2063 minit (NULL, -1)
2065 static const mstring binding_ppc[] =
2067 minit ("NO_PPC", 0),
2068 minit ("PPC", 1),
2069 minit (NULL, -1)
2072 /* Specialization of mio_name. */
2073 DECL_MIO_NAME (ab_attribute)
2074 DECL_MIO_NAME (ar_type)
2075 DECL_MIO_NAME (array_type)
2076 DECL_MIO_NAME (bt)
2077 DECL_MIO_NAME (expr_t)
2078 DECL_MIO_NAME (gfc_access)
2079 DECL_MIO_NAME (gfc_intrinsic_op)
2080 DECL_MIO_NAME (ifsrc)
2081 DECL_MIO_NAME (save_state)
2082 DECL_MIO_NAME (procedure_type)
2083 DECL_MIO_NAME (ref_type)
2084 DECL_MIO_NAME (sym_flavor)
2085 DECL_MIO_NAME (sym_intent)
2086 #undef DECL_MIO_NAME
2088 /* Symbol attributes are stored in list with the first three elements
2089 being the enumerated fields, while the remaining elements (if any)
2090 indicate the individual attribute bits. The access field is not
2091 saved-- it controls what symbols are exported when a module is
2092 written. */
2094 static void
2095 mio_symbol_attribute (symbol_attribute *attr)
2097 atom_type t;
2098 unsigned ext_attr,extension_level;
2100 mio_lparen ();
2102 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2103 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2104 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2105 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2106 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2108 ext_attr = attr->ext_attr;
2109 mio_integer ((int *) &ext_attr);
2110 attr->ext_attr = ext_attr;
2112 extension_level = attr->extension;
2113 mio_integer ((int *) &extension_level);
2114 attr->extension = extension_level;
2116 if (iomode == IO_OUTPUT)
2118 if (attr->allocatable)
2119 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2120 if (attr->artificial)
2121 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2122 if (attr->asynchronous)
2123 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2124 if (attr->dimension)
2125 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2126 if (attr->codimension)
2127 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2128 if (attr->contiguous)
2129 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2130 if (attr->external)
2131 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2132 if (attr->intrinsic)
2133 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2134 if (attr->optional)
2135 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2136 if (attr->pointer)
2137 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2138 if (attr->class_pointer)
2139 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2140 if (attr->is_protected)
2141 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2142 if (attr->value)
2143 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2144 if (attr->volatile_)
2145 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2146 if (attr->target)
2147 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2148 if (attr->threadprivate)
2149 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2150 if (attr->dummy)
2151 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2152 if (attr->result)
2153 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2154 /* We deliberately don't preserve the "entry" flag. */
2156 if (attr->data)
2157 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2158 if (attr->in_namelist)
2159 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2160 if (attr->in_common)
2161 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2163 if (attr->function)
2164 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2165 if (attr->subroutine)
2166 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2167 if (attr->generic)
2168 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2169 if (attr->abstract)
2170 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2172 if (attr->sequence)
2173 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2174 if (attr->elemental)
2175 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2176 if (attr->pure)
2177 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2178 if (attr->implicit_pure)
2179 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2180 if (attr->unlimited_polymorphic)
2181 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2182 if (attr->recursive)
2183 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2184 if (attr->always_explicit)
2185 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2186 if (attr->cray_pointer)
2187 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2188 if (attr->cray_pointee)
2189 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2190 if (attr->is_bind_c)
2191 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2192 if (attr->is_c_interop)
2193 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2194 if (attr->is_iso_c)
2195 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2196 if (attr->alloc_comp)
2197 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2198 if (attr->pointer_comp)
2199 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2200 if (attr->proc_pointer_comp)
2201 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2202 if (attr->private_comp)
2203 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2204 if (attr->coarray_comp)
2205 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2206 if (attr->lock_comp)
2207 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2208 if (attr->zero_comp)
2209 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2210 if (attr->is_class)
2211 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2212 if (attr->procedure)
2213 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2214 if (attr->proc_pointer)
2215 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2216 if (attr->vtype)
2217 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2218 if (attr->vtab)
2219 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2220 if (attr->omp_declare_target)
2221 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2222 if (attr->array_outer_dependency)
2223 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2224 if (attr->module_procedure)
2225 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2227 mio_rparen ();
2230 else
2232 for (;;)
2234 t = parse_atom ();
2235 if (t == ATOM_RPAREN)
2236 break;
2237 if (t != ATOM_NAME)
2238 bad_module ("Expected attribute bit name");
2240 switch ((ab_attribute) find_enum (attr_bits))
2242 case AB_ALLOCATABLE:
2243 attr->allocatable = 1;
2244 break;
2245 case AB_ARTIFICIAL:
2246 attr->artificial = 1;
2247 break;
2248 case AB_ASYNCHRONOUS:
2249 attr->asynchronous = 1;
2250 break;
2251 case AB_DIMENSION:
2252 attr->dimension = 1;
2253 break;
2254 case AB_CODIMENSION:
2255 attr->codimension = 1;
2256 break;
2257 case AB_CONTIGUOUS:
2258 attr->contiguous = 1;
2259 break;
2260 case AB_EXTERNAL:
2261 attr->external = 1;
2262 break;
2263 case AB_INTRINSIC:
2264 attr->intrinsic = 1;
2265 break;
2266 case AB_OPTIONAL:
2267 attr->optional = 1;
2268 break;
2269 case AB_POINTER:
2270 attr->pointer = 1;
2271 break;
2272 case AB_CLASS_POINTER:
2273 attr->class_pointer = 1;
2274 break;
2275 case AB_PROTECTED:
2276 attr->is_protected = 1;
2277 break;
2278 case AB_VALUE:
2279 attr->value = 1;
2280 break;
2281 case AB_VOLATILE:
2282 attr->volatile_ = 1;
2283 break;
2284 case AB_TARGET:
2285 attr->target = 1;
2286 break;
2287 case AB_THREADPRIVATE:
2288 attr->threadprivate = 1;
2289 break;
2290 case AB_DUMMY:
2291 attr->dummy = 1;
2292 break;
2293 case AB_RESULT:
2294 attr->result = 1;
2295 break;
2296 case AB_DATA:
2297 attr->data = 1;
2298 break;
2299 case AB_IN_NAMELIST:
2300 attr->in_namelist = 1;
2301 break;
2302 case AB_IN_COMMON:
2303 attr->in_common = 1;
2304 break;
2305 case AB_FUNCTION:
2306 attr->function = 1;
2307 break;
2308 case AB_SUBROUTINE:
2309 attr->subroutine = 1;
2310 break;
2311 case AB_GENERIC:
2312 attr->generic = 1;
2313 break;
2314 case AB_ABSTRACT:
2315 attr->abstract = 1;
2316 break;
2317 case AB_SEQUENCE:
2318 attr->sequence = 1;
2319 break;
2320 case AB_ELEMENTAL:
2321 attr->elemental = 1;
2322 break;
2323 case AB_PURE:
2324 attr->pure = 1;
2325 break;
2326 case AB_IMPLICIT_PURE:
2327 attr->implicit_pure = 1;
2328 break;
2329 case AB_UNLIMITED_POLY:
2330 attr->unlimited_polymorphic = 1;
2331 break;
2332 case AB_RECURSIVE:
2333 attr->recursive = 1;
2334 break;
2335 case AB_ALWAYS_EXPLICIT:
2336 attr->always_explicit = 1;
2337 break;
2338 case AB_CRAY_POINTER:
2339 attr->cray_pointer = 1;
2340 break;
2341 case AB_CRAY_POINTEE:
2342 attr->cray_pointee = 1;
2343 break;
2344 case AB_IS_BIND_C:
2345 attr->is_bind_c = 1;
2346 break;
2347 case AB_IS_C_INTEROP:
2348 attr->is_c_interop = 1;
2349 break;
2350 case AB_IS_ISO_C:
2351 attr->is_iso_c = 1;
2352 break;
2353 case AB_ALLOC_COMP:
2354 attr->alloc_comp = 1;
2355 break;
2356 case AB_COARRAY_COMP:
2357 attr->coarray_comp = 1;
2358 break;
2359 case AB_LOCK_COMP:
2360 attr->lock_comp = 1;
2361 break;
2362 case AB_POINTER_COMP:
2363 attr->pointer_comp = 1;
2364 break;
2365 case AB_PROC_POINTER_COMP:
2366 attr->proc_pointer_comp = 1;
2367 break;
2368 case AB_PRIVATE_COMP:
2369 attr->private_comp = 1;
2370 break;
2371 case AB_ZERO_COMP:
2372 attr->zero_comp = 1;
2373 break;
2374 case AB_IS_CLASS:
2375 attr->is_class = 1;
2376 break;
2377 case AB_PROCEDURE:
2378 attr->procedure = 1;
2379 break;
2380 case AB_PROC_POINTER:
2381 attr->proc_pointer = 1;
2382 break;
2383 case AB_VTYPE:
2384 attr->vtype = 1;
2385 break;
2386 case AB_VTAB:
2387 attr->vtab = 1;
2388 break;
2389 case AB_OMP_DECLARE_TARGET:
2390 attr->omp_declare_target = 1;
2391 break;
2392 case AB_ARRAY_OUTER_DEPENDENCY:
2393 attr->array_outer_dependency =1;
2394 break;
2395 case AB_MODULE_PROCEDURE:
2396 attr->module_procedure =1;
2397 break;
2404 static const mstring bt_types[] = {
2405 minit ("INTEGER", BT_INTEGER),
2406 minit ("REAL", BT_REAL),
2407 minit ("COMPLEX", BT_COMPLEX),
2408 minit ("LOGICAL", BT_LOGICAL),
2409 minit ("CHARACTER", BT_CHARACTER),
2410 minit ("DERIVED", BT_DERIVED),
2411 minit ("CLASS", BT_CLASS),
2412 minit ("PROCEDURE", BT_PROCEDURE),
2413 minit ("UNKNOWN", BT_UNKNOWN),
2414 minit ("VOID", BT_VOID),
2415 minit ("ASSUMED", BT_ASSUMED),
2416 minit (NULL, -1)
2420 static void
2421 mio_charlen (gfc_charlen **clp)
2423 gfc_charlen *cl;
2425 mio_lparen ();
2427 if (iomode == IO_OUTPUT)
2429 cl = *clp;
2430 if (cl != NULL)
2431 mio_expr (&cl->length);
2433 else
2435 if (peek_atom () != ATOM_RPAREN)
2437 cl = gfc_new_charlen (gfc_current_ns, NULL);
2438 mio_expr (&cl->length);
2439 *clp = cl;
2443 mio_rparen ();
2447 /* See if a name is a generated name. */
2449 static int
2450 check_unique_name (const char *name)
2452 return *name == '@';
2456 static void
2457 mio_typespec (gfc_typespec *ts)
2459 mio_lparen ();
2461 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2463 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2464 mio_integer (&ts->kind);
2465 else
2466 mio_symbol_ref (&ts->u.derived);
2468 mio_symbol_ref (&ts->interface);
2470 /* Add info for C interop and is_iso_c. */
2471 mio_integer (&ts->is_c_interop);
2472 mio_integer (&ts->is_iso_c);
2474 /* If the typespec is for an identifier either from iso_c_binding, or
2475 a constant that was initialized to an identifier from it, use the
2476 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2477 if (ts->is_iso_c)
2478 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2479 else
2480 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2482 if (ts->type != BT_CHARACTER)
2484 /* ts->u.cl is only valid for BT_CHARACTER. */
2485 mio_lparen ();
2486 mio_rparen ();
2488 else
2489 mio_charlen (&ts->u.cl);
2491 /* So as not to disturb the existing API, use an ATOM_NAME to
2492 transmit deferred characteristic for characters (F2003). */
2493 if (iomode == IO_OUTPUT)
2495 if (ts->type == BT_CHARACTER && ts->deferred)
2496 write_atom (ATOM_NAME, "DEFERRED_CL");
2498 else if (peek_atom () != ATOM_RPAREN)
2500 if (parse_atom () != ATOM_NAME)
2501 bad_module ("Expected string");
2502 ts->deferred = 1;
2505 mio_rparen ();
2509 static const mstring array_spec_types[] = {
2510 minit ("EXPLICIT", AS_EXPLICIT),
2511 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2512 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2513 minit ("DEFERRED", AS_DEFERRED),
2514 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2515 minit (NULL, -1)
2519 static void
2520 mio_array_spec (gfc_array_spec **asp)
2522 gfc_array_spec *as;
2523 int i;
2525 mio_lparen ();
2527 if (iomode == IO_OUTPUT)
2529 int rank;
2531 if (*asp == NULL)
2532 goto done;
2533 as = *asp;
2535 /* mio_integer expects nonnegative values. */
2536 rank = as->rank > 0 ? as->rank : 0;
2537 mio_integer (&rank);
2539 else
2541 if (peek_atom () == ATOM_RPAREN)
2543 *asp = NULL;
2544 goto done;
2547 *asp = as = gfc_get_array_spec ();
2548 mio_integer (&as->rank);
2551 mio_integer (&as->corank);
2552 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2554 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2555 as->rank = -1;
2556 if (iomode == IO_INPUT && as->corank)
2557 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2559 if (as->rank + as->corank > 0)
2560 for (i = 0; i < as->rank + as->corank; i++)
2562 mio_expr (&as->lower[i]);
2563 mio_expr (&as->upper[i]);
2566 done:
2567 mio_rparen ();
2571 /* Given a pointer to an array reference structure (which lives in a
2572 gfc_ref structure), find the corresponding array specification
2573 structure. Storing the pointer in the ref structure doesn't quite
2574 work when loading from a module. Generating code for an array
2575 reference also needs more information than just the array spec. */
2577 static const mstring array_ref_types[] = {
2578 minit ("FULL", AR_FULL),
2579 minit ("ELEMENT", AR_ELEMENT),
2580 minit ("SECTION", AR_SECTION),
2581 minit (NULL, -1)
2585 static void
2586 mio_array_ref (gfc_array_ref *ar)
2588 int i;
2590 mio_lparen ();
2591 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2592 mio_integer (&ar->dimen);
2594 switch (ar->type)
2596 case AR_FULL:
2597 break;
2599 case AR_ELEMENT:
2600 for (i = 0; i < ar->dimen; i++)
2601 mio_expr (&ar->start[i]);
2603 break;
2605 case AR_SECTION:
2606 for (i = 0; i < ar->dimen; i++)
2608 mio_expr (&ar->start[i]);
2609 mio_expr (&ar->end[i]);
2610 mio_expr (&ar->stride[i]);
2613 break;
2615 case AR_UNKNOWN:
2616 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2619 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2620 we can't call mio_integer directly. Instead loop over each element
2621 and cast it to/from an integer. */
2622 if (iomode == IO_OUTPUT)
2624 for (i = 0; i < ar->dimen; i++)
2626 int tmp = (int)ar->dimen_type[i];
2627 write_atom (ATOM_INTEGER, &tmp);
2630 else
2632 for (i = 0; i < ar->dimen; i++)
2634 require_atom (ATOM_INTEGER);
2635 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2639 if (iomode == IO_INPUT)
2641 ar->where = gfc_current_locus;
2643 for (i = 0; i < ar->dimen; i++)
2644 ar->c_where[i] = gfc_current_locus;
2647 mio_rparen ();
2651 /* Saves or restores a pointer. The pointer is converted back and
2652 forth from an integer. We return the pointer_info pointer so that
2653 the caller can take additional action based on the pointer type. */
2655 static pointer_info *
2656 mio_pointer_ref (void *gp)
2658 pointer_info *p;
2660 if (iomode == IO_OUTPUT)
2662 p = get_pointer (*((char **) gp));
2663 write_atom (ATOM_INTEGER, &p->integer);
2665 else
2667 require_atom (ATOM_INTEGER);
2668 p = add_fixup (atom_int, gp);
2671 return p;
2675 /* Save and load references to components that occur within
2676 expressions. We have to describe these references by a number and
2677 by name. The number is necessary for forward references during
2678 reading, and the name is necessary if the symbol already exists in
2679 the namespace and is not loaded again. */
2681 static void
2682 mio_component_ref (gfc_component **cp)
2684 pointer_info *p;
2686 p = mio_pointer_ref (cp);
2687 if (p->type == P_UNKNOWN)
2688 p->type = P_COMPONENT;
2692 static void mio_namespace_ref (gfc_namespace **nsp);
2693 static void mio_formal_arglist (gfc_formal_arglist **formal);
2694 static void mio_typebound_proc (gfc_typebound_proc** proc);
2696 static void
2697 mio_component (gfc_component *c, int vtype)
2699 pointer_info *p;
2700 int n;
2702 mio_lparen ();
2704 if (iomode == IO_OUTPUT)
2706 p = get_pointer (c);
2707 mio_integer (&p->integer);
2709 else
2711 mio_integer (&n);
2712 p = get_integer (n);
2713 associate_integer_pointer (p, c);
2716 if (p->type == P_UNKNOWN)
2717 p->type = P_COMPONENT;
2719 mio_pool_string (&c->name);
2720 mio_typespec (&c->ts);
2721 mio_array_spec (&c->as);
2723 mio_symbol_attribute (&c->attr);
2724 if (c->ts.type == BT_CLASS)
2725 c->attr.class_ok = 1;
2726 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2728 if (!vtype || strcmp (c->name, "_final") == 0
2729 || strcmp (c->name, "_hash") == 0)
2730 mio_expr (&c->initializer);
2732 if (c->attr.proc_pointer)
2733 mio_typebound_proc (&c->tb);
2735 mio_rparen ();
2739 static void
2740 mio_component_list (gfc_component **cp, int vtype)
2742 gfc_component *c, *tail;
2744 mio_lparen ();
2746 if (iomode == IO_OUTPUT)
2748 for (c = *cp; c; c = c->next)
2749 mio_component (c, vtype);
2751 else
2753 *cp = NULL;
2754 tail = NULL;
2756 for (;;)
2758 if (peek_atom () == ATOM_RPAREN)
2759 break;
2761 c = gfc_get_component ();
2762 mio_component (c, vtype);
2764 if (tail == NULL)
2765 *cp = c;
2766 else
2767 tail->next = c;
2769 tail = c;
2773 mio_rparen ();
2777 static void
2778 mio_actual_arg (gfc_actual_arglist *a)
2780 mio_lparen ();
2781 mio_pool_string (&a->name);
2782 mio_expr (&a->expr);
2783 mio_rparen ();
2787 static void
2788 mio_actual_arglist (gfc_actual_arglist **ap)
2790 gfc_actual_arglist *a, *tail;
2792 mio_lparen ();
2794 if (iomode == IO_OUTPUT)
2796 for (a = *ap; a; a = a->next)
2797 mio_actual_arg (a);
2800 else
2802 tail = NULL;
2804 for (;;)
2806 if (peek_atom () != ATOM_LPAREN)
2807 break;
2809 a = gfc_get_actual_arglist ();
2811 if (tail == NULL)
2812 *ap = a;
2813 else
2814 tail->next = a;
2816 tail = a;
2817 mio_actual_arg (a);
2821 mio_rparen ();
2825 /* Read and write formal argument lists. */
2827 static void
2828 mio_formal_arglist (gfc_formal_arglist **formal)
2830 gfc_formal_arglist *f, *tail;
2832 mio_lparen ();
2834 if (iomode == IO_OUTPUT)
2836 for (f = *formal; f; f = f->next)
2837 mio_symbol_ref (&f->sym);
2839 else
2841 *formal = tail = NULL;
2843 while (peek_atom () != ATOM_RPAREN)
2845 f = gfc_get_formal_arglist ();
2846 mio_symbol_ref (&f->sym);
2848 if (*formal == NULL)
2849 *formal = f;
2850 else
2851 tail->next = f;
2853 tail = f;
2857 mio_rparen ();
2861 /* Save or restore a reference to a symbol node. */
2863 pointer_info *
2864 mio_symbol_ref (gfc_symbol **symp)
2866 pointer_info *p;
2868 p = mio_pointer_ref (symp);
2869 if (p->type == P_UNKNOWN)
2870 p->type = P_SYMBOL;
2872 if (iomode == IO_OUTPUT)
2874 if (p->u.wsym.state == UNREFERENCED)
2875 p->u.wsym.state = NEEDS_WRITE;
2877 else
2879 if (p->u.rsym.state == UNUSED)
2880 p->u.rsym.state = NEEDED;
2882 return p;
2886 /* Save or restore a reference to a symtree node. */
2888 static void
2889 mio_symtree_ref (gfc_symtree **stp)
2891 pointer_info *p;
2892 fixup_t *f;
2894 if (iomode == IO_OUTPUT)
2895 mio_symbol_ref (&(*stp)->n.sym);
2896 else
2898 require_atom (ATOM_INTEGER);
2899 p = get_integer (atom_int);
2901 /* An unused equivalence member; make a symbol and a symtree
2902 for it. */
2903 if (in_load_equiv && p->u.rsym.symtree == NULL)
2905 /* Since this is not used, it must have a unique name. */
2906 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2908 /* Make the symbol. */
2909 if (p->u.rsym.sym == NULL)
2911 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2912 gfc_current_ns);
2913 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2916 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2917 p->u.rsym.symtree->n.sym->refs++;
2918 p->u.rsym.referenced = 1;
2920 /* If the symbol is PRIVATE and in COMMON, load_commons will
2921 generate a fixup symbol, which must be associated. */
2922 if (p->fixup)
2923 resolve_fixups (p->fixup, p->u.rsym.sym);
2924 p->fixup = NULL;
2927 if (p->type == P_UNKNOWN)
2928 p->type = P_SYMBOL;
2930 if (p->u.rsym.state == UNUSED)
2931 p->u.rsym.state = NEEDED;
2933 if (p->u.rsym.symtree != NULL)
2935 *stp = p->u.rsym.symtree;
2937 else
2939 f = XCNEW (fixup_t);
2941 f->next = p->u.rsym.stfixup;
2942 p->u.rsym.stfixup = f;
2944 f->pointer = (void **) stp;
2950 static void
2951 mio_iterator (gfc_iterator **ip)
2953 gfc_iterator *iter;
2955 mio_lparen ();
2957 if (iomode == IO_OUTPUT)
2959 if (*ip == NULL)
2960 goto done;
2962 else
2964 if (peek_atom () == ATOM_RPAREN)
2966 *ip = NULL;
2967 goto done;
2970 *ip = gfc_get_iterator ();
2973 iter = *ip;
2975 mio_expr (&iter->var);
2976 mio_expr (&iter->start);
2977 mio_expr (&iter->end);
2978 mio_expr (&iter->step);
2980 done:
2981 mio_rparen ();
2985 static void
2986 mio_constructor (gfc_constructor_base *cp)
2988 gfc_constructor *c;
2990 mio_lparen ();
2992 if (iomode == IO_OUTPUT)
2994 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2996 mio_lparen ();
2997 mio_expr (&c->expr);
2998 mio_iterator (&c->iterator);
2999 mio_rparen ();
3002 else
3004 while (peek_atom () != ATOM_RPAREN)
3006 c = gfc_constructor_append_expr (cp, NULL, NULL);
3008 mio_lparen ();
3009 mio_expr (&c->expr);
3010 mio_iterator (&c->iterator);
3011 mio_rparen ();
3015 mio_rparen ();
3019 static const mstring ref_types[] = {
3020 minit ("ARRAY", REF_ARRAY),
3021 minit ("COMPONENT", REF_COMPONENT),
3022 minit ("SUBSTRING", REF_SUBSTRING),
3023 minit (NULL, -1)
3027 static void
3028 mio_ref (gfc_ref **rp)
3030 gfc_ref *r;
3032 mio_lparen ();
3034 r = *rp;
3035 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3037 switch (r->type)
3039 case REF_ARRAY:
3040 mio_array_ref (&r->u.ar);
3041 break;
3043 case REF_COMPONENT:
3044 mio_symbol_ref (&r->u.c.sym);
3045 mio_component_ref (&r->u.c.component);
3046 break;
3048 case REF_SUBSTRING:
3049 mio_expr (&r->u.ss.start);
3050 mio_expr (&r->u.ss.end);
3051 mio_charlen (&r->u.ss.length);
3052 break;
3055 mio_rparen ();
3059 static void
3060 mio_ref_list (gfc_ref **rp)
3062 gfc_ref *ref, *head, *tail;
3064 mio_lparen ();
3066 if (iomode == IO_OUTPUT)
3068 for (ref = *rp; ref; ref = ref->next)
3069 mio_ref (&ref);
3071 else
3073 head = tail = NULL;
3075 while (peek_atom () != ATOM_RPAREN)
3077 if (head == NULL)
3078 head = tail = gfc_get_ref ();
3079 else
3081 tail->next = gfc_get_ref ();
3082 tail = tail->next;
3085 mio_ref (&tail);
3088 *rp = head;
3091 mio_rparen ();
3095 /* Read and write an integer value. */
3097 static void
3098 mio_gmp_integer (mpz_t *integer)
3100 char *p;
3102 if (iomode == IO_INPUT)
3104 if (parse_atom () != ATOM_STRING)
3105 bad_module ("Expected integer string");
3107 mpz_init (*integer);
3108 if (mpz_set_str (*integer, atom_string, 10))
3109 bad_module ("Error converting integer");
3111 free (atom_string);
3113 else
3115 p = mpz_get_str (NULL, 10, *integer);
3116 write_atom (ATOM_STRING, p);
3117 free (p);
3122 static void
3123 mio_gmp_real (mpfr_t *real)
3125 mp_exp_t exponent;
3126 char *p;
3128 if (iomode == IO_INPUT)
3130 if (parse_atom () != ATOM_STRING)
3131 bad_module ("Expected real string");
3133 mpfr_init (*real);
3134 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3135 free (atom_string);
3137 else
3139 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3141 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3143 write_atom (ATOM_STRING, p);
3144 free (p);
3145 return;
3148 atom_string = XCNEWVEC (char, strlen (p) + 20);
3150 sprintf (atom_string, "0.%s@%ld", p, exponent);
3152 /* Fix negative numbers. */
3153 if (atom_string[2] == '-')
3155 atom_string[0] = '-';
3156 atom_string[1] = '0';
3157 atom_string[2] = '.';
3160 write_atom (ATOM_STRING, atom_string);
3162 free (atom_string);
3163 free (p);
3168 /* Save and restore the shape of an array constructor. */
3170 static void
3171 mio_shape (mpz_t **pshape, int rank)
3173 mpz_t *shape;
3174 atom_type t;
3175 int n;
3177 /* A NULL shape is represented by (). */
3178 mio_lparen ();
3180 if (iomode == IO_OUTPUT)
3182 shape = *pshape;
3183 if (!shape)
3185 mio_rparen ();
3186 return;
3189 else
3191 t = peek_atom ();
3192 if (t == ATOM_RPAREN)
3194 *pshape = NULL;
3195 mio_rparen ();
3196 return;
3199 shape = gfc_get_shape (rank);
3200 *pshape = shape;
3203 for (n = 0; n < rank; n++)
3204 mio_gmp_integer (&shape[n]);
3206 mio_rparen ();
3210 static const mstring expr_types[] = {
3211 minit ("OP", EXPR_OP),
3212 minit ("FUNCTION", EXPR_FUNCTION),
3213 minit ("CONSTANT", EXPR_CONSTANT),
3214 minit ("VARIABLE", EXPR_VARIABLE),
3215 minit ("SUBSTRING", EXPR_SUBSTRING),
3216 minit ("STRUCTURE", EXPR_STRUCTURE),
3217 minit ("ARRAY", EXPR_ARRAY),
3218 minit ("NULL", EXPR_NULL),
3219 minit ("COMPCALL", EXPR_COMPCALL),
3220 minit (NULL, -1)
3223 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3224 generic operators, not in expressions. INTRINSIC_USER is also
3225 replaced by the correct function name by the time we see it. */
3227 static const mstring intrinsics[] =
3229 minit ("UPLUS", INTRINSIC_UPLUS),
3230 minit ("UMINUS", INTRINSIC_UMINUS),
3231 minit ("PLUS", INTRINSIC_PLUS),
3232 minit ("MINUS", INTRINSIC_MINUS),
3233 minit ("TIMES", INTRINSIC_TIMES),
3234 minit ("DIVIDE", INTRINSIC_DIVIDE),
3235 minit ("POWER", INTRINSIC_POWER),
3236 minit ("CONCAT", INTRINSIC_CONCAT),
3237 minit ("AND", INTRINSIC_AND),
3238 minit ("OR", INTRINSIC_OR),
3239 minit ("EQV", INTRINSIC_EQV),
3240 minit ("NEQV", INTRINSIC_NEQV),
3241 minit ("EQ_SIGN", INTRINSIC_EQ),
3242 minit ("EQ", INTRINSIC_EQ_OS),
3243 minit ("NE_SIGN", INTRINSIC_NE),
3244 minit ("NE", INTRINSIC_NE_OS),
3245 minit ("GT_SIGN", INTRINSIC_GT),
3246 minit ("GT", INTRINSIC_GT_OS),
3247 minit ("GE_SIGN", INTRINSIC_GE),
3248 minit ("GE", INTRINSIC_GE_OS),
3249 minit ("LT_SIGN", INTRINSIC_LT),
3250 minit ("LT", INTRINSIC_LT_OS),
3251 minit ("LE_SIGN", INTRINSIC_LE),
3252 minit ("LE", INTRINSIC_LE_OS),
3253 minit ("NOT", INTRINSIC_NOT),
3254 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3255 minit ("USER", INTRINSIC_USER),
3256 minit (NULL, -1)
3260 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3262 static void
3263 fix_mio_expr (gfc_expr *e)
3265 gfc_symtree *ns_st = NULL;
3266 const char *fname;
3268 if (iomode != IO_OUTPUT)
3269 return;
3271 if (e->symtree)
3273 /* If this is a symtree for a symbol that came from a contained module
3274 namespace, it has a unique name and we should look in the current
3275 namespace to see if the required, non-contained symbol is available
3276 yet. If so, the latter should be written. */
3277 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3279 const char *name = e->symtree->n.sym->name;
3280 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3281 name = dt_upper_string (name);
3282 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3285 /* On the other hand, if the existing symbol is the module name or the
3286 new symbol is a dummy argument, do not do the promotion. */
3287 if (ns_st && ns_st->n.sym
3288 && ns_st->n.sym->attr.flavor != FL_MODULE
3289 && !e->symtree->n.sym->attr.dummy)
3290 e->symtree = ns_st;
3292 else if (e->expr_type == EXPR_FUNCTION
3293 && (e->value.function.name || e->value.function.isym))
3295 gfc_symbol *sym;
3297 /* In some circumstances, a function used in an initialization
3298 expression, in one use associated module, can fail to be
3299 coupled to its symtree when used in a specification
3300 expression in another module. */
3301 fname = e->value.function.esym ? e->value.function.esym->name
3302 : e->value.function.isym->name;
3303 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3305 if (e->symtree)
3306 return;
3308 /* This is probably a reference to a private procedure from another
3309 module. To prevent a segfault, make a generic with no specific
3310 instances. If this module is used, without the required
3311 specific coming from somewhere, the appropriate error message
3312 is issued. */
3313 gfc_get_symbol (fname, gfc_current_ns, &sym);
3314 sym->attr.flavor = FL_PROCEDURE;
3315 sym->attr.generic = 1;
3316 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3317 gfc_commit_symbol (sym);
3322 /* Read and write expressions. The form "()" is allowed to indicate a
3323 NULL expression. */
3325 static void
3326 mio_expr (gfc_expr **ep)
3328 gfc_expr *e;
3329 atom_type t;
3330 int flag;
3332 mio_lparen ();
3334 if (iomode == IO_OUTPUT)
3336 if (*ep == NULL)
3338 mio_rparen ();
3339 return;
3342 e = *ep;
3343 MIO_NAME (expr_t) (e->expr_type, expr_types);
3345 else
3347 t = parse_atom ();
3348 if (t == ATOM_RPAREN)
3350 *ep = NULL;
3351 return;
3354 if (t != ATOM_NAME)
3355 bad_module ("Expected expression type");
3357 e = *ep = gfc_get_expr ();
3358 e->where = gfc_current_locus;
3359 e->expr_type = (expr_t) find_enum (expr_types);
3362 mio_typespec (&e->ts);
3363 mio_integer (&e->rank);
3365 fix_mio_expr (e);
3367 switch (e->expr_type)
3369 case EXPR_OP:
3370 e->value.op.op
3371 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3373 switch (e->value.op.op)
3375 case INTRINSIC_UPLUS:
3376 case INTRINSIC_UMINUS:
3377 case INTRINSIC_NOT:
3378 case INTRINSIC_PARENTHESES:
3379 mio_expr (&e->value.op.op1);
3380 break;
3382 case INTRINSIC_PLUS:
3383 case INTRINSIC_MINUS:
3384 case INTRINSIC_TIMES:
3385 case INTRINSIC_DIVIDE:
3386 case INTRINSIC_POWER:
3387 case INTRINSIC_CONCAT:
3388 case INTRINSIC_AND:
3389 case INTRINSIC_OR:
3390 case INTRINSIC_EQV:
3391 case INTRINSIC_NEQV:
3392 case INTRINSIC_EQ:
3393 case INTRINSIC_EQ_OS:
3394 case INTRINSIC_NE:
3395 case INTRINSIC_NE_OS:
3396 case INTRINSIC_GT:
3397 case INTRINSIC_GT_OS:
3398 case INTRINSIC_GE:
3399 case INTRINSIC_GE_OS:
3400 case INTRINSIC_LT:
3401 case INTRINSIC_LT_OS:
3402 case INTRINSIC_LE:
3403 case INTRINSIC_LE_OS:
3404 mio_expr (&e->value.op.op1);
3405 mio_expr (&e->value.op.op2);
3406 break;
3408 case INTRINSIC_USER:
3409 /* INTRINSIC_USER should not appear in resolved expressions,
3410 though for UDRs we need to stream unresolved ones. */
3411 if (iomode == IO_OUTPUT)
3412 write_atom (ATOM_STRING, e->value.op.uop->name);
3413 else
3415 char *name = read_string ();
3416 const char *uop_name = find_use_name (name, true);
3417 if (uop_name == NULL)
3419 size_t len = strlen (name);
3420 char *name2 = XCNEWVEC (char, len + 2);
3421 memcpy (name2, name, len);
3422 name2[len] = ' ';
3423 name2[len + 1] = '\0';
3424 free (name);
3425 uop_name = name = name2;
3427 e->value.op.uop = gfc_get_uop (uop_name);
3428 free (name);
3430 mio_expr (&e->value.op.op1);
3431 mio_expr (&e->value.op.op2);
3432 break;
3434 default:
3435 bad_module ("Bad operator");
3438 break;
3440 case EXPR_FUNCTION:
3441 mio_symtree_ref (&e->symtree);
3442 mio_actual_arglist (&e->value.function.actual);
3444 if (iomode == IO_OUTPUT)
3446 e->value.function.name
3447 = mio_allocated_string (e->value.function.name);
3448 if (e->value.function.esym)
3449 flag = 1;
3450 else if (e->ref)
3451 flag = 2;
3452 else if (e->value.function.isym == NULL)
3453 flag = 3;
3454 else
3455 flag = 0;
3456 mio_integer (&flag);
3457 switch (flag)
3459 case 1:
3460 mio_symbol_ref (&e->value.function.esym);
3461 break;
3462 case 2:
3463 mio_ref_list (&e->ref);
3464 break;
3465 case 3:
3466 break;
3467 default:
3468 write_atom (ATOM_STRING, e->value.function.isym->name);
3471 else
3473 require_atom (ATOM_STRING);
3474 if (atom_string[0] == '\0')
3475 e->value.function.name = NULL;
3476 else
3477 e->value.function.name = gfc_get_string (atom_string);
3478 free (atom_string);
3480 mio_integer (&flag);
3481 switch (flag)
3483 case 1:
3484 mio_symbol_ref (&e->value.function.esym);
3485 break;
3486 case 2:
3487 mio_ref_list (&e->ref);
3488 break;
3489 case 3:
3490 break;
3491 default:
3492 require_atom (ATOM_STRING);
3493 e->value.function.isym = gfc_find_function (atom_string);
3494 free (atom_string);
3498 break;
3500 case EXPR_VARIABLE:
3501 mio_symtree_ref (&e->symtree);
3502 mio_ref_list (&e->ref);
3503 break;
3505 case EXPR_SUBSTRING:
3506 e->value.character.string
3507 = CONST_CAST (gfc_char_t *,
3508 mio_allocated_wide_string (e->value.character.string,
3509 e->value.character.length));
3510 mio_ref_list (&e->ref);
3511 break;
3513 case EXPR_STRUCTURE:
3514 case EXPR_ARRAY:
3515 mio_constructor (&e->value.constructor);
3516 mio_shape (&e->shape, e->rank);
3517 break;
3519 case EXPR_CONSTANT:
3520 switch (e->ts.type)
3522 case BT_INTEGER:
3523 mio_gmp_integer (&e->value.integer);
3524 break;
3526 case BT_REAL:
3527 gfc_set_model_kind (e->ts.kind);
3528 mio_gmp_real (&e->value.real);
3529 break;
3531 case BT_COMPLEX:
3532 gfc_set_model_kind (e->ts.kind);
3533 mio_gmp_real (&mpc_realref (e->value.complex));
3534 mio_gmp_real (&mpc_imagref (e->value.complex));
3535 break;
3537 case BT_LOGICAL:
3538 mio_integer (&e->value.logical);
3539 break;
3541 case BT_CHARACTER:
3542 mio_integer (&e->value.character.length);
3543 e->value.character.string
3544 = CONST_CAST (gfc_char_t *,
3545 mio_allocated_wide_string (e->value.character.string,
3546 e->value.character.length));
3547 break;
3549 default:
3550 bad_module ("Bad type in constant expression");
3553 break;
3555 case EXPR_NULL:
3556 break;
3558 case EXPR_COMPCALL:
3559 case EXPR_PPC:
3560 gcc_unreachable ();
3561 break;
3564 mio_rparen ();
3568 /* Read and write namelists. */
3570 static void
3571 mio_namelist (gfc_symbol *sym)
3573 gfc_namelist *n, *m;
3574 const char *check_name;
3576 mio_lparen ();
3578 if (iomode == IO_OUTPUT)
3580 for (n = sym->namelist; n; n = n->next)
3581 mio_symbol_ref (&n->sym);
3583 else
3585 /* This departure from the standard is flagged as an error.
3586 It does, in fact, work correctly. TODO: Allow it
3587 conditionally? */
3588 if (sym->attr.flavor == FL_NAMELIST)
3590 check_name = find_use_name (sym->name, false);
3591 if (check_name && strcmp (check_name, sym->name) != 0)
3592 gfc_error ("Namelist %s cannot be renamed by USE "
3593 "association to %s", sym->name, check_name);
3596 m = NULL;
3597 while (peek_atom () != ATOM_RPAREN)
3599 n = gfc_get_namelist ();
3600 mio_symbol_ref (&n->sym);
3602 if (sym->namelist == NULL)
3603 sym->namelist = n;
3604 else
3605 m->next = n;
3607 m = n;
3609 sym->namelist_tail = m;
3612 mio_rparen ();
3616 /* Save/restore lists of gfc_interface structures. When loading an
3617 interface, we are really appending to the existing list of
3618 interfaces. Checking for duplicate and ambiguous interfaces has to
3619 be done later when all symbols have been loaded. */
3621 pointer_info *
3622 mio_interface_rest (gfc_interface **ip)
3624 gfc_interface *tail, *p;
3625 pointer_info *pi = NULL;
3627 if (iomode == IO_OUTPUT)
3629 if (ip != NULL)
3630 for (p = *ip; p; p = p->next)
3631 mio_symbol_ref (&p->sym);
3633 else
3635 if (*ip == NULL)
3636 tail = NULL;
3637 else
3639 tail = *ip;
3640 while (tail->next)
3641 tail = tail->next;
3644 for (;;)
3646 if (peek_atom () == ATOM_RPAREN)
3647 break;
3649 p = gfc_get_interface ();
3650 p->where = gfc_current_locus;
3651 pi = mio_symbol_ref (&p->sym);
3653 if (tail == NULL)
3654 *ip = p;
3655 else
3656 tail->next = p;
3658 tail = p;
3662 mio_rparen ();
3663 return pi;
3667 /* Save/restore a nameless operator interface. */
3669 static void
3670 mio_interface (gfc_interface **ip)
3672 mio_lparen ();
3673 mio_interface_rest (ip);
3677 /* Save/restore a named operator interface. */
3679 static void
3680 mio_symbol_interface (const char **name, const char **module,
3681 gfc_interface **ip)
3683 mio_lparen ();
3684 mio_pool_string (name);
3685 mio_pool_string (module);
3686 mio_interface_rest (ip);
3690 static void
3691 mio_namespace_ref (gfc_namespace **nsp)
3693 gfc_namespace *ns;
3694 pointer_info *p;
3696 p = mio_pointer_ref (nsp);
3698 if (p->type == P_UNKNOWN)
3699 p->type = P_NAMESPACE;
3701 if (iomode == IO_INPUT && p->integer != 0)
3703 ns = (gfc_namespace *) p->u.pointer;
3704 if (ns == NULL)
3706 ns = gfc_get_namespace (NULL, 0);
3707 associate_integer_pointer (p, ns);
3709 else
3710 ns->refs++;
3715 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3717 static gfc_namespace* current_f2k_derived;
3719 static void
3720 mio_typebound_proc (gfc_typebound_proc** proc)
3722 int flag;
3723 int overriding_flag;
3725 if (iomode == IO_INPUT)
3727 *proc = gfc_get_typebound_proc (NULL);
3728 (*proc)->where = gfc_current_locus;
3730 gcc_assert (*proc);
3732 mio_lparen ();
3734 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3736 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3737 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3738 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3739 overriding_flag = mio_name (overriding_flag, binding_overriding);
3740 (*proc)->deferred = ((overriding_flag & 2) != 0);
3741 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3742 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3744 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3745 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3746 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3748 mio_pool_string (&((*proc)->pass_arg));
3750 flag = (int) (*proc)->pass_arg_num;
3751 mio_integer (&flag);
3752 (*proc)->pass_arg_num = (unsigned) flag;
3754 if ((*proc)->is_generic)
3756 gfc_tbp_generic* g;
3757 int iop;
3759 mio_lparen ();
3761 if (iomode == IO_OUTPUT)
3762 for (g = (*proc)->u.generic; g; g = g->next)
3764 iop = (int) g->is_operator;
3765 mio_integer (&iop);
3766 mio_allocated_string (g->specific_st->name);
3768 else
3770 (*proc)->u.generic = NULL;
3771 while (peek_atom () != ATOM_RPAREN)
3773 gfc_symtree** sym_root;
3775 g = gfc_get_tbp_generic ();
3776 g->specific = NULL;
3778 mio_integer (&iop);
3779 g->is_operator = (bool) iop;
3781 require_atom (ATOM_STRING);
3782 sym_root = &current_f2k_derived->tb_sym_root;
3783 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3784 free (atom_string);
3786 g->next = (*proc)->u.generic;
3787 (*proc)->u.generic = g;
3791 mio_rparen ();
3793 else if (!(*proc)->ppc)
3794 mio_symtree_ref (&(*proc)->u.specific);
3796 mio_rparen ();
3799 /* Walker-callback function for this purpose. */
3800 static void
3801 mio_typebound_symtree (gfc_symtree* st)
3803 if (iomode == IO_OUTPUT && !st->n.tb)
3804 return;
3806 if (iomode == IO_OUTPUT)
3808 mio_lparen ();
3809 mio_allocated_string (st->name);
3811 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3813 mio_typebound_proc (&st->n.tb);
3814 mio_rparen ();
3817 /* IO a full symtree (in all depth). */
3818 static void
3819 mio_full_typebound_tree (gfc_symtree** root)
3821 mio_lparen ();
3823 if (iomode == IO_OUTPUT)
3824 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3825 else
3827 while (peek_atom () == ATOM_LPAREN)
3829 gfc_symtree* st;
3831 mio_lparen ();
3833 require_atom (ATOM_STRING);
3834 st = gfc_get_tbp_symtree (root, atom_string);
3835 free (atom_string);
3837 mio_typebound_symtree (st);
3841 mio_rparen ();
3844 static void
3845 mio_finalizer (gfc_finalizer **f)
3847 if (iomode == IO_OUTPUT)
3849 gcc_assert (*f);
3850 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3851 mio_symtree_ref (&(*f)->proc_tree);
3853 else
3855 *f = gfc_get_finalizer ();
3856 (*f)->where = gfc_current_locus; /* Value should not matter. */
3857 (*f)->next = NULL;
3859 mio_symtree_ref (&(*f)->proc_tree);
3860 (*f)->proc_sym = NULL;
3864 static void
3865 mio_f2k_derived (gfc_namespace *f2k)
3867 current_f2k_derived = f2k;
3869 /* Handle the list of finalizer procedures. */
3870 mio_lparen ();
3871 if (iomode == IO_OUTPUT)
3873 gfc_finalizer *f;
3874 for (f = f2k->finalizers; f; f = f->next)
3875 mio_finalizer (&f);
3877 else
3879 f2k->finalizers = NULL;
3880 while (peek_atom () != ATOM_RPAREN)
3882 gfc_finalizer *cur = NULL;
3883 mio_finalizer (&cur);
3884 cur->next = f2k->finalizers;
3885 f2k->finalizers = cur;
3888 mio_rparen ();
3890 /* Handle type-bound procedures. */
3891 mio_full_typebound_tree (&f2k->tb_sym_root);
3893 /* Type-bound user operators. */
3894 mio_full_typebound_tree (&f2k->tb_uop_root);
3896 /* Type-bound intrinsic operators. */
3897 mio_lparen ();
3898 if (iomode == IO_OUTPUT)
3900 int op;
3901 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3903 gfc_intrinsic_op realop;
3905 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3906 continue;
3908 mio_lparen ();
3909 realop = (gfc_intrinsic_op) op;
3910 mio_intrinsic_op (&realop);
3911 mio_typebound_proc (&f2k->tb_op[op]);
3912 mio_rparen ();
3915 else
3916 while (peek_atom () != ATOM_RPAREN)
3918 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3920 mio_lparen ();
3921 mio_intrinsic_op (&op);
3922 mio_typebound_proc (&f2k->tb_op[op]);
3923 mio_rparen ();
3925 mio_rparen ();
3928 static void
3929 mio_full_f2k_derived (gfc_symbol *sym)
3931 mio_lparen ();
3933 if (iomode == IO_OUTPUT)
3935 if (sym->f2k_derived)
3936 mio_f2k_derived (sym->f2k_derived);
3938 else
3940 if (peek_atom () != ATOM_RPAREN)
3942 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3943 mio_f2k_derived (sym->f2k_derived);
3945 else
3946 gcc_assert (!sym->f2k_derived);
3949 mio_rparen ();
3952 static const mstring omp_declare_simd_clauses[] =
3954 minit ("INBRANCH", 0),
3955 minit ("NOTINBRANCH", 1),
3956 minit ("SIMDLEN", 2),
3957 minit ("UNIFORM", 3),
3958 minit ("LINEAR", 4),
3959 minit ("ALIGNED", 5),
3960 minit (NULL, -1)
3963 /* Handle !$omp declare simd. */
3965 static void
3966 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
3968 if (iomode == IO_OUTPUT)
3970 if (*odsp == NULL)
3971 return;
3973 else if (peek_atom () != ATOM_LPAREN)
3974 return;
3976 gfc_omp_declare_simd *ods = *odsp;
3978 mio_lparen ();
3979 if (iomode == IO_OUTPUT)
3981 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
3982 if (ods->clauses)
3984 gfc_omp_namelist *n;
3986 if (ods->clauses->inbranch)
3987 mio_name (0, omp_declare_simd_clauses);
3988 if (ods->clauses->notinbranch)
3989 mio_name (1, omp_declare_simd_clauses);
3990 if (ods->clauses->simdlen_expr)
3992 mio_name (2, omp_declare_simd_clauses);
3993 mio_expr (&ods->clauses->simdlen_expr);
3995 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
3997 mio_name (3, omp_declare_simd_clauses);
3998 mio_symbol_ref (&n->sym);
4000 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4002 mio_name (4, omp_declare_simd_clauses);
4003 mio_symbol_ref (&n->sym);
4004 mio_expr (&n->expr);
4006 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4008 mio_name (5, omp_declare_simd_clauses);
4009 mio_symbol_ref (&n->sym);
4010 mio_expr (&n->expr);
4014 else
4016 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4018 require_atom (ATOM_NAME);
4019 *odsp = ods = gfc_get_omp_declare_simd ();
4020 ods->where = gfc_current_locus;
4021 ods->proc_name = ns->proc_name;
4022 if (peek_atom () == ATOM_NAME)
4024 ods->clauses = gfc_get_omp_clauses ();
4025 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4026 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4027 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4029 while (peek_atom () == ATOM_NAME)
4031 gfc_omp_namelist *n;
4032 int t = mio_name (0, omp_declare_simd_clauses);
4034 switch (t)
4036 case 0: ods->clauses->inbranch = true; break;
4037 case 1: ods->clauses->notinbranch = true; break;
4038 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4039 case 3:
4040 case 4:
4041 case 5:
4042 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4043 ptrs[t - 3] = &n->next;
4044 mio_symbol_ref (&n->sym);
4045 if (t != 3)
4046 mio_expr (&n->expr);
4047 break;
4052 mio_omp_declare_simd (ns, &ods->next);
4054 mio_rparen ();
4058 static const mstring omp_declare_reduction_stmt[] =
4060 minit ("ASSIGN", 0),
4061 minit ("CALL", 1),
4062 minit (NULL, -1)
4066 static void
4067 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4068 gfc_namespace *ns, bool is_initializer)
4070 if (iomode == IO_OUTPUT)
4072 if ((*sym1)->module == NULL)
4074 (*sym1)->module = module_name;
4075 (*sym2)->module = module_name;
4077 mio_symbol_ref (sym1);
4078 mio_symbol_ref (sym2);
4079 if (ns->code->op == EXEC_ASSIGN)
4081 mio_name (0, omp_declare_reduction_stmt);
4082 mio_expr (&ns->code->expr1);
4083 mio_expr (&ns->code->expr2);
4085 else
4087 int flag;
4088 mio_name (1, omp_declare_reduction_stmt);
4089 mio_symtree_ref (&ns->code->symtree);
4090 mio_actual_arglist (&ns->code->ext.actual);
4092 flag = ns->code->resolved_isym != NULL;
4093 mio_integer (&flag);
4094 if (flag)
4095 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4096 else
4097 mio_symbol_ref (&ns->code->resolved_sym);
4100 else
4102 pointer_info *p1 = mio_symbol_ref (sym1);
4103 pointer_info *p2 = mio_symbol_ref (sym2);
4104 gfc_symbol *sym;
4105 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4106 gcc_assert (p1->u.rsym.sym == NULL);
4107 /* Add hidden symbols to the symtree. */
4108 pointer_info *q = get_integer (p1->u.rsym.ns);
4109 q->u.pointer = (void *) ns;
4110 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4111 sym->ts = udr->ts;
4112 sym->module = gfc_get_string (p1->u.rsym.module);
4113 associate_integer_pointer (p1, sym);
4114 sym->attr.omp_udr_artificial_var = 1;
4115 gcc_assert (p2->u.rsym.sym == NULL);
4116 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4117 sym->ts = udr->ts;
4118 sym->module = gfc_get_string (p2->u.rsym.module);
4119 associate_integer_pointer (p2, sym);
4120 sym->attr.omp_udr_artificial_var = 1;
4121 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4123 ns->code = gfc_get_code (EXEC_ASSIGN);
4124 mio_expr (&ns->code->expr1);
4125 mio_expr (&ns->code->expr2);
4127 else
4129 int flag;
4130 ns->code = gfc_get_code (EXEC_CALL);
4131 mio_symtree_ref (&ns->code->symtree);
4132 mio_actual_arglist (&ns->code->ext.actual);
4134 mio_integer (&flag);
4135 if (flag)
4137 require_atom (ATOM_STRING);
4138 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4139 free (atom_string);
4141 else
4142 mio_symbol_ref (&ns->code->resolved_sym);
4144 ns->code->loc = gfc_current_locus;
4145 ns->omp_udr_ns = 1;
4150 /* Unlike most other routines, the address of the symbol node is already
4151 fixed on input and the name/module has already been filled in.
4152 If you update the symbol format here, don't forget to update read_module
4153 as well (look for "seek to the symbol's component list"). */
4155 static void
4156 mio_symbol (gfc_symbol *sym)
4158 int intmod = INTMOD_NONE;
4160 mio_lparen ();
4162 mio_symbol_attribute (&sym->attr);
4164 /* Note that components are always saved, even if they are supposed
4165 to be private. Component access is checked during searching. */
4166 mio_component_list (&sym->components, sym->attr.vtype);
4167 if (sym->components != NULL)
4168 sym->component_access
4169 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4171 mio_typespec (&sym->ts);
4172 if (sym->ts.type == BT_CLASS)
4173 sym->attr.class_ok = 1;
4175 if (iomode == IO_OUTPUT)
4176 mio_namespace_ref (&sym->formal_ns);
4177 else
4179 mio_namespace_ref (&sym->formal_ns);
4180 if (sym->formal_ns)
4181 sym->formal_ns->proc_name = sym;
4184 /* Save/restore common block links. */
4185 mio_symbol_ref (&sym->common_next);
4187 mio_formal_arglist (&sym->formal);
4189 if (sym->attr.flavor == FL_PARAMETER)
4190 mio_expr (&sym->value);
4192 mio_array_spec (&sym->as);
4194 mio_symbol_ref (&sym->result);
4196 if (sym->attr.cray_pointee)
4197 mio_symbol_ref (&sym->cp_pointer);
4199 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4200 mio_full_f2k_derived (sym);
4202 mio_namelist (sym);
4204 /* Add the fields that say whether this is from an intrinsic module,
4205 and if so, what symbol it is within the module. */
4206 /* mio_integer (&(sym->from_intmod)); */
4207 if (iomode == IO_OUTPUT)
4209 intmod = sym->from_intmod;
4210 mio_integer (&intmod);
4212 else
4214 mio_integer (&intmod);
4215 if (current_intmod)
4216 sym->from_intmod = current_intmod;
4217 else
4218 sym->from_intmod = (intmod_id) intmod;
4221 mio_integer (&(sym->intmod_sym_id));
4223 if (sym->attr.flavor == FL_DERIVED)
4224 mio_integer (&(sym->hash_value));
4226 if (sym->formal_ns
4227 && sym->formal_ns->proc_name == sym
4228 && sym->formal_ns->entries == NULL)
4229 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4231 mio_rparen ();
4235 /************************* Top level subroutines *************************/
4237 /* Given a root symtree node and a symbol, try to find a symtree that
4238 references the symbol that is not a unique name. */
4240 static gfc_symtree *
4241 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
4243 gfc_symtree *s = NULL;
4245 if (st == NULL)
4246 return s;
4248 s = find_symtree_for_symbol (st->right, sym);
4249 if (s != NULL)
4250 return s;
4251 s = find_symtree_for_symbol (st->left, sym);
4252 if (s != NULL)
4253 return s;
4255 if (st->n.sym == sym && !check_unique_name (st->name))
4256 return st;
4258 return s;
4262 /* A recursive function to look for a specific symbol by name and by
4263 module. Whilst several symtrees might point to one symbol, its
4264 is sufficient for the purposes here than one exist. Note that
4265 generic interfaces are distinguished as are symbols that have been
4266 renamed in another module. */
4267 static gfc_symtree *
4268 find_symbol (gfc_symtree *st, const char *name,
4269 const char *module, int generic)
4271 int c;
4272 gfc_symtree *retval, *s;
4274 if (st == NULL || st->n.sym == NULL)
4275 return NULL;
4277 c = strcmp (name, st->n.sym->name);
4278 if (c == 0 && st->n.sym->module
4279 && strcmp (module, st->n.sym->module) == 0
4280 && !check_unique_name (st->name))
4282 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4284 /* Detect symbols that are renamed by use association in another
4285 module by the absence of a symtree and null attr.use_rename,
4286 since the latter is not transmitted in the module file. */
4287 if (((!generic && !st->n.sym->attr.generic)
4288 || (generic && st->n.sym->attr.generic))
4289 && !(s == NULL && !st->n.sym->attr.use_rename))
4290 return st;
4293 retval = find_symbol (st->left, name, module, generic);
4295 if (retval == NULL)
4296 retval = find_symbol (st->right, name, module, generic);
4298 return retval;
4302 /* Skip a list between balanced left and right parens.
4303 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4304 have been already parsed by hand, and the remaining of the content is to be
4305 skipped here. The default value is 0 (balanced parens). */
4307 static void
4308 skip_list (int nest_level = 0)
4310 int level;
4312 level = nest_level;
4315 switch (parse_atom ())
4317 case ATOM_LPAREN:
4318 level++;
4319 break;
4321 case ATOM_RPAREN:
4322 level--;
4323 break;
4325 case ATOM_STRING:
4326 free (atom_string);
4327 break;
4329 case ATOM_NAME:
4330 case ATOM_INTEGER:
4331 break;
4334 while (level > 0);
4338 /* Load operator interfaces from the module. Interfaces are unusual
4339 in that they attach themselves to existing symbols. */
4341 static void
4342 load_operator_interfaces (void)
4344 const char *p;
4345 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4346 gfc_user_op *uop;
4347 pointer_info *pi = NULL;
4348 int n, i;
4350 mio_lparen ();
4352 while (peek_atom () != ATOM_RPAREN)
4354 mio_lparen ();
4356 mio_internal_string (name);
4357 mio_internal_string (module);
4359 n = number_use_names (name, true);
4360 n = n ? n : 1;
4362 for (i = 1; i <= n; i++)
4364 /* Decide if we need to load this one or not. */
4365 p = find_use_name_n (name, &i, true);
4367 if (p == NULL)
4369 while (parse_atom () != ATOM_RPAREN);
4370 continue;
4373 if (i == 1)
4375 uop = gfc_get_uop (p);
4376 pi = mio_interface_rest (&uop->op);
4378 else
4380 if (gfc_find_uop (p, NULL))
4381 continue;
4382 uop = gfc_get_uop (p);
4383 uop->op = gfc_get_interface ();
4384 uop->op->where = gfc_current_locus;
4385 add_fixup (pi->integer, &uop->op->sym);
4390 mio_rparen ();
4394 /* Load interfaces from the module. Interfaces are unusual in that
4395 they attach themselves to existing symbols. */
4397 static void
4398 load_generic_interfaces (void)
4400 const char *p;
4401 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4402 gfc_symbol *sym;
4403 gfc_interface *generic = NULL, *gen = NULL;
4404 int n, i, renamed;
4405 bool ambiguous_set = false;
4407 mio_lparen ();
4409 while (peek_atom () != ATOM_RPAREN)
4411 mio_lparen ();
4413 mio_internal_string (name);
4414 mio_internal_string (module);
4416 n = number_use_names (name, false);
4417 renamed = n ? 1 : 0;
4418 n = n ? n : 1;
4420 for (i = 1; i <= n; i++)
4422 gfc_symtree *st;
4423 /* Decide if we need to load this one or not. */
4424 p = find_use_name_n (name, &i, false);
4426 st = find_symbol (gfc_current_ns->sym_root,
4427 name, module_name, 1);
4429 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4431 /* Skip the specific names for these cases. */
4432 while (i == 1 && parse_atom () != ATOM_RPAREN);
4434 continue;
4437 /* If the symbol exists already and is being USEd without being
4438 in an ONLY clause, do not load a new symtree(11.3.2). */
4439 if (!only_flag && st)
4440 sym = st->n.sym;
4442 if (!sym)
4444 if (st)
4446 sym = st->n.sym;
4447 if (strcmp (st->name, p) != 0)
4449 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4450 st->n.sym = sym;
4451 sym->refs++;
4455 /* Since we haven't found a valid generic interface, we had
4456 better make one. */
4457 if (!sym)
4459 gfc_get_symbol (p, NULL, &sym);
4460 sym->name = gfc_get_string (name);
4461 sym->module = module_name;
4462 sym->attr.flavor = FL_PROCEDURE;
4463 sym->attr.generic = 1;
4464 sym->attr.use_assoc = 1;
4467 else
4469 /* Unless sym is a generic interface, this reference
4470 is ambiguous. */
4471 if (st == NULL)
4472 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4474 sym = st->n.sym;
4476 if (st && !sym->attr.generic
4477 && !st->ambiguous
4478 && sym->module
4479 && strcmp (module, sym->module))
4481 ambiguous_set = true;
4482 st->ambiguous = 1;
4486 sym->attr.use_only = only_flag;
4487 sym->attr.use_rename = renamed;
4489 if (i == 1)
4491 mio_interface_rest (&sym->generic);
4492 generic = sym->generic;
4494 else if (!sym->generic)
4496 sym->generic = generic;
4497 sym->attr.generic_copy = 1;
4500 /* If a procedure that is not generic has generic interfaces
4501 that include itself, it is generic! We need to take care
4502 to retain symbols ambiguous that were already so. */
4503 if (sym->attr.use_assoc
4504 && !sym->attr.generic
4505 && sym->attr.flavor == FL_PROCEDURE)
4507 for (gen = generic; gen; gen = gen->next)
4509 if (gen->sym == sym)
4511 sym->attr.generic = 1;
4512 if (ambiguous_set)
4513 st->ambiguous = 0;
4514 break;
4522 mio_rparen ();
4526 /* Load common blocks. */
4528 static void
4529 load_commons (void)
4531 char name[GFC_MAX_SYMBOL_LEN + 1];
4532 gfc_common_head *p;
4534 mio_lparen ();
4536 while (peek_atom () != ATOM_RPAREN)
4538 int flags;
4539 char* label;
4540 mio_lparen ();
4541 mio_internal_string (name);
4543 p = gfc_get_common (name, 1);
4545 mio_symbol_ref (&p->head);
4546 mio_integer (&flags);
4547 if (flags & 1)
4548 p->saved = 1;
4549 if (flags & 2)
4550 p->threadprivate = 1;
4551 p->use_assoc = 1;
4553 /* Get whether this was a bind(c) common or not. */
4554 mio_integer (&p->is_bind_c);
4555 /* Get the binding label. */
4556 label = read_string ();
4557 if (strlen (label))
4558 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4559 XDELETEVEC (label);
4561 mio_rparen ();
4564 mio_rparen ();
4568 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4569 so that unused variables are not loaded and so that the expression can
4570 be safely freed. */
4572 static void
4573 load_equiv (void)
4575 gfc_equiv *head, *tail, *end, *eq, *equiv;
4576 bool duplicate;
4578 mio_lparen ();
4579 in_load_equiv = true;
4581 end = gfc_current_ns->equiv;
4582 while (end != NULL && end->next != NULL)
4583 end = end->next;
4585 while (peek_atom () != ATOM_RPAREN) {
4586 mio_lparen ();
4587 head = tail = NULL;
4589 while(peek_atom () != ATOM_RPAREN)
4591 if (head == NULL)
4592 head = tail = gfc_get_equiv ();
4593 else
4595 tail->eq = gfc_get_equiv ();
4596 tail = tail->eq;
4599 mio_pool_string (&tail->module);
4600 mio_expr (&tail->expr);
4603 /* Check for duplicate equivalences being loaded from different modules */
4604 duplicate = false;
4605 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
4607 if (equiv->module && head->module
4608 && strcmp (equiv->module, head->module) == 0)
4610 duplicate = true;
4611 break;
4615 if (duplicate)
4617 for (eq = head; eq; eq = head)
4619 head = eq->eq;
4620 gfc_free_expr (eq->expr);
4621 free (eq);
4625 if (end == NULL)
4626 gfc_current_ns->equiv = head;
4627 else
4628 end->next = head;
4630 if (head != NULL)
4631 end = head;
4633 mio_rparen ();
4636 mio_rparen ();
4637 in_load_equiv = false;
4641 /* This function loads OpenMP user defined reductions. */
4642 static void
4643 load_omp_udrs (void)
4645 mio_lparen ();
4646 while (peek_atom () != ATOM_RPAREN)
4648 const char *name, *newname;
4649 char *altname;
4650 gfc_typespec ts;
4651 gfc_symtree *st;
4652 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4654 mio_lparen ();
4655 mio_pool_string (&name);
4656 mio_typespec (&ts);
4657 if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
4659 const char *p = name + sizeof ("operator ") - 1;
4660 if (strcmp (p, "+") == 0)
4661 rop = OMP_REDUCTION_PLUS;
4662 else if (strcmp (p, "*") == 0)
4663 rop = OMP_REDUCTION_TIMES;
4664 else if (strcmp (p, "-") == 0)
4665 rop = OMP_REDUCTION_MINUS;
4666 else if (strcmp (p, ".and.") == 0)
4667 rop = OMP_REDUCTION_AND;
4668 else if (strcmp (p, ".or.") == 0)
4669 rop = OMP_REDUCTION_OR;
4670 else if (strcmp (p, ".eqv.") == 0)
4671 rop = OMP_REDUCTION_EQV;
4672 else if (strcmp (p, ".neqv.") == 0)
4673 rop = OMP_REDUCTION_NEQV;
4675 altname = NULL;
4676 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4678 size_t len = strlen (name + 1);
4679 altname = XALLOCAVEC (char, len);
4680 gcc_assert (name[len] == '.');
4681 memcpy (altname, name + 1, len - 1);
4682 altname[len - 1] = '\0';
4684 newname = name;
4685 if (rop == OMP_REDUCTION_USER)
4686 newname = find_use_name (altname ? altname : name, !!altname);
4687 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4688 newname = NULL;
4689 if (newname == NULL)
4691 skip_list (1);
4692 continue;
4694 if (altname && newname != altname)
4696 size_t len = strlen (newname);
4697 altname = XALLOCAVEC (char, len + 3);
4698 altname[0] = '.';
4699 memcpy (altname + 1, newname, len);
4700 altname[len + 1] = '.';
4701 altname[len + 2] = '\0';
4702 name = gfc_get_string (altname);
4704 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4705 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4706 if (udr)
4708 require_atom (ATOM_INTEGER);
4709 pointer_info *p = get_integer (atom_int);
4710 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4712 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4713 "module %s at %L",
4714 p->u.rsym.module, &gfc_current_locus);
4715 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4716 "%s at %L",
4717 udr->omp_out->module, &udr->where);
4719 skip_list (1);
4720 continue;
4722 udr = gfc_get_omp_udr ();
4723 udr->name = name;
4724 udr->rop = rop;
4725 udr->ts = ts;
4726 udr->where = gfc_current_locus;
4727 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4728 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4729 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4730 false);
4731 if (peek_atom () != ATOM_RPAREN)
4733 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4734 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4735 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4736 udr->initializer_ns, true);
4738 if (st)
4740 udr->next = st->n.omp_udr;
4741 st->n.omp_udr = udr;
4743 else
4745 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4746 st->n.omp_udr = udr;
4748 mio_rparen ();
4750 mio_rparen ();
4754 /* Recursive function to traverse the pointer_info tree and load a
4755 needed symbol. We return nonzero if we load a symbol and stop the
4756 traversal, because the act of loading can alter the tree. */
4758 static int
4759 load_needed (pointer_info *p)
4761 gfc_namespace *ns;
4762 pointer_info *q;
4763 gfc_symbol *sym;
4764 int rv;
4766 rv = 0;
4767 if (p == NULL)
4768 return rv;
4770 rv |= load_needed (p->left);
4771 rv |= load_needed (p->right);
4773 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4774 return rv;
4776 p->u.rsym.state = USED;
4778 set_module_locus (&p->u.rsym.where);
4780 sym = p->u.rsym.sym;
4781 if (sym == NULL)
4783 q = get_integer (p->u.rsym.ns);
4785 ns = (gfc_namespace *) q->u.pointer;
4786 if (ns == NULL)
4788 /* Create an interface namespace if necessary. These are
4789 the namespaces that hold the formal parameters of module
4790 procedures. */
4792 ns = gfc_get_namespace (NULL, 0);
4793 associate_integer_pointer (q, ns);
4796 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4797 doesn't go pear-shaped if the symbol is used. */
4798 if (!ns->proc_name)
4799 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4800 1, &ns->proc_name);
4802 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4803 sym->name = dt_lower_string (p->u.rsym.true_name);
4804 sym->module = gfc_get_string (p->u.rsym.module);
4805 if (p->u.rsym.binding_label)
4806 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4807 (p->u.rsym.binding_label));
4809 associate_integer_pointer (p, sym);
4812 mio_symbol (sym);
4813 sym->attr.use_assoc = 1;
4815 /* Mark as only or rename for later diagnosis for explicitly imported
4816 but not used warnings; don't mark internal symbols such as __vtab,
4817 __def_init etc. Only mark them if they have been explicitly loaded. */
4819 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4821 gfc_use_rename *u;
4823 /* Search the use/rename list for the variable; if the variable is
4824 found, mark it. */
4825 for (u = gfc_rename_list; u; u = u->next)
4827 if (strcmp (u->use_name, sym->name) == 0)
4829 sym->attr.use_only = 1;
4830 break;
4835 if (p->u.rsym.renamed)
4836 sym->attr.use_rename = 1;
4838 return 1;
4842 /* Recursive function for cleaning up things after a module has been read. */
4844 static void
4845 read_cleanup (pointer_info *p)
4847 gfc_symtree *st;
4848 pointer_info *q;
4850 if (p == NULL)
4851 return;
4853 read_cleanup (p->left);
4854 read_cleanup (p->right);
4856 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4858 gfc_namespace *ns;
4859 /* Add hidden symbols to the symtree. */
4860 q = get_integer (p->u.rsym.ns);
4861 ns = (gfc_namespace *) q->u.pointer;
4863 if (!p->u.rsym.sym->attr.vtype
4864 && !p->u.rsym.sym->attr.vtab)
4865 st = gfc_get_unique_symtree (ns);
4866 else
4868 /* There is no reason to use 'unique_symtrees' for vtabs or
4869 vtypes - their name is fine for a symtree and reduces the
4870 namespace pollution. */
4871 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4872 if (!st)
4873 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4876 st->n.sym = p->u.rsym.sym;
4877 st->n.sym->refs++;
4879 /* Fixup any symtree references. */
4880 p->u.rsym.symtree = st;
4881 resolve_fixups (p->u.rsym.stfixup, st);
4882 p->u.rsym.stfixup = NULL;
4885 /* Free unused symbols. */
4886 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4887 gfc_free_symbol (p->u.rsym.sym);
4891 /* It is not quite enough to check for ambiguity in the symbols by
4892 the loaded symbol and the new symbol not being identical. */
4893 static bool
4894 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
4896 gfc_symbol *rsym;
4897 module_locus locus;
4898 symbol_attribute attr;
4899 gfc_symbol *st_sym;
4901 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
4903 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4904 "current program unit", st->name, module_name);
4905 return true;
4908 st_sym = st->n.sym;
4909 rsym = info->u.rsym.sym;
4910 if (st_sym == rsym)
4911 return false;
4913 if (st_sym->attr.vtab || st_sym->attr.vtype)
4914 return false;
4916 /* If the existing symbol is generic from a different module and
4917 the new symbol is generic there can be no ambiguity. */
4918 if (st_sym->attr.generic
4919 && st_sym->module
4920 && st_sym->module != module_name)
4922 /* The new symbol's attributes have not yet been read. Since
4923 we need attr.generic, read it directly. */
4924 get_module_locus (&locus);
4925 set_module_locus (&info->u.rsym.where);
4926 mio_lparen ();
4927 attr.generic = 0;
4928 mio_symbol_attribute (&attr);
4929 set_module_locus (&locus);
4930 if (attr.generic)
4931 return false;
4934 return true;
4938 /* Read a module file. */
4940 static void
4941 read_module (void)
4943 module_locus operator_interfaces, user_operators, omp_udrs;
4944 const char *p;
4945 char name[GFC_MAX_SYMBOL_LEN + 1];
4946 int i;
4947 /* Workaround -Wmaybe-uninitialized false positive during
4948 profiledbootstrap by initializing them. */
4949 int ambiguous = 0, j, nuse, symbol = 0;
4950 pointer_info *info, *q;
4951 gfc_use_rename *u = NULL;
4952 gfc_symtree *st;
4953 gfc_symbol *sym;
4955 get_module_locus (&operator_interfaces); /* Skip these for now. */
4956 skip_list ();
4958 get_module_locus (&user_operators);
4959 skip_list ();
4960 skip_list ();
4962 /* Skip commons and equivalences for now. */
4963 skip_list ();
4964 skip_list ();
4966 /* Skip OpenMP UDRs. */
4967 get_module_locus (&omp_udrs);
4968 skip_list ();
4970 mio_lparen ();
4972 /* Create the fixup nodes for all the symbols. */
4974 while (peek_atom () != ATOM_RPAREN)
4976 char* bind_label;
4977 require_atom (ATOM_INTEGER);
4978 info = get_integer (atom_int);
4980 info->type = P_SYMBOL;
4981 info->u.rsym.state = UNUSED;
4983 info->u.rsym.true_name = read_string ();
4984 info->u.rsym.module = read_string ();
4985 bind_label = read_string ();
4986 if (strlen (bind_label))
4987 info->u.rsym.binding_label = bind_label;
4988 else
4989 XDELETEVEC (bind_label);
4991 require_atom (ATOM_INTEGER);
4992 info->u.rsym.ns = atom_int;
4994 get_module_locus (&info->u.rsym.where);
4996 /* See if the symbol has already been loaded by a previous module.
4997 If so, we reference the existing symbol and prevent it from
4998 being loaded again. This should not happen if the symbol being
4999 read is an index for an assumed shape dummy array (ns != 1). */
5001 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5003 if (sym == NULL
5004 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5006 skip_list ();
5007 continue;
5010 info->u.rsym.state = USED;
5011 info->u.rsym.sym = sym;
5012 /* The current symbol has already been loaded, so we can avoid loading
5013 it again. However, if it is a derived type, some of its components
5014 can be used in expressions in the module. To avoid the module loading
5015 failing, we need to associate the module's component pointer indexes
5016 with the existing symbol's component pointers. */
5017 if (sym->attr.flavor == FL_DERIVED)
5019 gfc_component *c;
5021 /* First seek to the symbol's component list. */
5022 mio_lparen (); /* symbol opening. */
5023 skip_list (); /* skip symbol attribute. */
5025 mio_lparen (); /* component list opening. */
5026 for (c = sym->components; c; c = c->next)
5028 pointer_info *p;
5029 const char *comp_name;
5030 int n;
5032 mio_lparen (); /* component opening. */
5033 mio_integer (&n);
5034 p = get_integer (n);
5035 if (p->u.pointer == NULL)
5036 associate_integer_pointer (p, c);
5037 mio_pool_string (&comp_name);
5038 gcc_assert (comp_name == c->name);
5039 skip_list (1); /* component end. */
5041 mio_rparen (); /* component list closing. */
5043 skip_list (1); /* symbol end. */
5045 else
5046 skip_list ();
5048 /* Some symbols do not have a namespace (eg. formal arguments),
5049 so the automatic "unique symtree" mechanism must be suppressed
5050 by marking them as referenced. */
5051 q = get_integer (info->u.rsym.ns);
5052 if (q->u.pointer == NULL)
5054 info->u.rsym.referenced = 1;
5055 continue;
5058 /* If possible recycle the symtree that references the symbol.
5059 If a symtree is not found and the module does not import one,
5060 a unique-name symtree is found by read_cleanup. */
5061 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
5062 if (st != NULL)
5064 info->u.rsym.symtree = st;
5065 info->u.rsym.referenced = 1;
5069 mio_rparen ();
5071 /* Parse the symtree lists. This lets us mark which symbols need to
5072 be loaded. Renaming is also done at this point by replacing the
5073 symtree name. */
5075 mio_lparen ();
5077 while (peek_atom () != ATOM_RPAREN)
5079 mio_internal_string (name);
5080 mio_integer (&ambiguous);
5081 mio_integer (&symbol);
5083 info = get_integer (symbol);
5085 /* See how many use names there are. If none, go through the start
5086 of the loop at least once. */
5087 nuse = number_use_names (name, false);
5088 info->u.rsym.renamed = nuse ? 1 : 0;
5090 if (nuse == 0)
5091 nuse = 1;
5093 for (j = 1; j <= nuse; j++)
5095 /* Get the jth local name for this symbol. */
5096 p = find_use_name_n (name, &j, false);
5098 if (p == NULL && strcmp (name, module_name) == 0)
5099 p = name;
5101 /* Exception: Always import vtabs & vtypes. */
5102 if (p == NULL && name[0] == '_'
5103 && (strncmp (name, "__vtab_", 5) == 0
5104 || strncmp (name, "__vtype_", 6) == 0))
5105 p = name;
5107 /* Skip symtree nodes not in an ONLY clause, unless there
5108 is an existing symtree loaded from another USE statement. */
5109 if (p == NULL)
5111 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5112 if (st != NULL
5113 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5114 && st->n.sym->module != NULL
5115 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5117 info->u.rsym.symtree = st;
5118 info->u.rsym.sym = st->n.sym;
5120 continue;
5123 /* If a symbol of the same name and module exists already,
5124 this symbol, which is not in an ONLY clause, must not be
5125 added to the namespace(11.3.2). Note that find_symbol
5126 only returns the first occurrence that it finds. */
5127 if (!only_flag && !info->u.rsym.renamed
5128 && strcmp (name, module_name) != 0
5129 && find_symbol (gfc_current_ns->sym_root, name,
5130 module_name, 0))
5131 continue;
5133 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5135 if (st != NULL)
5137 /* Check for ambiguous symbols. */
5138 if (check_for_ambiguous (st, info))
5139 st->ambiguous = 1;
5140 else
5141 info->u.rsym.symtree = st;
5143 else
5145 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5147 /* Create a symtree node in the current namespace for this
5148 symbol. */
5149 st = check_unique_name (p)
5150 ? gfc_get_unique_symtree (gfc_current_ns)
5151 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5152 st->ambiguous = ambiguous;
5154 sym = info->u.rsym.sym;
5156 /* Create a symbol node if it doesn't already exist. */
5157 if (sym == NULL)
5159 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5160 gfc_current_ns);
5161 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
5162 sym = info->u.rsym.sym;
5163 sym->module = gfc_get_string (info->u.rsym.module);
5165 if (info->u.rsym.binding_label)
5166 sym->binding_label =
5167 IDENTIFIER_POINTER (get_identifier
5168 (info->u.rsym.binding_label));
5171 st->n.sym = sym;
5172 st->n.sym->refs++;
5174 if (strcmp (name, p) != 0)
5175 sym->attr.use_rename = 1;
5177 if (name[0] != '_'
5178 || (strncmp (name, "__vtab_", 5) != 0
5179 && strncmp (name, "__vtype_", 6) != 0))
5180 sym->attr.use_only = only_flag;
5182 /* Store the symtree pointing to this symbol. */
5183 info->u.rsym.symtree = st;
5185 if (info->u.rsym.state == UNUSED)
5186 info->u.rsym.state = NEEDED;
5187 info->u.rsym.referenced = 1;
5192 mio_rparen ();
5194 /* Load intrinsic operator interfaces. */
5195 set_module_locus (&operator_interfaces);
5196 mio_lparen ();
5198 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5200 if (i == INTRINSIC_USER)
5201 continue;
5203 if (only_flag)
5205 u = find_use_operator ((gfc_intrinsic_op) i);
5207 if (u == NULL)
5209 skip_list ();
5210 continue;
5213 u->found = 1;
5216 mio_interface (&gfc_current_ns->op[i]);
5217 if (u && !gfc_current_ns->op[i])
5218 u->found = 0;
5221 mio_rparen ();
5223 /* Load generic and user operator interfaces. These must follow the
5224 loading of symtree because otherwise symbols can be marked as
5225 ambiguous. */
5227 set_module_locus (&user_operators);
5229 load_operator_interfaces ();
5230 load_generic_interfaces ();
5232 load_commons ();
5233 load_equiv ();
5235 /* Load OpenMP user defined reductions. */
5236 set_module_locus (&omp_udrs);
5237 load_omp_udrs ();
5239 /* At this point, we read those symbols that are needed but haven't
5240 been loaded yet. If one symbol requires another, the other gets
5241 marked as NEEDED if its previous state was UNUSED. */
5243 while (load_needed (pi_root));
5245 /* Make sure all elements of the rename-list were found in the module. */
5247 for (u = gfc_rename_list; u; u = u->next)
5249 if (u->found)
5250 continue;
5252 if (u->op == INTRINSIC_NONE)
5254 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5255 u->use_name, &u->where, module_name);
5256 continue;
5259 if (u->op == INTRINSIC_USER)
5261 gfc_error ("User operator %qs referenced at %L not found "
5262 "in module %qs", u->use_name, &u->where, module_name);
5263 continue;
5266 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5267 "in module %qs", gfc_op2string (u->op), &u->where,
5268 module_name);
5271 /* Clean up symbol nodes that were never loaded, create references
5272 to hidden symbols. */
5274 read_cleanup (pi_root);
5278 /* Given an access type that is specific to an entity and the default
5279 access, return nonzero if the entity is publicly accessible. If the
5280 element is declared as PUBLIC, then it is public; if declared
5281 PRIVATE, then private, and otherwise it is public unless the default
5282 access in this context has been declared PRIVATE. */
5284 static bool dump_smod = false;
5286 static bool
5287 check_access (gfc_access specific_access, gfc_access default_access)
5289 if (dump_smod)
5290 return true;
5292 if (specific_access == ACCESS_PUBLIC)
5293 return TRUE;
5294 if (specific_access == ACCESS_PRIVATE)
5295 return FALSE;
5297 if (flag_module_private)
5298 return default_access == ACCESS_PUBLIC;
5299 else
5300 return default_access != ACCESS_PRIVATE;
5304 bool
5305 gfc_check_symbol_access (gfc_symbol *sym)
5307 if (sym->attr.vtab || sym->attr.vtype)
5308 return true;
5309 else
5310 return check_access (sym->attr.access, sym->ns->default_access);
5314 /* A structure to remember which commons we've already written. */
5316 struct written_common
5318 BBT_HEADER(written_common);
5319 const char *name, *label;
5322 static struct written_common *written_commons = NULL;
5324 /* Comparison function used for balancing the binary tree. */
5326 static int
5327 compare_written_commons (void *a1, void *b1)
5329 const char *aname = ((struct written_common *) a1)->name;
5330 const char *alabel = ((struct written_common *) a1)->label;
5331 const char *bname = ((struct written_common *) b1)->name;
5332 const char *blabel = ((struct written_common *) b1)->label;
5333 int c = strcmp (aname, bname);
5335 return (c != 0 ? c : strcmp (alabel, blabel));
5338 /* Free a list of written commons. */
5340 static void
5341 free_written_common (struct written_common *w)
5343 if (!w)
5344 return;
5346 if (w->left)
5347 free_written_common (w->left);
5348 if (w->right)
5349 free_written_common (w->right);
5351 free (w);
5354 /* Write a common block to the module -- recursive helper function. */
5356 static void
5357 write_common_0 (gfc_symtree *st, bool this_module)
5359 gfc_common_head *p;
5360 const char * name;
5361 int flags;
5362 const char *label;
5363 struct written_common *w;
5364 bool write_me = true;
5366 if (st == NULL)
5367 return;
5369 write_common_0 (st->left, this_module);
5371 /* We will write out the binding label, or "" if no label given. */
5372 name = st->n.common->name;
5373 p = st->n.common;
5374 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5376 /* Check if we've already output this common. */
5377 w = written_commons;
5378 while (w)
5380 int c = strcmp (name, w->name);
5381 c = (c != 0 ? c : strcmp (label, w->label));
5382 if (c == 0)
5383 write_me = false;
5385 w = (c < 0) ? w->left : w->right;
5388 if (this_module && p->use_assoc)
5389 write_me = false;
5391 if (write_me)
5393 /* Write the common to the module. */
5394 mio_lparen ();
5395 mio_pool_string (&name);
5397 mio_symbol_ref (&p->head);
5398 flags = p->saved ? 1 : 0;
5399 if (p->threadprivate)
5400 flags |= 2;
5401 mio_integer (&flags);
5403 /* Write out whether the common block is bind(c) or not. */
5404 mio_integer (&(p->is_bind_c));
5406 mio_pool_string (&label);
5407 mio_rparen ();
5409 /* Record that we have written this common. */
5410 w = XCNEW (struct written_common);
5411 w->name = p->name;
5412 w->label = label;
5413 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5416 write_common_0 (st->right, this_module);
5420 /* Write a common, by initializing the list of written commons, calling
5421 the recursive function write_common_0() and cleaning up afterwards. */
5423 static void
5424 write_common (gfc_symtree *st)
5426 written_commons = NULL;
5427 write_common_0 (st, true);
5428 write_common_0 (st, false);
5429 free_written_common (written_commons);
5430 written_commons = NULL;
5434 /* Write the blank common block to the module. */
5436 static void
5437 write_blank_common (void)
5439 const char * name = BLANK_COMMON_NAME;
5440 int saved;
5441 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5442 this, but it hasn't been checked. Just making it so for now. */
5443 int is_bind_c = 0;
5445 if (gfc_current_ns->blank_common.head == NULL)
5446 return;
5448 mio_lparen ();
5450 mio_pool_string (&name);
5452 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5453 saved = gfc_current_ns->blank_common.saved;
5454 mio_integer (&saved);
5456 /* Write out whether the common block is bind(c) or not. */
5457 mio_integer (&is_bind_c);
5459 /* Write out an empty binding label. */
5460 write_atom (ATOM_STRING, "");
5462 mio_rparen ();
5466 /* Write equivalences to the module. */
5468 static void
5469 write_equiv (void)
5471 gfc_equiv *eq, *e;
5472 int num;
5474 num = 0;
5475 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5477 mio_lparen ();
5479 for (e = eq; e; e = e->eq)
5481 if (e->module == NULL)
5482 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5483 mio_allocated_string (e->module);
5484 mio_expr (&e->expr);
5487 num++;
5488 mio_rparen ();
5493 /* Write a symbol to the module. */
5495 static void
5496 write_symbol (int n, gfc_symbol *sym)
5498 const char *label;
5500 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5501 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5503 mio_integer (&n);
5505 if (sym->attr.flavor == FL_DERIVED)
5507 const char *name;
5508 name = dt_upper_string (sym->name);
5509 mio_pool_string (&name);
5511 else
5512 mio_pool_string (&sym->name);
5514 mio_pool_string (&sym->module);
5515 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5517 label = sym->binding_label;
5518 mio_pool_string (&label);
5520 else
5521 write_atom (ATOM_STRING, "");
5523 mio_pointer_ref (&sym->ns);
5525 mio_symbol (sym);
5526 write_char ('\n');
5530 /* Recursive traversal function to write the initial set of symbols to
5531 the module. We check to see if the symbol should be written
5532 according to the access specification. */
5534 static void
5535 write_symbol0 (gfc_symtree *st)
5537 gfc_symbol *sym;
5538 pointer_info *p;
5539 bool dont_write = false;
5541 if (st == NULL)
5542 return;
5544 write_symbol0 (st->left);
5546 sym = st->n.sym;
5547 if (sym->module == NULL)
5548 sym->module = module_name;
5550 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5551 && !sym->attr.subroutine && !sym->attr.function)
5552 dont_write = true;
5554 if (!gfc_check_symbol_access (sym))
5555 dont_write = true;
5557 if (!dont_write)
5559 p = get_pointer (sym);
5560 if (p->type == P_UNKNOWN)
5561 p->type = P_SYMBOL;
5563 if (p->u.wsym.state != WRITTEN)
5565 write_symbol (p->integer, sym);
5566 p->u.wsym.state = WRITTEN;
5570 write_symbol0 (st->right);
5574 static void
5575 write_omp_udr (gfc_omp_udr *udr)
5577 switch (udr->rop)
5579 case OMP_REDUCTION_USER:
5580 /* Non-operators can't be used outside of the module. */
5581 if (udr->name[0] != '.')
5582 return;
5583 else
5585 gfc_symtree *st;
5586 size_t len = strlen (udr->name + 1);
5587 char *name = XALLOCAVEC (char, len);
5588 memcpy (name, udr->name, len - 1);
5589 name[len - 1] = '\0';
5590 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5591 /* If corresponding user operator is private, don't write
5592 the UDR. */
5593 if (st != NULL)
5595 gfc_user_op *uop = st->n.uop;
5596 if (!check_access (uop->access, uop->ns->default_access))
5597 return;
5600 break;
5601 case OMP_REDUCTION_PLUS:
5602 case OMP_REDUCTION_MINUS:
5603 case OMP_REDUCTION_TIMES:
5604 case OMP_REDUCTION_AND:
5605 case OMP_REDUCTION_OR:
5606 case OMP_REDUCTION_EQV:
5607 case OMP_REDUCTION_NEQV:
5608 /* If corresponding operator is private, don't write the UDR. */
5609 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5610 gfc_current_ns->default_access))
5611 return;
5612 break;
5613 default:
5614 break;
5616 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5618 /* If derived type is private, don't write the UDR. */
5619 if (!gfc_check_symbol_access (udr->ts.u.derived))
5620 return;
5623 mio_lparen ();
5624 mio_pool_string (&udr->name);
5625 mio_typespec (&udr->ts);
5626 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5627 if (udr->initializer_ns)
5628 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5629 udr->initializer_ns, true);
5630 mio_rparen ();
5634 static void
5635 write_omp_udrs (gfc_symtree *st)
5637 if (st == NULL)
5638 return;
5640 write_omp_udrs (st->left);
5641 gfc_omp_udr *udr;
5642 for (udr = st->n.omp_udr; udr; udr = udr->next)
5643 write_omp_udr (udr);
5644 write_omp_udrs (st->right);
5648 /* Type for the temporary tree used when writing secondary symbols. */
5650 struct sorted_pointer_info
5652 BBT_HEADER (sorted_pointer_info);
5654 pointer_info *p;
5657 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5659 /* Recursively traverse the temporary tree, free its contents. */
5661 static void
5662 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5664 if (!p)
5665 return;
5667 free_sorted_pointer_info_tree (p->left);
5668 free_sorted_pointer_info_tree (p->right);
5670 free (p);
5673 /* Comparison function for the temporary tree. */
5675 static int
5676 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5678 sorted_pointer_info *spi1, *spi2;
5679 spi1 = (sorted_pointer_info *)_spi1;
5680 spi2 = (sorted_pointer_info *)_spi2;
5682 if (spi1->p->integer < spi2->p->integer)
5683 return -1;
5684 if (spi1->p->integer > spi2->p->integer)
5685 return 1;
5686 return 0;
5690 /* Finds the symbols that need to be written and collects them in the
5691 sorted_pi tree so that they can be traversed in an order
5692 independent of memory addresses. */
5694 static void
5695 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5697 if (!p)
5698 return;
5700 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5702 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5703 sp->p = p;
5705 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5708 find_symbols_to_write (tree, p->left);
5709 find_symbols_to_write (tree, p->right);
5713 /* Recursive function that traverses the tree of symbols that need to be
5714 written and writes them in order. */
5716 static void
5717 write_symbol1_recursion (sorted_pointer_info *sp)
5719 if (!sp)
5720 return;
5722 write_symbol1_recursion (sp->left);
5724 pointer_info *p1 = sp->p;
5725 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5727 p1->u.wsym.state = WRITTEN;
5728 write_symbol (p1->integer, p1->u.wsym.sym);
5729 p1->u.wsym.sym->attr.public_used = 1;
5731 write_symbol1_recursion (sp->right);
5735 /* Write the secondary set of symbols to the module file. These are
5736 symbols that were not public yet are needed by the public symbols
5737 or another dependent symbol. The act of writing a symbol can add
5738 symbols to the pointer_info tree, so we return nonzero if a symbol
5739 was written and pass that information upwards. The caller will
5740 then call this function again until nothing was written. It uses
5741 the utility functions and a temporary tree to ensure a reproducible
5742 ordering of the symbol output and thus the module file. */
5744 static int
5745 write_symbol1 (pointer_info *p)
5747 if (!p)
5748 return 0;
5750 /* Put symbols that need to be written into a tree sorted on the
5751 integer field. */
5753 sorted_pointer_info *spi_root = NULL;
5754 find_symbols_to_write (&spi_root, p);
5756 /* No symbols to write, return. */
5757 if (!spi_root)
5758 return 0;
5760 /* Otherwise, write and free the tree again. */
5761 write_symbol1_recursion (spi_root);
5762 free_sorted_pointer_info_tree (spi_root);
5764 return 1;
5768 /* Write operator interfaces associated with a symbol. */
5770 static void
5771 write_operator (gfc_user_op *uop)
5773 static char nullstring[] = "";
5774 const char *p = nullstring;
5776 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5777 return;
5779 mio_symbol_interface (&uop->name, &p, &uop->op);
5783 /* Write generic interfaces from the namespace sym_root. */
5785 static void
5786 write_generic (gfc_symtree *st)
5788 gfc_symbol *sym;
5790 if (st == NULL)
5791 return;
5793 write_generic (st->left);
5795 sym = st->n.sym;
5796 if (sym && !check_unique_name (st->name)
5797 && sym->generic && gfc_check_symbol_access (sym))
5799 if (!sym->module)
5800 sym->module = module_name;
5802 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5805 write_generic (st->right);
5809 static void
5810 write_symtree (gfc_symtree *st)
5812 gfc_symbol *sym;
5813 pointer_info *p;
5815 sym = st->n.sym;
5817 /* A symbol in an interface body must not be visible in the
5818 module file. */
5819 if (sym->ns != gfc_current_ns
5820 && sym->ns->proc_name
5821 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5822 return;
5824 if (!gfc_check_symbol_access (sym)
5825 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5826 && !sym->attr.subroutine && !sym->attr.function))
5827 return;
5829 if (check_unique_name (st->name))
5830 return;
5832 p = find_pointer (sym);
5833 if (p == NULL)
5834 gfc_internal_error ("write_symtree(): Symbol not written");
5836 mio_pool_string (&st->name);
5837 mio_integer (&st->ambiguous);
5838 mio_integer (&p->integer);
5842 static void
5843 write_module (void)
5845 int i;
5847 /* Write the operator interfaces. */
5848 mio_lparen ();
5850 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5852 if (i == INTRINSIC_USER)
5853 continue;
5855 mio_interface (check_access (gfc_current_ns->operator_access[i],
5856 gfc_current_ns->default_access)
5857 ? &gfc_current_ns->op[i] : NULL);
5860 mio_rparen ();
5861 write_char ('\n');
5862 write_char ('\n');
5864 mio_lparen ();
5865 gfc_traverse_user_op (gfc_current_ns, write_operator);
5866 mio_rparen ();
5867 write_char ('\n');
5868 write_char ('\n');
5870 mio_lparen ();
5871 write_generic (gfc_current_ns->sym_root);
5872 mio_rparen ();
5873 write_char ('\n');
5874 write_char ('\n');
5876 mio_lparen ();
5877 write_blank_common ();
5878 write_common (gfc_current_ns->common_root);
5879 mio_rparen ();
5880 write_char ('\n');
5881 write_char ('\n');
5883 mio_lparen ();
5884 write_equiv ();
5885 mio_rparen ();
5886 write_char ('\n');
5887 write_char ('\n');
5889 mio_lparen ();
5890 write_omp_udrs (gfc_current_ns->omp_udr_root);
5891 mio_rparen ();
5892 write_char ('\n');
5893 write_char ('\n');
5895 /* Write symbol information. First we traverse all symbols in the
5896 primary namespace, writing those that need to be written.
5897 Sometimes writing one symbol will cause another to need to be
5898 written. A list of these symbols ends up on the write stack, and
5899 we end by popping the bottom of the stack and writing the symbol
5900 until the stack is empty. */
5902 mio_lparen ();
5904 write_symbol0 (gfc_current_ns->sym_root);
5905 while (write_symbol1 (pi_root))
5906 /* Nothing. */;
5908 mio_rparen ();
5910 write_char ('\n');
5911 write_char ('\n');
5913 mio_lparen ();
5914 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5915 mio_rparen ();
5919 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5920 true on success, false on failure. */
5922 static bool
5923 read_crc32_from_module_file (const char* filename, uLong* crc)
5925 FILE *file;
5926 char buf[4];
5927 unsigned int val;
5929 /* Open the file in binary mode. */
5930 if ((file = fopen (filename, "rb")) == NULL)
5931 return false;
5933 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5934 file. See RFC 1952. */
5935 if (fseek (file, -8, SEEK_END) != 0)
5937 fclose (file);
5938 return false;
5941 /* Read the CRC32. */
5942 if (fread (buf, 1, 4, file) != 4)
5944 fclose (file);
5945 return false;
5948 /* Close the file. */
5949 fclose (file);
5951 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5952 + ((buf[3] & 0xFF) << 24);
5953 *crc = val;
5955 /* For debugging, the CRC value printed in hexadecimal should match
5956 the CRC printed by "zcat -l -v filename".
5957 printf("CRC of file %s is %x\n", filename, val); */
5959 return true;
5963 /* Given module, dump it to disk. If there was an error while
5964 processing the module, dump_flag will be set to zero and we delete
5965 the module file, even if it was already there. */
5967 static void
5968 dump_module (const char *name, int dump_flag)
5970 int n;
5971 char *filename, *filename_tmp;
5972 uLong crc, crc_old;
5974 module_name = gfc_get_string (name);
5976 if (dump_smod)
5978 name = submodule_name;
5979 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
5981 else
5982 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5984 if (gfc_option.module_dir != NULL)
5986 n += strlen (gfc_option.module_dir);
5987 filename = (char *) alloca (n);
5988 strcpy (filename, gfc_option.module_dir);
5989 strcat (filename, name);
5991 else
5993 filename = (char *) alloca (n);
5994 strcpy (filename, name);
5997 if (dump_smod)
5998 strcat (filename, SUBMODULE_EXTENSION);
5999 else
6000 strcat (filename, MODULE_EXTENSION);
6002 /* Name of the temporary file used to write the module. */
6003 filename_tmp = (char *) alloca (n + 1);
6004 strcpy (filename_tmp, filename);
6005 strcat (filename_tmp, "0");
6007 /* There was an error while processing the module. We delete the
6008 module file, even if it was already there. */
6009 if (!dump_flag)
6011 remove (filename);
6012 return;
6015 if (gfc_cpp_makedep ())
6016 gfc_cpp_add_target (filename);
6018 /* Write the module to the temporary file. */
6019 module_fp = gzopen (filename_tmp, "w");
6020 if (module_fp == NULL)
6021 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6022 filename_tmp, xstrerror (errno));
6024 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6025 MOD_VERSION, gfc_source_file);
6027 /* Write the module itself. */
6028 iomode = IO_OUTPUT;
6030 init_pi_tree ();
6032 write_module ();
6034 free_pi_tree (pi_root);
6035 pi_root = NULL;
6037 write_char ('\n');
6039 if (gzclose (module_fp))
6040 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6041 filename_tmp, xstrerror (errno));
6043 /* Read the CRC32 from the gzip trailers of the module files and
6044 compare. */
6045 if (!read_crc32_from_module_file (filename_tmp, &crc)
6046 || !read_crc32_from_module_file (filename, &crc_old)
6047 || crc_old != crc)
6049 /* Module file have changed, replace the old one. */
6050 if (remove (filename) && errno != ENOENT)
6051 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6052 xstrerror (errno));
6053 if (rename (filename_tmp, filename))
6054 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6055 filename_tmp, filename, xstrerror (errno));
6057 else
6059 if (remove (filename_tmp))
6060 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6061 filename_tmp, xstrerror (errno));
6066 void
6067 gfc_dump_module (const char *name, int dump_flag)
6069 if (gfc_state_stack->state == COMP_SUBMODULE)
6070 dump_smod = true;
6071 else
6072 dump_smod =false;
6074 dump_module (name, dump_flag);
6076 if (dump_smod)
6077 return;
6079 /* Write a submodule file from a module. The 'dump_smod' flag switches
6080 off the check for PRIVATE entities. */
6081 dump_smod = true;
6082 submodule_name = module_name;
6083 dump_module (name, dump_flag);
6084 dump_smod = false;
6087 static void
6088 create_intrinsic_function (const char *name, int id,
6089 const char *modname, intmod_id module,
6090 bool subroutine, gfc_symbol *result_type)
6092 gfc_intrinsic_sym *isym;
6093 gfc_symtree *tmp_symtree;
6094 gfc_symbol *sym;
6096 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6097 if (tmp_symtree)
6099 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6100 return;
6101 gfc_error ("Symbol %qs already declared", name);
6104 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6105 sym = tmp_symtree->n.sym;
6107 if (subroutine)
6109 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6110 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6111 sym->attr.subroutine = 1;
6113 else
6115 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6116 isym = gfc_intrinsic_function_by_id (isym_id);
6118 sym->attr.function = 1;
6119 if (result_type)
6121 sym->ts.type = BT_DERIVED;
6122 sym->ts.u.derived = result_type;
6123 sym->ts.is_c_interop = 1;
6124 isym->ts.f90_type = BT_VOID;
6125 isym->ts.type = BT_DERIVED;
6126 isym->ts.f90_type = BT_VOID;
6127 isym->ts.u.derived = result_type;
6128 isym->ts.is_c_interop = 1;
6131 gcc_assert (isym);
6133 sym->attr.flavor = FL_PROCEDURE;
6134 sym->attr.intrinsic = 1;
6136 sym->module = gfc_get_string (modname);
6137 sym->attr.use_assoc = 1;
6138 sym->from_intmod = module;
6139 sym->intmod_sym_id = id;
6143 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6144 the current namespace for all named constants, pointer types, and
6145 procedures in the module unless the only clause was used or a rename
6146 list was provided. */
6148 static void
6149 import_iso_c_binding_module (void)
6151 gfc_symbol *mod_sym = NULL, *return_type;
6152 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6153 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6154 const char *iso_c_module_name = "__iso_c_binding";
6155 gfc_use_rename *u;
6156 int i;
6157 bool want_c_ptr = false, want_c_funptr = false;
6159 /* Look only in the current namespace. */
6160 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6162 if (mod_symtree == NULL)
6164 /* symtree doesn't already exist in current namespace. */
6165 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6166 false);
6168 if (mod_symtree != NULL)
6169 mod_sym = mod_symtree->n.sym;
6170 else
6171 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6172 "create symbol for %s", iso_c_module_name);
6174 mod_sym->attr.flavor = FL_MODULE;
6175 mod_sym->attr.intrinsic = 1;
6176 mod_sym->module = gfc_get_string (iso_c_module_name);
6177 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6180 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6181 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6182 need C_(FUN)PTR. */
6183 for (u = gfc_rename_list; u; u = u->next)
6185 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6186 u->use_name) == 0)
6187 want_c_ptr = true;
6188 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6189 u->use_name) == 0)
6190 want_c_ptr = true;
6191 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6192 u->use_name) == 0)
6193 want_c_funptr = true;
6194 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6195 u->use_name) == 0)
6196 want_c_funptr = true;
6197 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6198 u->use_name) == 0)
6200 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6201 (iso_c_binding_symbol)
6202 ISOCBINDING_PTR,
6203 u->local_name[0] ? u->local_name
6204 : u->use_name,
6205 NULL, false);
6207 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6208 u->use_name) == 0)
6210 c_funptr
6211 = generate_isocbinding_symbol (iso_c_module_name,
6212 (iso_c_binding_symbol)
6213 ISOCBINDING_FUNPTR,
6214 u->local_name[0] ? u->local_name
6215 : u->use_name,
6216 NULL, false);
6220 if ((want_c_ptr || !only_flag) && !c_ptr)
6221 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6222 (iso_c_binding_symbol)
6223 ISOCBINDING_PTR,
6224 NULL, NULL, only_flag);
6225 if ((want_c_funptr || !only_flag) && !c_funptr)
6226 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6227 (iso_c_binding_symbol)
6228 ISOCBINDING_FUNPTR,
6229 NULL, NULL, only_flag);
6231 /* Generate the symbols for the named constants representing
6232 the kinds for intrinsic data types. */
6233 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6235 bool found = false;
6236 for (u = gfc_rename_list; u; u = u->next)
6237 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6239 bool not_in_std;
6240 const char *name;
6241 u->found = 1;
6242 found = true;
6244 switch (i)
6246 #define NAMED_FUNCTION(a,b,c,d) \
6247 case a: \
6248 not_in_std = (gfc_option.allow_std & d) == 0; \
6249 name = b; \
6250 break;
6251 #define NAMED_SUBROUTINE(a,b,c,d) \
6252 case a: \
6253 not_in_std = (gfc_option.allow_std & d) == 0; \
6254 name = b; \
6255 break;
6256 #define NAMED_INTCST(a,b,c,d) \
6257 case a: \
6258 not_in_std = (gfc_option.allow_std & d) == 0; \
6259 name = b; \
6260 break;
6261 #define NAMED_REALCST(a,b,c,d) \
6262 case a: \
6263 not_in_std = (gfc_option.allow_std & d) == 0; \
6264 name = b; \
6265 break;
6266 #define NAMED_CMPXCST(a,b,c,d) \
6267 case a: \
6268 not_in_std = (gfc_option.allow_std & d) == 0; \
6269 name = b; \
6270 break;
6271 #include "iso-c-binding.def"
6272 default:
6273 not_in_std = false;
6274 name = "";
6277 if (not_in_std)
6279 gfc_error ("The symbol %qs, referenced at %L, is not "
6280 "in the selected standard", name, &u->where);
6281 continue;
6284 switch (i)
6286 #define NAMED_FUNCTION(a,b,c,d) \
6287 case a: \
6288 if (a == ISOCBINDING_LOC) \
6289 return_type = c_ptr->n.sym; \
6290 else if (a == ISOCBINDING_FUNLOC) \
6291 return_type = c_funptr->n.sym; \
6292 else \
6293 return_type = NULL; \
6294 create_intrinsic_function (u->local_name[0] \
6295 ? u->local_name : u->use_name, \
6296 a, iso_c_module_name, \
6297 INTMOD_ISO_C_BINDING, false, \
6298 return_type); \
6299 break;
6300 #define NAMED_SUBROUTINE(a,b,c,d) \
6301 case a: \
6302 create_intrinsic_function (u->local_name[0] ? u->local_name \
6303 : u->use_name, \
6304 a, iso_c_module_name, \
6305 INTMOD_ISO_C_BINDING, true, NULL); \
6306 break;
6307 #include "iso-c-binding.def"
6309 case ISOCBINDING_PTR:
6310 case ISOCBINDING_FUNPTR:
6311 /* Already handled above. */
6312 break;
6313 default:
6314 if (i == ISOCBINDING_NULL_PTR)
6315 tmp_symtree = c_ptr;
6316 else if (i == ISOCBINDING_NULL_FUNPTR)
6317 tmp_symtree = c_funptr;
6318 else
6319 tmp_symtree = NULL;
6320 generate_isocbinding_symbol (iso_c_module_name,
6321 (iso_c_binding_symbol) i,
6322 u->local_name[0]
6323 ? u->local_name : u->use_name,
6324 tmp_symtree, false);
6328 if (!found && !only_flag)
6330 /* Skip, if the symbol is not in the enabled standard. */
6331 switch (i)
6333 #define NAMED_FUNCTION(a,b,c,d) \
6334 case a: \
6335 if ((gfc_option.allow_std & d) == 0) \
6336 continue; \
6337 break;
6338 #define NAMED_SUBROUTINE(a,b,c,d) \
6339 case a: \
6340 if ((gfc_option.allow_std & d) == 0) \
6341 continue; \
6342 break;
6343 #define NAMED_INTCST(a,b,c,d) \
6344 case a: \
6345 if ((gfc_option.allow_std & d) == 0) \
6346 continue; \
6347 break;
6348 #define NAMED_REALCST(a,b,c,d) \
6349 case a: \
6350 if ((gfc_option.allow_std & d) == 0) \
6351 continue; \
6352 break;
6353 #define NAMED_CMPXCST(a,b,c,d) \
6354 case a: \
6355 if ((gfc_option.allow_std & d) == 0) \
6356 continue; \
6357 break;
6358 #include "iso-c-binding.def"
6359 default:
6360 ; /* Not GFC_STD_* versioned. */
6363 switch (i)
6365 #define NAMED_FUNCTION(a,b,c,d) \
6366 case a: \
6367 if (a == ISOCBINDING_LOC) \
6368 return_type = c_ptr->n.sym; \
6369 else if (a == ISOCBINDING_FUNLOC) \
6370 return_type = c_funptr->n.sym; \
6371 else \
6372 return_type = NULL; \
6373 create_intrinsic_function (b, a, iso_c_module_name, \
6374 INTMOD_ISO_C_BINDING, false, \
6375 return_type); \
6376 break;
6377 #define NAMED_SUBROUTINE(a,b,c,d) \
6378 case a: \
6379 create_intrinsic_function (b, a, iso_c_module_name, \
6380 INTMOD_ISO_C_BINDING, true, NULL); \
6381 break;
6382 #include "iso-c-binding.def"
6384 case ISOCBINDING_PTR:
6385 case ISOCBINDING_FUNPTR:
6386 /* Already handled above. */
6387 break;
6388 default:
6389 if (i == ISOCBINDING_NULL_PTR)
6390 tmp_symtree = c_ptr;
6391 else if (i == ISOCBINDING_NULL_FUNPTR)
6392 tmp_symtree = c_funptr;
6393 else
6394 tmp_symtree = NULL;
6395 generate_isocbinding_symbol (iso_c_module_name,
6396 (iso_c_binding_symbol) i, NULL,
6397 tmp_symtree, false);
6402 for (u = gfc_rename_list; u; u = u->next)
6404 if (u->found)
6405 continue;
6407 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6408 "module ISO_C_BINDING", u->use_name, &u->where);
6413 /* Add an integer named constant from a given module. */
6415 static void
6416 create_int_parameter (const char *name, int value, const char *modname,
6417 intmod_id module, int id)
6419 gfc_symtree *tmp_symtree;
6420 gfc_symbol *sym;
6422 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6423 if (tmp_symtree != NULL)
6425 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6426 return;
6427 else
6428 gfc_error ("Symbol %qs already declared", name);
6431 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6432 sym = tmp_symtree->n.sym;
6434 sym->module = gfc_get_string (modname);
6435 sym->attr.flavor = FL_PARAMETER;
6436 sym->ts.type = BT_INTEGER;
6437 sym->ts.kind = gfc_default_integer_kind;
6438 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6439 sym->attr.use_assoc = 1;
6440 sym->from_intmod = module;
6441 sym->intmod_sym_id = id;
6445 /* Value is already contained by the array constructor, but not
6446 yet the shape. */
6448 static void
6449 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6450 const char *modname, intmod_id module, int id)
6452 gfc_symtree *tmp_symtree;
6453 gfc_symbol *sym;
6455 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6456 if (tmp_symtree != NULL)
6458 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6459 return;
6460 else
6461 gfc_error ("Symbol %qs already declared", name);
6464 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6465 sym = tmp_symtree->n.sym;
6467 sym->module = gfc_get_string (modname);
6468 sym->attr.flavor = FL_PARAMETER;
6469 sym->ts.type = BT_INTEGER;
6470 sym->ts.kind = gfc_default_integer_kind;
6471 sym->attr.use_assoc = 1;
6472 sym->from_intmod = module;
6473 sym->intmod_sym_id = id;
6474 sym->attr.dimension = 1;
6475 sym->as = gfc_get_array_spec ();
6476 sym->as->rank = 1;
6477 sym->as->type = AS_EXPLICIT;
6478 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6479 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6481 sym->value = value;
6482 sym->value->shape = gfc_get_shape (1);
6483 mpz_init_set_ui (sym->value->shape[0], size);
6487 /* Add an derived type for a given module. */
6489 static void
6490 create_derived_type (const char *name, const char *modname,
6491 intmod_id module, int id)
6493 gfc_symtree *tmp_symtree;
6494 gfc_symbol *sym, *dt_sym;
6495 gfc_interface *intr, *head;
6497 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6498 if (tmp_symtree != NULL)
6500 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6501 return;
6502 else
6503 gfc_error ("Symbol %qs already declared", name);
6506 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6507 sym = tmp_symtree->n.sym;
6508 sym->module = gfc_get_string (modname);
6509 sym->from_intmod = module;
6510 sym->intmod_sym_id = id;
6511 sym->attr.flavor = FL_PROCEDURE;
6512 sym->attr.function = 1;
6513 sym->attr.generic = 1;
6515 gfc_get_sym_tree (dt_upper_string (sym->name),
6516 gfc_current_ns, &tmp_symtree, false);
6517 dt_sym = tmp_symtree->n.sym;
6518 dt_sym->name = gfc_get_string (sym->name);
6519 dt_sym->attr.flavor = FL_DERIVED;
6520 dt_sym->attr.private_comp = 1;
6521 dt_sym->attr.zero_comp = 1;
6522 dt_sym->attr.use_assoc = 1;
6523 dt_sym->module = gfc_get_string (modname);
6524 dt_sym->from_intmod = module;
6525 dt_sym->intmod_sym_id = id;
6527 head = sym->generic;
6528 intr = gfc_get_interface ();
6529 intr->sym = dt_sym;
6530 intr->where = gfc_current_locus;
6531 intr->next = head;
6532 sym->generic = intr;
6533 sym->attr.if_source = IFSRC_DECL;
6537 /* Read the contents of the module file into a temporary buffer. */
6539 static void
6540 read_module_to_tmpbuf ()
6542 /* We don't know the uncompressed size, so enlarge the buffer as
6543 needed. */
6544 int cursz = 4096;
6545 int rsize = cursz;
6546 int len = 0;
6548 module_content = XNEWVEC (char, cursz);
6550 while (1)
6552 int nread = gzread (module_fp, module_content + len, rsize);
6553 len += nread;
6554 if (nread < rsize)
6555 break;
6556 cursz *= 2;
6557 module_content = XRESIZEVEC (char, module_content, cursz);
6558 rsize = cursz - len;
6561 module_content = XRESIZEVEC (char, module_content, len + 1);
6562 module_content[len] = '\0';
6564 module_pos = 0;
6568 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6570 static void
6571 use_iso_fortran_env_module (void)
6573 static char mod[] = "iso_fortran_env";
6574 gfc_use_rename *u;
6575 gfc_symbol *mod_sym;
6576 gfc_symtree *mod_symtree;
6577 gfc_expr *expr;
6578 int i, j;
6580 intmod_sym symbol[] = {
6581 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6582 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6583 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6584 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6585 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6586 #include "iso-fortran-env.def"
6587 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6589 i = 0;
6590 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6591 #include "iso-fortran-env.def"
6593 /* Generate the symbol for the module itself. */
6594 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6595 if (mod_symtree == NULL)
6597 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6598 gcc_assert (mod_symtree);
6599 mod_sym = mod_symtree->n.sym;
6601 mod_sym->attr.flavor = FL_MODULE;
6602 mod_sym->attr.intrinsic = 1;
6603 mod_sym->module = gfc_get_string (mod);
6604 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6606 else
6607 if (!mod_symtree->n.sym->attr.intrinsic)
6608 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6609 "non-intrinsic module name used previously", mod);
6611 /* Generate the symbols for the module integer named constants. */
6613 for (i = 0; symbol[i].name; i++)
6615 bool found = false;
6616 for (u = gfc_rename_list; u; u = u->next)
6618 if (strcmp (symbol[i].name, u->use_name) == 0)
6620 found = true;
6621 u->found = 1;
6623 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6624 "referenced at %L, is not in the selected "
6625 "standard", symbol[i].name, &u->where))
6626 continue;
6628 if ((flag_default_integer || flag_default_real)
6629 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6630 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
6631 "constant from intrinsic module "
6632 "ISO_FORTRAN_ENV at %L is incompatible with "
6633 "option %qs", &u->where,
6634 flag_default_integer
6635 ? "-fdefault-integer-8"
6636 : "-fdefault-real-8");
6637 switch (symbol[i].id)
6639 #define NAMED_INTCST(a,b,c,d) \
6640 case a:
6641 #include "iso-fortran-env.def"
6642 create_int_parameter (u->local_name[0] ? u->local_name
6643 : u->use_name,
6644 symbol[i].value, mod,
6645 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6646 break;
6648 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6649 case a:\
6650 expr = gfc_get_array_expr (BT_INTEGER, \
6651 gfc_default_integer_kind,\
6652 NULL); \
6653 for (j = 0; KINDS[j].kind != 0; j++) \
6654 gfc_constructor_append_expr (&expr->value.constructor, \
6655 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6656 KINDS[j].kind), NULL); \
6657 create_int_parameter_array (u->local_name[0] ? u->local_name \
6658 : u->use_name, \
6659 j, expr, mod, \
6660 INTMOD_ISO_FORTRAN_ENV, \
6661 symbol[i].id); \
6662 break;
6663 #include "iso-fortran-env.def"
6665 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6666 case a:
6667 #include "iso-fortran-env.def"
6668 create_derived_type (u->local_name[0] ? u->local_name
6669 : u->use_name,
6670 mod, INTMOD_ISO_FORTRAN_ENV,
6671 symbol[i].id);
6672 break;
6674 #define NAMED_FUNCTION(a,b,c,d) \
6675 case a:
6676 #include "iso-fortran-env.def"
6677 create_intrinsic_function (u->local_name[0] ? u->local_name
6678 : u->use_name,
6679 symbol[i].id, mod,
6680 INTMOD_ISO_FORTRAN_ENV, false,
6681 NULL);
6682 break;
6684 default:
6685 gcc_unreachable ();
6690 if (!found && !only_flag)
6692 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6693 continue;
6695 if ((flag_default_integer || flag_default_real)
6696 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6697 gfc_warning_now (0,
6698 "Use of the NUMERIC_STORAGE_SIZE named constant "
6699 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6700 "incompatible with option %s",
6701 flag_default_integer
6702 ? "-fdefault-integer-8" : "-fdefault-real-8");
6704 switch (symbol[i].id)
6706 #define NAMED_INTCST(a,b,c,d) \
6707 case a:
6708 #include "iso-fortran-env.def"
6709 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6710 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6711 break;
6713 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6714 case a:\
6715 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6716 NULL); \
6717 for (j = 0; KINDS[j].kind != 0; j++) \
6718 gfc_constructor_append_expr (&expr->value.constructor, \
6719 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6720 KINDS[j].kind), NULL); \
6721 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6722 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6723 break;
6724 #include "iso-fortran-env.def"
6726 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6727 case a:
6728 #include "iso-fortran-env.def"
6729 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6730 symbol[i].id);
6731 break;
6733 #define NAMED_FUNCTION(a,b,c,d) \
6734 case a:
6735 #include "iso-fortran-env.def"
6736 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6737 INTMOD_ISO_FORTRAN_ENV, false,
6738 NULL);
6739 break;
6741 default:
6742 gcc_unreachable ();
6747 for (u = gfc_rename_list; u; u = u->next)
6749 if (u->found)
6750 continue;
6752 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6753 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6758 /* Process a USE directive. */
6760 static void
6761 gfc_use_module (gfc_use_list *module)
6763 char *filename;
6764 gfc_state_data *p;
6765 int c, line, start;
6766 gfc_symtree *mod_symtree;
6767 gfc_use_list *use_stmt;
6768 locus old_locus = gfc_current_locus;
6770 gfc_current_locus = module->where;
6771 module_name = module->module_name;
6772 gfc_rename_list = module->rename;
6773 only_flag = module->only_flag;
6774 current_intmod = INTMOD_NONE;
6776 if (!only_flag)
6777 gfc_warning_now (OPT_Wuse_without_only,
6778 "USE statement at %C has no ONLY qualifier");
6780 if (gfc_state_stack->state == COMP_MODULE
6781 || module->submodule_name == NULL)
6783 filename = XALLOCAVEC (char, strlen (module_name)
6784 + strlen (MODULE_EXTENSION) + 1);
6785 strcpy (filename, module_name);
6786 strcat (filename, MODULE_EXTENSION);
6788 else
6790 filename = XALLOCAVEC (char, strlen (module->submodule_name)
6791 + strlen (SUBMODULE_EXTENSION) + 1);
6792 strcpy (filename, module->submodule_name);
6793 strcat (filename, SUBMODULE_EXTENSION);
6796 /* First, try to find an non-intrinsic module, unless the USE statement
6797 specified that the module is intrinsic. */
6798 module_fp = NULL;
6799 if (!module->intrinsic)
6800 module_fp = gzopen_included_file (filename, true, true);
6802 /* Then, see if it's an intrinsic one, unless the USE statement
6803 specified that the module is non-intrinsic. */
6804 if (module_fp == NULL && !module->non_intrinsic)
6806 if (strcmp (module_name, "iso_fortran_env") == 0
6807 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6808 "intrinsic module at %C"))
6810 use_iso_fortran_env_module ();
6811 free_rename (module->rename);
6812 module->rename = NULL;
6813 gfc_current_locus = old_locus;
6814 module->intrinsic = true;
6815 return;
6818 if (strcmp (module_name, "iso_c_binding") == 0
6819 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6821 import_iso_c_binding_module();
6822 free_rename (module->rename);
6823 module->rename = NULL;
6824 gfc_current_locus = old_locus;
6825 module->intrinsic = true;
6826 return;
6829 module_fp = gzopen_intrinsic_module (filename);
6831 if (module_fp == NULL && module->intrinsic)
6832 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6833 module_name);
6835 /* Check for the IEEE modules, so we can mark their symbols
6836 accordingly when we read them. */
6837 if (strcmp (module_name, "ieee_features") == 0
6838 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
6840 current_intmod = INTMOD_IEEE_FEATURES;
6842 else if (strcmp (module_name, "ieee_exceptions") == 0
6843 && gfc_notify_std (GFC_STD_F2003,
6844 "IEEE_EXCEPTIONS module at %C"))
6846 current_intmod = INTMOD_IEEE_EXCEPTIONS;
6848 else if (strcmp (module_name, "ieee_arithmetic") == 0
6849 && gfc_notify_std (GFC_STD_F2003,
6850 "IEEE_ARITHMETIC module at %C"))
6852 current_intmod = INTMOD_IEEE_ARITHMETIC;
6856 if (module_fp == NULL)
6857 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6858 filename, xstrerror (errno));
6860 /* Check that we haven't already USEd an intrinsic module with the
6861 same name. */
6863 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6864 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6865 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6866 "intrinsic module name used previously", module_name);
6868 iomode = IO_INPUT;
6869 module_line = 1;
6870 module_column = 1;
6871 start = 0;
6873 read_module_to_tmpbuf ();
6874 gzclose (module_fp);
6876 /* Skip the first line of the module, after checking that this is
6877 a gfortran module file. */
6878 line = 0;
6879 while (line < 1)
6881 c = module_char ();
6882 if (c == EOF)
6883 bad_module ("Unexpected end of module");
6884 if (start++ < 3)
6885 parse_name (c);
6886 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6887 || (start == 2 && strcmp (atom_name, " module") != 0))
6888 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6889 " module file", filename);
6890 if (start == 3)
6892 if (strcmp (atom_name, " version") != 0
6893 || module_char () != ' '
6894 || parse_atom () != ATOM_STRING
6895 || strcmp (atom_string, MOD_VERSION))
6896 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6897 " because it was created by a different"
6898 " version of GNU Fortran", filename);
6900 free (atom_string);
6903 if (c == '\n')
6904 line++;
6907 /* Make sure we're not reading the same module that we may be building. */
6908 for (p = gfc_state_stack; p; p = p->previous)
6909 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
6910 && strcmp (p->sym->name, module_name) == 0)
6911 gfc_fatal_error ("Can't USE the same %smodule we're building!",
6912 p->state == COMP_SUBMODULE ? "sub" : "");
6914 init_pi_tree ();
6915 init_true_name_tree ();
6917 read_module ();
6919 free_true_name (true_name_root);
6920 true_name_root = NULL;
6922 free_pi_tree (pi_root);
6923 pi_root = NULL;
6925 XDELETEVEC (module_content);
6926 module_content = NULL;
6928 use_stmt = gfc_get_use_list ();
6929 *use_stmt = *module;
6930 use_stmt->next = gfc_current_ns->use_stmts;
6931 gfc_current_ns->use_stmts = use_stmt;
6933 gfc_current_locus = old_locus;
6937 /* Remove duplicated intrinsic operators from the rename list. */
6939 static void
6940 rename_list_remove_duplicate (gfc_use_rename *list)
6942 gfc_use_rename *seek, *last;
6944 for (; list; list = list->next)
6945 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6947 last = list;
6948 for (seek = list->next; seek; seek = last->next)
6950 if (list->op == seek->op)
6952 last->next = seek->next;
6953 free (seek);
6955 else
6956 last = seek;
6962 /* Process all USE directives. */
6964 void
6965 gfc_use_modules (void)
6967 gfc_use_list *next, *seek, *last;
6969 for (next = module_list; next; next = next->next)
6971 bool non_intrinsic = next->non_intrinsic;
6972 bool intrinsic = next->intrinsic;
6973 bool neither = !non_intrinsic && !intrinsic;
6975 for (seek = next->next; seek; seek = seek->next)
6977 if (next->module_name != seek->module_name)
6978 continue;
6980 if (seek->non_intrinsic)
6981 non_intrinsic = true;
6982 else if (seek->intrinsic)
6983 intrinsic = true;
6984 else
6985 neither = true;
6988 if (intrinsic && neither && !non_intrinsic)
6990 char *filename;
6991 FILE *fp;
6993 filename = XALLOCAVEC (char,
6994 strlen (next->module_name)
6995 + strlen (MODULE_EXTENSION) + 1);
6996 strcpy (filename, next->module_name);
6997 strcat (filename, MODULE_EXTENSION);
6998 fp = gfc_open_included_file (filename, true, true);
6999 if (fp != NULL)
7001 non_intrinsic = true;
7002 fclose (fp);
7006 last = next;
7007 for (seek = next->next; seek; seek = last->next)
7009 if (next->module_name != seek->module_name)
7011 last = seek;
7012 continue;
7015 if ((!next->intrinsic && !seek->intrinsic)
7016 || (next->intrinsic && seek->intrinsic)
7017 || !non_intrinsic)
7019 if (!seek->only_flag)
7020 next->only_flag = false;
7021 if (seek->rename)
7023 gfc_use_rename *r = seek->rename;
7024 while (r->next)
7025 r = r->next;
7026 r->next = next->rename;
7027 next->rename = seek->rename;
7029 last->next = seek->next;
7030 free (seek);
7032 else
7033 last = seek;
7037 for (; module_list; module_list = next)
7039 next = module_list->next;
7040 rename_list_remove_duplicate (module_list->rename);
7041 gfc_use_module (module_list);
7042 free (module_list);
7044 gfc_rename_list = NULL;
7048 void
7049 gfc_free_use_stmts (gfc_use_list *use_stmts)
7051 gfc_use_list *next;
7052 for (; use_stmts; use_stmts = next)
7054 gfc_use_rename *next_rename;
7056 for (; use_stmts->rename; use_stmts->rename = next_rename)
7058 next_rename = use_stmts->rename->next;
7059 free (use_stmts->rename);
7061 next = use_stmts->next;
7062 free (use_stmts);
7067 void
7068 gfc_module_init_2 (void)
7070 last_atom = ATOM_LPAREN;
7071 gfc_rename_list = NULL;
7072 module_list = NULL;
7076 void
7077 gfc_module_done_2 (void)
7079 free_rename (gfc_rename_list);
7080 gfc_rename_list = NULL;