re PR c++/19476 (Missed null checking elimination with new)
[official-gcc.git] / gcc / fortran / module.c
blobc390a95952e6b7a47abd853089e6fff6591ccdb7
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "gfortran.h"
71 #include "arith.h"
72 #include "match.h"
73 #include "parse.h" /* FIXME */
74 #include "constructor.h"
75 #include "cpp.h"
76 #include "tree.h"
77 #include "scanner.h"
78 #include <zlib.h>
80 #define MODULE_EXTENSION ".mod"
82 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
83 recognized. */
84 #define MOD_VERSION "11"
87 /* Structure that describes a position within a module file. */
89 typedef struct
91 int column, line;
92 long pos;
94 module_locus;
96 /* Structure for list of symbols of intrinsic modules. */
97 typedef struct
99 int id;
100 const char *name;
101 int value;
102 int standard;
104 intmod_sym;
107 typedef enum
109 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
111 pointer_t;
113 /* The fixup structure lists pointers to pointers that have to
114 be updated when a pointer value becomes known. */
116 typedef struct fixup_t
118 void **pointer;
119 struct fixup_t *next;
121 fixup_t;
124 /* Structure for holding extra info needed for pointers being read. */
126 enum gfc_rsym_state
128 UNUSED,
129 NEEDED,
130 USED
133 enum gfc_wsym_state
135 UNREFERENCED = 0,
136 NEEDS_WRITE,
137 WRITTEN
140 typedef struct pointer_info
142 BBT_HEADER (pointer_info);
143 int integer;
144 pointer_t type;
146 /* The first component of each member of the union is the pointer
147 being stored. */
149 fixup_t *fixup;
151 union
153 void *pointer; /* Member for doing pointer searches. */
155 struct
157 gfc_symbol *sym;
158 char *true_name, *module, *binding_label;
159 fixup_t *stfixup;
160 gfc_symtree *symtree;
161 enum gfc_rsym_state state;
162 int ns, referenced, renamed;
163 module_locus where;
165 rsym;
167 struct
169 gfc_symbol *sym;
170 enum gfc_wsym_state state;
172 wsym;
177 pointer_info;
179 #define gfc_get_pointer_info() XCNEW (pointer_info)
182 /* Local variables */
184 /* The gzFile for the module we're reading or writing. */
185 static gzFile module_fp;
188 /* The name of the module we're reading (USE'ing) or writing. */
189 static const char *module_name;
190 static gfc_use_list *module_list;
192 /* Content of module. */
193 static char* module_content;
195 static long module_pos;
196 static int module_line, module_column, only_flag;
197 static int prev_module_line, prev_module_column;
199 static enum
200 { IO_INPUT, IO_OUTPUT }
201 iomode;
203 static gfc_use_rename *gfc_rename_list;
204 static pointer_info *pi_root;
205 static int symbol_number; /* Counter for assigning symbol numbers */
207 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
208 static bool in_load_equiv;
212 /*****************************************************************/
214 /* Pointer/integer conversion. Pointers between structures are stored
215 as integers in the module file. The next couple of subroutines
216 handle this translation for reading and writing. */
218 /* Recursively free the tree of pointer structures. */
220 static void
221 free_pi_tree (pointer_info *p)
223 if (p == NULL)
224 return;
226 if (p->fixup != NULL)
227 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
229 free_pi_tree (p->left);
230 free_pi_tree (p->right);
232 if (iomode == IO_INPUT)
234 XDELETEVEC (p->u.rsym.true_name);
235 XDELETEVEC (p->u.rsym.module);
236 XDELETEVEC (p->u.rsym.binding_label);
239 free (p);
243 /* Compare pointers when searching by pointer. Used when writing a
244 module. */
246 static int
247 compare_pointers (void *_sn1, void *_sn2)
249 pointer_info *sn1, *sn2;
251 sn1 = (pointer_info *) _sn1;
252 sn2 = (pointer_info *) _sn2;
254 if (sn1->u.pointer < sn2->u.pointer)
255 return -1;
256 if (sn1->u.pointer > sn2->u.pointer)
257 return 1;
259 return 0;
263 /* Compare integers when searching by integer. Used when reading a
264 module. */
266 static int
267 compare_integers (void *_sn1, void *_sn2)
269 pointer_info *sn1, *sn2;
271 sn1 = (pointer_info *) _sn1;
272 sn2 = (pointer_info *) _sn2;
274 if (sn1->integer < sn2->integer)
275 return -1;
276 if (sn1->integer > sn2->integer)
277 return 1;
279 return 0;
283 /* Initialize the pointer_info tree. */
285 static void
286 init_pi_tree (void)
288 compare_fn compare;
289 pointer_info *p;
291 pi_root = NULL;
292 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
294 /* Pointer 0 is the NULL pointer. */
295 p = gfc_get_pointer_info ();
296 p->u.pointer = NULL;
297 p->integer = 0;
298 p->type = P_OTHER;
300 gfc_insert_bbt (&pi_root, p, compare);
302 /* Pointer 1 is the current namespace. */
303 p = gfc_get_pointer_info ();
304 p->u.pointer = gfc_current_ns;
305 p->integer = 1;
306 p->type = P_NAMESPACE;
308 gfc_insert_bbt (&pi_root, p, compare);
310 symbol_number = 2;
314 /* During module writing, call here with a pointer to something,
315 returning the pointer_info node. */
317 static pointer_info *
318 find_pointer (void *gp)
320 pointer_info *p;
322 p = pi_root;
323 while (p != NULL)
325 if (p->u.pointer == gp)
326 break;
327 p = (gp < p->u.pointer) ? p->left : p->right;
330 return p;
334 /* Given a pointer while writing, returns the pointer_info tree node,
335 creating it if it doesn't exist. */
337 static pointer_info *
338 get_pointer (void *gp)
340 pointer_info *p;
342 p = find_pointer (gp);
343 if (p != NULL)
344 return p;
346 /* Pointer doesn't have an integer. Give it one. */
347 p = gfc_get_pointer_info ();
349 p->u.pointer = gp;
350 p->integer = symbol_number++;
352 gfc_insert_bbt (&pi_root, p, compare_pointers);
354 return p;
358 /* Given an integer during reading, find it in the pointer_info tree,
359 creating the node if not found. */
361 static pointer_info *
362 get_integer (int integer)
364 pointer_info *p, t;
365 int c;
367 t.integer = integer;
369 p = pi_root;
370 while (p != NULL)
372 c = compare_integers (&t, p);
373 if (c == 0)
374 break;
376 p = (c < 0) ? p->left : p->right;
379 if (p != NULL)
380 return p;
382 p = gfc_get_pointer_info ();
383 p->integer = integer;
384 p->u.pointer = NULL;
386 gfc_insert_bbt (&pi_root, p, compare_integers);
388 return p;
392 /* Recursive function to find a pointer within a tree by brute force. */
394 static pointer_info *
395 fp2 (pointer_info *p, const void *target)
397 pointer_info *q;
399 if (p == NULL)
400 return NULL;
402 if (p->u.pointer == target)
403 return p;
405 q = fp2 (p->left, target);
406 if (q != NULL)
407 return q;
409 return fp2 (p->right, target);
413 /* During reading, find a pointer_info node from the pointer value.
414 This amounts to a brute-force search. */
416 static pointer_info *
417 find_pointer2 (void *p)
419 return fp2 (pi_root, p);
423 /* Resolve any fixups using a known pointer. */
425 static void
426 resolve_fixups (fixup_t *f, void *gp)
428 fixup_t *next;
430 for (; f; f = next)
432 next = f->next;
433 *(f->pointer) = gp;
434 free (f);
439 /* Convert a string such that it starts with a lower-case character. Used
440 to convert the symtree name of a derived-type to the symbol name or to
441 the name of the associated generic function. */
443 static const char *
444 dt_lower_string (const char *name)
446 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
447 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
448 &name[1]);
449 return gfc_get_string (name);
453 /* Convert a string such that it starts with an upper-case character. Used to
454 return the symtree-name for a derived type; the symbol name itself and the
455 symtree/symbol name of the associated generic function start with a lower-
456 case character. */
458 static const char *
459 dt_upper_string (const char *name)
461 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
462 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
463 &name[1]);
464 return gfc_get_string (name);
467 /* Call here during module reading when we know what pointer to
468 associate with an integer. Any fixups that exist are resolved at
469 this time. */
471 static void
472 associate_integer_pointer (pointer_info *p, void *gp)
474 if (p->u.pointer != NULL)
475 gfc_internal_error ("associate_integer_pointer(): Already associated");
477 p->u.pointer = gp;
479 resolve_fixups (p->fixup, gp);
481 p->fixup = NULL;
485 /* During module reading, given an integer and a pointer to a pointer,
486 either store the pointer from an already-known value or create a
487 fixup structure in order to store things later. Returns zero if
488 the reference has been actually stored, or nonzero if the reference
489 must be fixed later (i.e., associate_integer_pointer must be called
490 sometime later. Returns the pointer_info structure. */
492 static pointer_info *
493 add_fixup (int integer, void *gp)
495 pointer_info *p;
496 fixup_t *f;
497 char **cp;
499 p = get_integer (integer);
501 if (p->integer == 0 || p->u.pointer != NULL)
503 cp = (char **) gp;
504 *cp = (char *) p->u.pointer;
506 else
508 f = XCNEW (fixup_t);
510 f->next = p->fixup;
511 p->fixup = f;
513 f->pointer = (void **) gp;
516 return p;
520 /*****************************************************************/
522 /* Parser related subroutines */
524 /* Free the rename list left behind by a USE statement. */
526 static void
527 free_rename (gfc_use_rename *list)
529 gfc_use_rename *next;
531 for (; list; list = next)
533 next = list->next;
534 free (list);
539 /* Match a USE statement. */
541 match
542 gfc_match_use (void)
544 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
545 gfc_use_rename *tail = NULL, *new_use;
546 interface_type type, type2;
547 gfc_intrinsic_op op;
548 match m;
549 gfc_use_list *use_list;
551 use_list = gfc_get_use_list ();
553 if (gfc_match (" , ") == MATCH_YES)
555 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
557 if (!gfc_notify_std (GFC_STD_F2003, "module "
558 "nature in USE statement at %C"))
559 goto cleanup;
561 if (strcmp (module_nature, "intrinsic") == 0)
562 use_list->intrinsic = true;
563 else
565 if (strcmp (module_nature, "non_intrinsic") == 0)
566 use_list->non_intrinsic = true;
567 else
569 gfc_error ("Module nature in USE statement at %C shall "
570 "be either INTRINSIC or NON_INTRINSIC");
571 goto cleanup;
575 else
577 /* Help output a better error message than "Unclassifiable
578 statement". */
579 gfc_match (" %n", module_nature);
580 if (strcmp (module_nature, "intrinsic") == 0
581 || strcmp (module_nature, "non_intrinsic") == 0)
582 gfc_error ("\"::\" was expected after module nature at %C "
583 "but was not found");
584 free (use_list);
585 return m;
588 else
590 m = gfc_match (" ::");
591 if (m == MATCH_YES &&
592 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
593 goto cleanup;
595 if (m != MATCH_YES)
597 m = gfc_match ("% ");
598 if (m != MATCH_YES)
600 free (use_list);
601 return m;
606 use_list->where = gfc_current_locus;
608 m = gfc_match_name (name);
609 if (m != MATCH_YES)
611 free (use_list);
612 return m;
615 use_list->module_name = gfc_get_string (name);
617 if (gfc_match_eos () == MATCH_YES)
618 goto done;
620 if (gfc_match_char (',') != MATCH_YES)
621 goto syntax;
623 if (gfc_match (" only :") == MATCH_YES)
624 use_list->only_flag = true;
626 if (gfc_match_eos () == MATCH_YES)
627 goto done;
629 for (;;)
631 /* Get a new rename struct and add it to the rename list. */
632 new_use = gfc_get_use_rename ();
633 new_use->where = gfc_current_locus;
634 new_use->found = 0;
636 if (use_list->rename == NULL)
637 use_list->rename = new_use;
638 else
639 tail->next = new_use;
640 tail = new_use;
642 /* See what kind of interface we're dealing with. Assume it is
643 not an operator. */
644 new_use->op = INTRINSIC_NONE;
645 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
646 goto cleanup;
648 switch (type)
650 case INTERFACE_NAMELESS:
651 gfc_error ("Missing generic specification in USE statement at %C");
652 goto cleanup;
654 case INTERFACE_USER_OP:
655 case INTERFACE_GENERIC:
656 m = gfc_match (" =>");
658 if (type == INTERFACE_USER_OP && m == MATCH_YES
659 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
660 "operators in USE statements at %C")))
661 goto cleanup;
663 if (type == INTERFACE_USER_OP)
664 new_use->op = INTRINSIC_USER;
666 if (use_list->only_flag)
668 if (m != MATCH_YES)
669 strcpy (new_use->use_name, name);
670 else
672 strcpy (new_use->local_name, name);
673 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
674 if (type != type2)
675 goto syntax;
676 if (m == MATCH_NO)
677 goto syntax;
678 if (m == MATCH_ERROR)
679 goto cleanup;
682 else
684 if (m != MATCH_YES)
685 goto syntax;
686 strcpy (new_use->local_name, name);
688 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
689 if (type != type2)
690 goto syntax;
691 if (m == MATCH_NO)
692 goto syntax;
693 if (m == MATCH_ERROR)
694 goto cleanup;
697 if (strcmp (new_use->use_name, use_list->module_name) == 0
698 || strcmp (new_use->local_name, use_list->module_name) == 0)
700 gfc_error ("The name '%s' at %C has already been used as "
701 "an external module name.", use_list->module_name);
702 goto cleanup;
704 break;
706 case INTERFACE_INTRINSIC_OP:
707 new_use->op = op;
708 break;
710 default:
711 gcc_unreachable ();
714 if (gfc_match_eos () == MATCH_YES)
715 break;
716 if (gfc_match_char (',') != MATCH_YES)
717 goto syntax;
720 done:
721 if (module_list)
723 gfc_use_list *last = module_list;
724 while (last->next)
725 last = last->next;
726 last->next = use_list;
728 else
729 module_list = use_list;
731 return MATCH_YES;
733 syntax:
734 gfc_syntax_error (ST_USE);
736 cleanup:
737 free_rename (use_list->rename);
738 free (use_list);
739 return MATCH_ERROR;
743 /* Given a name and a number, inst, return the inst name
744 under which to load this symbol. Returns NULL if this
745 symbol shouldn't be loaded. If inst is zero, returns
746 the number of instances of this name. If interface is
747 true, a user-defined operator is sought, otherwise only
748 non-operators are sought. */
750 static const char *
751 find_use_name_n (const char *name, int *inst, bool interface)
753 gfc_use_rename *u;
754 const char *low_name = NULL;
755 int i;
757 /* For derived types. */
758 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
759 low_name = dt_lower_string (name);
761 i = 0;
762 for (u = gfc_rename_list; u; u = u->next)
764 if ((!low_name && strcmp (u->use_name, name) != 0)
765 || (low_name && strcmp (u->use_name, low_name) != 0)
766 || (u->op == INTRINSIC_USER && !interface)
767 || (u->op != INTRINSIC_USER && interface))
768 continue;
769 if (++i == *inst)
770 break;
773 if (!*inst)
775 *inst = i;
776 return NULL;
779 if (u == NULL)
780 return only_flag ? NULL : name;
782 u->found = 1;
784 if (low_name)
786 if (u->local_name[0] == '\0')
787 return name;
788 return dt_upper_string (u->local_name);
791 return (u->local_name[0] != '\0') ? u->local_name : name;
795 /* Given a name, return the name under which to load this symbol.
796 Returns NULL if this symbol shouldn't be loaded. */
798 static const char *
799 find_use_name (const char *name, bool interface)
801 int i = 1;
802 return find_use_name_n (name, &i, interface);
806 /* Given a real name, return the number of use names associated with it. */
808 static int
809 number_use_names (const char *name, bool interface)
811 int i = 0;
812 find_use_name_n (name, &i, interface);
813 return i;
817 /* Try to find the operator in the current list. */
819 static gfc_use_rename *
820 find_use_operator (gfc_intrinsic_op op)
822 gfc_use_rename *u;
824 for (u = gfc_rename_list; u; u = u->next)
825 if (u->op == op)
826 return u;
828 return NULL;
832 /*****************************************************************/
834 /* The next couple of subroutines maintain a tree used to avoid a
835 brute-force search for a combination of true name and module name.
836 While symtree names, the name that a particular symbol is known by
837 can changed with USE statements, we still have to keep track of the
838 true names to generate the correct reference, and also avoid
839 loading the same real symbol twice in a program unit.
841 When we start reading, the true name tree is built and maintained
842 as symbols are read. The tree is searched as we load new symbols
843 to see if it already exists someplace in the namespace. */
845 typedef struct true_name
847 BBT_HEADER (true_name);
848 const char *name;
849 gfc_symbol *sym;
851 true_name;
853 static true_name *true_name_root;
856 /* Compare two true_name structures. */
858 static int
859 compare_true_names (void *_t1, void *_t2)
861 true_name *t1, *t2;
862 int c;
864 t1 = (true_name *) _t1;
865 t2 = (true_name *) _t2;
867 c = ((t1->sym->module > t2->sym->module)
868 - (t1->sym->module < t2->sym->module));
869 if (c != 0)
870 return c;
872 return strcmp (t1->name, t2->name);
876 /* Given a true name, search the true name tree to see if it exists
877 within the main namespace. */
879 static gfc_symbol *
880 find_true_name (const char *name, const char *module)
882 true_name t, *p;
883 gfc_symbol sym;
884 int c;
886 t.name = gfc_get_string (name);
887 if (module != NULL)
888 sym.module = gfc_get_string (module);
889 else
890 sym.module = NULL;
891 t.sym = &sym;
893 p = true_name_root;
894 while (p != NULL)
896 c = compare_true_names ((void *) (&t), (void *) p);
897 if (c == 0)
898 return p->sym;
900 p = (c < 0) ? p->left : p->right;
903 return NULL;
907 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
909 static void
910 add_true_name (gfc_symbol *sym)
912 true_name *t;
914 t = XCNEW (true_name);
915 t->sym = sym;
916 if (sym->attr.flavor == FL_DERIVED)
917 t->name = dt_upper_string (sym->name);
918 else
919 t->name = sym->name;
921 gfc_insert_bbt (&true_name_root, t, compare_true_names);
925 /* Recursive function to build the initial true name tree by
926 recursively traversing the current namespace. */
928 static void
929 build_tnt (gfc_symtree *st)
931 const char *name;
932 if (st == NULL)
933 return;
935 build_tnt (st->left);
936 build_tnt (st->right);
938 if (st->n.sym->attr.flavor == FL_DERIVED)
939 name = dt_upper_string (st->n.sym->name);
940 else
941 name = st->n.sym->name;
943 if (find_true_name (name, st->n.sym->module) != NULL)
944 return;
946 add_true_name (st->n.sym);
950 /* Initialize the true name tree with the current namespace. */
952 static void
953 init_true_name_tree (void)
955 true_name_root = NULL;
956 build_tnt (gfc_current_ns->sym_root);
960 /* Recursively free a true name tree node. */
962 static void
963 free_true_name (true_name *t)
965 if (t == NULL)
966 return;
967 free_true_name (t->left);
968 free_true_name (t->right);
970 free (t);
974 /*****************************************************************/
976 /* Module reading and writing. */
978 /* The following are versions similar to the ones in scanner.c, but
979 for dealing with compressed module files. */
981 static gzFile
982 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
983 bool module, bool system)
985 char *fullname;
986 gfc_directorylist *p;
987 gzFile f;
989 for (p = list; p; p = p->next)
991 if (module && !p->use_for_modules)
992 continue;
994 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
995 strcpy (fullname, p->path);
996 strcat (fullname, name);
998 f = gzopen (fullname, "r");
999 if (f != NULL)
1001 if (gfc_cpp_makedep ())
1002 gfc_cpp_add_dep (fullname, system);
1004 return f;
1008 return NULL;
1011 static gzFile
1012 gzopen_included_file (const char *name, bool include_cwd, bool module)
1014 gzFile f = NULL;
1016 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1018 f = gzopen (name, "r");
1019 if (f && gfc_cpp_makedep ())
1020 gfc_cpp_add_dep (name, false);
1023 if (!f)
1024 f = gzopen_included_file_1 (name, include_dirs, module, false);
1026 return f;
1029 static gzFile
1030 gzopen_intrinsic_module (const char* name)
1032 gzFile f = NULL;
1034 if (IS_ABSOLUTE_PATH (name))
1036 f = gzopen (name, "r");
1037 if (f && gfc_cpp_makedep ())
1038 gfc_cpp_add_dep (name, true);
1041 if (!f)
1042 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1044 return f;
1048 typedef enum
1050 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1052 atom_type;
1054 static atom_type last_atom;
1057 /* The name buffer must be at least as long as a symbol name. Right
1058 now it's not clear how we're going to store numeric constants--
1059 probably as a hexadecimal string, since this will allow the exact
1060 number to be preserved (this can't be done by a decimal
1061 representation). Worry about that later. TODO! */
1063 #define MAX_ATOM_SIZE 100
1065 static int atom_int;
1066 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1069 /* Report problems with a module. Error reporting is not very
1070 elaborate, since this sorts of errors shouldn't really happen.
1071 This subroutine never returns. */
1073 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1075 static void
1076 bad_module (const char *msgid)
1078 XDELETEVEC (module_content);
1079 module_content = NULL;
1081 switch (iomode)
1083 case IO_INPUT:
1084 gfc_fatal_error ("Reading module %s at line %d column %d: %s",
1085 module_name, module_line, module_column, msgid);
1086 break;
1087 case IO_OUTPUT:
1088 gfc_fatal_error ("Writing module %s at line %d column %d: %s",
1089 module_name, module_line, module_column, msgid);
1090 break;
1091 default:
1092 gfc_fatal_error ("Module %s at line %d column %d: %s",
1093 module_name, module_line, module_column, msgid);
1094 break;
1099 /* Set the module's input pointer. */
1101 static void
1102 set_module_locus (module_locus *m)
1104 module_column = m->column;
1105 module_line = m->line;
1106 module_pos = m->pos;
1110 /* Get the module's input pointer so that we can restore it later. */
1112 static void
1113 get_module_locus (module_locus *m)
1115 m->column = module_column;
1116 m->line = module_line;
1117 m->pos = module_pos;
1121 /* Get the next character in the module, updating our reckoning of
1122 where we are. */
1124 static int
1125 module_char (void)
1127 const char c = module_content[module_pos++];
1128 if (c == '\0')
1129 bad_module ("Unexpected EOF");
1131 prev_module_line = module_line;
1132 prev_module_column = module_column;
1134 if (c == '\n')
1136 module_line++;
1137 module_column = 0;
1140 module_column++;
1141 return c;
1144 /* Unget a character while remembering the line and column. Works for
1145 a single character only. */
1147 static void
1148 module_unget_char (void)
1150 module_line = prev_module_line;
1151 module_column = prev_module_column;
1152 module_pos--;
1155 /* Parse a string constant. The delimiter is guaranteed to be a
1156 single quote. */
1158 static void
1159 parse_string (void)
1161 int c;
1162 size_t cursz = 30;
1163 size_t len = 0;
1165 atom_string = XNEWVEC (char, cursz);
1167 for ( ; ; )
1169 c = module_char ();
1171 if (c == '\'')
1173 int c2 = module_char ();
1174 if (c2 != '\'')
1176 module_unget_char ();
1177 break;
1181 if (len >= cursz)
1183 cursz *= 2;
1184 atom_string = XRESIZEVEC (char, atom_string, cursz);
1186 atom_string[len] = c;
1187 len++;
1190 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1191 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1195 /* Parse a small integer. */
1197 static void
1198 parse_integer (int c)
1200 atom_int = c - '0';
1202 for (;;)
1204 c = module_char ();
1205 if (!ISDIGIT (c))
1207 module_unget_char ();
1208 break;
1211 atom_int = 10 * atom_int + c - '0';
1212 if (atom_int > 99999999)
1213 bad_module ("Integer overflow");
1219 /* Parse a name. */
1221 static void
1222 parse_name (int c)
1224 char *p;
1225 int len;
1227 p = atom_name;
1229 *p++ = c;
1230 len = 1;
1232 for (;;)
1234 c = module_char ();
1235 if (!ISALNUM (c) && c != '_' && c != '-')
1237 module_unget_char ();
1238 break;
1241 *p++ = c;
1242 if (++len > GFC_MAX_SYMBOL_LEN)
1243 bad_module ("Name too long");
1246 *p = '\0';
1251 /* Read the next atom in the module's input stream. */
1253 static atom_type
1254 parse_atom (void)
1256 int c;
1260 c = module_char ();
1262 while (c == ' ' || c == '\r' || c == '\n');
1264 switch (c)
1266 case '(':
1267 return ATOM_LPAREN;
1269 case ')':
1270 return ATOM_RPAREN;
1272 case '\'':
1273 parse_string ();
1274 return ATOM_STRING;
1276 case '0':
1277 case '1':
1278 case '2':
1279 case '3':
1280 case '4':
1281 case '5':
1282 case '6':
1283 case '7':
1284 case '8':
1285 case '9':
1286 parse_integer (c);
1287 return ATOM_INTEGER;
1289 case 'a':
1290 case 'b':
1291 case 'c':
1292 case 'd':
1293 case 'e':
1294 case 'f':
1295 case 'g':
1296 case 'h':
1297 case 'i':
1298 case 'j':
1299 case 'k':
1300 case 'l':
1301 case 'm':
1302 case 'n':
1303 case 'o':
1304 case 'p':
1305 case 'q':
1306 case 'r':
1307 case 's':
1308 case 't':
1309 case 'u':
1310 case 'v':
1311 case 'w':
1312 case 'x':
1313 case 'y':
1314 case 'z':
1315 case 'A':
1316 case 'B':
1317 case 'C':
1318 case 'D':
1319 case 'E':
1320 case 'F':
1321 case 'G':
1322 case 'H':
1323 case 'I':
1324 case 'J':
1325 case 'K':
1326 case 'L':
1327 case 'M':
1328 case 'N':
1329 case 'O':
1330 case 'P':
1331 case 'Q':
1332 case 'R':
1333 case 'S':
1334 case 'T':
1335 case 'U':
1336 case 'V':
1337 case 'W':
1338 case 'X':
1339 case 'Y':
1340 case 'Z':
1341 parse_name (c);
1342 return ATOM_NAME;
1344 default:
1345 bad_module ("Bad name");
1348 /* Not reached. */
1352 /* Peek at the next atom on the input. */
1354 static atom_type
1355 peek_atom (void)
1357 int c;
1361 c = module_char ();
1363 while (c == ' ' || c == '\r' || c == '\n');
1365 switch (c)
1367 case '(':
1368 module_unget_char ();
1369 return ATOM_LPAREN;
1371 case ')':
1372 module_unget_char ();
1373 return ATOM_RPAREN;
1375 case '\'':
1376 module_unget_char ();
1377 return ATOM_STRING;
1379 case '0':
1380 case '1':
1381 case '2':
1382 case '3':
1383 case '4':
1384 case '5':
1385 case '6':
1386 case '7':
1387 case '8':
1388 case '9':
1389 module_unget_char ();
1390 return ATOM_INTEGER;
1392 case 'a':
1393 case 'b':
1394 case 'c':
1395 case 'd':
1396 case 'e':
1397 case 'f':
1398 case 'g':
1399 case 'h':
1400 case 'i':
1401 case 'j':
1402 case 'k':
1403 case 'l':
1404 case 'm':
1405 case 'n':
1406 case 'o':
1407 case 'p':
1408 case 'q':
1409 case 'r':
1410 case 's':
1411 case 't':
1412 case 'u':
1413 case 'v':
1414 case 'w':
1415 case 'x':
1416 case 'y':
1417 case 'z':
1418 case 'A':
1419 case 'B':
1420 case 'C':
1421 case 'D':
1422 case 'E':
1423 case 'F':
1424 case 'G':
1425 case 'H':
1426 case 'I':
1427 case 'J':
1428 case 'K':
1429 case 'L':
1430 case 'M':
1431 case 'N':
1432 case 'O':
1433 case 'P':
1434 case 'Q':
1435 case 'R':
1436 case 'S':
1437 case 'T':
1438 case 'U':
1439 case 'V':
1440 case 'W':
1441 case 'X':
1442 case 'Y':
1443 case 'Z':
1444 module_unget_char ();
1445 return ATOM_NAME;
1447 default:
1448 bad_module ("Bad name");
1453 /* Read the next atom from the input, requiring that it be a
1454 particular kind. */
1456 static void
1457 require_atom (atom_type type)
1459 atom_type t;
1460 const char *p;
1461 int column, line;
1463 column = module_column;
1464 line = module_line;
1466 t = parse_atom ();
1467 if (t != type)
1469 switch (type)
1471 case ATOM_NAME:
1472 p = _("Expected name");
1473 break;
1474 case ATOM_LPAREN:
1475 p = _("Expected left parenthesis");
1476 break;
1477 case ATOM_RPAREN:
1478 p = _("Expected right parenthesis");
1479 break;
1480 case ATOM_INTEGER:
1481 p = _("Expected integer");
1482 break;
1483 case ATOM_STRING:
1484 p = _("Expected string");
1485 break;
1486 default:
1487 gfc_internal_error ("require_atom(): bad atom type required");
1490 module_column = column;
1491 module_line = line;
1492 bad_module (p);
1497 /* Given a pointer to an mstring array, require that the current input
1498 be one of the strings in the array. We return the enum value. */
1500 static int
1501 find_enum (const mstring *m)
1503 int i;
1505 i = gfc_string2code (m, atom_name);
1506 if (i >= 0)
1507 return i;
1509 bad_module ("find_enum(): Enum not found");
1511 /* Not reached. */
1515 /* Read a string. The caller is responsible for freeing. */
1517 static char*
1518 read_string (void)
1520 char* p;
1521 require_atom (ATOM_STRING);
1522 p = atom_string;
1523 atom_string = NULL;
1524 return p;
1528 /**************** Module output subroutines ***************************/
1530 /* Output a character to a module file. */
1532 static void
1533 write_char (char out)
1535 if (gzputc (module_fp, out) == EOF)
1536 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1538 if (out != '\n')
1539 module_column++;
1540 else
1542 module_column = 1;
1543 module_line++;
1548 /* Write an atom to a module. The line wrapping isn't perfect, but it
1549 should work most of the time. This isn't that big of a deal, since
1550 the file really isn't meant to be read by people anyway. */
1552 static void
1553 write_atom (atom_type atom, const void *v)
1555 char buffer[20];
1556 int i, len;
1557 const char *p;
1559 switch (atom)
1561 case ATOM_STRING:
1562 case ATOM_NAME:
1563 p = (const char *) v;
1564 break;
1566 case ATOM_LPAREN:
1567 p = "(";
1568 break;
1570 case ATOM_RPAREN:
1571 p = ")";
1572 break;
1574 case ATOM_INTEGER:
1575 i = *((const int *) v);
1576 if (i < 0)
1577 gfc_internal_error ("write_atom(): Writing negative integer");
1579 sprintf (buffer, "%d", i);
1580 p = buffer;
1581 break;
1583 default:
1584 gfc_internal_error ("write_atom(): Trying to write dab atom");
1588 if(p == NULL || *p == '\0')
1589 len = 0;
1590 else
1591 len = strlen (p);
1593 if (atom != ATOM_RPAREN)
1595 if (module_column + len > 72)
1596 write_char ('\n');
1597 else
1600 if (last_atom != ATOM_LPAREN && module_column != 1)
1601 write_char (' ');
1605 if (atom == ATOM_STRING)
1606 write_char ('\'');
1608 while (p != NULL && *p)
1610 if (atom == ATOM_STRING && *p == '\'')
1611 write_char ('\'');
1612 write_char (*p++);
1615 if (atom == ATOM_STRING)
1616 write_char ('\'');
1618 last_atom = atom;
1623 /***************** Mid-level I/O subroutines *****************/
1625 /* These subroutines let their caller read or write atoms without
1626 caring about which of the two is actually happening. This lets a
1627 subroutine concentrate on the actual format of the data being
1628 written. */
1630 static void mio_expr (gfc_expr **);
1631 pointer_info *mio_symbol_ref (gfc_symbol **);
1632 pointer_info *mio_interface_rest (gfc_interface **);
1633 static void mio_symtree_ref (gfc_symtree **);
1635 /* Read or write an enumerated value. On writing, we return the input
1636 value for the convenience of callers. We avoid using an integer
1637 pointer because enums are sometimes inside bitfields. */
1639 static int
1640 mio_name (int t, const mstring *m)
1642 if (iomode == IO_OUTPUT)
1643 write_atom (ATOM_NAME, gfc_code2string (m, t));
1644 else
1646 require_atom (ATOM_NAME);
1647 t = find_enum (m);
1650 return t;
1653 /* Specialization of mio_name. */
1655 #define DECL_MIO_NAME(TYPE) \
1656 static inline TYPE \
1657 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1659 return (TYPE) mio_name ((int) t, m); \
1661 #define MIO_NAME(TYPE) mio_name_##TYPE
1663 static void
1664 mio_lparen (void)
1666 if (iomode == IO_OUTPUT)
1667 write_atom (ATOM_LPAREN, NULL);
1668 else
1669 require_atom (ATOM_LPAREN);
1673 static void
1674 mio_rparen (void)
1676 if (iomode == IO_OUTPUT)
1677 write_atom (ATOM_RPAREN, NULL);
1678 else
1679 require_atom (ATOM_RPAREN);
1683 static void
1684 mio_integer (int *ip)
1686 if (iomode == IO_OUTPUT)
1687 write_atom (ATOM_INTEGER, ip);
1688 else
1690 require_atom (ATOM_INTEGER);
1691 *ip = atom_int;
1696 /* Read or write a gfc_intrinsic_op value. */
1698 static void
1699 mio_intrinsic_op (gfc_intrinsic_op* op)
1701 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1702 if (iomode == IO_OUTPUT)
1704 int converted = (int) *op;
1705 write_atom (ATOM_INTEGER, &converted);
1707 else
1709 require_atom (ATOM_INTEGER);
1710 *op = (gfc_intrinsic_op) atom_int;
1715 /* Read or write a character pointer that points to a string on the heap. */
1717 static const char *
1718 mio_allocated_string (const char *s)
1720 if (iomode == IO_OUTPUT)
1722 write_atom (ATOM_STRING, s);
1723 return s;
1725 else
1727 require_atom (ATOM_STRING);
1728 return atom_string;
1733 /* Functions for quoting and unquoting strings. */
1735 static char *
1736 quote_string (const gfc_char_t *s, const size_t slength)
1738 const gfc_char_t *p;
1739 char *res, *q;
1740 size_t len = 0, i;
1742 /* Calculate the length we'll need: a backslash takes two ("\\"),
1743 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1744 for (p = s, i = 0; i < slength; p++, i++)
1746 if (*p == '\\')
1747 len += 2;
1748 else if (!gfc_wide_is_printable (*p))
1749 len += 10;
1750 else
1751 len++;
1754 q = res = XCNEWVEC (char, len + 1);
1755 for (p = s, i = 0; i < slength; p++, i++)
1757 if (*p == '\\')
1758 *q++ = '\\', *q++ = '\\';
1759 else if (!gfc_wide_is_printable (*p))
1761 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1762 (unsigned HOST_WIDE_INT) *p);
1763 q += 10;
1765 else
1766 *q++ = (unsigned char) *p;
1769 res[len] = '\0';
1770 return res;
1773 static gfc_char_t *
1774 unquote_string (const char *s)
1776 size_t len, i;
1777 const char *p;
1778 gfc_char_t *res;
1780 for (p = s, len = 0; *p; p++, len++)
1782 if (*p != '\\')
1783 continue;
1785 if (p[1] == '\\')
1786 p++;
1787 else if (p[1] == 'U')
1788 p += 9; /* That is a "\U????????". */
1789 else
1790 gfc_internal_error ("unquote_string(): got bad string");
1793 res = gfc_get_wide_string (len + 1);
1794 for (i = 0, p = s; i < len; i++, p++)
1796 gcc_assert (*p);
1798 if (*p != '\\')
1799 res[i] = (unsigned char) *p;
1800 else if (p[1] == '\\')
1802 res[i] = (unsigned char) '\\';
1803 p++;
1805 else
1807 /* We read the 8-digits hexadecimal constant that follows. */
1808 int j;
1809 unsigned n;
1810 gfc_char_t c = 0;
1812 gcc_assert (p[1] == 'U');
1813 for (j = 0; j < 8; j++)
1815 c = c << 4;
1816 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1817 c += n;
1820 res[i] = c;
1821 p += 9;
1825 res[len] = '\0';
1826 return res;
1830 /* Read or write a character pointer that points to a wide string on the
1831 heap, performing quoting/unquoting of nonprintable characters using the
1832 form \U???????? (where each ? is a hexadecimal digit).
1833 Length is the length of the string, only known and used in output mode. */
1835 static const gfc_char_t *
1836 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1838 if (iomode == IO_OUTPUT)
1840 char *quoted = quote_string (s, length);
1841 write_atom (ATOM_STRING, quoted);
1842 free (quoted);
1843 return s;
1845 else
1847 gfc_char_t *unquoted;
1849 require_atom (ATOM_STRING);
1850 unquoted = unquote_string (atom_string);
1851 free (atom_string);
1852 return unquoted;
1857 /* Read or write a string that is in static memory. */
1859 static void
1860 mio_pool_string (const char **stringp)
1862 /* TODO: one could write the string only once, and refer to it via a
1863 fixup pointer. */
1865 /* As a special case we have to deal with a NULL string. This
1866 happens for the 'module' member of 'gfc_symbol's that are not in a
1867 module. We read / write these as the empty string. */
1868 if (iomode == IO_OUTPUT)
1870 const char *p = *stringp == NULL ? "" : *stringp;
1871 write_atom (ATOM_STRING, p);
1873 else
1875 require_atom (ATOM_STRING);
1876 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1877 free (atom_string);
1882 /* Read or write a string that is inside of some already-allocated
1883 structure. */
1885 static void
1886 mio_internal_string (char *string)
1888 if (iomode == IO_OUTPUT)
1889 write_atom (ATOM_STRING, string);
1890 else
1892 require_atom (ATOM_STRING);
1893 strcpy (string, atom_string);
1894 free (atom_string);
1899 typedef enum
1900 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1901 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1902 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1903 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1904 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1905 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1906 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1907 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1908 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1909 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1910 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY
1912 ab_attribute;
1914 static const mstring attr_bits[] =
1916 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1917 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1918 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1919 minit ("DIMENSION", AB_DIMENSION),
1920 minit ("CODIMENSION", AB_CODIMENSION),
1921 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1922 minit ("EXTERNAL", AB_EXTERNAL),
1923 minit ("INTRINSIC", AB_INTRINSIC),
1924 minit ("OPTIONAL", AB_OPTIONAL),
1925 minit ("POINTER", AB_POINTER),
1926 minit ("VOLATILE", AB_VOLATILE),
1927 minit ("TARGET", AB_TARGET),
1928 minit ("THREADPRIVATE", AB_THREADPRIVATE),
1929 minit ("DUMMY", AB_DUMMY),
1930 minit ("RESULT", AB_RESULT),
1931 minit ("DATA", AB_DATA),
1932 minit ("IN_NAMELIST", AB_IN_NAMELIST),
1933 minit ("IN_COMMON", AB_IN_COMMON),
1934 minit ("FUNCTION", AB_FUNCTION),
1935 minit ("SUBROUTINE", AB_SUBROUTINE),
1936 minit ("SEQUENCE", AB_SEQUENCE),
1937 minit ("ELEMENTAL", AB_ELEMENTAL),
1938 minit ("PURE", AB_PURE),
1939 minit ("RECURSIVE", AB_RECURSIVE),
1940 minit ("GENERIC", AB_GENERIC),
1941 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
1942 minit ("CRAY_POINTER", AB_CRAY_POINTER),
1943 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
1944 minit ("IS_BIND_C", AB_IS_BIND_C),
1945 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
1946 minit ("IS_ISO_C", AB_IS_ISO_C),
1947 minit ("VALUE", AB_VALUE),
1948 minit ("ALLOC_COMP", AB_ALLOC_COMP),
1949 minit ("COARRAY_COMP", AB_COARRAY_COMP),
1950 minit ("LOCK_COMP", AB_LOCK_COMP),
1951 minit ("POINTER_COMP", AB_POINTER_COMP),
1952 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
1953 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
1954 minit ("ZERO_COMP", AB_ZERO_COMP),
1955 minit ("PROTECTED", AB_PROTECTED),
1956 minit ("ABSTRACT", AB_ABSTRACT),
1957 minit ("IS_CLASS", AB_IS_CLASS),
1958 minit ("PROCEDURE", AB_PROCEDURE),
1959 minit ("PROC_POINTER", AB_PROC_POINTER),
1960 minit ("VTYPE", AB_VTYPE),
1961 minit ("VTAB", AB_VTAB),
1962 minit ("CLASS_POINTER", AB_CLASS_POINTER),
1963 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
1964 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
1965 minit (NULL, -1)
1968 /* For binding attributes. */
1969 static const mstring binding_passing[] =
1971 minit ("PASS", 0),
1972 minit ("NOPASS", 1),
1973 minit (NULL, -1)
1975 static const mstring binding_overriding[] =
1977 minit ("OVERRIDABLE", 0),
1978 minit ("NON_OVERRIDABLE", 1),
1979 minit ("DEFERRED", 2),
1980 minit (NULL, -1)
1982 static const mstring binding_generic[] =
1984 minit ("SPECIFIC", 0),
1985 minit ("GENERIC", 1),
1986 minit (NULL, -1)
1988 static const mstring binding_ppc[] =
1990 minit ("NO_PPC", 0),
1991 minit ("PPC", 1),
1992 minit (NULL, -1)
1995 /* Specialization of mio_name. */
1996 DECL_MIO_NAME (ab_attribute)
1997 DECL_MIO_NAME (ar_type)
1998 DECL_MIO_NAME (array_type)
1999 DECL_MIO_NAME (bt)
2000 DECL_MIO_NAME (expr_t)
2001 DECL_MIO_NAME (gfc_access)
2002 DECL_MIO_NAME (gfc_intrinsic_op)
2003 DECL_MIO_NAME (ifsrc)
2004 DECL_MIO_NAME (save_state)
2005 DECL_MIO_NAME (procedure_type)
2006 DECL_MIO_NAME (ref_type)
2007 DECL_MIO_NAME (sym_flavor)
2008 DECL_MIO_NAME (sym_intent)
2009 #undef DECL_MIO_NAME
2011 /* Symbol attributes are stored in list with the first three elements
2012 being the enumerated fields, while the remaining elements (if any)
2013 indicate the individual attribute bits. The access field is not
2014 saved-- it controls what symbols are exported when a module is
2015 written. */
2017 static void
2018 mio_symbol_attribute (symbol_attribute *attr)
2020 atom_type t;
2021 unsigned ext_attr,extension_level;
2023 mio_lparen ();
2025 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2026 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2027 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2028 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2029 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2031 ext_attr = attr->ext_attr;
2032 mio_integer ((int *) &ext_attr);
2033 attr->ext_attr = ext_attr;
2035 extension_level = attr->extension;
2036 mio_integer ((int *) &extension_level);
2037 attr->extension = extension_level;
2039 if (iomode == IO_OUTPUT)
2041 if (attr->allocatable)
2042 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2043 if (attr->artificial)
2044 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2045 if (attr->asynchronous)
2046 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2047 if (attr->dimension)
2048 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2049 if (attr->codimension)
2050 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2051 if (attr->contiguous)
2052 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2053 if (attr->external)
2054 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2055 if (attr->intrinsic)
2056 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2057 if (attr->optional)
2058 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2059 if (attr->pointer)
2060 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2061 if (attr->class_pointer)
2062 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2063 if (attr->is_protected)
2064 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2065 if (attr->value)
2066 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2067 if (attr->volatile_)
2068 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2069 if (attr->target)
2070 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2071 if (attr->threadprivate)
2072 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2073 if (attr->dummy)
2074 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2075 if (attr->result)
2076 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2077 /* We deliberately don't preserve the "entry" flag. */
2079 if (attr->data)
2080 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2081 if (attr->in_namelist)
2082 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2083 if (attr->in_common)
2084 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2086 if (attr->function)
2087 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2088 if (attr->subroutine)
2089 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2090 if (attr->generic)
2091 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2092 if (attr->abstract)
2093 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2095 if (attr->sequence)
2096 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2097 if (attr->elemental)
2098 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2099 if (attr->pure)
2100 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2101 if (attr->implicit_pure)
2102 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2103 if (attr->unlimited_polymorphic)
2104 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2105 if (attr->recursive)
2106 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2107 if (attr->always_explicit)
2108 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2109 if (attr->cray_pointer)
2110 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2111 if (attr->cray_pointee)
2112 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2113 if (attr->is_bind_c)
2114 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2115 if (attr->is_c_interop)
2116 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2117 if (attr->is_iso_c)
2118 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2119 if (attr->alloc_comp)
2120 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2121 if (attr->pointer_comp)
2122 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2123 if (attr->proc_pointer_comp)
2124 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2125 if (attr->private_comp)
2126 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2127 if (attr->coarray_comp)
2128 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2129 if (attr->lock_comp)
2130 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2131 if (attr->zero_comp)
2132 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2133 if (attr->is_class)
2134 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2135 if (attr->procedure)
2136 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2137 if (attr->proc_pointer)
2138 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2139 if (attr->vtype)
2140 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2141 if (attr->vtab)
2142 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2144 mio_rparen ();
2147 else
2149 for (;;)
2151 t = parse_atom ();
2152 if (t == ATOM_RPAREN)
2153 break;
2154 if (t != ATOM_NAME)
2155 bad_module ("Expected attribute bit name");
2157 switch ((ab_attribute) find_enum (attr_bits))
2159 case AB_ALLOCATABLE:
2160 attr->allocatable = 1;
2161 break;
2162 case AB_ARTIFICIAL:
2163 attr->artificial = 1;
2164 break;
2165 case AB_ASYNCHRONOUS:
2166 attr->asynchronous = 1;
2167 break;
2168 case AB_DIMENSION:
2169 attr->dimension = 1;
2170 break;
2171 case AB_CODIMENSION:
2172 attr->codimension = 1;
2173 break;
2174 case AB_CONTIGUOUS:
2175 attr->contiguous = 1;
2176 break;
2177 case AB_EXTERNAL:
2178 attr->external = 1;
2179 break;
2180 case AB_INTRINSIC:
2181 attr->intrinsic = 1;
2182 break;
2183 case AB_OPTIONAL:
2184 attr->optional = 1;
2185 break;
2186 case AB_POINTER:
2187 attr->pointer = 1;
2188 break;
2189 case AB_CLASS_POINTER:
2190 attr->class_pointer = 1;
2191 break;
2192 case AB_PROTECTED:
2193 attr->is_protected = 1;
2194 break;
2195 case AB_VALUE:
2196 attr->value = 1;
2197 break;
2198 case AB_VOLATILE:
2199 attr->volatile_ = 1;
2200 break;
2201 case AB_TARGET:
2202 attr->target = 1;
2203 break;
2204 case AB_THREADPRIVATE:
2205 attr->threadprivate = 1;
2206 break;
2207 case AB_DUMMY:
2208 attr->dummy = 1;
2209 break;
2210 case AB_RESULT:
2211 attr->result = 1;
2212 break;
2213 case AB_DATA:
2214 attr->data = 1;
2215 break;
2216 case AB_IN_NAMELIST:
2217 attr->in_namelist = 1;
2218 break;
2219 case AB_IN_COMMON:
2220 attr->in_common = 1;
2221 break;
2222 case AB_FUNCTION:
2223 attr->function = 1;
2224 break;
2225 case AB_SUBROUTINE:
2226 attr->subroutine = 1;
2227 break;
2228 case AB_GENERIC:
2229 attr->generic = 1;
2230 break;
2231 case AB_ABSTRACT:
2232 attr->abstract = 1;
2233 break;
2234 case AB_SEQUENCE:
2235 attr->sequence = 1;
2236 break;
2237 case AB_ELEMENTAL:
2238 attr->elemental = 1;
2239 break;
2240 case AB_PURE:
2241 attr->pure = 1;
2242 break;
2243 case AB_IMPLICIT_PURE:
2244 attr->implicit_pure = 1;
2245 break;
2246 case AB_UNLIMITED_POLY:
2247 attr->unlimited_polymorphic = 1;
2248 break;
2249 case AB_RECURSIVE:
2250 attr->recursive = 1;
2251 break;
2252 case AB_ALWAYS_EXPLICIT:
2253 attr->always_explicit = 1;
2254 break;
2255 case AB_CRAY_POINTER:
2256 attr->cray_pointer = 1;
2257 break;
2258 case AB_CRAY_POINTEE:
2259 attr->cray_pointee = 1;
2260 break;
2261 case AB_IS_BIND_C:
2262 attr->is_bind_c = 1;
2263 break;
2264 case AB_IS_C_INTEROP:
2265 attr->is_c_interop = 1;
2266 break;
2267 case AB_IS_ISO_C:
2268 attr->is_iso_c = 1;
2269 break;
2270 case AB_ALLOC_COMP:
2271 attr->alloc_comp = 1;
2272 break;
2273 case AB_COARRAY_COMP:
2274 attr->coarray_comp = 1;
2275 break;
2276 case AB_LOCK_COMP:
2277 attr->lock_comp = 1;
2278 break;
2279 case AB_POINTER_COMP:
2280 attr->pointer_comp = 1;
2281 break;
2282 case AB_PROC_POINTER_COMP:
2283 attr->proc_pointer_comp = 1;
2284 break;
2285 case AB_PRIVATE_COMP:
2286 attr->private_comp = 1;
2287 break;
2288 case AB_ZERO_COMP:
2289 attr->zero_comp = 1;
2290 break;
2291 case AB_IS_CLASS:
2292 attr->is_class = 1;
2293 break;
2294 case AB_PROCEDURE:
2295 attr->procedure = 1;
2296 break;
2297 case AB_PROC_POINTER:
2298 attr->proc_pointer = 1;
2299 break;
2300 case AB_VTYPE:
2301 attr->vtype = 1;
2302 break;
2303 case AB_VTAB:
2304 attr->vtab = 1;
2305 break;
2312 static const mstring bt_types[] = {
2313 minit ("INTEGER", BT_INTEGER),
2314 minit ("REAL", BT_REAL),
2315 minit ("COMPLEX", BT_COMPLEX),
2316 minit ("LOGICAL", BT_LOGICAL),
2317 minit ("CHARACTER", BT_CHARACTER),
2318 minit ("DERIVED", BT_DERIVED),
2319 minit ("CLASS", BT_CLASS),
2320 minit ("PROCEDURE", BT_PROCEDURE),
2321 minit ("UNKNOWN", BT_UNKNOWN),
2322 minit ("VOID", BT_VOID),
2323 minit ("ASSUMED", BT_ASSUMED),
2324 minit (NULL, -1)
2328 static void
2329 mio_charlen (gfc_charlen **clp)
2331 gfc_charlen *cl;
2333 mio_lparen ();
2335 if (iomode == IO_OUTPUT)
2337 cl = *clp;
2338 if (cl != NULL)
2339 mio_expr (&cl->length);
2341 else
2343 if (peek_atom () != ATOM_RPAREN)
2345 cl = gfc_new_charlen (gfc_current_ns, NULL);
2346 mio_expr (&cl->length);
2347 *clp = cl;
2351 mio_rparen ();
2355 /* See if a name is a generated name. */
2357 static int
2358 check_unique_name (const char *name)
2360 return *name == '@';
2364 static void
2365 mio_typespec (gfc_typespec *ts)
2367 mio_lparen ();
2369 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2371 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2372 mio_integer (&ts->kind);
2373 else
2374 mio_symbol_ref (&ts->u.derived);
2376 mio_symbol_ref (&ts->interface);
2378 /* Add info for C interop and is_iso_c. */
2379 mio_integer (&ts->is_c_interop);
2380 mio_integer (&ts->is_iso_c);
2382 /* If the typespec is for an identifier either from iso_c_binding, or
2383 a constant that was initialized to an identifier from it, use the
2384 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2385 if (ts->is_iso_c)
2386 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2387 else
2388 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2390 if (ts->type != BT_CHARACTER)
2392 /* ts->u.cl is only valid for BT_CHARACTER. */
2393 mio_lparen ();
2394 mio_rparen ();
2396 else
2397 mio_charlen (&ts->u.cl);
2399 /* So as not to disturb the existing API, use an ATOM_NAME to
2400 transmit deferred characteristic for characters (F2003). */
2401 if (iomode == IO_OUTPUT)
2403 if (ts->type == BT_CHARACTER && ts->deferred)
2404 write_atom (ATOM_NAME, "DEFERRED_CL");
2406 else if (peek_atom () != ATOM_RPAREN)
2408 if (parse_atom () != ATOM_NAME)
2409 bad_module ("Expected string");
2410 ts->deferred = 1;
2413 mio_rparen ();
2417 static const mstring array_spec_types[] = {
2418 minit ("EXPLICIT", AS_EXPLICIT),
2419 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2420 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2421 minit ("DEFERRED", AS_DEFERRED),
2422 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2423 minit (NULL, -1)
2427 static void
2428 mio_array_spec (gfc_array_spec **asp)
2430 gfc_array_spec *as;
2431 int i;
2433 mio_lparen ();
2435 if (iomode == IO_OUTPUT)
2437 int rank;
2439 if (*asp == NULL)
2440 goto done;
2441 as = *asp;
2443 /* mio_integer expects nonnegative values. */
2444 rank = as->rank > 0 ? as->rank : 0;
2445 mio_integer (&rank);
2447 else
2449 if (peek_atom () == ATOM_RPAREN)
2451 *asp = NULL;
2452 goto done;
2455 *asp = as = gfc_get_array_spec ();
2456 mio_integer (&as->rank);
2459 mio_integer (&as->corank);
2460 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2462 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2463 as->rank = -1;
2464 if (iomode == IO_INPUT && as->corank)
2465 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2467 if (as->rank + as->corank > 0)
2468 for (i = 0; i < as->rank + as->corank; i++)
2470 mio_expr (&as->lower[i]);
2471 mio_expr (&as->upper[i]);
2474 done:
2475 mio_rparen ();
2479 /* Given a pointer to an array reference structure (which lives in a
2480 gfc_ref structure), find the corresponding array specification
2481 structure. Storing the pointer in the ref structure doesn't quite
2482 work when loading from a module. Generating code for an array
2483 reference also needs more information than just the array spec. */
2485 static const mstring array_ref_types[] = {
2486 minit ("FULL", AR_FULL),
2487 minit ("ELEMENT", AR_ELEMENT),
2488 minit ("SECTION", AR_SECTION),
2489 minit (NULL, -1)
2493 static void
2494 mio_array_ref (gfc_array_ref *ar)
2496 int i;
2498 mio_lparen ();
2499 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2500 mio_integer (&ar->dimen);
2502 switch (ar->type)
2504 case AR_FULL:
2505 break;
2507 case AR_ELEMENT:
2508 for (i = 0; i < ar->dimen; i++)
2509 mio_expr (&ar->start[i]);
2511 break;
2513 case AR_SECTION:
2514 for (i = 0; i < ar->dimen; i++)
2516 mio_expr (&ar->start[i]);
2517 mio_expr (&ar->end[i]);
2518 mio_expr (&ar->stride[i]);
2521 break;
2523 case AR_UNKNOWN:
2524 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2527 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2528 we can't call mio_integer directly. Instead loop over each element
2529 and cast it to/from an integer. */
2530 if (iomode == IO_OUTPUT)
2532 for (i = 0; i < ar->dimen; i++)
2534 int tmp = (int)ar->dimen_type[i];
2535 write_atom (ATOM_INTEGER, &tmp);
2538 else
2540 for (i = 0; i < ar->dimen; i++)
2542 require_atom (ATOM_INTEGER);
2543 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2547 if (iomode == IO_INPUT)
2549 ar->where = gfc_current_locus;
2551 for (i = 0; i < ar->dimen; i++)
2552 ar->c_where[i] = gfc_current_locus;
2555 mio_rparen ();
2559 /* Saves or restores a pointer. The pointer is converted back and
2560 forth from an integer. We return the pointer_info pointer so that
2561 the caller can take additional action based on the pointer type. */
2563 static pointer_info *
2564 mio_pointer_ref (void *gp)
2566 pointer_info *p;
2568 if (iomode == IO_OUTPUT)
2570 p = get_pointer (*((char **) gp));
2571 write_atom (ATOM_INTEGER, &p->integer);
2573 else
2575 require_atom (ATOM_INTEGER);
2576 p = add_fixup (atom_int, gp);
2579 return p;
2583 /* Save and load references to components that occur within
2584 expressions. We have to describe these references by a number and
2585 by name. The number is necessary for forward references during
2586 reading, and the name is necessary if the symbol already exists in
2587 the namespace and is not loaded again. */
2589 static void
2590 mio_component_ref (gfc_component **cp, gfc_symbol *sym)
2592 char name[GFC_MAX_SYMBOL_LEN + 1];
2593 gfc_component *q;
2594 pointer_info *p;
2596 p = mio_pointer_ref (cp);
2597 if (p->type == P_UNKNOWN)
2598 p->type = P_COMPONENT;
2600 if (iomode == IO_OUTPUT)
2601 mio_pool_string (&(*cp)->name);
2602 else
2604 mio_internal_string (name);
2606 if (sym && sym->attr.is_class)
2607 sym = sym->components->ts.u.derived;
2609 /* It can happen that a component reference can be read before the
2610 associated derived type symbol has been loaded. Return now and
2611 wait for a later iteration of load_needed. */
2612 if (sym == NULL)
2613 return;
2615 if (sym->components != NULL && p->u.pointer == NULL)
2617 /* Symbol already loaded, so search by name. */
2618 q = gfc_find_component (sym, name, true, true);
2620 if (q)
2621 associate_integer_pointer (p, q);
2624 /* Make sure this symbol will eventually be loaded. */
2625 p = find_pointer2 (sym);
2626 if (p->u.rsym.state == UNUSED)
2627 p->u.rsym.state = NEEDED;
2632 static void mio_namespace_ref (gfc_namespace **nsp);
2633 static void mio_formal_arglist (gfc_formal_arglist **formal);
2634 static void mio_typebound_proc (gfc_typebound_proc** proc);
2636 static void
2637 mio_component (gfc_component *c, int vtype)
2639 pointer_info *p;
2640 int n;
2642 mio_lparen ();
2644 if (iomode == IO_OUTPUT)
2646 p = get_pointer (c);
2647 mio_integer (&p->integer);
2649 else
2651 mio_integer (&n);
2652 p = get_integer (n);
2653 associate_integer_pointer (p, c);
2656 if (p->type == P_UNKNOWN)
2657 p->type = P_COMPONENT;
2659 mio_pool_string (&c->name);
2660 mio_typespec (&c->ts);
2661 mio_array_spec (&c->as);
2663 mio_symbol_attribute (&c->attr);
2664 if (c->ts.type == BT_CLASS)
2665 c->attr.class_ok = 1;
2666 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2668 if (!vtype || strcmp (c->name, "_final") == 0
2669 || strcmp (c->name, "_hash") == 0)
2670 mio_expr (&c->initializer);
2672 if (c->attr.proc_pointer)
2673 mio_typebound_proc (&c->tb);
2675 mio_rparen ();
2679 static void
2680 mio_component_list (gfc_component **cp, int vtype)
2682 gfc_component *c, *tail;
2684 mio_lparen ();
2686 if (iomode == IO_OUTPUT)
2688 for (c = *cp; c; c = c->next)
2689 mio_component (c, vtype);
2691 else
2693 *cp = NULL;
2694 tail = NULL;
2696 for (;;)
2698 if (peek_atom () == ATOM_RPAREN)
2699 break;
2701 c = gfc_get_component ();
2702 mio_component (c, vtype);
2704 if (tail == NULL)
2705 *cp = c;
2706 else
2707 tail->next = c;
2709 tail = c;
2713 mio_rparen ();
2717 static void
2718 mio_actual_arg (gfc_actual_arglist *a)
2720 mio_lparen ();
2721 mio_pool_string (&a->name);
2722 mio_expr (&a->expr);
2723 mio_rparen ();
2727 static void
2728 mio_actual_arglist (gfc_actual_arglist **ap)
2730 gfc_actual_arglist *a, *tail;
2732 mio_lparen ();
2734 if (iomode == IO_OUTPUT)
2736 for (a = *ap; a; a = a->next)
2737 mio_actual_arg (a);
2740 else
2742 tail = NULL;
2744 for (;;)
2746 if (peek_atom () != ATOM_LPAREN)
2747 break;
2749 a = gfc_get_actual_arglist ();
2751 if (tail == NULL)
2752 *ap = a;
2753 else
2754 tail->next = a;
2756 tail = a;
2757 mio_actual_arg (a);
2761 mio_rparen ();
2765 /* Read and write formal argument lists. */
2767 static void
2768 mio_formal_arglist (gfc_formal_arglist **formal)
2770 gfc_formal_arglist *f, *tail;
2772 mio_lparen ();
2774 if (iomode == IO_OUTPUT)
2776 for (f = *formal; f; f = f->next)
2777 mio_symbol_ref (&f->sym);
2779 else
2781 *formal = tail = NULL;
2783 while (peek_atom () != ATOM_RPAREN)
2785 f = gfc_get_formal_arglist ();
2786 mio_symbol_ref (&f->sym);
2788 if (*formal == NULL)
2789 *formal = f;
2790 else
2791 tail->next = f;
2793 tail = f;
2797 mio_rparen ();
2801 /* Save or restore a reference to a symbol node. */
2803 pointer_info *
2804 mio_symbol_ref (gfc_symbol **symp)
2806 pointer_info *p;
2808 p = mio_pointer_ref (symp);
2809 if (p->type == P_UNKNOWN)
2810 p->type = P_SYMBOL;
2812 if (iomode == IO_OUTPUT)
2814 if (p->u.wsym.state == UNREFERENCED)
2815 p->u.wsym.state = NEEDS_WRITE;
2817 else
2819 if (p->u.rsym.state == UNUSED)
2820 p->u.rsym.state = NEEDED;
2822 return p;
2826 /* Save or restore a reference to a symtree node. */
2828 static void
2829 mio_symtree_ref (gfc_symtree **stp)
2831 pointer_info *p;
2832 fixup_t *f;
2834 if (iomode == IO_OUTPUT)
2835 mio_symbol_ref (&(*stp)->n.sym);
2836 else
2838 require_atom (ATOM_INTEGER);
2839 p = get_integer (atom_int);
2841 /* An unused equivalence member; make a symbol and a symtree
2842 for it. */
2843 if (in_load_equiv && p->u.rsym.symtree == NULL)
2845 /* Since this is not used, it must have a unique name. */
2846 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2848 /* Make the symbol. */
2849 if (p->u.rsym.sym == NULL)
2851 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2852 gfc_current_ns);
2853 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2856 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2857 p->u.rsym.symtree->n.sym->refs++;
2858 p->u.rsym.referenced = 1;
2860 /* If the symbol is PRIVATE and in COMMON, load_commons will
2861 generate a fixup symbol, which must be associated. */
2862 if (p->fixup)
2863 resolve_fixups (p->fixup, p->u.rsym.sym);
2864 p->fixup = NULL;
2867 if (p->type == P_UNKNOWN)
2868 p->type = P_SYMBOL;
2870 if (p->u.rsym.state == UNUSED)
2871 p->u.rsym.state = NEEDED;
2873 if (p->u.rsym.symtree != NULL)
2875 *stp = p->u.rsym.symtree;
2877 else
2879 f = XCNEW (fixup_t);
2881 f->next = p->u.rsym.stfixup;
2882 p->u.rsym.stfixup = f;
2884 f->pointer = (void **) stp;
2890 static void
2891 mio_iterator (gfc_iterator **ip)
2893 gfc_iterator *iter;
2895 mio_lparen ();
2897 if (iomode == IO_OUTPUT)
2899 if (*ip == NULL)
2900 goto done;
2902 else
2904 if (peek_atom () == ATOM_RPAREN)
2906 *ip = NULL;
2907 goto done;
2910 *ip = gfc_get_iterator ();
2913 iter = *ip;
2915 mio_expr (&iter->var);
2916 mio_expr (&iter->start);
2917 mio_expr (&iter->end);
2918 mio_expr (&iter->step);
2920 done:
2921 mio_rparen ();
2925 static void
2926 mio_constructor (gfc_constructor_base *cp)
2928 gfc_constructor *c;
2930 mio_lparen ();
2932 if (iomode == IO_OUTPUT)
2934 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2936 mio_lparen ();
2937 mio_expr (&c->expr);
2938 mio_iterator (&c->iterator);
2939 mio_rparen ();
2942 else
2944 while (peek_atom () != ATOM_RPAREN)
2946 c = gfc_constructor_append_expr (cp, NULL, NULL);
2948 mio_lparen ();
2949 mio_expr (&c->expr);
2950 mio_iterator (&c->iterator);
2951 mio_rparen ();
2955 mio_rparen ();
2959 static const mstring ref_types[] = {
2960 minit ("ARRAY", REF_ARRAY),
2961 minit ("COMPONENT", REF_COMPONENT),
2962 minit ("SUBSTRING", REF_SUBSTRING),
2963 minit (NULL, -1)
2967 static void
2968 mio_ref (gfc_ref **rp)
2970 gfc_ref *r;
2972 mio_lparen ();
2974 r = *rp;
2975 r->type = MIO_NAME (ref_type) (r->type, ref_types);
2977 switch (r->type)
2979 case REF_ARRAY:
2980 mio_array_ref (&r->u.ar);
2981 break;
2983 case REF_COMPONENT:
2984 mio_symbol_ref (&r->u.c.sym);
2985 mio_component_ref (&r->u.c.component, r->u.c.sym);
2986 break;
2988 case REF_SUBSTRING:
2989 mio_expr (&r->u.ss.start);
2990 mio_expr (&r->u.ss.end);
2991 mio_charlen (&r->u.ss.length);
2992 break;
2995 mio_rparen ();
2999 static void
3000 mio_ref_list (gfc_ref **rp)
3002 gfc_ref *ref, *head, *tail;
3004 mio_lparen ();
3006 if (iomode == IO_OUTPUT)
3008 for (ref = *rp; ref; ref = ref->next)
3009 mio_ref (&ref);
3011 else
3013 head = tail = NULL;
3015 while (peek_atom () != ATOM_RPAREN)
3017 if (head == NULL)
3018 head = tail = gfc_get_ref ();
3019 else
3021 tail->next = gfc_get_ref ();
3022 tail = tail->next;
3025 mio_ref (&tail);
3028 *rp = head;
3031 mio_rparen ();
3035 /* Read and write an integer value. */
3037 static void
3038 mio_gmp_integer (mpz_t *integer)
3040 char *p;
3042 if (iomode == IO_INPUT)
3044 if (parse_atom () != ATOM_STRING)
3045 bad_module ("Expected integer string");
3047 mpz_init (*integer);
3048 if (mpz_set_str (*integer, atom_string, 10))
3049 bad_module ("Error converting integer");
3051 free (atom_string);
3053 else
3055 p = mpz_get_str (NULL, 10, *integer);
3056 write_atom (ATOM_STRING, p);
3057 free (p);
3062 static void
3063 mio_gmp_real (mpfr_t *real)
3065 mp_exp_t exponent;
3066 char *p;
3068 if (iomode == IO_INPUT)
3070 if (parse_atom () != ATOM_STRING)
3071 bad_module ("Expected real string");
3073 mpfr_init (*real);
3074 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3075 free (atom_string);
3077 else
3079 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3081 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3083 write_atom (ATOM_STRING, p);
3084 free (p);
3085 return;
3088 atom_string = XCNEWVEC (char, strlen (p) + 20);
3090 sprintf (atom_string, "0.%s@%ld", p, exponent);
3092 /* Fix negative numbers. */
3093 if (atom_string[2] == '-')
3095 atom_string[0] = '-';
3096 atom_string[1] = '0';
3097 atom_string[2] = '.';
3100 write_atom (ATOM_STRING, atom_string);
3102 free (atom_string);
3103 free (p);
3108 /* Save and restore the shape of an array constructor. */
3110 static void
3111 mio_shape (mpz_t **pshape, int rank)
3113 mpz_t *shape;
3114 atom_type t;
3115 int n;
3117 /* A NULL shape is represented by (). */
3118 mio_lparen ();
3120 if (iomode == IO_OUTPUT)
3122 shape = *pshape;
3123 if (!shape)
3125 mio_rparen ();
3126 return;
3129 else
3131 t = peek_atom ();
3132 if (t == ATOM_RPAREN)
3134 *pshape = NULL;
3135 mio_rparen ();
3136 return;
3139 shape = gfc_get_shape (rank);
3140 *pshape = shape;
3143 for (n = 0; n < rank; n++)
3144 mio_gmp_integer (&shape[n]);
3146 mio_rparen ();
3150 static const mstring expr_types[] = {
3151 minit ("OP", EXPR_OP),
3152 minit ("FUNCTION", EXPR_FUNCTION),
3153 minit ("CONSTANT", EXPR_CONSTANT),
3154 minit ("VARIABLE", EXPR_VARIABLE),
3155 minit ("SUBSTRING", EXPR_SUBSTRING),
3156 minit ("STRUCTURE", EXPR_STRUCTURE),
3157 minit ("ARRAY", EXPR_ARRAY),
3158 minit ("NULL", EXPR_NULL),
3159 minit ("COMPCALL", EXPR_COMPCALL),
3160 minit (NULL, -1)
3163 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3164 generic operators, not in expressions. INTRINSIC_USER is also
3165 replaced by the correct function name by the time we see it. */
3167 static const mstring intrinsics[] =
3169 minit ("UPLUS", INTRINSIC_UPLUS),
3170 minit ("UMINUS", INTRINSIC_UMINUS),
3171 minit ("PLUS", INTRINSIC_PLUS),
3172 minit ("MINUS", INTRINSIC_MINUS),
3173 minit ("TIMES", INTRINSIC_TIMES),
3174 minit ("DIVIDE", INTRINSIC_DIVIDE),
3175 minit ("POWER", INTRINSIC_POWER),
3176 minit ("CONCAT", INTRINSIC_CONCAT),
3177 minit ("AND", INTRINSIC_AND),
3178 minit ("OR", INTRINSIC_OR),
3179 minit ("EQV", INTRINSIC_EQV),
3180 minit ("NEQV", INTRINSIC_NEQV),
3181 minit ("EQ_SIGN", INTRINSIC_EQ),
3182 minit ("EQ", INTRINSIC_EQ_OS),
3183 minit ("NE_SIGN", INTRINSIC_NE),
3184 minit ("NE", INTRINSIC_NE_OS),
3185 minit ("GT_SIGN", INTRINSIC_GT),
3186 minit ("GT", INTRINSIC_GT_OS),
3187 minit ("GE_SIGN", INTRINSIC_GE),
3188 minit ("GE", INTRINSIC_GE_OS),
3189 minit ("LT_SIGN", INTRINSIC_LT),
3190 minit ("LT", INTRINSIC_LT_OS),
3191 minit ("LE_SIGN", INTRINSIC_LE),
3192 minit ("LE", INTRINSIC_LE_OS),
3193 minit ("NOT", INTRINSIC_NOT),
3194 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3195 minit (NULL, -1)
3199 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3201 static void
3202 fix_mio_expr (gfc_expr *e)
3204 gfc_symtree *ns_st = NULL;
3205 const char *fname;
3207 if (iomode != IO_OUTPUT)
3208 return;
3210 if (e->symtree)
3212 /* If this is a symtree for a symbol that came from a contained module
3213 namespace, it has a unique name and we should look in the current
3214 namespace to see if the required, non-contained symbol is available
3215 yet. If so, the latter should be written. */
3216 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3218 const char *name = e->symtree->n.sym->name;
3219 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3220 name = dt_upper_string (name);
3221 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3224 /* On the other hand, if the existing symbol is the module name or the
3225 new symbol is a dummy argument, do not do the promotion. */
3226 if (ns_st && ns_st->n.sym
3227 && ns_st->n.sym->attr.flavor != FL_MODULE
3228 && !e->symtree->n.sym->attr.dummy)
3229 e->symtree = ns_st;
3231 else if (e->expr_type == EXPR_FUNCTION && e->value.function.name)
3233 gfc_symbol *sym;
3235 /* In some circumstances, a function used in an initialization
3236 expression, in one use associated module, can fail to be
3237 coupled to its symtree when used in a specification
3238 expression in another module. */
3239 fname = e->value.function.esym ? e->value.function.esym->name
3240 : e->value.function.isym->name;
3241 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3243 if (e->symtree)
3244 return;
3246 /* This is probably a reference to a private procedure from another
3247 module. To prevent a segfault, make a generic with no specific
3248 instances. If this module is used, without the required
3249 specific coming from somewhere, the appropriate error message
3250 is issued. */
3251 gfc_get_symbol (fname, gfc_current_ns, &sym);
3252 sym->attr.flavor = FL_PROCEDURE;
3253 sym->attr.generic = 1;
3254 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3255 gfc_commit_symbol (sym);
3260 /* Read and write expressions. The form "()" is allowed to indicate a
3261 NULL expression. */
3263 static void
3264 mio_expr (gfc_expr **ep)
3266 gfc_expr *e;
3267 atom_type t;
3268 int flag;
3270 mio_lparen ();
3272 if (iomode == IO_OUTPUT)
3274 if (*ep == NULL)
3276 mio_rparen ();
3277 return;
3280 e = *ep;
3281 MIO_NAME (expr_t) (e->expr_type, expr_types);
3283 else
3285 t = parse_atom ();
3286 if (t == ATOM_RPAREN)
3288 *ep = NULL;
3289 return;
3292 if (t != ATOM_NAME)
3293 bad_module ("Expected expression type");
3295 e = *ep = gfc_get_expr ();
3296 e->where = gfc_current_locus;
3297 e->expr_type = (expr_t) find_enum (expr_types);
3300 mio_typespec (&e->ts);
3301 mio_integer (&e->rank);
3303 fix_mio_expr (e);
3305 switch (e->expr_type)
3307 case EXPR_OP:
3308 e->value.op.op
3309 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3311 switch (e->value.op.op)
3313 case INTRINSIC_UPLUS:
3314 case INTRINSIC_UMINUS:
3315 case INTRINSIC_NOT:
3316 case INTRINSIC_PARENTHESES:
3317 mio_expr (&e->value.op.op1);
3318 break;
3320 case INTRINSIC_PLUS:
3321 case INTRINSIC_MINUS:
3322 case INTRINSIC_TIMES:
3323 case INTRINSIC_DIVIDE:
3324 case INTRINSIC_POWER:
3325 case INTRINSIC_CONCAT:
3326 case INTRINSIC_AND:
3327 case INTRINSIC_OR:
3328 case INTRINSIC_EQV:
3329 case INTRINSIC_NEQV:
3330 case INTRINSIC_EQ:
3331 case INTRINSIC_EQ_OS:
3332 case INTRINSIC_NE:
3333 case INTRINSIC_NE_OS:
3334 case INTRINSIC_GT:
3335 case INTRINSIC_GT_OS:
3336 case INTRINSIC_GE:
3337 case INTRINSIC_GE_OS:
3338 case INTRINSIC_LT:
3339 case INTRINSIC_LT_OS:
3340 case INTRINSIC_LE:
3341 case INTRINSIC_LE_OS:
3342 mio_expr (&e->value.op.op1);
3343 mio_expr (&e->value.op.op2);
3344 break;
3346 default:
3347 bad_module ("Bad operator");
3350 break;
3352 case EXPR_FUNCTION:
3353 mio_symtree_ref (&e->symtree);
3354 mio_actual_arglist (&e->value.function.actual);
3356 if (iomode == IO_OUTPUT)
3358 e->value.function.name
3359 = mio_allocated_string (e->value.function.name);
3360 flag = e->value.function.esym != NULL;
3361 mio_integer (&flag);
3362 if (flag)
3363 mio_symbol_ref (&e->value.function.esym);
3364 else
3365 write_atom (ATOM_STRING, e->value.function.isym->name);
3367 else
3369 require_atom (ATOM_STRING);
3370 e->value.function.name = gfc_get_string (atom_string);
3371 free (atom_string);
3373 mio_integer (&flag);
3374 if (flag)
3375 mio_symbol_ref (&e->value.function.esym);
3376 else
3378 require_atom (ATOM_STRING);
3379 e->value.function.isym = gfc_find_function (atom_string);
3380 free (atom_string);
3384 break;
3386 case EXPR_VARIABLE:
3387 mio_symtree_ref (&e->symtree);
3388 mio_ref_list (&e->ref);
3389 break;
3391 case EXPR_SUBSTRING:
3392 e->value.character.string
3393 = CONST_CAST (gfc_char_t *,
3394 mio_allocated_wide_string (e->value.character.string,
3395 e->value.character.length));
3396 mio_ref_list (&e->ref);
3397 break;
3399 case EXPR_STRUCTURE:
3400 case EXPR_ARRAY:
3401 mio_constructor (&e->value.constructor);
3402 mio_shape (&e->shape, e->rank);
3403 break;
3405 case EXPR_CONSTANT:
3406 switch (e->ts.type)
3408 case BT_INTEGER:
3409 mio_gmp_integer (&e->value.integer);
3410 break;
3412 case BT_REAL:
3413 gfc_set_model_kind (e->ts.kind);
3414 mio_gmp_real (&e->value.real);
3415 break;
3417 case BT_COMPLEX:
3418 gfc_set_model_kind (e->ts.kind);
3419 mio_gmp_real (&mpc_realref (e->value.complex));
3420 mio_gmp_real (&mpc_imagref (e->value.complex));
3421 break;
3423 case BT_LOGICAL:
3424 mio_integer (&e->value.logical);
3425 break;
3427 case BT_CHARACTER:
3428 mio_integer (&e->value.character.length);
3429 e->value.character.string
3430 = CONST_CAST (gfc_char_t *,
3431 mio_allocated_wide_string (e->value.character.string,
3432 e->value.character.length));
3433 break;
3435 default:
3436 bad_module ("Bad type in constant expression");
3439 break;
3441 case EXPR_NULL:
3442 break;
3444 case EXPR_COMPCALL:
3445 case EXPR_PPC:
3446 gcc_unreachable ();
3447 break;
3450 mio_rparen ();
3454 /* Read and write namelists. */
3456 static void
3457 mio_namelist (gfc_symbol *sym)
3459 gfc_namelist *n, *m;
3460 const char *check_name;
3462 mio_lparen ();
3464 if (iomode == IO_OUTPUT)
3466 for (n = sym->namelist; n; n = n->next)
3467 mio_symbol_ref (&n->sym);
3469 else
3471 /* This departure from the standard is flagged as an error.
3472 It does, in fact, work correctly. TODO: Allow it
3473 conditionally? */
3474 if (sym->attr.flavor == FL_NAMELIST)
3476 check_name = find_use_name (sym->name, false);
3477 if (check_name && strcmp (check_name, sym->name) != 0)
3478 gfc_error ("Namelist %s cannot be renamed by USE "
3479 "association to %s", sym->name, check_name);
3482 m = NULL;
3483 while (peek_atom () != ATOM_RPAREN)
3485 n = gfc_get_namelist ();
3486 mio_symbol_ref (&n->sym);
3488 if (sym->namelist == NULL)
3489 sym->namelist = n;
3490 else
3491 m->next = n;
3493 m = n;
3495 sym->namelist_tail = m;
3498 mio_rparen ();
3502 /* Save/restore lists of gfc_interface structures. When loading an
3503 interface, we are really appending to the existing list of
3504 interfaces. Checking for duplicate and ambiguous interfaces has to
3505 be done later when all symbols have been loaded. */
3507 pointer_info *
3508 mio_interface_rest (gfc_interface **ip)
3510 gfc_interface *tail, *p;
3511 pointer_info *pi = NULL;
3513 if (iomode == IO_OUTPUT)
3515 if (ip != NULL)
3516 for (p = *ip; p; p = p->next)
3517 mio_symbol_ref (&p->sym);
3519 else
3521 if (*ip == NULL)
3522 tail = NULL;
3523 else
3525 tail = *ip;
3526 while (tail->next)
3527 tail = tail->next;
3530 for (;;)
3532 if (peek_atom () == ATOM_RPAREN)
3533 break;
3535 p = gfc_get_interface ();
3536 p->where = gfc_current_locus;
3537 pi = mio_symbol_ref (&p->sym);
3539 if (tail == NULL)
3540 *ip = p;
3541 else
3542 tail->next = p;
3544 tail = p;
3548 mio_rparen ();
3549 return pi;
3553 /* Save/restore a nameless operator interface. */
3555 static void
3556 mio_interface (gfc_interface **ip)
3558 mio_lparen ();
3559 mio_interface_rest (ip);
3563 /* Save/restore a named operator interface. */
3565 static void
3566 mio_symbol_interface (const char **name, const char **module,
3567 gfc_interface **ip)
3569 mio_lparen ();
3570 mio_pool_string (name);
3571 mio_pool_string (module);
3572 mio_interface_rest (ip);
3576 static void
3577 mio_namespace_ref (gfc_namespace **nsp)
3579 gfc_namespace *ns;
3580 pointer_info *p;
3582 p = mio_pointer_ref (nsp);
3584 if (p->type == P_UNKNOWN)
3585 p->type = P_NAMESPACE;
3587 if (iomode == IO_INPUT && p->integer != 0)
3589 ns = (gfc_namespace *) p->u.pointer;
3590 if (ns == NULL)
3592 ns = gfc_get_namespace (NULL, 0);
3593 associate_integer_pointer (p, ns);
3595 else
3596 ns->refs++;
3601 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3603 static gfc_namespace* current_f2k_derived;
3605 static void
3606 mio_typebound_proc (gfc_typebound_proc** proc)
3608 int flag;
3609 int overriding_flag;
3611 if (iomode == IO_INPUT)
3613 *proc = gfc_get_typebound_proc (NULL);
3614 (*proc)->where = gfc_current_locus;
3616 gcc_assert (*proc);
3618 mio_lparen ();
3620 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3622 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3623 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3624 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3625 overriding_flag = mio_name (overriding_flag, binding_overriding);
3626 (*proc)->deferred = ((overriding_flag & 2) != 0);
3627 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3628 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3630 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3631 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3632 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3634 mio_pool_string (&((*proc)->pass_arg));
3636 flag = (int) (*proc)->pass_arg_num;
3637 mio_integer (&flag);
3638 (*proc)->pass_arg_num = (unsigned) flag;
3640 if ((*proc)->is_generic)
3642 gfc_tbp_generic* g;
3643 int iop;
3645 mio_lparen ();
3647 if (iomode == IO_OUTPUT)
3648 for (g = (*proc)->u.generic; g; g = g->next)
3650 iop = (int) g->is_operator;
3651 mio_integer (&iop);
3652 mio_allocated_string (g->specific_st->name);
3654 else
3656 (*proc)->u.generic = NULL;
3657 while (peek_atom () != ATOM_RPAREN)
3659 gfc_symtree** sym_root;
3661 g = gfc_get_tbp_generic ();
3662 g->specific = NULL;
3664 mio_integer (&iop);
3665 g->is_operator = (bool) iop;
3667 require_atom (ATOM_STRING);
3668 sym_root = &current_f2k_derived->tb_sym_root;
3669 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3670 free (atom_string);
3672 g->next = (*proc)->u.generic;
3673 (*proc)->u.generic = g;
3677 mio_rparen ();
3679 else if (!(*proc)->ppc)
3680 mio_symtree_ref (&(*proc)->u.specific);
3682 mio_rparen ();
3685 /* Walker-callback function for this purpose. */
3686 static void
3687 mio_typebound_symtree (gfc_symtree* st)
3689 if (iomode == IO_OUTPUT && !st->n.tb)
3690 return;
3692 if (iomode == IO_OUTPUT)
3694 mio_lparen ();
3695 mio_allocated_string (st->name);
3697 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3699 mio_typebound_proc (&st->n.tb);
3700 mio_rparen ();
3703 /* IO a full symtree (in all depth). */
3704 static void
3705 mio_full_typebound_tree (gfc_symtree** root)
3707 mio_lparen ();
3709 if (iomode == IO_OUTPUT)
3710 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3711 else
3713 while (peek_atom () == ATOM_LPAREN)
3715 gfc_symtree* st;
3717 mio_lparen ();
3719 require_atom (ATOM_STRING);
3720 st = gfc_get_tbp_symtree (root, atom_string);
3721 free (atom_string);
3723 mio_typebound_symtree (st);
3727 mio_rparen ();
3730 static void
3731 mio_finalizer (gfc_finalizer **f)
3733 if (iomode == IO_OUTPUT)
3735 gcc_assert (*f);
3736 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3737 mio_symtree_ref (&(*f)->proc_tree);
3739 else
3741 *f = gfc_get_finalizer ();
3742 (*f)->where = gfc_current_locus; /* Value should not matter. */
3743 (*f)->next = NULL;
3745 mio_symtree_ref (&(*f)->proc_tree);
3746 (*f)->proc_sym = NULL;
3750 static void
3751 mio_f2k_derived (gfc_namespace *f2k)
3753 current_f2k_derived = f2k;
3755 /* Handle the list of finalizer procedures. */
3756 mio_lparen ();
3757 if (iomode == IO_OUTPUT)
3759 gfc_finalizer *f;
3760 for (f = f2k->finalizers; f; f = f->next)
3761 mio_finalizer (&f);
3763 else
3765 f2k->finalizers = NULL;
3766 while (peek_atom () != ATOM_RPAREN)
3768 gfc_finalizer *cur = NULL;
3769 mio_finalizer (&cur);
3770 cur->next = f2k->finalizers;
3771 f2k->finalizers = cur;
3774 mio_rparen ();
3776 /* Handle type-bound procedures. */
3777 mio_full_typebound_tree (&f2k->tb_sym_root);
3779 /* Type-bound user operators. */
3780 mio_full_typebound_tree (&f2k->tb_uop_root);
3782 /* Type-bound intrinsic operators. */
3783 mio_lparen ();
3784 if (iomode == IO_OUTPUT)
3786 int op;
3787 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3789 gfc_intrinsic_op realop;
3791 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3792 continue;
3794 mio_lparen ();
3795 realop = (gfc_intrinsic_op) op;
3796 mio_intrinsic_op (&realop);
3797 mio_typebound_proc (&f2k->tb_op[op]);
3798 mio_rparen ();
3801 else
3802 while (peek_atom () != ATOM_RPAREN)
3804 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3806 mio_lparen ();
3807 mio_intrinsic_op (&op);
3808 mio_typebound_proc (&f2k->tb_op[op]);
3809 mio_rparen ();
3811 mio_rparen ();
3814 static void
3815 mio_full_f2k_derived (gfc_symbol *sym)
3817 mio_lparen ();
3819 if (iomode == IO_OUTPUT)
3821 if (sym->f2k_derived)
3822 mio_f2k_derived (sym->f2k_derived);
3824 else
3826 if (peek_atom () != ATOM_RPAREN)
3828 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3829 mio_f2k_derived (sym->f2k_derived);
3831 else
3832 gcc_assert (!sym->f2k_derived);
3835 mio_rparen ();
3839 /* Unlike most other routines, the address of the symbol node is already
3840 fixed on input and the name/module has already been filled in. */
3842 static void
3843 mio_symbol (gfc_symbol *sym)
3845 int intmod = INTMOD_NONE;
3847 mio_lparen ();
3849 mio_symbol_attribute (&sym->attr);
3850 mio_typespec (&sym->ts);
3851 if (sym->ts.type == BT_CLASS)
3852 sym->attr.class_ok = 1;
3854 if (iomode == IO_OUTPUT)
3855 mio_namespace_ref (&sym->formal_ns);
3856 else
3858 mio_namespace_ref (&sym->formal_ns);
3859 if (sym->formal_ns)
3860 sym->formal_ns->proc_name = sym;
3863 /* Save/restore common block links. */
3864 mio_symbol_ref (&sym->common_next);
3866 mio_formal_arglist (&sym->formal);
3868 if (sym->attr.flavor == FL_PARAMETER)
3869 mio_expr (&sym->value);
3871 mio_array_spec (&sym->as);
3873 mio_symbol_ref (&sym->result);
3875 if (sym->attr.cray_pointee)
3876 mio_symbol_ref (&sym->cp_pointer);
3878 /* Note that components are always saved, even if they are supposed
3879 to be private. Component access is checked during searching. */
3881 mio_component_list (&sym->components, sym->attr.vtype);
3883 if (sym->components != NULL)
3884 sym->component_access
3885 = MIO_NAME (gfc_access) (sym->component_access, access_types);
3887 /* Load/save the f2k_derived namespace of a derived-type symbol. */
3888 mio_full_f2k_derived (sym);
3890 mio_namelist (sym);
3892 /* Add the fields that say whether this is from an intrinsic module,
3893 and if so, what symbol it is within the module. */
3894 /* mio_integer (&(sym->from_intmod)); */
3895 if (iomode == IO_OUTPUT)
3897 intmod = sym->from_intmod;
3898 mio_integer (&intmod);
3900 else
3902 mio_integer (&intmod);
3903 sym->from_intmod = (intmod_id) intmod;
3906 mio_integer (&(sym->intmod_sym_id));
3908 if (sym->attr.flavor == FL_DERIVED)
3909 mio_integer (&(sym->hash_value));
3911 mio_rparen ();
3915 /************************* Top level subroutines *************************/
3917 /* Given a root symtree node and a symbol, try to find a symtree that
3918 references the symbol that is not a unique name. */
3920 static gfc_symtree *
3921 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
3923 gfc_symtree *s = NULL;
3925 if (st == NULL)
3926 return s;
3928 s = find_symtree_for_symbol (st->right, sym);
3929 if (s != NULL)
3930 return s;
3931 s = find_symtree_for_symbol (st->left, sym);
3932 if (s != NULL)
3933 return s;
3935 if (st->n.sym == sym && !check_unique_name (st->name))
3936 return st;
3938 return s;
3942 /* A recursive function to look for a specific symbol by name and by
3943 module. Whilst several symtrees might point to one symbol, its
3944 is sufficient for the purposes here than one exist. Note that
3945 generic interfaces are distinguished as are symbols that have been
3946 renamed in another module. */
3947 static gfc_symtree *
3948 find_symbol (gfc_symtree *st, const char *name,
3949 const char *module, int generic)
3951 int c;
3952 gfc_symtree *retval, *s;
3954 if (st == NULL || st->n.sym == NULL)
3955 return NULL;
3957 c = strcmp (name, st->n.sym->name);
3958 if (c == 0 && st->n.sym->module
3959 && strcmp (module, st->n.sym->module) == 0
3960 && !check_unique_name (st->name))
3962 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
3964 /* Detect symbols that are renamed by use association in another
3965 module by the absence of a symtree and null attr.use_rename,
3966 since the latter is not transmitted in the module file. */
3967 if (((!generic && !st->n.sym->attr.generic)
3968 || (generic && st->n.sym->attr.generic))
3969 && !(s == NULL && !st->n.sym->attr.use_rename))
3970 return st;
3973 retval = find_symbol (st->left, name, module, generic);
3975 if (retval == NULL)
3976 retval = find_symbol (st->right, name, module, generic);
3978 return retval;
3982 /* Skip a list between balanced left and right parens. */
3984 static void
3985 skip_list (void)
3987 int level;
3989 level = 0;
3992 switch (parse_atom ())
3994 case ATOM_LPAREN:
3995 level++;
3996 break;
3998 case ATOM_RPAREN:
3999 level--;
4000 break;
4002 case ATOM_STRING:
4003 free (atom_string);
4004 break;
4006 case ATOM_NAME:
4007 case ATOM_INTEGER:
4008 break;
4011 while (level > 0);
4015 /* Load operator interfaces from the module. Interfaces are unusual
4016 in that they attach themselves to existing symbols. */
4018 static void
4019 load_operator_interfaces (void)
4021 const char *p;
4022 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4023 gfc_user_op *uop;
4024 pointer_info *pi = NULL;
4025 int n, i;
4027 mio_lparen ();
4029 while (peek_atom () != ATOM_RPAREN)
4031 mio_lparen ();
4033 mio_internal_string (name);
4034 mio_internal_string (module);
4036 n = number_use_names (name, true);
4037 n = n ? n : 1;
4039 for (i = 1; i <= n; i++)
4041 /* Decide if we need to load this one or not. */
4042 p = find_use_name_n (name, &i, true);
4044 if (p == NULL)
4046 while (parse_atom () != ATOM_RPAREN);
4047 continue;
4050 if (i == 1)
4052 uop = gfc_get_uop (p);
4053 pi = mio_interface_rest (&uop->op);
4055 else
4057 if (gfc_find_uop (p, NULL))
4058 continue;
4059 uop = gfc_get_uop (p);
4060 uop->op = gfc_get_interface ();
4061 uop->op->where = gfc_current_locus;
4062 add_fixup (pi->integer, &uop->op->sym);
4067 mio_rparen ();
4071 /* Load interfaces from the module. Interfaces are unusual in that
4072 they attach themselves to existing symbols. */
4074 static void
4075 load_generic_interfaces (void)
4077 const char *p;
4078 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4079 gfc_symbol *sym;
4080 gfc_interface *generic = NULL, *gen = NULL;
4081 int n, i, renamed;
4082 bool ambiguous_set = false;
4084 mio_lparen ();
4086 while (peek_atom () != ATOM_RPAREN)
4088 mio_lparen ();
4090 mio_internal_string (name);
4091 mio_internal_string (module);
4093 n = number_use_names (name, false);
4094 renamed = n ? 1 : 0;
4095 n = n ? n : 1;
4097 for (i = 1; i <= n; i++)
4099 gfc_symtree *st;
4100 /* Decide if we need to load this one or not. */
4101 p = find_use_name_n (name, &i, false);
4103 st = find_symbol (gfc_current_ns->sym_root,
4104 name, module_name, 1);
4106 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4108 /* Skip the specific names for these cases. */
4109 while (i == 1 && parse_atom () != ATOM_RPAREN);
4111 continue;
4114 /* If the symbol exists already and is being USEd without being
4115 in an ONLY clause, do not load a new symtree(11.3.2). */
4116 if (!only_flag && st)
4117 sym = st->n.sym;
4119 if (!sym)
4121 if (st)
4123 sym = st->n.sym;
4124 if (strcmp (st->name, p) != 0)
4126 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4127 st->n.sym = sym;
4128 sym->refs++;
4132 /* Since we haven't found a valid generic interface, we had
4133 better make one. */
4134 if (!sym)
4136 gfc_get_symbol (p, NULL, &sym);
4137 sym->name = gfc_get_string (name);
4138 sym->module = module_name;
4139 sym->attr.flavor = FL_PROCEDURE;
4140 sym->attr.generic = 1;
4141 sym->attr.use_assoc = 1;
4144 else
4146 /* Unless sym is a generic interface, this reference
4147 is ambiguous. */
4148 if (st == NULL)
4149 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4151 sym = st->n.sym;
4153 if (st && !sym->attr.generic
4154 && !st->ambiguous
4155 && sym->module
4156 && strcmp (module, sym->module))
4158 ambiguous_set = true;
4159 st->ambiguous = 1;
4163 sym->attr.use_only = only_flag;
4164 sym->attr.use_rename = renamed;
4166 if (i == 1)
4168 mio_interface_rest (&sym->generic);
4169 generic = sym->generic;
4171 else if (!sym->generic)
4173 sym->generic = generic;
4174 sym->attr.generic_copy = 1;
4177 /* If a procedure that is not generic has generic interfaces
4178 that include itself, it is generic! We need to take care
4179 to retain symbols ambiguous that were already so. */
4180 if (sym->attr.use_assoc
4181 && !sym->attr.generic
4182 && sym->attr.flavor == FL_PROCEDURE)
4184 for (gen = generic; gen; gen = gen->next)
4186 if (gen->sym == sym)
4188 sym->attr.generic = 1;
4189 if (ambiguous_set)
4190 st->ambiguous = 0;
4191 break;
4199 mio_rparen ();
4203 /* Load common blocks. */
4205 static void
4206 load_commons (void)
4208 char name[GFC_MAX_SYMBOL_LEN + 1];
4209 gfc_common_head *p;
4211 mio_lparen ();
4213 while (peek_atom () != ATOM_RPAREN)
4215 int flags;
4216 char* label;
4217 mio_lparen ();
4218 mio_internal_string (name);
4220 p = gfc_get_common (name, 1);
4222 mio_symbol_ref (&p->head);
4223 mio_integer (&flags);
4224 if (flags & 1)
4225 p->saved = 1;
4226 if (flags & 2)
4227 p->threadprivate = 1;
4228 p->use_assoc = 1;
4230 /* Get whether this was a bind(c) common or not. */
4231 mio_integer (&p->is_bind_c);
4232 /* Get the binding label. */
4233 label = read_string ();
4234 if (strlen (label))
4235 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4236 XDELETEVEC (label);
4238 mio_rparen ();
4241 mio_rparen ();
4245 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4246 so that unused variables are not loaded and so that the expression can
4247 be safely freed. */
4249 static void
4250 load_equiv (void)
4252 gfc_equiv *head, *tail, *end, *eq;
4253 bool unused;
4255 mio_lparen ();
4256 in_load_equiv = true;
4258 end = gfc_current_ns->equiv;
4259 while (end != NULL && end->next != NULL)
4260 end = end->next;
4262 while (peek_atom () != ATOM_RPAREN) {
4263 mio_lparen ();
4264 head = tail = NULL;
4266 while(peek_atom () != ATOM_RPAREN)
4268 if (head == NULL)
4269 head = tail = gfc_get_equiv ();
4270 else
4272 tail->eq = gfc_get_equiv ();
4273 tail = tail->eq;
4276 mio_pool_string (&tail->module);
4277 mio_expr (&tail->expr);
4280 /* Unused equivalence members have a unique name. In addition, it
4281 must be checked that the symbols are from the same module. */
4282 unused = true;
4283 for (eq = head; eq; eq = eq->eq)
4285 if (eq->expr->symtree->n.sym->module
4286 && head->expr->symtree->n.sym->module
4287 && strcmp (head->expr->symtree->n.sym->module,
4288 eq->expr->symtree->n.sym->module) == 0
4289 && !check_unique_name (eq->expr->symtree->name))
4291 unused = false;
4292 break;
4296 if (unused)
4298 for (eq = head; eq; eq = head)
4300 head = eq->eq;
4301 gfc_free_expr (eq->expr);
4302 free (eq);
4306 if (end == NULL)
4307 gfc_current_ns->equiv = head;
4308 else
4309 end->next = head;
4311 if (head != NULL)
4312 end = head;
4314 mio_rparen ();
4317 mio_rparen ();
4318 in_load_equiv = false;
4322 /* This function loads the sym_root of f2k_derived with the extensions to
4323 the derived type. */
4324 static void
4325 load_derived_extensions (void)
4327 int symbol, j;
4328 gfc_symbol *derived;
4329 gfc_symbol *dt;
4330 gfc_symtree *st;
4331 pointer_info *info;
4332 char name[GFC_MAX_SYMBOL_LEN + 1];
4333 char module[GFC_MAX_SYMBOL_LEN + 1];
4334 const char *p;
4336 mio_lparen ();
4337 while (peek_atom () != ATOM_RPAREN)
4339 mio_lparen ();
4340 mio_integer (&symbol);
4341 info = get_integer (symbol);
4342 derived = info->u.rsym.sym;
4344 /* This one is not being loaded. */
4345 if (!info || !derived)
4347 while (peek_atom () != ATOM_RPAREN)
4348 skip_list ();
4349 continue;
4352 gcc_assert (derived->attr.flavor == FL_DERIVED);
4353 if (derived->f2k_derived == NULL)
4354 derived->f2k_derived = gfc_get_namespace (NULL, 0);
4356 while (peek_atom () != ATOM_RPAREN)
4358 mio_lparen ();
4359 mio_internal_string (name);
4360 mio_internal_string (module);
4362 /* Only use one use name to find the symbol. */
4363 j = 1;
4364 p = find_use_name_n (name, &j, false);
4365 if (p)
4367 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4368 dt = st->n.sym;
4369 st = gfc_find_symtree (derived->f2k_derived->sym_root, name);
4370 if (st == NULL)
4372 /* Only use the real name in f2k_derived to ensure a single
4373 symtree. */
4374 st = gfc_new_symtree (&derived->f2k_derived->sym_root, name);
4375 st->n.sym = dt;
4376 st->n.sym->refs++;
4379 mio_rparen ();
4381 mio_rparen ();
4383 mio_rparen ();
4387 /* Recursive function to traverse the pointer_info tree and load a
4388 needed symbol. We return nonzero if we load a symbol and stop the
4389 traversal, because the act of loading can alter the tree. */
4391 static int
4392 load_needed (pointer_info *p)
4394 gfc_namespace *ns;
4395 pointer_info *q;
4396 gfc_symbol *sym;
4397 int rv;
4399 rv = 0;
4400 if (p == NULL)
4401 return rv;
4403 rv |= load_needed (p->left);
4404 rv |= load_needed (p->right);
4406 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4407 return rv;
4409 p->u.rsym.state = USED;
4411 set_module_locus (&p->u.rsym.where);
4413 sym = p->u.rsym.sym;
4414 if (sym == NULL)
4416 q = get_integer (p->u.rsym.ns);
4418 ns = (gfc_namespace *) q->u.pointer;
4419 if (ns == NULL)
4421 /* Create an interface namespace if necessary. These are
4422 the namespaces that hold the formal parameters of module
4423 procedures. */
4425 ns = gfc_get_namespace (NULL, 0);
4426 associate_integer_pointer (q, ns);
4429 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4430 doesn't go pear-shaped if the symbol is used. */
4431 if (!ns->proc_name)
4432 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4433 1, &ns->proc_name);
4435 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4436 sym->name = dt_lower_string (p->u.rsym.true_name);
4437 sym->module = gfc_get_string (p->u.rsym.module);
4438 if (p->u.rsym.binding_label)
4439 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4440 (p->u.rsym.binding_label));
4442 associate_integer_pointer (p, sym);
4445 mio_symbol (sym);
4446 sym->attr.use_assoc = 1;
4448 /* Mark as only or rename for later diagnosis for explicitly imported
4449 but not used warnings; don't mark internal symbols such as __vtab,
4450 __def_init etc. Only mark them if they have been explicitly loaded. */
4452 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4454 gfc_use_rename *u;
4456 /* Search the use/rename list for the variable; if the variable is
4457 found, mark it. */
4458 for (u = gfc_rename_list; u; u = u->next)
4460 if (strcmp (u->use_name, sym->name) == 0)
4462 sym->attr.use_only = 1;
4463 break;
4468 if (p->u.rsym.renamed)
4469 sym->attr.use_rename = 1;
4471 return 1;
4475 /* Recursive function for cleaning up things after a module has been read. */
4477 static void
4478 read_cleanup (pointer_info *p)
4480 gfc_symtree *st;
4481 pointer_info *q;
4483 if (p == NULL)
4484 return;
4486 read_cleanup (p->left);
4487 read_cleanup (p->right);
4489 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4491 gfc_namespace *ns;
4492 /* Add hidden symbols to the symtree. */
4493 q = get_integer (p->u.rsym.ns);
4494 ns = (gfc_namespace *) q->u.pointer;
4496 if (!p->u.rsym.sym->attr.vtype
4497 && !p->u.rsym.sym->attr.vtab)
4498 st = gfc_get_unique_symtree (ns);
4499 else
4501 /* There is no reason to use 'unique_symtrees' for vtabs or
4502 vtypes - their name is fine for a symtree and reduces the
4503 namespace pollution. */
4504 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4505 if (!st)
4506 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4509 st->n.sym = p->u.rsym.sym;
4510 st->n.sym->refs++;
4512 /* Fixup any symtree references. */
4513 p->u.rsym.symtree = st;
4514 resolve_fixups (p->u.rsym.stfixup, st);
4515 p->u.rsym.stfixup = NULL;
4518 /* Free unused symbols. */
4519 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4520 gfc_free_symbol (p->u.rsym.sym);
4524 /* It is not quite enough to check for ambiguity in the symbols by
4525 the loaded symbol and the new symbol not being identical. */
4526 static bool
4527 check_for_ambiguous (gfc_symbol *st_sym, pointer_info *info)
4529 gfc_symbol *rsym;
4530 module_locus locus;
4531 symbol_attribute attr;
4533 if (gfc_current_ns->proc_name && st_sym->name == gfc_current_ns->proc_name->name)
4535 gfc_error ("'%s' of module '%s', imported at %C, is also the name of the "
4536 "current program unit", st_sym->name, module_name);
4537 return true;
4540 rsym = info->u.rsym.sym;
4541 if (st_sym == rsym)
4542 return false;
4544 if (st_sym->attr.vtab || st_sym->attr.vtype)
4545 return false;
4547 /* If the existing symbol is generic from a different module and
4548 the new symbol is generic there can be no ambiguity. */
4549 if (st_sym->attr.generic
4550 && st_sym->module
4551 && st_sym->module != module_name)
4553 /* The new symbol's attributes have not yet been read. Since
4554 we need attr.generic, read it directly. */
4555 get_module_locus (&locus);
4556 set_module_locus (&info->u.rsym.where);
4557 mio_lparen ();
4558 attr.generic = 0;
4559 mio_symbol_attribute (&attr);
4560 set_module_locus (&locus);
4561 if (attr.generic)
4562 return false;
4565 return true;
4569 /* Read a module file. */
4571 static void
4572 read_module (void)
4574 module_locus operator_interfaces, user_operators, extensions;
4575 const char *p;
4576 char name[GFC_MAX_SYMBOL_LEN + 1];
4577 int i;
4578 int ambiguous, j, nuse, symbol;
4579 pointer_info *info, *q;
4580 gfc_use_rename *u = NULL;
4581 gfc_symtree *st;
4582 gfc_symbol *sym;
4584 get_module_locus (&operator_interfaces); /* Skip these for now. */
4585 skip_list ();
4587 get_module_locus (&user_operators);
4588 skip_list ();
4589 skip_list ();
4591 /* Skip commons, equivalences and derived type extensions for now. */
4592 skip_list ();
4593 skip_list ();
4595 get_module_locus (&extensions);
4596 skip_list ();
4598 mio_lparen ();
4600 /* Create the fixup nodes for all the symbols. */
4602 while (peek_atom () != ATOM_RPAREN)
4604 char* bind_label;
4605 require_atom (ATOM_INTEGER);
4606 info = get_integer (atom_int);
4608 info->type = P_SYMBOL;
4609 info->u.rsym.state = UNUSED;
4611 info->u.rsym.true_name = read_string ();
4612 info->u.rsym.module = read_string ();
4613 bind_label = read_string ();
4614 if (strlen (bind_label))
4615 info->u.rsym.binding_label = bind_label;
4616 else
4617 XDELETEVEC (bind_label);
4619 require_atom (ATOM_INTEGER);
4620 info->u.rsym.ns = atom_int;
4622 get_module_locus (&info->u.rsym.where);
4623 skip_list ();
4625 /* See if the symbol has already been loaded by a previous module.
4626 If so, we reference the existing symbol and prevent it from
4627 being loaded again. This should not happen if the symbol being
4628 read is an index for an assumed shape dummy array (ns != 1). */
4630 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
4632 if (sym == NULL
4633 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
4634 continue;
4636 info->u.rsym.state = USED;
4637 info->u.rsym.sym = sym;
4639 /* Some symbols do not have a namespace (eg. formal arguments),
4640 so the automatic "unique symtree" mechanism must be suppressed
4641 by marking them as referenced. */
4642 q = get_integer (info->u.rsym.ns);
4643 if (q->u.pointer == NULL)
4645 info->u.rsym.referenced = 1;
4646 continue;
4649 /* If possible recycle the symtree that references the symbol.
4650 If a symtree is not found and the module does not import one,
4651 a unique-name symtree is found by read_cleanup. */
4652 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
4653 if (st != NULL)
4655 info->u.rsym.symtree = st;
4656 info->u.rsym.referenced = 1;
4660 mio_rparen ();
4662 /* Parse the symtree lists. This lets us mark which symbols need to
4663 be loaded. Renaming is also done at this point by replacing the
4664 symtree name. */
4666 mio_lparen ();
4668 while (peek_atom () != ATOM_RPAREN)
4670 mio_internal_string (name);
4671 mio_integer (&ambiguous);
4672 mio_integer (&symbol);
4674 info = get_integer (symbol);
4676 /* See how many use names there are. If none, go through the start
4677 of the loop at least once. */
4678 nuse = number_use_names (name, false);
4679 info->u.rsym.renamed = nuse ? 1 : 0;
4681 if (nuse == 0)
4682 nuse = 1;
4684 for (j = 1; j <= nuse; j++)
4686 /* Get the jth local name for this symbol. */
4687 p = find_use_name_n (name, &j, false);
4689 if (p == NULL && strcmp (name, module_name) == 0)
4690 p = name;
4692 /* Exception: Always import vtabs & vtypes. */
4693 if (p == NULL && name[0] == '_'
4694 && (strncmp (name, "__vtab_", 5) == 0
4695 || strncmp (name, "__vtype_", 6) == 0))
4696 p = name;
4698 /* Skip symtree nodes not in an ONLY clause, unless there
4699 is an existing symtree loaded from another USE statement. */
4700 if (p == NULL)
4702 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4703 if (st != NULL
4704 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
4705 && st->n.sym->module != NULL
4706 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
4708 info->u.rsym.symtree = st;
4709 info->u.rsym.sym = st->n.sym;
4711 continue;
4714 /* If a symbol of the same name and module exists already,
4715 this symbol, which is not in an ONLY clause, must not be
4716 added to the namespace(11.3.2). Note that find_symbol
4717 only returns the first occurrence that it finds. */
4718 if (!only_flag && !info->u.rsym.renamed
4719 && strcmp (name, module_name) != 0
4720 && find_symbol (gfc_current_ns->sym_root, name,
4721 module_name, 0))
4722 continue;
4724 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4726 if (st != NULL)
4728 /* Check for ambiguous symbols. */
4729 if (check_for_ambiguous (st->n.sym, info))
4730 st->ambiguous = 1;
4731 else
4732 info->u.rsym.symtree = st;
4734 else
4736 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4738 /* Create a symtree node in the current namespace for this
4739 symbol. */
4740 st = check_unique_name (p)
4741 ? gfc_get_unique_symtree (gfc_current_ns)
4742 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
4743 st->ambiguous = ambiguous;
4745 sym = info->u.rsym.sym;
4747 /* Create a symbol node if it doesn't already exist. */
4748 if (sym == NULL)
4750 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
4751 gfc_current_ns);
4752 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
4753 sym = info->u.rsym.sym;
4754 sym->module = gfc_get_string (info->u.rsym.module);
4756 if (info->u.rsym.binding_label)
4757 sym->binding_label =
4758 IDENTIFIER_POINTER (get_identifier
4759 (info->u.rsym.binding_label));
4762 st->n.sym = sym;
4763 st->n.sym->refs++;
4765 if (strcmp (name, p) != 0)
4766 sym->attr.use_rename = 1;
4768 if (name[0] != '_'
4769 || (strncmp (name, "__vtab_", 5) != 0
4770 && strncmp (name, "__vtype_", 6) != 0))
4771 sym->attr.use_only = only_flag;
4773 /* Store the symtree pointing to this symbol. */
4774 info->u.rsym.symtree = st;
4776 if (info->u.rsym.state == UNUSED)
4777 info->u.rsym.state = NEEDED;
4778 info->u.rsym.referenced = 1;
4783 mio_rparen ();
4785 /* Load intrinsic operator interfaces. */
4786 set_module_locus (&operator_interfaces);
4787 mio_lparen ();
4789 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
4791 if (i == INTRINSIC_USER)
4792 continue;
4794 if (only_flag)
4796 u = find_use_operator ((gfc_intrinsic_op) i);
4798 if (u == NULL)
4800 skip_list ();
4801 continue;
4804 u->found = 1;
4807 mio_interface (&gfc_current_ns->op[i]);
4808 if (u && !gfc_current_ns->op[i])
4809 u->found = 0;
4812 mio_rparen ();
4814 /* Load generic and user operator interfaces. These must follow the
4815 loading of symtree because otherwise symbols can be marked as
4816 ambiguous. */
4818 set_module_locus (&user_operators);
4820 load_operator_interfaces ();
4821 load_generic_interfaces ();
4823 load_commons ();
4824 load_equiv ();
4826 /* At this point, we read those symbols that are needed but haven't
4827 been loaded yet. If one symbol requires another, the other gets
4828 marked as NEEDED if its previous state was UNUSED. */
4830 while (load_needed (pi_root));
4832 /* Make sure all elements of the rename-list were found in the module. */
4834 for (u = gfc_rename_list; u; u = u->next)
4836 if (u->found)
4837 continue;
4839 if (u->op == INTRINSIC_NONE)
4841 gfc_error ("Symbol '%s' referenced at %L not found in module '%s'",
4842 u->use_name, &u->where, module_name);
4843 continue;
4846 if (u->op == INTRINSIC_USER)
4848 gfc_error ("User operator '%s' referenced at %L not found "
4849 "in module '%s'", u->use_name, &u->where, module_name);
4850 continue;
4853 gfc_error ("Intrinsic operator '%s' referenced at %L not found "
4854 "in module '%s'", gfc_op2string (u->op), &u->where,
4855 module_name);
4858 /* Now we should be in a position to fill f2k_derived with derived type
4859 extensions, since everything has been loaded. */
4860 set_module_locus (&extensions);
4861 load_derived_extensions ();
4863 /* Clean up symbol nodes that were never loaded, create references
4864 to hidden symbols. */
4866 read_cleanup (pi_root);
4870 /* Given an access type that is specific to an entity and the default
4871 access, return nonzero if the entity is publicly accessible. If the
4872 element is declared as PUBLIC, then it is public; if declared
4873 PRIVATE, then private, and otherwise it is public unless the default
4874 access in this context has been declared PRIVATE. */
4876 static bool
4877 check_access (gfc_access specific_access, gfc_access default_access)
4879 if (specific_access == ACCESS_PUBLIC)
4880 return TRUE;
4881 if (specific_access == ACCESS_PRIVATE)
4882 return FALSE;
4884 if (gfc_option.flag_module_private)
4885 return default_access == ACCESS_PUBLIC;
4886 else
4887 return default_access != ACCESS_PRIVATE;
4891 bool
4892 gfc_check_symbol_access (gfc_symbol *sym)
4894 if (sym->attr.vtab || sym->attr.vtype)
4895 return true;
4896 else
4897 return check_access (sym->attr.access, sym->ns->default_access);
4901 /* A structure to remember which commons we've already written. */
4903 struct written_common
4905 BBT_HEADER(written_common);
4906 const char *name, *label;
4909 static struct written_common *written_commons = NULL;
4911 /* Comparison function used for balancing the binary tree. */
4913 static int
4914 compare_written_commons (void *a1, void *b1)
4916 const char *aname = ((struct written_common *) a1)->name;
4917 const char *alabel = ((struct written_common *) a1)->label;
4918 const char *bname = ((struct written_common *) b1)->name;
4919 const char *blabel = ((struct written_common *) b1)->label;
4920 int c = strcmp (aname, bname);
4922 return (c != 0 ? c : strcmp (alabel, blabel));
4925 /* Free a list of written commons. */
4927 static void
4928 free_written_common (struct written_common *w)
4930 if (!w)
4931 return;
4933 if (w->left)
4934 free_written_common (w->left);
4935 if (w->right)
4936 free_written_common (w->right);
4938 free (w);
4941 /* Write a common block to the module -- recursive helper function. */
4943 static void
4944 write_common_0 (gfc_symtree *st, bool this_module)
4946 gfc_common_head *p;
4947 const char * name;
4948 int flags;
4949 const char *label;
4950 struct written_common *w;
4951 bool write_me = true;
4953 if (st == NULL)
4954 return;
4956 write_common_0 (st->left, this_module);
4958 /* We will write out the binding label, or "" if no label given. */
4959 name = st->n.common->name;
4960 p = st->n.common;
4961 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
4963 /* Check if we've already output this common. */
4964 w = written_commons;
4965 while (w)
4967 int c = strcmp (name, w->name);
4968 c = (c != 0 ? c : strcmp (label, w->label));
4969 if (c == 0)
4970 write_me = false;
4972 w = (c < 0) ? w->left : w->right;
4975 if (this_module && p->use_assoc)
4976 write_me = false;
4978 if (write_me)
4980 /* Write the common to the module. */
4981 mio_lparen ();
4982 mio_pool_string (&name);
4984 mio_symbol_ref (&p->head);
4985 flags = p->saved ? 1 : 0;
4986 if (p->threadprivate)
4987 flags |= 2;
4988 mio_integer (&flags);
4990 /* Write out whether the common block is bind(c) or not. */
4991 mio_integer (&(p->is_bind_c));
4993 mio_pool_string (&label);
4994 mio_rparen ();
4996 /* Record that we have written this common. */
4997 w = XCNEW (struct written_common);
4998 w->name = p->name;
4999 w->label = label;
5000 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5003 write_common_0 (st->right, this_module);
5007 /* Write a common, by initializing the list of written commons, calling
5008 the recursive function write_common_0() and cleaning up afterwards. */
5010 static void
5011 write_common (gfc_symtree *st)
5013 written_commons = NULL;
5014 write_common_0 (st, true);
5015 write_common_0 (st, false);
5016 free_written_common (written_commons);
5017 written_commons = NULL;
5021 /* Write the blank common block to the module. */
5023 static void
5024 write_blank_common (void)
5026 const char * name = BLANK_COMMON_NAME;
5027 int saved;
5028 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5029 this, but it hasn't been checked. Just making it so for now. */
5030 int is_bind_c = 0;
5032 if (gfc_current_ns->blank_common.head == NULL)
5033 return;
5035 mio_lparen ();
5037 mio_pool_string (&name);
5039 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5040 saved = gfc_current_ns->blank_common.saved;
5041 mio_integer (&saved);
5043 /* Write out whether the common block is bind(c) or not. */
5044 mio_integer (&is_bind_c);
5046 /* Write out an empty binding label. */
5047 write_atom (ATOM_STRING, "");
5049 mio_rparen ();
5053 /* Write equivalences to the module. */
5055 static void
5056 write_equiv (void)
5058 gfc_equiv *eq, *e;
5059 int num;
5061 num = 0;
5062 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5064 mio_lparen ();
5066 for (e = eq; e; e = e->eq)
5068 if (e->module == NULL)
5069 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5070 mio_allocated_string (e->module);
5071 mio_expr (&e->expr);
5074 num++;
5075 mio_rparen ();
5080 /* Write derived type extensions to the module. */
5082 static void
5083 write_dt_extensions (gfc_symtree *st)
5085 if (!gfc_check_symbol_access (st->n.sym))
5086 return;
5087 if (!(st->n.sym->ns && st->n.sym->ns->proc_name
5088 && st->n.sym->ns->proc_name->attr.flavor == FL_MODULE))
5089 return;
5091 mio_lparen ();
5092 mio_pool_string (&st->name);
5093 if (st->n.sym->module != NULL)
5094 mio_pool_string (&st->n.sym->module);
5095 else
5097 char name[GFC_MAX_SYMBOL_LEN + 1];
5098 if (iomode == IO_OUTPUT)
5099 strcpy (name, module_name);
5100 mio_internal_string (name);
5101 if (iomode == IO_INPUT)
5102 module_name = gfc_get_string (name);
5104 mio_rparen ();
5107 static void
5108 write_derived_extensions (gfc_symtree *st)
5110 if (!((st->n.sym->attr.flavor == FL_DERIVED)
5111 && (st->n.sym->f2k_derived != NULL)
5112 && (st->n.sym->f2k_derived->sym_root != NULL)))
5113 return;
5115 mio_lparen ();
5116 mio_symbol_ref (&(st->n.sym));
5117 gfc_traverse_symtree (st->n.sym->f2k_derived->sym_root,
5118 write_dt_extensions);
5119 mio_rparen ();
5123 /* Write a symbol to the module. */
5125 static void
5126 write_symbol (int n, gfc_symbol *sym)
5128 const char *label;
5130 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5131 gfc_internal_error ("write_symbol(): bad module symbol '%s'", sym->name);
5133 mio_integer (&n);
5135 if (sym->attr.flavor == FL_DERIVED)
5137 const char *name;
5138 name = dt_upper_string (sym->name);
5139 mio_pool_string (&name);
5141 else
5142 mio_pool_string (&sym->name);
5144 mio_pool_string (&sym->module);
5145 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5147 label = sym->binding_label;
5148 mio_pool_string (&label);
5150 else
5151 write_atom (ATOM_STRING, "");
5153 mio_pointer_ref (&sym->ns);
5155 mio_symbol (sym);
5156 write_char ('\n');
5160 /* Recursive traversal function to write the initial set of symbols to
5161 the module. We check to see if the symbol should be written
5162 according to the access specification. */
5164 static void
5165 write_symbol0 (gfc_symtree *st)
5167 gfc_symbol *sym;
5168 pointer_info *p;
5169 bool dont_write = false;
5171 if (st == NULL)
5172 return;
5174 write_symbol0 (st->left);
5176 sym = st->n.sym;
5177 if (sym->module == NULL)
5178 sym->module = module_name;
5180 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5181 && !sym->attr.subroutine && !sym->attr.function)
5182 dont_write = true;
5184 if (!gfc_check_symbol_access (sym))
5185 dont_write = true;
5187 if (!dont_write)
5189 p = get_pointer (sym);
5190 if (p->type == P_UNKNOWN)
5191 p->type = P_SYMBOL;
5193 if (p->u.wsym.state != WRITTEN)
5195 write_symbol (p->integer, sym);
5196 p->u.wsym.state = WRITTEN;
5200 write_symbol0 (st->right);
5204 /* Type for the temporary tree used when writing secondary symbols. */
5206 struct sorted_pointer_info
5208 BBT_HEADER (sorted_pointer_info);
5210 pointer_info *p;
5213 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5215 /* Recursively traverse the temporary tree, free its contents. */
5217 static void
5218 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5220 if (!p)
5221 return;
5223 free_sorted_pointer_info_tree (p->left);
5224 free_sorted_pointer_info_tree (p->right);
5226 free (p);
5229 /* Comparison function for the temporary tree. */
5231 static int
5232 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5234 sorted_pointer_info *spi1, *spi2;
5235 spi1 = (sorted_pointer_info *)_spi1;
5236 spi2 = (sorted_pointer_info *)_spi2;
5238 if (spi1->p->integer < spi2->p->integer)
5239 return -1;
5240 if (spi1->p->integer > spi2->p->integer)
5241 return 1;
5242 return 0;
5246 /* Finds the symbols that need to be written and collects them in the
5247 sorted_pi tree so that they can be traversed in an order
5248 independent of memory addresses. */
5250 static void
5251 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5253 if (!p)
5254 return;
5256 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5258 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5259 sp->p = p;
5261 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5264 find_symbols_to_write (tree, p->left);
5265 find_symbols_to_write (tree, p->right);
5269 /* Recursive function that traverses the tree of symbols that need to be
5270 written and writes them in order. */
5272 static void
5273 write_symbol1_recursion (sorted_pointer_info *sp)
5275 if (!sp)
5276 return;
5278 write_symbol1_recursion (sp->left);
5280 pointer_info *p1 = sp->p;
5281 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5283 p1->u.wsym.state = WRITTEN;
5284 write_symbol (p1->integer, p1->u.wsym.sym);
5285 p1->u.wsym.sym->attr.public_used = 1;
5287 write_symbol1_recursion (sp->right);
5291 /* Write the secondary set of symbols to the module file. These are
5292 symbols that were not public yet are needed by the public symbols
5293 or another dependent symbol. The act of writing a symbol can add
5294 symbols to the pointer_info tree, so we return nonzero if a symbol
5295 was written and pass that information upwards. The caller will
5296 then call this function again until nothing was written. It uses
5297 the utility functions and a temporary tree to ensure a reproducible
5298 ordering of the symbol output and thus the module file. */
5300 static int
5301 write_symbol1 (pointer_info *p)
5303 if (!p)
5304 return 0;
5306 /* Put symbols that need to be written into a tree sorted on the
5307 integer field. */
5309 sorted_pointer_info *spi_root = NULL;
5310 find_symbols_to_write (&spi_root, p);
5312 /* No symbols to write, return. */
5313 if (!spi_root)
5314 return 0;
5316 /* Otherwise, write and free the tree again. */
5317 write_symbol1_recursion (spi_root);
5318 free_sorted_pointer_info_tree (spi_root);
5320 return 1;
5324 /* Write operator interfaces associated with a symbol. */
5326 static void
5327 write_operator (gfc_user_op *uop)
5329 static char nullstring[] = "";
5330 const char *p = nullstring;
5332 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5333 return;
5335 mio_symbol_interface (&uop->name, &p, &uop->op);
5339 /* Write generic interfaces from the namespace sym_root. */
5341 static void
5342 write_generic (gfc_symtree *st)
5344 gfc_symbol *sym;
5346 if (st == NULL)
5347 return;
5349 write_generic (st->left);
5351 sym = st->n.sym;
5352 if (sym && !check_unique_name (st->name)
5353 && sym->generic && gfc_check_symbol_access (sym))
5355 if (!sym->module)
5356 sym->module = module_name;
5358 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5361 write_generic (st->right);
5365 static void
5366 write_symtree (gfc_symtree *st)
5368 gfc_symbol *sym;
5369 pointer_info *p;
5371 sym = st->n.sym;
5373 /* A symbol in an interface body must not be visible in the
5374 module file. */
5375 if (sym->ns != gfc_current_ns
5376 && sym->ns->proc_name
5377 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5378 return;
5380 if (!gfc_check_symbol_access (sym)
5381 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5382 && !sym->attr.subroutine && !sym->attr.function))
5383 return;
5385 if (check_unique_name (st->name))
5386 return;
5388 p = find_pointer (sym);
5389 if (p == NULL)
5390 gfc_internal_error ("write_symtree(): Symbol not written");
5392 mio_pool_string (&st->name);
5393 mio_integer (&st->ambiguous);
5394 mio_integer (&p->integer);
5398 static void
5399 write_module (void)
5401 int i;
5403 /* Write the operator interfaces. */
5404 mio_lparen ();
5406 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5408 if (i == INTRINSIC_USER)
5409 continue;
5411 mio_interface (check_access (gfc_current_ns->operator_access[i],
5412 gfc_current_ns->default_access)
5413 ? &gfc_current_ns->op[i] : NULL);
5416 mio_rparen ();
5417 write_char ('\n');
5418 write_char ('\n');
5420 mio_lparen ();
5421 gfc_traverse_user_op (gfc_current_ns, write_operator);
5422 mio_rparen ();
5423 write_char ('\n');
5424 write_char ('\n');
5426 mio_lparen ();
5427 write_generic (gfc_current_ns->sym_root);
5428 mio_rparen ();
5429 write_char ('\n');
5430 write_char ('\n');
5432 mio_lparen ();
5433 write_blank_common ();
5434 write_common (gfc_current_ns->common_root);
5435 mio_rparen ();
5436 write_char ('\n');
5437 write_char ('\n');
5439 mio_lparen ();
5440 write_equiv ();
5441 mio_rparen ();
5442 write_char ('\n');
5443 write_char ('\n');
5445 mio_lparen ();
5446 gfc_traverse_symtree (gfc_current_ns->sym_root,
5447 write_derived_extensions);
5448 mio_rparen ();
5449 write_char ('\n');
5450 write_char ('\n');
5452 /* Write symbol information. First we traverse all symbols in the
5453 primary namespace, writing those that need to be written.
5454 Sometimes writing one symbol will cause another to need to be
5455 written. A list of these symbols ends up on the write stack, and
5456 we end by popping the bottom of the stack and writing the symbol
5457 until the stack is empty. */
5459 mio_lparen ();
5461 write_symbol0 (gfc_current_ns->sym_root);
5462 while (write_symbol1 (pi_root))
5463 /* Nothing. */;
5465 mio_rparen ();
5467 write_char ('\n');
5468 write_char ('\n');
5470 mio_lparen ();
5471 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5472 mio_rparen ();
5476 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5477 true on success, false on failure. */
5479 static bool
5480 read_crc32_from_module_file (const char* filename, uLong* crc)
5482 FILE *file;
5483 char buf[4];
5484 unsigned int val;
5486 /* Open the file in binary mode. */
5487 if ((file = fopen (filename, "rb")) == NULL)
5488 return false;
5490 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5491 file. See RFC 1952. */
5492 if (fseek (file, -8, SEEK_END) != 0)
5494 fclose (file);
5495 return false;
5498 /* Read the CRC32. */
5499 if (fread (buf, 1, 4, file) != 4)
5501 fclose (file);
5502 return false;
5505 /* Close the file. */
5506 fclose (file);
5508 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5509 + ((buf[3] & 0xFF) << 24);
5510 *crc = val;
5512 /* For debugging, the CRC value printed in hexadecimal should match
5513 the CRC printed by "zcat -l -v filename".
5514 printf("CRC of file %s is %x\n", filename, val); */
5516 return true;
5520 /* Given module, dump it to disk. If there was an error while
5521 processing the module, dump_flag will be set to zero and we delete
5522 the module file, even if it was already there. */
5524 void
5525 gfc_dump_module (const char *name, int dump_flag)
5527 int n;
5528 char *filename, *filename_tmp;
5529 uLong crc, crc_old;
5531 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5532 if (gfc_option.module_dir != NULL)
5534 n += strlen (gfc_option.module_dir);
5535 filename = (char *) alloca (n);
5536 strcpy (filename, gfc_option.module_dir);
5537 strcat (filename, name);
5539 else
5541 filename = (char *) alloca (n);
5542 strcpy (filename, name);
5544 strcat (filename, MODULE_EXTENSION);
5546 /* Name of the temporary file used to write the module. */
5547 filename_tmp = (char *) alloca (n + 1);
5548 strcpy (filename_tmp, filename);
5549 strcat (filename_tmp, "0");
5551 /* There was an error while processing the module. We delete the
5552 module file, even if it was already there. */
5553 if (!dump_flag)
5555 unlink (filename);
5556 return;
5559 if (gfc_cpp_makedep ())
5560 gfc_cpp_add_target (filename);
5562 /* Write the module to the temporary file. */
5563 module_fp = gzopen (filename_tmp, "w");
5564 if (module_fp == NULL)
5565 gfc_fatal_error ("Can't open module file '%s' for writing at %C: %s",
5566 filename_tmp, xstrerror (errno));
5568 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
5569 MOD_VERSION, gfc_source_file);
5571 /* Write the module itself. */
5572 iomode = IO_OUTPUT;
5573 module_name = gfc_get_string (name);
5575 init_pi_tree ();
5577 write_module ();
5579 free_pi_tree (pi_root);
5580 pi_root = NULL;
5582 write_char ('\n');
5584 if (gzclose (module_fp))
5585 gfc_fatal_error ("Error writing module file '%s' for writing: %s",
5586 filename_tmp, xstrerror (errno));
5588 /* Read the CRC32 from the gzip trailers of the module files and
5589 compare. */
5590 if (!read_crc32_from_module_file (filename_tmp, &crc)
5591 || !read_crc32_from_module_file (filename, &crc_old)
5592 || crc_old != crc)
5594 /* Module file have changed, replace the old one. */
5595 if (rename (filename_tmp, filename))
5596 gfc_fatal_error ("Can't rename module file '%s' to '%s': %s",
5597 filename_tmp, filename, xstrerror (errno));
5599 else
5601 if (unlink (filename_tmp))
5602 gfc_fatal_error ("Can't delete temporary module file '%s': %s",
5603 filename_tmp, xstrerror (errno));
5608 static void
5609 create_intrinsic_function (const char *name, int id,
5610 const char *modname, intmod_id module,
5611 bool subroutine, gfc_symbol *result_type)
5613 gfc_intrinsic_sym *isym;
5614 gfc_symtree *tmp_symtree;
5615 gfc_symbol *sym;
5617 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5618 if (tmp_symtree)
5620 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5621 return;
5622 gfc_error ("Symbol '%s' already declared", name);
5625 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5626 sym = tmp_symtree->n.sym;
5628 if (subroutine)
5630 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5631 isym = gfc_intrinsic_subroutine_by_id (isym_id);
5632 sym->attr.subroutine = 1;
5634 else
5636 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5637 isym = gfc_intrinsic_function_by_id (isym_id);
5639 sym->attr.function = 1;
5640 if (result_type)
5642 sym->ts.type = BT_DERIVED;
5643 sym->ts.u.derived = result_type;
5644 sym->ts.is_c_interop = 1;
5645 isym->ts.f90_type = BT_VOID;
5646 isym->ts.type = BT_DERIVED;
5647 isym->ts.f90_type = BT_VOID;
5648 isym->ts.u.derived = result_type;
5649 isym->ts.is_c_interop = 1;
5652 gcc_assert (isym);
5654 sym->attr.flavor = FL_PROCEDURE;
5655 sym->attr.intrinsic = 1;
5657 sym->module = gfc_get_string (modname);
5658 sym->attr.use_assoc = 1;
5659 sym->from_intmod = module;
5660 sym->intmod_sym_id = id;
5664 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
5665 the current namespace for all named constants, pointer types, and
5666 procedures in the module unless the only clause was used or a rename
5667 list was provided. */
5669 static void
5670 import_iso_c_binding_module (void)
5672 gfc_symbol *mod_sym = NULL, *return_type;
5673 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
5674 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
5675 const char *iso_c_module_name = "__iso_c_binding";
5676 gfc_use_rename *u;
5677 int i;
5678 bool want_c_ptr = false, want_c_funptr = false;
5680 /* Look only in the current namespace. */
5681 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
5683 if (mod_symtree == NULL)
5685 /* symtree doesn't already exist in current namespace. */
5686 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
5687 false);
5689 if (mod_symtree != NULL)
5690 mod_sym = mod_symtree->n.sym;
5691 else
5692 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
5693 "create symbol for %s", iso_c_module_name);
5695 mod_sym->attr.flavor = FL_MODULE;
5696 mod_sym->attr.intrinsic = 1;
5697 mod_sym->module = gfc_get_string (iso_c_module_name);
5698 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
5701 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
5702 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
5703 need C_(FUN)PTR. */
5704 for (u = gfc_rename_list; u; u = u->next)
5706 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
5707 u->use_name) == 0)
5708 want_c_ptr = true;
5709 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
5710 u->use_name) == 0)
5711 want_c_ptr = true;
5712 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
5713 u->use_name) == 0)
5714 want_c_funptr = true;
5715 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
5716 u->use_name) == 0)
5717 want_c_funptr = true;
5718 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
5719 u->use_name) == 0)
5721 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5722 (iso_c_binding_symbol)
5723 ISOCBINDING_PTR,
5724 u->local_name[0] ? u->local_name
5725 : u->use_name,
5726 NULL, false);
5728 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
5729 u->use_name) == 0)
5731 c_funptr
5732 = generate_isocbinding_symbol (iso_c_module_name,
5733 (iso_c_binding_symbol)
5734 ISOCBINDING_FUNPTR,
5735 u->local_name[0] ? u->local_name
5736 : u->use_name,
5737 NULL, false);
5741 if ((want_c_ptr || !only_flag) && !c_ptr)
5742 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5743 (iso_c_binding_symbol)
5744 ISOCBINDING_PTR,
5745 NULL, NULL, only_flag);
5746 if ((want_c_funptr || !only_flag) && !c_funptr)
5747 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
5748 (iso_c_binding_symbol)
5749 ISOCBINDING_FUNPTR,
5750 NULL, NULL, only_flag);
5752 /* Generate the symbols for the named constants representing
5753 the kinds for intrinsic data types. */
5754 for (i = 0; i < ISOCBINDING_NUMBER; i++)
5756 bool found = false;
5757 for (u = gfc_rename_list; u; u = u->next)
5758 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
5760 bool not_in_std;
5761 const char *name;
5762 u->found = 1;
5763 found = true;
5765 switch (i)
5767 #define NAMED_FUNCTION(a,b,c,d) \
5768 case a: \
5769 not_in_std = (gfc_option.allow_std & d) == 0; \
5770 name = b; \
5771 break;
5772 #define NAMED_SUBROUTINE(a,b,c,d) \
5773 case a: \
5774 not_in_std = (gfc_option.allow_std & d) == 0; \
5775 name = b; \
5776 break;
5777 #define NAMED_INTCST(a,b,c,d) \
5778 case a: \
5779 not_in_std = (gfc_option.allow_std & d) == 0; \
5780 name = b; \
5781 break;
5782 #define NAMED_REALCST(a,b,c,d) \
5783 case a: \
5784 not_in_std = (gfc_option.allow_std & d) == 0; \
5785 name = b; \
5786 break;
5787 #define NAMED_CMPXCST(a,b,c,d) \
5788 case a: \
5789 not_in_std = (gfc_option.allow_std & d) == 0; \
5790 name = b; \
5791 break;
5792 #include "iso-c-binding.def"
5793 default:
5794 not_in_std = false;
5795 name = "";
5798 if (not_in_std)
5800 gfc_error ("The symbol '%s', referenced at %L, is not "
5801 "in the selected standard", name, &u->where);
5802 continue;
5805 switch (i)
5807 #define NAMED_FUNCTION(a,b,c,d) \
5808 case a: \
5809 if (a == ISOCBINDING_LOC) \
5810 return_type = c_ptr->n.sym; \
5811 else if (a == ISOCBINDING_FUNLOC) \
5812 return_type = c_funptr->n.sym; \
5813 else \
5814 return_type = NULL; \
5815 create_intrinsic_function (u->local_name[0] \
5816 ? u->local_name : u->use_name, \
5817 a, iso_c_module_name, \
5818 INTMOD_ISO_C_BINDING, false, \
5819 return_type); \
5820 break;
5821 #define NAMED_SUBROUTINE(a,b,c,d) \
5822 case a: \
5823 create_intrinsic_function (u->local_name[0] ? u->local_name \
5824 : u->use_name, \
5825 a, iso_c_module_name, \
5826 INTMOD_ISO_C_BINDING, true, NULL); \
5827 break;
5828 #include "iso-c-binding.def"
5830 case ISOCBINDING_PTR:
5831 case ISOCBINDING_FUNPTR:
5832 /* Already handled above. */
5833 break;
5834 default:
5835 if (i == ISOCBINDING_NULL_PTR)
5836 tmp_symtree = c_ptr;
5837 else if (i == ISOCBINDING_NULL_FUNPTR)
5838 tmp_symtree = c_funptr;
5839 else
5840 tmp_symtree = NULL;
5841 generate_isocbinding_symbol (iso_c_module_name,
5842 (iso_c_binding_symbol) i,
5843 u->local_name[0]
5844 ? u->local_name : u->use_name,
5845 tmp_symtree, false);
5849 if (!found && !only_flag)
5851 /* Skip, if the symbol is not in the enabled standard. */
5852 switch (i)
5854 #define NAMED_FUNCTION(a,b,c,d) \
5855 case a: \
5856 if ((gfc_option.allow_std & d) == 0) \
5857 continue; \
5858 break;
5859 #define NAMED_SUBROUTINE(a,b,c,d) \
5860 case a: \
5861 if ((gfc_option.allow_std & d) == 0) \
5862 continue; \
5863 break;
5864 #define NAMED_INTCST(a,b,c,d) \
5865 case a: \
5866 if ((gfc_option.allow_std & d) == 0) \
5867 continue; \
5868 break;
5869 #define NAMED_REALCST(a,b,c,d) \
5870 case a: \
5871 if ((gfc_option.allow_std & d) == 0) \
5872 continue; \
5873 break;
5874 #define NAMED_CMPXCST(a,b,c,d) \
5875 case a: \
5876 if ((gfc_option.allow_std & d) == 0) \
5877 continue; \
5878 break;
5879 #include "iso-c-binding.def"
5880 default:
5881 ; /* Not GFC_STD_* versioned. */
5884 switch (i)
5886 #define NAMED_FUNCTION(a,b,c,d) \
5887 case a: \
5888 if (a == ISOCBINDING_LOC) \
5889 return_type = c_ptr->n.sym; \
5890 else if (a == ISOCBINDING_FUNLOC) \
5891 return_type = c_funptr->n.sym; \
5892 else \
5893 return_type = NULL; \
5894 create_intrinsic_function (b, a, iso_c_module_name, \
5895 INTMOD_ISO_C_BINDING, false, \
5896 return_type); \
5897 break;
5898 #define NAMED_SUBROUTINE(a,b,c,d) \
5899 case a: \
5900 create_intrinsic_function (b, a, iso_c_module_name, \
5901 INTMOD_ISO_C_BINDING, true, NULL); \
5902 break;
5903 #include "iso-c-binding.def"
5905 case ISOCBINDING_PTR:
5906 case ISOCBINDING_FUNPTR:
5907 /* Already handled above. */
5908 break;
5909 default:
5910 if (i == ISOCBINDING_NULL_PTR)
5911 tmp_symtree = c_ptr;
5912 else if (i == ISOCBINDING_NULL_FUNPTR)
5913 tmp_symtree = c_funptr;
5914 else
5915 tmp_symtree = NULL;
5916 generate_isocbinding_symbol (iso_c_module_name,
5917 (iso_c_binding_symbol) i, NULL,
5918 tmp_symtree, false);
5923 for (u = gfc_rename_list; u; u = u->next)
5925 if (u->found)
5926 continue;
5928 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
5929 "module ISO_C_BINDING", u->use_name, &u->where);
5934 /* Add an integer named constant from a given module. */
5936 static void
5937 create_int_parameter (const char *name, int value, const char *modname,
5938 intmod_id module, int id)
5940 gfc_symtree *tmp_symtree;
5941 gfc_symbol *sym;
5943 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5944 if (tmp_symtree != NULL)
5946 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5947 return;
5948 else
5949 gfc_error ("Symbol '%s' already declared", name);
5952 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5953 sym = tmp_symtree->n.sym;
5955 sym->module = gfc_get_string (modname);
5956 sym->attr.flavor = FL_PARAMETER;
5957 sym->ts.type = BT_INTEGER;
5958 sym->ts.kind = gfc_default_integer_kind;
5959 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
5960 sym->attr.use_assoc = 1;
5961 sym->from_intmod = module;
5962 sym->intmod_sym_id = id;
5966 /* Value is already contained by the array constructor, but not
5967 yet the shape. */
5969 static void
5970 create_int_parameter_array (const char *name, int size, gfc_expr *value,
5971 const char *modname, intmod_id module, int id)
5973 gfc_symtree *tmp_symtree;
5974 gfc_symbol *sym;
5976 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5977 if (tmp_symtree != NULL)
5979 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5980 return;
5981 else
5982 gfc_error ("Symbol '%s' already declared", name);
5985 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5986 sym = tmp_symtree->n.sym;
5988 sym->module = gfc_get_string (modname);
5989 sym->attr.flavor = FL_PARAMETER;
5990 sym->ts.type = BT_INTEGER;
5991 sym->ts.kind = gfc_default_integer_kind;
5992 sym->attr.use_assoc = 1;
5993 sym->from_intmod = module;
5994 sym->intmod_sym_id = id;
5995 sym->attr.dimension = 1;
5996 sym->as = gfc_get_array_spec ();
5997 sym->as->rank = 1;
5998 sym->as->type = AS_EXPLICIT;
5999 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6000 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6002 sym->value = value;
6003 sym->value->shape = gfc_get_shape (1);
6004 mpz_init_set_ui (sym->value->shape[0], size);
6008 /* Add an derived type for a given module. */
6010 static void
6011 create_derived_type (const char *name, const char *modname,
6012 intmod_id module, int id)
6014 gfc_symtree *tmp_symtree;
6015 gfc_symbol *sym, *dt_sym;
6016 gfc_interface *intr, *head;
6018 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6019 if (tmp_symtree != NULL)
6021 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6022 return;
6023 else
6024 gfc_error ("Symbol '%s' already declared", name);
6027 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6028 sym = tmp_symtree->n.sym;
6029 sym->module = gfc_get_string (modname);
6030 sym->from_intmod = module;
6031 sym->intmod_sym_id = id;
6032 sym->attr.flavor = FL_PROCEDURE;
6033 sym->attr.function = 1;
6034 sym->attr.generic = 1;
6036 gfc_get_sym_tree (dt_upper_string (sym->name),
6037 gfc_current_ns, &tmp_symtree, false);
6038 dt_sym = tmp_symtree->n.sym;
6039 dt_sym->name = gfc_get_string (sym->name);
6040 dt_sym->attr.flavor = FL_DERIVED;
6041 dt_sym->attr.private_comp = 1;
6042 dt_sym->attr.zero_comp = 1;
6043 dt_sym->attr.use_assoc = 1;
6044 dt_sym->module = gfc_get_string (modname);
6045 dt_sym->from_intmod = module;
6046 dt_sym->intmod_sym_id = id;
6048 head = sym->generic;
6049 intr = gfc_get_interface ();
6050 intr->sym = dt_sym;
6051 intr->where = gfc_current_locus;
6052 intr->next = head;
6053 sym->generic = intr;
6054 sym->attr.if_source = IFSRC_DECL;
6058 /* Read the contents of the module file into a temporary buffer. */
6060 static void
6061 read_module_to_tmpbuf ()
6063 /* We don't know the uncompressed size, so enlarge the buffer as
6064 needed. */
6065 int cursz = 4096;
6066 int rsize = cursz;
6067 int len = 0;
6069 module_content = XNEWVEC (char, cursz);
6071 while (1)
6073 int nread = gzread (module_fp, module_content + len, rsize);
6074 len += nread;
6075 if (nread < rsize)
6076 break;
6077 cursz *= 2;
6078 module_content = XRESIZEVEC (char, module_content, cursz);
6079 rsize = cursz - len;
6082 module_content = XRESIZEVEC (char, module_content, len + 1);
6083 module_content[len] = '\0';
6085 module_pos = 0;
6089 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6091 static void
6092 use_iso_fortran_env_module (void)
6094 static char mod[] = "iso_fortran_env";
6095 gfc_use_rename *u;
6096 gfc_symbol *mod_sym;
6097 gfc_symtree *mod_symtree;
6098 gfc_expr *expr;
6099 int i, j;
6101 intmod_sym symbol[] = {
6102 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6103 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6104 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6105 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6106 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6107 #include "iso-fortran-env.def"
6108 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6110 i = 0;
6111 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6112 #include "iso-fortran-env.def"
6114 /* Generate the symbol for the module itself. */
6115 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6116 if (mod_symtree == NULL)
6118 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6119 gcc_assert (mod_symtree);
6120 mod_sym = mod_symtree->n.sym;
6122 mod_sym->attr.flavor = FL_MODULE;
6123 mod_sym->attr.intrinsic = 1;
6124 mod_sym->module = gfc_get_string (mod);
6125 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6127 else
6128 if (!mod_symtree->n.sym->attr.intrinsic)
6129 gfc_error ("Use of intrinsic module '%s' at %C conflicts with "
6130 "non-intrinsic module name used previously", mod);
6132 /* Generate the symbols for the module integer named constants. */
6134 for (i = 0; symbol[i].name; i++)
6136 bool found = false;
6137 for (u = gfc_rename_list; u; u = u->next)
6139 if (strcmp (symbol[i].name, u->use_name) == 0)
6141 found = true;
6142 u->found = 1;
6144 if (!gfc_notify_std (symbol[i].standard, "The symbol '%s', "
6145 "referenced at %L, is not in the selected "
6146 "standard", symbol[i].name, &u->where))
6147 continue;
6149 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6150 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6151 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6152 "constant from intrinsic module "
6153 "ISO_FORTRAN_ENV at %L is incompatible with "
6154 "option %s", &u->where,
6155 gfc_option.flag_default_integer
6156 ? "-fdefault-integer-8"
6157 : "-fdefault-real-8");
6158 switch (symbol[i].id)
6160 #define NAMED_INTCST(a,b,c,d) \
6161 case a:
6162 #include "iso-fortran-env.def"
6163 create_int_parameter (u->local_name[0] ? u->local_name
6164 : u->use_name,
6165 symbol[i].value, mod,
6166 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6167 break;
6169 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6170 case a:\
6171 expr = gfc_get_array_expr (BT_INTEGER, \
6172 gfc_default_integer_kind,\
6173 NULL); \
6174 for (j = 0; KINDS[j].kind != 0; j++) \
6175 gfc_constructor_append_expr (&expr->value.constructor, \
6176 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6177 KINDS[j].kind), NULL); \
6178 create_int_parameter_array (u->local_name[0] ? u->local_name \
6179 : u->use_name, \
6180 j, expr, mod, \
6181 INTMOD_ISO_FORTRAN_ENV, \
6182 symbol[i].id); \
6183 break;
6184 #include "iso-fortran-env.def"
6186 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6187 case a:
6188 #include "iso-fortran-env.def"
6189 create_derived_type (u->local_name[0] ? u->local_name
6190 : u->use_name,
6191 mod, INTMOD_ISO_FORTRAN_ENV,
6192 symbol[i].id);
6193 break;
6195 #define NAMED_FUNCTION(a,b,c,d) \
6196 case a:
6197 #include "iso-fortran-env.def"
6198 create_intrinsic_function (u->local_name[0] ? u->local_name
6199 : u->use_name,
6200 symbol[i].id, mod,
6201 INTMOD_ISO_FORTRAN_ENV, false,
6202 NULL);
6203 break;
6205 default:
6206 gcc_unreachable ();
6211 if (!found && !only_flag)
6213 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6214 continue;
6216 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6217 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6218 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6219 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6220 "incompatible with option %s",
6221 gfc_option.flag_default_integer
6222 ? "-fdefault-integer-8" : "-fdefault-real-8");
6224 switch (symbol[i].id)
6226 #define NAMED_INTCST(a,b,c,d) \
6227 case a:
6228 #include "iso-fortran-env.def"
6229 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6230 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6231 break;
6233 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6234 case a:\
6235 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6236 NULL); \
6237 for (j = 0; KINDS[j].kind != 0; j++) \
6238 gfc_constructor_append_expr (&expr->value.constructor, \
6239 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6240 KINDS[j].kind), NULL); \
6241 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6242 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6243 break;
6244 #include "iso-fortran-env.def"
6246 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6247 case a:
6248 #include "iso-fortran-env.def"
6249 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6250 symbol[i].id);
6251 break;
6253 #define NAMED_FUNCTION(a,b,c,d) \
6254 case a:
6255 #include "iso-fortran-env.def"
6256 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6257 INTMOD_ISO_FORTRAN_ENV, false,
6258 NULL);
6259 break;
6261 default:
6262 gcc_unreachable ();
6267 for (u = gfc_rename_list; u; u = u->next)
6269 if (u->found)
6270 continue;
6272 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
6273 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6278 /* Process a USE directive. */
6280 static void
6281 gfc_use_module (gfc_use_list *module)
6283 char *filename;
6284 gfc_state_data *p;
6285 int c, line, start;
6286 gfc_symtree *mod_symtree;
6287 gfc_use_list *use_stmt;
6288 locus old_locus = gfc_current_locus;
6290 gfc_current_locus = module->where;
6291 module_name = module->module_name;
6292 gfc_rename_list = module->rename;
6293 only_flag = module->only_flag;
6295 filename = XALLOCAVEC (char, strlen (module_name) + strlen (MODULE_EXTENSION)
6296 + 1);
6297 strcpy (filename, module_name);
6298 strcat (filename, MODULE_EXTENSION);
6300 /* First, try to find an non-intrinsic module, unless the USE statement
6301 specified that the module is intrinsic. */
6302 module_fp = NULL;
6303 if (!module->intrinsic)
6304 module_fp = gzopen_included_file (filename, true, true);
6306 /* Then, see if it's an intrinsic one, unless the USE statement
6307 specified that the module is non-intrinsic. */
6308 if (module_fp == NULL && !module->non_intrinsic)
6310 if (strcmp (module_name, "iso_fortran_env") == 0
6311 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6312 "intrinsic module at %C"))
6314 use_iso_fortran_env_module ();
6315 free_rename (module->rename);
6316 module->rename = NULL;
6317 gfc_current_locus = old_locus;
6318 module->intrinsic = true;
6319 return;
6322 if (strcmp (module_name, "iso_c_binding") == 0
6323 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6325 import_iso_c_binding_module();
6326 free_rename (module->rename);
6327 module->rename = NULL;
6328 gfc_current_locus = old_locus;
6329 module->intrinsic = true;
6330 return;
6333 module_fp = gzopen_intrinsic_module (filename);
6335 if (module_fp == NULL && module->intrinsic)
6336 gfc_fatal_error ("Can't find an intrinsic module named '%s' at %C",
6337 module_name);
6340 if (module_fp == NULL)
6341 gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
6342 filename, xstrerror (errno));
6344 /* Check that we haven't already USEd an intrinsic module with the
6345 same name. */
6347 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6348 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6349 gfc_error ("Use of non-intrinsic module '%s' at %C conflicts with "
6350 "intrinsic module name used previously", module_name);
6352 iomode = IO_INPUT;
6353 module_line = 1;
6354 module_column = 1;
6355 start = 0;
6357 read_module_to_tmpbuf ();
6358 gzclose (module_fp);
6360 /* Skip the first line of the module, after checking that this is
6361 a gfortran module file. */
6362 line = 0;
6363 while (line < 1)
6365 c = module_char ();
6366 if (c == EOF)
6367 bad_module ("Unexpected end of module");
6368 if (start++ < 3)
6369 parse_name (c);
6370 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6371 || (start == 2 && strcmp (atom_name, " module") != 0))
6372 gfc_fatal_error ("File '%s' opened at %C is not a GNU Fortran"
6373 " module file", filename);
6374 if (start == 3)
6376 if (strcmp (atom_name, " version") != 0
6377 || module_char () != ' '
6378 || parse_atom () != ATOM_STRING
6379 || strcmp (atom_string, MOD_VERSION))
6380 gfc_fatal_error ("Cannot read module file '%s' opened at %C,"
6381 " because it was created by a different"
6382 " version of GNU Fortran", filename);
6384 free (atom_string);
6387 if (c == '\n')
6388 line++;
6391 /* Make sure we're not reading the same module that we may be building. */
6392 for (p = gfc_state_stack; p; p = p->previous)
6393 if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
6394 gfc_fatal_error ("Can't USE the same module we're building!");
6396 init_pi_tree ();
6397 init_true_name_tree ();
6399 read_module ();
6401 free_true_name (true_name_root);
6402 true_name_root = NULL;
6404 free_pi_tree (pi_root);
6405 pi_root = NULL;
6407 XDELETEVEC (module_content);
6408 module_content = NULL;
6410 use_stmt = gfc_get_use_list ();
6411 *use_stmt = *module;
6412 use_stmt->next = gfc_current_ns->use_stmts;
6413 gfc_current_ns->use_stmts = use_stmt;
6415 gfc_current_locus = old_locus;
6419 /* Remove duplicated intrinsic operators from the rename list. */
6421 static void
6422 rename_list_remove_duplicate (gfc_use_rename *list)
6424 gfc_use_rename *seek, *last;
6426 for (; list; list = list->next)
6427 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6429 last = list;
6430 for (seek = list->next; seek; seek = last->next)
6432 if (list->op == seek->op)
6434 last->next = seek->next;
6435 free (seek);
6437 else
6438 last = seek;
6444 /* Process all USE directives. */
6446 void
6447 gfc_use_modules (void)
6449 gfc_use_list *next, *seek, *last;
6451 for (next = module_list; next; next = next->next)
6453 bool non_intrinsic = next->non_intrinsic;
6454 bool intrinsic = next->intrinsic;
6455 bool neither = !non_intrinsic && !intrinsic;
6457 for (seek = next->next; seek; seek = seek->next)
6459 if (next->module_name != seek->module_name)
6460 continue;
6462 if (seek->non_intrinsic)
6463 non_intrinsic = true;
6464 else if (seek->intrinsic)
6465 intrinsic = true;
6466 else
6467 neither = true;
6470 if (intrinsic && neither && !non_intrinsic)
6472 char *filename;
6473 FILE *fp;
6475 filename = XALLOCAVEC (char,
6476 strlen (next->module_name)
6477 + strlen (MODULE_EXTENSION) + 1);
6478 strcpy (filename, next->module_name);
6479 strcat (filename, MODULE_EXTENSION);
6480 fp = gfc_open_included_file (filename, true, true);
6481 if (fp != NULL)
6483 non_intrinsic = true;
6484 fclose (fp);
6488 last = next;
6489 for (seek = next->next; seek; seek = last->next)
6491 if (next->module_name != seek->module_name)
6493 last = seek;
6494 continue;
6497 if ((!next->intrinsic && !seek->intrinsic)
6498 || (next->intrinsic && seek->intrinsic)
6499 || !non_intrinsic)
6501 if (!seek->only_flag)
6502 next->only_flag = false;
6503 if (seek->rename)
6505 gfc_use_rename *r = seek->rename;
6506 while (r->next)
6507 r = r->next;
6508 r->next = next->rename;
6509 next->rename = seek->rename;
6511 last->next = seek->next;
6512 free (seek);
6514 else
6515 last = seek;
6519 for (; module_list; module_list = next)
6521 next = module_list->next;
6522 rename_list_remove_duplicate (module_list->rename);
6523 gfc_use_module (module_list);
6524 free (module_list);
6526 gfc_rename_list = NULL;
6530 void
6531 gfc_free_use_stmts (gfc_use_list *use_stmts)
6533 gfc_use_list *next;
6534 for (; use_stmts; use_stmts = next)
6536 gfc_use_rename *next_rename;
6538 for (; use_stmts->rename; use_stmts->rename = next_rename)
6540 next_rename = use_stmts->rename->next;
6541 free (use_stmts->rename);
6543 next = use_stmts->next;
6544 free (use_stmts);
6549 void
6550 gfc_module_init_2 (void)
6552 last_atom = ATOM_LPAREN;
6553 gfc_rename_list = NULL;
6554 module_list = NULL;
6558 void
6559 gfc_module_done_2 (void)
6561 free_rename (gfc_rename_list);
6562 gfc_rename_list = NULL;