Fix compilation failure with C++98 compilers
[official-gcc.git] / gcc / fortran / module.c
blob7b8e863ca0a0e1b98f62e7a4795d53c70ce91aee
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2018 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 "options.h"
71 #include "tree.h"
72 #include "gfortran.h"
73 #include "stringpool.h"
74 #include "arith.h"
75 #include "match.h"
76 #include "parse.h" /* FIXME */
77 #include "constructor.h"
78 #include "cpp.h"
79 #include "scanner.h"
80 #include <zlib.h>
82 #define MODULE_EXTENSION ".mod"
83 #define SUBMODULE_EXTENSION ".smod"
85 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
86 recognized. */
87 #define MOD_VERSION "15"
90 /* Structure that describes a position within a module file. */
92 typedef struct
94 int column, line;
95 long pos;
97 module_locus;
99 /* Structure for list of symbols of intrinsic modules. */
100 typedef struct
102 int id;
103 const char *name;
104 int value;
105 int standard;
107 intmod_sym;
110 typedef enum
112 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
114 pointer_t;
116 /* The fixup structure lists pointers to pointers that have to
117 be updated when a pointer value becomes known. */
119 typedef struct fixup_t
121 void **pointer;
122 struct fixup_t *next;
124 fixup_t;
127 /* Structure for holding extra info needed for pointers being read. */
129 enum gfc_rsym_state
131 UNUSED,
132 NEEDED,
133 USED
136 enum gfc_wsym_state
138 UNREFERENCED = 0,
139 NEEDS_WRITE,
140 WRITTEN
143 typedef struct pointer_info
145 BBT_HEADER (pointer_info);
146 HOST_WIDE_INT integer;
147 pointer_t type;
149 /* The first component of each member of the union is the pointer
150 being stored. */
152 fixup_t *fixup;
154 union
156 void *pointer; /* Member for doing pointer searches. */
158 struct
160 gfc_symbol *sym;
161 char *true_name, *module, *binding_label;
162 fixup_t *stfixup;
163 gfc_symtree *symtree;
164 enum gfc_rsym_state state;
165 int ns, referenced, renamed;
166 module_locus where;
168 rsym;
170 struct
172 gfc_symbol *sym;
173 enum gfc_wsym_state state;
175 wsym;
180 pointer_info;
182 #define gfc_get_pointer_info() XCNEW (pointer_info)
185 /* Local variables */
187 /* The gzFile for the module we're reading or writing. */
188 static gzFile module_fp;
191 /* The name of the module we're reading (USE'ing) or writing. */
192 static const char *module_name;
193 /* The name of the .smod file that the submodule will write to. */
194 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 (HOST_WIDE_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 const char *
422 gfc_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 ("%s", 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 const char *
437 gfc_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 ("%s", 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 (HOST_WIDE_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 ("%s", 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 case INTERFACE_DTIO:
635 m = gfc_match (" =>");
637 if (type == INTERFACE_USER_OP && m == MATCH_YES
638 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
639 "operators in USE statements at %C")))
640 goto cleanup;
642 if (type == INTERFACE_USER_OP)
643 new_use->op = INTRINSIC_USER;
645 if (use_list->only_flag)
647 if (m != MATCH_YES)
648 strcpy (new_use->use_name, name);
649 else
651 strcpy (new_use->local_name, name);
652 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
653 if (type != type2)
654 goto syntax;
655 if (m == MATCH_NO)
656 goto syntax;
657 if (m == MATCH_ERROR)
658 goto cleanup;
661 else
663 if (m != MATCH_YES)
664 goto syntax;
665 strcpy (new_use->local_name, name);
667 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
668 if (type != type2)
669 goto syntax;
670 if (m == MATCH_NO)
671 goto syntax;
672 if (m == MATCH_ERROR)
673 goto cleanup;
676 if (strcmp (new_use->use_name, use_list->module_name) == 0
677 || strcmp (new_use->local_name, use_list->module_name) == 0)
679 gfc_error ("The name %qs at %C has already been used as "
680 "an external module name", use_list->module_name);
681 goto cleanup;
683 break;
685 case INTERFACE_INTRINSIC_OP:
686 new_use->op = op;
687 break;
689 default:
690 gcc_unreachable ();
693 if (gfc_match_eos () == MATCH_YES)
694 break;
695 if (gfc_match_char (',') != MATCH_YES)
696 goto syntax;
699 done:
700 if (module_list)
702 gfc_use_list *last = module_list;
703 while (last->next)
704 last = last->next;
705 last->next = use_list;
707 else
708 module_list = use_list;
710 return MATCH_YES;
712 syntax:
713 gfc_syntax_error (ST_USE);
715 cleanup:
716 free_rename (use_list->rename);
717 free (use_list);
718 return MATCH_ERROR;
722 /* Match a SUBMODULE statement.
724 According to F2008:11.2.3.2, "The submodule identifier is the
725 ordered pair whose first element is the ancestor module name and
726 whose second element is the submodule name. 'Submodule_name' is
727 used for the submodule filename and uses '@' as a separator, whilst
728 the name of the symbol for the module uses '.' as a a separator.
729 The reasons for these choices are:
730 (i) To follow another leading brand in the submodule filenames;
731 (ii) Since '.' is not particularly visible in the filenames; and
732 (iii) The linker does not permit '@' in mnemonics. */
734 match
735 gfc_match_submodule (void)
737 match m;
738 char name[GFC_MAX_SYMBOL_LEN + 1];
739 gfc_use_list *use_list;
740 bool seen_colon = false;
742 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
743 return MATCH_ERROR;
745 if (gfc_current_state () != COMP_NONE)
747 gfc_error ("SUBMODULE declaration at %C cannot appear within "
748 "another scoping unit");
749 return MATCH_ERROR;
752 gfc_new_block = NULL;
753 gcc_assert (module_list == NULL);
755 if (gfc_match_char ('(') != MATCH_YES)
756 goto syntax;
758 while (1)
760 m = gfc_match (" %n", name);
761 if (m != MATCH_YES)
762 goto syntax;
764 use_list = gfc_get_use_list ();
765 use_list->where = gfc_current_locus;
767 if (module_list)
769 gfc_use_list *last = module_list;
770 while (last->next)
771 last = last->next;
772 last->next = use_list;
773 use_list->module_name
774 = gfc_get_string ("%s.%s", module_list->module_name, name);
775 use_list->submodule_name
776 = gfc_get_string ("%s@%s", module_list->module_name, name);
778 else
780 module_list = use_list;
781 use_list->module_name = gfc_get_string ("%s", name);
782 use_list->submodule_name = use_list->module_name;
785 if (gfc_match_char (')') == MATCH_YES)
786 break;
788 if (gfc_match_char (':') != MATCH_YES
789 || seen_colon)
790 goto syntax;
792 seen_colon = true;
795 m = gfc_match (" %s%t", &gfc_new_block);
796 if (m != MATCH_YES)
797 goto syntax;
799 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
800 gfc_new_block->name);
802 gfc_new_block->name = gfc_get_string ("%s.%s",
803 module_list->module_name,
804 gfc_new_block->name);
806 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
807 gfc_new_block->name, NULL))
808 return MATCH_ERROR;
810 /* Just retain the ultimate .(s)mod file for reading, since it
811 contains all the information in its ancestors. */
812 use_list = module_list;
813 for (; module_list->next; use_list = module_list)
815 module_list = use_list->next;
816 free (use_list);
819 return MATCH_YES;
821 syntax:
822 gfc_error ("Syntax error in SUBMODULE statement at %C");
823 return MATCH_ERROR;
827 /* Given a name and a number, inst, return the inst name
828 under which to load this symbol. Returns NULL if this
829 symbol shouldn't be loaded. If inst is zero, returns
830 the number of instances of this name. If interface is
831 true, a user-defined operator is sought, otherwise only
832 non-operators are sought. */
834 static const char *
835 find_use_name_n (const char *name, int *inst, bool interface)
837 gfc_use_rename *u;
838 const char *low_name = NULL;
839 int i;
841 /* For derived types. */
842 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
843 low_name = gfc_dt_lower_string (name);
845 i = 0;
846 for (u = gfc_rename_list; u; u = u->next)
848 if ((!low_name && strcmp (u->use_name, name) != 0)
849 || (low_name && strcmp (u->use_name, low_name) != 0)
850 || (u->op == INTRINSIC_USER && !interface)
851 || (u->op != INTRINSIC_USER && interface))
852 continue;
853 if (++i == *inst)
854 break;
857 if (!*inst)
859 *inst = i;
860 return NULL;
863 if (u == NULL)
864 return only_flag ? NULL : name;
866 u->found = 1;
868 if (low_name)
870 if (u->local_name[0] == '\0')
871 return name;
872 return gfc_dt_upper_string (u->local_name);
875 return (u->local_name[0] != '\0') ? u->local_name : name;
879 /* Given a name, return the name under which to load this symbol.
880 Returns NULL if this symbol shouldn't be loaded. */
882 static const char *
883 find_use_name (const char *name, bool interface)
885 int i = 1;
886 return find_use_name_n (name, &i, interface);
890 /* Given a real name, return the number of use names associated with it. */
892 static int
893 number_use_names (const char *name, bool interface)
895 int i = 0;
896 find_use_name_n (name, &i, interface);
897 return i;
901 /* Try to find the operator in the current list. */
903 static gfc_use_rename *
904 find_use_operator (gfc_intrinsic_op op)
906 gfc_use_rename *u;
908 for (u = gfc_rename_list; u; u = u->next)
909 if (u->op == op)
910 return u;
912 return NULL;
916 /*****************************************************************/
918 /* The next couple of subroutines maintain a tree used to avoid a
919 brute-force search for a combination of true name and module name.
920 While symtree names, the name that a particular symbol is known by
921 can changed with USE statements, we still have to keep track of the
922 true names to generate the correct reference, and also avoid
923 loading the same real symbol twice in a program unit.
925 When we start reading, the true name tree is built and maintained
926 as symbols are read. The tree is searched as we load new symbols
927 to see if it already exists someplace in the namespace. */
929 typedef struct true_name
931 BBT_HEADER (true_name);
932 const char *name;
933 gfc_symbol *sym;
935 true_name;
937 static true_name *true_name_root;
940 /* Compare two true_name structures. */
942 static int
943 compare_true_names (void *_t1, void *_t2)
945 true_name *t1, *t2;
946 int c;
948 t1 = (true_name *) _t1;
949 t2 = (true_name *) _t2;
951 c = ((t1->sym->module > t2->sym->module)
952 - (t1->sym->module < t2->sym->module));
953 if (c != 0)
954 return c;
956 return strcmp (t1->name, t2->name);
960 /* Given a true name, search the true name tree to see if it exists
961 within the main namespace. */
963 static gfc_symbol *
964 find_true_name (const char *name, const char *module)
966 true_name t, *p;
967 gfc_symbol sym;
968 int c;
970 t.name = gfc_get_string ("%s", name);
971 if (module != NULL)
972 sym.module = gfc_get_string ("%s", module);
973 else
974 sym.module = NULL;
975 t.sym = &sym;
977 p = true_name_root;
978 while (p != NULL)
980 c = compare_true_names ((void *) (&t), (void *) p);
981 if (c == 0)
982 return p->sym;
984 p = (c < 0) ? p->left : p->right;
987 return NULL;
991 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
993 static void
994 add_true_name (gfc_symbol *sym)
996 true_name *t;
998 t = XCNEW (true_name);
999 t->sym = sym;
1000 if (gfc_fl_struct (sym->attr.flavor))
1001 t->name = gfc_dt_upper_string (sym->name);
1002 else
1003 t->name = sym->name;
1005 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1009 /* Recursive function to build the initial true name tree by
1010 recursively traversing the current namespace. */
1012 static void
1013 build_tnt (gfc_symtree *st)
1015 const char *name;
1016 if (st == NULL)
1017 return;
1019 build_tnt (st->left);
1020 build_tnt (st->right);
1022 if (gfc_fl_struct (st->n.sym->attr.flavor))
1023 name = gfc_dt_upper_string (st->n.sym->name);
1024 else
1025 name = st->n.sym->name;
1027 if (find_true_name (name, st->n.sym->module) != NULL)
1028 return;
1030 add_true_name (st->n.sym);
1034 /* Initialize the true name tree with the current namespace. */
1036 static void
1037 init_true_name_tree (void)
1039 true_name_root = NULL;
1040 build_tnt (gfc_current_ns->sym_root);
1044 /* Recursively free a true name tree node. */
1046 static void
1047 free_true_name (true_name *t)
1049 if (t == NULL)
1050 return;
1051 free_true_name (t->left);
1052 free_true_name (t->right);
1054 free (t);
1058 /*****************************************************************/
1060 /* Module reading and writing. */
1062 /* The following are versions similar to the ones in scanner.c, but
1063 for dealing with compressed module files. */
1065 static gzFile
1066 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1067 bool module, bool system)
1069 char *fullname;
1070 gfc_directorylist *p;
1071 gzFile f;
1073 for (p = list; p; p = p->next)
1075 if (module && !p->use_for_modules)
1076 continue;
1078 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
1079 strcpy (fullname, p->path);
1080 strcat (fullname, name);
1082 f = gzopen (fullname, "r");
1083 if (f != NULL)
1085 if (gfc_cpp_makedep ())
1086 gfc_cpp_add_dep (fullname, system);
1088 return f;
1092 return NULL;
1095 static gzFile
1096 gzopen_included_file (const char *name, bool include_cwd, bool module)
1098 gzFile f = NULL;
1100 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1102 f = gzopen (name, "r");
1103 if (f && gfc_cpp_makedep ())
1104 gfc_cpp_add_dep (name, false);
1107 if (!f)
1108 f = gzopen_included_file_1 (name, include_dirs, module, false);
1110 return f;
1113 static gzFile
1114 gzopen_intrinsic_module (const char* name)
1116 gzFile f = NULL;
1118 if (IS_ABSOLUTE_PATH (name))
1120 f = gzopen (name, "r");
1121 if (f && gfc_cpp_makedep ())
1122 gfc_cpp_add_dep (name, true);
1125 if (!f)
1126 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1128 return f;
1132 enum atom_type
1134 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1137 static atom_type last_atom;
1140 /* The name buffer must be at least as long as a symbol name. Right
1141 now it's not clear how we're going to store numeric constants--
1142 probably as a hexadecimal string, since this will allow the exact
1143 number to be preserved (this can't be done by a decimal
1144 representation). Worry about that later. TODO! */
1146 #define MAX_ATOM_SIZE 100
1148 static HOST_WIDE_INT atom_int;
1149 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1152 /* Report problems with a module. Error reporting is not very
1153 elaborate, since this sorts of errors shouldn't really happen.
1154 This subroutine never returns. */
1156 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1158 static void
1159 bad_module (const char *msgid)
1161 XDELETEVEC (module_content);
1162 module_content = NULL;
1164 switch (iomode)
1166 case IO_INPUT:
1167 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1168 module_name, module_line, module_column, msgid);
1169 break;
1170 case IO_OUTPUT:
1171 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1172 module_name, module_line, module_column, msgid);
1173 break;
1174 default:
1175 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1176 module_name, module_line, module_column, msgid);
1177 break;
1182 /* Set the module's input pointer. */
1184 static void
1185 set_module_locus (module_locus *m)
1187 module_column = m->column;
1188 module_line = m->line;
1189 module_pos = m->pos;
1193 /* Get the module's input pointer so that we can restore it later. */
1195 static void
1196 get_module_locus (module_locus *m)
1198 m->column = module_column;
1199 m->line = module_line;
1200 m->pos = module_pos;
1204 /* Get the next character in the module, updating our reckoning of
1205 where we are. */
1207 static int
1208 module_char (void)
1210 const char c = module_content[module_pos++];
1211 if (c == '\0')
1212 bad_module ("Unexpected EOF");
1214 prev_module_line = module_line;
1215 prev_module_column = module_column;
1217 if (c == '\n')
1219 module_line++;
1220 module_column = 0;
1223 module_column++;
1224 return c;
1227 /* Unget a character while remembering the line and column. Works for
1228 a single character only. */
1230 static void
1231 module_unget_char (void)
1233 module_line = prev_module_line;
1234 module_column = prev_module_column;
1235 module_pos--;
1238 /* Parse a string constant. The delimiter is guaranteed to be a
1239 single quote. */
1241 static void
1242 parse_string (void)
1244 int c;
1245 size_t cursz = 30;
1246 size_t len = 0;
1248 atom_string = XNEWVEC (char, cursz);
1250 for ( ; ; )
1252 c = module_char ();
1254 if (c == '\'')
1256 int c2 = module_char ();
1257 if (c2 != '\'')
1259 module_unget_char ();
1260 break;
1264 if (len >= cursz)
1266 cursz *= 2;
1267 atom_string = XRESIZEVEC (char, atom_string, cursz);
1269 atom_string[len] = c;
1270 len++;
1273 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1274 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1278 /* Parse an integer. Should fit in a HOST_WIDE_INT. */
1280 static void
1281 parse_integer (int c)
1283 atom_int = c - '0';
1285 for (;;)
1287 c = module_char ();
1288 if (!ISDIGIT (c))
1290 module_unget_char ();
1291 break;
1294 atom_int = 10 * atom_int + c - '0';
1300 /* Parse a name. */
1302 static void
1303 parse_name (int c)
1305 char *p;
1306 int len;
1308 p = atom_name;
1310 *p++ = c;
1311 len = 1;
1313 for (;;)
1315 c = module_char ();
1316 if (!ISALNUM (c) && c != '_' && c != '-')
1318 module_unget_char ();
1319 break;
1322 *p++ = c;
1323 if (++len > GFC_MAX_SYMBOL_LEN)
1324 bad_module ("Name too long");
1327 *p = '\0';
1332 /* Read the next atom in the module's input stream. */
1334 static atom_type
1335 parse_atom (void)
1337 int c;
1341 c = module_char ();
1343 while (c == ' ' || c == '\r' || c == '\n');
1345 switch (c)
1347 case '(':
1348 return ATOM_LPAREN;
1350 case ')':
1351 return ATOM_RPAREN;
1353 case '\'':
1354 parse_string ();
1355 return ATOM_STRING;
1357 case '0':
1358 case '1':
1359 case '2':
1360 case '3':
1361 case '4':
1362 case '5':
1363 case '6':
1364 case '7':
1365 case '8':
1366 case '9':
1367 parse_integer (c);
1368 return ATOM_INTEGER;
1370 case 'a':
1371 case 'b':
1372 case 'c':
1373 case 'd':
1374 case 'e':
1375 case 'f':
1376 case 'g':
1377 case 'h':
1378 case 'i':
1379 case 'j':
1380 case 'k':
1381 case 'l':
1382 case 'm':
1383 case 'n':
1384 case 'o':
1385 case 'p':
1386 case 'q':
1387 case 'r':
1388 case 's':
1389 case 't':
1390 case 'u':
1391 case 'v':
1392 case 'w':
1393 case 'x':
1394 case 'y':
1395 case 'z':
1396 case 'A':
1397 case 'B':
1398 case 'C':
1399 case 'D':
1400 case 'E':
1401 case 'F':
1402 case 'G':
1403 case 'H':
1404 case 'I':
1405 case 'J':
1406 case 'K':
1407 case 'L':
1408 case 'M':
1409 case 'N':
1410 case 'O':
1411 case 'P':
1412 case 'Q':
1413 case 'R':
1414 case 'S':
1415 case 'T':
1416 case 'U':
1417 case 'V':
1418 case 'W':
1419 case 'X':
1420 case 'Y':
1421 case 'Z':
1422 parse_name (c);
1423 return ATOM_NAME;
1425 default:
1426 bad_module ("Bad name");
1429 /* Not reached. */
1433 /* Peek at the next atom on the input. */
1435 static atom_type
1436 peek_atom (void)
1438 int c;
1442 c = module_char ();
1444 while (c == ' ' || c == '\r' || c == '\n');
1446 switch (c)
1448 case '(':
1449 module_unget_char ();
1450 return ATOM_LPAREN;
1452 case ')':
1453 module_unget_char ();
1454 return ATOM_RPAREN;
1456 case '\'':
1457 module_unget_char ();
1458 return ATOM_STRING;
1460 case '0':
1461 case '1':
1462 case '2':
1463 case '3':
1464 case '4':
1465 case '5':
1466 case '6':
1467 case '7':
1468 case '8':
1469 case '9':
1470 module_unget_char ();
1471 return ATOM_INTEGER;
1473 case 'a':
1474 case 'b':
1475 case 'c':
1476 case 'd':
1477 case 'e':
1478 case 'f':
1479 case 'g':
1480 case 'h':
1481 case 'i':
1482 case 'j':
1483 case 'k':
1484 case 'l':
1485 case 'm':
1486 case 'n':
1487 case 'o':
1488 case 'p':
1489 case 'q':
1490 case 'r':
1491 case 's':
1492 case 't':
1493 case 'u':
1494 case 'v':
1495 case 'w':
1496 case 'x':
1497 case 'y':
1498 case 'z':
1499 case 'A':
1500 case 'B':
1501 case 'C':
1502 case 'D':
1503 case 'E':
1504 case 'F':
1505 case 'G':
1506 case 'H':
1507 case 'I':
1508 case 'J':
1509 case 'K':
1510 case 'L':
1511 case 'M':
1512 case 'N':
1513 case 'O':
1514 case 'P':
1515 case 'Q':
1516 case 'R':
1517 case 'S':
1518 case 'T':
1519 case 'U':
1520 case 'V':
1521 case 'W':
1522 case 'X':
1523 case 'Y':
1524 case 'Z':
1525 module_unget_char ();
1526 return ATOM_NAME;
1528 default:
1529 bad_module ("Bad name");
1534 /* Read the next atom from the input, requiring that it be a
1535 particular kind. */
1537 static void
1538 require_atom (atom_type type)
1540 atom_type t;
1541 const char *p;
1542 int column, line;
1544 column = module_column;
1545 line = module_line;
1547 t = parse_atom ();
1548 if (t != type)
1550 switch (type)
1552 case ATOM_NAME:
1553 p = _("Expected name");
1554 break;
1555 case ATOM_LPAREN:
1556 p = _("Expected left parenthesis");
1557 break;
1558 case ATOM_RPAREN:
1559 p = _("Expected right parenthesis");
1560 break;
1561 case ATOM_INTEGER:
1562 p = _("Expected integer");
1563 break;
1564 case ATOM_STRING:
1565 p = _("Expected string");
1566 break;
1567 default:
1568 gfc_internal_error ("require_atom(): bad atom type required");
1571 module_column = column;
1572 module_line = line;
1573 bad_module (p);
1578 /* Given a pointer to an mstring array, require that the current input
1579 be one of the strings in the array. We return the enum value. */
1581 static int
1582 find_enum (const mstring *m)
1584 int i;
1586 i = gfc_string2code (m, atom_name);
1587 if (i >= 0)
1588 return i;
1590 bad_module ("find_enum(): Enum not found");
1592 /* Not reached. */
1596 /* Read a string. The caller is responsible for freeing. */
1598 static char*
1599 read_string (void)
1601 char* p;
1602 require_atom (ATOM_STRING);
1603 p = atom_string;
1604 atom_string = NULL;
1605 return p;
1609 /**************** Module output subroutines ***************************/
1611 /* Output a character to a module file. */
1613 static void
1614 write_char (char out)
1616 if (gzputc (module_fp, out) == EOF)
1617 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1619 if (out != '\n')
1620 module_column++;
1621 else
1623 module_column = 1;
1624 module_line++;
1629 /* Write an atom to a module. The line wrapping isn't perfect, but it
1630 should work most of the time. This isn't that big of a deal, since
1631 the file really isn't meant to be read by people anyway. */
1633 static void
1634 write_atom (atom_type atom, const void *v)
1636 char buffer[32];
1638 /* Workaround -Wmaybe-uninitialized false positive during
1639 profiledbootstrap by initializing them. */
1640 int len;
1641 HOST_WIDE_INT i = 0;
1642 const char *p;
1644 switch (atom)
1646 case ATOM_STRING:
1647 case ATOM_NAME:
1648 p = (const char *) v;
1649 break;
1651 case ATOM_LPAREN:
1652 p = "(";
1653 break;
1655 case ATOM_RPAREN:
1656 p = ")";
1657 break;
1659 case ATOM_INTEGER:
1660 i = *((const HOST_WIDE_INT *) v);
1662 snprintf (buffer, sizeof (buffer), HOST_WIDE_INT_PRINT_DEC, i);
1663 p = buffer;
1664 break;
1666 default:
1667 gfc_internal_error ("write_atom(): Trying to write dab atom");
1671 if(p == NULL || *p == '\0')
1672 len = 0;
1673 else
1674 len = strlen (p);
1676 if (atom != ATOM_RPAREN)
1678 if (module_column + len > 72)
1679 write_char ('\n');
1680 else
1683 if (last_atom != ATOM_LPAREN && module_column != 1)
1684 write_char (' ');
1688 if (atom == ATOM_STRING)
1689 write_char ('\'');
1691 while (p != NULL && *p)
1693 if (atom == ATOM_STRING && *p == '\'')
1694 write_char ('\'');
1695 write_char (*p++);
1698 if (atom == ATOM_STRING)
1699 write_char ('\'');
1701 last_atom = atom;
1706 /***************** Mid-level I/O subroutines *****************/
1708 /* These subroutines let their caller read or write atoms without
1709 caring about which of the two is actually happening. This lets a
1710 subroutine concentrate on the actual format of the data being
1711 written. */
1713 static void mio_expr (gfc_expr **);
1714 pointer_info *mio_symbol_ref (gfc_symbol **);
1715 pointer_info *mio_interface_rest (gfc_interface **);
1716 static void mio_symtree_ref (gfc_symtree **);
1718 /* Read or write an enumerated value. On writing, we return the input
1719 value for the convenience of callers. We avoid using an integer
1720 pointer because enums are sometimes inside bitfields. */
1722 static int
1723 mio_name (int t, const mstring *m)
1725 if (iomode == IO_OUTPUT)
1726 write_atom (ATOM_NAME, gfc_code2string (m, t));
1727 else
1729 require_atom (ATOM_NAME);
1730 t = find_enum (m);
1733 return t;
1736 /* Specialization of mio_name. */
1738 #define DECL_MIO_NAME(TYPE) \
1739 static inline TYPE \
1740 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1742 return (TYPE) mio_name ((int) t, m); \
1744 #define MIO_NAME(TYPE) mio_name_##TYPE
1746 static void
1747 mio_lparen (void)
1749 if (iomode == IO_OUTPUT)
1750 write_atom (ATOM_LPAREN, NULL);
1751 else
1752 require_atom (ATOM_LPAREN);
1756 static void
1757 mio_rparen (void)
1759 if (iomode == IO_OUTPUT)
1760 write_atom (ATOM_RPAREN, NULL);
1761 else
1762 require_atom (ATOM_RPAREN);
1766 static void
1767 mio_integer (int *ip)
1769 if (iomode == IO_OUTPUT)
1771 HOST_WIDE_INT hwi = *ip;
1772 write_atom (ATOM_INTEGER, &hwi);
1774 else
1776 require_atom (ATOM_INTEGER);
1777 *ip = atom_int;
1781 static void
1782 mio_hwi (HOST_WIDE_INT *hwi)
1784 if (iomode == IO_OUTPUT)
1785 write_atom (ATOM_INTEGER, hwi);
1786 else
1788 require_atom (ATOM_INTEGER);
1789 *hwi = atom_int;
1794 /* Read or write a gfc_intrinsic_op value. */
1796 static void
1797 mio_intrinsic_op (gfc_intrinsic_op* op)
1799 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1800 if (iomode == IO_OUTPUT)
1802 HOST_WIDE_INT converted = (HOST_WIDE_INT) *op;
1803 write_atom (ATOM_INTEGER, &converted);
1805 else
1807 require_atom (ATOM_INTEGER);
1808 *op = (gfc_intrinsic_op) atom_int;
1813 /* Read or write a character pointer that points to a string on the heap. */
1815 static const char *
1816 mio_allocated_string (const char *s)
1818 if (iomode == IO_OUTPUT)
1820 write_atom (ATOM_STRING, s);
1821 return s;
1823 else
1825 require_atom (ATOM_STRING);
1826 return atom_string;
1831 /* Functions for quoting and unquoting strings. */
1833 static char *
1834 quote_string (const gfc_char_t *s, const size_t slength)
1836 const gfc_char_t *p;
1837 char *res, *q;
1838 size_t len = 0, i;
1840 /* Calculate the length we'll need: a backslash takes two ("\\"),
1841 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1842 for (p = s, i = 0; i < slength; p++, i++)
1844 if (*p == '\\')
1845 len += 2;
1846 else if (!gfc_wide_is_printable (*p))
1847 len += 10;
1848 else
1849 len++;
1852 q = res = XCNEWVEC (char, len + 1);
1853 for (p = s, i = 0; i < slength; p++, i++)
1855 if (*p == '\\')
1856 *q++ = '\\', *q++ = '\\';
1857 else if (!gfc_wide_is_printable (*p))
1859 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1860 (unsigned HOST_WIDE_INT) *p);
1861 q += 10;
1863 else
1864 *q++ = (unsigned char) *p;
1867 res[len] = '\0';
1868 return res;
1871 static gfc_char_t *
1872 unquote_string (const char *s)
1874 size_t len, i;
1875 const char *p;
1876 gfc_char_t *res;
1878 for (p = s, len = 0; *p; p++, len++)
1880 if (*p != '\\')
1881 continue;
1883 if (p[1] == '\\')
1884 p++;
1885 else if (p[1] == 'U')
1886 p += 9; /* That is a "\U????????". */
1887 else
1888 gfc_internal_error ("unquote_string(): got bad string");
1891 res = gfc_get_wide_string (len + 1);
1892 for (i = 0, p = s; i < len; i++, p++)
1894 gcc_assert (*p);
1896 if (*p != '\\')
1897 res[i] = (unsigned char) *p;
1898 else if (p[1] == '\\')
1900 res[i] = (unsigned char) '\\';
1901 p++;
1903 else
1905 /* We read the 8-digits hexadecimal constant that follows. */
1906 int j;
1907 unsigned n;
1908 gfc_char_t c = 0;
1910 gcc_assert (p[1] == 'U');
1911 for (j = 0; j < 8; j++)
1913 c = c << 4;
1914 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1915 c += n;
1918 res[i] = c;
1919 p += 9;
1923 res[len] = '\0';
1924 return res;
1928 /* Read or write a character pointer that points to a wide string on the
1929 heap, performing quoting/unquoting of nonprintable characters using the
1930 form \U???????? (where each ? is a hexadecimal digit).
1931 Length is the length of the string, only known and used in output mode. */
1933 static const gfc_char_t *
1934 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1936 if (iomode == IO_OUTPUT)
1938 char *quoted = quote_string (s, length);
1939 write_atom (ATOM_STRING, quoted);
1940 free (quoted);
1941 return s;
1943 else
1945 gfc_char_t *unquoted;
1947 require_atom (ATOM_STRING);
1948 unquoted = unquote_string (atom_string);
1949 free (atom_string);
1950 return unquoted;
1955 /* Read or write a string that is in static memory. */
1957 static void
1958 mio_pool_string (const char **stringp)
1960 /* TODO: one could write the string only once, and refer to it via a
1961 fixup pointer. */
1963 /* As a special case we have to deal with a NULL string. This
1964 happens for the 'module' member of 'gfc_symbol's that are not in a
1965 module. We read / write these as the empty string. */
1966 if (iomode == IO_OUTPUT)
1968 const char *p = *stringp == NULL ? "" : *stringp;
1969 write_atom (ATOM_STRING, p);
1971 else
1973 require_atom (ATOM_STRING);
1974 *stringp = (atom_string[0] == '\0'
1975 ? NULL : gfc_get_string ("%s", atom_string));
1976 free (atom_string);
1981 /* Read or write a string that is inside of some already-allocated
1982 structure. */
1984 static void
1985 mio_internal_string (char *string)
1987 if (iomode == IO_OUTPUT)
1988 write_atom (ATOM_STRING, string);
1989 else
1991 require_atom (ATOM_STRING);
1992 strcpy (string, atom_string);
1993 free (atom_string);
1998 enum ab_attribute
1999 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
2000 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
2001 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
2002 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
2003 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
2004 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
2005 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
2006 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
2007 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
2008 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
2009 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
2010 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
2011 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
2012 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK,
2013 AB_OMP_DECLARE_TARGET_LINK, AB_PDT_KIND, AB_PDT_LEN, AB_PDT_TYPE,
2014 AB_PDT_TEMPLATE, AB_PDT_ARRAY, AB_PDT_STRING
2017 static const mstring attr_bits[] =
2019 minit ("ALLOCATABLE", AB_ALLOCATABLE),
2020 minit ("ARTIFICIAL", AB_ARTIFICIAL),
2021 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
2022 minit ("DIMENSION", AB_DIMENSION),
2023 minit ("CODIMENSION", AB_CODIMENSION),
2024 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2025 minit ("EXTERNAL", AB_EXTERNAL),
2026 minit ("INTRINSIC", AB_INTRINSIC),
2027 minit ("OPTIONAL", AB_OPTIONAL),
2028 minit ("POINTER", AB_POINTER),
2029 minit ("VOLATILE", AB_VOLATILE),
2030 minit ("TARGET", AB_TARGET),
2031 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2032 minit ("DUMMY", AB_DUMMY),
2033 minit ("RESULT", AB_RESULT),
2034 minit ("DATA", AB_DATA),
2035 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2036 minit ("IN_COMMON", AB_IN_COMMON),
2037 minit ("FUNCTION", AB_FUNCTION),
2038 minit ("SUBROUTINE", AB_SUBROUTINE),
2039 minit ("SEQUENCE", AB_SEQUENCE),
2040 minit ("ELEMENTAL", AB_ELEMENTAL),
2041 minit ("PURE", AB_PURE),
2042 minit ("RECURSIVE", AB_RECURSIVE),
2043 minit ("GENERIC", AB_GENERIC),
2044 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2045 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2046 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2047 minit ("IS_BIND_C", AB_IS_BIND_C),
2048 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2049 minit ("IS_ISO_C", AB_IS_ISO_C),
2050 minit ("VALUE", AB_VALUE),
2051 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2052 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2053 minit ("LOCK_COMP", AB_LOCK_COMP),
2054 minit ("EVENT_COMP", AB_EVENT_COMP),
2055 minit ("POINTER_COMP", AB_POINTER_COMP),
2056 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2057 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2058 minit ("ZERO_COMP", AB_ZERO_COMP),
2059 minit ("PROTECTED", AB_PROTECTED),
2060 minit ("ABSTRACT", AB_ABSTRACT),
2061 minit ("IS_CLASS", AB_IS_CLASS),
2062 minit ("PROCEDURE", AB_PROCEDURE),
2063 minit ("PROC_POINTER", AB_PROC_POINTER),
2064 minit ("VTYPE", AB_VTYPE),
2065 minit ("VTAB", AB_VTAB),
2066 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2067 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2068 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2069 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2070 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2071 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2072 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2073 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2074 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2075 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2076 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2077 minit ("OMP_DECLARE_TARGET_LINK", AB_OMP_DECLARE_TARGET_LINK),
2078 minit ("PDT_KIND", AB_PDT_KIND),
2079 minit ("PDT_LEN", AB_PDT_LEN),
2080 minit ("PDT_TYPE", AB_PDT_TYPE),
2081 minit ("PDT_TEMPLATE", AB_PDT_TEMPLATE),
2082 minit ("PDT_ARRAY", AB_PDT_ARRAY),
2083 minit ("PDT_STRING", AB_PDT_STRING),
2084 minit (NULL, -1)
2087 /* For binding attributes. */
2088 static const mstring binding_passing[] =
2090 minit ("PASS", 0),
2091 minit ("NOPASS", 1),
2092 minit (NULL, -1)
2094 static const mstring binding_overriding[] =
2096 minit ("OVERRIDABLE", 0),
2097 minit ("NON_OVERRIDABLE", 1),
2098 minit ("DEFERRED", 2),
2099 minit (NULL, -1)
2101 static const mstring binding_generic[] =
2103 minit ("SPECIFIC", 0),
2104 minit ("GENERIC", 1),
2105 minit (NULL, -1)
2107 static const mstring binding_ppc[] =
2109 minit ("NO_PPC", 0),
2110 minit ("PPC", 1),
2111 minit (NULL, -1)
2114 /* Specialization of mio_name. */
2115 DECL_MIO_NAME (ab_attribute)
2116 DECL_MIO_NAME (ar_type)
2117 DECL_MIO_NAME (array_type)
2118 DECL_MIO_NAME (bt)
2119 DECL_MIO_NAME (expr_t)
2120 DECL_MIO_NAME (gfc_access)
2121 DECL_MIO_NAME (gfc_intrinsic_op)
2122 DECL_MIO_NAME (ifsrc)
2123 DECL_MIO_NAME (save_state)
2124 DECL_MIO_NAME (procedure_type)
2125 DECL_MIO_NAME (ref_type)
2126 DECL_MIO_NAME (sym_flavor)
2127 DECL_MIO_NAME (sym_intent)
2128 #undef DECL_MIO_NAME
2130 /* Symbol attributes are stored in list with the first three elements
2131 being the enumerated fields, while the remaining elements (if any)
2132 indicate the individual attribute bits. The access field is not
2133 saved-- it controls what symbols are exported when a module is
2134 written. */
2136 static void
2137 mio_symbol_attribute (symbol_attribute *attr)
2139 atom_type t;
2140 unsigned ext_attr,extension_level;
2142 mio_lparen ();
2144 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2145 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2146 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2147 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2148 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2150 ext_attr = attr->ext_attr;
2151 mio_integer ((int *) &ext_attr);
2152 attr->ext_attr = ext_attr;
2154 extension_level = attr->extension;
2155 mio_integer ((int *) &extension_level);
2156 attr->extension = extension_level;
2158 if (iomode == IO_OUTPUT)
2160 if (attr->allocatable)
2161 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2162 if (attr->artificial)
2163 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2164 if (attr->asynchronous)
2165 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2166 if (attr->dimension)
2167 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2168 if (attr->codimension)
2169 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2170 if (attr->contiguous)
2171 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2172 if (attr->external)
2173 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2174 if (attr->intrinsic)
2175 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2176 if (attr->optional)
2177 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2178 if (attr->pointer)
2179 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2180 if (attr->class_pointer)
2181 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2182 if (attr->is_protected)
2183 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2184 if (attr->value)
2185 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2186 if (attr->volatile_)
2187 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2188 if (attr->target)
2189 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2190 if (attr->threadprivate)
2191 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2192 if (attr->dummy)
2193 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2194 if (attr->result)
2195 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2196 /* We deliberately don't preserve the "entry" flag. */
2198 if (attr->data)
2199 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2200 if (attr->in_namelist)
2201 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2202 if (attr->in_common)
2203 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2205 if (attr->function)
2206 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2207 if (attr->subroutine)
2208 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2209 if (attr->generic)
2210 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2211 if (attr->abstract)
2212 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2214 if (attr->sequence)
2215 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2216 if (attr->elemental)
2217 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2218 if (attr->pure)
2219 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2220 if (attr->implicit_pure)
2221 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2222 if (attr->unlimited_polymorphic)
2223 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2224 if (attr->recursive)
2225 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2226 if (attr->always_explicit)
2227 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2228 if (attr->cray_pointer)
2229 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2230 if (attr->cray_pointee)
2231 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2232 if (attr->is_bind_c)
2233 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2234 if (attr->is_c_interop)
2235 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2236 if (attr->is_iso_c)
2237 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2238 if (attr->alloc_comp)
2239 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2240 if (attr->pointer_comp)
2241 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2242 if (attr->proc_pointer_comp)
2243 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2244 if (attr->private_comp)
2245 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2246 if (attr->coarray_comp)
2247 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2248 if (attr->lock_comp)
2249 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2250 if (attr->event_comp)
2251 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2252 if (attr->zero_comp)
2253 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2254 if (attr->is_class)
2255 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2256 if (attr->procedure)
2257 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2258 if (attr->proc_pointer)
2259 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2260 if (attr->vtype)
2261 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2262 if (attr->vtab)
2263 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2264 if (attr->omp_declare_target)
2265 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2266 if (attr->array_outer_dependency)
2267 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2268 if (attr->module_procedure)
2269 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2270 if (attr->oacc_declare_create)
2271 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2272 if (attr->oacc_declare_copyin)
2273 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2274 if (attr->oacc_declare_deviceptr)
2275 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2276 if (attr->oacc_declare_device_resident)
2277 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2278 if (attr->oacc_declare_link)
2279 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2280 if (attr->omp_declare_target_link)
2281 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET_LINK, attr_bits);
2282 if (attr->pdt_kind)
2283 MIO_NAME (ab_attribute) (AB_PDT_KIND, attr_bits);
2284 if (attr->pdt_len)
2285 MIO_NAME (ab_attribute) (AB_PDT_LEN, attr_bits);
2286 if (attr->pdt_type)
2287 MIO_NAME (ab_attribute) (AB_PDT_TYPE, attr_bits);
2288 if (attr->pdt_template)
2289 MIO_NAME (ab_attribute) (AB_PDT_TEMPLATE, attr_bits);
2290 if (attr->pdt_array)
2291 MIO_NAME (ab_attribute) (AB_PDT_ARRAY, attr_bits);
2292 if (attr->pdt_string)
2293 MIO_NAME (ab_attribute) (AB_PDT_STRING, attr_bits);
2295 mio_rparen ();
2298 else
2300 for (;;)
2302 t = parse_atom ();
2303 if (t == ATOM_RPAREN)
2304 break;
2305 if (t != ATOM_NAME)
2306 bad_module ("Expected attribute bit name");
2308 switch ((ab_attribute) find_enum (attr_bits))
2310 case AB_ALLOCATABLE:
2311 attr->allocatable = 1;
2312 break;
2313 case AB_ARTIFICIAL:
2314 attr->artificial = 1;
2315 break;
2316 case AB_ASYNCHRONOUS:
2317 attr->asynchronous = 1;
2318 break;
2319 case AB_DIMENSION:
2320 attr->dimension = 1;
2321 break;
2322 case AB_CODIMENSION:
2323 attr->codimension = 1;
2324 break;
2325 case AB_CONTIGUOUS:
2326 attr->contiguous = 1;
2327 break;
2328 case AB_EXTERNAL:
2329 attr->external = 1;
2330 break;
2331 case AB_INTRINSIC:
2332 attr->intrinsic = 1;
2333 break;
2334 case AB_OPTIONAL:
2335 attr->optional = 1;
2336 break;
2337 case AB_POINTER:
2338 attr->pointer = 1;
2339 break;
2340 case AB_CLASS_POINTER:
2341 attr->class_pointer = 1;
2342 break;
2343 case AB_PROTECTED:
2344 attr->is_protected = 1;
2345 break;
2346 case AB_VALUE:
2347 attr->value = 1;
2348 break;
2349 case AB_VOLATILE:
2350 attr->volatile_ = 1;
2351 break;
2352 case AB_TARGET:
2353 attr->target = 1;
2354 break;
2355 case AB_THREADPRIVATE:
2356 attr->threadprivate = 1;
2357 break;
2358 case AB_DUMMY:
2359 attr->dummy = 1;
2360 break;
2361 case AB_RESULT:
2362 attr->result = 1;
2363 break;
2364 case AB_DATA:
2365 attr->data = 1;
2366 break;
2367 case AB_IN_NAMELIST:
2368 attr->in_namelist = 1;
2369 break;
2370 case AB_IN_COMMON:
2371 attr->in_common = 1;
2372 break;
2373 case AB_FUNCTION:
2374 attr->function = 1;
2375 break;
2376 case AB_SUBROUTINE:
2377 attr->subroutine = 1;
2378 break;
2379 case AB_GENERIC:
2380 attr->generic = 1;
2381 break;
2382 case AB_ABSTRACT:
2383 attr->abstract = 1;
2384 break;
2385 case AB_SEQUENCE:
2386 attr->sequence = 1;
2387 break;
2388 case AB_ELEMENTAL:
2389 attr->elemental = 1;
2390 break;
2391 case AB_PURE:
2392 attr->pure = 1;
2393 break;
2394 case AB_IMPLICIT_PURE:
2395 attr->implicit_pure = 1;
2396 break;
2397 case AB_UNLIMITED_POLY:
2398 attr->unlimited_polymorphic = 1;
2399 break;
2400 case AB_RECURSIVE:
2401 attr->recursive = 1;
2402 break;
2403 case AB_ALWAYS_EXPLICIT:
2404 attr->always_explicit = 1;
2405 break;
2406 case AB_CRAY_POINTER:
2407 attr->cray_pointer = 1;
2408 break;
2409 case AB_CRAY_POINTEE:
2410 attr->cray_pointee = 1;
2411 break;
2412 case AB_IS_BIND_C:
2413 attr->is_bind_c = 1;
2414 break;
2415 case AB_IS_C_INTEROP:
2416 attr->is_c_interop = 1;
2417 break;
2418 case AB_IS_ISO_C:
2419 attr->is_iso_c = 1;
2420 break;
2421 case AB_ALLOC_COMP:
2422 attr->alloc_comp = 1;
2423 break;
2424 case AB_COARRAY_COMP:
2425 attr->coarray_comp = 1;
2426 break;
2427 case AB_LOCK_COMP:
2428 attr->lock_comp = 1;
2429 break;
2430 case AB_EVENT_COMP:
2431 attr->event_comp = 1;
2432 break;
2433 case AB_POINTER_COMP:
2434 attr->pointer_comp = 1;
2435 break;
2436 case AB_PROC_POINTER_COMP:
2437 attr->proc_pointer_comp = 1;
2438 break;
2439 case AB_PRIVATE_COMP:
2440 attr->private_comp = 1;
2441 break;
2442 case AB_ZERO_COMP:
2443 attr->zero_comp = 1;
2444 break;
2445 case AB_IS_CLASS:
2446 attr->is_class = 1;
2447 break;
2448 case AB_PROCEDURE:
2449 attr->procedure = 1;
2450 break;
2451 case AB_PROC_POINTER:
2452 attr->proc_pointer = 1;
2453 break;
2454 case AB_VTYPE:
2455 attr->vtype = 1;
2456 break;
2457 case AB_VTAB:
2458 attr->vtab = 1;
2459 break;
2460 case AB_OMP_DECLARE_TARGET:
2461 attr->omp_declare_target = 1;
2462 break;
2463 case AB_OMP_DECLARE_TARGET_LINK:
2464 attr->omp_declare_target_link = 1;
2465 break;
2466 case AB_ARRAY_OUTER_DEPENDENCY:
2467 attr->array_outer_dependency =1;
2468 break;
2469 case AB_MODULE_PROCEDURE:
2470 attr->module_procedure =1;
2471 break;
2472 case AB_OACC_DECLARE_CREATE:
2473 attr->oacc_declare_create = 1;
2474 break;
2475 case AB_OACC_DECLARE_COPYIN:
2476 attr->oacc_declare_copyin = 1;
2477 break;
2478 case AB_OACC_DECLARE_DEVICEPTR:
2479 attr->oacc_declare_deviceptr = 1;
2480 break;
2481 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2482 attr->oacc_declare_device_resident = 1;
2483 break;
2484 case AB_OACC_DECLARE_LINK:
2485 attr->oacc_declare_link = 1;
2486 break;
2487 case AB_PDT_KIND:
2488 attr->pdt_kind = 1;
2489 break;
2490 case AB_PDT_LEN:
2491 attr->pdt_len = 1;
2492 break;
2493 case AB_PDT_TYPE:
2494 attr->pdt_type = 1;
2495 break;
2496 case AB_PDT_TEMPLATE:
2497 attr->pdt_template = 1;
2498 break;
2499 case AB_PDT_ARRAY:
2500 attr->pdt_array = 1;
2501 break;
2502 case AB_PDT_STRING:
2503 attr->pdt_string = 1;
2504 break;
2511 static const mstring bt_types[] = {
2512 minit ("INTEGER", BT_INTEGER),
2513 minit ("REAL", BT_REAL),
2514 minit ("COMPLEX", BT_COMPLEX),
2515 minit ("LOGICAL", BT_LOGICAL),
2516 minit ("CHARACTER", BT_CHARACTER),
2517 minit ("UNION", BT_UNION),
2518 minit ("DERIVED", BT_DERIVED),
2519 minit ("CLASS", BT_CLASS),
2520 minit ("PROCEDURE", BT_PROCEDURE),
2521 minit ("UNKNOWN", BT_UNKNOWN),
2522 minit ("VOID", BT_VOID),
2523 minit ("ASSUMED", BT_ASSUMED),
2524 minit (NULL, -1)
2528 static void
2529 mio_charlen (gfc_charlen **clp)
2531 gfc_charlen *cl;
2533 mio_lparen ();
2535 if (iomode == IO_OUTPUT)
2537 cl = *clp;
2538 if (cl != NULL)
2539 mio_expr (&cl->length);
2541 else
2543 if (peek_atom () != ATOM_RPAREN)
2545 cl = gfc_new_charlen (gfc_current_ns, NULL);
2546 mio_expr (&cl->length);
2547 *clp = cl;
2551 mio_rparen ();
2555 /* See if a name is a generated name. */
2557 static int
2558 check_unique_name (const char *name)
2560 return *name == '@';
2564 static void
2565 mio_typespec (gfc_typespec *ts)
2567 mio_lparen ();
2569 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2571 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2572 mio_integer (&ts->kind);
2573 else
2574 mio_symbol_ref (&ts->u.derived);
2576 mio_symbol_ref (&ts->interface);
2578 /* Add info for C interop and is_iso_c. */
2579 mio_integer (&ts->is_c_interop);
2580 mio_integer (&ts->is_iso_c);
2582 /* If the typespec is for an identifier either from iso_c_binding, or
2583 a constant that was initialized to an identifier from it, use the
2584 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2585 if (ts->is_iso_c)
2586 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2587 else
2588 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2590 if (ts->type != BT_CHARACTER)
2592 /* ts->u.cl is only valid for BT_CHARACTER. */
2593 mio_lparen ();
2594 mio_rparen ();
2596 else
2597 mio_charlen (&ts->u.cl);
2599 /* So as not to disturb the existing API, use an ATOM_NAME to
2600 transmit deferred characteristic for characters (F2003). */
2601 if (iomode == IO_OUTPUT)
2603 if (ts->type == BT_CHARACTER && ts->deferred)
2604 write_atom (ATOM_NAME, "DEFERRED_CL");
2606 else if (peek_atom () != ATOM_RPAREN)
2608 if (parse_atom () != ATOM_NAME)
2609 bad_module ("Expected string");
2610 ts->deferred = 1;
2613 mio_rparen ();
2617 static const mstring array_spec_types[] = {
2618 minit ("EXPLICIT", AS_EXPLICIT),
2619 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2620 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2621 minit ("DEFERRED", AS_DEFERRED),
2622 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2623 minit (NULL, -1)
2627 static void
2628 mio_array_spec (gfc_array_spec **asp)
2630 gfc_array_spec *as;
2631 int i;
2633 mio_lparen ();
2635 if (iomode == IO_OUTPUT)
2637 int rank;
2639 if (*asp == NULL)
2640 goto done;
2641 as = *asp;
2643 /* mio_integer expects nonnegative values. */
2644 rank = as->rank > 0 ? as->rank : 0;
2645 mio_integer (&rank);
2647 else
2649 if (peek_atom () == ATOM_RPAREN)
2651 *asp = NULL;
2652 goto done;
2655 *asp = as = gfc_get_array_spec ();
2656 mio_integer (&as->rank);
2659 mio_integer (&as->corank);
2660 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2662 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2663 as->rank = -1;
2664 if (iomode == IO_INPUT && as->corank)
2665 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2667 if (as->rank + as->corank > 0)
2668 for (i = 0; i < as->rank + as->corank; i++)
2670 mio_expr (&as->lower[i]);
2671 mio_expr (&as->upper[i]);
2674 done:
2675 mio_rparen ();
2679 /* Given a pointer to an array reference structure (which lives in a
2680 gfc_ref structure), find the corresponding array specification
2681 structure. Storing the pointer in the ref structure doesn't quite
2682 work when loading from a module. Generating code for an array
2683 reference also needs more information than just the array spec. */
2685 static const mstring array_ref_types[] = {
2686 minit ("FULL", AR_FULL),
2687 minit ("ELEMENT", AR_ELEMENT),
2688 minit ("SECTION", AR_SECTION),
2689 minit (NULL, -1)
2693 static void
2694 mio_array_ref (gfc_array_ref *ar)
2696 int i;
2698 mio_lparen ();
2699 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2700 mio_integer (&ar->dimen);
2702 switch (ar->type)
2704 case AR_FULL:
2705 break;
2707 case AR_ELEMENT:
2708 for (i = 0; i < ar->dimen; i++)
2709 mio_expr (&ar->start[i]);
2711 break;
2713 case AR_SECTION:
2714 for (i = 0; i < ar->dimen; i++)
2716 mio_expr (&ar->start[i]);
2717 mio_expr (&ar->end[i]);
2718 mio_expr (&ar->stride[i]);
2721 break;
2723 case AR_UNKNOWN:
2724 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2727 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2728 we can't call mio_integer directly. Instead loop over each element
2729 and cast it to/from an integer. */
2730 if (iomode == IO_OUTPUT)
2732 for (i = 0; i < ar->dimen; i++)
2734 HOST_WIDE_INT tmp = (HOST_WIDE_INT)ar->dimen_type[i];
2735 write_atom (ATOM_INTEGER, &tmp);
2738 else
2740 for (i = 0; i < ar->dimen; i++)
2742 require_atom (ATOM_INTEGER);
2743 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2747 if (iomode == IO_INPUT)
2749 ar->where = gfc_current_locus;
2751 for (i = 0; i < ar->dimen; i++)
2752 ar->c_where[i] = gfc_current_locus;
2755 mio_rparen ();
2759 /* Saves or restores a pointer. The pointer is converted back and
2760 forth from an integer. We return the pointer_info pointer so that
2761 the caller can take additional action based on the pointer type. */
2763 static pointer_info *
2764 mio_pointer_ref (void *gp)
2766 pointer_info *p;
2768 if (iomode == IO_OUTPUT)
2770 p = get_pointer (*((char **) gp));
2771 HOST_WIDE_INT hwi = p->integer;
2772 write_atom (ATOM_INTEGER, &hwi);
2774 else
2776 require_atom (ATOM_INTEGER);
2777 p = add_fixup (atom_int, gp);
2780 return p;
2784 /* Save and load references to components that occur within
2785 expressions. We have to describe these references by a number and
2786 by name. The number is necessary for forward references during
2787 reading, and the name is necessary if the symbol already exists in
2788 the namespace and is not loaded again. */
2790 static void
2791 mio_component_ref (gfc_component **cp)
2793 pointer_info *p;
2795 p = mio_pointer_ref (cp);
2796 if (p->type == P_UNKNOWN)
2797 p->type = P_COMPONENT;
2801 static void mio_namespace_ref (gfc_namespace **nsp);
2802 static void mio_formal_arglist (gfc_formal_arglist **formal);
2803 static void mio_typebound_proc (gfc_typebound_proc** proc);
2804 static void mio_actual_arglist (gfc_actual_arglist **ap, bool pdt);
2806 static void
2807 mio_component (gfc_component *c, int vtype)
2809 pointer_info *p;
2811 mio_lparen ();
2813 if (iomode == IO_OUTPUT)
2815 p = get_pointer (c);
2816 mio_hwi (&p->integer);
2818 else
2820 HOST_WIDE_INT n;
2821 mio_hwi (&n);
2822 p = get_integer (n);
2823 associate_integer_pointer (p, c);
2826 if (p->type == P_UNKNOWN)
2827 p->type = P_COMPONENT;
2829 mio_pool_string (&c->name);
2830 mio_typespec (&c->ts);
2831 mio_array_spec (&c->as);
2833 /* PDT templates store the expression for the kind of a component here. */
2834 mio_expr (&c->kind_expr);
2836 /* PDT types store the component specification list here. */
2837 mio_actual_arglist (&c->param_list, true);
2839 mio_symbol_attribute (&c->attr);
2840 if (c->ts.type == BT_CLASS)
2841 c->attr.class_ok = 1;
2842 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2844 if (!vtype || strcmp (c->name, "_final") == 0
2845 || strcmp (c->name, "_hash") == 0)
2846 mio_expr (&c->initializer);
2848 if (c->attr.proc_pointer)
2849 mio_typebound_proc (&c->tb);
2851 c->loc = gfc_current_locus;
2853 mio_rparen ();
2857 static void
2858 mio_component_list (gfc_component **cp, int vtype)
2860 gfc_component *c, *tail;
2862 mio_lparen ();
2864 if (iomode == IO_OUTPUT)
2866 for (c = *cp; c; c = c->next)
2867 mio_component (c, vtype);
2869 else
2871 *cp = NULL;
2872 tail = NULL;
2874 for (;;)
2876 if (peek_atom () == ATOM_RPAREN)
2877 break;
2879 c = gfc_get_component ();
2880 mio_component (c, vtype);
2882 if (tail == NULL)
2883 *cp = c;
2884 else
2885 tail->next = c;
2887 tail = c;
2891 mio_rparen ();
2895 static void
2896 mio_actual_arg (gfc_actual_arglist *a, bool pdt)
2898 mio_lparen ();
2899 mio_pool_string (&a->name);
2900 mio_expr (&a->expr);
2901 if (pdt)
2902 mio_integer ((int *)&a->spec_type);
2903 mio_rparen ();
2907 static void
2908 mio_actual_arglist (gfc_actual_arglist **ap, bool pdt)
2910 gfc_actual_arglist *a, *tail;
2912 mio_lparen ();
2914 if (iomode == IO_OUTPUT)
2916 for (a = *ap; a; a = a->next)
2917 mio_actual_arg (a, pdt);
2920 else
2922 tail = NULL;
2924 for (;;)
2926 if (peek_atom () != ATOM_LPAREN)
2927 break;
2929 a = gfc_get_actual_arglist ();
2931 if (tail == NULL)
2932 *ap = a;
2933 else
2934 tail->next = a;
2936 tail = a;
2937 mio_actual_arg (a, pdt);
2941 mio_rparen ();
2945 /* Read and write formal argument lists. */
2947 static void
2948 mio_formal_arglist (gfc_formal_arglist **formal)
2950 gfc_formal_arglist *f, *tail;
2952 mio_lparen ();
2954 if (iomode == IO_OUTPUT)
2956 for (f = *formal; f; f = f->next)
2957 mio_symbol_ref (&f->sym);
2959 else
2961 *formal = tail = NULL;
2963 while (peek_atom () != ATOM_RPAREN)
2965 f = gfc_get_formal_arglist ();
2966 mio_symbol_ref (&f->sym);
2968 if (*formal == NULL)
2969 *formal = f;
2970 else
2971 tail->next = f;
2973 tail = f;
2977 mio_rparen ();
2981 /* Save or restore a reference to a symbol node. */
2983 pointer_info *
2984 mio_symbol_ref (gfc_symbol **symp)
2986 pointer_info *p;
2988 p = mio_pointer_ref (symp);
2989 if (p->type == P_UNKNOWN)
2990 p->type = P_SYMBOL;
2992 if (iomode == IO_OUTPUT)
2994 if (p->u.wsym.state == UNREFERENCED)
2995 p->u.wsym.state = NEEDS_WRITE;
2997 else
2999 if (p->u.rsym.state == UNUSED)
3000 p->u.rsym.state = NEEDED;
3002 return p;
3006 /* Save or restore a reference to a symtree node. */
3008 static void
3009 mio_symtree_ref (gfc_symtree **stp)
3011 pointer_info *p;
3012 fixup_t *f;
3014 if (iomode == IO_OUTPUT)
3015 mio_symbol_ref (&(*stp)->n.sym);
3016 else
3018 require_atom (ATOM_INTEGER);
3019 p = get_integer (atom_int);
3021 /* An unused equivalence member; make a symbol and a symtree
3022 for it. */
3023 if (in_load_equiv && p->u.rsym.symtree == NULL)
3025 /* Since this is not used, it must have a unique name. */
3026 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
3028 /* Make the symbol. */
3029 if (p->u.rsym.sym == NULL)
3031 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
3032 gfc_current_ns);
3033 p->u.rsym.sym->module = gfc_get_string ("%s", p->u.rsym.module);
3036 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
3037 p->u.rsym.symtree->n.sym->refs++;
3038 p->u.rsym.referenced = 1;
3040 /* If the symbol is PRIVATE and in COMMON, load_commons will
3041 generate a fixup symbol, which must be associated. */
3042 if (p->fixup)
3043 resolve_fixups (p->fixup, p->u.rsym.sym);
3044 p->fixup = NULL;
3047 if (p->type == P_UNKNOWN)
3048 p->type = P_SYMBOL;
3050 if (p->u.rsym.state == UNUSED)
3051 p->u.rsym.state = NEEDED;
3053 if (p->u.rsym.symtree != NULL)
3055 *stp = p->u.rsym.symtree;
3057 else
3059 f = XCNEW (fixup_t);
3061 f->next = p->u.rsym.stfixup;
3062 p->u.rsym.stfixup = f;
3064 f->pointer = (void **) stp;
3070 static void
3071 mio_iterator (gfc_iterator **ip)
3073 gfc_iterator *iter;
3075 mio_lparen ();
3077 if (iomode == IO_OUTPUT)
3079 if (*ip == NULL)
3080 goto done;
3082 else
3084 if (peek_atom () == ATOM_RPAREN)
3086 *ip = NULL;
3087 goto done;
3090 *ip = gfc_get_iterator ();
3093 iter = *ip;
3095 mio_expr (&iter->var);
3096 mio_expr (&iter->start);
3097 mio_expr (&iter->end);
3098 mio_expr (&iter->step);
3100 done:
3101 mio_rparen ();
3105 static void
3106 mio_constructor (gfc_constructor_base *cp)
3108 gfc_constructor *c;
3110 mio_lparen ();
3112 if (iomode == IO_OUTPUT)
3114 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3116 mio_lparen ();
3117 mio_expr (&c->expr);
3118 mio_iterator (&c->iterator);
3119 mio_rparen ();
3122 else
3124 while (peek_atom () != ATOM_RPAREN)
3126 c = gfc_constructor_append_expr (cp, NULL, NULL);
3128 mio_lparen ();
3129 mio_expr (&c->expr);
3130 mio_iterator (&c->iterator);
3131 mio_rparen ();
3135 mio_rparen ();
3139 static const mstring ref_types[] = {
3140 minit ("ARRAY", REF_ARRAY),
3141 minit ("COMPONENT", REF_COMPONENT),
3142 minit ("SUBSTRING", REF_SUBSTRING),
3143 minit (NULL, -1)
3147 static void
3148 mio_ref (gfc_ref **rp)
3150 gfc_ref *r;
3152 mio_lparen ();
3154 r = *rp;
3155 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3157 switch (r->type)
3159 case REF_ARRAY:
3160 mio_array_ref (&r->u.ar);
3161 break;
3163 case REF_COMPONENT:
3164 mio_symbol_ref (&r->u.c.sym);
3165 mio_component_ref (&r->u.c.component);
3166 break;
3168 case REF_SUBSTRING:
3169 mio_expr (&r->u.ss.start);
3170 mio_expr (&r->u.ss.end);
3171 mio_charlen (&r->u.ss.length);
3172 break;
3175 mio_rparen ();
3179 static void
3180 mio_ref_list (gfc_ref **rp)
3182 gfc_ref *ref, *head, *tail;
3184 mio_lparen ();
3186 if (iomode == IO_OUTPUT)
3188 for (ref = *rp; ref; ref = ref->next)
3189 mio_ref (&ref);
3191 else
3193 head = tail = NULL;
3195 while (peek_atom () != ATOM_RPAREN)
3197 if (head == NULL)
3198 head = tail = gfc_get_ref ();
3199 else
3201 tail->next = gfc_get_ref ();
3202 tail = tail->next;
3205 mio_ref (&tail);
3208 *rp = head;
3211 mio_rparen ();
3215 /* Read and write an integer value. */
3217 static void
3218 mio_gmp_integer (mpz_t *integer)
3220 char *p;
3222 if (iomode == IO_INPUT)
3224 if (parse_atom () != ATOM_STRING)
3225 bad_module ("Expected integer string");
3227 mpz_init (*integer);
3228 if (mpz_set_str (*integer, atom_string, 10))
3229 bad_module ("Error converting integer");
3231 free (atom_string);
3233 else
3235 p = mpz_get_str (NULL, 10, *integer);
3236 write_atom (ATOM_STRING, p);
3237 free (p);
3242 static void
3243 mio_gmp_real (mpfr_t *real)
3245 mp_exp_t exponent;
3246 char *p;
3248 if (iomode == IO_INPUT)
3250 if (parse_atom () != ATOM_STRING)
3251 bad_module ("Expected real string");
3253 mpfr_init (*real);
3254 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3255 free (atom_string);
3257 else
3259 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3261 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3263 write_atom (ATOM_STRING, p);
3264 free (p);
3265 return;
3268 atom_string = XCNEWVEC (char, strlen (p) + 20);
3270 sprintf (atom_string, "0.%s@%ld", p, exponent);
3272 /* Fix negative numbers. */
3273 if (atom_string[2] == '-')
3275 atom_string[0] = '-';
3276 atom_string[1] = '0';
3277 atom_string[2] = '.';
3280 write_atom (ATOM_STRING, atom_string);
3282 free (atom_string);
3283 free (p);
3288 /* Save and restore the shape of an array constructor. */
3290 static void
3291 mio_shape (mpz_t **pshape, int rank)
3293 mpz_t *shape;
3294 atom_type t;
3295 int n;
3297 /* A NULL shape is represented by (). */
3298 mio_lparen ();
3300 if (iomode == IO_OUTPUT)
3302 shape = *pshape;
3303 if (!shape)
3305 mio_rparen ();
3306 return;
3309 else
3311 t = peek_atom ();
3312 if (t == ATOM_RPAREN)
3314 *pshape = NULL;
3315 mio_rparen ();
3316 return;
3319 shape = gfc_get_shape (rank);
3320 *pshape = shape;
3323 for (n = 0; n < rank; n++)
3324 mio_gmp_integer (&shape[n]);
3326 mio_rparen ();
3330 static const mstring expr_types[] = {
3331 minit ("OP", EXPR_OP),
3332 minit ("FUNCTION", EXPR_FUNCTION),
3333 minit ("CONSTANT", EXPR_CONSTANT),
3334 minit ("VARIABLE", EXPR_VARIABLE),
3335 minit ("SUBSTRING", EXPR_SUBSTRING),
3336 minit ("STRUCTURE", EXPR_STRUCTURE),
3337 minit ("ARRAY", EXPR_ARRAY),
3338 minit ("NULL", EXPR_NULL),
3339 minit ("COMPCALL", EXPR_COMPCALL),
3340 minit (NULL, -1)
3343 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3344 generic operators, not in expressions. INTRINSIC_USER is also
3345 replaced by the correct function name by the time we see it. */
3347 static const mstring intrinsics[] =
3349 minit ("UPLUS", INTRINSIC_UPLUS),
3350 minit ("UMINUS", INTRINSIC_UMINUS),
3351 minit ("PLUS", INTRINSIC_PLUS),
3352 minit ("MINUS", INTRINSIC_MINUS),
3353 minit ("TIMES", INTRINSIC_TIMES),
3354 minit ("DIVIDE", INTRINSIC_DIVIDE),
3355 minit ("POWER", INTRINSIC_POWER),
3356 minit ("CONCAT", INTRINSIC_CONCAT),
3357 minit ("AND", INTRINSIC_AND),
3358 minit ("OR", INTRINSIC_OR),
3359 minit ("EQV", INTRINSIC_EQV),
3360 minit ("NEQV", INTRINSIC_NEQV),
3361 minit ("EQ_SIGN", INTRINSIC_EQ),
3362 minit ("EQ", INTRINSIC_EQ_OS),
3363 minit ("NE_SIGN", INTRINSIC_NE),
3364 minit ("NE", INTRINSIC_NE_OS),
3365 minit ("GT_SIGN", INTRINSIC_GT),
3366 minit ("GT", INTRINSIC_GT_OS),
3367 minit ("GE_SIGN", INTRINSIC_GE),
3368 minit ("GE", INTRINSIC_GE_OS),
3369 minit ("LT_SIGN", INTRINSIC_LT),
3370 minit ("LT", INTRINSIC_LT_OS),
3371 minit ("LE_SIGN", INTRINSIC_LE),
3372 minit ("LE", INTRINSIC_LE_OS),
3373 minit ("NOT", INTRINSIC_NOT),
3374 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3375 minit ("USER", INTRINSIC_USER),
3376 minit (NULL, -1)
3380 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3382 static void
3383 fix_mio_expr (gfc_expr *e)
3385 gfc_symtree *ns_st = NULL;
3386 const char *fname;
3388 if (iomode != IO_OUTPUT)
3389 return;
3391 if (e->symtree)
3393 /* If this is a symtree for a symbol that came from a contained module
3394 namespace, it has a unique name and we should look in the current
3395 namespace to see if the required, non-contained symbol is available
3396 yet. If so, the latter should be written. */
3397 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3399 const char *name = e->symtree->n.sym->name;
3400 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3401 name = gfc_dt_upper_string (name);
3402 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3405 /* On the other hand, if the existing symbol is the module name or the
3406 new symbol is a dummy argument, do not do the promotion. */
3407 if (ns_st && ns_st->n.sym
3408 && ns_st->n.sym->attr.flavor != FL_MODULE
3409 && !e->symtree->n.sym->attr.dummy)
3410 e->symtree = ns_st;
3412 else if (e->expr_type == EXPR_FUNCTION
3413 && (e->value.function.name || e->value.function.isym))
3415 gfc_symbol *sym;
3417 /* In some circumstances, a function used in an initialization
3418 expression, in one use associated module, can fail to be
3419 coupled to its symtree when used in a specification
3420 expression in another module. */
3421 fname = e->value.function.esym ? e->value.function.esym->name
3422 : e->value.function.isym->name;
3423 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3425 if (e->symtree)
3426 return;
3428 /* This is probably a reference to a private procedure from another
3429 module. To prevent a segfault, make a generic with no specific
3430 instances. If this module is used, without the required
3431 specific coming from somewhere, the appropriate error message
3432 is issued. */
3433 gfc_get_symbol (fname, gfc_current_ns, &sym);
3434 sym->attr.flavor = FL_PROCEDURE;
3435 sym->attr.generic = 1;
3436 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3437 gfc_commit_symbol (sym);
3442 /* Read and write expressions. The form "()" is allowed to indicate a
3443 NULL expression. */
3445 static void
3446 mio_expr (gfc_expr **ep)
3448 HOST_WIDE_INT hwi;
3449 gfc_expr *e;
3450 atom_type t;
3451 int flag;
3453 mio_lparen ();
3455 if (iomode == IO_OUTPUT)
3457 if (*ep == NULL)
3459 mio_rparen ();
3460 return;
3463 e = *ep;
3464 MIO_NAME (expr_t) (e->expr_type, expr_types);
3466 else
3468 t = parse_atom ();
3469 if (t == ATOM_RPAREN)
3471 *ep = NULL;
3472 return;
3475 if (t != ATOM_NAME)
3476 bad_module ("Expected expression type");
3478 e = *ep = gfc_get_expr ();
3479 e->where = gfc_current_locus;
3480 e->expr_type = (expr_t) find_enum (expr_types);
3483 mio_typespec (&e->ts);
3484 mio_integer (&e->rank);
3486 fix_mio_expr (e);
3488 switch (e->expr_type)
3490 case EXPR_OP:
3491 e->value.op.op
3492 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3494 switch (e->value.op.op)
3496 case INTRINSIC_UPLUS:
3497 case INTRINSIC_UMINUS:
3498 case INTRINSIC_NOT:
3499 case INTRINSIC_PARENTHESES:
3500 mio_expr (&e->value.op.op1);
3501 break;
3503 case INTRINSIC_PLUS:
3504 case INTRINSIC_MINUS:
3505 case INTRINSIC_TIMES:
3506 case INTRINSIC_DIVIDE:
3507 case INTRINSIC_POWER:
3508 case INTRINSIC_CONCAT:
3509 case INTRINSIC_AND:
3510 case INTRINSIC_OR:
3511 case INTRINSIC_EQV:
3512 case INTRINSIC_NEQV:
3513 case INTRINSIC_EQ:
3514 case INTRINSIC_EQ_OS:
3515 case INTRINSIC_NE:
3516 case INTRINSIC_NE_OS:
3517 case INTRINSIC_GT:
3518 case INTRINSIC_GT_OS:
3519 case INTRINSIC_GE:
3520 case INTRINSIC_GE_OS:
3521 case INTRINSIC_LT:
3522 case INTRINSIC_LT_OS:
3523 case INTRINSIC_LE:
3524 case INTRINSIC_LE_OS:
3525 mio_expr (&e->value.op.op1);
3526 mio_expr (&e->value.op.op2);
3527 break;
3529 case INTRINSIC_USER:
3530 /* INTRINSIC_USER should not appear in resolved expressions,
3531 though for UDRs we need to stream unresolved ones. */
3532 if (iomode == IO_OUTPUT)
3533 write_atom (ATOM_STRING, e->value.op.uop->name);
3534 else
3536 char *name = read_string ();
3537 const char *uop_name = find_use_name (name, true);
3538 if (uop_name == NULL)
3540 size_t len = strlen (name);
3541 char *name2 = XCNEWVEC (char, len + 2);
3542 memcpy (name2, name, len);
3543 name2[len] = ' ';
3544 name2[len + 1] = '\0';
3545 free (name);
3546 uop_name = name = name2;
3548 e->value.op.uop = gfc_get_uop (uop_name);
3549 free (name);
3551 mio_expr (&e->value.op.op1);
3552 mio_expr (&e->value.op.op2);
3553 break;
3555 default:
3556 bad_module ("Bad operator");
3559 break;
3561 case EXPR_FUNCTION:
3562 mio_symtree_ref (&e->symtree);
3563 mio_actual_arglist (&e->value.function.actual, false);
3565 if (iomode == IO_OUTPUT)
3567 e->value.function.name
3568 = mio_allocated_string (e->value.function.name);
3569 if (e->value.function.esym)
3570 flag = 1;
3571 else if (e->ref)
3572 flag = 2;
3573 else if (e->value.function.isym == NULL)
3574 flag = 3;
3575 else
3576 flag = 0;
3577 mio_integer (&flag);
3578 switch (flag)
3580 case 1:
3581 mio_symbol_ref (&e->value.function.esym);
3582 break;
3583 case 2:
3584 mio_ref_list (&e->ref);
3585 break;
3586 case 3:
3587 break;
3588 default:
3589 write_atom (ATOM_STRING, e->value.function.isym->name);
3592 else
3594 require_atom (ATOM_STRING);
3595 if (atom_string[0] == '\0')
3596 e->value.function.name = NULL;
3597 else
3598 e->value.function.name = gfc_get_string ("%s", atom_string);
3599 free (atom_string);
3601 mio_integer (&flag);
3602 switch (flag)
3604 case 1:
3605 mio_symbol_ref (&e->value.function.esym);
3606 break;
3607 case 2:
3608 mio_ref_list (&e->ref);
3609 break;
3610 case 3:
3611 break;
3612 default:
3613 require_atom (ATOM_STRING);
3614 e->value.function.isym = gfc_find_function (atom_string);
3615 free (atom_string);
3619 break;
3621 case EXPR_VARIABLE:
3622 mio_symtree_ref (&e->symtree);
3623 mio_ref_list (&e->ref);
3624 break;
3626 case EXPR_SUBSTRING:
3627 e->value.character.string
3628 = CONST_CAST (gfc_char_t *,
3629 mio_allocated_wide_string (e->value.character.string,
3630 e->value.character.length));
3631 mio_ref_list (&e->ref);
3632 break;
3634 case EXPR_STRUCTURE:
3635 case EXPR_ARRAY:
3636 mio_constructor (&e->value.constructor);
3637 mio_shape (&e->shape, e->rank);
3638 break;
3640 case EXPR_CONSTANT:
3641 switch (e->ts.type)
3643 case BT_INTEGER:
3644 mio_gmp_integer (&e->value.integer);
3645 break;
3647 case BT_REAL:
3648 gfc_set_model_kind (e->ts.kind);
3649 mio_gmp_real (&e->value.real);
3650 break;
3652 case BT_COMPLEX:
3653 gfc_set_model_kind (e->ts.kind);
3654 mio_gmp_real (&mpc_realref (e->value.complex));
3655 mio_gmp_real (&mpc_imagref (e->value.complex));
3656 break;
3658 case BT_LOGICAL:
3659 mio_integer (&e->value.logical);
3660 break;
3662 case BT_CHARACTER:
3663 hwi = e->value.character.length;
3664 mio_hwi (&hwi);
3665 e->value.character.length = hwi;
3666 e->value.character.string
3667 = CONST_CAST (gfc_char_t *,
3668 mio_allocated_wide_string (e->value.character.string,
3669 e->value.character.length));
3670 break;
3672 default:
3673 bad_module ("Bad type in constant expression");
3676 break;
3678 case EXPR_NULL:
3679 break;
3681 case EXPR_COMPCALL:
3682 case EXPR_PPC:
3683 gcc_unreachable ();
3684 break;
3687 /* PDT types store the expression specification list here. */
3688 mio_actual_arglist (&e->param_list, true);
3690 mio_rparen ();
3694 /* Read and write namelists. */
3696 static void
3697 mio_namelist (gfc_symbol *sym)
3699 gfc_namelist *n, *m;
3700 const char *check_name;
3702 mio_lparen ();
3704 if (iomode == IO_OUTPUT)
3706 for (n = sym->namelist; n; n = n->next)
3707 mio_symbol_ref (&n->sym);
3709 else
3711 /* This departure from the standard is flagged as an error.
3712 It does, in fact, work correctly. TODO: Allow it
3713 conditionally? */
3714 if (sym->attr.flavor == FL_NAMELIST)
3716 check_name = find_use_name (sym->name, false);
3717 if (check_name && strcmp (check_name, sym->name) != 0)
3718 gfc_error ("Namelist %s cannot be renamed by USE "
3719 "association to %s", sym->name, check_name);
3722 m = NULL;
3723 while (peek_atom () != ATOM_RPAREN)
3725 n = gfc_get_namelist ();
3726 mio_symbol_ref (&n->sym);
3728 if (sym->namelist == NULL)
3729 sym->namelist = n;
3730 else
3731 m->next = n;
3733 m = n;
3735 sym->namelist_tail = m;
3738 mio_rparen ();
3742 /* Save/restore lists of gfc_interface structures. When loading an
3743 interface, we are really appending to the existing list of
3744 interfaces. Checking for duplicate and ambiguous interfaces has to
3745 be done later when all symbols have been loaded. */
3747 pointer_info *
3748 mio_interface_rest (gfc_interface **ip)
3750 gfc_interface *tail, *p;
3751 pointer_info *pi = NULL;
3753 if (iomode == IO_OUTPUT)
3755 if (ip != NULL)
3756 for (p = *ip; p; p = p->next)
3757 mio_symbol_ref (&p->sym);
3759 else
3761 if (*ip == NULL)
3762 tail = NULL;
3763 else
3765 tail = *ip;
3766 while (tail->next)
3767 tail = tail->next;
3770 for (;;)
3772 if (peek_atom () == ATOM_RPAREN)
3773 break;
3775 p = gfc_get_interface ();
3776 p->where = gfc_current_locus;
3777 pi = mio_symbol_ref (&p->sym);
3779 if (tail == NULL)
3780 *ip = p;
3781 else
3782 tail->next = p;
3784 tail = p;
3788 mio_rparen ();
3789 return pi;
3793 /* Save/restore a nameless operator interface. */
3795 static void
3796 mio_interface (gfc_interface **ip)
3798 mio_lparen ();
3799 mio_interface_rest (ip);
3803 /* Save/restore a named operator interface. */
3805 static void
3806 mio_symbol_interface (const char **name, const char **module,
3807 gfc_interface **ip)
3809 mio_lparen ();
3810 mio_pool_string (name);
3811 mio_pool_string (module);
3812 mio_interface_rest (ip);
3816 static void
3817 mio_namespace_ref (gfc_namespace **nsp)
3819 gfc_namespace *ns;
3820 pointer_info *p;
3822 p = mio_pointer_ref (nsp);
3824 if (p->type == P_UNKNOWN)
3825 p->type = P_NAMESPACE;
3827 if (iomode == IO_INPUT && p->integer != 0)
3829 ns = (gfc_namespace *) p->u.pointer;
3830 if (ns == NULL)
3832 ns = gfc_get_namespace (NULL, 0);
3833 associate_integer_pointer (p, ns);
3835 else
3836 ns->refs++;
3841 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3843 static gfc_namespace* current_f2k_derived;
3845 static void
3846 mio_typebound_proc (gfc_typebound_proc** proc)
3848 int flag;
3849 int overriding_flag;
3851 if (iomode == IO_INPUT)
3853 *proc = gfc_get_typebound_proc (NULL);
3854 (*proc)->where = gfc_current_locus;
3856 gcc_assert (*proc);
3858 mio_lparen ();
3860 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3862 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3863 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3864 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3865 overriding_flag = mio_name (overriding_flag, binding_overriding);
3866 (*proc)->deferred = ((overriding_flag & 2) != 0);
3867 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3868 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3870 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3871 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3872 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3874 mio_pool_string (&((*proc)->pass_arg));
3876 flag = (int) (*proc)->pass_arg_num;
3877 mio_integer (&flag);
3878 (*proc)->pass_arg_num = (unsigned) flag;
3880 if ((*proc)->is_generic)
3882 gfc_tbp_generic* g;
3883 int iop;
3885 mio_lparen ();
3887 if (iomode == IO_OUTPUT)
3888 for (g = (*proc)->u.generic; g; g = g->next)
3890 iop = (int) g->is_operator;
3891 mio_integer (&iop);
3892 mio_allocated_string (g->specific_st->name);
3894 else
3896 (*proc)->u.generic = NULL;
3897 while (peek_atom () != ATOM_RPAREN)
3899 gfc_symtree** sym_root;
3901 g = gfc_get_tbp_generic ();
3902 g->specific = NULL;
3904 mio_integer (&iop);
3905 g->is_operator = (bool) iop;
3907 require_atom (ATOM_STRING);
3908 sym_root = &current_f2k_derived->tb_sym_root;
3909 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3910 free (atom_string);
3912 g->next = (*proc)->u.generic;
3913 (*proc)->u.generic = g;
3917 mio_rparen ();
3919 else if (!(*proc)->ppc)
3920 mio_symtree_ref (&(*proc)->u.specific);
3922 mio_rparen ();
3925 /* Walker-callback function for this purpose. */
3926 static void
3927 mio_typebound_symtree (gfc_symtree* st)
3929 if (iomode == IO_OUTPUT && !st->n.tb)
3930 return;
3932 if (iomode == IO_OUTPUT)
3934 mio_lparen ();
3935 mio_allocated_string (st->name);
3937 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3939 mio_typebound_proc (&st->n.tb);
3940 mio_rparen ();
3943 /* IO a full symtree (in all depth). */
3944 static void
3945 mio_full_typebound_tree (gfc_symtree** root)
3947 mio_lparen ();
3949 if (iomode == IO_OUTPUT)
3950 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3951 else
3953 while (peek_atom () == ATOM_LPAREN)
3955 gfc_symtree* st;
3957 mio_lparen ();
3959 require_atom (ATOM_STRING);
3960 st = gfc_get_tbp_symtree (root, atom_string);
3961 free (atom_string);
3963 mio_typebound_symtree (st);
3967 mio_rparen ();
3970 static void
3971 mio_finalizer (gfc_finalizer **f)
3973 if (iomode == IO_OUTPUT)
3975 gcc_assert (*f);
3976 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3977 mio_symtree_ref (&(*f)->proc_tree);
3979 else
3981 *f = gfc_get_finalizer ();
3982 (*f)->where = gfc_current_locus; /* Value should not matter. */
3983 (*f)->next = NULL;
3985 mio_symtree_ref (&(*f)->proc_tree);
3986 (*f)->proc_sym = NULL;
3990 static void
3991 mio_f2k_derived (gfc_namespace *f2k)
3993 current_f2k_derived = f2k;
3995 /* Handle the list of finalizer procedures. */
3996 mio_lparen ();
3997 if (iomode == IO_OUTPUT)
3999 gfc_finalizer *f;
4000 for (f = f2k->finalizers; f; f = f->next)
4001 mio_finalizer (&f);
4003 else
4005 f2k->finalizers = NULL;
4006 while (peek_atom () != ATOM_RPAREN)
4008 gfc_finalizer *cur = NULL;
4009 mio_finalizer (&cur);
4010 cur->next = f2k->finalizers;
4011 f2k->finalizers = cur;
4014 mio_rparen ();
4016 /* Handle type-bound procedures. */
4017 mio_full_typebound_tree (&f2k->tb_sym_root);
4019 /* Type-bound user operators. */
4020 mio_full_typebound_tree (&f2k->tb_uop_root);
4022 /* Type-bound intrinsic operators. */
4023 mio_lparen ();
4024 if (iomode == IO_OUTPUT)
4026 int op;
4027 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
4029 gfc_intrinsic_op realop;
4031 if (op == INTRINSIC_USER || !f2k->tb_op[op])
4032 continue;
4034 mio_lparen ();
4035 realop = (gfc_intrinsic_op) op;
4036 mio_intrinsic_op (&realop);
4037 mio_typebound_proc (&f2k->tb_op[op]);
4038 mio_rparen ();
4041 else
4042 while (peek_atom () != ATOM_RPAREN)
4044 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
4046 mio_lparen ();
4047 mio_intrinsic_op (&op);
4048 mio_typebound_proc (&f2k->tb_op[op]);
4049 mio_rparen ();
4051 mio_rparen ();
4054 static void
4055 mio_full_f2k_derived (gfc_symbol *sym)
4057 mio_lparen ();
4059 if (iomode == IO_OUTPUT)
4061 if (sym->f2k_derived)
4062 mio_f2k_derived (sym->f2k_derived);
4064 else
4066 if (peek_atom () != ATOM_RPAREN)
4068 gfc_namespace *ns;
4070 sym->f2k_derived = gfc_get_namespace (NULL, 0);
4072 /* PDT templates make use of the mechanisms for formal args
4073 and so the parameter symbols are stored in the formal
4074 namespace. Transfer the sym_root to f2k_derived and then
4075 free the formal namespace since it is uneeded. */
4076 if (sym->attr.pdt_template && sym->formal && sym->formal->sym)
4078 ns = sym->formal->sym->ns;
4079 sym->f2k_derived->sym_root = ns->sym_root;
4080 ns->sym_root = NULL;
4081 ns->refs++;
4082 gfc_free_namespace (ns);
4083 ns = NULL;
4086 mio_f2k_derived (sym->f2k_derived);
4088 else
4089 gcc_assert (!sym->f2k_derived);
4092 mio_rparen ();
4095 static const mstring omp_declare_simd_clauses[] =
4097 minit ("INBRANCH", 0),
4098 minit ("NOTINBRANCH", 1),
4099 minit ("SIMDLEN", 2),
4100 minit ("UNIFORM", 3),
4101 minit ("LINEAR", 4),
4102 minit ("ALIGNED", 5),
4103 minit ("LINEAR_REF", 33),
4104 minit ("LINEAR_VAL", 34),
4105 minit ("LINEAR_UVAL", 35),
4106 minit (NULL, -1)
4109 /* Handle !$omp declare simd. */
4111 static void
4112 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4114 if (iomode == IO_OUTPUT)
4116 if (*odsp == NULL)
4117 return;
4119 else if (peek_atom () != ATOM_LPAREN)
4120 return;
4122 gfc_omp_declare_simd *ods = *odsp;
4124 mio_lparen ();
4125 if (iomode == IO_OUTPUT)
4127 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4128 if (ods->clauses)
4130 gfc_omp_namelist *n;
4132 if (ods->clauses->inbranch)
4133 mio_name (0, omp_declare_simd_clauses);
4134 if (ods->clauses->notinbranch)
4135 mio_name (1, omp_declare_simd_clauses);
4136 if (ods->clauses->simdlen_expr)
4138 mio_name (2, omp_declare_simd_clauses);
4139 mio_expr (&ods->clauses->simdlen_expr);
4141 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4143 mio_name (3, omp_declare_simd_clauses);
4144 mio_symbol_ref (&n->sym);
4146 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4148 if (n->u.linear_op == OMP_LINEAR_DEFAULT)
4149 mio_name (4, omp_declare_simd_clauses);
4150 else
4151 mio_name (32 + n->u.linear_op, omp_declare_simd_clauses);
4152 mio_symbol_ref (&n->sym);
4153 mio_expr (&n->expr);
4155 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4157 mio_name (5, omp_declare_simd_clauses);
4158 mio_symbol_ref (&n->sym);
4159 mio_expr (&n->expr);
4163 else
4165 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4167 require_atom (ATOM_NAME);
4168 *odsp = ods = gfc_get_omp_declare_simd ();
4169 ods->where = gfc_current_locus;
4170 ods->proc_name = ns->proc_name;
4171 if (peek_atom () == ATOM_NAME)
4173 ods->clauses = gfc_get_omp_clauses ();
4174 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4175 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4176 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4178 while (peek_atom () == ATOM_NAME)
4180 gfc_omp_namelist *n;
4181 int t = mio_name (0, omp_declare_simd_clauses);
4183 switch (t)
4185 case 0: ods->clauses->inbranch = true; break;
4186 case 1: ods->clauses->notinbranch = true; break;
4187 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4188 case 3:
4189 case 4:
4190 case 5:
4191 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4192 finish_namelist:
4193 n->where = gfc_current_locus;
4194 ptrs[t - 3] = &n->next;
4195 mio_symbol_ref (&n->sym);
4196 if (t != 3)
4197 mio_expr (&n->expr);
4198 break;
4199 case 33:
4200 case 34:
4201 case 35:
4202 *ptrs[1] = n = gfc_get_omp_namelist ();
4203 n->u.linear_op = (enum gfc_omp_linear_op) (t - 32);
4204 t = 4;
4205 goto finish_namelist;
4210 mio_omp_declare_simd (ns, &ods->next);
4212 mio_rparen ();
4216 static const mstring omp_declare_reduction_stmt[] =
4218 minit ("ASSIGN", 0),
4219 minit ("CALL", 1),
4220 minit (NULL, -1)
4224 static void
4225 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4226 gfc_namespace *ns, bool is_initializer)
4228 if (iomode == IO_OUTPUT)
4230 if ((*sym1)->module == NULL)
4232 (*sym1)->module = module_name;
4233 (*sym2)->module = module_name;
4235 mio_symbol_ref (sym1);
4236 mio_symbol_ref (sym2);
4237 if (ns->code->op == EXEC_ASSIGN)
4239 mio_name (0, omp_declare_reduction_stmt);
4240 mio_expr (&ns->code->expr1);
4241 mio_expr (&ns->code->expr2);
4243 else
4245 int flag;
4246 mio_name (1, omp_declare_reduction_stmt);
4247 mio_symtree_ref (&ns->code->symtree);
4248 mio_actual_arglist (&ns->code->ext.actual, false);
4250 flag = ns->code->resolved_isym != NULL;
4251 mio_integer (&flag);
4252 if (flag)
4253 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4254 else
4255 mio_symbol_ref (&ns->code->resolved_sym);
4258 else
4260 pointer_info *p1 = mio_symbol_ref (sym1);
4261 pointer_info *p2 = mio_symbol_ref (sym2);
4262 gfc_symbol *sym;
4263 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4264 gcc_assert (p1->u.rsym.sym == NULL);
4265 /* Add hidden symbols to the symtree. */
4266 pointer_info *q = get_integer (p1->u.rsym.ns);
4267 q->u.pointer = (void *) ns;
4268 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4269 sym->ts = udr->ts;
4270 sym->module = gfc_get_string ("%s", p1->u.rsym.module);
4271 associate_integer_pointer (p1, sym);
4272 sym->attr.omp_udr_artificial_var = 1;
4273 gcc_assert (p2->u.rsym.sym == NULL);
4274 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4275 sym->ts = udr->ts;
4276 sym->module = gfc_get_string ("%s", p2->u.rsym.module);
4277 associate_integer_pointer (p2, sym);
4278 sym->attr.omp_udr_artificial_var = 1;
4279 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4281 ns->code = gfc_get_code (EXEC_ASSIGN);
4282 mio_expr (&ns->code->expr1);
4283 mio_expr (&ns->code->expr2);
4285 else
4287 int flag;
4288 ns->code = gfc_get_code (EXEC_CALL);
4289 mio_symtree_ref (&ns->code->symtree);
4290 mio_actual_arglist (&ns->code->ext.actual, false);
4292 mio_integer (&flag);
4293 if (flag)
4295 require_atom (ATOM_STRING);
4296 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4297 free (atom_string);
4299 else
4300 mio_symbol_ref (&ns->code->resolved_sym);
4302 ns->code->loc = gfc_current_locus;
4303 ns->omp_udr_ns = 1;
4308 /* Unlike most other routines, the address of the symbol node is already
4309 fixed on input and the name/module has already been filled in.
4310 If you update the symbol format here, don't forget to update read_module
4311 as well (look for "seek to the symbol's component list"). */
4313 static void
4314 mio_symbol (gfc_symbol *sym)
4316 int intmod = INTMOD_NONE;
4318 mio_lparen ();
4320 mio_symbol_attribute (&sym->attr);
4322 /* Note that components are always saved, even if they are supposed
4323 to be private. Component access is checked during searching. */
4324 mio_component_list (&sym->components, sym->attr.vtype);
4325 if (sym->components != NULL)
4326 sym->component_access
4327 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4329 mio_typespec (&sym->ts);
4330 if (sym->ts.type == BT_CLASS)
4331 sym->attr.class_ok = 1;
4333 if (iomode == IO_OUTPUT)
4334 mio_namespace_ref (&sym->formal_ns);
4335 else
4337 mio_namespace_ref (&sym->formal_ns);
4338 if (sym->formal_ns)
4339 sym->formal_ns->proc_name = sym;
4342 /* Save/restore common block links. */
4343 mio_symbol_ref (&sym->common_next);
4345 mio_formal_arglist (&sym->formal);
4347 if (sym->attr.flavor == FL_PARAMETER)
4348 mio_expr (&sym->value);
4350 mio_array_spec (&sym->as);
4352 mio_symbol_ref (&sym->result);
4354 if (sym->attr.cray_pointee)
4355 mio_symbol_ref (&sym->cp_pointer);
4357 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4358 mio_full_f2k_derived (sym);
4360 /* PDT types store the symbol specification list here. */
4361 mio_actual_arglist (&sym->param_list, true);
4363 mio_namelist (sym);
4365 /* Add the fields that say whether this is from an intrinsic module,
4366 and if so, what symbol it is within the module. */
4367 /* mio_integer (&(sym->from_intmod)); */
4368 if (iomode == IO_OUTPUT)
4370 intmod = sym->from_intmod;
4371 mio_integer (&intmod);
4373 else
4375 mio_integer (&intmod);
4376 if (current_intmod)
4377 sym->from_intmod = current_intmod;
4378 else
4379 sym->from_intmod = (intmod_id) intmod;
4382 mio_integer (&(sym->intmod_sym_id));
4384 if (gfc_fl_struct (sym->attr.flavor))
4385 mio_integer (&(sym->hash_value));
4387 if (sym->formal_ns
4388 && sym->formal_ns->proc_name == sym
4389 && sym->formal_ns->entries == NULL)
4390 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4392 mio_rparen ();
4396 /************************* Top level subroutines *************************/
4398 /* A recursive function to look for a specific symbol by name and by
4399 module. Whilst several symtrees might point to one symbol, its
4400 is sufficient for the purposes here than one exist. Note that
4401 generic interfaces are distinguished as are symbols that have been
4402 renamed in another module. */
4403 static gfc_symtree *
4404 find_symbol (gfc_symtree *st, const char *name,
4405 const char *module, int generic)
4407 int c;
4408 gfc_symtree *retval, *s;
4410 if (st == NULL || st->n.sym == NULL)
4411 return NULL;
4413 c = strcmp (name, st->n.sym->name);
4414 if (c == 0 && st->n.sym->module
4415 && strcmp (module, st->n.sym->module) == 0
4416 && !check_unique_name (st->name))
4418 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4420 /* Detect symbols that are renamed by use association in another
4421 module by the absence of a symtree and null attr.use_rename,
4422 since the latter is not transmitted in the module file. */
4423 if (((!generic && !st->n.sym->attr.generic)
4424 || (generic && st->n.sym->attr.generic))
4425 && !(s == NULL && !st->n.sym->attr.use_rename))
4426 return st;
4429 retval = find_symbol (st->left, name, module, generic);
4431 if (retval == NULL)
4432 retval = find_symbol (st->right, name, module, generic);
4434 return retval;
4438 /* Skip a list between balanced left and right parens.
4439 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4440 have been already parsed by hand, and the remaining of the content is to be
4441 skipped here. The default value is 0 (balanced parens). */
4443 static void
4444 skip_list (int nest_level = 0)
4446 int level;
4448 level = nest_level;
4451 switch (parse_atom ())
4453 case ATOM_LPAREN:
4454 level++;
4455 break;
4457 case ATOM_RPAREN:
4458 level--;
4459 break;
4461 case ATOM_STRING:
4462 free (atom_string);
4463 break;
4465 case ATOM_NAME:
4466 case ATOM_INTEGER:
4467 break;
4470 while (level > 0);
4474 /* Load operator interfaces from the module. Interfaces are unusual
4475 in that they attach themselves to existing symbols. */
4477 static void
4478 load_operator_interfaces (void)
4480 const char *p;
4481 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4482 gfc_user_op *uop;
4483 pointer_info *pi = NULL;
4484 int n, i;
4486 mio_lparen ();
4488 while (peek_atom () != ATOM_RPAREN)
4490 mio_lparen ();
4492 mio_internal_string (name);
4493 mio_internal_string (module);
4495 n = number_use_names (name, true);
4496 n = n ? n : 1;
4498 for (i = 1; i <= n; i++)
4500 /* Decide if we need to load this one or not. */
4501 p = find_use_name_n (name, &i, true);
4503 if (p == NULL)
4505 while (parse_atom () != ATOM_RPAREN);
4506 continue;
4509 if (i == 1)
4511 uop = gfc_get_uop (p);
4512 pi = mio_interface_rest (&uop->op);
4514 else
4516 if (gfc_find_uop (p, NULL))
4517 continue;
4518 uop = gfc_get_uop (p);
4519 uop->op = gfc_get_interface ();
4520 uop->op->where = gfc_current_locus;
4521 add_fixup (pi->integer, &uop->op->sym);
4526 mio_rparen ();
4530 /* Load interfaces from the module. Interfaces are unusual in that
4531 they attach themselves to existing symbols. */
4533 static void
4534 load_generic_interfaces (void)
4536 const char *p;
4537 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4538 gfc_symbol *sym;
4539 gfc_interface *generic = NULL, *gen = NULL;
4540 int n, i, renamed;
4541 bool ambiguous_set = false;
4543 mio_lparen ();
4545 while (peek_atom () != ATOM_RPAREN)
4547 mio_lparen ();
4549 mio_internal_string (name);
4550 mio_internal_string (module);
4552 n = number_use_names (name, false);
4553 renamed = n ? 1 : 0;
4554 n = n ? n : 1;
4556 for (i = 1; i <= n; i++)
4558 gfc_symtree *st;
4559 /* Decide if we need to load this one or not. */
4560 p = find_use_name_n (name, &i, false);
4562 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4564 /* Skip the specific names for these cases. */
4565 while (i == 1 && parse_atom () != ATOM_RPAREN);
4567 continue;
4570 st = find_symbol (gfc_current_ns->sym_root,
4571 name, module_name, 1);
4573 /* If the symbol exists already and is being USEd without being
4574 in an ONLY clause, do not load a new symtree(11.3.2). */
4575 if (!only_flag && st)
4576 sym = st->n.sym;
4578 if (!sym)
4580 if (st)
4582 sym = st->n.sym;
4583 if (strcmp (st->name, p) != 0)
4585 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4586 st->n.sym = sym;
4587 sym->refs++;
4591 /* Since we haven't found a valid generic interface, we had
4592 better make one. */
4593 if (!sym)
4595 gfc_get_symbol (p, NULL, &sym);
4596 sym->name = gfc_get_string ("%s", name);
4597 sym->module = module_name;
4598 sym->attr.flavor = FL_PROCEDURE;
4599 sym->attr.generic = 1;
4600 sym->attr.use_assoc = 1;
4603 else
4605 /* Unless sym is a generic interface, this reference
4606 is ambiguous. */
4607 if (st == NULL)
4608 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4610 sym = st->n.sym;
4612 if (st && !sym->attr.generic
4613 && !st->ambiguous
4614 && sym->module
4615 && strcmp (module, sym->module))
4617 ambiguous_set = true;
4618 st->ambiguous = 1;
4622 sym->attr.use_only = only_flag;
4623 sym->attr.use_rename = renamed;
4625 if (i == 1)
4627 mio_interface_rest (&sym->generic);
4628 generic = sym->generic;
4630 else if (!sym->generic)
4632 sym->generic = generic;
4633 sym->attr.generic_copy = 1;
4636 /* If a procedure that is not generic has generic interfaces
4637 that include itself, it is generic! We need to take care
4638 to retain symbols ambiguous that were already so. */
4639 if (sym->attr.use_assoc
4640 && !sym->attr.generic
4641 && sym->attr.flavor == FL_PROCEDURE)
4643 for (gen = generic; gen; gen = gen->next)
4645 if (gen->sym == sym)
4647 sym->attr.generic = 1;
4648 if (ambiguous_set)
4649 st->ambiguous = 0;
4650 break;
4658 mio_rparen ();
4662 /* Load common blocks. */
4664 static void
4665 load_commons (void)
4667 char name[GFC_MAX_SYMBOL_LEN + 1];
4668 gfc_common_head *p;
4670 mio_lparen ();
4672 while (peek_atom () != ATOM_RPAREN)
4674 int flags;
4675 char* label;
4676 mio_lparen ();
4677 mio_internal_string (name);
4679 p = gfc_get_common (name, 1);
4681 mio_symbol_ref (&p->head);
4682 mio_integer (&flags);
4683 if (flags & 1)
4684 p->saved = 1;
4685 if (flags & 2)
4686 p->threadprivate = 1;
4687 p->use_assoc = 1;
4689 /* Get whether this was a bind(c) common or not. */
4690 mio_integer (&p->is_bind_c);
4691 /* Get the binding label. */
4692 label = read_string ();
4693 if (strlen (label))
4694 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4695 XDELETEVEC (label);
4697 mio_rparen ();
4700 mio_rparen ();
4704 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4705 so that unused variables are not loaded and so that the expression can
4706 be safely freed. */
4708 static void
4709 load_equiv (void)
4711 gfc_equiv *head, *tail, *end, *eq, *equiv;
4712 bool duplicate;
4714 mio_lparen ();
4715 in_load_equiv = true;
4717 end = gfc_current_ns->equiv;
4718 while (end != NULL && end->next != NULL)
4719 end = end->next;
4721 while (peek_atom () != ATOM_RPAREN) {
4722 mio_lparen ();
4723 head = tail = NULL;
4725 while(peek_atom () != ATOM_RPAREN)
4727 if (head == NULL)
4728 head = tail = gfc_get_equiv ();
4729 else
4731 tail->eq = gfc_get_equiv ();
4732 tail = tail->eq;
4735 mio_pool_string (&tail->module);
4736 mio_expr (&tail->expr);
4739 /* Check for duplicate equivalences being loaded from different modules */
4740 duplicate = false;
4741 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
4743 if (equiv->module && head->module
4744 && strcmp (equiv->module, head->module) == 0)
4746 duplicate = true;
4747 break;
4751 if (duplicate)
4753 for (eq = head; eq; eq = head)
4755 head = eq->eq;
4756 gfc_free_expr (eq->expr);
4757 free (eq);
4761 if (end == NULL)
4762 gfc_current_ns->equiv = head;
4763 else
4764 end->next = head;
4766 if (head != NULL)
4767 end = head;
4769 mio_rparen ();
4772 mio_rparen ();
4773 in_load_equiv = false;
4777 /* This function loads OpenMP user defined reductions. */
4778 static void
4779 load_omp_udrs (void)
4781 mio_lparen ();
4782 while (peek_atom () != ATOM_RPAREN)
4784 const char *name = NULL, *newname;
4785 char *altname;
4786 gfc_typespec ts;
4787 gfc_symtree *st;
4788 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4790 mio_lparen ();
4791 mio_pool_string (&name);
4792 gfc_clear_ts (&ts);
4793 mio_typespec (&ts);
4794 if (gfc_str_startswith (name, "operator "))
4796 const char *p = name + sizeof ("operator ") - 1;
4797 if (strcmp (p, "+") == 0)
4798 rop = OMP_REDUCTION_PLUS;
4799 else if (strcmp (p, "*") == 0)
4800 rop = OMP_REDUCTION_TIMES;
4801 else if (strcmp (p, "-") == 0)
4802 rop = OMP_REDUCTION_MINUS;
4803 else if (strcmp (p, ".and.") == 0)
4804 rop = OMP_REDUCTION_AND;
4805 else if (strcmp (p, ".or.") == 0)
4806 rop = OMP_REDUCTION_OR;
4807 else if (strcmp (p, ".eqv.") == 0)
4808 rop = OMP_REDUCTION_EQV;
4809 else if (strcmp (p, ".neqv.") == 0)
4810 rop = OMP_REDUCTION_NEQV;
4812 altname = NULL;
4813 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4815 size_t len = strlen (name + 1);
4816 altname = XALLOCAVEC (char, len);
4817 gcc_assert (name[len] == '.');
4818 memcpy (altname, name + 1, len - 1);
4819 altname[len - 1] = '\0';
4821 newname = name;
4822 if (rop == OMP_REDUCTION_USER)
4823 newname = find_use_name (altname ? altname : name, !!altname);
4824 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4825 newname = NULL;
4826 if (newname == NULL)
4828 skip_list (1);
4829 continue;
4831 if (altname && newname != altname)
4833 size_t len = strlen (newname);
4834 altname = XALLOCAVEC (char, len + 3);
4835 altname[0] = '.';
4836 memcpy (altname + 1, newname, len);
4837 altname[len + 1] = '.';
4838 altname[len + 2] = '\0';
4839 name = gfc_get_string ("%s", altname);
4841 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4842 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4843 if (udr)
4845 require_atom (ATOM_INTEGER);
4846 pointer_info *p = get_integer (atom_int);
4847 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4849 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4850 "module %s at %L",
4851 p->u.rsym.module, &gfc_current_locus);
4852 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4853 "%s at %L",
4854 udr->omp_out->module, &udr->where);
4856 skip_list (1);
4857 continue;
4859 udr = gfc_get_omp_udr ();
4860 udr->name = name;
4861 udr->rop = rop;
4862 udr->ts = ts;
4863 udr->where = gfc_current_locus;
4864 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4865 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4866 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4867 false);
4868 if (peek_atom () != ATOM_RPAREN)
4870 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4871 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4872 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4873 udr->initializer_ns, true);
4875 if (st)
4877 udr->next = st->n.omp_udr;
4878 st->n.omp_udr = udr;
4880 else
4882 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4883 st->n.omp_udr = udr;
4885 mio_rparen ();
4887 mio_rparen ();
4891 /* Recursive function to traverse the pointer_info tree and load a
4892 needed symbol. We return nonzero if we load a symbol and stop the
4893 traversal, because the act of loading can alter the tree. */
4895 static int
4896 load_needed (pointer_info *p)
4898 gfc_namespace *ns;
4899 pointer_info *q;
4900 gfc_symbol *sym;
4901 int rv;
4903 rv = 0;
4904 if (p == NULL)
4905 return rv;
4907 rv |= load_needed (p->left);
4908 rv |= load_needed (p->right);
4910 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4911 return rv;
4913 p->u.rsym.state = USED;
4915 set_module_locus (&p->u.rsym.where);
4917 sym = p->u.rsym.sym;
4918 if (sym == NULL)
4920 q = get_integer (p->u.rsym.ns);
4922 ns = (gfc_namespace *) q->u.pointer;
4923 if (ns == NULL)
4925 /* Create an interface namespace if necessary. These are
4926 the namespaces that hold the formal parameters of module
4927 procedures. */
4929 ns = gfc_get_namespace (NULL, 0);
4930 associate_integer_pointer (q, ns);
4933 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4934 doesn't go pear-shaped if the symbol is used. */
4935 if (!ns->proc_name)
4936 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4937 1, &ns->proc_name);
4939 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4940 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
4941 sym->module = gfc_get_string ("%s", p->u.rsym.module);
4942 if (p->u.rsym.binding_label)
4943 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4944 (p->u.rsym.binding_label));
4946 associate_integer_pointer (p, sym);
4949 mio_symbol (sym);
4950 sym->attr.use_assoc = 1;
4952 /* Unliked derived types, a STRUCTURE may share names with other symbols.
4953 We greedily converted the the symbol name to lowercase before we knew its
4954 type, so now we must fix it. */
4955 if (sym->attr.flavor == FL_STRUCT)
4956 sym->name = gfc_dt_upper_string (sym->name);
4958 /* Mark as only or rename for later diagnosis for explicitly imported
4959 but not used warnings; don't mark internal symbols such as __vtab,
4960 __def_init etc. Only mark them if they have been explicitly loaded. */
4962 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4964 gfc_use_rename *u;
4966 /* Search the use/rename list for the variable; if the variable is
4967 found, mark it. */
4968 for (u = gfc_rename_list; u; u = u->next)
4970 if (strcmp (u->use_name, sym->name) == 0)
4972 sym->attr.use_only = 1;
4973 break;
4978 if (p->u.rsym.renamed)
4979 sym->attr.use_rename = 1;
4981 return 1;
4985 /* Recursive function for cleaning up things after a module has been read. */
4987 static void
4988 read_cleanup (pointer_info *p)
4990 gfc_symtree *st;
4991 pointer_info *q;
4993 if (p == NULL)
4994 return;
4996 read_cleanup (p->left);
4997 read_cleanup (p->right);
4999 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
5001 gfc_namespace *ns;
5002 /* Add hidden symbols to the symtree. */
5003 q = get_integer (p->u.rsym.ns);
5004 ns = (gfc_namespace *) q->u.pointer;
5006 if (!p->u.rsym.sym->attr.vtype
5007 && !p->u.rsym.sym->attr.vtab)
5008 st = gfc_get_unique_symtree (ns);
5009 else
5011 /* There is no reason to use 'unique_symtrees' for vtabs or
5012 vtypes - their name is fine for a symtree and reduces the
5013 namespace pollution. */
5014 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
5015 if (!st)
5016 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
5019 st->n.sym = p->u.rsym.sym;
5020 st->n.sym->refs++;
5022 /* Fixup any symtree references. */
5023 p->u.rsym.symtree = st;
5024 resolve_fixups (p->u.rsym.stfixup, st);
5025 p->u.rsym.stfixup = NULL;
5028 /* Free unused symbols. */
5029 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
5030 gfc_free_symbol (p->u.rsym.sym);
5034 /* It is not quite enough to check for ambiguity in the symbols by
5035 the loaded symbol and the new symbol not being identical. */
5036 static bool
5037 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
5039 gfc_symbol *rsym;
5040 module_locus locus;
5041 symbol_attribute attr;
5042 gfc_symbol *st_sym;
5044 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
5046 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
5047 "current program unit", st->name, module_name);
5048 return true;
5051 st_sym = st->n.sym;
5052 rsym = info->u.rsym.sym;
5053 if (st_sym == rsym)
5054 return false;
5056 if (st_sym->attr.vtab || st_sym->attr.vtype)
5057 return false;
5059 /* If the existing symbol is generic from a different module and
5060 the new symbol is generic there can be no ambiguity. */
5061 if (st_sym->attr.generic
5062 && st_sym->module
5063 && st_sym->module != module_name)
5065 /* The new symbol's attributes have not yet been read. Since
5066 we need attr.generic, read it directly. */
5067 get_module_locus (&locus);
5068 set_module_locus (&info->u.rsym.where);
5069 mio_lparen ();
5070 attr.generic = 0;
5071 mio_symbol_attribute (&attr);
5072 set_module_locus (&locus);
5073 if (attr.generic)
5074 return false;
5077 return true;
5081 /* Read a module file. */
5083 static void
5084 read_module (void)
5086 module_locus operator_interfaces, user_operators, omp_udrs;
5087 const char *p;
5088 char name[GFC_MAX_SYMBOL_LEN + 1];
5089 int i;
5090 /* Workaround -Wmaybe-uninitialized false positive during
5091 profiledbootstrap by initializing them. */
5092 int ambiguous = 0, j, nuse, symbol = 0;
5093 pointer_info *info, *q;
5094 gfc_use_rename *u = NULL;
5095 gfc_symtree *st;
5096 gfc_symbol *sym;
5098 get_module_locus (&operator_interfaces); /* Skip these for now. */
5099 skip_list ();
5101 get_module_locus (&user_operators);
5102 skip_list ();
5103 skip_list ();
5105 /* Skip commons and equivalences for now. */
5106 skip_list ();
5107 skip_list ();
5109 /* Skip OpenMP UDRs. */
5110 get_module_locus (&omp_udrs);
5111 skip_list ();
5113 mio_lparen ();
5115 /* Create the fixup nodes for all the symbols. */
5117 while (peek_atom () != ATOM_RPAREN)
5119 char* bind_label;
5120 require_atom (ATOM_INTEGER);
5121 info = get_integer (atom_int);
5123 info->type = P_SYMBOL;
5124 info->u.rsym.state = UNUSED;
5126 info->u.rsym.true_name = read_string ();
5127 info->u.rsym.module = read_string ();
5128 bind_label = read_string ();
5129 if (strlen (bind_label))
5130 info->u.rsym.binding_label = bind_label;
5131 else
5132 XDELETEVEC (bind_label);
5134 require_atom (ATOM_INTEGER);
5135 info->u.rsym.ns = atom_int;
5137 get_module_locus (&info->u.rsym.where);
5139 /* See if the symbol has already been loaded by a previous module.
5140 If so, we reference the existing symbol and prevent it from
5141 being loaded again. This should not happen if the symbol being
5142 read is an index for an assumed shape dummy array (ns != 1). */
5144 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5146 if (sym == NULL
5147 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5149 skip_list ();
5150 continue;
5153 info->u.rsym.state = USED;
5154 info->u.rsym.sym = sym;
5155 /* The current symbol has already been loaded, so we can avoid loading
5156 it again. However, if it is a derived type, some of its components
5157 can be used in expressions in the module. To avoid the module loading
5158 failing, we need to associate the module's component pointer indexes
5159 with the existing symbol's component pointers. */
5160 if (gfc_fl_struct (sym->attr.flavor))
5162 gfc_component *c;
5164 /* First seek to the symbol's component list. */
5165 mio_lparen (); /* symbol opening. */
5166 skip_list (); /* skip symbol attribute. */
5168 mio_lparen (); /* component list opening. */
5169 for (c = sym->components; c; c = c->next)
5171 pointer_info *p;
5172 const char *comp_name;
5173 int n;
5175 mio_lparen (); /* component opening. */
5176 mio_integer (&n);
5177 p = get_integer (n);
5178 if (p->u.pointer == NULL)
5179 associate_integer_pointer (p, c);
5180 mio_pool_string (&comp_name);
5181 gcc_assert (comp_name == c->name);
5182 skip_list (1); /* component end. */
5184 mio_rparen (); /* component list closing. */
5186 skip_list (1); /* symbol end. */
5188 else
5189 skip_list ();
5191 /* Some symbols do not have a namespace (eg. formal arguments),
5192 so the automatic "unique symtree" mechanism must be suppressed
5193 by marking them as referenced. */
5194 q = get_integer (info->u.rsym.ns);
5195 if (q->u.pointer == NULL)
5197 info->u.rsym.referenced = 1;
5198 continue;
5202 mio_rparen ();
5204 /* Parse the symtree lists. This lets us mark which symbols need to
5205 be loaded. Renaming is also done at this point by replacing the
5206 symtree name. */
5208 mio_lparen ();
5210 while (peek_atom () != ATOM_RPAREN)
5212 mio_internal_string (name);
5213 mio_integer (&ambiguous);
5214 mio_integer (&symbol);
5216 info = get_integer (symbol);
5218 /* See how many use names there are. If none, go through the start
5219 of the loop at least once. */
5220 nuse = number_use_names (name, false);
5221 info->u.rsym.renamed = nuse ? 1 : 0;
5223 if (nuse == 0)
5224 nuse = 1;
5226 for (j = 1; j <= nuse; j++)
5228 /* Get the jth local name for this symbol. */
5229 p = find_use_name_n (name, &j, false);
5231 if (p == NULL && strcmp (name, module_name) == 0)
5232 p = name;
5234 /* Exception: Always import vtabs & vtypes. */
5235 if (p == NULL && name[0] == '_'
5236 && (gfc_str_startswith (name, "__vtab_")
5237 || gfc_str_startswith (name, "__vtype_")))
5238 p = name;
5240 /* Skip symtree nodes not in an ONLY clause, unless there
5241 is an existing symtree loaded from another USE statement. */
5242 if (p == NULL)
5244 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5245 if (st != NULL
5246 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5247 && st->n.sym->module != NULL
5248 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5250 info->u.rsym.symtree = st;
5251 info->u.rsym.sym = st->n.sym;
5253 continue;
5256 /* If a symbol of the same name and module exists already,
5257 this symbol, which is not in an ONLY clause, must not be
5258 added to the namespace(11.3.2). Note that find_symbol
5259 only returns the first occurrence that it finds. */
5260 if (!only_flag && !info->u.rsym.renamed
5261 && strcmp (name, module_name) != 0
5262 && find_symbol (gfc_current_ns->sym_root, name,
5263 module_name, 0))
5264 continue;
5266 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5268 if (st != NULL
5269 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5271 /* Check for ambiguous symbols. */
5272 if (check_for_ambiguous (st, info))
5273 st->ambiguous = 1;
5274 else
5275 info->u.rsym.symtree = st;
5277 else
5279 if (st)
5281 /* This symbol is host associated from a module in a
5282 submodule. Hide it with a unique symtree. */
5283 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5284 s->n.sym = st->n.sym;
5285 st->n.sym = NULL;
5287 else
5289 /* Create a symtree node in the current namespace for this
5290 symbol. */
5291 st = check_unique_name (p)
5292 ? gfc_get_unique_symtree (gfc_current_ns)
5293 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5294 st->ambiguous = ambiguous;
5297 sym = info->u.rsym.sym;
5299 /* Create a symbol node if it doesn't already exist. */
5300 if (sym == NULL)
5302 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5303 gfc_current_ns);
5304 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5305 sym = info->u.rsym.sym;
5306 sym->module = gfc_get_string ("%s", info->u.rsym.module);
5308 if (info->u.rsym.binding_label)
5310 tree id = get_identifier (info->u.rsym.binding_label);
5311 sym->binding_label = IDENTIFIER_POINTER (id);
5315 st->n.sym = sym;
5316 st->n.sym->refs++;
5318 if (strcmp (name, p) != 0)
5319 sym->attr.use_rename = 1;
5321 if (name[0] != '_'
5322 || (!gfc_str_startswith (name, "__vtab_")
5323 && !gfc_str_startswith (name, "__vtype_")))
5324 sym->attr.use_only = only_flag;
5326 /* Store the symtree pointing to this symbol. */
5327 info->u.rsym.symtree = st;
5329 if (info->u.rsym.state == UNUSED)
5330 info->u.rsym.state = NEEDED;
5331 info->u.rsym.referenced = 1;
5336 mio_rparen ();
5338 /* Load intrinsic operator interfaces. */
5339 set_module_locus (&operator_interfaces);
5340 mio_lparen ();
5342 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5344 if (i == INTRINSIC_USER)
5345 continue;
5347 if (only_flag)
5349 u = find_use_operator ((gfc_intrinsic_op) i);
5351 if (u == NULL)
5353 skip_list ();
5354 continue;
5357 u->found = 1;
5360 mio_interface (&gfc_current_ns->op[i]);
5361 if (u && !gfc_current_ns->op[i])
5362 u->found = 0;
5365 mio_rparen ();
5367 /* Load generic and user operator interfaces. These must follow the
5368 loading of symtree because otherwise symbols can be marked as
5369 ambiguous. */
5371 set_module_locus (&user_operators);
5373 load_operator_interfaces ();
5374 load_generic_interfaces ();
5376 load_commons ();
5377 load_equiv ();
5379 /* Load OpenMP user defined reductions. */
5380 set_module_locus (&omp_udrs);
5381 load_omp_udrs ();
5383 /* At this point, we read those symbols that are needed but haven't
5384 been loaded yet. If one symbol requires another, the other gets
5385 marked as NEEDED if its previous state was UNUSED. */
5387 while (load_needed (pi_root));
5389 /* Make sure all elements of the rename-list were found in the module. */
5391 for (u = gfc_rename_list; u; u = u->next)
5393 if (u->found)
5394 continue;
5396 if (u->op == INTRINSIC_NONE)
5398 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5399 u->use_name, &u->where, module_name);
5400 continue;
5403 if (u->op == INTRINSIC_USER)
5405 gfc_error ("User operator %qs referenced at %L not found "
5406 "in module %qs", u->use_name, &u->where, module_name);
5407 continue;
5410 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5411 "in module %qs", gfc_op2string (u->op), &u->where,
5412 module_name);
5415 /* Clean up symbol nodes that were never loaded, create references
5416 to hidden symbols. */
5418 read_cleanup (pi_root);
5422 /* Given an access type that is specific to an entity and the default
5423 access, return nonzero if the entity is publicly accessible. If the
5424 element is declared as PUBLIC, then it is public; if declared
5425 PRIVATE, then private, and otherwise it is public unless the default
5426 access in this context has been declared PRIVATE. */
5428 static bool dump_smod = false;
5430 static bool
5431 check_access (gfc_access specific_access, gfc_access default_access)
5433 if (dump_smod)
5434 return true;
5436 if (specific_access == ACCESS_PUBLIC)
5437 return TRUE;
5438 if (specific_access == ACCESS_PRIVATE)
5439 return FALSE;
5441 if (flag_module_private)
5442 return default_access == ACCESS_PUBLIC;
5443 else
5444 return default_access != ACCESS_PRIVATE;
5448 bool
5449 gfc_check_symbol_access (gfc_symbol *sym)
5451 if (sym->attr.vtab || sym->attr.vtype)
5452 return true;
5453 else
5454 return check_access (sym->attr.access, sym->ns->default_access);
5458 /* A structure to remember which commons we've already written. */
5460 struct written_common
5462 BBT_HEADER(written_common);
5463 const char *name, *label;
5466 static struct written_common *written_commons = NULL;
5468 /* Comparison function used for balancing the binary tree. */
5470 static int
5471 compare_written_commons (void *a1, void *b1)
5473 const char *aname = ((struct written_common *) a1)->name;
5474 const char *alabel = ((struct written_common *) a1)->label;
5475 const char *bname = ((struct written_common *) b1)->name;
5476 const char *blabel = ((struct written_common *) b1)->label;
5477 int c = strcmp (aname, bname);
5479 return (c != 0 ? c : strcmp (alabel, blabel));
5482 /* Free a list of written commons. */
5484 static void
5485 free_written_common (struct written_common *w)
5487 if (!w)
5488 return;
5490 if (w->left)
5491 free_written_common (w->left);
5492 if (w->right)
5493 free_written_common (w->right);
5495 free (w);
5498 /* Write a common block to the module -- recursive helper function. */
5500 static void
5501 write_common_0 (gfc_symtree *st, bool this_module)
5503 gfc_common_head *p;
5504 const char * name;
5505 int flags;
5506 const char *label;
5507 struct written_common *w;
5508 bool write_me = true;
5510 if (st == NULL)
5511 return;
5513 write_common_0 (st->left, this_module);
5515 /* We will write out the binding label, or "" if no label given. */
5516 name = st->n.common->name;
5517 p = st->n.common;
5518 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5520 /* Check if we've already output this common. */
5521 w = written_commons;
5522 while (w)
5524 int c = strcmp (name, w->name);
5525 c = (c != 0 ? c : strcmp (label, w->label));
5526 if (c == 0)
5527 write_me = false;
5529 w = (c < 0) ? w->left : w->right;
5532 if (this_module && p->use_assoc)
5533 write_me = false;
5535 if (write_me)
5537 /* Write the common to the module. */
5538 mio_lparen ();
5539 mio_pool_string (&name);
5541 mio_symbol_ref (&p->head);
5542 flags = p->saved ? 1 : 0;
5543 if (p->threadprivate)
5544 flags |= 2;
5545 mio_integer (&flags);
5547 /* Write out whether the common block is bind(c) or not. */
5548 mio_integer (&(p->is_bind_c));
5550 mio_pool_string (&label);
5551 mio_rparen ();
5553 /* Record that we have written this common. */
5554 w = XCNEW (struct written_common);
5555 w->name = p->name;
5556 w->label = label;
5557 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5560 write_common_0 (st->right, this_module);
5564 /* Write a common, by initializing the list of written commons, calling
5565 the recursive function write_common_0() and cleaning up afterwards. */
5567 static void
5568 write_common (gfc_symtree *st)
5570 written_commons = NULL;
5571 write_common_0 (st, true);
5572 write_common_0 (st, false);
5573 free_written_common (written_commons);
5574 written_commons = NULL;
5578 /* Write the blank common block to the module. */
5580 static void
5581 write_blank_common (void)
5583 const char * name = BLANK_COMMON_NAME;
5584 int saved;
5585 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5586 this, but it hasn't been checked. Just making it so for now. */
5587 int is_bind_c = 0;
5589 if (gfc_current_ns->blank_common.head == NULL)
5590 return;
5592 mio_lparen ();
5594 mio_pool_string (&name);
5596 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5597 saved = gfc_current_ns->blank_common.saved;
5598 mio_integer (&saved);
5600 /* Write out whether the common block is bind(c) or not. */
5601 mio_integer (&is_bind_c);
5603 /* Write out an empty binding label. */
5604 write_atom (ATOM_STRING, "");
5606 mio_rparen ();
5610 /* Write equivalences to the module. */
5612 static void
5613 write_equiv (void)
5615 gfc_equiv *eq, *e;
5616 int num;
5618 num = 0;
5619 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5621 mio_lparen ();
5623 for (e = eq; e; e = e->eq)
5625 if (e->module == NULL)
5626 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5627 mio_allocated_string (e->module);
5628 mio_expr (&e->expr);
5631 num++;
5632 mio_rparen ();
5637 /* Write a symbol to the module. */
5639 static void
5640 write_symbol (int n, gfc_symbol *sym)
5642 const char *label;
5644 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5645 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5647 mio_integer (&n);
5649 if (gfc_fl_struct (sym->attr.flavor))
5651 const char *name;
5652 name = gfc_dt_upper_string (sym->name);
5653 mio_pool_string (&name);
5655 else
5656 mio_pool_string (&sym->name);
5658 mio_pool_string (&sym->module);
5659 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5661 label = sym->binding_label;
5662 mio_pool_string (&label);
5664 else
5665 write_atom (ATOM_STRING, "");
5667 mio_pointer_ref (&sym->ns);
5669 mio_symbol (sym);
5670 write_char ('\n');
5674 /* Recursive traversal function to write the initial set of symbols to
5675 the module. We check to see if the symbol should be written
5676 according to the access specification. */
5678 static void
5679 write_symbol0 (gfc_symtree *st)
5681 gfc_symbol *sym;
5682 pointer_info *p;
5683 bool dont_write = false;
5685 if (st == NULL)
5686 return;
5688 write_symbol0 (st->left);
5690 sym = st->n.sym;
5691 if (sym->module == NULL)
5692 sym->module = module_name;
5694 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5695 && !sym->attr.subroutine && !sym->attr.function)
5696 dont_write = true;
5698 if (!gfc_check_symbol_access (sym))
5699 dont_write = true;
5701 if (!dont_write)
5703 p = get_pointer (sym);
5704 if (p->type == P_UNKNOWN)
5705 p->type = P_SYMBOL;
5707 if (p->u.wsym.state != WRITTEN)
5709 write_symbol (p->integer, sym);
5710 p->u.wsym.state = WRITTEN;
5714 write_symbol0 (st->right);
5718 static void
5719 write_omp_udr (gfc_omp_udr *udr)
5721 switch (udr->rop)
5723 case OMP_REDUCTION_USER:
5724 /* Non-operators can't be used outside of the module. */
5725 if (udr->name[0] != '.')
5726 return;
5727 else
5729 gfc_symtree *st;
5730 size_t len = strlen (udr->name + 1);
5731 char *name = XALLOCAVEC (char, len);
5732 memcpy (name, udr->name, len - 1);
5733 name[len - 1] = '\0';
5734 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5735 /* If corresponding user operator is private, don't write
5736 the UDR. */
5737 if (st != NULL)
5739 gfc_user_op *uop = st->n.uop;
5740 if (!check_access (uop->access, uop->ns->default_access))
5741 return;
5744 break;
5745 case OMP_REDUCTION_PLUS:
5746 case OMP_REDUCTION_MINUS:
5747 case OMP_REDUCTION_TIMES:
5748 case OMP_REDUCTION_AND:
5749 case OMP_REDUCTION_OR:
5750 case OMP_REDUCTION_EQV:
5751 case OMP_REDUCTION_NEQV:
5752 /* If corresponding operator is private, don't write the UDR. */
5753 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5754 gfc_current_ns->default_access))
5755 return;
5756 break;
5757 default:
5758 break;
5760 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5762 /* If derived type is private, don't write the UDR. */
5763 if (!gfc_check_symbol_access (udr->ts.u.derived))
5764 return;
5767 mio_lparen ();
5768 mio_pool_string (&udr->name);
5769 mio_typespec (&udr->ts);
5770 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5771 if (udr->initializer_ns)
5772 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5773 udr->initializer_ns, true);
5774 mio_rparen ();
5778 static void
5779 write_omp_udrs (gfc_symtree *st)
5781 if (st == NULL)
5782 return;
5784 write_omp_udrs (st->left);
5785 gfc_omp_udr *udr;
5786 for (udr = st->n.omp_udr; udr; udr = udr->next)
5787 write_omp_udr (udr);
5788 write_omp_udrs (st->right);
5792 /* Type for the temporary tree used when writing secondary symbols. */
5794 struct sorted_pointer_info
5796 BBT_HEADER (sorted_pointer_info);
5798 pointer_info *p;
5801 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5803 /* Recursively traverse the temporary tree, free its contents. */
5805 static void
5806 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5808 if (!p)
5809 return;
5811 free_sorted_pointer_info_tree (p->left);
5812 free_sorted_pointer_info_tree (p->right);
5814 free (p);
5817 /* Comparison function for the temporary tree. */
5819 static int
5820 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5822 sorted_pointer_info *spi1, *spi2;
5823 spi1 = (sorted_pointer_info *)_spi1;
5824 spi2 = (sorted_pointer_info *)_spi2;
5826 if (spi1->p->integer < spi2->p->integer)
5827 return -1;
5828 if (spi1->p->integer > spi2->p->integer)
5829 return 1;
5830 return 0;
5834 /* Finds the symbols that need to be written and collects them in the
5835 sorted_pi tree so that they can be traversed in an order
5836 independent of memory addresses. */
5838 static void
5839 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5841 if (!p)
5842 return;
5844 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5846 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5847 sp->p = p;
5849 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5852 find_symbols_to_write (tree, p->left);
5853 find_symbols_to_write (tree, p->right);
5857 /* Recursive function that traverses the tree of symbols that need to be
5858 written and writes them in order. */
5860 static void
5861 write_symbol1_recursion (sorted_pointer_info *sp)
5863 if (!sp)
5864 return;
5866 write_symbol1_recursion (sp->left);
5868 pointer_info *p1 = sp->p;
5869 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5871 p1->u.wsym.state = WRITTEN;
5872 write_symbol (p1->integer, p1->u.wsym.sym);
5873 p1->u.wsym.sym->attr.public_used = 1;
5875 write_symbol1_recursion (sp->right);
5879 /* Write the secondary set of symbols to the module file. These are
5880 symbols that were not public yet are needed by the public symbols
5881 or another dependent symbol. The act of writing a symbol can add
5882 symbols to the pointer_info tree, so we return nonzero if a symbol
5883 was written and pass that information upwards. The caller will
5884 then call this function again until nothing was written. It uses
5885 the utility functions and a temporary tree to ensure a reproducible
5886 ordering of the symbol output and thus the module file. */
5888 static int
5889 write_symbol1 (pointer_info *p)
5891 if (!p)
5892 return 0;
5894 /* Put symbols that need to be written into a tree sorted on the
5895 integer field. */
5897 sorted_pointer_info *spi_root = NULL;
5898 find_symbols_to_write (&spi_root, p);
5900 /* No symbols to write, return. */
5901 if (!spi_root)
5902 return 0;
5904 /* Otherwise, write and free the tree again. */
5905 write_symbol1_recursion (spi_root);
5906 free_sorted_pointer_info_tree (spi_root);
5908 return 1;
5912 /* Write operator interfaces associated with a symbol. */
5914 static void
5915 write_operator (gfc_user_op *uop)
5917 static char nullstring[] = "";
5918 const char *p = nullstring;
5920 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5921 return;
5923 mio_symbol_interface (&uop->name, &p, &uop->op);
5927 /* Write generic interfaces from the namespace sym_root. */
5929 static void
5930 write_generic (gfc_symtree *st)
5932 gfc_symbol *sym;
5934 if (st == NULL)
5935 return;
5937 write_generic (st->left);
5939 sym = st->n.sym;
5940 if (sym && !check_unique_name (st->name)
5941 && sym->generic && gfc_check_symbol_access (sym))
5943 if (!sym->module)
5944 sym->module = module_name;
5946 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5949 write_generic (st->right);
5953 static void
5954 write_symtree (gfc_symtree *st)
5956 gfc_symbol *sym;
5957 pointer_info *p;
5959 sym = st->n.sym;
5961 /* A symbol in an interface body must not be visible in the
5962 module file. */
5963 if (sym->ns != gfc_current_ns
5964 && sym->ns->proc_name
5965 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5966 return;
5968 if (!gfc_check_symbol_access (sym)
5969 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5970 && !sym->attr.subroutine && !sym->attr.function))
5971 return;
5973 if (check_unique_name (st->name))
5974 return;
5976 p = find_pointer (sym);
5977 if (p == NULL)
5978 gfc_internal_error ("write_symtree(): Symbol not written");
5980 mio_pool_string (&st->name);
5981 mio_integer (&st->ambiguous);
5982 mio_hwi (&p->integer);
5986 static void
5987 write_module (void)
5989 int i;
5991 /* Write the operator interfaces. */
5992 mio_lparen ();
5994 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5996 if (i == INTRINSIC_USER)
5997 continue;
5999 mio_interface (check_access (gfc_current_ns->operator_access[i],
6000 gfc_current_ns->default_access)
6001 ? &gfc_current_ns->op[i] : NULL);
6004 mio_rparen ();
6005 write_char ('\n');
6006 write_char ('\n');
6008 mio_lparen ();
6009 gfc_traverse_user_op (gfc_current_ns, write_operator);
6010 mio_rparen ();
6011 write_char ('\n');
6012 write_char ('\n');
6014 mio_lparen ();
6015 write_generic (gfc_current_ns->sym_root);
6016 mio_rparen ();
6017 write_char ('\n');
6018 write_char ('\n');
6020 mio_lparen ();
6021 write_blank_common ();
6022 write_common (gfc_current_ns->common_root);
6023 mio_rparen ();
6024 write_char ('\n');
6025 write_char ('\n');
6027 mio_lparen ();
6028 write_equiv ();
6029 mio_rparen ();
6030 write_char ('\n');
6031 write_char ('\n');
6033 mio_lparen ();
6034 write_omp_udrs (gfc_current_ns->omp_udr_root);
6035 mio_rparen ();
6036 write_char ('\n');
6037 write_char ('\n');
6039 /* Write symbol information. First we traverse all symbols in the
6040 primary namespace, writing those that need to be written.
6041 Sometimes writing one symbol will cause another to need to be
6042 written. A list of these symbols ends up on the write stack, and
6043 we end by popping the bottom of the stack and writing the symbol
6044 until the stack is empty. */
6046 mio_lparen ();
6048 write_symbol0 (gfc_current_ns->sym_root);
6049 while (write_symbol1 (pi_root))
6050 /* Nothing. */;
6052 mio_rparen ();
6054 write_char ('\n');
6055 write_char ('\n');
6057 mio_lparen ();
6058 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
6059 mio_rparen ();
6063 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
6064 true on success, false on failure. */
6066 static bool
6067 read_crc32_from_module_file (const char* filename, uLong* crc)
6069 FILE *file;
6070 char buf[4];
6071 unsigned int val;
6073 /* Open the file in binary mode. */
6074 if ((file = fopen (filename, "rb")) == NULL)
6075 return false;
6077 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
6078 file. See RFC 1952. */
6079 if (fseek (file, -8, SEEK_END) != 0)
6081 fclose (file);
6082 return false;
6085 /* Read the CRC32. */
6086 if (fread (buf, 1, 4, file) != 4)
6088 fclose (file);
6089 return false;
6092 /* Close the file. */
6093 fclose (file);
6095 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6096 + ((buf[3] & 0xFF) << 24);
6097 *crc = val;
6099 /* For debugging, the CRC value printed in hexadecimal should match
6100 the CRC printed by "zcat -l -v filename".
6101 printf("CRC of file %s is %x\n", filename, val); */
6103 return true;
6107 /* Given module, dump it to disk. If there was an error while
6108 processing the module, dump_flag will be set to zero and we delete
6109 the module file, even if it was already there. */
6111 static void
6112 dump_module (const char *name, int dump_flag)
6114 int n;
6115 char *filename, *filename_tmp;
6116 uLong crc, crc_old;
6118 module_name = gfc_get_string ("%s", name);
6120 if (dump_smod)
6122 name = submodule_name;
6123 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6125 else
6126 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6128 if (gfc_option.module_dir != NULL)
6130 n += strlen (gfc_option.module_dir);
6131 filename = (char *) alloca (n);
6132 strcpy (filename, gfc_option.module_dir);
6133 strcat (filename, name);
6135 else
6137 filename = (char *) alloca (n);
6138 strcpy (filename, name);
6141 if (dump_smod)
6142 strcat (filename, SUBMODULE_EXTENSION);
6143 else
6144 strcat (filename, MODULE_EXTENSION);
6146 /* Name of the temporary file used to write the module. */
6147 filename_tmp = (char *) alloca (n + 1);
6148 strcpy (filename_tmp, filename);
6149 strcat (filename_tmp, "0");
6151 /* There was an error while processing the module. We delete the
6152 module file, even if it was already there. */
6153 if (!dump_flag)
6155 remove (filename);
6156 return;
6159 if (gfc_cpp_makedep ())
6160 gfc_cpp_add_target (filename);
6162 /* Write the module to the temporary file. */
6163 module_fp = gzopen (filename_tmp, "w");
6164 if (module_fp == NULL)
6165 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6166 filename_tmp, xstrerror (errno));
6168 /* Use lbasename to ensure module files are reproducible regardless
6169 of the build path (see the reproducible builds project). */
6170 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6171 MOD_VERSION, lbasename (gfc_source_file));
6173 /* Write the module itself. */
6174 iomode = IO_OUTPUT;
6176 init_pi_tree ();
6178 write_module ();
6180 free_pi_tree (pi_root);
6181 pi_root = NULL;
6183 write_char ('\n');
6185 if (gzclose (module_fp))
6186 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6187 filename_tmp, xstrerror (errno));
6189 /* Read the CRC32 from the gzip trailers of the module files and
6190 compare. */
6191 if (!read_crc32_from_module_file (filename_tmp, &crc)
6192 || !read_crc32_from_module_file (filename, &crc_old)
6193 || crc_old != crc)
6195 /* Module file have changed, replace the old one. */
6196 if (remove (filename) && errno != ENOENT)
6197 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6198 xstrerror (errno));
6199 if (rename (filename_tmp, filename))
6200 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6201 filename_tmp, filename, xstrerror (errno));
6203 else
6205 if (remove (filename_tmp))
6206 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6207 filename_tmp, xstrerror (errno));
6212 /* Suppress the output of a .smod file by module, if no module
6213 procedures have been seen. */
6214 static bool no_module_procedures;
6216 static void
6217 check_for_module_procedures (gfc_symbol *sym)
6219 if (sym && sym->attr.module_procedure)
6220 no_module_procedures = false;
6224 void
6225 gfc_dump_module (const char *name, int dump_flag)
6227 if (gfc_state_stack->state == COMP_SUBMODULE)
6228 dump_smod = true;
6229 else
6230 dump_smod =false;
6232 no_module_procedures = true;
6233 gfc_traverse_ns (gfc_current_ns, check_for_module_procedures);
6235 dump_module (name, dump_flag);
6237 if (no_module_procedures || dump_smod)
6238 return;
6240 /* Write a submodule file from a module. The 'dump_smod' flag switches
6241 off the check for PRIVATE entities. */
6242 dump_smod = true;
6243 submodule_name = module_name;
6244 dump_module (name, dump_flag);
6245 dump_smod = false;
6248 static void
6249 create_intrinsic_function (const char *name, int id,
6250 const char *modname, intmod_id module,
6251 bool subroutine, gfc_symbol *result_type)
6253 gfc_intrinsic_sym *isym;
6254 gfc_symtree *tmp_symtree;
6255 gfc_symbol *sym;
6257 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6258 if (tmp_symtree)
6260 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6261 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6262 return;
6263 gfc_error ("Symbol %qs at %C already declared", name);
6264 return;
6267 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6268 sym = tmp_symtree->n.sym;
6270 if (subroutine)
6272 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6273 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6274 sym->attr.subroutine = 1;
6276 else
6278 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6279 isym = gfc_intrinsic_function_by_id (isym_id);
6281 sym->attr.function = 1;
6282 if (result_type)
6284 sym->ts.type = BT_DERIVED;
6285 sym->ts.u.derived = result_type;
6286 sym->ts.is_c_interop = 1;
6287 isym->ts.f90_type = BT_VOID;
6288 isym->ts.type = BT_DERIVED;
6289 isym->ts.f90_type = BT_VOID;
6290 isym->ts.u.derived = result_type;
6291 isym->ts.is_c_interop = 1;
6294 gcc_assert (isym);
6296 sym->attr.flavor = FL_PROCEDURE;
6297 sym->attr.intrinsic = 1;
6299 sym->module = gfc_get_string ("%s", modname);
6300 sym->attr.use_assoc = 1;
6301 sym->from_intmod = module;
6302 sym->intmod_sym_id = id;
6306 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6307 the current namespace for all named constants, pointer types, and
6308 procedures in the module unless the only clause was used or a rename
6309 list was provided. */
6311 static void
6312 import_iso_c_binding_module (void)
6314 gfc_symbol *mod_sym = NULL, *return_type;
6315 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6316 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6317 const char *iso_c_module_name = "__iso_c_binding";
6318 gfc_use_rename *u;
6319 int i;
6320 bool want_c_ptr = false, want_c_funptr = false;
6322 /* Look only in the current namespace. */
6323 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6325 if (mod_symtree == NULL)
6327 /* symtree doesn't already exist in current namespace. */
6328 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6329 false);
6331 if (mod_symtree != NULL)
6332 mod_sym = mod_symtree->n.sym;
6333 else
6334 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6335 "create symbol for %s", iso_c_module_name);
6337 mod_sym->attr.flavor = FL_MODULE;
6338 mod_sym->attr.intrinsic = 1;
6339 mod_sym->module = gfc_get_string ("%s", iso_c_module_name);
6340 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6343 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6344 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6345 need C_(FUN)PTR. */
6346 for (u = gfc_rename_list; u; u = u->next)
6348 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6349 u->use_name) == 0)
6350 want_c_ptr = true;
6351 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6352 u->use_name) == 0)
6353 want_c_ptr = true;
6354 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6355 u->use_name) == 0)
6356 want_c_funptr = true;
6357 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6358 u->use_name) == 0)
6359 want_c_funptr = true;
6360 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6361 u->use_name) == 0)
6363 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6364 (iso_c_binding_symbol)
6365 ISOCBINDING_PTR,
6366 u->local_name[0] ? u->local_name
6367 : u->use_name,
6368 NULL, false);
6370 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6371 u->use_name) == 0)
6373 c_funptr
6374 = generate_isocbinding_symbol (iso_c_module_name,
6375 (iso_c_binding_symbol)
6376 ISOCBINDING_FUNPTR,
6377 u->local_name[0] ? u->local_name
6378 : u->use_name,
6379 NULL, false);
6383 if ((want_c_ptr || !only_flag) && !c_ptr)
6384 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6385 (iso_c_binding_symbol)
6386 ISOCBINDING_PTR,
6387 NULL, NULL, only_flag);
6388 if ((want_c_funptr || !only_flag) && !c_funptr)
6389 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6390 (iso_c_binding_symbol)
6391 ISOCBINDING_FUNPTR,
6392 NULL, NULL, only_flag);
6394 /* Generate the symbols for the named constants representing
6395 the kinds for intrinsic data types. */
6396 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6398 bool found = false;
6399 for (u = gfc_rename_list; u; u = u->next)
6400 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6402 bool not_in_std;
6403 const char *name;
6404 u->found = 1;
6405 found = true;
6407 switch (i)
6409 #define NAMED_FUNCTION(a,b,c,d) \
6410 case a: \
6411 not_in_std = (gfc_option.allow_std & d) == 0; \
6412 name = b; \
6413 break;
6414 #define NAMED_SUBROUTINE(a,b,c,d) \
6415 case a: \
6416 not_in_std = (gfc_option.allow_std & d) == 0; \
6417 name = b; \
6418 break;
6419 #define NAMED_INTCST(a,b,c,d) \
6420 case a: \
6421 not_in_std = (gfc_option.allow_std & d) == 0; \
6422 name = b; \
6423 break;
6424 #define NAMED_REALCST(a,b,c,d) \
6425 case a: \
6426 not_in_std = (gfc_option.allow_std & d) == 0; \
6427 name = b; \
6428 break;
6429 #define NAMED_CMPXCST(a,b,c,d) \
6430 case a: \
6431 not_in_std = (gfc_option.allow_std & d) == 0; \
6432 name = b; \
6433 break;
6434 #include "iso-c-binding.def"
6435 default:
6436 not_in_std = false;
6437 name = "";
6440 if (not_in_std)
6442 gfc_error ("The symbol %qs, referenced at %L, is not "
6443 "in the selected standard", name, &u->where);
6444 continue;
6447 switch (i)
6449 #define NAMED_FUNCTION(a,b,c,d) \
6450 case a: \
6451 if (a == ISOCBINDING_LOC) \
6452 return_type = c_ptr->n.sym; \
6453 else if (a == ISOCBINDING_FUNLOC) \
6454 return_type = c_funptr->n.sym; \
6455 else \
6456 return_type = NULL; \
6457 create_intrinsic_function (u->local_name[0] \
6458 ? u->local_name : u->use_name, \
6459 a, iso_c_module_name, \
6460 INTMOD_ISO_C_BINDING, false, \
6461 return_type); \
6462 break;
6463 #define NAMED_SUBROUTINE(a,b,c,d) \
6464 case a: \
6465 create_intrinsic_function (u->local_name[0] ? u->local_name \
6466 : u->use_name, \
6467 a, iso_c_module_name, \
6468 INTMOD_ISO_C_BINDING, true, NULL); \
6469 break;
6470 #include "iso-c-binding.def"
6472 case ISOCBINDING_PTR:
6473 case ISOCBINDING_FUNPTR:
6474 /* Already handled above. */
6475 break;
6476 default:
6477 if (i == ISOCBINDING_NULL_PTR)
6478 tmp_symtree = c_ptr;
6479 else if (i == ISOCBINDING_NULL_FUNPTR)
6480 tmp_symtree = c_funptr;
6481 else
6482 tmp_symtree = NULL;
6483 generate_isocbinding_symbol (iso_c_module_name,
6484 (iso_c_binding_symbol) i,
6485 u->local_name[0]
6486 ? u->local_name : u->use_name,
6487 tmp_symtree, false);
6491 if (!found && !only_flag)
6493 /* Skip, if the symbol is not in the enabled standard. */
6494 switch (i)
6496 #define NAMED_FUNCTION(a,b,c,d) \
6497 case a: \
6498 if ((gfc_option.allow_std & d) == 0) \
6499 continue; \
6500 break;
6501 #define NAMED_SUBROUTINE(a,b,c,d) \
6502 case a: \
6503 if ((gfc_option.allow_std & d) == 0) \
6504 continue; \
6505 break;
6506 #define NAMED_INTCST(a,b,c,d) \
6507 case a: \
6508 if ((gfc_option.allow_std & d) == 0) \
6509 continue; \
6510 break;
6511 #define NAMED_REALCST(a,b,c,d) \
6512 case a: \
6513 if ((gfc_option.allow_std & d) == 0) \
6514 continue; \
6515 break;
6516 #define NAMED_CMPXCST(a,b,c,d) \
6517 case a: \
6518 if ((gfc_option.allow_std & d) == 0) \
6519 continue; \
6520 break;
6521 #include "iso-c-binding.def"
6522 default:
6523 ; /* Not GFC_STD_* versioned. */
6526 switch (i)
6528 #define NAMED_FUNCTION(a,b,c,d) \
6529 case a: \
6530 if (a == ISOCBINDING_LOC) \
6531 return_type = c_ptr->n.sym; \
6532 else if (a == ISOCBINDING_FUNLOC) \
6533 return_type = c_funptr->n.sym; \
6534 else \
6535 return_type = NULL; \
6536 create_intrinsic_function (b, a, iso_c_module_name, \
6537 INTMOD_ISO_C_BINDING, false, \
6538 return_type); \
6539 break;
6540 #define NAMED_SUBROUTINE(a,b,c,d) \
6541 case a: \
6542 create_intrinsic_function (b, a, iso_c_module_name, \
6543 INTMOD_ISO_C_BINDING, true, NULL); \
6544 break;
6545 #include "iso-c-binding.def"
6547 case ISOCBINDING_PTR:
6548 case ISOCBINDING_FUNPTR:
6549 /* Already handled above. */
6550 break;
6551 default:
6552 if (i == ISOCBINDING_NULL_PTR)
6553 tmp_symtree = c_ptr;
6554 else if (i == ISOCBINDING_NULL_FUNPTR)
6555 tmp_symtree = c_funptr;
6556 else
6557 tmp_symtree = NULL;
6558 generate_isocbinding_symbol (iso_c_module_name,
6559 (iso_c_binding_symbol) i, NULL,
6560 tmp_symtree, false);
6565 for (u = gfc_rename_list; u; u = u->next)
6567 if (u->found)
6568 continue;
6570 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6571 "module ISO_C_BINDING", u->use_name, &u->where);
6576 /* Add an integer named constant from a given module. */
6578 static void
6579 create_int_parameter (const char *name, int value, const char *modname,
6580 intmod_id module, int id)
6582 gfc_symtree *tmp_symtree;
6583 gfc_symbol *sym;
6585 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6586 if (tmp_symtree != NULL)
6588 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6589 return;
6590 else
6591 gfc_error ("Symbol %qs already declared", name);
6594 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6595 sym = tmp_symtree->n.sym;
6597 sym->module = gfc_get_string ("%s", modname);
6598 sym->attr.flavor = FL_PARAMETER;
6599 sym->ts.type = BT_INTEGER;
6600 sym->ts.kind = gfc_default_integer_kind;
6601 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6602 sym->attr.use_assoc = 1;
6603 sym->from_intmod = module;
6604 sym->intmod_sym_id = id;
6608 /* Value is already contained by the array constructor, but not
6609 yet the shape. */
6611 static void
6612 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6613 const char *modname, intmod_id module, int id)
6615 gfc_symtree *tmp_symtree;
6616 gfc_symbol *sym;
6618 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6619 if (tmp_symtree != NULL)
6621 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6622 return;
6623 else
6624 gfc_error ("Symbol %qs already declared", name);
6627 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6628 sym = tmp_symtree->n.sym;
6630 sym->module = gfc_get_string ("%s", modname);
6631 sym->attr.flavor = FL_PARAMETER;
6632 sym->ts.type = BT_INTEGER;
6633 sym->ts.kind = gfc_default_integer_kind;
6634 sym->attr.use_assoc = 1;
6635 sym->from_intmod = module;
6636 sym->intmod_sym_id = id;
6637 sym->attr.dimension = 1;
6638 sym->as = gfc_get_array_spec ();
6639 sym->as->rank = 1;
6640 sym->as->type = AS_EXPLICIT;
6641 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6642 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6644 sym->value = value;
6645 sym->value->shape = gfc_get_shape (1);
6646 mpz_init_set_ui (sym->value->shape[0], size);
6650 /* Add an derived type for a given module. */
6652 static void
6653 create_derived_type (const char *name, const char *modname,
6654 intmod_id module, int id)
6656 gfc_symtree *tmp_symtree;
6657 gfc_symbol *sym, *dt_sym;
6658 gfc_interface *intr, *head;
6660 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6661 if (tmp_symtree != NULL)
6663 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6664 return;
6665 else
6666 gfc_error ("Symbol %qs already declared", name);
6669 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6670 sym = tmp_symtree->n.sym;
6671 sym->module = gfc_get_string ("%s", modname);
6672 sym->from_intmod = module;
6673 sym->intmod_sym_id = id;
6674 sym->attr.flavor = FL_PROCEDURE;
6675 sym->attr.function = 1;
6676 sym->attr.generic = 1;
6678 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
6679 gfc_current_ns, &tmp_symtree, false);
6680 dt_sym = tmp_symtree->n.sym;
6681 dt_sym->name = gfc_get_string ("%s", sym->name);
6682 dt_sym->attr.flavor = FL_DERIVED;
6683 dt_sym->attr.private_comp = 1;
6684 dt_sym->attr.zero_comp = 1;
6685 dt_sym->attr.use_assoc = 1;
6686 dt_sym->module = gfc_get_string ("%s", modname);
6687 dt_sym->from_intmod = module;
6688 dt_sym->intmod_sym_id = id;
6690 head = sym->generic;
6691 intr = gfc_get_interface ();
6692 intr->sym = dt_sym;
6693 intr->where = gfc_current_locus;
6694 intr->next = head;
6695 sym->generic = intr;
6696 sym->attr.if_source = IFSRC_DECL;
6700 /* Read the contents of the module file into a temporary buffer. */
6702 static void
6703 read_module_to_tmpbuf ()
6705 /* We don't know the uncompressed size, so enlarge the buffer as
6706 needed. */
6707 int cursz = 4096;
6708 int rsize = cursz;
6709 int len = 0;
6711 module_content = XNEWVEC (char, cursz);
6713 while (1)
6715 int nread = gzread (module_fp, module_content + len, rsize);
6716 len += nread;
6717 if (nread < rsize)
6718 break;
6719 cursz *= 2;
6720 module_content = XRESIZEVEC (char, module_content, cursz);
6721 rsize = cursz - len;
6724 module_content = XRESIZEVEC (char, module_content, len + 1);
6725 module_content[len] = '\0';
6727 module_pos = 0;
6731 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6733 static void
6734 use_iso_fortran_env_module (void)
6736 static char mod[] = "iso_fortran_env";
6737 gfc_use_rename *u;
6738 gfc_symbol *mod_sym;
6739 gfc_symtree *mod_symtree;
6740 gfc_expr *expr;
6741 int i, j;
6743 intmod_sym symbol[] = {
6744 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6745 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6746 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6747 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6748 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6749 #include "iso-fortran-env.def"
6750 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6752 i = 0;
6753 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6754 #include "iso-fortran-env.def"
6756 /* Generate the symbol for the module itself. */
6757 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6758 if (mod_symtree == NULL)
6760 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6761 gcc_assert (mod_symtree);
6762 mod_sym = mod_symtree->n.sym;
6764 mod_sym->attr.flavor = FL_MODULE;
6765 mod_sym->attr.intrinsic = 1;
6766 mod_sym->module = gfc_get_string ("%s", mod);
6767 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6769 else
6770 if (!mod_symtree->n.sym->attr.intrinsic)
6771 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6772 "non-intrinsic module name used previously", mod);
6774 /* Generate the symbols for the module integer named constants. */
6776 for (i = 0; symbol[i].name; i++)
6778 bool found = false;
6779 for (u = gfc_rename_list; u; u = u->next)
6781 if (strcmp (symbol[i].name, u->use_name) == 0)
6783 found = true;
6784 u->found = 1;
6786 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6787 "referenced at %L, is not in the selected "
6788 "standard", symbol[i].name, &u->where))
6789 continue;
6791 if ((flag_default_integer || flag_default_real_8)
6792 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6793 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
6794 "constant from intrinsic module "
6795 "ISO_FORTRAN_ENV at %L is incompatible with "
6796 "option %qs", &u->where,
6797 flag_default_integer
6798 ? "-fdefault-integer-8"
6799 : "-fdefault-real-8");
6800 switch (symbol[i].id)
6802 #define NAMED_INTCST(a,b,c,d) \
6803 case a:
6804 #include "iso-fortran-env.def"
6805 create_int_parameter (u->local_name[0] ? u->local_name
6806 : u->use_name,
6807 symbol[i].value, mod,
6808 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6809 break;
6811 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6812 case a:\
6813 expr = gfc_get_array_expr (BT_INTEGER, \
6814 gfc_default_integer_kind,\
6815 NULL); \
6816 for (j = 0; KINDS[j].kind != 0; j++) \
6817 gfc_constructor_append_expr (&expr->value.constructor, \
6818 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6819 KINDS[j].kind), NULL); \
6820 create_int_parameter_array (u->local_name[0] ? u->local_name \
6821 : u->use_name, \
6822 j, expr, mod, \
6823 INTMOD_ISO_FORTRAN_ENV, \
6824 symbol[i].id); \
6825 break;
6826 #include "iso-fortran-env.def"
6828 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6829 case a:
6830 #include "iso-fortran-env.def"
6831 create_derived_type (u->local_name[0] ? u->local_name
6832 : u->use_name,
6833 mod, INTMOD_ISO_FORTRAN_ENV,
6834 symbol[i].id);
6835 break;
6837 #define NAMED_FUNCTION(a,b,c,d) \
6838 case a:
6839 #include "iso-fortran-env.def"
6840 create_intrinsic_function (u->local_name[0] ? u->local_name
6841 : u->use_name,
6842 symbol[i].id, mod,
6843 INTMOD_ISO_FORTRAN_ENV, false,
6844 NULL);
6845 break;
6847 default:
6848 gcc_unreachable ();
6853 if (!found && !only_flag)
6855 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6856 continue;
6858 if ((flag_default_integer || flag_default_real_8)
6859 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6860 gfc_warning_now (0,
6861 "Use of the NUMERIC_STORAGE_SIZE named constant "
6862 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6863 "incompatible with option %s",
6864 flag_default_integer
6865 ? "-fdefault-integer-8" : "-fdefault-real-8");
6867 switch (symbol[i].id)
6869 #define NAMED_INTCST(a,b,c,d) \
6870 case a:
6871 #include "iso-fortran-env.def"
6872 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6873 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6874 break;
6876 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6877 case a:\
6878 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6879 NULL); \
6880 for (j = 0; KINDS[j].kind != 0; j++) \
6881 gfc_constructor_append_expr (&expr->value.constructor, \
6882 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6883 KINDS[j].kind), NULL); \
6884 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6885 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6886 break;
6887 #include "iso-fortran-env.def"
6889 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6890 case a:
6891 #include "iso-fortran-env.def"
6892 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6893 symbol[i].id);
6894 break;
6896 #define NAMED_FUNCTION(a,b,c,d) \
6897 case a:
6898 #include "iso-fortran-env.def"
6899 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6900 INTMOD_ISO_FORTRAN_ENV, false,
6901 NULL);
6902 break;
6904 default:
6905 gcc_unreachable ();
6910 for (u = gfc_rename_list; u; u = u->next)
6912 if (u->found)
6913 continue;
6915 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6916 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6921 /* Process a USE directive. */
6923 static void
6924 gfc_use_module (gfc_use_list *module)
6926 char *filename;
6927 gfc_state_data *p;
6928 int c, line, start;
6929 gfc_symtree *mod_symtree;
6930 gfc_use_list *use_stmt;
6931 locus old_locus = gfc_current_locus;
6933 gfc_current_locus = module->where;
6934 module_name = module->module_name;
6935 gfc_rename_list = module->rename;
6936 only_flag = module->only_flag;
6937 current_intmod = INTMOD_NONE;
6939 if (!only_flag)
6940 gfc_warning_now (OPT_Wuse_without_only,
6941 "USE statement at %C has no ONLY qualifier");
6943 if (gfc_state_stack->state == COMP_MODULE
6944 || module->submodule_name == NULL)
6946 filename = XALLOCAVEC (char, strlen (module_name)
6947 + strlen (MODULE_EXTENSION) + 1);
6948 strcpy (filename, module_name);
6949 strcat (filename, MODULE_EXTENSION);
6951 else
6953 filename = XALLOCAVEC (char, strlen (module->submodule_name)
6954 + strlen (SUBMODULE_EXTENSION) + 1);
6955 strcpy (filename, module->submodule_name);
6956 strcat (filename, SUBMODULE_EXTENSION);
6959 /* First, try to find an non-intrinsic module, unless the USE statement
6960 specified that the module is intrinsic. */
6961 module_fp = NULL;
6962 if (!module->intrinsic)
6963 module_fp = gzopen_included_file (filename, true, true);
6965 /* Then, see if it's an intrinsic one, unless the USE statement
6966 specified that the module is non-intrinsic. */
6967 if (module_fp == NULL && !module->non_intrinsic)
6969 if (strcmp (module_name, "iso_fortran_env") == 0
6970 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6971 "intrinsic module at %C"))
6973 use_iso_fortran_env_module ();
6974 free_rename (module->rename);
6975 module->rename = NULL;
6976 gfc_current_locus = old_locus;
6977 module->intrinsic = true;
6978 return;
6981 if (strcmp (module_name, "iso_c_binding") == 0
6982 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6984 import_iso_c_binding_module();
6985 free_rename (module->rename);
6986 module->rename = NULL;
6987 gfc_current_locus = old_locus;
6988 module->intrinsic = true;
6989 return;
6992 module_fp = gzopen_intrinsic_module (filename);
6994 if (module_fp == NULL && module->intrinsic)
6995 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6996 module_name);
6998 /* Check for the IEEE modules, so we can mark their symbols
6999 accordingly when we read them. */
7000 if (strcmp (module_name, "ieee_features") == 0
7001 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
7003 current_intmod = INTMOD_IEEE_FEATURES;
7005 else if (strcmp (module_name, "ieee_exceptions") == 0
7006 && gfc_notify_std (GFC_STD_F2003,
7007 "IEEE_EXCEPTIONS module at %C"))
7009 current_intmod = INTMOD_IEEE_EXCEPTIONS;
7011 else if (strcmp (module_name, "ieee_arithmetic") == 0
7012 && gfc_notify_std (GFC_STD_F2003,
7013 "IEEE_ARITHMETIC module at %C"))
7015 current_intmod = INTMOD_IEEE_ARITHMETIC;
7019 if (module_fp == NULL)
7021 if (gfc_state_stack->state != COMP_SUBMODULE
7022 && module->submodule_name == NULL)
7023 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
7024 filename, xstrerror (errno));
7025 else
7026 gfc_fatal_error ("Module file %qs has not been generated, either "
7027 "because the module does not contain a MODULE "
7028 "PROCEDURE or there is an error in the module.",
7029 filename);
7032 /* Check that we haven't already USEd an intrinsic module with the
7033 same name. */
7035 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
7036 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
7037 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
7038 "intrinsic module name used previously", module_name);
7040 iomode = IO_INPUT;
7041 module_line = 1;
7042 module_column = 1;
7043 start = 0;
7045 read_module_to_tmpbuf ();
7046 gzclose (module_fp);
7048 /* Skip the first line of the module, after checking that this is
7049 a gfortran module file. */
7050 line = 0;
7051 while (line < 1)
7053 c = module_char ();
7054 if (c == EOF)
7055 bad_module ("Unexpected end of module");
7056 if (start++ < 3)
7057 parse_name (c);
7058 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
7059 || (start == 2 && strcmp (atom_name, " module") != 0))
7060 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
7061 " module file", filename);
7062 if (start == 3)
7064 if (strcmp (atom_name, " version") != 0
7065 || module_char () != ' '
7066 || parse_atom () != ATOM_STRING
7067 || strcmp (atom_string, MOD_VERSION))
7068 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
7069 " because it was created by a different"
7070 " version of GNU Fortran", filename);
7072 free (atom_string);
7075 if (c == '\n')
7076 line++;
7079 /* Make sure we're not reading the same module that we may be building. */
7080 for (p = gfc_state_stack; p; p = p->previous)
7081 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
7082 && strcmp (p->sym->name, module_name) == 0)
7083 gfc_fatal_error ("Can't USE the same %smodule we're building",
7084 p->state == COMP_SUBMODULE ? "sub" : "");
7086 init_pi_tree ();
7087 init_true_name_tree ();
7089 read_module ();
7091 free_true_name (true_name_root);
7092 true_name_root = NULL;
7094 free_pi_tree (pi_root);
7095 pi_root = NULL;
7097 XDELETEVEC (module_content);
7098 module_content = NULL;
7100 use_stmt = gfc_get_use_list ();
7101 *use_stmt = *module;
7102 use_stmt->next = gfc_current_ns->use_stmts;
7103 gfc_current_ns->use_stmts = use_stmt;
7105 gfc_current_locus = old_locus;
7109 /* Remove duplicated intrinsic operators from the rename list. */
7111 static void
7112 rename_list_remove_duplicate (gfc_use_rename *list)
7114 gfc_use_rename *seek, *last;
7116 for (; list; list = list->next)
7117 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7119 last = list;
7120 for (seek = list->next; seek; seek = last->next)
7122 if (list->op == seek->op)
7124 last->next = seek->next;
7125 free (seek);
7127 else
7128 last = seek;
7134 /* Process all USE directives. */
7136 void
7137 gfc_use_modules (void)
7139 gfc_use_list *next, *seek, *last;
7141 for (next = module_list; next; next = next->next)
7143 bool non_intrinsic = next->non_intrinsic;
7144 bool intrinsic = next->intrinsic;
7145 bool neither = !non_intrinsic && !intrinsic;
7147 for (seek = next->next; seek; seek = seek->next)
7149 if (next->module_name != seek->module_name)
7150 continue;
7152 if (seek->non_intrinsic)
7153 non_intrinsic = true;
7154 else if (seek->intrinsic)
7155 intrinsic = true;
7156 else
7157 neither = true;
7160 if (intrinsic && neither && !non_intrinsic)
7162 char *filename;
7163 FILE *fp;
7165 filename = XALLOCAVEC (char,
7166 strlen (next->module_name)
7167 + strlen (MODULE_EXTENSION) + 1);
7168 strcpy (filename, next->module_name);
7169 strcat (filename, MODULE_EXTENSION);
7170 fp = gfc_open_included_file (filename, true, true);
7171 if (fp != NULL)
7173 non_intrinsic = true;
7174 fclose (fp);
7178 last = next;
7179 for (seek = next->next; seek; seek = last->next)
7181 if (next->module_name != seek->module_name)
7183 last = seek;
7184 continue;
7187 if ((!next->intrinsic && !seek->intrinsic)
7188 || (next->intrinsic && seek->intrinsic)
7189 || !non_intrinsic)
7191 if (!seek->only_flag)
7192 next->only_flag = false;
7193 if (seek->rename)
7195 gfc_use_rename *r = seek->rename;
7196 while (r->next)
7197 r = r->next;
7198 r->next = next->rename;
7199 next->rename = seek->rename;
7201 last->next = seek->next;
7202 free (seek);
7204 else
7205 last = seek;
7209 for (; module_list; module_list = next)
7211 next = module_list->next;
7212 rename_list_remove_duplicate (module_list->rename);
7213 gfc_use_module (module_list);
7214 free (module_list);
7216 gfc_rename_list = NULL;
7220 void
7221 gfc_free_use_stmts (gfc_use_list *use_stmts)
7223 gfc_use_list *next;
7224 for (; use_stmts; use_stmts = next)
7226 gfc_use_rename *next_rename;
7228 for (; use_stmts->rename; use_stmts->rename = next_rename)
7230 next_rename = use_stmts->rename->next;
7231 free (use_stmts->rename);
7233 next = use_stmts->next;
7234 free (use_stmts);
7239 void
7240 gfc_module_init_2 (void)
7242 last_atom = ATOM_LPAREN;
7243 gfc_rename_list = NULL;
7244 module_list = NULL;
7248 void
7249 gfc_module_done_2 (void)
7251 free_rename (gfc_rename_list);
7252 gfc_rename_list = NULL;