* de.po: Update.
[official-gcc.git] / gcc / fortran / module.c
blob5515fed4ab4e17d7feeae637dc05412b6e48c4b9
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "options.h"
71 #include "tree.h"
72 #include "gfortran.h"
73 #include "stringpool.h"
74 #include "arith.h"
75 #include "match.h"
76 #include "parse.h" /* FIXME */
77 #include "constructor.h"
78 #include "cpp.h"
79 #include "scanner.h"
80 #include <zlib.h>
82 #define MODULE_EXTENSION ".mod"
83 #define SUBMODULE_EXTENSION ".smod"
85 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
86 recognized. */
87 #define MOD_VERSION "14"
90 /* Structure that describes a position within a module file. */
92 typedef struct
94 int column, line;
95 long pos;
97 module_locus;
99 /* Structure for list of symbols of intrinsic modules. */
100 typedef struct
102 int id;
103 const char *name;
104 int value;
105 int standard;
107 intmod_sym;
110 typedef enum
112 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
114 pointer_t;
116 /* The fixup structure lists pointers to pointers that have to
117 be updated when a pointer value becomes known. */
119 typedef struct fixup_t
121 void **pointer;
122 struct fixup_t *next;
124 fixup_t;
127 /* Structure for holding extra info needed for pointers being read. */
129 enum gfc_rsym_state
131 UNUSED,
132 NEEDED,
133 USED
136 enum gfc_wsym_state
138 UNREFERENCED = 0,
139 NEEDS_WRITE,
140 WRITTEN
143 typedef struct pointer_info
145 BBT_HEADER (pointer_info);
146 int integer;
147 pointer_t type;
149 /* The first component of each member of the union is the pointer
150 being stored. */
152 fixup_t *fixup;
154 union
156 void *pointer; /* Member for doing pointer searches. */
158 struct
160 gfc_symbol *sym;
161 char *true_name, *module, *binding_label;
162 fixup_t *stfixup;
163 gfc_symtree *symtree;
164 enum gfc_rsym_state state;
165 int ns, referenced, renamed;
166 module_locus where;
168 rsym;
170 struct
172 gfc_symbol *sym;
173 enum gfc_wsym_state state;
175 wsym;
180 pointer_info;
182 #define gfc_get_pointer_info() XCNEW (pointer_info)
185 /* Local variables */
187 /* The gzFile for the module we're reading or writing. */
188 static gzFile module_fp;
191 /* The name of the module we're reading (USE'ing) or writing. */
192 static const char *module_name;
193 /* The name of the .smod file that the submodule will write to. */
194 static const char *submodule_name;
196 /* Suppress the output of a .smod file by module, if no module
197 procedures have been seen. */
198 static bool no_module_procedures;
200 static gfc_use_list *module_list;
202 /* If we're reading an intrinsic module, this is its ID. */
203 static intmod_id current_intmod;
205 /* Content of module. */
206 static char* module_content;
208 static long module_pos;
209 static int module_line, module_column, only_flag;
210 static int prev_module_line, prev_module_column;
212 static enum
213 { IO_INPUT, IO_OUTPUT }
214 iomode;
216 static gfc_use_rename *gfc_rename_list;
217 static pointer_info *pi_root;
218 static int symbol_number; /* Counter for assigning symbol numbers */
220 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
221 static bool in_load_equiv;
225 /*****************************************************************/
227 /* Pointer/integer conversion. Pointers between structures are stored
228 as integers in the module file. The next couple of subroutines
229 handle this translation for reading and writing. */
231 /* Recursively free the tree of pointer structures. */
233 static void
234 free_pi_tree (pointer_info *p)
236 if (p == NULL)
237 return;
239 if (p->fixup != NULL)
240 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
242 free_pi_tree (p->left);
243 free_pi_tree (p->right);
245 if (iomode == IO_INPUT)
247 XDELETEVEC (p->u.rsym.true_name);
248 XDELETEVEC (p->u.rsym.module);
249 XDELETEVEC (p->u.rsym.binding_label);
252 free (p);
256 /* Compare pointers when searching by pointer. Used when writing a
257 module. */
259 static int
260 compare_pointers (void *_sn1, void *_sn2)
262 pointer_info *sn1, *sn2;
264 sn1 = (pointer_info *) _sn1;
265 sn2 = (pointer_info *) _sn2;
267 if (sn1->u.pointer < sn2->u.pointer)
268 return -1;
269 if (sn1->u.pointer > sn2->u.pointer)
270 return 1;
272 return 0;
276 /* Compare integers when searching by integer. Used when reading a
277 module. */
279 static int
280 compare_integers (void *_sn1, void *_sn2)
282 pointer_info *sn1, *sn2;
284 sn1 = (pointer_info *) _sn1;
285 sn2 = (pointer_info *) _sn2;
287 if (sn1->integer < sn2->integer)
288 return -1;
289 if (sn1->integer > sn2->integer)
290 return 1;
292 return 0;
296 /* Initialize the pointer_info tree. */
298 static void
299 init_pi_tree (void)
301 compare_fn compare;
302 pointer_info *p;
304 pi_root = NULL;
305 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
307 /* Pointer 0 is the NULL pointer. */
308 p = gfc_get_pointer_info ();
309 p->u.pointer = NULL;
310 p->integer = 0;
311 p->type = P_OTHER;
313 gfc_insert_bbt (&pi_root, p, compare);
315 /* Pointer 1 is the current namespace. */
316 p = gfc_get_pointer_info ();
317 p->u.pointer = gfc_current_ns;
318 p->integer = 1;
319 p->type = P_NAMESPACE;
321 gfc_insert_bbt (&pi_root, p, compare);
323 symbol_number = 2;
327 /* During module writing, call here with a pointer to something,
328 returning the pointer_info node. */
330 static pointer_info *
331 find_pointer (void *gp)
333 pointer_info *p;
335 p = pi_root;
336 while (p != NULL)
338 if (p->u.pointer == gp)
339 break;
340 p = (gp < p->u.pointer) ? p->left : p->right;
343 return p;
347 /* Given a pointer while writing, returns the pointer_info tree node,
348 creating it if it doesn't exist. */
350 static pointer_info *
351 get_pointer (void *gp)
353 pointer_info *p;
355 p = find_pointer (gp);
356 if (p != NULL)
357 return p;
359 /* Pointer doesn't have an integer. Give it one. */
360 p = gfc_get_pointer_info ();
362 p->u.pointer = gp;
363 p->integer = symbol_number++;
365 gfc_insert_bbt (&pi_root, p, compare_pointers);
367 return p;
371 /* Given an integer during reading, find it in the pointer_info tree,
372 creating the node if not found. */
374 static pointer_info *
375 get_integer (int integer)
377 pointer_info *p, t;
378 int c;
380 t.integer = integer;
382 p = pi_root;
383 while (p != NULL)
385 c = compare_integers (&t, p);
386 if (c == 0)
387 break;
389 p = (c < 0) ? p->left : p->right;
392 if (p != NULL)
393 return p;
395 p = gfc_get_pointer_info ();
396 p->integer = integer;
397 p->u.pointer = NULL;
399 gfc_insert_bbt (&pi_root, p, compare_integers);
401 return p;
405 /* Resolve any fixups using a known pointer. */
407 static void
408 resolve_fixups (fixup_t *f, void *gp)
410 fixup_t *next;
412 for (; f; f = next)
414 next = f->next;
415 *(f->pointer) = gp;
416 free (f);
421 /* Convert a string such that it starts with a lower-case character. Used
422 to convert the symtree name of a derived-type to the symbol name or to
423 the name of the associated generic function. */
425 const char *
426 gfc_dt_lower_string (const char *name)
428 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
429 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
430 &name[1]);
431 return gfc_get_string ("%s", name);
435 /* Convert a string such that it starts with an upper-case character. Used to
436 return the symtree-name for a derived type; the symbol name itself and the
437 symtree/symbol name of the associated generic function start with a lower-
438 case character. */
440 const char *
441 gfc_dt_upper_string (const char *name)
443 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
444 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
445 &name[1]);
446 return gfc_get_string ("%s", name);
449 /* Call here during module reading when we know what pointer to
450 associate with an integer. Any fixups that exist are resolved at
451 this time. */
453 static void
454 associate_integer_pointer (pointer_info *p, void *gp)
456 if (p->u.pointer != NULL)
457 gfc_internal_error ("associate_integer_pointer(): Already associated");
459 p->u.pointer = gp;
461 resolve_fixups (p->fixup, gp);
463 p->fixup = NULL;
467 /* During module reading, given an integer and a pointer to a pointer,
468 either store the pointer from an already-known value or create a
469 fixup structure in order to store things later. Returns zero if
470 the reference has been actually stored, or nonzero if the reference
471 must be fixed later (i.e., associate_integer_pointer must be called
472 sometime later. Returns the pointer_info structure. */
474 static pointer_info *
475 add_fixup (int integer, void *gp)
477 pointer_info *p;
478 fixup_t *f;
479 char **cp;
481 p = get_integer (integer);
483 if (p->integer == 0 || p->u.pointer != NULL)
485 cp = (char **) gp;
486 *cp = (char *) p->u.pointer;
488 else
490 f = XCNEW (fixup_t);
492 f->next = p->fixup;
493 p->fixup = f;
495 f->pointer = (void **) gp;
498 return p;
502 /*****************************************************************/
504 /* Parser related subroutines */
506 /* Free the rename list left behind by a USE statement. */
508 static void
509 free_rename (gfc_use_rename *list)
511 gfc_use_rename *next;
513 for (; list; list = next)
515 next = list->next;
516 free (list);
521 /* Match a USE statement. */
523 match
524 gfc_match_use (void)
526 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
527 gfc_use_rename *tail = NULL, *new_use;
528 interface_type type, type2;
529 gfc_intrinsic_op op;
530 match m;
531 gfc_use_list *use_list;
533 use_list = gfc_get_use_list ();
535 if (gfc_match (" , ") == MATCH_YES)
537 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
539 if (!gfc_notify_std (GFC_STD_F2003, "module "
540 "nature in USE statement at %C"))
541 goto cleanup;
543 if (strcmp (module_nature, "intrinsic") == 0)
544 use_list->intrinsic = true;
545 else
547 if (strcmp (module_nature, "non_intrinsic") == 0)
548 use_list->non_intrinsic = true;
549 else
551 gfc_error ("Module nature in USE statement at %C shall "
552 "be either INTRINSIC or NON_INTRINSIC");
553 goto cleanup;
557 else
559 /* Help output a better error message than "Unclassifiable
560 statement". */
561 gfc_match (" %n", module_nature);
562 if (strcmp (module_nature, "intrinsic") == 0
563 || strcmp (module_nature, "non_intrinsic") == 0)
564 gfc_error ("\"::\" was expected after module nature at %C "
565 "but was not found");
566 free (use_list);
567 return m;
570 else
572 m = gfc_match (" ::");
573 if (m == MATCH_YES &&
574 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
575 goto cleanup;
577 if (m != MATCH_YES)
579 m = gfc_match ("% ");
580 if (m != MATCH_YES)
582 free (use_list);
583 return m;
588 use_list->where = gfc_current_locus;
590 m = gfc_match_name (name);
591 if (m != MATCH_YES)
593 free (use_list);
594 return m;
597 use_list->module_name = gfc_get_string ("%s", name);
599 if (gfc_match_eos () == MATCH_YES)
600 goto done;
602 if (gfc_match_char (',') != MATCH_YES)
603 goto syntax;
605 if (gfc_match (" only :") == MATCH_YES)
606 use_list->only_flag = true;
608 if (gfc_match_eos () == MATCH_YES)
609 goto done;
611 for (;;)
613 /* Get a new rename struct and add it to the rename list. */
614 new_use = gfc_get_use_rename ();
615 new_use->where = gfc_current_locus;
616 new_use->found = 0;
618 if (use_list->rename == NULL)
619 use_list->rename = new_use;
620 else
621 tail->next = new_use;
622 tail = new_use;
624 /* See what kind of interface we're dealing with. Assume it is
625 not an operator. */
626 new_use->op = INTRINSIC_NONE;
627 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
628 goto cleanup;
630 switch (type)
632 case INTERFACE_NAMELESS:
633 gfc_error ("Missing generic specification in USE statement at %C");
634 goto cleanup;
636 case INTERFACE_USER_OP:
637 case INTERFACE_GENERIC:
638 m = gfc_match (" =>");
640 if (type == INTERFACE_USER_OP && m == MATCH_YES
641 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
642 "operators in USE statements at %C")))
643 goto cleanup;
645 if (type == INTERFACE_USER_OP)
646 new_use->op = INTRINSIC_USER;
648 if (use_list->only_flag)
650 if (m != MATCH_YES)
651 strcpy (new_use->use_name, name);
652 else
654 strcpy (new_use->local_name, name);
655 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
656 if (type != type2)
657 goto syntax;
658 if (m == MATCH_NO)
659 goto syntax;
660 if (m == MATCH_ERROR)
661 goto cleanup;
664 else
666 if (m != MATCH_YES)
667 goto syntax;
668 strcpy (new_use->local_name, name);
670 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
671 if (type != type2)
672 goto syntax;
673 if (m == MATCH_NO)
674 goto syntax;
675 if (m == MATCH_ERROR)
676 goto cleanup;
679 if (strcmp (new_use->use_name, use_list->module_name) == 0
680 || strcmp (new_use->local_name, use_list->module_name) == 0)
682 gfc_error ("The name %qs at %C has already been used as "
683 "an external module name.", use_list->module_name);
684 goto cleanup;
686 break;
688 case INTERFACE_INTRINSIC_OP:
689 new_use->op = op;
690 break;
692 default:
693 gcc_unreachable ();
696 if (gfc_match_eos () == MATCH_YES)
697 break;
698 if (gfc_match_char (',') != MATCH_YES)
699 goto syntax;
702 done:
703 if (module_list)
705 gfc_use_list *last = module_list;
706 while (last->next)
707 last = last->next;
708 last->next = use_list;
710 else
711 module_list = use_list;
713 return MATCH_YES;
715 syntax:
716 gfc_syntax_error (ST_USE);
718 cleanup:
719 free_rename (use_list->rename);
720 free (use_list);
721 return MATCH_ERROR;
725 /* Match a SUBMODULE statement.
727 According to F2008:11.2.3.2, "The submodule identifier is the
728 ordered pair whose first element is the ancestor module name and
729 whose second element is the submodule name. 'Submodule_name' is
730 used for the submodule filename and uses '@' as a separator, whilst
731 the name of the symbol for the module uses '.' as a a separator.
732 The reasons for these choices are:
733 (i) To follow another leading brand in the submodule filenames;
734 (ii) Since '.' is not particularly visible in the filenames; and
735 (iii) The linker does not permit '@' in mnemonics. */
737 match
738 gfc_match_submodule (void)
740 match m;
741 char name[GFC_MAX_SYMBOL_LEN + 1];
742 gfc_use_list *use_list;
743 bool seen_colon = false;
745 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
746 return MATCH_ERROR;
748 gfc_new_block = NULL;
749 gcc_assert (module_list == NULL);
751 if (gfc_match_char ('(') != MATCH_YES)
752 goto syntax;
754 while (1)
756 m = gfc_match (" %n", name);
757 if (m != MATCH_YES)
758 goto syntax;
760 use_list = gfc_get_use_list ();
761 use_list->where = gfc_current_locus;
763 if (module_list)
765 gfc_use_list *last = module_list;
766 while (last->next)
767 last = last->next;
768 last->next = use_list;
769 use_list->module_name
770 = gfc_get_string ("%s.%s", module_list->module_name, name);
771 use_list->submodule_name
772 = gfc_get_string ("%s@%s", module_list->module_name, name);
774 else
776 module_list = use_list;
777 use_list->module_name = gfc_get_string ("%s", name);
778 use_list->submodule_name = use_list->module_name;
781 if (gfc_match_char (')') == MATCH_YES)
782 break;
784 if (gfc_match_char (':') != MATCH_YES
785 || seen_colon)
786 goto syntax;
788 seen_colon = true;
791 m = gfc_match (" %s%t", &gfc_new_block);
792 if (m != MATCH_YES)
793 goto syntax;
795 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
796 gfc_new_block->name);
798 gfc_new_block->name = gfc_get_string ("%s.%s",
799 module_list->module_name,
800 gfc_new_block->name);
802 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
803 gfc_new_block->name, NULL))
804 return MATCH_ERROR;
806 /* Just retain the ultimate .(s)mod file for reading, since it
807 contains all the information in its ancestors. */
808 use_list = module_list;
809 for (; module_list->next; use_list = module_list)
811 module_list = use_list->next;
812 free (use_list);
815 return MATCH_YES;
817 syntax:
818 gfc_error ("Syntax error in SUBMODULE statement at %C");
819 return MATCH_ERROR;
823 /* Given a name and a number, inst, return the inst name
824 under which to load this symbol. Returns NULL if this
825 symbol shouldn't be loaded. If inst is zero, returns
826 the number of instances of this name. If interface is
827 true, a user-defined operator is sought, otherwise only
828 non-operators are sought. */
830 static const char *
831 find_use_name_n (const char *name, int *inst, bool interface)
833 gfc_use_rename *u;
834 const char *low_name = NULL;
835 int i;
837 /* For derived types. */
838 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
839 low_name = gfc_dt_lower_string (name);
841 i = 0;
842 for (u = gfc_rename_list; u; u = u->next)
844 if ((!low_name && strcmp (u->use_name, name) != 0)
845 || (low_name && strcmp (u->use_name, low_name) != 0)
846 || (u->op == INTRINSIC_USER && !interface)
847 || (u->op != INTRINSIC_USER && interface))
848 continue;
849 if (++i == *inst)
850 break;
853 if (!*inst)
855 *inst = i;
856 return NULL;
859 if (u == NULL)
860 return only_flag ? NULL : name;
862 u->found = 1;
864 if (low_name)
866 if (u->local_name[0] == '\0')
867 return name;
868 return gfc_dt_upper_string (u->local_name);
871 return (u->local_name[0] != '\0') ? u->local_name : name;
875 /* Given a name, return the name under which to load this symbol.
876 Returns NULL if this symbol shouldn't be loaded. */
878 static const char *
879 find_use_name (const char *name, bool interface)
881 int i = 1;
882 return find_use_name_n (name, &i, interface);
886 /* Given a real name, return the number of use names associated with it. */
888 static int
889 number_use_names (const char *name, bool interface)
891 int i = 0;
892 find_use_name_n (name, &i, interface);
893 return i;
897 /* Try to find the operator in the current list. */
899 static gfc_use_rename *
900 find_use_operator (gfc_intrinsic_op op)
902 gfc_use_rename *u;
904 for (u = gfc_rename_list; u; u = u->next)
905 if (u->op == op)
906 return u;
908 return NULL;
912 /*****************************************************************/
914 /* The next couple of subroutines maintain a tree used to avoid a
915 brute-force search for a combination of true name and module name.
916 While symtree names, the name that a particular symbol is known by
917 can changed with USE statements, we still have to keep track of the
918 true names to generate the correct reference, and also avoid
919 loading the same real symbol twice in a program unit.
921 When we start reading, the true name tree is built and maintained
922 as symbols are read. The tree is searched as we load new symbols
923 to see if it already exists someplace in the namespace. */
925 typedef struct true_name
927 BBT_HEADER (true_name);
928 const char *name;
929 gfc_symbol *sym;
931 true_name;
933 static true_name *true_name_root;
936 /* Compare two true_name structures. */
938 static int
939 compare_true_names (void *_t1, void *_t2)
941 true_name *t1, *t2;
942 int c;
944 t1 = (true_name *) _t1;
945 t2 = (true_name *) _t2;
947 c = ((t1->sym->module > t2->sym->module)
948 - (t1->sym->module < t2->sym->module));
949 if (c != 0)
950 return c;
952 return strcmp (t1->name, t2->name);
956 /* Given a true name, search the true name tree to see if it exists
957 within the main namespace. */
959 static gfc_symbol *
960 find_true_name (const char *name, const char *module)
962 true_name t, *p;
963 gfc_symbol sym;
964 int c;
966 t.name = gfc_get_string ("%s", name);
967 if (module != NULL)
968 sym.module = gfc_get_string ("%s", module);
969 else
970 sym.module = NULL;
971 t.sym = &sym;
973 p = true_name_root;
974 while (p != NULL)
976 c = compare_true_names ((void *) (&t), (void *) p);
977 if (c == 0)
978 return p->sym;
980 p = (c < 0) ? p->left : p->right;
983 return NULL;
987 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
989 static void
990 add_true_name (gfc_symbol *sym)
992 true_name *t;
994 t = XCNEW (true_name);
995 t->sym = sym;
996 if (gfc_fl_struct (sym->attr.flavor))
997 t->name = gfc_dt_upper_string (sym->name);
998 else
999 t->name = sym->name;
1001 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1005 /* Recursive function to build the initial true name tree by
1006 recursively traversing the current namespace. */
1008 static void
1009 build_tnt (gfc_symtree *st)
1011 const char *name;
1012 if (st == NULL)
1013 return;
1015 build_tnt (st->left);
1016 build_tnt (st->right);
1018 if (gfc_fl_struct (st->n.sym->attr.flavor))
1019 name = gfc_dt_upper_string (st->n.sym->name);
1020 else
1021 name = st->n.sym->name;
1023 if (find_true_name (name, st->n.sym->module) != NULL)
1024 return;
1026 add_true_name (st->n.sym);
1030 /* Initialize the true name tree with the current namespace. */
1032 static void
1033 init_true_name_tree (void)
1035 true_name_root = NULL;
1036 build_tnt (gfc_current_ns->sym_root);
1040 /* Recursively free a true name tree node. */
1042 static void
1043 free_true_name (true_name *t)
1045 if (t == NULL)
1046 return;
1047 free_true_name (t->left);
1048 free_true_name (t->right);
1050 free (t);
1054 /*****************************************************************/
1056 /* Module reading and writing. */
1058 /* The following are versions similar to the ones in scanner.c, but
1059 for dealing with compressed module files. */
1061 static gzFile
1062 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1063 bool module, bool system)
1065 char *fullname;
1066 gfc_directorylist *p;
1067 gzFile f;
1069 for (p = list; p; p = p->next)
1071 if (module && !p->use_for_modules)
1072 continue;
1074 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
1075 strcpy (fullname, p->path);
1076 strcat (fullname, name);
1078 f = gzopen (fullname, "r");
1079 if (f != NULL)
1081 if (gfc_cpp_makedep ())
1082 gfc_cpp_add_dep (fullname, system);
1084 return f;
1088 return NULL;
1091 static gzFile
1092 gzopen_included_file (const char *name, bool include_cwd, bool module)
1094 gzFile f = NULL;
1096 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1098 f = gzopen (name, "r");
1099 if (f && gfc_cpp_makedep ())
1100 gfc_cpp_add_dep (name, false);
1103 if (!f)
1104 f = gzopen_included_file_1 (name, include_dirs, module, false);
1106 return f;
1109 static gzFile
1110 gzopen_intrinsic_module (const char* name)
1112 gzFile f = NULL;
1114 if (IS_ABSOLUTE_PATH (name))
1116 f = gzopen (name, "r");
1117 if (f && gfc_cpp_makedep ())
1118 gfc_cpp_add_dep (name, true);
1121 if (!f)
1122 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1124 return f;
1128 enum atom_type
1130 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1133 static atom_type last_atom;
1136 /* The name buffer must be at least as long as a symbol name. Right
1137 now it's not clear how we're going to store numeric constants--
1138 probably as a hexadecimal string, since this will allow the exact
1139 number to be preserved (this can't be done by a decimal
1140 representation). Worry about that later. TODO! */
1142 #define MAX_ATOM_SIZE 100
1144 static int atom_int;
1145 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1148 /* Report problems with a module. Error reporting is not very
1149 elaborate, since this sorts of errors shouldn't really happen.
1150 This subroutine never returns. */
1152 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1154 static void
1155 bad_module (const char *msgid)
1157 XDELETEVEC (module_content);
1158 module_content = NULL;
1160 switch (iomode)
1162 case IO_INPUT:
1163 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1164 module_name, module_line, module_column, msgid);
1165 break;
1166 case IO_OUTPUT:
1167 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1168 module_name, module_line, module_column, msgid);
1169 break;
1170 default:
1171 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1172 module_name, module_line, module_column, msgid);
1173 break;
1178 /* Set the module's input pointer. */
1180 static void
1181 set_module_locus (module_locus *m)
1183 module_column = m->column;
1184 module_line = m->line;
1185 module_pos = m->pos;
1189 /* Get the module's input pointer so that we can restore it later. */
1191 static void
1192 get_module_locus (module_locus *m)
1194 m->column = module_column;
1195 m->line = module_line;
1196 m->pos = module_pos;
1200 /* Get the next character in the module, updating our reckoning of
1201 where we are. */
1203 static int
1204 module_char (void)
1206 const char c = module_content[module_pos++];
1207 if (c == '\0')
1208 bad_module ("Unexpected EOF");
1210 prev_module_line = module_line;
1211 prev_module_column = module_column;
1213 if (c == '\n')
1215 module_line++;
1216 module_column = 0;
1219 module_column++;
1220 return c;
1223 /* Unget a character while remembering the line and column. Works for
1224 a single character only. */
1226 static void
1227 module_unget_char (void)
1229 module_line = prev_module_line;
1230 module_column = prev_module_column;
1231 module_pos--;
1234 /* Parse a string constant. The delimiter is guaranteed to be a
1235 single quote. */
1237 static void
1238 parse_string (void)
1240 int c;
1241 size_t cursz = 30;
1242 size_t len = 0;
1244 atom_string = XNEWVEC (char, cursz);
1246 for ( ; ; )
1248 c = module_char ();
1250 if (c == '\'')
1252 int c2 = module_char ();
1253 if (c2 != '\'')
1255 module_unget_char ();
1256 break;
1260 if (len >= cursz)
1262 cursz *= 2;
1263 atom_string = XRESIZEVEC (char, atom_string, cursz);
1265 atom_string[len] = c;
1266 len++;
1269 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1270 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1274 /* Parse a small integer. */
1276 static void
1277 parse_integer (int c)
1279 atom_int = c - '0';
1281 for (;;)
1283 c = module_char ();
1284 if (!ISDIGIT (c))
1286 module_unget_char ();
1287 break;
1290 atom_int = 10 * atom_int + c - '0';
1291 if (atom_int > 99999999)
1292 bad_module ("Integer overflow");
1298 /* Parse a name. */
1300 static void
1301 parse_name (int c)
1303 char *p;
1304 int len;
1306 p = atom_name;
1308 *p++ = c;
1309 len = 1;
1311 for (;;)
1313 c = module_char ();
1314 if (!ISALNUM (c) && c != '_' && c != '-')
1316 module_unget_char ();
1317 break;
1320 *p++ = c;
1321 if (++len > GFC_MAX_SYMBOL_LEN)
1322 bad_module ("Name too long");
1325 *p = '\0';
1330 /* Read the next atom in the module's input stream. */
1332 static atom_type
1333 parse_atom (void)
1335 int c;
1339 c = module_char ();
1341 while (c == ' ' || c == '\r' || c == '\n');
1343 switch (c)
1345 case '(':
1346 return ATOM_LPAREN;
1348 case ')':
1349 return ATOM_RPAREN;
1351 case '\'':
1352 parse_string ();
1353 return ATOM_STRING;
1355 case '0':
1356 case '1':
1357 case '2':
1358 case '3':
1359 case '4':
1360 case '5':
1361 case '6':
1362 case '7':
1363 case '8':
1364 case '9':
1365 parse_integer (c);
1366 return ATOM_INTEGER;
1368 case 'a':
1369 case 'b':
1370 case 'c':
1371 case 'd':
1372 case 'e':
1373 case 'f':
1374 case 'g':
1375 case 'h':
1376 case 'i':
1377 case 'j':
1378 case 'k':
1379 case 'l':
1380 case 'm':
1381 case 'n':
1382 case 'o':
1383 case 'p':
1384 case 'q':
1385 case 'r':
1386 case 's':
1387 case 't':
1388 case 'u':
1389 case 'v':
1390 case 'w':
1391 case 'x':
1392 case 'y':
1393 case 'z':
1394 case 'A':
1395 case 'B':
1396 case 'C':
1397 case 'D':
1398 case 'E':
1399 case 'F':
1400 case 'G':
1401 case 'H':
1402 case 'I':
1403 case 'J':
1404 case 'K':
1405 case 'L':
1406 case 'M':
1407 case 'N':
1408 case 'O':
1409 case 'P':
1410 case 'Q':
1411 case 'R':
1412 case 'S':
1413 case 'T':
1414 case 'U':
1415 case 'V':
1416 case 'W':
1417 case 'X':
1418 case 'Y':
1419 case 'Z':
1420 parse_name (c);
1421 return ATOM_NAME;
1423 default:
1424 bad_module ("Bad name");
1427 /* Not reached. */
1431 /* Peek at the next atom on the input. */
1433 static atom_type
1434 peek_atom (void)
1436 int c;
1440 c = module_char ();
1442 while (c == ' ' || c == '\r' || c == '\n');
1444 switch (c)
1446 case '(':
1447 module_unget_char ();
1448 return ATOM_LPAREN;
1450 case ')':
1451 module_unget_char ();
1452 return ATOM_RPAREN;
1454 case '\'':
1455 module_unget_char ();
1456 return ATOM_STRING;
1458 case '0':
1459 case '1':
1460 case '2':
1461 case '3':
1462 case '4':
1463 case '5':
1464 case '6':
1465 case '7':
1466 case '8':
1467 case '9':
1468 module_unget_char ();
1469 return ATOM_INTEGER;
1471 case 'a':
1472 case 'b':
1473 case 'c':
1474 case 'd':
1475 case 'e':
1476 case 'f':
1477 case 'g':
1478 case 'h':
1479 case 'i':
1480 case 'j':
1481 case 'k':
1482 case 'l':
1483 case 'm':
1484 case 'n':
1485 case 'o':
1486 case 'p':
1487 case 'q':
1488 case 'r':
1489 case 's':
1490 case 't':
1491 case 'u':
1492 case 'v':
1493 case 'w':
1494 case 'x':
1495 case 'y':
1496 case 'z':
1497 case 'A':
1498 case 'B':
1499 case 'C':
1500 case 'D':
1501 case 'E':
1502 case 'F':
1503 case 'G':
1504 case 'H':
1505 case 'I':
1506 case 'J':
1507 case 'K':
1508 case 'L':
1509 case 'M':
1510 case 'N':
1511 case 'O':
1512 case 'P':
1513 case 'Q':
1514 case 'R':
1515 case 'S':
1516 case 'T':
1517 case 'U':
1518 case 'V':
1519 case 'W':
1520 case 'X':
1521 case 'Y':
1522 case 'Z':
1523 module_unget_char ();
1524 return ATOM_NAME;
1526 default:
1527 bad_module ("Bad name");
1532 /* Read the next atom from the input, requiring that it be a
1533 particular kind. */
1535 static void
1536 require_atom (atom_type type)
1538 atom_type t;
1539 const char *p;
1540 int column, line;
1542 column = module_column;
1543 line = module_line;
1545 t = parse_atom ();
1546 if (t != type)
1548 switch (type)
1550 case ATOM_NAME:
1551 p = _("Expected name");
1552 break;
1553 case ATOM_LPAREN:
1554 p = _("Expected left parenthesis");
1555 break;
1556 case ATOM_RPAREN:
1557 p = _("Expected right parenthesis");
1558 break;
1559 case ATOM_INTEGER:
1560 p = _("Expected integer");
1561 break;
1562 case ATOM_STRING:
1563 p = _("Expected string");
1564 break;
1565 default:
1566 gfc_internal_error ("require_atom(): bad atom type required");
1569 module_column = column;
1570 module_line = line;
1571 bad_module (p);
1576 /* Given a pointer to an mstring array, require that the current input
1577 be one of the strings in the array. We return the enum value. */
1579 static int
1580 find_enum (const mstring *m)
1582 int i;
1584 i = gfc_string2code (m, atom_name);
1585 if (i >= 0)
1586 return i;
1588 bad_module ("find_enum(): Enum not found");
1590 /* Not reached. */
1594 /* Read a string. The caller is responsible for freeing. */
1596 static char*
1597 read_string (void)
1599 char* p;
1600 require_atom (ATOM_STRING);
1601 p = atom_string;
1602 atom_string = NULL;
1603 return p;
1607 /**************** Module output subroutines ***************************/
1609 /* Output a character to a module file. */
1611 static void
1612 write_char (char out)
1614 if (gzputc (module_fp, out) == EOF)
1615 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1617 if (out != '\n')
1618 module_column++;
1619 else
1621 module_column = 1;
1622 module_line++;
1627 /* Write an atom to a module. The line wrapping isn't perfect, but it
1628 should work most of the time. This isn't that big of a deal, since
1629 the file really isn't meant to be read by people anyway. */
1631 static void
1632 write_atom (atom_type atom, const void *v)
1634 char buffer[20];
1636 /* Workaround -Wmaybe-uninitialized false positive during
1637 profiledbootstrap by initializing them. */
1638 int i = 0, len;
1639 const char *p;
1641 switch (atom)
1643 case ATOM_STRING:
1644 case ATOM_NAME:
1645 p = (const char *) v;
1646 break;
1648 case ATOM_LPAREN:
1649 p = "(";
1650 break;
1652 case ATOM_RPAREN:
1653 p = ")";
1654 break;
1656 case ATOM_INTEGER:
1657 i = *((const int *) v);
1658 if (i < 0)
1659 gfc_internal_error ("write_atom(): Writing negative integer");
1661 sprintf (buffer, "%d", i);
1662 p = buffer;
1663 break;
1665 default:
1666 gfc_internal_error ("write_atom(): Trying to write dab atom");
1670 if(p == NULL || *p == '\0')
1671 len = 0;
1672 else
1673 len = strlen (p);
1675 if (atom != ATOM_RPAREN)
1677 if (module_column + len > 72)
1678 write_char ('\n');
1679 else
1682 if (last_atom != ATOM_LPAREN && module_column != 1)
1683 write_char (' ');
1687 if (atom == ATOM_STRING)
1688 write_char ('\'');
1690 while (p != NULL && *p)
1692 if (atom == ATOM_STRING && *p == '\'')
1693 write_char ('\'');
1694 write_char (*p++);
1697 if (atom == ATOM_STRING)
1698 write_char ('\'');
1700 last_atom = atom;
1705 /***************** Mid-level I/O subroutines *****************/
1707 /* These subroutines let their caller read or write atoms without
1708 caring about which of the two is actually happening. This lets a
1709 subroutine concentrate on the actual format of the data being
1710 written. */
1712 static void mio_expr (gfc_expr **);
1713 pointer_info *mio_symbol_ref (gfc_symbol **);
1714 pointer_info *mio_interface_rest (gfc_interface **);
1715 static void mio_symtree_ref (gfc_symtree **);
1717 /* Read or write an enumerated value. On writing, we return the input
1718 value for the convenience of callers. We avoid using an integer
1719 pointer because enums are sometimes inside bitfields. */
1721 static int
1722 mio_name (int t, const mstring *m)
1724 if (iomode == IO_OUTPUT)
1725 write_atom (ATOM_NAME, gfc_code2string (m, t));
1726 else
1728 require_atom (ATOM_NAME);
1729 t = find_enum (m);
1732 return t;
1735 /* Specialization of mio_name. */
1737 #define DECL_MIO_NAME(TYPE) \
1738 static inline TYPE \
1739 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1741 return (TYPE) mio_name ((int) t, m); \
1743 #define MIO_NAME(TYPE) mio_name_##TYPE
1745 static void
1746 mio_lparen (void)
1748 if (iomode == IO_OUTPUT)
1749 write_atom (ATOM_LPAREN, NULL);
1750 else
1751 require_atom (ATOM_LPAREN);
1755 static void
1756 mio_rparen (void)
1758 if (iomode == IO_OUTPUT)
1759 write_atom (ATOM_RPAREN, NULL);
1760 else
1761 require_atom (ATOM_RPAREN);
1765 static void
1766 mio_integer (int *ip)
1768 if (iomode == IO_OUTPUT)
1769 write_atom (ATOM_INTEGER, ip);
1770 else
1772 require_atom (ATOM_INTEGER);
1773 *ip = atom_int;
1778 /* Read or write a gfc_intrinsic_op value. */
1780 static void
1781 mio_intrinsic_op (gfc_intrinsic_op* op)
1783 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1784 if (iomode == IO_OUTPUT)
1786 int converted = (int) *op;
1787 write_atom (ATOM_INTEGER, &converted);
1789 else
1791 require_atom (ATOM_INTEGER);
1792 *op = (gfc_intrinsic_op) atom_int;
1797 /* Read or write a character pointer that points to a string on the heap. */
1799 static const char *
1800 mio_allocated_string (const char *s)
1802 if (iomode == IO_OUTPUT)
1804 write_atom (ATOM_STRING, s);
1805 return s;
1807 else
1809 require_atom (ATOM_STRING);
1810 return atom_string;
1815 /* Functions for quoting and unquoting strings. */
1817 static char *
1818 quote_string (const gfc_char_t *s, const size_t slength)
1820 const gfc_char_t *p;
1821 char *res, *q;
1822 size_t len = 0, i;
1824 /* Calculate the length we'll need: a backslash takes two ("\\"),
1825 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1826 for (p = s, i = 0; i < slength; p++, i++)
1828 if (*p == '\\')
1829 len += 2;
1830 else if (!gfc_wide_is_printable (*p))
1831 len += 10;
1832 else
1833 len++;
1836 q = res = XCNEWVEC (char, len + 1);
1837 for (p = s, i = 0; i < slength; p++, i++)
1839 if (*p == '\\')
1840 *q++ = '\\', *q++ = '\\';
1841 else if (!gfc_wide_is_printable (*p))
1843 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1844 (unsigned HOST_WIDE_INT) *p);
1845 q += 10;
1847 else
1848 *q++ = (unsigned char) *p;
1851 res[len] = '\0';
1852 return res;
1855 static gfc_char_t *
1856 unquote_string (const char *s)
1858 size_t len, i;
1859 const char *p;
1860 gfc_char_t *res;
1862 for (p = s, len = 0; *p; p++, len++)
1864 if (*p != '\\')
1865 continue;
1867 if (p[1] == '\\')
1868 p++;
1869 else if (p[1] == 'U')
1870 p += 9; /* That is a "\U????????". */
1871 else
1872 gfc_internal_error ("unquote_string(): got bad string");
1875 res = gfc_get_wide_string (len + 1);
1876 for (i = 0, p = s; i < len; i++, p++)
1878 gcc_assert (*p);
1880 if (*p != '\\')
1881 res[i] = (unsigned char) *p;
1882 else if (p[1] == '\\')
1884 res[i] = (unsigned char) '\\';
1885 p++;
1887 else
1889 /* We read the 8-digits hexadecimal constant that follows. */
1890 int j;
1891 unsigned n;
1892 gfc_char_t c = 0;
1894 gcc_assert (p[1] == 'U');
1895 for (j = 0; j < 8; j++)
1897 c = c << 4;
1898 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1899 c += n;
1902 res[i] = c;
1903 p += 9;
1907 res[len] = '\0';
1908 return res;
1912 /* Read or write a character pointer that points to a wide string on the
1913 heap, performing quoting/unquoting of nonprintable characters using the
1914 form \U???????? (where each ? is a hexadecimal digit).
1915 Length is the length of the string, only known and used in output mode. */
1917 static const gfc_char_t *
1918 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1920 if (iomode == IO_OUTPUT)
1922 char *quoted = quote_string (s, length);
1923 write_atom (ATOM_STRING, quoted);
1924 free (quoted);
1925 return s;
1927 else
1929 gfc_char_t *unquoted;
1931 require_atom (ATOM_STRING);
1932 unquoted = unquote_string (atom_string);
1933 free (atom_string);
1934 return unquoted;
1939 /* Read or write a string that is in static memory. */
1941 static void
1942 mio_pool_string (const char **stringp)
1944 /* TODO: one could write the string only once, and refer to it via a
1945 fixup pointer. */
1947 /* As a special case we have to deal with a NULL string. This
1948 happens for the 'module' member of 'gfc_symbol's that are not in a
1949 module. We read / write these as the empty string. */
1950 if (iomode == IO_OUTPUT)
1952 const char *p = *stringp == NULL ? "" : *stringp;
1953 write_atom (ATOM_STRING, p);
1955 else
1957 require_atom (ATOM_STRING);
1958 *stringp = (atom_string[0] == '\0'
1959 ? NULL : gfc_get_string ("%s", atom_string));
1960 free (atom_string);
1965 /* Read or write a string that is inside of some already-allocated
1966 structure. */
1968 static void
1969 mio_internal_string (char *string)
1971 if (iomode == IO_OUTPUT)
1972 write_atom (ATOM_STRING, string);
1973 else
1975 require_atom (ATOM_STRING);
1976 strcpy (string, atom_string);
1977 free (atom_string);
1982 enum ab_attribute
1983 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1984 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1985 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1986 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1987 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1988 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1989 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
1990 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1991 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1992 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1993 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
1994 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
1995 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
1996 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK,
1997 AB_OMP_DECLARE_TARGET_LINK
2000 static const mstring attr_bits[] =
2002 minit ("ALLOCATABLE", AB_ALLOCATABLE),
2003 minit ("ARTIFICIAL", AB_ARTIFICIAL),
2004 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
2005 minit ("DIMENSION", AB_DIMENSION),
2006 minit ("CODIMENSION", AB_CODIMENSION),
2007 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2008 minit ("EXTERNAL", AB_EXTERNAL),
2009 minit ("INTRINSIC", AB_INTRINSIC),
2010 minit ("OPTIONAL", AB_OPTIONAL),
2011 minit ("POINTER", AB_POINTER),
2012 minit ("VOLATILE", AB_VOLATILE),
2013 minit ("TARGET", AB_TARGET),
2014 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2015 minit ("DUMMY", AB_DUMMY),
2016 minit ("RESULT", AB_RESULT),
2017 minit ("DATA", AB_DATA),
2018 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2019 minit ("IN_COMMON", AB_IN_COMMON),
2020 minit ("FUNCTION", AB_FUNCTION),
2021 minit ("SUBROUTINE", AB_SUBROUTINE),
2022 minit ("SEQUENCE", AB_SEQUENCE),
2023 minit ("ELEMENTAL", AB_ELEMENTAL),
2024 minit ("PURE", AB_PURE),
2025 minit ("RECURSIVE", AB_RECURSIVE),
2026 minit ("GENERIC", AB_GENERIC),
2027 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2028 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2029 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2030 minit ("IS_BIND_C", AB_IS_BIND_C),
2031 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2032 minit ("IS_ISO_C", AB_IS_ISO_C),
2033 minit ("VALUE", AB_VALUE),
2034 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2035 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2036 minit ("LOCK_COMP", AB_LOCK_COMP),
2037 minit ("EVENT_COMP", AB_EVENT_COMP),
2038 minit ("POINTER_COMP", AB_POINTER_COMP),
2039 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2040 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2041 minit ("ZERO_COMP", AB_ZERO_COMP),
2042 minit ("PROTECTED", AB_PROTECTED),
2043 minit ("ABSTRACT", AB_ABSTRACT),
2044 minit ("IS_CLASS", AB_IS_CLASS),
2045 minit ("PROCEDURE", AB_PROCEDURE),
2046 minit ("PROC_POINTER", AB_PROC_POINTER),
2047 minit ("VTYPE", AB_VTYPE),
2048 minit ("VTAB", AB_VTAB),
2049 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2050 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2051 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2052 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2053 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2054 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2055 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2056 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2057 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2058 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2059 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2060 minit ("OMP_DECLARE_TARGET_LINK", AB_OMP_DECLARE_TARGET_LINK),
2061 minit (NULL, -1)
2064 /* For binding attributes. */
2065 static const mstring binding_passing[] =
2067 minit ("PASS", 0),
2068 minit ("NOPASS", 1),
2069 minit (NULL, -1)
2071 static const mstring binding_overriding[] =
2073 minit ("OVERRIDABLE", 0),
2074 minit ("NON_OVERRIDABLE", 1),
2075 minit ("DEFERRED", 2),
2076 minit (NULL, -1)
2078 static const mstring binding_generic[] =
2080 minit ("SPECIFIC", 0),
2081 minit ("GENERIC", 1),
2082 minit (NULL, -1)
2084 static const mstring binding_ppc[] =
2086 minit ("NO_PPC", 0),
2087 minit ("PPC", 1),
2088 minit (NULL, -1)
2091 /* Specialization of mio_name. */
2092 DECL_MIO_NAME (ab_attribute)
2093 DECL_MIO_NAME (ar_type)
2094 DECL_MIO_NAME (array_type)
2095 DECL_MIO_NAME (bt)
2096 DECL_MIO_NAME (expr_t)
2097 DECL_MIO_NAME (gfc_access)
2098 DECL_MIO_NAME (gfc_intrinsic_op)
2099 DECL_MIO_NAME (ifsrc)
2100 DECL_MIO_NAME (save_state)
2101 DECL_MIO_NAME (procedure_type)
2102 DECL_MIO_NAME (ref_type)
2103 DECL_MIO_NAME (sym_flavor)
2104 DECL_MIO_NAME (sym_intent)
2105 #undef DECL_MIO_NAME
2107 /* Symbol attributes are stored in list with the first three elements
2108 being the enumerated fields, while the remaining elements (if any)
2109 indicate the individual attribute bits. The access field is not
2110 saved-- it controls what symbols are exported when a module is
2111 written. */
2113 static void
2114 mio_symbol_attribute (symbol_attribute *attr)
2116 atom_type t;
2117 unsigned ext_attr,extension_level;
2119 mio_lparen ();
2121 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2122 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2123 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2124 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2125 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2127 ext_attr = attr->ext_attr;
2128 mio_integer ((int *) &ext_attr);
2129 attr->ext_attr = ext_attr;
2131 extension_level = attr->extension;
2132 mio_integer ((int *) &extension_level);
2133 attr->extension = extension_level;
2135 if (iomode == IO_OUTPUT)
2137 if (attr->allocatable)
2138 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2139 if (attr->artificial)
2140 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2141 if (attr->asynchronous)
2142 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2143 if (attr->dimension)
2144 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2145 if (attr->codimension)
2146 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2147 if (attr->contiguous)
2148 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2149 if (attr->external)
2150 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2151 if (attr->intrinsic)
2152 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2153 if (attr->optional)
2154 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2155 if (attr->pointer)
2156 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2157 if (attr->class_pointer)
2158 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2159 if (attr->is_protected)
2160 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2161 if (attr->value)
2162 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2163 if (attr->volatile_)
2164 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2165 if (attr->target)
2166 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2167 if (attr->threadprivate)
2168 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2169 if (attr->dummy)
2170 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2171 if (attr->result)
2172 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2173 /* We deliberately don't preserve the "entry" flag. */
2175 if (attr->data)
2176 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2177 if (attr->in_namelist)
2178 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2179 if (attr->in_common)
2180 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2182 if (attr->function)
2183 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2184 if (attr->subroutine)
2185 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2186 if (attr->generic)
2187 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2188 if (attr->abstract)
2189 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2191 if (attr->sequence)
2192 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2193 if (attr->elemental)
2194 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2195 if (attr->pure)
2196 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2197 if (attr->implicit_pure)
2198 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2199 if (attr->unlimited_polymorphic)
2200 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2201 if (attr->recursive)
2202 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2203 if (attr->always_explicit)
2204 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2205 if (attr->cray_pointer)
2206 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2207 if (attr->cray_pointee)
2208 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2209 if (attr->is_bind_c)
2210 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2211 if (attr->is_c_interop)
2212 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2213 if (attr->is_iso_c)
2214 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2215 if (attr->alloc_comp)
2216 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2217 if (attr->pointer_comp)
2218 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2219 if (attr->proc_pointer_comp)
2220 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2221 if (attr->private_comp)
2222 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2223 if (attr->coarray_comp)
2224 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2225 if (attr->lock_comp)
2226 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2227 if (attr->event_comp)
2228 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2229 if (attr->zero_comp)
2230 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2231 if (attr->is_class)
2232 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2233 if (attr->procedure)
2234 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2235 if (attr->proc_pointer)
2236 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2237 if (attr->vtype)
2238 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2239 if (attr->vtab)
2240 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2241 if (attr->omp_declare_target)
2242 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2243 if (attr->array_outer_dependency)
2244 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2245 if (attr->module_procedure)
2247 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2248 no_module_procedures = false;
2250 if (attr->oacc_declare_create)
2251 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2252 if (attr->oacc_declare_copyin)
2253 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2254 if (attr->oacc_declare_deviceptr)
2255 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2256 if (attr->oacc_declare_device_resident)
2257 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2258 if (attr->oacc_declare_link)
2259 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2260 if (attr->omp_declare_target_link)
2261 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET_LINK, attr_bits);
2263 mio_rparen ();
2266 else
2268 for (;;)
2270 t = parse_atom ();
2271 if (t == ATOM_RPAREN)
2272 break;
2273 if (t != ATOM_NAME)
2274 bad_module ("Expected attribute bit name");
2276 switch ((ab_attribute) find_enum (attr_bits))
2278 case AB_ALLOCATABLE:
2279 attr->allocatable = 1;
2280 break;
2281 case AB_ARTIFICIAL:
2282 attr->artificial = 1;
2283 break;
2284 case AB_ASYNCHRONOUS:
2285 attr->asynchronous = 1;
2286 break;
2287 case AB_DIMENSION:
2288 attr->dimension = 1;
2289 break;
2290 case AB_CODIMENSION:
2291 attr->codimension = 1;
2292 break;
2293 case AB_CONTIGUOUS:
2294 attr->contiguous = 1;
2295 break;
2296 case AB_EXTERNAL:
2297 attr->external = 1;
2298 break;
2299 case AB_INTRINSIC:
2300 attr->intrinsic = 1;
2301 break;
2302 case AB_OPTIONAL:
2303 attr->optional = 1;
2304 break;
2305 case AB_POINTER:
2306 attr->pointer = 1;
2307 break;
2308 case AB_CLASS_POINTER:
2309 attr->class_pointer = 1;
2310 break;
2311 case AB_PROTECTED:
2312 attr->is_protected = 1;
2313 break;
2314 case AB_VALUE:
2315 attr->value = 1;
2316 break;
2317 case AB_VOLATILE:
2318 attr->volatile_ = 1;
2319 break;
2320 case AB_TARGET:
2321 attr->target = 1;
2322 break;
2323 case AB_THREADPRIVATE:
2324 attr->threadprivate = 1;
2325 break;
2326 case AB_DUMMY:
2327 attr->dummy = 1;
2328 break;
2329 case AB_RESULT:
2330 attr->result = 1;
2331 break;
2332 case AB_DATA:
2333 attr->data = 1;
2334 break;
2335 case AB_IN_NAMELIST:
2336 attr->in_namelist = 1;
2337 break;
2338 case AB_IN_COMMON:
2339 attr->in_common = 1;
2340 break;
2341 case AB_FUNCTION:
2342 attr->function = 1;
2343 break;
2344 case AB_SUBROUTINE:
2345 attr->subroutine = 1;
2346 break;
2347 case AB_GENERIC:
2348 attr->generic = 1;
2349 break;
2350 case AB_ABSTRACT:
2351 attr->abstract = 1;
2352 break;
2353 case AB_SEQUENCE:
2354 attr->sequence = 1;
2355 break;
2356 case AB_ELEMENTAL:
2357 attr->elemental = 1;
2358 break;
2359 case AB_PURE:
2360 attr->pure = 1;
2361 break;
2362 case AB_IMPLICIT_PURE:
2363 attr->implicit_pure = 1;
2364 break;
2365 case AB_UNLIMITED_POLY:
2366 attr->unlimited_polymorphic = 1;
2367 break;
2368 case AB_RECURSIVE:
2369 attr->recursive = 1;
2370 break;
2371 case AB_ALWAYS_EXPLICIT:
2372 attr->always_explicit = 1;
2373 break;
2374 case AB_CRAY_POINTER:
2375 attr->cray_pointer = 1;
2376 break;
2377 case AB_CRAY_POINTEE:
2378 attr->cray_pointee = 1;
2379 break;
2380 case AB_IS_BIND_C:
2381 attr->is_bind_c = 1;
2382 break;
2383 case AB_IS_C_INTEROP:
2384 attr->is_c_interop = 1;
2385 break;
2386 case AB_IS_ISO_C:
2387 attr->is_iso_c = 1;
2388 break;
2389 case AB_ALLOC_COMP:
2390 attr->alloc_comp = 1;
2391 break;
2392 case AB_COARRAY_COMP:
2393 attr->coarray_comp = 1;
2394 break;
2395 case AB_LOCK_COMP:
2396 attr->lock_comp = 1;
2397 break;
2398 case AB_EVENT_COMP:
2399 attr->event_comp = 1;
2400 break;
2401 case AB_POINTER_COMP:
2402 attr->pointer_comp = 1;
2403 break;
2404 case AB_PROC_POINTER_COMP:
2405 attr->proc_pointer_comp = 1;
2406 break;
2407 case AB_PRIVATE_COMP:
2408 attr->private_comp = 1;
2409 break;
2410 case AB_ZERO_COMP:
2411 attr->zero_comp = 1;
2412 break;
2413 case AB_IS_CLASS:
2414 attr->is_class = 1;
2415 break;
2416 case AB_PROCEDURE:
2417 attr->procedure = 1;
2418 break;
2419 case AB_PROC_POINTER:
2420 attr->proc_pointer = 1;
2421 break;
2422 case AB_VTYPE:
2423 attr->vtype = 1;
2424 break;
2425 case AB_VTAB:
2426 attr->vtab = 1;
2427 break;
2428 case AB_OMP_DECLARE_TARGET:
2429 attr->omp_declare_target = 1;
2430 break;
2431 case AB_OMP_DECLARE_TARGET_LINK:
2432 attr->omp_declare_target_link = 1;
2433 break;
2434 case AB_ARRAY_OUTER_DEPENDENCY:
2435 attr->array_outer_dependency =1;
2436 break;
2437 case AB_MODULE_PROCEDURE:
2438 attr->module_procedure =1;
2439 break;
2440 case AB_OACC_DECLARE_CREATE:
2441 attr->oacc_declare_create = 1;
2442 break;
2443 case AB_OACC_DECLARE_COPYIN:
2444 attr->oacc_declare_copyin = 1;
2445 break;
2446 case AB_OACC_DECLARE_DEVICEPTR:
2447 attr->oacc_declare_deviceptr = 1;
2448 break;
2449 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2450 attr->oacc_declare_device_resident = 1;
2451 break;
2452 case AB_OACC_DECLARE_LINK:
2453 attr->oacc_declare_link = 1;
2454 break;
2461 static const mstring bt_types[] = {
2462 minit ("INTEGER", BT_INTEGER),
2463 minit ("REAL", BT_REAL),
2464 minit ("COMPLEX", BT_COMPLEX),
2465 minit ("LOGICAL", BT_LOGICAL),
2466 minit ("CHARACTER", BT_CHARACTER),
2467 minit ("UNION", BT_UNION),
2468 minit ("DERIVED", BT_DERIVED),
2469 minit ("CLASS", BT_CLASS),
2470 minit ("PROCEDURE", BT_PROCEDURE),
2471 minit ("UNKNOWN", BT_UNKNOWN),
2472 minit ("VOID", BT_VOID),
2473 minit ("ASSUMED", BT_ASSUMED),
2474 minit (NULL, -1)
2478 static void
2479 mio_charlen (gfc_charlen **clp)
2481 gfc_charlen *cl;
2483 mio_lparen ();
2485 if (iomode == IO_OUTPUT)
2487 cl = *clp;
2488 if (cl != NULL)
2489 mio_expr (&cl->length);
2491 else
2493 if (peek_atom () != ATOM_RPAREN)
2495 cl = gfc_new_charlen (gfc_current_ns, NULL);
2496 mio_expr (&cl->length);
2497 *clp = cl;
2501 mio_rparen ();
2505 /* See if a name is a generated name. */
2507 static int
2508 check_unique_name (const char *name)
2510 return *name == '@';
2514 static void
2515 mio_typespec (gfc_typespec *ts)
2517 mio_lparen ();
2519 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2521 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2522 mio_integer (&ts->kind);
2523 else
2524 mio_symbol_ref (&ts->u.derived);
2526 mio_symbol_ref (&ts->interface);
2528 /* Add info for C interop and is_iso_c. */
2529 mio_integer (&ts->is_c_interop);
2530 mio_integer (&ts->is_iso_c);
2532 /* If the typespec is for an identifier either from iso_c_binding, or
2533 a constant that was initialized to an identifier from it, use the
2534 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2535 if (ts->is_iso_c)
2536 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2537 else
2538 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2540 if (ts->type != BT_CHARACTER)
2542 /* ts->u.cl is only valid for BT_CHARACTER. */
2543 mio_lparen ();
2544 mio_rparen ();
2546 else
2547 mio_charlen (&ts->u.cl);
2549 /* So as not to disturb the existing API, use an ATOM_NAME to
2550 transmit deferred characteristic for characters (F2003). */
2551 if (iomode == IO_OUTPUT)
2553 if (ts->type == BT_CHARACTER && ts->deferred)
2554 write_atom (ATOM_NAME, "DEFERRED_CL");
2556 else if (peek_atom () != ATOM_RPAREN)
2558 if (parse_atom () != ATOM_NAME)
2559 bad_module ("Expected string");
2560 ts->deferred = 1;
2563 mio_rparen ();
2567 static const mstring array_spec_types[] = {
2568 minit ("EXPLICIT", AS_EXPLICIT),
2569 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2570 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2571 minit ("DEFERRED", AS_DEFERRED),
2572 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2573 minit (NULL, -1)
2577 static void
2578 mio_array_spec (gfc_array_spec **asp)
2580 gfc_array_spec *as;
2581 int i;
2583 mio_lparen ();
2585 if (iomode == IO_OUTPUT)
2587 int rank;
2589 if (*asp == NULL)
2590 goto done;
2591 as = *asp;
2593 /* mio_integer expects nonnegative values. */
2594 rank = as->rank > 0 ? as->rank : 0;
2595 mio_integer (&rank);
2597 else
2599 if (peek_atom () == ATOM_RPAREN)
2601 *asp = NULL;
2602 goto done;
2605 *asp = as = gfc_get_array_spec ();
2606 mio_integer (&as->rank);
2609 mio_integer (&as->corank);
2610 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2612 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2613 as->rank = -1;
2614 if (iomode == IO_INPUT && as->corank)
2615 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2617 if (as->rank + as->corank > 0)
2618 for (i = 0; i < as->rank + as->corank; i++)
2620 mio_expr (&as->lower[i]);
2621 mio_expr (&as->upper[i]);
2624 done:
2625 mio_rparen ();
2629 /* Given a pointer to an array reference structure (which lives in a
2630 gfc_ref structure), find the corresponding array specification
2631 structure. Storing the pointer in the ref structure doesn't quite
2632 work when loading from a module. Generating code for an array
2633 reference also needs more information than just the array spec. */
2635 static const mstring array_ref_types[] = {
2636 minit ("FULL", AR_FULL),
2637 minit ("ELEMENT", AR_ELEMENT),
2638 minit ("SECTION", AR_SECTION),
2639 minit (NULL, -1)
2643 static void
2644 mio_array_ref (gfc_array_ref *ar)
2646 int i;
2648 mio_lparen ();
2649 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2650 mio_integer (&ar->dimen);
2652 switch (ar->type)
2654 case AR_FULL:
2655 break;
2657 case AR_ELEMENT:
2658 for (i = 0; i < ar->dimen; i++)
2659 mio_expr (&ar->start[i]);
2661 break;
2663 case AR_SECTION:
2664 for (i = 0; i < ar->dimen; i++)
2666 mio_expr (&ar->start[i]);
2667 mio_expr (&ar->end[i]);
2668 mio_expr (&ar->stride[i]);
2671 break;
2673 case AR_UNKNOWN:
2674 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2677 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2678 we can't call mio_integer directly. Instead loop over each element
2679 and cast it to/from an integer. */
2680 if (iomode == IO_OUTPUT)
2682 for (i = 0; i < ar->dimen; i++)
2684 int tmp = (int)ar->dimen_type[i];
2685 write_atom (ATOM_INTEGER, &tmp);
2688 else
2690 for (i = 0; i < ar->dimen; i++)
2692 require_atom (ATOM_INTEGER);
2693 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2697 if (iomode == IO_INPUT)
2699 ar->where = gfc_current_locus;
2701 for (i = 0; i < ar->dimen; i++)
2702 ar->c_where[i] = gfc_current_locus;
2705 mio_rparen ();
2709 /* Saves or restores a pointer. The pointer is converted back and
2710 forth from an integer. We return the pointer_info pointer so that
2711 the caller can take additional action based on the pointer type. */
2713 static pointer_info *
2714 mio_pointer_ref (void *gp)
2716 pointer_info *p;
2718 if (iomode == IO_OUTPUT)
2720 p = get_pointer (*((char **) gp));
2721 write_atom (ATOM_INTEGER, &p->integer);
2723 else
2725 require_atom (ATOM_INTEGER);
2726 p = add_fixup (atom_int, gp);
2729 return p;
2733 /* Save and load references to components that occur within
2734 expressions. We have to describe these references by a number and
2735 by name. The number is necessary for forward references during
2736 reading, and the name is necessary if the symbol already exists in
2737 the namespace and is not loaded again. */
2739 static void
2740 mio_component_ref (gfc_component **cp)
2742 pointer_info *p;
2744 p = mio_pointer_ref (cp);
2745 if (p->type == P_UNKNOWN)
2746 p->type = P_COMPONENT;
2750 static void mio_namespace_ref (gfc_namespace **nsp);
2751 static void mio_formal_arglist (gfc_formal_arglist **formal);
2752 static void mio_typebound_proc (gfc_typebound_proc** proc);
2754 static void
2755 mio_component (gfc_component *c, int vtype)
2757 pointer_info *p;
2758 int n;
2760 mio_lparen ();
2762 if (iomode == IO_OUTPUT)
2764 p = get_pointer (c);
2765 mio_integer (&p->integer);
2767 else
2769 mio_integer (&n);
2770 p = get_integer (n);
2771 associate_integer_pointer (p, c);
2774 if (p->type == P_UNKNOWN)
2775 p->type = P_COMPONENT;
2777 mio_pool_string (&c->name);
2778 mio_typespec (&c->ts);
2779 mio_array_spec (&c->as);
2781 mio_symbol_attribute (&c->attr);
2782 if (c->ts.type == BT_CLASS)
2783 c->attr.class_ok = 1;
2784 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2786 if (!vtype || strcmp (c->name, "_final") == 0
2787 || strcmp (c->name, "_hash") == 0)
2788 mio_expr (&c->initializer);
2790 if (c->attr.proc_pointer)
2791 mio_typebound_proc (&c->tb);
2793 mio_rparen ();
2797 static void
2798 mio_component_list (gfc_component **cp, int vtype)
2800 gfc_component *c, *tail;
2802 mio_lparen ();
2804 if (iomode == IO_OUTPUT)
2806 for (c = *cp; c; c = c->next)
2807 mio_component (c, vtype);
2809 else
2811 *cp = NULL;
2812 tail = NULL;
2814 for (;;)
2816 if (peek_atom () == ATOM_RPAREN)
2817 break;
2819 c = gfc_get_component ();
2820 mio_component (c, vtype);
2822 if (tail == NULL)
2823 *cp = c;
2824 else
2825 tail->next = c;
2827 tail = c;
2831 mio_rparen ();
2835 static void
2836 mio_actual_arg (gfc_actual_arglist *a)
2838 mio_lparen ();
2839 mio_pool_string (&a->name);
2840 mio_expr (&a->expr);
2841 mio_rparen ();
2845 static void
2846 mio_actual_arglist (gfc_actual_arglist **ap)
2848 gfc_actual_arglist *a, *tail;
2850 mio_lparen ();
2852 if (iomode == IO_OUTPUT)
2854 for (a = *ap; a; a = a->next)
2855 mio_actual_arg (a);
2858 else
2860 tail = NULL;
2862 for (;;)
2864 if (peek_atom () != ATOM_LPAREN)
2865 break;
2867 a = gfc_get_actual_arglist ();
2869 if (tail == NULL)
2870 *ap = a;
2871 else
2872 tail->next = a;
2874 tail = a;
2875 mio_actual_arg (a);
2879 mio_rparen ();
2883 /* Read and write formal argument lists. */
2885 static void
2886 mio_formal_arglist (gfc_formal_arglist **formal)
2888 gfc_formal_arglist *f, *tail;
2890 mio_lparen ();
2892 if (iomode == IO_OUTPUT)
2894 for (f = *formal; f; f = f->next)
2895 mio_symbol_ref (&f->sym);
2897 else
2899 *formal = tail = NULL;
2901 while (peek_atom () != ATOM_RPAREN)
2903 f = gfc_get_formal_arglist ();
2904 mio_symbol_ref (&f->sym);
2906 if (*formal == NULL)
2907 *formal = f;
2908 else
2909 tail->next = f;
2911 tail = f;
2915 mio_rparen ();
2919 /* Save or restore a reference to a symbol node. */
2921 pointer_info *
2922 mio_symbol_ref (gfc_symbol **symp)
2924 pointer_info *p;
2926 p = mio_pointer_ref (symp);
2927 if (p->type == P_UNKNOWN)
2928 p->type = P_SYMBOL;
2930 if (iomode == IO_OUTPUT)
2932 if (p->u.wsym.state == UNREFERENCED)
2933 p->u.wsym.state = NEEDS_WRITE;
2935 else
2937 if (p->u.rsym.state == UNUSED)
2938 p->u.rsym.state = NEEDED;
2940 return p;
2944 /* Save or restore a reference to a symtree node. */
2946 static void
2947 mio_symtree_ref (gfc_symtree **stp)
2949 pointer_info *p;
2950 fixup_t *f;
2952 if (iomode == IO_OUTPUT)
2953 mio_symbol_ref (&(*stp)->n.sym);
2954 else
2956 require_atom (ATOM_INTEGER);
2957 p = get_integer (atom_int);
2959 /* An unused equivalence member; make a symbol and a symtree
2960 for it. */
2961 if (in_load_equiv && p->u.rsym.symtree == NULL)
2963 /* Since this is not used, it must have a unique name. */
2964 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2966 /* Make the symbol. */
2967 if (p->u.rsym.sym == NULL)
2969 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2970 gfc_current_ns);
2971 p->u.rsym.sym->module = gfc_get_string ("%s", p->u.rsym.module);
2974 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2975 p->u.rsym.symtree->n.sym->refs++;
2976 p->u.rsym.referenced = 1;
2978 /* If the symbol is PRIVATE and in COMMON, load_commons will
2979 generate a fixup symbol, which must be associated. */
2980 if (p->fixup)
2981 resolve_fixups (p->fixup, p->u.rsym.sym);
2982 p->fixup = NULL;
2985 if (p->type == P_UNKNOWN)
2986 p->type = P_SYMBOL;
2988 if (p->u.rsym.state == UNUSED)
2989 p->u.rsym.state = NEEDED;
2991 if (p->u.rsym.symtree != NULL)
2993 *stp = p->u.rsym.symtree;
2995 else
2997 f = XCNEW (fixup_t);
2999 f->next = p->u.rsym.stfixup;
3000 p->u.rsym.stfixup = f;
3002 f->pointer = (void **) stp;
3008 static void
3009 mio_iterator (gfc_iterator **ip)
3011 gfc_iterator *iter;
3013 mio_lparen ();
3015 if (iomode == IO_OUTPUT)
3017 if (*ip == NULL)
3018 goto done;
3020 else
3022 if (peek_atom () == ATOM_RPAREN)
3024 *ip = NULL;
3025 goto done;
3028 *ip = gfc_get_iterator ();
3031 iter = *ip;
3033 mio_expr (&iter->var);
3034 mio_expr (&iter->start);
3035 mio_expr (&iter->end);
3036 mio_expr (&iter->step);
3038 done:
3039 mio_rparen ();
3043 static void
3044 mio_constructor (gfc_constructor_base *cp)
3046 gfc_constructor *c;
3048 mio_lparen ();
3050 if (iomode == IO_OUTPUT)
3052 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3054 mio_lparen ();
3055 mio_expr (&c->expr);
3056 mio_iterator (&c->iterator);
3057 mio_rparen ();
3060 else
3062 while (peek_atom () != ATOM_RPAREN)
3064 c = gfc_constructor_append_expr (cp, NULL, NULL);
3066 mio_lparen ();
3067 mio_expr (&c->expr);
3068 mio_iterator (&c->iterator);
3069 mio_rparen ();
3073 mio_rparen ();
3077 static const mstring ref_types[] = {
3078 minit ("ARRAY", REF_ARRAY),
3079 minit ("COMPONENT", REF_COMPONENT),
3080 minit ("SUBSTRING", REF_SUBSTRING),
3081 minit (NULL, -1)
3085 static void
3086 mio_ref (gfc_ref **rp)
3088 gfc_ref *r;
3090 mio_lparen ();
3092 r = *rp;
3093 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3095 switch (r->type)
3097 case REF_ARRAY:
3098 mio_array_ref (&r->u.ar);
3099 break;
3101 case REF_COMPONENT:
3102 mio_symbol_ref (&r->u.c.sym);
3103 mio_component_ref (&r->u.c.component);
3104 break;
3106 case REF_SUBSTRING:
3107 mio_expr (&r->u.ss.start);
3108 mio_expr (&r->u.ss.end);
3109 mio_charlen (&r->u.ss.length);
3110 break;
3113 mio_rparen ();
3117 static void
3118 mio_ref_list (gfc_ref **rp)
3120 gfc_ref *ref, *head, *tail;
3122 mio_lparen ();
3124 if (iomode == IO_OUTPUT)
3126 for (ref = *rp; ref; ref = ref->next)
3127 mio_ref (&ref);
3129 else
3131 head = tail = NULL;
3133 while (peek_atom () != ATOM_RPAREN)
3135 if (head == NULL)
3136 head = tail = gfc_get_ref ();
3137 else
3139 tail->next = gfc_get_ref ();
3140 tail = tail->next;
3143 mio_ref (&tail);
3146 *rp = head;
3149 mio_rparen ();
3153 /* Read and write an integer value. */
3155 static void
3156 mio_gmp_integer (mpz_t *integer)
3158 char *p;
3160 if (iomode == IO_INPUT)
3162 if (parse_atom () != ATOM_STRING)
3163 bad_module ("Expected integer string");
3165 mpz_init (*integer);
3166 if (mpz_set_str (*integer, atom_string, 10))
3167 bad_module ("Error converting integer");
3169 free (atom_string);
3171 else
3173 p = mpz_get_str (NULL, 10, *integer);
3174 write_atom (ATOM_STRING, p);
3175 free (p);
3180 static void
3181 mio_gmp_real (mpfr_t *real)
3183 mp_exp_t exponent;
3184 char *p;
3186 if (iomode == IO_INPUT)
3188 if (parse_atom () != ATOM_STRING)
3189 bad_module ("Expected real string");
3191 mpfr_init (*real);
3192 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3193 free (atom_string);
3195 else
3197 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3199 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3201 write_atom (ATOM_STRING, p);
3202 free (p);
3203 return;
3206 atom_string = XCNEWVEC (char, strlen (p) + 20);
3208 sprintf (atom_string, "0.%s@%ld", p, exponent);
3210 /* Fix negative numbers. */
3211 if (atom_string[2] == '-')
3213 atom_string[0] = '-';
3214 atom_string[1] = '0';
3215 atom_string[2] = '.';
3218 write_atom (ATOM_STRING, atom_string);
3220 free (atom_string);
3221 free (p);
3226 /* Save and restore the shape of an array constructor. */
3228 static void
3229 mio_shape (mpz_t **pshape, int rank)
3231 mpz_t *shape;
3232 atom_type t;
3233 int n;
3235 /* A NULL shape is represented by (). */
3236 mio_lparen ();
3238 if (iomode == IO_OUTPUT)
3240 shape = *pshape;
3241 if (!shape)
3243 mio_rparen ();
3244 return;
3247 else
3249 t = peek_atom ();
3250 if (t == ATOM_RPAREN)
3252 *pshape = NULL;
3253 mio_rparen ();
3254 return;
3257 shape = gfc_get_shape (rank);
3258 *pshape = shape;
3261 for (n = 0; n < rank; n++)
3262 mio_gmp_integer (&shape[n]);
3264 mio_rparen ();
3268 static const mstring expr_types[] = {
3269 minit ("OP", EXPR_OP),
3270 minit ("FUNCTION", EXPR_FUNCTION),
3271 minit ("CONSTANT", EXPR_CONSTANT),
3272 minit ("VARIABLE", EXPR_VARIABLE),
3273 minit ("SUBSTRING", EXPR_SUBSTRING),
3274 minit ("STRUCTURE", EXPR_STRUCTURE),
3275 minit ("ARRAY", EXPR_ARRAY),
3276 minit ("NULL", EXPR_NULL),
3277 minit ("COMPCALL", EXPR_COMPCALL),
3278 minit (NULL, -1)
3281 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3282 generic operators, not in expressions. INTRINSIC_USER is also
3283 replaced by the correct function name by the time we see it. */
3285 static const mstring intrinsics[] =
3287 minit ("UPLUS", INTRINSIC_UPLUS),
3288 minit ("UMINUS", INTRINSIC_UMINUS),
3289 minit ("PLUS", INTRINSIC_PLUS),
3290 minit ("MINUS", INTRINSIC_MINUS),
3291 minit ("TIMES", INTRINSIC_TIMES),
3292 minit ("DIVIDE", INTRINSIC_DIVIDE),
3293 minit ("POWER", INTRINSIC_POWER),
3294 minit ("CONCAT", INTRINSIC_CONCAT),
3295 minit ("AND", INTRINSIC_AND),
3296 minit ("OR", INTRINSIC_OR),
3297 minit ("EQV", INTRINSIC_EQV),
3298 minit ("NEQV", INTRINSIC_NEQV),
3299 minit ("EQ_SIGN", INTRINSIC_EQ),
3300 minit ("EQ", INTRINSIC_EQ_OS),
3301 minit ("NE_SIGN", INTRINSIC_NE),
3302 minit ("NE", INTRINSIC_NE_OS),
3303 minit ("GT_SIGN", INTRINSIC_GT),
3304 minit ("GT", INTRINSIC_GT_OS),
3305 minit ("GE_SIGN", INTRINSIC_GE),
3306 minit ("GE", INTRINSIC_GE_OS),
3307 minit ("LT_SIGN", INTRINSIC_LT),
3308 minit ("LT", INTRINSIC_LT_OS),
3309 minit ("LE_SIGN", INTRINSIC_LE),
3310 minit ("LE", INTRINSIC_LE_OS),
3311 minit ("NOT", INTRINSIC_NOT),
3312 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3313 minit ("USER", INTRINSIC_USER),
3314 minit (NULL, -1)
3318 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3320 static void
3321 fix_mio_expr (gfc_expr *e)
3323 gfc_symtree *ns_st = NULL;
3324 const char *fname;
3326 if (iomode != IO_OUTPUT)
3327 return;
3329 if (e->symtree)
3331 /* If this is a symtree for a symbol that came from a contained module
3332 namespace, it has a unique name and we should look in the current
3333 namespace to see if the required, non-contained symbol is available
3334 yet. If so, the latter should be written. */
3335 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3337 const char *name = e->symtree->n.sym->name;
3338 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3339 name = gfc_dt_upper_string (name);
3340 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3343 /* On the other hand, if the existing symbol is the module name or the
3344 new symbol is a dummy argument, do not do the promotion. */
3345 if (ns_st && ns_st->n.sym
3346 && ns_st->n.sym->attr.flavor != FL_MODULE
3347 && !e->symtree->n.sym->attr.dummy)
3348 e->symtree = ns_st;
3350 else if (e->expr_type == EXPR_FUNCTION
3351 && (e->value.function.name || e->value.function.isym))
3353 gfc_symbol *sym;
3355 /* In some circumstances, a function used in an initialization
3356 expression, in one use associated module, can fail to be
3357 coupled to its symtree when used in a specification
3358 expression in another module. */
3359 fname = e->value.function.esym ? e->value.function.esym->name
3360 : e->value.function.isym->name;
3361 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3363 if (e->symtree)
3364 return;
3366 /* This is probably a reference to a private procedure from another
3367 module. To prevent a segfault, make a generic with no specific
3368 instances. If this module is used, without the required
3369 specific coming from somewhere, the appropriate error message
3370 is issued. */
3371 gfc_get_symbol (fname, gfc_current_ns, &sym);
3372 sym->attr.flavor = FL_PROCEDURE;
3373 sym->attr.generic = 1;
3374 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3375 gfc_commit_symbol (sym);
3380 /* Read and write expressions. The form "()" is allowed to indicate a
3381 NULL expression. */
3383 static void
3384 mio_expr (gfc_expr **ep)
3386 gfc_expr *e;
3387 atom_type t;
3388 int flag;
3390 mio_lparen ();
3392 if (iomode == IO_OUTPUT)
3394 if (*ep == NULL)
3396 mio_rparen ();
3397 return;
3400 e = *ep;
3401 MIO_NAME (expr_t) (e->expr_type, expr_types);
3403 else
3405 t = parse_atom ();
3406 if (t == ATOM_RPAREN)
3408 *ep = NULL;
3409 return;
3412 if (t != ATOM_NAME)
3413 bad_module ("Expected expression type");
3415 e = *ep = gfc_get_expr ();
3416 e->where = gfc_current_locus;
3417 e->expr_type = (expr_t) find_enum (expr_types);
3420 mio_typespec (&e->ts);
3421 mio_integer (&e->rank);
3423 fix_mio_expr (e);
3425 switch (e->expr_type)
3427 case EXPR_OP:
3428 e->value.op.op
3429 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3431 switch (e->value.op.op)
3433 case INTRINSIC_UPLUS:
3434 case INTRINSIC_UMINUS:
3435 case INTRINSIC_NOT:
3436 case INTRINSIC_PARENTHESES:
3437 mio_expr (&e->value.op.op1);
3438 break;
3440 case INTRINSIC_PLUS:
3441 case INTRINSIC_MINUS:
3442 case INTRINSIC_TIMES:
3443 case INTRINSIC_DIVIDE:
3444 case INTRINSIC_POWER:
3445 case INTRINSIC_CONCAT:
3446 case INTRINSIC_AND:
3447 case INTRINSIC_OR:
3448 case INTRINSIC_EQV:
3449 case INTRINSIC_NEQV:
3450 case INTRINSIC_EQ:
3451 case INTRINSIC_EQ_OS:
3452 case INTRINSIC_NE:
3453 case INTRINSIC_NE_OS:
3454 case INTRINSIC_GT:
3455 case INTRINSIC_GT_OS:
3456 case INTRINSIC_GE:
3457 case INTRINSIC_GE_OS:
3458 case INTRINSIC_LT:
3459 case INTRINSIC_LT_OS:
3460 case INTRINSIC_LE:
3461 case INTRINSIC_LE_OS:
3462 mio_expr (&e->value.op.op1);
3463 mio_expr (&e->value.op.op2);
3464 break;
3466 case INTRINSIC_USER:
3467 /* INTRINSIC_USER should not appear in resolved expressions,
3468 though for UDRs we need to stream unresolved ones. */
3469 if (iomode == IO_OUTPUT)
3470 write_atom (ATOM_STRING, e->value.op.uop->name);
3471 else
3473 char *name = read_string ();
3474 const char *uop_name = find_use_name (name, true);
3475 if (uop_name == NULL)
3477 size_t len = strlen (name);
3478 char *name2 = XCNEWVEC (char, len + 2);
3479 memcpy (name2, name, len);
3480 name2[len] = ' ';
3481 name2[len + 1] = '\0';
3482 free (name);
3483 uop_name = name = name2;
3485 e->value.op.uop = gfc_get_uop (uop_name);
3486 free (name);
3488 mio_expr (&e->value.op.op1);
3489 mio_expr (&e->value.op.op2);
3490 break;
3492 default:
3493 bad_module ("Bad operator");
3496 break;
3498 case EXPR_FUNCTION:
3499 mio_symtree_ref (&e->symtree);
3500 mio_actual_arglist (&e->value.function.actual);
3502 if (iomode == IO_OUTPUT)
3504 e->value.function.name
3505 = mio_allocated_string (e->value.function.name);
3506 if (e->value.function.esym)
3507 flag = 1;
3508 else if (e->ref)
3509 flag = 2;
3510 else if (e->value.function.isym == NULL)
3511 flag = 3;
3512 else
3513 flag = 0;
3514 mio_integer (&flag);
3515 switch (flag)
3517 case 1:
3518 mio_symbol_ref (&e->value.function.esym);
3519 break;
3520 case 2:
3521 mio_ref_list (&e->ref);
3522 break;
3523 case 3:
3524 break;
3525 default:
3526 write_atom (ATOM_STRING, e->value.function.isym->name);
3529 else
3531 require_atom (ATOM_STRING);
3532 if (atom_string[0] == '\0')
3533 e->value.function.name = NULL;
3534 else
3535 e->value.function.name = gfc_get_string ("%s", atom_string);
3536 free (atom_string);
3538 mio_integer (&flag);
3539 switch (flag)
3541 case 1:
3542 mio_symbol_ref (&e->value.function.esym);
3543 break;
3544 case 2:
3545 mio_ref_list (&e->ref);
3546 break;
3547 case 3:
3548 break;
3549 default:
3550 require_atom (ATOM_STRING);
3551 e->value.function.isym = gfc_find_function (atom_string);
3552 free (atom_string);
3556 break;
3558 case EXPR_VARIABLE:
3559 mio_symtree_ref (&e->symtree);
3560 mio_ref_list (&e->ref);
3561 break;
3563 case EXPR_SUBSTRING:
3564 e->value.character.string
3565 = CONST_CAST (gfc_char_t *,
3566 mio_allocated_wide_string (e->value.character.string,
3567 e->value.character.length));
3568 mio_ref_list (&e->ref);
3569 break;
3571 case EXPR_STRUCTURE:
3572 case EXPR_ARRAY:
3573 mio_constructor (&e->value.constructor);
3574 mio_shape (&e->shape, e->rank);
3575 break;
3577 case EXPR_CONSTANT:
3578 switch (e->ts.type)
3580 case BT_INTEGER:
3581 mio_gmp_integer (&e->value.integer);
3582 break;
3584 case BT_REAL:
3585 gfc_set_model_kind (e->ts.kind);
3586 mio_gmp_real (&e->value.real);
3587 break;
3589 case BT_COMPLEX:
3590 gfc_set_model_kind (e->ts.kind);
3591 mio_gmp_real (&mpc_realref (e->value.complex));
3592 mio_gmp_real (&mpc_imagref (e->value.complex));
3593 break;
3595 case BT_LOGICAL:
3596 mio_integer (&e->value.logical);
3597 break;
3599 case BT_CHARACTER:
3600 mio_integer (&e->value.character.length);
3601 e->value.character.string
3602 = CONST_CAST (gfc_char_t *,
3603 mio_allocated_wide_string (e->value.character.string,
3604 e->value.character.length));
3605 break;
3607 default:
3608 bad_module ("Bad type in constant expression");
3611 break;
3613 case EXPR_NULL:
3614 break;
3616 case EXPR_COMPCALL:
3617 case EXPR_PPC:
3618 gcc_unreachable ();
3619 break;
3622 mio_rparen ();
3626 /* Read and write namelists. */
3628 static void
3629 mio_namelist (gfc_symbol *sym)
3631 gfc_namelist *n, *m;
3632 const char *check_name;
3634 mio_lparen ();
3636 if (iomode == IO_OUTPUT)
3638 for (n = sym->namelist; n; n = n->next)
3639 mio_symbol_ref (&n->sym);
3641 else
3643 /* This departure from the standard is flagged as an error.
3644 It does, in fact, work correctly. TODO: Allow it
3645 conditionally? */
3646 if (sym->attr.flavor == FL_NAMELIST)
3648 check_name = find_use_name (sym->name, false);
3649 if (check_name && strcmp (check_name, sym->name) != 0)
3650 gfc_error ("Namelist %s cannot be renamed by USE "
3651 "association to %s", sym->name, check_name);
3654 m = NULL;
3655 while (peek_atom () != ATOM_RPAREN)
3657 n = gfc_get_namelist ();
3658 mio_symbol_ref (&n->sym);
3660 if (sym->namelist == NULL)
3661 sym->namelist = n;
3662 else
3663 m->next = n;
3665 m = n;
3667 sym->namelist_tail = m;
3670 mio_rparen ();
3674 /* Save/restore lists of gfc_interface structures. When loading an
3675 interface, we are really appending to the existing list of
3676 interfaces. Checking for duplicate and ambiguous interfaces has to
3677 be done later when all symbols have been loaded. */
3679 pointer_info *
3680 mio_interface_rest (gfc_interface **ip)
3682 gfc_interface *tail, *p;
3683 pointer_info *pi = NULL;
3685 if (iomode == IO_OUTPUT)
3687 if (ip != NULL)
3688 for (p = *ip; p; p = p->next)
3689 mio_symbol_ref (&p->sym);
3691 else
3693 if (*ip == NULL)
3694 tail = NULL;
3695 else
3697 tail = *ip;
3698 while (tail->next)
3699 tail = tail->next;
3702 for (;;)
3704 if (peek_atom () == ATOM_RPAREN)
3705 break;
3707 p = gfc_get_interface ();
3708 p->where = gfc_current_locus;
3709 pi = mio_symbol_ref (&p->sym);
3711 if (tail == NULL)
3712 *ip = p;
3713 else
3714 tail->next = p;
3716 tail = p;
3720 mio_rparen ();
3721 return pi;
3725 /* Save/restore a nameless operator interface. */
3727 static void
3728 mio_interface (gfc_interface **ip)
3730 mio_lparen ();
3731 mio_interface_rest (ip);
3735 /* Save/restore a named operator interface. */
3737 static void
3738 mio_symbol_interface (const char **name, const char **module,
3739 gfc_interface **ip)
3741 mio_lparen ();
3742 mio_pool_string (name);
3743 mio_pool_string (module);
3744 mio_interface_rest (ip);
3748 static void
3749 mio_namespace_ref (gfc_namespace **nsp)
3751 gfc_namespace *ns;
3752 pointer_info *p;
3754 p = mio_pointer_ref (nsp);
3756 if (p->type == P_UNKNOWN)
3757 p->type = P_NAMESPACE;
3759 if (iomode == IO_INPUT && p->integer != 0)
3761 ns = (gfc_namespace *) p->u.pointer;
3762 if (ns == NULL)
3764 ns = gfc_get_namespace (NULL, 0);
3765 associate_integer_pointer (p, ns);
3767 else
3768 ns->refs++;
3773 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3775 static gfc_namespace* current_f2k_derived;
3777 static void
3778 mio_typebound_proc (gfc_typebound_proc** proc)
3780 int flag;
3781 int overriding_flag;
3783 if (iomode == IO_INPUT)
3785 *proc = gfc_get_typebound_proc (NULL);
3786 (*proc)->where = gfc_current_locus;
3788 gcc_assert (*proc);
3790 mio_lparen ();
3792 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3794 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3795 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3796 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3797 overriding_flag = mio_name (overriding_flag, binding_overriding);
3798 (*proc)->deferred = ((overriding_flag & 2) != 0);
3799 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3800 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3802 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3803 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3804 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3806 mio_pool_string (&((*proc)->pass_arg));
3808 flag = (int) (*proc)->pass_arg_num;
3809 mio_integer (&flag);
3810 (*proc)->pass_arg_num = (unsigned) flag;
3812 if ((*proc)->is_generic)
3814 gfc_tbp_generic* g;
3815 int iop;
3817 mio_lparen ();
3819 if (iomode == IO_OUTPUT)
3820 for (g = (*proc)->u.generic; g; g = g->next)
3822 iop = (int) g->is_operator;
3823 mio_integer (&iop);
3824 mio_allocated_string (g->specific_st->name);
3826 else
3828 (*proc)->u.generic = NULL;
3829 while (peek_atom () != ATOM_RPAREN)
3831 gfc_symtree** sym_root;
3833 g = gfc_get_tbp_generic ();
3834 g->specific = NULL;
3836 mio_integer (&iop);
3837 g->is_operator = (bool) iop;
3839 require_atom (ATOM_STRING);
3840 sym_root = &current_f2k_derived->tb_sym_root;
3841 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3842 free (atom_string);
3844 g->next = (*proc)->u.generic;
3845 (*proc)->u.generic = g;
3849 mio_rparen ();
3851 else if (!(*proc)->ppc)
3852 mio_symtree_ref (&(*proc)->u.specific);
3854 mio_rparen ();
3857 /* Walker-callback function for this purpose. */
3858 static void
3859 mio_typebound_symtree (gfc_symtree* st)
3861 if (iomode == IO_OUTPUT && !st->n.tb)
3862 return;
3864 if (iomode == IO_OUTPUT)
3866 mio_lparen ();
3867 mio_allocated_string (st->name);
3869 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3871 mio_typebound_proc (&st->n.tb);
3872 mio_rparen ();
3875 /* IO a full symtree (in all depth). */
3876 static void
3877 mio_full_typebound_tree (gfc_symtree** root)
3879 mio_lparen ();
3881 if (iomode == IO_OUTPUT)
3882 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3883 else
3885 while (peek_atom () == ATOM_LPAREN)
3887 gfc_symtree* st;
3889 mio_lparen ();
3891 require_atom (ATOM_STRING);
3892 st = gfc_get_tbp_symtree (root, atom_string);
3893 free (atom_string);
3895 mio_typebound_symtree (st);
3899 mio_rparen ();
3902 static void
3903 mio_finalizer (gfc_finalizer **f)
3905 if (iomode == IO_OUTPUT)
3907 gcc_assert (*f);
3908 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3909 mio_symtree_ref (&(*f)->proc_tree);
3911 else
3913 *f = gfc_get_finalizer ();
3914 (*f)->where = gfc_current_locus; /* Value should not matter. */
3915 (*f)->next = NULL;
3917 mio_symtree_ref (&(*f)->proc_tree);
3918 (*f)->proc_sym = NULL;
3922 static void
3923 mio_f2k_derived (gfc_namespace *f2k)
3925 current_f2k_derived = f2k;
3927 /* Handle the list of finalizer procedures. */
3928 mio_lparen ();
3929 if (iomode == IO_OUTPUT)
3931 gfc_finalizer *f;
3932 for (f = f2k->finalizers; f; f = f->next)
3933 mio_finalizer (&f);
3935 else
3937 f2k->finalizers = NULL;
3938 while (peek_atom () != ATOM_RPAREN)
3940 gfc_finalizer *cur = NULL;
3941 mio_finalizer (&cur);
3942 cur->next = f2k->finalizers;
3943 f2k->finalizers = cur;
3946 mio_rparen ();
3948 /* Handle type-bound procedures. */
3949 mio_full_typebound_tree (&f2k->tb_sym_root);
3951 /* Type-bound user operators. */
3952 mio_full_typebound_tree (&f2k->tb_uop_root);
3954 /* Type-bound intrinsic operators. */
3955 mio_lparen ();
3956 if (iomode == IO_OUTPUT)
3958 int op;
3959 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3961 gfc_intrinsic_op realop;
3963 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3964 continue;
3966 mio_lparen ();
3967 realop = (gfc_intrinsic_op) op;
3968 mio_intrinsic_op (&realop);
3969 mio_typebound_proc (&f2k->tb_op[op]);
3970 mio_rparen ();
3973 else
3974 while (peek_atom () != ATOM_RPAREN)
3976 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3978 mio_lparen ();
3979 mio_intrinsic_op (&op);
3980 mio_typebound_proc (&f2k->tb_op[op]);
3981 mio_rparen ();
3983 mio_rparen ();
3986 static void
3987 mio_full_f2k_derived (gfc_symbol *sym)
3989 mio_lparen ();
3991 if (iomode == IO_OUTPUT)
3993 if (sym->f2k_derived)
3994 mio_f2k_derived (sym->f2k_derived);
3996 else
3998 if (peek_atom () != ATOM_RPAREN)
4000 sym->f2k_derived = gfc_get_namespace (NULL, 0);
4001 mio_f2k_derived (sym->f2k_derived);
4003 else
4004 gcc_assert (!sym->f2k_derived);
4007 mio_rparen ();
4010 static const mstring omp_declare_simd_clauses[] =
4012 minit ("INBRANCH", 0),
4013 minit ("NOTINBRANCH", 1),
4014 minit ("SIMDLEN", 2),
4015 minit ("UNIFORM", 3),
4016 minit ("LINEAR", 4),
4017 minit ("ALIGNED", 5),
4018 minit (NULL, -1)
4021 /* Handle !$omp declare simd. */
4023 static void
4024 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4026 if (iomode == IO_OUTPUT)
4028 if (*odsp == NULL)
4029 return;
4031 else if (peek_atom () != ATOM_LPAREN)
4032 return;
4034 gfc_omp_declare_simd *ods = *odsp;
4036 mio_lparen ();
4037 if (iomode == IO_OUTPUT)
4039 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4040 if (ods->clauses)
4042 gfc_omp_namelist *n;
4044 if (ods->clauses->inbranch)
4045 mio_name (0, omp_declare_simd_clauses);
4046 if (ods->clauses->notinbranch)
4047 mio_name (1, omp_declare_simd_clauses);
4048 if (ods->clauses->simdlen_expr)
4050 mio_name (2, omp_declare_simd_clauses);
4051 mio_expr (&ods->clauses->simdlen_expr);
4053 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4055 mio_name (3, omp_declare_simd_clauses);
4056 mio_symbol_ref (&n->sym);
4058 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4060 mio_name (4, omp_declare_simd_clauses);
4061 mio_symbol_ref (&n->sym);
4062 mio_expr (&n->expr);
4064 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4066 mio_name (5, omp_declare_simd_clauses);
4067 mio_symbol_ref (&n->sym);
4068 mio_expr (&n->expr);
4072 else
4074 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4076 require_atom (ATOM_NAME);
4077 *odsp = ods = gfc_get_omp_declare_simd ();
4078 ods->where = gfc_current_locus;
4079 ods->proc_name = ns->proc_name;
4080 if (peek_atom () == ATOM_NAME)
4082 ods->clauses = gfc_get_omp_clauses ();
4083 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4084 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4085 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4087 while (peek_atom () == ATOM_NAME)
4089 gfc_omp_namelist *n;
4090 int t = mio_name (0, omp_declare_simd_clauses);
4092 switch (t)
4094 case 0: ods->clauses->inbranch = true; break;
4095 case 1: ods->clauses->notinbranch = true; break;
4096 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4097 case 3:
4098 case 4:
4099 case 5:
4100 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4101 ptrs[t - 3] = &n->next;
4102 mio_symbol_ref (&n->sym);
4103 if (t != 3)
4104 mio_expr (&n->expr);
4105 break;
4110 mio_omp_declare_simd (ns, &ods->next);
4112 mio_rparen ();
4116 static const mstring omp_declare_reduction_stmt[] =
4118 minit ("ASSIGN", 0),
4119 minit ("CALL", 1),
4120 minit (NULL, -1)
4124 static void
4125 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4126 gfc_namespace *ns, bool is_initializer)
4128 if (iomode == IO_OUTPUT)
4130 if ((*sym1)->module == NULL)
4132 (*sym1)->module = module_name;
4133 (*sym2)->module = module_name;
4135 mio_symbol_ref (sym1);
4136 mio_symbol_ref (sym2);
4137 if (ns->code->op == EXEC_ASSIGN)
4139 mio_name (0, omp_declare_reduction_stmt);
4140 mio_expr (&ns->code->expr1);
4141 mio_expr (&ns->code->expr2);
4143 else
4145 int flag;
4146 mio_name (1, omp_declare_reduction_stmt);
4147 mio_symtree_ref (&ns->code->symtree);
4148 mio_actual_arglist (&ns->code->ext.actual);
4150 flag = ns->code->resolved_isym != NULL;
4151 mio_integer (&flag);
4152 if (flag)
4153 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4154 else
4155 mio_symbol_ref (&ns->code->resolved_sym);
4158 else
4160 pointer_info *p1 = mio_symbol_ref (sym1);
4161 pointer_info *p2 = mio_symbol_ref (sym2);
4162 gfc_symbol *sym;
4163 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4164 gcc_assert (p1->u.rsym.sym == NULL);
4165 /* Add hidden symbols to the symtree. */
4166 pointer_info *q = get_integer (p1->u.rsym.ns);
4167 q->u.pointer = (void *) ns;
4168 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4169 sym->ts = udr->ts;
4170 sym->module = gfc_get_string ("%s", p1->u.rsym.module);
4171 associate_integer_pointer (p1, sym);
4172 sym->attr.omp_udr_artificial_var = 1;
4173 gcc_assert (p2->u.rsym.sym == NULL);
4174 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4175 sym->ts = udr->ts;
4176 sym->module = gfc_get_string ("%s", p2->u.rsym.module);
4177 associate_integer_pointer (p2, sym);
4178 sym->attr.omp_udr_artificial_var = 1;
4179 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4181 ns->code = gfc_get_code (EXEC_ASSIGN);
4182 mio_expr (&ns->code->expr1);
4183 mio_expr (&ns->code->expr2);
4185 else
4187 int flag;
4188 ns->code = gfc_get_code (EXEC_CALL);
4189 mio_symtree_ref (&ns->code->symtree);
4190 mio_actual_arglist (&ns->code->ext.actual);
4192 mio_integer (&flag);
4193 if (flag)
4195 require_atom (ATOM_STRING);
4196 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4197 free (atom_string);
4199 else
4200 mio_symbol_ref (&ns->code->resolved_sym);
4202 ns->code->loc = gfc_current_locus;
4203 ns->omp_udr_ns = 1;
4208 /* Unlike most other routines, the address of the symbol node is already
4209 fixed on input and the name/module has already been filled in.
4210 If you update the symbol format here, don't forget to update read_module
4211 as well (look for "seek to the symbol's component list"). */
4213 static void
4214 mio_symbol (gfc_symbol *sym)
4216 int intmod = INTMOD_NONE;
4218 mio_lparen ();
4220 mio_symbol_attribute (&sym->attr);
4222 /* Note that components are always saved, even if they are supposed
4223 to be private. Component access is checked during searching. */
4224 mio_component_list (&sym->components, sym->attr.vtype);
4225 if (sym->components != NULL)
4226 sym->component_access
4227 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4229 mio_typespec (&sym->ts);
4230 if (sym->ts.type == BT_CLASS)
4231 sym->attr.class_ok = 1;
4233 if (iomode == IO_OUTPUT)
4234 mio_namespace_ref (&sym->formal_ns);
4235 else
4237 mio_namespace_ref (&sym->formal_ns);
4238 if (sym->formal_ns)
4239 sym->formal_ns->proc_name = sym;
4242 /* Save/restore common block links. */
4243 mio_symbol_ref (&sym->common_next);
4245 mio_formal_arglist (&sym->formal);
4247 if (sym->attr.flavor == FL_PARAMETER)
4248 mio_expr (&sym->value);
4250 mio_array_spec (&sym->as);
4252 mio_symbol_ref (&sym->result);
4254 if (sym->attr.cray_pointee)
4255 mio_symbol_ref (&sym->cp_pointer);
4257 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4258 mio_full_f2k_derived (sym);
4260 mio_namelist (sym);
4262 /* Add the fields that say whether this is from an intrinsic module,
4263 and if so, what symbol it is within the module. */
4264 /* mio_integer (&(sym->from_intmod)); */
4265 if (iomode == IO_OUTPUT)
4267 intmod = sym->from_intmod;
4268 mio_integer (&intmod);
4270 else
4272 mio_integer (&intmod);
4273 if (current_intmod)
4274 sym->from_intmod = current_intmod;
4275 else
4276 sym->from_intmod = (intmod_id) intmod;
4279 mio_integer (&(sym->intmod_sym_id));
4281 if (gfc_fl_struct (sym->attr.flavor))
4282 mio_integer (&(sym->hash_value));
4284 if (sym->formal_ns
4285 && sym->formal_ns->proc_name == sym
4286 && sym->formal_ns->entries == NULL)
4287 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4289 mio_rparen ();
4293 /************************* Top level subroutines *************************/
4295 /* Given a root symtree node and a symbol, try to find a symtree that
4296 references the symbol that is not a unique name. */
4298 static gfc_symtree *
4299 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
4301 gfc_symtree *s = NULL;
4303 if (st == NULL)
4304 return s;
4306 s = find_symtree_for_symbol (st->right, sym);
4307 if (s != NULL)
4308 return s;
4309 s = find_symtree_for_symbol (st->left, sym);
4310 if (s != NULL)
4311 return s;
4313 if (st->n.sym == sym && !check_unique_name (st->name))
4314 return st;
4316 return s;
4320 /* A recursive function to look for a specific symbol by name and by
4321 module. Whilst several symtrees might point to one symbol, its
4322 is sufficient for the purposes here than one exist. Note that
4323 generic interfaces are distinguished as are symbols that have been
4324 renamed in another module. */
4325 static gfc_symtree *
4326 find_symbol (gfc_symtree *st, const char *name,
4327 const char *module, int generic)
4329 int c;
4330 gfc_symtree *retval, *s;
4332 if (st == NULL || st->n.sym == NULL)
4333 return NULL;
4335 c = strcmp (name, st->n.sym->name);
4336 if (c == 0 && st->n.sym->module
4337 && strcmp (module, st->n.sym->module) == 0
4338 && !check_unique_name (st->name))
4340 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4342 /* Detect symbols that are renamed by use association in another
4343 module by the absence of a symtree and null attr.use_rename,
4344 since the latter is not transmitted in the module file. */
4345 if (((!generic && !st->n.sym->attr.generic)
4346 || (generic && st->n.sym->attr.generic))
4347 && !(s == NULL && !st->n.sym->attr.use_rename))
4348 return st;
4351 retval = find_symbol (st->left, name, module, generic);
4353 if (retval == NULL)
4354 retval = find_symbol (st->right, name, module, generic);
4356 return retval;
4360 /* Skip a list between balanced left and right parens.
4361 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4362 have been already parsed by hand, and the remaining of the content is to be
4363 skipped here. The default value is 0 (balanced parens). */
4365 static void
4366 skip_list (int nest_level = 0)
4368 int level;
4370 level = nest_level;
4373 switch (parse_atom ())
4375 case ATOM_LPAREN:
4376 level++;
4377 break;
4379 case ATOM_RPAREN:
4380 level--;
4381 break;
4383 case ATOM_STRING:
4384 free (atom_string);
4385 break;
4387 case ATOM_NAME:
4388 case ATOM_INTEGER:
4389 break;
4392 while (level > 0);
4396 /* Load operator interfaces from the module. Interfaces are unusual
4397 in that they attach themselves to existing symbols. */
4399 static void
4400 load_operator_interfaces (void)
4402 const char *p;
4403 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4404 gfc_user_op *uop;
4405 pointer_info *pi = NULL;
4406 int n, i;
4408 mio_lparen ();
4410 while (peek_atom () != ATOM_RPAREN)
4412 mio_lparen ();
4414 mio_internal_string (name);
4415 mio_internal_string (module);
4417 n = number_use_names (name, true);
4418 n = n ? n : 1;
4420 for (i = 1; i <= n; i++)
4422 /* Decide if we need to load this one or not. */
4423 p = find_use_name_n (name, &i, true);
4425 if (p == NULL)
4427 while (parse_atom () != ATOM_RPAREN);
4428 continue;
4431 if (i == 1)
4433 uop = gfc_get_uop (p);
4434 pi = mio_interface_rest (&uop->op);
4436 else
4438 if (gfc_find_uop (p, NULL))
4439 continue;
4440 uop = gfc_get_uop (p);
4441 uop->op = gfc_get_interface ();
4442 uop->op->where = gfc_current_locus;
4443 add_fixup (pi->integer, &uop->op->sym);
4448 mio_rparen ();
4452 /* Load interfaces from the module. Interfaces are unusual in that
4453 they attach themselves to existing symbols. */
4455 static void
4456 load_generic_interfaces (void)
4458 const char *p;
4459 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4460 gfc_symbol *sym;
4461 gfc_interface *generic = NULL, *gen = NULL;
4462 int n, i, renamed;
4463 bool ambiguous_set = false;
4465 mio_lparen ();
4467 while (peek_atom () != ATOM_RPAREN)
4469 mio_lparen ();
4471 mio_internal_string (name);
4472 mio_internal_string (module);
4474 n = number_use_names (name, false);
4475 renamed = n ? 1 : 0;
4476 n = n ? n : 1;
4478 for (i = 1; i <= n; i++)
4480 gfc_symtree *st;
4481 /* Decide if we need to load this one or not. */
4482 p = find_use_name_n (name, &i, false);
4484 st = find_symbol (gfc_current_ns->sym_root,
4485 name, module_name, 1);
4487 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4489 /* Skip the specific names for these cases. */
4490 while (i == 1 && parse_atom () != ATOM_RPAREN);
4492 continue;
4495 /* If the symbol exists already and is being USEd without being
4496 in an ONLY clause, do not load a new symtree(11.3.2). */
4497 if (!only_flag && st)
4498 sym = st->n.sym;
4500 if (!sym)
4502 if (st)
4504 sym = st->n.sym;
4505 if (strcmp (st->name, p) != 0)
4507 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4508 st->n.sym = sym;
4509 sym->refs++;
4513 /* Since we haven't found a valid generic interface, we had
4514 better make one. */
4515 if (!sym)
4517 gfc_get_symbol (p, NULL, &sym);
4518 sym->name = gfc_get_string ("%s", name);
4519 sym->module = module_name;
4520 sym->attr.flavor = FL_PROCEDURE;
4521 sym->attr.generic = 1;
4522 sym->attr.use_assoc = 1;
4525 else
4527 /* Unless sym is a generic interface, this reference
4528 is ambiguous. */
4529 if (st == NULL)
4530 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4532 sym = st->n.sym;
4534 if (st && !sym->attr.generic
4535 && !st->ambiguous
4536 && sym->module
4537 && strcmp (module, sym->module))
4539 ambiguous_set = true;
4540 st->ambiguous = 1;
4544 sym->attr.use_only = only_flag;
4545 sym->attr.use_rename = renamed;
4547 if (i == 1)
4549 mio_interface_rest (&sym->generic);
4550 generic = sym->generic;
4552 else if (!sym->generic)
4554 sym->generic = generic;
4555 sym->attr.generic_copy = 1;
4558 /* If a procedure that is not generic has generic interfaces
4559 that include itself, it is generic! We need to take care
4560 to retain symbols ambiguous that were already so. */
4561 if (sym->attr.use_assoc
4562 && !sym->attr.generic
4563 && sym->attr.flavor == FL_PROCEDURE)
4565 for (gen = generic; gen; gen = gen->next)
4567 if (gen->sym == sym)
4569 sym->attr.generic = 1;
4570 if (ambiguous_set)
4571 st->ambiguous = 0;
4572 break;
4580 mio_rparen ();
4584 /* Load common blocks. */
4586 static void
4587 load_commons (void)
4589 char name[GFC_MAX_SYMBOL_LEN + 1];
4590 gfc_common_head *p;
4592 mio_lparen ();
4594 while (peek_atom () != ATOM_RPAREN)
4596 int flags;
4597 char* label;
4598 mio_lparen ();
4599 mio_internal_string (name);
4601 p = gfc_get_common (name, 1);
4603 mio_symbol_ref (&p->head);
4604 mio_integer (&flags);
4605 if (flags & 1)
4606 p->saved = 1;
4607 if (flags & 2)
4608 p->threadprivate = 1;
4609 p->use_assoc = 1;
4611 /* Get whether this was a bind(c) common or not. */
4612 mio_integer (&p->is_bind_c);
4613 /* Get the binding label. */
4614 label = read_string ();
4615 if (strlen (label))
4616 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4617 XDELETEVEC (label);
4619 mio_rparen ();
4622 mio_rparen ();
4626 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4627 so that unused variables are not loaded and so that the expression can
4628 be safely freed. */
4630 static void
4631 load_equiv (void)
4633 gfc_equiv *head, *tail, *end, *eq, *equiv;
4634 bool duplicate;
4636 mio_lparen ();
4637 in_load_equiv = true;
4639 end = gfc_current_ns->equiv;
4640 while (end != NULL && end->next != NULL)
4641 end = end->next;
4643 while (peek_atom () != ATOM_RPAREN) {
4644 mio_lparen ();
4645 head = tail = NULL;
4647 while(peek_atom () != ATOM_RPAREN)
4649 if (head == NULL)
4650 head = tail = gfc_get_equiv ();
4651 else
4653 tail->eq = gfc_get_equiv ();
4654 tail = tail->eq;
4657 mio_pool_string (&tail->module);
4658 mio_expr (&tail->expr);
4661 /* Check for duplicate equivalences being loaded from different modules */
4662 duplicate = false;
4663 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
4665 if (equiv->module && head->module
4666 && strcmp (equiv->module, head->module) == 0)
4668 duplicate = true;
4669 break;
4673 if (duplicate)
4675 for (eq = head; eq; eq = head)
4677 head = eq->eq;
4678 gfc_free_expr (eq->expr);
4679 free (eq);
4683 if (end == NULL)
4684 gfc_current_ns->equiv = head;
4685 else
4686 end->next = head;
4688 if (head != NULL)
4689 end = head;
4691 mio_rparen ();
4694 mio_rparen ();
4695 in_load_equiv = false;
4699 /* This function loads OpenMP user defined reductions. */
4700 static void
4701 load_omp_udrs (void)
4703 mio_lparen ();
4704 while (peek_atom () != ATOM_RPAREN)
4706 const char *name = NULL, *newname;
4707 char *altname;
4708 gfc_typespec ts;
4709 gfc_symtree *st;
4710 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4712 mio_lparen ();
4713 mio_pool_string (&name);
4714 gfc_clear_ts (&ts);
4715 mio_typespec (&ts);
4716 if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
4718 const char *p = name + sizeof ("operator ") - 1;
4719 if (strcmp (p, "+") == 0)
4720 rop = OMP_REDUCTION_PLUS;
4721 else if (strcmp (p, "*") == 0)
4722 rop = OMP_REDUCTION_TIMES;
4723 else if (strcmp (p, "-") == 0)
4724 rop = OMP_REDUCTION_MINUS;
4725 else if (strcmp (p, ".and.") == 0)
4726 rop = OMP_REDUCTION_AND;
4727 else if (strcmp (p, ".or.") == 0)
4728 rop = OMP_REDUCTION_OR;
4729 else if (strcmp (p, ".eqv.") == 0)
4730 rop = OMP_REDUCTION_EQV;
4731 else if (strcmp (p, ".neqv.") == 0)
4732 rop = OMP_REDUCTION_NEQV;
4734 altname = NULL;
4735 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4737 size_t len = strlen (name + 1);
4738 altname = XALLOCAVEC (char, len);
4739 gcc_assert (name[len] == '.');
4740 memcpy (altname, name + 1, len - 1);
4741 altname[len - 1] = '\0';
4743 newname = name;
4744 if (rop == OMP_REDUCTION_USER)
4745 newname = find_use_name (altname ? altname : name, !!altname);
4746 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4747 newname = NULL;
4748 if (newname == NULL)
4750 skip_list (1);
4751 continue;
4753 if (altname && newname != altname)
4755 size_t len = strlen (newname);
4756 altname = XALLOCAVEC (char, len + 3);
4757 altname[0] = '.';
4758 memcpy (altname + 1, newname, len);
4759 altname[len + 1] = '.';
4760 altname[len + 2] = '\0';
4761 name = gfc_get_string ("%s", altname);
4763 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4764 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4765 if (udr)
4767 require_atom (ATOM_INTEGER);
4768 pointer_info *p = get_integer (atom_int);
4769 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4771 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4772 "module %s at %L",
4773 p->u.rsym.module, &gfc_current_locus);
4774 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4775 "%s at %L",
4776 udr->omp_out->module, &udr->where);
4778 skip_list (1);
4779 continue;
4781 udr = gfc_get_omp_udr ();
4782 udr->name = name;
4783 udr->rop = rop;
4784 udr->ts = ts;
4785 udr->where = gfc_current_locus;
4786 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4787 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4788 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4789 false);
4790 if (peek_atom () != ATOM_RPAREN)
4792 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4793 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4794 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4795 udr->initializer_ns, true);
4797 if (st)
4799 udr->next = st->n.omp_udr;
4800 st->n.omp_udr = udr;
4802 else
4804 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4805 st->n.omp_udr = udr;
4807 mio_rparen ();
4809 mio_rparen ();
4813 /* Recursive function to traverse the pointer_info tree and load a
4814 needed symbol. We return nonzero if we load a symbol and stop the
4815 traversal, because the act of loading can alter the tree. */
4817 static int
4818 load_needed (pointer_info *p)
4820 gfc_namespace *ns;
4821 pointer_info *q;
4822 gfc_symbol *sym;
4823 int rv;
4825 rv = 0;
4826 if (p == NULL)
4827 return rv;
4829 rv |= load_needed (p->left);
4830 rv |= load_needed (p->right);
4832 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4833 return rv;
4835 p->u.rsym.state = USED;
4837 set_module_locus (&p->u.rsym.where);
4839 sym = p->u.rsym.sym;
4840 if (sym == NULL)
4842 q = get_integer (p->u.rsym.ns);
4844 ns = (gfc_namespace *) q->u.pointer;
4845 if (ns == NULL)
4847 /* Create an interface namespace if necessary. These are
4848 the namespaces that hold the formal parameters of module
4849 procedures. */
4851 ns = gfc_get_namespace (NULL, 0);
4852 associate_integer_pointer (q, ns);
4855 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4856 doesn't go pear-shaped if the symbol is used. */
4857 if (!ns->proc_name)
4858 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4859 1, &ns->proc_name);
4861 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4862 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
4863 sym->module = gfc_get_string ("%s", p->u.rsym.module);
4864 if (p->u.rsym.binding_label)
4865 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4866 (p->u.rsym.binding_label));
4868 associate_integer_pointer (p, sym);
4871 mio_symbol (sym);
4872 sym->attr.use_assoc = 1;
4874 /* Unliked derived types, a STRUCTURE may share names with other symbols.
4875 We greedily converted the the symbol name to lowercase before we knew its
4876 type, so now we must fix it. */
4877 if (sym->attr.flavor == FL_STRUCT)
4878 sym->name = gfc_dt_upper_string (sym->name);
4880 /* Mark as only or rename for later diagnosis for explicitly imported
4881 but not used warnings; don't mark internal symbols such as __vtab,
4882 __def_init etc. Only mark them if they have been explicitly loaded. */
4884 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4886 gfc_use_rename *u;
4888 /* Search the use/rename list for the variable; if the variable is
4889 found, mark it. */
4890 for (u = gfc_rename_list; u; u = u->next)
4892 if (strcmp (u->use_name, sym->name) == 0)
4894 sym->attr.use_only = 1;
4895 break;
4900 if (p->u.rsym.renamed)
4901 sym->attr.use_rename = 1;
4903 return 1;
4907 /* Recursive function for cleaning up things after a module has been read. */
4909 static void
4910 read_cleanup (pointer_info *p)
4912 gfc_symtree *st;
4913 pointer_info *q;
4915 if (p == NULL)
4916 return;
4918 read_cleanup (p->left);
4919 read_cleanup (p->right);
4921 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4923 gfc_namespace *ns;
4924 /* Add hidden symbols to the symtree. */
4925 q = get_integer (p->u.rsym.ns);
4926 ns = (gfc_namespace *) q->u.pointer;
4928 if (!p->u.rsym.sym->attr.vtype
4929 && !p->u.rsym.sym->attr.vtab)
4930 st = gfc_get_unique_symtree (ns);
4931 else
4933 /* There is no reason to use 'unique_symtrees' for vtabs or
4934 vtypes - their name is fine for a symtree and reduces the
4935 namespace pollution. */
4936 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4937 if (!st)
4938 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4941 st->n.sym = p->u.rsym.sym;
4942 st->n.sym->refs++;
4944 /* Fixup any symtree references. */
4945 p->u.rsym.symtree = st;
4946 resolve_fixups (p->u.rsym.stfixup, st);
4947 p->u.rsym.stfixup = NULL;
4950 /* Free unused symbols. */
4951 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4952 gfc_free_symbol (p->u.rsym.sym);
4956 /* It is not quite enough to check for ambiguity in the symbols by
4957 the loaded symbol and the new symbol not being identical. */
4958 static bool
4959 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
4961 gfc_symbol *rsym;
4962 module_locus locus;
4963 symbol_attribute attr;
4964 gfc_symbol *st_sym;
4966 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
4968 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4969 "current program unit", st->name, module_name);
4970 return true;
4973 st_sym = st->n.sym;
4974 rsym = info->u.rsym.sym;
4975 if (st_sym == rsym)
4976 return false;
4978 if (st_sym->attr.vtab || st_sym->attr.vtype)
4979 return false;
4981 /* If the existing symbol is generic from a different module and
4982 the new symbol is generic there can be no ambiguity. */
4983 if (st_sym->attr.generic
4984 && st_sym->module
4985 && st_sym->module != module_name)
4987 /* The new symbol's attributes have not yet been read. Since
4988 we need attr.generic, read it directly. */
4989 get_module_locus (&locus);
4990 set_module_locus (&info->u.rsym.where);
4991 mio_lparen ();
4992 attr.generic = 0;
4993 mio_symbol_attribute (&attr);
4994 set_module_locus (&locus);
4995 if (attr.generic)
4996 return false;
4999 return true;
5003 /* Read a module file. */
5005 static void
5006 read_module (void)
5008 module_locus operator_interfaces, user_operators, omp_udrs;
5009 const char *p;
5010 char name[GFC_MAX_SYMBOL_LEN + 1];
5011 int i;
5012 /* Workaround -Wmaybe-uninitialized false positive during
5013 profiledbootstrap by initializing them. */
5014 int ambiguous = 0, j, nuse, symbol = 0;
5015 pointer_info *info, *q;
5016 gfc_use_rename *u = NULL;
5017 gfc_symtree *st;
5018 gfc_symbol *sym;
5020 get_module_locus (&operator_interfaces); /* Skip these for now. */
5021 skip_list ();
5023 get_module_locus (&user_operators);
5024 skip_list ();
5025 skip_list ();
5027 /* Skip commons and equivalences for now. */
5028 skip_list ();
5029 skip_list ();
5031 /* Skip OpenMP UDRs. */
5032 get_module_locus (&omp_udrs);
5033 skip_list ();
5035 mio_lparen ();
5037 /* Create the fixup nodes for all the symbols. */
5039 while (peek_atom () != ATOM_RPAREN)
5041 char* bind_label;
5042 require_atom (ATOM_INTEGER);
5043 info = get_integer (atom_int);
5045 info->type = P_SYMBOL;
5046 info->u.rsym.state = UNUSED;
5048 info->u.rsym.true_name = read_string ();
5049 info->u.rsym.module = read_string ();
5050 bind_label = read_string ();
5051 if (strlen (bind_label))
5052 info->u.rsym.binding_label = bind_label;
5053 else
5054 XDELETEVEC (bind_label);
5056 require_atom (ATOM_INTEGER);
5057 info->u.rsym.ns = atom_int;
5059 get_module_locus (&info->u.rsym.where);
5061 /* See if the symbol has already been loaded by a previous module.
5062 If so, we reference the existing symbol and prevent it from
5063 being loaded again. This should not happen if the symbol being
5064 read is an index for an assumed shape dummy array (ns != 1). */
5066 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5068 if (sym == NULL
5069 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5071 skip_list ();
5072 continue;
5075 info->u.rsym.state = USED;
5076 info->u.rsym.sym = sym;
5077 /* The current symbol has already been loaded, so we can avoid loading
5078 it again. However, if it is a derived type, some of its components
5079 can be used in expressions in the module. To avoid the module loading
5080 failing, we need to associate the module's component pointer indexes
5081 with the existing symbol's component pointers. */
5082 if (gfc_fl_struct (sym->attr.flavor))
5084 gfc_component *c;
5086 /* First seek to the symbol's component list. */
5087 mio_lparen (); /* symbol opening. */
5088 skip_list (); /* skip symbol attribute. */
5090 mio_lparen (); /* component list opening. */
5091 for (c = sym->components; c; c = c->next)
5093 pointer_info *p;
5094 const char *comp_name;
5095 int n;
5097 mio_lparen (); /* component opening. */
5098 mio_integer (&n);
5099 p = get_integer (n);
5100 if (p->u.pointer == NULL)
5101 associate_integer_pointer (p, c);
5102 mio_pool_string (&comp_name);
5103 gcc_assert (comp_name == c->name);
5104 skip_list (1); /* component end. */
5106 mio_rparen (); /* component list closing. */
5108 skip_list (1); /* symbol end. */
5110 else
5111 skip_list ();
5113 /* Some symbols do not have a namespace (eg. formal arguments),
5114 so the automatic "unique symtree" mechanism must be suppressed
5115 by marking them as referenced. */
5116 q = get_integer (info->u.rsym.ns);
5117 if (q->u.pointer == NULL)
5119 info->u.rsym.referenced = 1;
5120 continue;
5123 /* If possible recycle the symtree that references the symbol.
5124 If a symtree is not found and the module does not import one,
5125 a unique-name symtree is found by read_cleanup. */
5126 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
5127 if (st != NULL)
5129 info->u.rsym.symtree = st;
5130 info->u.rsym.referenced = 1;
5134 mio_rparen ();
5136 /* Parse the symtree lists. This lets us mark which symbols need to
5137 be loaded. Renaming is also done at this point by replacing the
5138 symtree name. */
5140 mio_lparen ();
5142 while (peek_atom () != ATOM_RPAREN)
5144 mio_internal_string (name);
5145 mio_integer (&ambiguous);
5146 mio_integer (&symbol);
5148 info = get_integer (symbol);
5150 /* See how many use names there are. If none, go through the start
5151 of the loop at least once. */
5152 nuse = number_use_names (name, false);
5153 info->u.rsym.renamed = nuse ? 1 : 0;
5155 if (nuse == 0)
5156 nuse = 1;
5158 for (j = 1; j <= nuse; j++)
5160 /* Get the jth local name for this symbol. */
5161 p = find_use_name_n (name, &j, false);
5163 if (p == NULL && strcmp (name, module_name) == 0)
5164 p = name;
5166 /* Exception: Always import vtabs & vtypes. */
5167 if (p == NULL && name[0] == '_'
5168 && (strncmp (name, "__vtab_", 5) == 0
5169 || strncmp (name, "__vtype_", 6) == 0))
5170 p = name;
5172 /* Skip symtree nodes not in an ONLY clause, unless there
5173 is an existing symtree loaded from another USE statement. */
5174 if (p == NULL)
5176 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5177 if (st != NULL
5178 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5179 && st->n.sym->module != NULL
5180 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5182 info->u.rsym.symtree = st;
5183 info->u.rsym.sym = st->n.sym;
5185 continue;
5188 /* If a symbol of the same name and module exists already,
5189 this symbol, which is not in an ONLY clause, must not be
5190 added to the namespace(11.3.2). Note that find_symbol
5191 only returns the first occurrence that it finds. */
5192 if (!only_flag && !info->u.rsym.renamed
5193 && strcmp (name, module_name) != 0
5194 && find_symbol (gfc_current_ns->sym_root, name,
5195 module_name, 0))
5196 continue;
5198 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5200 if (st != NULL
5201 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5203 /* Check for ambiguous symbols. */
5204 if (check_for_ambiguous (st, info))
5205 st->ambiguous = 1;
5206 else
5207 info->u.rsym.symtree = st;
5209 else
5211 if (st)
5213 /* This symbol is host associated from a module in a
5214 submodule. Hide it with a unique symtree. */
5215 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5216 s->n.sym = st->n.sym;
5217 st->n.sym = NULL;
5219 else
5221 /* Create a symtree node in the current namespace for this
5222 symbol. */
5223 st = check_unique_name (p)
5224 ? gfc_get_unique_symtree (gfc_current_ns)
5225 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5226 st->ambiguous = ambiguous;
5229 sym = info->u.rsym.sym;
5231 /* Create a symbol node if it doesn't already exist. */
5232 if (sym == NULL)
5234 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5235 gfc_current_ns);
5236 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5237 sym = info->u.rsym.sym;
5238 sym->module = gfc_get_string ("%s", info->u.rsym.module);
5240 if (info->u.rsym.binding_label)
5242 tree id = get_identifier (info->u.rsym.binding_label);
5243 sym->binding_label = IDENTIFIER_POINTER (id);
5247 st->n.sym = sym;
5248 st->n.sym->refs++;
5250 if (strcmp (name, p) != 0)
5251 sym->attr.use_rename = 1;
5253 if (name[0] != '_'
5254 || (strncmp (name, "__vtab_", 5) != 0
5255 && strncmp (name, "__vtype_", 6) != 0))
5256 sym->attr.use_only = only_flag;
5258 /* Store the symtree pointing to this symbol. */
5259 info->u.rsym.symtree = st;
5261 if (info->u.rsym.state == UNUSED)
5262 info->u.rsym.state = NEEDED;
5263 info->u.rsym.referenced = 1;
5268 mio_rparen ();
5270 /* Load intrinsic operator interfaces. */
5271 set_module_locus (&operator_interfaces);
5272 mio_lparen ();
5274 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5276 if (i == INTRINSIC_USER)
5277 continue;
5279 if (only_flag)
5281 u = find_use_operator ((gfc_intrinsic_op) i);
5283 if (u == NULL)
5285 skip_list ();
5286 continue;
5289 u->found = 1;
5292 mio_interface (&gfc_current_ns->op[i]);
5293 if (u && !gfc_current_ns->op[i])
5294 u->found = 0;
5297 mio_rparen ();
5299 /* Load generic and user operator interfaces. These must follow the
5300 loading of symtree because otherwise symbols can be marked as
5301 ambiguous. */
5303 set_module_locus (&user_operators);
5305 load_operator_interfaces ();
5306 load_generic_interfaces ();
5308 load_commons ();
5309 load_equiv ();
5311 /* Load OpenMP user defined reductions. */
5312 set_module_locus (&omp_udrs);
5313 load_omp_udrs ();
5315 /* At this point, we read those symbols that are needed but haven't
5316 been loaded yet. If one symbol requires another, the other gets
5317 marked as NEEDED if its previous state was UNUSED. */
5319 while (load_needed (pi_root));
5321 /* Make sure all elements of the rename-list were found in the module. */
5323 for (u = gfc_rename_list; u; u = u->next)
5325 if (u->found)
5326 continue;
5328 if (u->op == INTRINSIC_NONE)
5330 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5331 u->use_name, &u->where, module_name);
5332 continue;
5335 if (u->op == INTRINSIC_USER)
5337 gfc_error ("User operator %qs referenced at %L not found "
5338 "in module %qs", u->use_name, &u->where, module_name);
5339 continue;
5342 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5343 "in module %qs", gfc_op2string (u->op), &u->where,
5344 module_name);
5347 /* Clean up symbol nodes that were never loaded, create references
5348 to hidden symbols. */
5350 read_cleanup (pi_root);
5354 /* Given an access type that is specific to an entity and the default
5355 access, return nonzero if the entity is publicly accessible. If the
5356 element is declared as PUBLIC, then it is public; if declared
5357 PRIVATE, then private, and otherwise it is public unless the default
5358 access in this context has been declared PRIVATE. */
5360 static bool dump_smod = false;
5362 static bool
5363 check_access (gfc_access specific_access, gfc_access default_access)
5365 if (dump_smod)
5366 return true;
5368 if (specific_access == ACCESS_PUBLIC)
5369 return TRUE;
5370 if (specific_access == ACCESS_PRIVATE)
5371 return FALSE;
5373 if (flag_module_private)
5374 return default_access == ACCESS_PUBLIC;
5375 else
5376 return default_access != ACCESS_PRIVATE;
5380 bool
5381 gfc_check_symbol_access (gfc_symbol *sym)
5383 if (sym->attr.vtab || sym->attr.vtype)
5384 return true;
5385 else
5386 return check_access (sym->attr.access, sym->ns->default_access);
5390 /* A structure to remember which commons we've already written. */
5392 struct written_common
5394 BBT_HEADER(written_common);
5395 const char *name, *label;
5398 static struct written_common *written_commons = NULL;
5400 /* Comparison function used for balancing the binary tree. */
5402 static int
5403 compare_written_commons (void *a1, void *b1)
5405 const char *aname = ((struct written_common *) a1)->name;
5406 const char *alabel = ((struct written_common *) a1)->label;
5407 const char *bname = ((struct written_common *) b1)->name;
5408 const char *blabel = ((struct written_common *) b1)->label;
5409 int c = strcmp (aname, bname);
5411 return (c != 0 ? c : strcmp (alabel, blabel));
5414 /* Free a list of written commons. */
5416 static void
5417 free_written_common (struct written_common *w)
5419 if (!w)
5420 return;
5422 if (w->left)
5423 free_written_common (w->left);
5424 if (w->right)
5425 free_written_common (w->right);
5427 free (w);
5430 /* Write a common block to the module -- recursive helper function. */
5432 static void
5433 write_common_0 (gfc_symtree *st, bool this_module)
5435 gfc_common_head *p;
5436 const char * name;
5437 int flags;
5438 const char *label;
5439 struct written_common *w;
5440 bool write_me = true;
5442 if (st == NULL)
5443 return;
5445 write_common_0 (st->left, this_module);
5447 /* We will write out the binding label, or "" if no label given. */
5448 name = st->n.common->name;
5449 p = st->n.common;
5450 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5452 /* Check if we've already output this common. */
5453 w = written_commons;
5454 while (w)
5456 int c = strcmp (name, w->name);
5457 c = (c != 0 ? c : strcmp (label, w->label));
5458 if (c == 0)
5459 write_me = false;
5461 w = (c < 0) ? w->left : w->right;
5464 if (this_module && p->use_assoc)
5465 write_me = false;
5467 if (write_me)
5469 /* Write the common to the module. */
5470 mio_lparen ();
5471 mio_pool_string (&name);
5473 mio_symbol_ref (&p->head);
5474 flags = p->saved ? 1 : 0;
5475 if (p->threadprivate)
5476 flags |= 2;
5477 mio_integer (&flags);
5479 /* Write out whether the common block is bind(c) or not. */
5480 mio_integer (&(p->is_bind_c));
5482 mio_pool_string (&label);
5483 mio_rparen ();
5485 /* Record that we have written this common. */
5486 w = XCNEW (struct written_common);
5487 w->name = p->name;
5488 w->label = label;
5489 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5492 write_common_0 (st->right, this_module);
5496 /* Write a common, by initializing the list of written commons, calling
5497 the recursive function write_common_0() and cleaning up afterwards. */
5499 static void
5500 write_common (gfc_symtree *st)
5502 written_commons = NULL;
5503 write_common_0 (st, true);
5504 write_common_0 (st, false);
5505 free_written_common (written_commons);
5506 written_commons = NULL;
5510 /* Write the blank common block to the module. */
5512 static void
5513 write_blank_common (void)
5515 const char * name = BLANK_COMMON_NAME;
5516 int saved;
5517 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5518 this, but it hasn't been checked. Just making it so for now. */
5519 int is_bind_c = 0;
5521 if (gfc_current_ns->blank_common.head == NULL)
5522 return;
5524 mio_lparen ();
5526 mio_pool_string (&name);
5528 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5529 saved = gfc_current_ns->blank_common.saved;
5530 mio_integer (&saved);
5532 /* Write out whether the common block is bind(c) or not. */
5533 mio_integer (&is_bind_c);
5535 /* Write out an empty binding label. */
5536 write_atom (ATOM_STRING, "");
5538 mio_rparen ();
5542 /* Write equivalences to the module. */
5544 static void
5545 write_equiv (void)
5547 gfc_equiv *eq, *e;
5548 int num;
5550 num = 0;
5551 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5553 mio_lparen ();
5555 for (e = eq; e; e = e->eq)
5557 if (e->module == NULL)
5558 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5559 mio_allocated_string (e->module);
5560 mio_expr (&e->expr);
5563 num++;
5564 mio_rparen ();
5569 /* Write a symbol to the module. */
5571 static void
5572 write_symbol (int n, gfc_symbol *sym)
5574 const char *label;
5576 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5577 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5579 mio_integer (&n);
5581 if (gfc_fl_struct (sym->attr.flavor))
5583 const char *name;
5584 name = gfc_dt_upper_string (sym->name);
5585 mio_pool_string (&name);
5587 else
5588 mio_pool_string (&sym->name);
5590 mio_pool_string (&sym->module);
5591 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5593 label = sym->binding_label;
5594 mio_pool_string (&label);
5596 else
5597 write_atom (ATOM_STRING, "");
5599 mio_pointer_ref (&sym->ns);
5601 mio_symbol (sym);
5602 write_char ('\n');
5606 /* Recursive traversal function to write the initial set of symbols to
5607 the module. We check to see if the symbol should be written
5608 according to the access specification. */
5610 static void
5611 write_symbol0 (gfc_symtree *st)
5613 gfc_symbol *sym;
5614 pointer_info *p;
5615 bool dont_write = false;
5617 if (st == NULL)
5618 return;
5620 write_symbol0 (st->left);
5622 sym = st->n.sym;
5623 if (sym->module == NULL)
5624 sym->module = module_name;
5626 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5627 && !sym->attr.subroutine && !sym->attr.function)
5628 dont_write = true;
5630 if (!gfc_check_symbol_access (sym))
5631 dont_write = true;
5633 if (!dont_write)
5635 p = get_pointer (sym);
5636 if (p->type == P_UNKNOWN)
5637 p->type = P_SYMBOL;
5639 if (p->u.wsym.state != WRITTEN)
5641 write_symbol (p->integer, sym);
5642 p->u.wsym.state = WRITTEN;
5646 write_symbol0 (st->right);
5650 static void
5651 write_omp_udr (gfc_omp_udr *udr)
5653 switch (udr->rop)
5655 case OMP_REDUCTION_USER:
5656 /* Non-operators can't be used outside of the module. */
5657 if (udr->name[0] != '.')
5658 return;
5659 else
5661 gfc_symtree *st;
5662 size_t len = strlen (udr->name + 1);
5663 char *name = XALLOCAVEC (char, len);
5664 memcpy (name, udr->name, len - 1);
5665 name[len - 1] = '\0';
5666 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5667 /* If corresponding user operator is private, don't write
5668 the UDR. */
5669 if (st != NULL)
5671 gfc_user_op *uop = st->n.uop;
5672 if (!check_access (uop->access, uop->ns->default_access))
5673 return;
5676 break;
5677 case OMP_REDUCTION_PLUS:
5678 case OMP_REDUCTION_MINUS:
5679 case OMP_REDUCTION_TIMES:
5680 case OMP_REDUCTION_AND:
5681 case OMP_REDUCTION_OR:
5682 case OMP_REDUCTION_EQV:
5683 case OMP_REDUCTION_NEQV:
5684 /* If corresponding operator is private, don't write the UDR. */
5685 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5686 gfc_current_ns->default_access))
5687 return;
5688 break;
5689 default:
5690 break;
5692 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5694 /* If derived type is private, don't write the UDR. */
5695 if (!gfc_check_symbol_access (udr->ts.u.derived))
5696 return;
5699 mio_lparen ();
5700 mio_pool_string (&udr->name);
5701 mio_typespec (&udr->ts);
5702 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5703 if (udr->initializer_ns)
5704 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5705 udr->initializer_ns, true);
5706 mio_rparen ();
5710 static void
5711 write_omp_udrs (gfc_symtree *st)
5713 if (st == NULL)
5714 return;
5716 write_omp_udrs (st->left);
5717 gfc_omp_udr *udr;
5718 for (udr = st->n.omp_udr; udr; udr = udr->next)
5719 write_omp_udr (udr);
5720 write_omp_udrs (st->right);
5724 /* Type for the temporary tree used when writing secondary symbols. */
5726 struct sorted_pointer_info
5728 BBT_HEADER (sorted_pointer_info);
5730 pointer_info *p;
5733 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5735 /* Recursively traverse the temporary tree, free its contents. */
5737 static void
5738 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5740 if (!p)
5741 return;
5743 free_sorted_pointer_info_tree (p->left);
5744 free_sorted_pointer_info_tree (p->right);
5746 free (p);
5749 /* Comparison function for the temporary tree. */
5751 static int
5752 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5754 sorted_pointer_info *spi1, *spi2;
5755 spi1 = (sorted_pointer_info *)_spi1;
5756 spi2 = (sorted_pointer_info *)_spi2;
5758 if (spi1->p->integer < spi2->p->integer)
5759 return -1;
5760 if (spi1->p->integer > spi2->p->integer)
5761 return 1;
5762 return 0;
5766 /* Finds the symbols that need to be written and collects them in the
5767 sorted_pi tree so that they can be traversed in an order
5768 independent of memory addresses. */
5770 static void
5771 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5773 if (!p)
5774 return;
5776 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5778 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5779 sp->p = p;
5781 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5784 find_symbols_to_write (tree, p->left);
5785 find_symbols_to_write (tree, p->right);
5789 /* Recursive function that traverses the tree of symbols that need to be
5790 written and writes them in order. */
5792 static void
5793 write_symbol1_recursion (sorted_pointer_info *sp)
5795 if (!sp)
5796 return;
5798 write_symbol1_recursion (sp->left);
5800 pointer_info *p1 = sp->p;
5801 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5803 p1->u.wsym.state = WRITTEN;
5804 write_symbol (p1->integer, p1->u.wsym.sym);
5805 p1->u.wsym.sym->attr.public_used = 1;
5807 write_symbol1_recursion (sp->right);
5811 /* Write the secondary set of symbols to the module file. These are
5812 symbols that were not public yet are needed by the public symbols
5813 or another dependent symbol. The act of writing a symbol can add
5814 symbols to the pointer_info tree, so we return nonzero if a symbol
5815 was written and pass that information upwards. The caller will
5816 then call this function again until nothing was written. It uses
5817 the utility functions and a temporary tree to ensure a reproducible
5818 ordering of the symbol output and thus the module file. */
5820 static int
5821 write_symbol1 (pointer_info *p)
5823 if (!p)
5824 return 0;
5826 /* Put symbols that need to be written into a tree sorted on the
5827 integer field. */
5829 sorted_pointer_info *spi_root = NULL;
5830 find_symbols_to_write (&spi_root, p);
5832 /* No symbols to write, return. */
5833 if (!spi_root)
5834 return 0;
5836 /* Otherwise, write and free the tree again. */
5837 write_symbol1_recursion (spi_root);
5838 free_sorted_pointer_info_tree (spi_root);
5840 return 1;
5844 /* Write operator interfaces associated with a symbol. */
5846 static void
5847 write_operator (gfc_user_op *uop)
5849 static char nullstring[] = "";
5850 const char *p = nullstring;
5852 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5853 return;
5855 mio_symbol_interface (&uop->name, &p, &uop->op);
5859 /* Write generic interfaces from the namespace sym_root. */
5861 static void
5862 write_generic (gfc_symtree *st)
5864 gfc_symbol *sym;
5866 if (st == NULL)
5867 return;
5869 write_generic (st->left);
5871 sym = st->n.sym;
5872 if (sym && !check_unique_name (st->name)
5873 && sym->generic && gfc_check_symbol_access (sym))
5875 if (!sym->module)
5876 sym->module = module_name;
5878 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5881 write_generic (st->right);
5885 static void
5886 write_symtree (gfc_symtree *st)
5888 gfc_symbol *sym;
5889 pointer_info *p;
5891 sym = st->n.sym;
5893 /* A symbol in an interface body must not be visible in the
5894 module file. */
5895 if (sym->ns != gfc_current_ns
5896 && sym->ns->proc_name
5897 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5898 return;
5900 if (!gfc_check_symbol_access (sym)
5901 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5902 && !sym->attr.subroutine && !sym->attr.function))
5903 return;
5905 if (check_unique_name (st->name))
5906 return;
5908 p = find_pointer (sym);
5909 if (p == NULL)
5910 gfc_internal_error ("write_symtree(): Symbol not written");
5912 mio_pool_string (&st->name);
5913 mio_integer (&st->ambiguous);
5914 mio_integer (&p->integer);
5918 static void
5919 write_module (void)
5921 int i;
5923 /* Write the operator interfaces. */
5924 mio_lparen ();
5926 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5928 if (i == INTRINSIC_USER)
5929 continue;
5931 mio_interface (check_access (gfc_current_ns->operator_access[i],
5932 gfc_current_ns->default_access)
5933 ? &gfc_current_ns->op[i] : NULL);
5936 mio_rparen ();
5937 write_char ('\n');
5938 write_char ('\n');
5940 mio_lparen ();
5941 gfc_traverse_user_op (gfc_current_ns, write_operator);
5942 mio_rparen ();
5943 write_char ('\n');
5944 write_char ('\n');
5946 mio_lparen ();
5947 write_generic (gfc_current_ns->sym_root);
5948 mio_rparen ();
5949 write_char ('\n');
5950 write_char ('\n');
5952 mio_lparen ();
5953 write_blank_common ();
5954 write_common (gfc_current_ns->common_root);
5955 mio_rparen ();
5956 write_char ('\n');
5957 write_char ('\n');
5959 mio_lparen ();
5960 write_equiv ();
5961 mio_rparen ();
5962 write_char ('\n');
5963 write_char ('\n');
5965 mio_lparen ();
5966 write_omp_udrs (gfc_current_ns->omp_udr_root);
5967 mio_rparen ();
5968 write_char ('\n');
5969 write_char ('\n');
5971 /* Write symbol information. First we traverse all symbols in the
5972 primary namespace, writing those that need to be written.
5973 Sometimes writing one symbol will cause another to need to be
5974 written. A list of these symbols ends up on the write stack, and
5975 we end by popping the bottom of the stack and writing the symbol
5976 until the stack is empty. */
5978 mio_lparen ();
5980 write_symbol0 (gfc_current_ns->sym_root);
5981 while (write_symbol1 (pi_root))
5982 /* Nothing. */;
5984 mio_rparen ();
5986 write_char ('\n');
5987 write_char ('\n');
5989 mio_lparen ();
5990 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5991 mio_rparen ();
5995 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5996 true on success, false on failure. */
5998 static bool
5999 read_crc32_from_module_file (const char* filename, uLong* crc)
6001 FILE *file;
6002 char buf[4];
6003 unsigned int val;
6005 /* Open the file in binary mode. */
6006 if ((file = fopen (filename, "rb")) == NULL)
6007 return false;
6009 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
6010 file. See RFC 1952. */
6011 if (fseek (file, -8, SEEK_END) != 0)
6013 fclose (file);
6014 return false;
6017 /* Read the CRC32. */
6018 if (fread (buf, 1, 4, file) != 4)
6020 fclose (file);
6021 return false;
6024 /* Close the file. */
6025 fclose (file);
6027 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6028 + ((buf[3] & 0xFF) << 24);
6029 *crc = val;
6031 /* For debugging, the CRC value printed in hexadecimal should match
6032 the CRC printed by "zcat -l -v filename".
6033 printf("CRC of file %s is %x\n", filename, val); */
6035 return true;
6039 /* Given module, dump it to disk. If there was an error while
6040 processing the module, dump_flag will be set to zero and we delete
6041 the module file, even if it was already there. */
6043 static void
6044 dump_module (const char *name, int dump_flag)
6046 int n;
6047 char *filename, *filename_tmp;
6048 uLong crc, crc_old;
6050 module_name = gfc_get_string ("%s", name);
6052 if (dump_smod)
6054 name = submodule_name;
6055 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6057 else
6058 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6060 if (gfc_option.module_dir != NULL)
6062 n += strlen (gfc_option.module_dir);
6063 filename = (char *) alloca (n);
6064 strcpy (filename, gfc_option.module_dir);
6065 strcat (filename, name);
6067 else
6069 filename = (char *) alloca (n);
6070 strcpy (filename, name);
6073 if (dump_smod)
6074 strcat (filename, SUBMODULE_EXTENSION);
6075 else
6076 strcat (filename, MODULE_EXTENSION);
6078 /* Name of the temporary file used to write the module. */
6079 filename_tmp = (char *) alloca (n + 1);
6080 strcpy (filename_tmp, filename);
6081 strcat (filename_tmp, "0");
6083 /* There was an error while processing the module. We delete the
6084 module file, even if it was already there. */
6085 if (!dump_flag)
6087 remove (filename);
6088 return;
6091 if (gfc_cpp_makedep ())
6092 gfc_cpp_add_target (filename);
6094 /* Write the module to the temporary file. */
6095 module_fp = gzopen (filename_tmp, "w");
6096 if (module_fp == NULL)
6097 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6098 filename_tmp, xstrerror (errno));
6100 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6101 MOD_VERSION, gfc_source_file);
6103 /* Write the module itself. */
6104 iomode = IO_OUTPUT;
6106 init_pi_tree ();
6108 write_module ();
6110 free_pi_tree (pi_root);
6111 pi_root = NULL;
6113 write_char ('\n');
6115 if (gzclose (module_fp))
6116 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6117 filename_tmp, xstrerror (errno));
6119 /* Read the CRC32 from the gzip trailers of the module files and
6120 compare. */
6121 if (!read_crc32_from_module_file (filename_tmp, &crc)
6122 || !read_crc32_from_module_file (filename, &crc_old)
6123 || crc_old != crc)
6125 /* Module file have changed, replace the old one. */
6126 if (remove (filename) && errno != ENOENT)
6127 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6128 xstrerror (errno));
6129 if (rename (filename_tmp, filename))
6130 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6131 filename_tmp, filename, xstrerror (errno));
6133 else
6135 if (remove (filename_tmp))
6136 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6137 filename_tmp, xstrerror (errno));
6142 void
6143 gfc_dump_module (const char *name, int dump_flag)
6145 if (gfc_state_stack->state == COMP_SUBMODULE)
6146 dump_smod = true;
6147 else
6148 dump_smod =false;
6150 no_module_procedures = true;
6151 dump_module (name, dump_flag);
6153 if (no_module_procedures || dump_smod)
6154 return;
6156 /* Write a submodule file from a module. The 'dump_smod' flag switches
6157 off the check for PRIVATE entities. */
6158 dump_smod = true;
6159 submodule_name = module_name;
6160 dump_module (name, dump_flag);
6161 dump_smod = false;
6164 static void
6165 create_intrinsic_function (const char *name, int id,
6166 const char *modname, intmod_id module,
6167 bool subroutine, gfc_symbol *result_type)
6169 gfc_intrinsic_sym *isym;
6170 gfc_symtree *tmp_symtree;
6171 gfc_symbol *sym;
6173 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6174 if (tmp_symtree)
6176 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6177 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6178 return;
6179 gfc_error ("Symbol %qs at %C already declared", name);
6180 return;
6183 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6184 sym = tmp_symtree->n.sym;
6186 if (subroutine)
6188 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6189 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6190 sym->attr.subroutine = 1;
6192 else
6194 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6195 isym = gfc_intrinsic_function_by_id (isym_id);
6197 sym->attr.function = 1;
6198 if (result_type)
6200 sym->ts.type = BT_DERIVED;
6201 sym->ts.u.derived = result_type;
6202 sym->ts.is_c_interop = 1;
6203 isym->ts.f90_type = BT_VOID;
6204 isym->ts.type = BT_DERIVED;
6205 isym->ts.f90_type = BT_VOID;
6206 isym->ts.u.derived = result_type;
6207 isym->ts.is_c_interop = 1;
6210 gcc_assert (isym);
6212 sym->attr.flavor = FL_PROCEDURE;
6213 sym->attr.intrinsic = 1;
6215 sym->module = gfc_get_string ("%s", modname);
6216 sym->attr.use_assoc = 1;
6217 sym->from_intmod = module;
6218 sym->intmod_sym_id = id;
6222 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6223 the current namespace for all named constants, pointer types, and
6224 procedures in the module unless the only clause was used or a rename
6225 list was provided. */
6227 static void
6228 import_iso_c_binding_module (void)
6230 gfc_symbol *mod_sym = NULL, *return_type;
6231 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6232 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6233 const char *iso_c_module_name = "__iso_c_binding";
6234 gfc_use_rename *u;
6235 int i;
6236 bool want_c_ptr = false, want_c_funptr = false;
6238 /* Look only in the current namespace. */
6239 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6241 if (mod_symtree == NULL)
6243 /* symtree doesn't already exist in current namespace. */
6244 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6245 false);
6247 if (mod_symtree != NULL)
6248 mod_sym = mod_symtree->n.sym;
6249 else
6250 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6251 "create symbol for %s", iso_c_module_name);
6253 mod_sym->attr.flavor = FL_MODULE;
6254 mod_sym->attr.intrinsic = 1;
6255 mod_sym->module = gfc_get_string ("%s", iso_c_module_name);
6256 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6259 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6260 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6261 need C_(FUN)PTR. */
6262 for (u = gfc_rename_list; u; u = u->next)
6264 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6265 u->use_name) == 0)
6266 want_c_ptr = true;
6267 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6268 u->use_name) == 0)
6269 want_c_ptr = true;
6270 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6271 u->use_name) == 0)
6272 want_c_funptr = true;
6273 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6274 u->use_name) == 0)
6275 want_c_funptr = true;
6276 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6277 u->use_name) == 0)
6279 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6280 (iso_c_binding_symbol)
6281 ISOCBINDING_PTR,
6282 u->local_name[0] ? u->local_name
6283 : u->use_name,
6284 NULL, false);
6286 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6287 u->use_name) == 0)
6289 c_funptr
6290 = generate_isocbinding_symbol (iso_c_module_name,
6291 (iso_c_binding_symbol)
6292 ISOCBINDING_FUNPTR,
6293 u->local_name[0] ? u->local_name
6294 : u->use_name,
6295 NULL, false);
6299 if ((want_c_ptr || !only_flag) && !c_ptr)
6300 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6301 (iso_c_binding_symbol)
6302 ISOCBINDING_PTR,
6303 NULL, NULL, only_flag);
6304 if ((want_c_funptr || !only_flag) && !c_funptr)
6305 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6306 (iso_c_binding_symbol)
6307 ISOCBINDING_FUNPTR,
6308 NULL, NULL, only_flag);
6310 /* Generate the symbols for the named constants representing
6311 the kinds for intrinsic data types. */
6312 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6314 bool found = false;
6315 for (u = gfc_rename_list; u; u = u->next)
6316 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6318 bool not_in_std;
6319 const char *name;
6320 u->found = 1;
6321 found = true;
6323 switch (i)
6325 #define NAMED_FUNCTION(a,b,c,d) \
6326 case a: \
6327 not_in_std = (gfc_option.allow_std & d) == 0; \
6328 name = b; \
6329 break;
6330 #define NAMED_SUBROUTINE(a,b,c,d) \
6331 case a: \
6332 not_in_std = (gfc_option.allow_std & d) == 0; \
6333 name = b; \
6334 break;
6335 #define NAMED_INTCST(a,b,c,d) \
6336 case a: \
6337 not_in_std = (gfc_option.allow_std & d) == 0; \
6338 name = b; \
6339 break;
6340 #define NAMED_REALCST(a,b,c,d) \
6341 case a: \
6342 not_in_std = (gfc_option.allow_std & d) == 0; \
6343 name = b; \
6344 break;
6345 #define NAMED_CMPXCST(a,b,c,d) \
6346 case a: \
6347 not_in_std = (gfc_option.allow_std & d) == 0; \
6348 name = b; \
6349 break;
6350 #include "iso-c-binding.def"
6351 default:
6352 not_in_std = false;
6353 name = "";
6356 if (not_in_std)
6358 gfc_error ("The symbol %qs, referenced at %L, is not "
6359 "in the selected standard", name, &u->where);
6360 continue;
6363 switch (i)
6365 #define NAMED_FUNCTION(a,b,c,d) \
6366 case a: \
6367 if (a == ISOCBINDING_LOC) \
6368 return_type = c_ptr->n.sym; \
6369 else if (a == ISOCBINDING_FUNLOC) \
6370 return_type = c_funptr->n.sym; \
6371 else \
6372 return_type = NULL; \
6373 create_intrinsic_function (u->local_name[0] \
6374 ? u->local_name : u->use_name, \
6375 a, iso_c_module_name, \
6376 INTMOD_ISO_C_BINDING, false, \
6377 return_type); \
6378 break;
6379 #define NAMED_SUBROUTINE(a,b,c,d) \
6380 case a: \
6381 create_intrinsic_function (u->local_name[0] ? u->local_name \
6382 : u->use_name, \
6383 a, iso_c_module_name, \
6384 INTMOD_ISO_C_BINDING, true, NULL); \
6385 break;
6386 #include "iso-c-binding.def"
6388 case ISOCBINDING_PTR:
6389 case ISOCBINDING_FUNPTR:
6390 /* Already handled above. */
6391 break;
6392 default:
6393 if (i == ISOCBINDING_NULL_PTR)
6394 tmp_symtree = c_ptr;
6395 else if (i == ISOCBINDING_NULL_FUNPTR)
6396 tmp_symtree = c_funptr;
6397 else
6398 tmp_symtree = NULL;
6399 generate_isocbinding_symbol (iso_c_module_name,
6400 (iso_c_binding_symbol) i,
6401 u->local_name[0]
6402 ? u->local_name : u->use_name,
6403 tmp_symtree, false);
6407 if (!found && !only_flag)
6409 /* Skip, if the symbol is not in the enabled standard. */
6410 switch (i)
6412 #define NAMED_FUNCTION(a,b,c,d) \
6413 case a: \
6414 if ((gfc_option.allow_std & d) == 0) \
6415 continue; \
6416 break;
6417 #define NAMED_SUBROUTINE(a,b,c,d) \
6418 case a: \
6419 if ((gfc_option.allow_std & d) == 0) \
6420 continue; \
6421 break;
6422 #define NAMED_INTCST(a,b,c,d) \
6423 case a: \
6424 if ((gfc_option.allow_std & d) == 0) \
6425 continue; \
6426 break;
6427 #define NAMED_REALCST(a,b,c,d) \
6428 case a: \
6429 if ((gfc_option.allow_std & d) == 0) \
6430 continue; \
6431 break;
6432 #define NAMED_CMPXCST(a,b,c,d) \
6433 case a: \
6434 if ((gfc_option.allow_std & d) == 0) \
6435 continue; \
6436 break;
6437 #include "iso-c-binding.def"
6438 default:
6439 ; /* Not GFC_STD_* versioned. */
6442 switch (i)
6444 #define NAMED_FUNCTION(a,b,c,d) \
6445 case a: \
6446 if (a == ISOCBINDING_LOC) \
6447 return_type = c_ptr->n.sym; \
6448 else if (a == ISOCBINDING_FUNLOC) \
6449 return_type = c_funptr->n.sym; \
6450 else \
6451 return_type = NULL; \
6452 create_intrinsic_function (b, a, iso_c_module_name, \
6453 INTMOD_ISO_C_BINDING, false, \
6454 return_type); \
6455 break;
6456 #define NAMED_SUBROUTINE(a,b,c,d) \
6457 case a: \
6458 create_intrinsic_function (b, a, iso_c_module_name, \
6459 INTMOD_ISO_C_BINDING, true, NULL); \
6460 break;
6461 #include "iso-c-binding.def"
6463 case ISOCBINDING_PTR:
6464 case ISOCBINDING_FUNPTR:
6465 /* Already handled above. */
6466 break;
6467 default:
6468 if (i == ISOCBINDING_NULL_PTR)
6469 tmp_symtree = c_ptr;
6470 else if (i == ISOCBINDING_NULL_FUNPTR)
6471 tmp_symtree = c_funptr;
6472 else
6473 tmp_symtree = NULL;
6474 generate_isocbinding_symbol (iso_c_module_name,
6475 (iso_c_binding_symbol) i, NULL,
6476 tmp_symtree, false);
6481 for (u = gfc_rename_list; u; u = u->next)
6483 if (u->found)
6484 continue;
6486 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6487 "module ISO_C_BINDING", u->use_name, &u->where);
6492 /* Add an integer named constant from a given module. */
6494 static void
6495 create_int_parameter (const char *name, int value, const char *modname,
6496 intmod_id module, int id)
6498 gfc_symtree *tmp_symtree;
6499 gfc_symbol *sym;
6501 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6502 if (tmp_symtree != NULL)
6504 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6505 return;
6506 else
6507 gfc_error ("Symbol %qs already declared", name);
6510 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6511 sym = tmp_symtree->n.sym;
6513 sym->module = gfc_get_string ("%s", modname);
6514 sym->attr.flavor = FL_PARAMETER;
6515 sym->ts.type = BT_INTEGER;
6516 sym->ts.kind = gfc_default_integer_kind;
6517 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6518 sym->attr.use_assoc = 1;
6519 sym->from_intmod = module;
6520 sym->intmod_sym_id = id;
6524 /* Value is already contained by the array constructor, but not
6525 yet the shape. */
6527 static void
6528 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6529 const char *modname, intmod_id module, int id)
6531 gfc_symtree *tmp_symtree;
6532 gfc_symbol *sym;
6534 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6535 if (tmp_symtree != NULL)
6537 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6538 return;
6539 else
6540 gfc_error ("Symbol %qs already declared", name);
6543 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6544 sym = tmp_symtree->n.sym;
6546 sym->module = gfc_get_string ("%s", modname);
6547 sym->attr.flavor = FL_PARAMETER;
6548 sym->ts.type = BT_INTEGER;
6549 sym->ts.kind = gfc_default_integer_kind;
6550 sym->attr.use_assoc = 1;
6551 sym->from_intmod = module;
6552 sym->intmod_sym_id = id;
6553 sym->attr.dimension = 1;
6554 sym->as = gfc_get_array_spec ();
6555 sym->as->rank = 1;
6556 sym->as->type = AS_EXPLICIT;
6557 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6558 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6560 sym->value = value;
6561 sym->value->shape = gfc_get_shape (1);
6562 mpz_init_set_ui (sym->value->shape[0], size);
6566 /* Add an derived type for a given module. */
6568 static void
6569 create_derived_type (const char *name, const char *modname,
6570 intmod_id module, int id)
6572 gfc_symtree *tmp_symtree;
6573 gfc_symbol *sym, *dt_sym;
6574 gfc_interface *intr, *head;
6576 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6577 if (tmp_symtree != NULL)
6579 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6580 return;
6581 else
6582 gfc_error ("Symbol %qs already declared", name);
6585 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6586 sym = tmp_symtree->n.sym;
6587 sym->module = gfc_get_string ("%s", modname);
6588 sym->from_intmod = module;
6589 sym->intmod_sym_id = id;
6590 sym->attr.flavor = FL_PROCEDURE;
6591 sym->attr.function = 1;
6592 sym->attr.generic = 1;
6594 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
6595 gfc_current_ns, &tmp_symtree, false);
6596 dt_sym = tmp_symtree->n.sym;
6597 dt_sym->name = gfc_get_string ("%s", sym->name);
6598 dt_sym->attr.flavor = FL_DERIVED;
6599 dt_sym->attr.private_comp = 1;
6600 dt_sym->attr.zero_comp = 1;
6601 dt_sym->attr.use_assoc = 1;
6602 dt_sym->module = gfc_get_string ("%s", modname);
6603 dt_sym->from_intmod = module;
6604 dt_sym->intmod_sym_id = id;
6606 head = sym->generic;
6607 intr = gfc_get_interface ();
6608 intr->sym = dt_sym;
6609 intr->where = gfc_current_locus;
6610 intr->next = head;
6611 sym->generic = intr;
6612 sym->attr.if_source = IFSRC_DECL;
6616 /* Read the contents of the module file into a temporary buffer. */
6618 static void
6619 read_module_to_tmpbuf ()
6621 /* We don't know the uncompressed size, so enlarge the buffer as
6622 needed. */
6623 int cursz = 4096;
6624 int rsize = cursz;
6625 int len = 0;
6627 module_content = XNEWVEC (char, cursz);
6629 while (1)
6631 int nread = gzread (module_fp, module_content + len, rsize);
6632 len += nread;
6633 if (nread < rsize)
6634 break;
6635 cursz *= 2;
6636 module_content = XRESIZEVEC (char, module_content, cursz);
6637 rsize = cursz - len;
6640 module_content = XRESIZEVEC (char, module_content, len + 1);
6641 module_content[len] = '\0';
6643 module_pos = 0;
6647 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6649 static void
6650 use_iso_fortran_env_module (void)
6652 static char mod[] = "iso_fortran_env";
6653 gfc_use_rename *u;
6654 gfc_symbol *mod_sym;
6655 gfc_symtree *mod_symtree;
6656 gfc_expr *expr;
6657 int i, j;
6659 intmod_sym symbol[] = {
6660 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6661 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6662 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6663 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6664 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6665 #include "iso-fortran-env.def"
6666 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6668 i = 0;
6669 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6670 #include "iso-fortran-env.def"
6672 /* Generate the symbol for the module itself. */
6673 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6674 if (mod_symtree == NULL)
6676 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6677 gcc_assert (mod_symtree);
6678 mod_sym = mod_symtree->n.sym;
6680 mod_sym->attr.flavor = FL_MODULE;
6681 mod_sym->attr.intrinsic = 1;
6682 mod_sym->module = gfc_get_string ("%s", mod);
6683 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6685 else
6686 if (!mod_symtree->n.sym->attr.intrinsic)
6687 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6688 "non-intrinsic module name used previously", mod);
6690 /* Generate the symbols for the module integer named constants. */
6692 for (i = 0; symbol[i].name; i++)
6694 bool found = false;
6695 for (u = gfc_rename_list; u; u = u->next)
6697 if (strcmp (symbol[i].name, u->use_name) == 0)
6699 found = true;
6700 u->found = 1;
6702 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6703 "referenced at %L, is not in the selected "
6704 "standard", symbol[i].name, &u->where))
6705 continue;
6707 if ((flag_default_integer || flag_default_real)
6708 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6709 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
6710 "constant from intrinsic module "
6711 "ISO_FORTRAN_ENV at %L is incompatible with "
6712 "option %qs", &u->where,
6713 flag_default_integer
6714 ? "-fdefault-integer-8"
6715 : "-fdefault-real-8");
6716 switch (symbol[i].id)
6718 #define NAMED_INTCST(a,b,c,d) \
6719 case a:
6720 #include "iso-fortran-env.def"
6721 create_int_parameter (u->local_name[0] ? u->local_name
6722 : u->use_name,
6723 symbol[i].value, mod,
6724 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6725 break;
6727 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6728 case a:\
6729 expr = gfc_get_array_expr (BT_INTEGER, \
6730 gfc_default_integer_kind,\
6731 NULL); \
6732 for (j = 0; KINDS[j].kind != 0; j++) \
6733 gfc_constructor_append_expr (&expr->value.constructor, \
6734 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6735 KINDS[j].kind), NULL); \
6736 create_int_parameter_array (u->local_name[0] ? u->local_name \
6737 : u->use_name, \
6738 j, expr, mod, \
6739 INTMOD_ISO_FORTRAN_ENV, \
6740 symbol[i].id); \
6741 break;
6742 #include "iso-fortran-env.def"
6744 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6745 case a:
6746 #include "iso-fortran-env.def"
6747 create_derived_type (u->local_name[0] ? u->local_name
6748 : u->use_name,
6749 mod, INTMOD_ISO_FORTRAN_ENV,
6750 symbol[i].id);
6751 break;
6753 #define NAMED_FUNCTION(a,b,c,d) \
6754 case a:
6755 #include "iso-fortran-env.def"
6756 create_intrinsic_function (u->local_name[0] ? u->local_name
6757 : u->use_name,
6758 symbol[i].id, mod,
6759 INTMOD_ISO_FORTRAN_ENV, false,
6760 NULL);
6761 break;
6763 default:
6764 gcc_unreachable ();
6769 if (!found && !only_flag)
6771 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6772 continue;
6774 if ((flag_default_integer || flag_default_real)
6775 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6776 gfc_warning_now (0,
6777 "Use of the NUMERIC_STORAGE_SIZE named constant "
6778 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6779 "incompatible with option %s",
6780 flag_default_integer
6781 ? "-fdefault-integer-8" : "-fdefault-real-8");
6783 switch (symbol[i].id)
6785 #define NAMED_INTCST(a,b,c,d) \
6786 case a:
6787 #include "iso-fortran-env.def"
6788 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6789 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6790 break;
6792 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6793 case a:\
6794 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6795 NULL); \
6796 for (j = 0; KINDS[j].kind != 0; j++) \
6797 gfc_constructor_append_expr (&expr->value.constructor, \
6798 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6799 KINDS[j].kind), NULL); \
6800 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6801 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6802 break;
6803 #include "iso-fortran-env.def"
6805 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6806 case a:
6807 #include "iso-fortran-env.def"
6808 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6809 symbol[i].id);
6810 break;
6812 #define NAMED_FUNCTION(a,b,c,d) \
6813 case a:
6814 #include "iso-fortran-env.def"
6815 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6816 INTMOD_ISO_FORTRAN_ENV, false,
6817 NULL);
6818 break;
6820 default:
6821 gcc_unreachable ();
6826 for (u = gfc_rename_list; u; u = u->next)
6828 if (u->found)
6829 continue;
6831 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6832 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6837 /* Process a USE directive. */
6839 static void
6840 gfc_use_module (gfc_use_list *module)
6842 char *filename;
6843 gfc_state_data *p;
6844 int c, line, start;
6845 gfc_symtree *mod_symtree;
6846 gfc_use_list *use_stmt;
6847 locus old_locus = gfc_current_locus;
6849 gfc_current_locus = module->where;
6850 module_name = module->module_name;
6851 gfc_rename_list = module->rename;
6852 only_flag = module->only_flag;
6853 current_intmod = INTMOD_NONE;
6855 if (!only_flag)
6856 gfc_warning_now (OPT_Wuse_without_only,
6857 "USE statement at %C has no ONLY qualifier");
6859 if (gfc_state_stack->state == COMP_MODULE
6860 || module->submodule_name == NULL)
6862 filename = XALLOCAVEC (char, strlen (module_name)
6863 + strlen (MODULE_EXTENSION) + 1);
6864 strcpy (filename, module_name);
6865 strcat (filename, MODULE_EXTENSION);
6867 else
6869 filename = XALLOCAVEC (char, strlen (module->submodule_name)
6870 + strlen (SUBMODULE_EXTENSION) + 1);
6871 strcpy (filename, module->submodule_name);
6872 strcat (filename, SUBMODULE_EXTENSION);
6875 /* First, try to find an non-intrinsic module, unless the USE statement
6876 specified that the module is intrinsic. */
6877 module_fp = NULL;
6878 if (!module->intrinsic)
6879 module_fp = gzopen_included_file (filename, true, true);
6881 /* Then, see if it's an intrinsic one, unless the USE statement
6882 specified that the module is non-intrinsic. */
6883 if (module_fp == NULL && !module->non_intrinsic)
6885 if (strcmp (module_name, "iso_fortran_env") == 0
6886 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6887 "intrinsic module at %C"))
6889 use_iso_fortran_env_module ();
6890 free_rename (module->rename);
6891 module->rename = NULL;
6892 gfc_current_locus = old_locus;
6893 module->intrinsic = true;
6894 return;
6897 if (strcmp (module_name, "iso_c_binding") == 0
6898 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6900 import_iso_c_binding_module();
6901 free_rename (module->rename);
6902 module->rename = NULL;
6903 gfc_current_locus = old_locus;
6904 module->intrinsic = true;
6905 return;
6908 module_fp = gzopen_intrinsic_module (filename);
6910 if (module_fp == NULL && module->intrinsic)
6911 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6912 module_name);
6914 /* Check for the IEEE modules, so we can mark their symbols
6915 accordingly when we read them. */
6916 if (strcmp (module_name, "ieee_features") == 0
6917 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
6919 current_intmod = INTMOD_IEEE_FEATURES;
6921 else if (strcmp (module_name, "ieee_exceptions") == 0
6922 && gfc_notify_std (GFC_STD_F2003,
6923 "IEEE_EXCEPTIONS module at %C"))
6925 current_intmod = INTMOD_IEEE_EXCEPTIONS;
6927 else if (strcmp (module_name, "ieee_arithmetic") == 0
6928 && gfc_notify_std (GFC_STD_F2003,
6929 "IEEE_ARITHMETIC module at %C"))
6931 current_intmod = INTMOD_IEEE_ARITHMETIC;
6935 if (module_fp == NULL)
6937 if (gfc_state_stack->state != COMP_SUBMODULE
6938 && module->submodule_name == NULL)
6939 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6940 filename, xstrerror (errno));
6941 else
6942 gfc_fatal_error ("Module file %qs has not been generated, either "
6943 "because the module does not contain a MODULE "
6944 "PROCEDURE or there is an error in the module.",
6945 filename);
6948 /* Check that we haven't already USEd an intrinsic module with the
6949 same name. */
6951 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6952 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6953 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6954 "intrinsic module name used previously", module_name);
6956 iomode = IO_INPUT;
6957 module_line = 1;
6958 module_column = 1;
6959 start = 0;
6961 read_module_to_tmpbuf ();
6962 gzclose (module_fp);
6964 /* Skip the first line of the module, after checking that this is
6965 a gfortran module file. */
6966 line = 0;
6967 while (line < 1)
6969 c = module_char ();
6970 if (c == EOF)
6971 bad_module ("Unexpected end of module");
6972 if (start++ < 3)
6973 parse_name (c);
6974 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6975 || (start == 2 && strcmp (atom_name, " module") != 0))
6976 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6977 " module file", filename);
6978 if (start == 3)
6980 if (strcmp (atom_name, " version") != 0
6981 || module_char () != ' '
6982 || parse_atom () != ATOM_STRING
6983 || strcmp (atom_string, MOD_VERSION))
6984 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6985 " because it was created by a different"
6986 " version of GNU Fortran", filename);
6988 free (atom_string);
6991 if (c == '\n')
6992 line++;
6995 /* Make sure we're not reading the same module that we may be building. */
6996 for (p = gfc_state_stack; p; p = p->previous)
6997 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
6998 && strcmp (p->sym->name, module_name) == 0)
6999 gfc_fatal_error ("Can't USE the same %smodule we're building!",
7000 p->state == COMP_SUBMODULE ? "sub" : "");
7002 init_pi_tree ();
7003 init_true_name_tree ();
7005 read_module ();
7007 free_true_name (true_name_root);
7008 true_name_root = NULL;
7010 free_pi_tree (pi_root);
7011 pi_root = NULL;
7013 XDELETEVEC (module_content);
7014 module_content = NULL;
7016 use_stmt = gfc_get_use_list ();
7017 *use_stmt = *module;
7018 use_stmt->next = gfc_current_ns->use_stmts;
7019 gfc_current_ns->use_stmts = use_stmt;
7021 gfc_current_locus = old_locus;
7025 /* Remove duplicated intrinsic operators from the rename list. */
7027 static void
7028 rename_list_remove_duplicate (gfc_use_rename *list)
7030 gfc_use_rename *seek, *last;
7032 for (; list; list = list->next)
7033 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7035 last = list;
7036 for (seek = list->next; seek; seek = last->next)
7038 if (list->op == seek->op)
7040 last->next = seek->next;
7041 free (seek);
7043 else
7044 last = seek;
7050 /* Process all USE directives. */
7052 void
7053 gfc_use_modules (void)
7055 gfc_use_list *next, *seek, *last;
7057 for (next = module_list; next; next = next->next)
7059 bool non_intrinsic = next->non_intrinsic;
7060 bool intrinsic = next->intrinsic;
7061 bool neither = !non_intrinsic && !intrinsic;
7063 for (seek = next->next; seek; seek = seek->next)
7065 if (next->module_name != seek->module_name)
7066 continue;
7068 if (seek->non_intrinsic)
7069 non_intrinsic = true;
7070 else if (seek->intrinsic)
7071 intrinsic = true;
7072 else
7073 neither = true;
7076 if (intrinsic && neither && !non_intrinsic)
7078 char *filename;
7079 FILE *fp;
7081 filename = XALLOCAVEC (char,
7082 strlen (next->module_name)
7083 + strlen (MODULE_EXTENSION) + 1);
7084 strcpy (filename, next->module_name);
7085 strcat (filename, MODULE_EXTENSION);
7086 fp = gfc_open_included_file (filename, true, true);
7087 if (fp != NULL)
7089 non_intrinsic = true;
7090 fclose (fp);
7094 last = next;
7095 for (seek = next->next; seek; seek = last->next)
7097 if (next->module_name != seek->module_name)
7099 last = seek;
7100 continue;
7103 if ((!next->intrinsic && !seek->intrinsic)
7104 || (next->intrinsic && seek->intrinsic)
7105 || !non_intrinsic)
7107 if (!seek->only_flag)
7108 next->only_flag = false;
7109 if (seek->rename)
7111 gfc_use_rename *r = seek->rename;
7112 while (r->next)
7113 r = r->next;
7114 r->next = next->rename;
7115 next->rename = seek->rename;
7117 last->next = seek->next;
7118 free (seek);
7120 else
7121 last = seek;
7125 for (; module_list; module_list = next)
7127 next = module_list->next;
7128 rename_list_remove_duplicate (module_list->rename);
7129 gfc_use_module (module_list);
7130 free (module_list);
7132 gfc_rename_list = NULL;
7136 void
7137 gfc_free_use_stmts (gfc_use_list *use_stmts)
7139 gfc_use_list *next;
7140 for (; use_stmts; use_stmts = next)
7142 gfc_use_rename *next_rename;
7144 for (; use_stmts->rename; use_stmts->rename = next_rename)
7146 next_rename = use_stmts->rename->next;
7147 free (use_stmts->rename);
7149 next = use_stmts->next;
7150 free (use_stmts);
7155 void
7156 gfc_module_init_2 (void)
7158 last_atom = ATOM_LPAREN;
7159 gfc_rename_list = NULL;
7160 module_list = NULL;
7164 void
7165 gfc_module_done_2 (void)
7167 free_rename (gfc_rename_list);
7168 gfc_rename_list = NULL;