[RS6000] TOC refs generated during reload
[official-gcc.git] / gcc / fortran / module.c
blob4d664f079f5a9c4e71c0af14f3bc8415a5cd2190
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2016 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 (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 (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 (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;
744 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
745 return MATCH_ERROR;
747 gfc_new_block = NULL;
748 gcc_assert (module_list == NULL);
750 if (gfc_match_char ('(') != MATCH_YES)
751 goto syntax;
753 while (1)
755 m = gfc_match (" %n", name);
756 if (m != MATCH_YES)
757 goto syntax;
759 use_list = gfc_get_use_list ();
760 use_list->where = gfc_current_locus;
762 if (module_list)
764 gfc_use_list *last = module_list;
765 while (last->next)
766 last = last->next;
767 last->next = use_list;
768 use_list->module_name
769 = gfc_get_string ("%s.%s", module_list->module_name, name);
770 use_list->submodule_name
771 = gfc_get_string ("%s@%s", module_list->module_name, name);
773 else
775 module_list = use_list;
776 use_list->module_name = gfc_get_string (name);
777 use_list->submodule_name = use_list->module_name;
780 if (gfc_match_char (')') == MATCH_YES)
781 break;
783 if (gfc_match_char (':') != MATCH_YES)
784 goto syntax;
787 m = gfc_match (" %s%t", &gfc_new_block);
788 if (m != MATCH_YES)
789 goto syntax;
791 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
792 gfc_new_block->name);
794 gfc_new_block->name = gfc_get_string ("%s.%s",
795 module_list->module_name,
796 gfc_new_block->name);
798 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
799 gfc_new_block->name, NULL))
800 return MATCH_ERROR;
802 /* Just retain the ultimate .(s)mod file for reading, since it
803 contains all the information in its ancestors. */
804 use_list = module_list;
805 for (; module_list->next; use_list = module_list)
807 module_list = use_list->next;
808 free (use_list);
811 return MATCH_YES;
813 syntax:
814 gfc_error ("Syntax error in SUBMODULE statement at %C");
815 return MATCH_ERROR;
819 /* Given a name and a number, inst, return the inst name
820 under which to load this symbol. Returns NULL if this
821 symbol shouldn't be loaded. If inst is zero, returns
822 the number of instances of this name. If interface is
823 true, a user-defined operator is sought, otherwise only
824 non-operators are sought. */
826 static const char *
827 find_use_name_n (const char *name, int *inst, bool interface)
829 gfc_use_rename *u;
830 const char *low_name = NULL;
831 int i;
833 /* For derived types. */
834 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
835 low_name = gfc_dt_lower_string (name);
837 i = 0;
838 for (u = gfc_rename_list; u; u = u->next)
840 if ((!low_name && strcmp (u->use_name, name) != 0)
841 || (low_name && strcmp (u->use_name, low_name) != 0)
842 || (u->op == INTRINSIC_USER && !interface)
843 || (u->op != INTRINSIC_USER && interface))
844 continue;
845 if (++i == *inst)
846 break;
849 if (!*inst)
851 *inst = i;
852 return NULL;
855 if (u == NULL)
856 return only_flag ? NULL : name;
858 u->found = 1;
860 if (low_name)
862 if (u->local_name[0] == '\0')
863 return name;
864 return gfc_dt_upper_string (u->local_name);
867 return (u->local_name[0] != '\0') ? u->local_name : name;
871 /* Given a name, return the name under which to load this symbol.
872 Returns NULL if this symbol shouldn't be loaded. */
874 static const char *
875 find_use_name (const char *name, bool interface)
877 int i = 1;
878 return find_use_name_n (name, &i, interface);
882 /* Given a real name, return the number of use names associated with it. */
884 static int
885 number_use_names (const char *name, bool interface)
887 int i = 0;
888 find_use_name_n (name, &i, interface);
889 return i;
893 /* Try to find the operator in the current list. */
895 static gfc_use_rename *
896 find_use_operator (gfc_intrinsic_op op)
898 gfc_use_rename *u;
900 for (u = gfc_rename_list; u; u = u->next)
901 if (u->op == op)
902 return u;
904 return NULL;
908 /*****************************************************************/
910 /* The next couple of subroutines maintain a tree used to avoid a
911 brute-force search for a combination of true name and module name.
912 While symtree names, the name that a particular symbol is known by
913 can changed with USE statements, we still have to keep track of the
914 true names to generate the correct reference, and also avoid
915 loading the same real symbol twice in a program unit.
917 When we start reading, the true name tree is built and maintained
918 as symbols are read. The tree is searched as we load new symbols
919 to see if it already exists someplace in the namespace. */
921 typedef struct true_name
923 BBT_HEADER (true_name);
924 const char *name;
925 gfc_symbol *sym;
927 true_name;
929 static true_name *true_name_root;
932 /* Compare two true_name structures. */
934 static int
935 compare_true_names (void *_t1, void *_t2)
937 true_name *t1, *t2;
938 int c;
940 t1 = (true_name *) _t1;
941 t2 = (true_name *) _t2;
943 c = ((t1->sym->module > t2->sym->module)
944 - (t1->sym->module < t2->sym->module));
945 if (c != 0)
946 return c;
948 return strcmp (t1->name, t2->name);
952 /* Given a true name, search the true name tree to see if it exists
953 within the main namespace. */
955 static gfc_symbol *
956 find_true_name (const char *name, const char *module)
958 true_name t, *p;
959 gfc_symbol sym;
960 int c;
962 t.name = gfc_get_string (name);
963 if (module != NULL)
964 sym.module = gfc_get_string (module);
965 else
966 sym.module = NULL;
967 t.sym = &sym;
969 p = true_name_root;
970 while (p != NULL)
972 c = compare_true_names ((void *) (&t), (void *) p);
973 if (c == 0)
974 return p->sym;
976 p = (c < 0) ? p->left : p->right;
979 return NULL;
983 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
985 static void
986 add_true_name (gfc_symbol *sym)
988 true_name *t;
990 t = XCNEW (true_name);
991 t->sym = sym;
992 if (gfc_fl_struct (sym->attr.flavor))
993 t->name = gfc_dt_upper_string (sym->name);
994 else
995 t->name = sym->name;
997 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1001 /* Recursive function to build the initial true name tree by
1002 recursively traversing the current namespace. */
1004 static void
1005 build_tnt (gfc_symtree *st)
1007 const char *name;
1008 if (st == NULL)
1009 return;
1011 build_tnt (st->left);
1012 build_tnt (st->right);
1014 if (gfc_fl_struct (st->n.sym->attr.flavor))
1015 name = gfc_dt_upper_string (st->n.sym->name);
1016 else
1017 name = st->n.sym->name;
1019 if (find_true_name (name, st->n.sym->module) != NULL)
1020 return;
1022 add_true_name (st->n.sym);
1026 /* Initialize the true name tree with the current namespace. */
1028 static void
1029 init_true_name_tree (void)
1031 true_name_root = NULL;
1032 build_tnt (gfc_current_ns->sym_root);
1036 /* Recursively free a true name tree node. */
1038 static void
1039 free_true_name (true_name *t)
1041 if (t == NULL)
1042 return;
1043 free_true_name (t->left);
1044 free_true_name (t->right);
1046 free (t);
1050 /*****************************************************************/
1052 /* Module reading and writing. */
1054 /* The following are versions similar to the ones in scanner.c, but
1055 for dealing with compressed module files. */
1057 static gzFile
1058 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1059 bool module, bool system)
1061 char *fullname;
1062 gfc_directorylist *p;
1063 gzFile f;
1065 for (p = list; p; p = p->next)
1067 if (module && !p->use_for_modules)
1068 continue;
1070 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
1071 strcpy (fullname, p->path);
1072 strcat (fullname, name);
1074 f = gzopen (fullname, "r");
1075 if (f != NULL)
1077 if (gfc_cpp_makedep ())
1078 gfc_cpp_add_dep (fullname, system);
1080 return f;
1084 return NULL;
1087 static gzFile
1088 gzopen_included_file (const char *name, bool include_cwd, bool module)
1090 gzFile f = NULL;
1092 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1094 f = gzopen (name, "r");
1095 if (f && gfc_cpp_makedep ())
1096 gfc_cpp_add_dep (name, false);
1099 if (!f)
1100 f = gzopen_included_file_1 (name, include_dirs, module, false);
1102 return f;
1105 static gzFile
1106 gzopen_intrinsic_module (const char* name)
1108 gzFile f = NULL;
1110 if (IS_ABSOLUTE_PATH (name))
1112 f = gzopen (name, "r");
1113 if (f && gfc_cpp_makedep ())
1114 gfc_cpp_add_dep (name, true);
1117 if (!f)
1118 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1120 return f;
1124 enum atom_type
1126 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1129 static atom_type last_atom;
1132 /* The name buffer must be at least as long as a symbol name. Right
1133 now it's not clear how we're going to store numeric constants--
1134 probably as a hexadecimal string, since this will allow the exact
1135 number to be preserved (this can't be done by a decimal
1136 representation). Worry about that later. TODO! */
1138 #define MAX_ATOM_SIZE 100
1140 static int atom_int;
1141 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1144 /* Report problems with a module. Error reporting is not very
1145 elaborate, since this sorts of errors shouldn't really happen.
1146 This subroutine never returns. */
1148 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1150 static void
1151 bad_module (const char *msgid)
1153 XDELETEVEC (module_content);
1154 module_content = NULL;
1156 switch (iomode)
1158 case IO_INPUT:
1159 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1160 module_name, module_line, module_column, msgid);
1161 break;
1162 case IO_OUTPUT:
1163 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1164 module_name, module_line, module_column, msgid);
1165 break;
1166 default:
1167 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1168 module_name, module_line, module_column, msgid);
1169 break;
1174 /* Set the module's input pointer. */
1176 static void
1177 set_module_locus (module_locus *m)
1179 module_column = m->column;
1180 module_line = m->line;
1181 module_pos = m->pos;
1185 /* Get the module's input pointer so that we can restore it later. */
1187 static void
1188 get_module_locus (module_locus *m)
1190 m->column = module_column;
1191 m->line = module_line;
1192 m->pos = module_pos;
1196 /* Get the next character in the module, updating our reckoning of
1197 where we are. */
1199 static int
1200 module_char (void)
1202 const char c = module_content[module_pos++];
1203 if (c == '\0')
1204 bad_module ("Unexpected EOF");
1206 prev_module_line = module_line;
1207 prev_module_column = module_column;
1209 if (c == '\n')
1211 module_line++;
1212 module_column = 0;
1215 module_column++;
1216 return c;
1219 /* Unget a character while remembering the line and column. Works for
1220 a single character only. */
1222 static void
1223 module_unget_char (void)
1225 module_line = prev_module_line;
1226 module_column = prev_module_column;
1227 module_pos--;
1230 /* Parse a string constant. The delimiter is guaranteed to be a
1231 single quote. */
1233 static void
1234 parse_string (void)
1236 int c;
1237 size_t cursz = 30;
1238 size_t len = 0;
1240 atom_string = XNEWVEC (char, cursz);
1242 for ( ; ; )
1244 c = module_char ();
1246 if (c == '\'')
1248 int c2 = module_char ();
1249 if (c2 != '\'')
1251 module_unget_char ();
1252 break;
1256 if (len >= cursz)
1258 cursz *= 2;
1259 atom_string = XRESIZEVEC (char, atom_string, cursz);
1261 atom_string[len] = c;
1262 len++;
1265 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1266 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1270 /* Parse a small integer. */
1272 static void
1273 parse_integer (int c)
1275 atom_int = c - '0';
1277 for (;;)
1279 c = module_char ();
1280 if (!ISDIGIT (c))
1282 module_unget_char ();
1283 break;
1286 atom_int = 10 * atom_int + c - '0';
1287 if (atom_int > 99999999)
1288 bad_module ("Integer overflow");
1294 /* Parse a name. */
1296 static void
1297 parse_name (int c)
1299 char *p;
1300 int len;
1302 p = atom_name;
1304 *p++ = c;
1305 len = 1;
1307 for (;;)
1309 c = module_char ();
1310 if (!ISALNUM (c) && c != '_' && c != '-')
1312 module_unget_char ();
1313 break;
1316 *p++ = c;
1317 if (++len > GFC_MAX_SYMBOL_LEN)
1318 bad_module ("Name too long");
1321 *p = '\0';
1326 /* Read the next atom in the module's input stream. */
1328 static atom_type
1329 parse_atom (void)
1331 int c;
1335 c = module_char ();
1337 while (c == ' ' || c == '\r' || c == '\n');
1339 switch (c)
1341 case '(':
1342 return ATOM_LPAREN;
1344 case ')':
1345 return ATOM_RPAREN;
1347 case '\'':
1348 parse_string ();
1349 return ATOM_STRING;
1351 case '0':
1352 case '1':
1353 case '2':
1354 case '3':
1355 case '4':
1356 case '5':
1357 case '6':
1358 case '7':
1359 case '8':
1360 case '9':
1361 parse_integer (c);
1362 return ATOM_INTEGER;
1364 case 'a':
1365 case 'b':
1366 case 'c':
1367 case 'd':
1368 case 'e':
1369 case 'f':
1370 case 'g':
1371 case 'h':
1372 case 'i':
1373 case 'j':
1374 case 'k':
1375 case 'l':
1376 case 'm':
1377 case 'n':
1378 case 'o':
1379 case 'p':
1380 case 'q':
1381 case 'r':
1382 case 's':
1383 case 't':
1384 case 'u':
1385 case 'v':
1386 case 'w':
1387 case 'x':
1388 case 'y':
1389 case 'z':
1390 case 'A':
1391 case 'B':
1392 case 'C':
1393 case 'D':
1394 case 'E':
1395 case 'F':
1396 case 'G':
1397 case 'H':
1398 case 'I':
1399 case 'J':
1400 case 'K':
1401 case 'L':
1402 case 'M':
1403 case 'N':
1404 case 'O':
1405 case 'P':
1406 case 'Q':
1407 case 'R':
1408 case 'S':
1409 case 'T':
1410 case 'U':
1411 case 'V':
1412 case 'W':
1413 case 'X':
1414 case 'Y':
1415 case 'Z':
1416 parse_name (c);
1417 return ATOM_NAME;
1419 default:
1420 bad_module ("Bad name");
1423 /* Not reached. */
1427 /* Peek at the next atom on the input. */
1429 static atom_type
1430 peek_atom (void)
1432 int c;
1436 c = module_char ();
1438 while (c == ' ' || c == '\r' || c == '\n');
1440 switch (c)
1442 case '(':
1443 module_unget_char ();
1444 return ATOM_LPAREN;
1446 case ')':
1447 module_unget_char ();
1448 return ATOM_RPAREN;
1450 case '\'':
1451 module_unget_char ();
1452 return ATOM_STRING;
1454 case '0':
1455 case '1':
1456 case '2':
1457 case '3':
1458 case '4':
1459 case '5':
1460 case '6':
1461 case '7':
1462 case '8':
1463 case '9':
1464 module_unget_char ();
1465 return ATOM_INTEGER;
1467 case 'a':
1468 case 'b':
1469 case 'c':
1470 case 'd':
1471 case 'e':
1472 case 'f':
1473 case 'g':
1474 case 'h':
1475 case 'i':
1476 case 'j':
1477 case 'k':
1478 case 'l':
1479 case 'm':
1480 case 'n':
1481 case 'o':
1482 case 'p':
1483 case 'q':
1484 case 'r':
1485 case 's':
1486 case 't':
1487 case 'u':
1488 case 'v':
1489 case 'w':
1490 case 'x':
1491 case 'y':
1492 case 'z':
1493 case 'A':
1494 case 'B':
1495 case 'C':
1496 case 'D':
1497 case 'E':
1498 case 'F':
1499 case 'G':
1500 case 'H':
1501 case 'I':
1502 case 'J':
1503 case 'K':
1504 case 'L':
1505 case 'M':
1506 case 'N':
1507 case 'O':
1508 case 'P':
1509 case 'Q':
1510 case 'R':
1511 case 'S':
1512 case 'T':
1513 case 'U':
1514 case 'V':
1515 case 'W':
1516 case 'X':
1517 case 'Y':
1518 case 'Z':
1519 module_unget_char ();
1520 return ATOM_NAME;
1522 default:
1523 bad_module ("Bad name");
1528 /* Read the next atom from the input, requiring that it be a
1529 particular kind. */
1531 static void
1532 require_atom (atom_type type)
1534 atom_type t;
1535 const char *p;
1536 int column, line;
1538 column = module_column;
1539 line = module_line;
1541 t = parse_atom ();
1542 if (t != type)
1544 switch (type)
1546 case ATOM_NAME:
1547 p = _("Expected name");
1548 break;
1549 case ATOM_LPAREN:
1550 p = _("Expected left parenthesis");
1551 break;
1552 case ATOM_RPAREN:
1553 p = _("Expected right parenthesis");
1554 break;
1555 case ATOM_INTEGER:
1556 p = _("Expected integer");
1557 break;
1558 case ATOM_STRING:
1559 p = _("Expected string");
1560 break;
1561 default:
1562 gfc_internal_error ("require_atom(): bad atom type required");
1565 module_column = column;
1566 module_line = line;
1567 bad_module (p);
1572 /* Given a pointer to an mstring array, require that the current input
1573 be one of the strings in the array. We return the enum value. */
1575 static int
1576 find_enum (const mstring *m)
1578 int i;
1580 i = gfc_string2code (m, atom_name);
1581 if (i >= 0)
1582 return i;
1584 bad_module ("find_enum(): Enum not found");
1586 /* Not reached. */
1590 /* Read a string. The caller is responsible for freeing. */
1592 static char*
1593 read_string (void)
1595 char* p;
1596 require_atom (ATOM_STRING);
1597 p = atom_string;
1598 atom_string = NULL;
1599 return p;
1603 /**************** Module output subroutines ***************************/
1605 /* Output a character to a module file. */
1607 static void
1608 write_char (char out)
1610 if (gzputc (module_fp, out) == EOF)
1611 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1613 if (out != '\n')
1614 module_column++;
1615 else
1617 module_column = 1;
1618 module_line++;
1623 /* Write an atom to a module. The line wrapping isn't perfect, but it
1624 should work most of the time. This isn't that big of a deal, since
1625 the file really isn't meant to be read by people anyway. */
1627 static void
1628 write_atom (atom_type atom, const void *v)
1630 char buffer[20];
1632 /* Workaround -Wmaybe-uninitialized false positive during
1633 profiledbootstrap by initializing them. */
1634 int i = 0, len;
1635 const char *p;
1637 switch (atom)
1639 case ATOM_STRING:
1640 case ATOM_NAME:
1641 p = (const char *) v;
1642 break;
1644 case ATOM_LPAREN:
1645 p = "(";
1646 break;
1648 case ATOM_RPAREN:
1649 p = ")";
1650 break;
1652 case ATOM_INTEGER:
1653 i = *((const int *) v);
1654 if (i < 0)
1655 gfc_internal_error ("write_atom(): Writing negative integer");
1657 sprintf (buffer, "%d", i);
1658 p = buffer;
1659 break;
1661 default:
1662 gfc_internal_error ("write_atom(): Trying to write dab atom");
1666 if(p == NULL || *p == '\0')
1667 len = 0;
1668 else
1669 len = strlen (p);
1671 if (atom != ATOM_RPAREN)
1673 if (module_column + len > 72)
1674 write_char ('\n');
1675 else
1678 if (last_atom != ATOM_LPAREN && module_column != 1)
1679 write_char (' ');
1683 if (atom == ATOM_STRING)
1684 write_char ('\'');
1686 while (p != NULL && *p)
1688 if (atom == ATOM_STRING && *p == '\'')
1689 write_char ('\'');
1690 write_char (*p++);
1693 if (atom == ATOM_STRING)
1694 write_char ('\'');
1696 last_atom = atom;
1701 /***************** Mid-level I/O subroutines *****************/
1703 /* These subroutines let their caller read or write atoms without
1704 caring about which of the two is actually happening. This lets a
1705 subroutine concentrate on the actual format of the data being
1706 written. */
1708 static void mio_expr (gfc_expr **);
1709 pointer_info *mio_symbol_ref (gfc_symbol **);
1710 pointer_info *mio_interface_rest (gfc_interface **);
1711 static void mio_symtree_ref (gfc_symtree **);
1713 /* Read or write an enumerated value. On writing, we return the input
1714 value for the convenience of callers. We avoid using an integer
1715 pointer because enums are sometimes inside bitfields. */
1717 static int
1718 mio_name (int t, const mstring *m)
1720 if (iomode == IO_OUTPUT)
1721 write_atom (ATOM_NAME, gfc_code2string (m, t));
1722 else
1724 require_atom (ATOM_NAME);
1725 t = find_enum (m);
1728 return t;
1731 /* Specialization of mio_name. */
1733 #define DECL_MIO_NAME(TYPE) \
1734 static inline TYPE \
1735 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1737 return (TYPE) mio_name ((int) t, m); \
1739 #define MIO_NAME(TYPE) mio_name_##TYPE
1741 static void
1742 mio_lparen (void)
1744 if (iomode == IO_OUTPUT)
1745 write_atom (ATOM_LPAREN, NULL);
1746 else
1747 require_atom (ATOM_LPAREN);
1751 static void
1752 mio_rparen (void)
1754 if (iomode == IO_OUTPUT)
1755 write_atom (ATOM_RPAREN, NULL);
1756 else
1757 require_atom (ATOM_RPAREN);
1761 static void
1762 mio_integer (int *ip)
1764 if (iomode == IO_OUTPUT)
1765 write_atom (ATOM_INTEGER, ip);
1766 else
1768 require_atom (ATOM_INTEGER);
1769 *ip = atom_int;
1774 /* Read or write a gfc_intrinsic_op value. */
1776 static void
1777 mio_intrinsic_op (gfc_intrinsic_op* op)
1779 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1780 if (iomode == IO_OUTPUT)
1782 int converted = (int) *op;
1783 write_atom (ATOM_INTEGER, &converted);
1785 else
1787 require_atom (ATOM_INTEGER);
1788 *op = (gfc_intrinsic_op) atom_int;
1793 /* Read or write a character pointer that points to a string on the heap. */
1795 static const char *
1796 mio_allocated_string (const char *s)
1798 if (iomode == IO_OUTPUT)
1800 write_atom (ATOM_STRING, s);
1801 return s;
1803 else
1805 require_atom (ATOM_STRING);
1806 return atom_string;
1811 /* Functions for quoting and unquoting strings. */
1813 static char *
1814 quote_string (const gfc_char_t *s, const size_t slength)
1816 const gfc_char_t *p;
1817 char *res, *q;
1818 size_t len = 0, i;
1820 /* Calculate the length we'll need: a backslash takes two ("\\"),
1821 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1822 for (p = s, i = 0; i < slength; p++, i++)
1824 if (*p == '\\')
1825 len += 2;
1826 else if (!gfc_wide_is_printable (*p))
1827 len += 10;
1828 else
1829 len++;
1832 q = res = XCNEWVEC (char, len + 1);
1833 for (p = s, i = 0; i < slength; p++, i++)
1835 if (*p == '\\')
1836 *q++ = '\\', *q++ = '\\';
1837 else if (!gfc_wide_is_printable (*p))
1839 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1840 (unsigned HOST_WIDE_INT) *p);
1841 q += 10;
1843 else
1844 *q++ = (unsigned char) *p;
1847 res[len] = '\0';
1848 return res;
1851 static gfc_char_t *
1852 unquote_string (const char *s)
1854 size_t len, i;
1855 const char *p;
1856 gfc_char_t *res;
1858 for (p = s, len = 0; *p; p++, len++)
1860 if (*p != '\\')
1861 continue;
1863 if (p[1] == '\\')
1864 p++;
1865 else if (p[1] == 'U')
1866 p += 9; /* That is a "\U????????". */
1867 else
1868 gfc_internal_error ("unquote_string(): got bad string");
1871 res = gfc_get_wide_string (len + 1);
1872 for (i = 0, p = s; i < len; i++, p++)
1874 gcc_assert (*p);
1876 if (*p != '\\')
1877 res[i] = (unsigned char) *p;
1878 else if (p[1] == '\\')
1880 res[i] = (unsigned char) '\\';
1881 p++;
1883 else
1885 /* We read the 8-digits hexadecimal constant that follows. */
1886 int j;
1887 unsigned n;
1888 gfc_char_t c = 0;
1890 gcc_assert (p[1] == 'U');
1891 for (j = 0; j < 8; j++)
1893 c = c << 4;
1894 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1895 c += n;
1898 res[i] = c;
1899 p += 9;
1903 res[len] = '\0';
1904 return res;
1908 /* Read or write a character pointer that points to a wide string on the
1909 heap, performing quoting/unquoting of nonprintable characters using the
1910 form \U???????? (where each ? is a hexadecimal digit).
1911 Length is the length of the string, only known and used in output mode. */
1913 static const gfc_char_t *
1914 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1916 if (iomode == IO_OUTPUT)
1918 char *quoted = quote_string (s, length);
1919 write_atom (ATOM_STRING, quoted);
1920 free (quoted);
1921 return s;
1923 else
1925 gfc_char_t *unquoted;
1927 require_atom (ATOM_STRING);
1928 unquoted = unquote_string (atom_string);
1929 free (atom_string);
1930 return unquoted;
1935 /* Read or write a string that is in static memory. */
1937 static void
1938 mio_pool_string (const char **stringp)
1940 /* TODO: one could write the string only once, and refer to it via a
1941 fixup pointer. */
1943 /* As a special case we have to deal with a NULL string. This
1944 happens for the 'module' member of 'gfc_symbol's that are not in a
1945 module. We read / write these as the empty string. */
1946 if (iomode == IO_OUTPUT)
1948 const char *p = *stringp == NULL ? "" : *stringp;
1949 write_atom (ATOM_STRING, p);
1951 else
1953 require_atom (ATOM_STRING);
1954 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1955 free (atom_string);
1960 /* Read or write a string that is inside of some already-allocated
1961 structure. */
1963 static void
1964 mio_internal_string (char *string)
1966 if (iomode == IO_OUTPUT)
1967 write_atom (ATOM_STRING, string);
1968 else
1970 require_atom (ATOM_STRING);
1971 strcpy (string, atom_string);
1972 free (atom_string);
1977 enum ab_attribute
1978 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1979 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1980 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1981 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1982 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1983 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1984 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
1985 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1986 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1987 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1988 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
1989 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
1990 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
1991 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK
1994 static const mstring attr_bits[] =
1996 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1997 minit ("ARTIFICIAL", AB_ARTIFICIAL),
1998 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1999 minit ("DIMENSION", AB_DIMENSION),
2000 minit ("CODIMENSION", AB_CODIMENSION),
2001 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2002 minit ("EXTERNAL", AB_EXTERNAL),
2003 minit ("INTRINSIC", AB_INTRINSIC),
2004 minit ("OPTIONAL", AB_OPTIONAL),
2005 minit ("POINTER", AB_POINTER),
2006 minit ("VOLATILE", AB_VOLATILE),
2007 minit ("TARGET", AB_TARGET),
2008 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2009 minit ("DUMMY", AB_DUMMY),
2010 minit ("RESULT", AB_RESULT),
2011 minit ("DATA", AB_DATA),
2012 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2013 minit ("IN_COMMON", AB_IN_COMMON),
2014 minit ("FUNCTION", AB_FUNCTION),
2015 minit ("SUBROUTINE", AB_SUBROUTINE),
2016 minit ("SEQUENCE", AB_SEQUENCE),
2017 minit ("ELEMENTAL", AB_ELEMENTAL),
2018 minit ("PURE", AB_PURE),
2019 minit ("RECURSIVE", AB_RECURSIVE),
2020 minit ("GENERIC", AB_GENERIC),
2021 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2022 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2023 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2024 minit ("IS_BIND_C", AB_IS_BIND_C),
2025 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2026 minit ("IS_ISO_C", AB_IS_ISO_C),
2027 minit ("VALUE", AB_VALUE),
2028 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2029 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2030 minit ("LOCK_COMP", AB_LOCK_COMP),
2031 minit ("EVENT_COMP", AB_EVENT_COMP),
2032 minit ("POINTER_COMP", AB_POINTER_COMP),
2033 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2034 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2035 minit ("ZERO_COMP", AB_ZERO_COMP),
2036 minit ("PROTECTED", AB_PROTECTED),
2037 minit ("ABSTRACT", AB_ABSTRACT),
2038 minit ("IS_CLASS", AB_IS_CLASS),
2039 minit ("PROCEDURE", AB_PROCEDURE),
2040 minit ("PROC_POINTER", AB_PROC_POINTER),
2041 minit ("VTYPE", AB_VTYPE),
2042 minit ("VTAB", AB_VTAB),
2043 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2044 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2045 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2046 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2047 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2048 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2049 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2050 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2051 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2052 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2053 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2054 minit (NULL, -1)
2057 /* For binding attributes. */
2058 static const mstring binding_passing[] =
2060 minit ("PASS", 0),
2061 minit ("NOPASS", 1),
2062 minit (NULL, -1)
2064 static const mstring binding_overriding[] =
2066 minit ("OVERRIDABLE", 0),
2067 minit ("NON_OVERRIDABLE", 1),
2068 minit ("DEFERRED", 2),
2069 minit (NULL, -1)
2071 static const mstring binding_generic[] =
2073 minit ("SPECIFIC", 0),
2074 minit ("GENERIC", 1),
2075 minit (NULL, -1)
2077 static const mstring binding_ppc[] =
2079 minit ("NO_PPC", 0),
2080 minit ("PPC", 1),
2081 minit (NULL, -1)
2084 /* Specialization of mio_name. */
2085 DECL_MIO_NAME (ab_attribute)
2086 DECL_MIO_NAME (ar_type)
2087 DECL_MIO_NAME (array_type)
2088 DECL_MIO_NAME (bt)
2089 DECL_MIO_NAME (expr_t)
2090 DECL_MIO_NAME (gfc_access)
2091 DECL_MIO_NAME (gfc_intrinsic_op)
2092 DECL_MIO_NAME (ifsrc)
2093 DECL_MIO_NAME (save_state)
2094 DECL_MIO_NAME (procedure_type)
2095 DECL_MIO_NAME (ref_type)
2096 DECL_MIO_NAME (sym_flavor)
2097 DECL_MIO_NAME (sym_intent)
2098 #undef DECL_MIO_NAME
2100 /* Symbol attributes are stored in list with the first three elements
2101 being the enumerated fields, while the remaining elements (if any)
2102 indicate the individual attribute bits. The access field is not
2103 saved-- it controls what symbols are exported when a module is
2104 written. */
2106 static void
2107 mio_symbol_attribute (symbol_attribute *attr)
2109 atom_type t;
2110 unsigned ext_attr,extension_level;
2112 mio_lparen ();
2114 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2115 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2116 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2117 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2118 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2120 ext_attr = attr->ext_attr;
2121 mio_integer ((int *) &ext_attr);
2122 attr->ext_attr = ext_attr;
2124 extension_level = attr->extension;
2125 mio_integer ((int *) &extension_level);
2126 attr->extension = extension_level;
2128 if (iomode == IO_OUTPUT)
2130 if (attr->allocatable)
2131 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2132 if (attr->artificial)
2133 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2134 if (attr->asynchronous)
2135 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2136 if (attr->dimension)
2137 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2138 if (attr->codimension)
2139 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2140 if (attr->contiguous)
2141 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2142 if (attr->external)
2143 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2144 if (attr->intrinsic)
2145 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2146 if (attr->optional)
2147 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2148 if (attr->pointer)
2149 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2150 if (attr->class_pointer)
2151 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2152 if (attr->is_protected)
2153 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2154 if (attr->value)
2155 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2156 if (attr->volatile_)
2157 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2158 if (attr->target)
2159 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2160 if (attr->threadprivate)
2161 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2162 if (attr->dummy)
2163 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2164 if (attr->result)
2165 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2166 /* We deliberately don't preserve the "entry" flag. */
2168 if (attr->data)
2169 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2170 if (attr->in_namelist)
2171 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2172 if (attr->in_common)
2173 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2175 if (attr->function)
2176 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2177 if (attr->subroutine)
2178 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2179 if (attr->generic)
2180 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2181 if (attr->abstract)
2182 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2184 if (attr->sequence)
2185 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2186 if (attr->elemental)
2187 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2188 if (attr->pure)
2189 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2190 if (attr->implicit_pure)
2191 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2192 if (attr->unlimited_polymorphic)
2193 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2194 if (attr->recursive)
2195 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2196 if (attr->always_explicit)
2197 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2198 if (attr->cray_pointer)
2199 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2200 if (attr->cray_pointee)
2201 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2202 if (attr->is_bind_c)
2203 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2204 if (attr->is_c_interop)
2205 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2206 if (attr->is_iso_c)
2207 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2208 if (attr->alloc_comp)
2209 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2210 if (attr->pointer_comp)
2211 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2212 if (attr->proc_pointer_comp)
2213 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2214 if (attr->private_comp)
2215 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2216 if (attr->coarray_comp)
2217 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2218 if (attr->lock_comp)
2219 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2220 if (attr->event_comp)
2221 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2222 if (attr->zero_comp)
2223 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2224 if (attr->is_class)
2225 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2226 if (attr->procedure)
2227 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2228 if (attr->proc_pointer)
2229 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2230 if (attr->vtype)
2231 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2232 if (attr->vtab)
2233 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2234 if (attr->omp_declare_target)
2235 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2236 if (attr->array_outer_dependency)
2237 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2238 if (attr->module_procedure)
2240 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2241 no_module_procedures = false;
2243 if (attr->oacc_declare_create)
2244 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2245 if (attr->oacc_declare_copyin)
2246 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2247 if (attr->oacc_declare_deviceptr)
2248 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2249 if (attr->oacc_declare_device_resident)
2250 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2251 if (attr->oacc_declare_link)
2252 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2254 mio_rparen ();
2257 else
2259 for (;;)
2261 t = parse_atom ();
2262 if (t == ATOM_RPAREN)
2263 break;
2264 if (t != ATOM_NAME)
2265 bad_module ("Expected attribute bit name");
2267 switch ((ab_attribute) find_enum (attr_bits))
2269 case AB_ALLOCATABLE:
2270 attr->allocatable = 1;
2271 break;
2272 case AB_ARTIFICIAL:
2273 attr->artificial = 1;
2274 break;
2275 case AB_ASYNCHRONOUS:
2276 attr->asynchronous = 1;
2277 break;
2278 case AB_DIMENSION:
2279 attr->dimension = 1;
2280 break;
2281 case AB_CODIMENSION:
2282 attr->codimension = 1;
2283 break;
2284 case AB_CONTIGUOUS:
2285 attr->contiguous = 1;
2286 break;
2287 case AB_EXTERNAL:
2288 attr->external = 1;
2289 break;
2290 case AB_INTRINSIC:
2291 attr->intrinsic = 1;
2292 break;
2293 case AB_OPTIONAL:
2294 attr->optional = 1;
2295 break;
2296 case AB_POINTER:
2297 attr->pointer = 1;
2298 break;
2299 case AB_CLASS_POINTER:
2300 attr->class_pointer = 1;
2301 break;
2302 case AB_PROTECTED:
2303 attr->is_protected = 1;
2304 break;
2305 case AB_VALUE:
2306 attr->value = 1;
2307 break;
2308 case AB_VOLATILE:
2309 attr->volatile_ = 1;
2310 break;
2311 case AB_TARGET:
2312 attr->target = 1;
2313 break;
2314 case AB_THREADPRIVATE:
2315 attr->threadprivate = 1;
2316 break;
2317 case AB_DUMMY:
2318 attr->dummy = 1;
2319 break;
2320 case AB_RESULT:
2321 attr->result = 1;
2322 break;
2323 case AB_DATA:
2324 attr->data = 1;
2325 break;
2326 case AB_IN_NAMELIST:
2327 attr->in_namelist = 1;
2328 break;
2329 case AB_IN_COMMON:
2330 attr->in_common = 1;
2331 break;
2332 case AB_FUNCTION:
2333 attr->function = 1;
2334 break;
2335 case AB_SUBROUTINE:
2336 attr->subroutine = 1;
2337 break;
2338 case AB_GENERIC:
2339 attr->generic = 1;
2340 break;
2341 case AB_ABSTRACT:
2342 attr->abstract = 1;
2343 break;
2344 case AB_SEQUENCE:
2345 attr->sequence = 1;
2346 break;
2347 case AB_ELEMENTAL:
2348 attr->elemental = 1;
2349 break;
2350 case AB_PURE:
2351 attr->pure = 1;
2352 break;
2353 case AB_IMPLICIT_PURE:
2354 attr->implicit_pure = 1;
2355 break;
2356 case AB_UNLIMITED_POLY:
2357 attr->unlimited_polymorphic = 1;
2358 break;
2359 case AB_RECURSIVE:
2360 attr->recursive = 1;
2361 break;
2362 case AB_ALWAYS_EXPLICIT:
2363 attr->always_explicit = 1;
2364 break;
2365 case AB_CRAY_POINTER:
2366 attr->cray_pointer = 1;
2367 break;
2368 case AB_CRAY_POINTEE:
2369 attr->cray_pointee = 1;
2370 break;
2371 case AB_IS_BIND_C:
2372 attr->is_bind_c = 1;
2373 break;
2374 case AB_IS_C_INTEROP:
2375 attr->is_c_interop = 1;
2376 break;
2377 case AB_IS_ISO_C:
2378 attr->is_iso_c = 1;
2379 break;
2380 case AB_ALLOC_COMP:
2381 attr->alloc_comp = 1;
2382 break;
2383 case AB_COARRAY_COMP:
2384 attr->coarray_comp = 1;
2385 break;
2386 case AB_LOCK_COMP:
2387 attr->lock_comp = 1;
2388 break;
2389 case AB_EVENT_COMP:
2390 attr->event_comp = 1;
2391 break;
2392 case AB_POINTER_COMP:
2393 attr->pointer_comp = 1;
2394 break;
2395 case AB_PROC_POINTER_COMP:
2396 attr->proc_pointer_comp = 1;
2397 break;
2398 case AB_PRIVATE_COMP:
2399 attr->private_comp = 1;
2400 break;
2401 case AB_ZERO_COMP:
2402 attr->zero_comp = 1;
2403 break;
2404 case AB_IS_CLASS:
2405 attr->is_class = 1;
2406 break;
2407 case AB_PROCEDURE:
2408 attr->procedure = 1;
2409 break;
2410 case AB_PROC_POINTER:
2411 attr->proc_pointer = 1;
2412 break;
2413 case AB_VTYPE:
2414 attr->vtype = 1;
2415 break;
2416 case AB_VTAB:
2417 attr->vtab = 1;
2418 break;
2419 case AB_OMP_DECLARE_TARGET:
2420 attr->omp_declare_target = 1;
2421 break;
2422 case AB_ARRAY_OUTER_DEPENDENCY:
2423 attr->array_outer_dependency =1;
2424 break;
2425 case AB_MODULE_PROCEDURE:
2426 attr->module_procedure =1;
2427 break;
2428 case AB_OACC_DECLARE_CREATE:
2429 attr->oacc_declare_create = 1;
2430 break;
2431 case AB_OACC_DECLARE_COPYIN:
2432 attr->oacc_declare_copyin = 1;
2433 break;
2434 case AB_OACC_DECLARE_DEVICEPTR:
2435 attr->oacc_declare_deviceptr = 1;
2436 break;
2437 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2438 attr->oacc_declare_device_resident = 1;
2439 break;
2440 case AB_OACC_DECLARE_LINK:
2441 attr->oacc_declare_link = 1;
2442 break;
2449 static const mstring bt_types[] = {
2450 minit ("INTEGER", BT_INTEGER),
2451 minit ("REAL", BT_REAL),
2452 minit ("COMPLEX", BT_COMPLEX),
2453 minit ("LOGICAL", BT_LOGICAL),
2454 minit ("CHARACTER", BT_CHARACTER),
2455 minit ("UNION", BT_UNION),
2456 minit ("DERIVED", BT_DERIVED),
2457 minit ("CLASS", BT_CLASS),
2458 minit ("PROCEDURE", BT_PROCEDURE),
2459 minit ("UNKNOWN", BT_UNKNOWN),
2460 minit ("VOID", BT_VOID),
2461 minit ("ASSUMED", BT_ASSUMED),
2462 minit (NULL, -1)
2466 static void
2467 mio_charlen (gfc_charlen **clp)
2469 gfc_charlen *cl;
2471 mio_lparen ();
2473 if (iomode == IO_OUTPUT)
2475 cl = *clp;
2476 if (cl != NULL)
2477 mio_expr (&cl->length);
2479 else
2481 if (peek_atom () != ATOM_RPAREN)
2483 cl = gfc_new_charlen (gfc_current_ns, NULL);
2484 mio_expr (&cl->length);
2485 *clp = cl;
2489 mio_rparen ();
2493 /* See if a name is a generated name. */
2495 static int
2496 check_unique_name (const char *name)
2498 return *name == '@';
2502 static void
2503 mio_typespec (gfc_typespec *ts)
2505 mio_lparen ();
2507 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2509 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2510 mio_integer (&ts->kind);
2511 else
2512 mio_symbol_ref (&ts->u.derived);
2514 mio_symbol_ref (&ts->interface);
2516 /* Add info for C interop and is_iso_c. */
2517 mio_integer (&ts->is_c_interop);
2518 mio_integer (&ts->is_iso_c);
2520 /* If the typespec is for an identifier either from iso_c_binding, or
2521 a constant that was initialized to an identifier from it, use the
2522 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2523 if (ts->is_iso_c)
2524 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2525 else
2526 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2528 if (ts->type != BT_CHARACTER)
2530 /* ts->u.cl is only valid for BT_CHARACTER. */
2531 mio_lparen ();
2532 mio_rparen ();
2534 else
2535 mio_charlen (&ts->u.cl);
2537 /* So as not to disturb the existing API, use an ATOM_NAME to
2538 transmit deferred characteristic for characters (F2003). */
2539 if (iomode == IO_OUTPUT)
2541 if (ts->type == BT_CHARACTER && ts->deferred)
2542 write_atom (ATOM_NAME, "DEFERRED_CL");
2544 else if (peek_atom () != ATOM_RPAREN)
2546 if (parse_atom () != ATOM_NAME)
2547 bad_module ("Expected string");
2548 ts->deferred = 1;
2551 mio_rparen ();
2555 static const mstring array_spec_types[] = {
2556 minit ("EXPLICIT", AS_EXPLICIT),
2557 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2558 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2559 minit ("DEFERRED", AS_DEFERRED),
2560 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2561 minit (NULL, -1)
2565 static void
2566 mio_array_spec (gfc_array_spec **asp)
2568 gfc_array_spec *as;
2569 int i;
2571 mio_lparen ();
2573 if (iomode == IO_OUTPUT)
2575 int rank;
2577 if (*asp == NULL)
2578 goto done;
2579 as = *asp;
2581 /* mio_integer expects nonnegative values. */
2582 rank = as->rank > 0 ? as->rank : 0;
2583 mio_integer (&rank);
2585 else
2587 if (peek_atom () == ATOM_RPAREN)
2589 *asp = NULL;
2590 goto done;
2593 *asp = as = gfc_get_array_spec ();
2594 mio_integer (&as->rank);
2597 mio_integer (&as->corank);
2598 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2600 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2601 as->rank = -1;
2602 if (iomode == IO_INPUT && as->corank)
2603 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2605 if (as->rank + as->corank > 0)
2606 for (i = 0; i < as->rank + as->corank; i++)
2608 mio_expr (&as->lower[i]);
2609 mio_expr (&as->upper[i]);
2612 done:
2613 mio_rparen ();
2617 /* Given a pointer to an array reference structure (which lives in a
2618 gfc_ref structure), find the corresponding array specification
2619 structure. Storing the pointer in the ref structure doesn't quite
2620 work when loading from a module. Generating code for an array
2621 reference also needs more information than just the array spec. */
2623 static const mstring array_ref_types[] = {
2624 minit ("FULL", AR_FULL),
2625 minit ("ELEMENT", AR_ELEMENT),
2626 minit ("SECTION", AR_SECTION),
2627 minit (NULL, -1)
2631 static void
2632 mio_array_ref (gfc_array_ref *ar)
2634 int i;
2636 mio_lparen ();
2637 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2638 mio_integer (&ar->dimen);
2640 switch (ar->type)
2642 case AR_FULL:
2643 break;
2645 case AR_ELEMENT:
2646 for (i = 0; i < ar->dimen; i++)
2647 mio_expr (&ar->start[i]);
2649 break;
2651 case AR_SECTION:
2652 for (i = 0; i < ar->dimen; i++)
2654 mio_expr (&ar->start[i]);
2655 mio_expr (&ar->end[i]);
2656 mio_expr (&ar->stride[i]);
2659 break;
2661 case AR_UNKNOWN:
2662 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2665 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2666 we can't call mio_integer directly. Instead loop over each element
2667 and cast it to/from an integer. */
2668 if (iomode == IO_OUTPUT)
2670 for (i = 0; i < ar->dimen; i++)
2672 int tmp = (int)ar->dimen_type[i];
2673 write_atom (ATOM_INTEGER, &tmp);
2676 else
2678 for (i = 0; i < ar->dimen; i++)
2680 require_atom (ATOM_INTEGER);
2681 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2685 if (iomode == IO_INPUT)
2687 ar->where = gfc_current_locus;
2689 for (i = 0; i < ar->dimen; i++)
2690 ar->c_where[i] = gfc_current_locus;
2693 mio_rparen ();
2697 /* Saves or restores a pointer. The pointer is converted back and
2698 forth from an integer. We return the pointer_info pointer so that
2699 the caller can take additional action based on the pointer type. */
2701 static pointer_info *
2702 mio_pointer_ref (void *gp)
2704 pointer_info *p;
2706 if (iomode == IO_OUTPUT)
2708 p = get_pointer (*((char **) gp));
2709 write_atom (ATOM_INTEGER, &p->integer);
2711 else
2713 require_atom (ATOM_INTEGER);
2714 p = add_fixup (atom_int, gp);
2717 return p;
2721 /* Save and load references to components that occur within
2722 expressions. We have to describe these references by a number and
2723 by name. The number is necessary for forward references during
2724 reading, and the name is necessary if the symbol already exists in
2725 the namespace and is not loaded again. */
2727 static void
2728 mio_component_ref (gfc_component **cp)
2730 pointer_info *p;
2732 p = mio_pointer_ref (cp);
2733 if (p->type == P_UNKNOWN)
2734 p->type = P_COMPONENT;
2738 static void mio_namespace_ref (gfc_namespace **nsp);
2739 static void mio_formal_arglist (gfc_formal_arglist **formal);
2740 static void mio_typebound_proc (gfc_typebound_proc** proc);
2742 static void
2743 mio_component (gfc_component *c, int vtype)
2745 pointer_info *p;
2746 int n;
2748 mio_lparen ();
2750 if (iomode == IO_OUTPUT)
2752 p = get_pointer (c);
2753 mio_integer (&p->integer);
2755 else
2757 mio_integer (&n);
2758 p = get_integer (n);
2759 associate_integer_pointer (p, c);
2762 if (p->type == P_UNKNOWN)
2763 p->type = P_COMPONENT;
2765 mio_pool_string (&c->name);
2766 mio_typespec (&c->ts);
2767 mio_array_spec (&c->as);
2769 mio_symbol_attribute (&c->attr);
2770 if (c->ts.type == BT_CLASS)
2771 c->attr.class_ok = 1;
2772 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2774 if (!vtype || strcmp (c->name, "_final") == 0
2775 || strcmp (c->name, "_hash") == 0)
2776 mio_expr (&c->initializer);
2778 if (c->attr.proc_pointer)
2779 mio_typebound_proc (&c->tb);
2781 mio_rparen ();
2785 static void
2786 mio_component_list (gfc_component **cp, int vtype)
2788 gfc_component *c, *tail;
2790 mio_lparen ();
2792 if (iomode == IO_OUTPUT)
2794 for (c = *cp; c; c = c->next)
2795 mio_component (c, vtype);
2797 else
2799 *cp = NULL;
2800 tail = NULL;
2802 for (;;)
2804 if (peek_atom () == ATOM_RPAREN)
2805 break;
2807 c = gfc_get_component ();
2808 mio_component (c, vtype);
2810 if (tail == NULL)
2811 *cp = c;
2812 else
2813 tail->next = c;
2815 tail = c;
2819 mio_rparen ();
2823 static void
2824 mio_actual_arg (gfc_actual_arglist *a)
2826 mio_lparen ();
2827 mio_pool_string (&a->name);
2828 mio_expr (&a->expr);
2829 mio_rparen ();
2833 static void
2834 mio_actual_arglist (gfc_actual_arglist **ap)
2836 gfc_actual_arglist *a, *tail;
2838 mio_lparen ();
2840 if (iomode == IO_OUTPUT)
2842 for (a = *ap; a; a = a->next)
2843 mio_actual_arg (a);
2846 else
2848 tail = NULL;
2850 for (;;)
2852 if (peek_atom () != ATOM_LPAREN)
2853 break;
2855 a = gfc_get_actual_arglist ();
2857 if (tail == NULL)
2858 *ap = a;
2859 else
2860 tail->next = a;
2862 tail = a;
2863 mio_actual_arg (a);
2867 mio_rparen ();
2871 /* Read and write formal argument lists. */
2873 static void
2874 mio_formal_arglist (gfc_formal_arglist **formal)
2876 gfc_formal_arglist *f, *tail;
2878 mio_lparen ();
2880 if (iomode == IO_OUTPUT)
2882 for (f = *formal; f; f = f->next)
2883 mio_symbol_ref (&f->sym);
2885 else
2887 *formal = tail = NULL;
2889 while (peek_atom () != ATOM_RPAREN)
2891 f = gfc_get_formal_arglist ();
2892 mio_symbol_ref (&f->sym);
2894 if (*formal == NULL)
2895 *formal = f;
2896 else
2897 tail->next = f;
2899 tail = f;
2903 mio_rparen ();
2907 /* Save or restore a reference to a symbol node. */
2909 pointer_info *
2910 mio_symbol_ref (gfc_symbol **symp)
2912 pointer_info *p;
2914 p = mio_pointer_ref (symp);
2915 if (p->type == P_UNKNOWN)
2916 p->type = P_SYMBOL;
2918 if (iomode == IO_OUTPUT)
2920 if (p->u.wsym.state == UNREFERENCED)
2921 p->u.wsym.state = NEEDS_WRITE;
2923 else
2925 if (p->u.rsym.state == UNUSED)
2926 p->u.rsym.state = NEEDED;
2928 return p;
2932 /* Save or restore a reference to a symtree node. */
2934 static void
2935 mio_symtree_ref (gfc_symtree **stp)
2937 pointer_info *p;
2938 fixup_t *f;
2940 if (iomode == IO_OUTPUT)
2941 mio_symbol_ref (&(*stp)->n.sym);
2942 else
2944 require_atom (ATOM_INTEGER);
2945 p = get_integer (atom_int);
2947 /* An unused equivalence member; make a symbol and a symtree
2948 for it. */
2949 if (in_load_equiv && p->u.rsym.symtree == NULL)
2951 /* Since this is not used, it must have a unique name. */
2952 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2954 /* Make the symbol. */
2955 if (p->u.rsym.sym == NULL)
2957 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2958 gfc_current_ns);
2959 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2962 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2963 p->u.rsym.symtree->n.sym->refs++;
2964 p->u.rsym.referenced = 1;
2966 /* If the symbol is PRIVATE and in COMMON, load_commons will
2967 generate a fixup symbol, which must be associated. */
2968 if (p->fixup)
2969 resolve_fixups (p->fixup, p->u.rsym.sym);
2970 p->fixup = NULL;
2973 if (p->type == P_UNKNOWN)
2974 p->type = P_SYMBOL;
2976 if (p->u.rsym.state == UNUSED)
2977 p->u.rsym.state = NEEDED;
2979 if (p->u.rsym.symtree != NULL)
2981 *stp = p->u.rsym.symtree;
2983 else
2985 f = XCNEW (fixup_t);
2987 f->next = p->u.rsym.stfixup;
2988 p->u.rsym.stfixup = f;
2990 f->pointer = (void **) stp;
2996 static void
2997 mio_iterator (gfc_iterator **ip)
2999 gfc_iterator *iter;
3001 mio_lparen ();
3003 if (iomode == IO_OUTPUT)
3005 if (*ip == NULL)
3006 goto done;
3008 else
3010 if (peek_atom () == ATOM_RPAREN)
3012 *ip = NULL;
3013 goto done;
3016 *ip = gfc_get_iterator ();
3019 iter = *ip;
3021 mio_expr (&iter->var);
3022 mio_expr (&iter->start);
3023 mio_expr (&iter->end);
3024 mio_expr (&iter->step);
3026 done:
3027 mio_rparen ();
3031 static void
3032 mio_constructor (gfc_constructor_base *cp)
3034 gfc_constructor *c;
3036 mio_lparen ();
3038 if (iomode == IO_OUTPUT)
3040 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3042 mio_lparen ();
3043 mio_expr (&c->expr);
3044 mio_iterator (&c->iterator);
3045 mio_rparen ();
3048 else
3050 while (peek_atom () != ATOM_RPAREN)
3052 c = gfc_constructor_append_expr (cp, NULL, NULL);
3054 mio_lparen ();
3055 mio_expr (&c->expr);
3056 mio_iterator (&c->iterator);
3057 mio_rparen ();
3061 mio_rparen ();
3065 static const mstring ref_types[] = {
3066 minit ("ARRAY", REF_ARRAY),
3067 minit ("COMPONENT", REF_COMPONENT),
3068 minit ("SUBSTRING", REF_SUBSTRING),
3069 minit (NULL, -1)
3073 static void
3074 mio_ref (gfc_ref **rp)
3076 gfc_ref *r;
3078 mio_lparen ();
3080 r = *rp;
3081 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3083 switch (r->type)
3085 case REF_ARRAY:
3086 mio_array_ref (&r->u.ar);
3087 break;
3089 case REF_COMPONENT:
3090 mio_symbol_ref (&r->u.c.sym);
3091 mio_component_ref (&r->u.c.component);
3092 break;
3094 case REF_SUBSTRING:
3095 mio_expr (&r->u.ss.start);
3096 mio_expr (&r->u.ss.end);
3097 mio_charlen (&r->u.ss.length);
3098 break;
3101 mio_rparen ();
3105 static void
3106 mio_ref_list (gfc_ref **rp)
3108 gfc_ref *ref, *head, *tail;
3110 mio_lparen ();
3112 if (iomode == IO_OUTPUT)
3114 for (ref = *rp; ref; ref = ref->next)
3115 mio_ref (&ref);
3117 else
3119 head = tail = NULL;
3121 while (peek_atom () != ATOM_RPAREN)
3123 if (head == NULL)
3124 head = tail = gfc_get_ref ();
3125 else
3127 tail->next = gfc_get_ref ();
3128 tail = tail->next;
3131 mio_ref (&tail);
3134 *rp = head;
3137 mio_rparen ();
3141 /* Read and write an integer value. */
3143 static void
3144 mio_gmp_integer (mpz_t *integer)
3146 char *p;
3148 if (iomode == IO_INPUT)
3150 if (parse_atom () != ATOM_STRING)
3151 bad_module ("Expected integer string");
3153 mpz_init (*integer);
3154 if (mpz_set_str (*integer, atom_string, 10))
3155 bad_module ("Error converting integer");
3157 free (atom_string);
3159 else
3161 p = mpz_get_str (NULL, 10, *integer);
3162 write_atom (ATOM_STRING, p);
3163 free (p);
3168 static void
3169 mio_gmp_real (mpfr_t *real)
3171 mp_exp_t exponent;
3172 char *p;
3174 if (iomode == IO_INPUT)
3176 if (parse_atom () != ATOM_STRING)
3177 bad_module ("Expected real string");
3179 mpfr_init (*real);
3180 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3181 free (atom_string);
3183 else
3185 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3187 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3189 write_atom (ATOM_STRING, p);
3190 free (p);
3191 return;
3194 atom_string = XCNEWVEC (char, strlen (p) + 20);
3196 sprintf (atom_string, "0.%s@%ld", p, exponent);
3198 /* Fix negative numbers. */
3199 if (atom_string[2] == '-')
3201 atom_string[0] = '-';
3202 atom_string[1] = '0';
3203 atom_string[2] = '.';
3206 write_atom (ATOM_STRING, atom_string);
3208 free (atom_string);
3209 free (p);
3214 /* Save and restore the shape of an array constructor. */
3216 static void
3217 mio_shape (mpz_t **pshape, int rank)
3219 mpz_t *shape;
3220 atom_type t;
3221 int n;
3223 /* A NULL shape is represented by (). */
3224 mio_lparen ();
3226 if (iomode == IO_OUTPUT)
3228 shape = *pshape;
3229 if (!shape)
3231 mio_rparen ();
3232 return;
3235 else
3237 t = peek_atom ();
3238 if (t == ATOM_RPAREN)
3240 *pshape = NULL;
3241 mio_rparen ();
3242 return;
3245 shape = gfc_get_shape (rank);
3246 *pshape = shape;
3249 for (n = 0; n < rank; n++)
3250 mio_gmp_integer (&shape[n]);
3252 mio_rparen ();
3256 static const mstring expr_types[] = {
3257 minit ("OP", EXPR_OP),
3258 minit ("FUNCTION", EXPR_FUNCTION),
3259 minit ("CONSTANT", EXPR_CONSTANT),
3260 minit ("VARIABLE", EXPR_VARIABLE),
3261 minit ("SUBSTRING", EXPR_SUBSTRING),
3262 minit ("STRUCTURE", EXPR_STRUCTURE),
3263 minit ("ARRAY", EXPR_ARRAY),
3264 minit ("NULL", EXPR_NULL),
3265 minit ("COMPCALL", EXPR_COMPCALL),
3266 minit (NULL, -1)
3269 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3270 generic operators, not in expressions. INTRINSIC_USER is also
3271 replaced by the correct function name by the time we see it. */
3273 static const mstring intrinsics[] =
3275 minit ("UPLUS", INTRINSIC_UPLUS),
3276 minit ("UMINUS", INTRINSIC_UMINUS),
3277 minit ("PLUS", INTRINSIC_PLUS),
3278 minit ("MINUS", INTRINSIC_MINUS),
3279 minit ("TIMES", INTRINSIC_TIMES),
3280 minit ("DIVIDE", INTRINSIC_DIVIDE),
3281 minit ("POWER", INTRINSIC_POWER),
3282 minit ("CONCAT", INTRINSIC_CONCAT),
3283 minit ("AND", INTRINSIC_AND),
3284 minit ("OR", INTRINSIC_OR),
3285 minit ("EQV", INTRINSIC_EQV),
3286 minit ("NEQV", INTRINSIC_NEQV),
3287 minit ("EQ_SIGN", INTRINSIC_EQ),
3288 minit ("EQ", INTRINSIC_EQ_OS),
3289 minit ("NE_SIGN", INTRINSIC_NE),
3290 minit ("NE", INTRINSIC_NE_OS),
3291 minit ("GT_SIGN", INTRINSIC_GT),
3292 minit ("GT", INTRINSIC_GT_OS),
3293 minit ("GE_SIGN", INTRINSIC_GE),
3294 minit ("GE", INTRINSIC_GE_OS),
3295 minit ("LT_SIGN", INTRINSIC_LT),
3296 minit ("LT", INTRINSIC_LT_OS),
3297 minit ("LE_SIGN", INTRINSIC_LE),
3298 minit ("LE", INTRINSIC_LE_OS),
3299 minit ("NOT", INTRINSIC_NOT),
3300 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3301 minit ("USER", INTRINSIC_USER),
3302 minit (NULL, -1)
3306 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3308 static void
3309 fix_mio_expr (gfc_expr *e)
3311 gfc_symtree *ns_st = NULL;
3312 const char *fname;
3314 if (iomode != IO_OUTPUT)
3315 return;
3317 if (e->symtree)
3319 /* If this is a symtree for a symbol that came from a contained module
3320 namespace, it has a unique name and we should look in the current
3321 namespace to see if the required, non-contained symbol is available
3322 yet. If so, the latter should be written. */
3323 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3325 const char *name = e->symtree->n.sym->name;
3326 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3327 name = gfc_dt_upper_string (name);
3328 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3331 /* On the other hand, if the existing symbol is the module name or the
3332 new symbol is a dummy argument, do not do the promotion. */
3333 if (ns_st && ns_st->n.sym
3334 && ns_st->n.sym->attr.flavor != FL_MODULE
3335 && !e->symtree->n.sym->attr.dummy)
3336 e->symtree = ns_st;
3338 else if (e->expr_type == EXPR_FUNCTION
3339 && (e->value.function.name || e->value.function.isym))
3341 gfc_symbol *sym;
3343 /* In some circumstances, a function used in an initialization
3344 expression, in one use associated module, can fail to be
3345 coupled to its symtree when used in a specification
3346 expression in another module. */
3347 fname = e->value.function.esym ? e->value.function.esym->name
3348 : e->value.function.isym->name;
3349 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3351 if (e->symtree)
3352 return;
3354 /* This is probably a reference to a private procedure from another
3355 module. To prevent a segfault, make a generic with no specific
3356 instances. If this module is used, without the required
3357 specific coming from somewhere, the appropriate error message
3358 is issued. */
3359 gfc_get_symbol (fname, gfc_current_ns, &sym);
3360 sym->attr.flavor = FL_PROCEDURE;
3361 sym->attr.generic = 1;
3362 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3363 gfc_commit_symbol (sym);
3368 /* Read and write expressions. The form "()" is allowed to indicate a
3369 NULL expression. */
3371 static void
3372 mio_expr (gfc_expr **ep)
3374 gfc_expr *e;
3375 atom_type t;
3376 int flag;
3378 mio_lparen ();
3380 if (iomode == IO_OUTPUT)
3382 if (*ep == NULL)
3384 mio_rparen ();
3385 return;
3388 e = *ep;
3389 MIO_NAME (expr_t) (e->expr_type, expr_types);
3391 else
3393 t = parse_atom ();
3394 if (t == ATOM_RPAREN)
3396 *ep = NULL;
3397 return;
3400 if (t != ATOM_NAME)
3401 bad_module ("Expected expression type");
3403 e = *ep = gfc_get_expr ();
3404 e->where = gfc_current_locus;
3405 e->expr_type = (expr_t) find_enum (expr_types);
3408 mio_typespec (&e->ts);
3409 mio_integer (&e->rank);
3411 fix_mio_expr (e);
3413 switch (e->expr_type)
3415 case EXPR_OP:
3416 e->value.op.op
3417 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3419 switch (e->value.op.op)
3421 case INTRINSIC_UPLUS:
3422 case INTRINSIC_UMINUS:
3423 case INTRINSIC_NOT:
3424 case INTRINSIC_PARENTHESES:
3425 mio_expr (&e->value.op.op1);
3426 break;
3428 case INTRINSIC_PLUS:
3429 case INTRINSIC_MINUS:
3430 case INTRINSIC_TIMES:
3431 case INTRINSIC_DIVIDE:
3432 case INTRINSIC_POWER:
3433 case INTRINSIC_CONCAT:
3434 case INTRINSIC_AND:
3435 case INTRINSIC_OR:
3436 case INTRINSIC_EQV:
3437 case INTRINSIC_NEQV:
3438 case INTRINSIC_EQ:
3439 case INTRINSIC_EQ_OS:
3440 case INTRINSIC_NE:
3441 case INTRINSIC_NE_OS:
3442 case INTRINSIC_GT:
3443 case INTRINSIC_GT_OS:
3444 case INTRINSIC_GE:
3445 case INTRINSIC_GE_OS:
3446 case INTRINSIC_LT:
3447 case INTRINSIC_LT_OS:
3448 case INTRINSIC_LE:
3449 case INTRINSIC_LE_OS:
3450 mio_expr (&e->value.op.op1);
3451 mio_expr (&e->value.op.op2);
3452 break;
3454 case INTRINSIC_USER:
3455 /* INTRINSIC_USER should not appear in resolved expressions,
3456 though for UDRs we need to stream unresolved ones. */
3457 if (iomode == IO_OUTPUT)
3458 write_atom (ATOM_STRING, e->value.op.uop->name);
3459 else
3461 char *name = read_string ();
3462 const char *uop_name = find_use_name (name, true);
3463 if (uop_name == NULL)
3465 size_t len = strlen (name);
3466 char *name2 = XCNEWVEC (char, len + 2);
3467 memcpy (name2, name, len);
3468 name2[len] = ' ';
3469 name2[len + 1] = '\0';
3470 free (name);
3471 uop_name = name = name2;
3473 e->value.op.uop = gfc_get_uop (uop_name);
3474 free (name);
3476 mio_expr (&e->value.op.op1);
3477 mio_expr (&e->value.op.op2);
3478 break;
3480 default:
3481 bad_module ("Bad operator");
3484 break;
3486 case EXPR_FUNCTION:
3487 mio_symtree_ref (&e->symtree);
3488 mio_actual_arglist (&e->value.function.actual);
3490 if (iomode == IO_OUTPUT)
3492 e->value.function.name
3493 = mio_allocated_string (e->value.function.name);
3494 if (e->value.function.esym)
3495 flag = 1;
3496 else if (e->ref)
3497 flag = 2;
3498 else if (e->value.function.isym == NULL)
3499 flag = 3;
3500 else
3501 flag = 0;
3502 mio_integer (&flag);
3503 switch (flag)
3505 case 1:
3506 mio_symbol_ref (&e->value.function.esym);
3507 break;
3508 case 2:
3509 mio_ref_list (&e->ref);
3510 break;
3511 case 3:
3512 break;
3513 default:
3514 write_atom (ATOM_STRING, e->value.function.isym->name);
3517 else
3519 require_atom (ATOM_STRING);
3520 if (atom_string[0] == '\0')
3521 e->value.function.name = NULL;
3522 else
3523 e->value.function.name = gfc_get_string (atom_string);
3524 free (atom_string);
3526 mio_integer (&flag);
3527 switch (flag)
3529 case 1:
3530 mio_symbol_ref (&e->value.function.esym);
3531 break;
3532 case 2:
3533 mio_ref_list (&e->ref);
3534 break;
3535 case 3:
3536 break;
3537 default:
3538 require_atom (ATOM_STRING);
3539 e->value.function.isym = gfc_find_function (atom_string);
3540 free (atom_string);
3544 break;
3546 case EXPR_VARIABLE:
3547 mio_symtree_ref (&e->symtree);
3548 mio_ref_list (&e->ref);
3549 break;
3551 case EXPR_SUBSTRING:
3552 e->value.character.string
3553 = CONST_CAST (gfc_char_t *,
3554 mio_allocated_wide_string (e->value.character.string,
3555 e->value.character.length));
3556 mio_ref_list (&e->ref);
3557 break;
3559 case EXPR_STRUCTURE:
3560 case EXPR_ARRAY:
3561 mio_constructor (&e->value.constructor);
3562 mio_shape (&e->shape, e->rank);
3563 break;
3565 case EXPR_CONSTANT:
3566 switch (e->ts.type)
3568 case BT_INTEGER:
3569 mio_gmp_integer (&e->value.integer);
3570 break;
3572 case BT_REAL:
3573 gfc_set_model_kind (e->ts.kind);
3574 mio_gmp_real (&e->value.real);
3575 break;
3577 case BT_COMPLEX:
3578 gfc_set_model_kind (e->ts.kind);
3579 mio_gmp_real (&mpc_realref (e->value.complex));
3580 mio_gmp_real (&mpc_imagref (e->value.complex));
3581 break;
3583 case BT_LOGICAL:
3584 mio_integer (&e->value.logical);
3585 break;
3587 case BT_CHARACTER:
3588 mio_integer (&e->value.character.length);
3589 e->value.character.string
3590 = CONST_CAST (gfc_char_t *,
3591 mio_allocated_wide_string (e->value.character.string,
3592 e->value.character.length));
3593 break;
3595 default:
3596 bad_module ("Bad type in constant expression");
3599 break;
3601 case EXPR_NULL:
3602 break;
3604 case EXPR_COMPCALL:
3605 case EXPR_PPC:
3606 gcc_unreachable ();
3607 break;
3610 mio_rparen ();
3614 /* Read and write namelists. */
3616 static void
3617 mio_namelist (gfc_symbol *sym)
3619 gfc_namelist *n, *m;
3620 const char *check_name;
3622 mio_lparen ();
3624 if (iomode == IO_OUTPUT)
3626 for (n = sym->namelist; n; n = n->next)
3627 mio_symbol_ref (&n->sym);
3629 else
3631 /* This departure from the standard is flagged as an error.
3632 It does, in fact, work correctly. TODO: Allow it
3633 conditionally? */
3634 if (sym->attr.flavor == FL_NAMELIST)
3636 check_name = find_use_name (sym->name, false);
3637 if (check_name && strcmp (check_name, sym->name) != 0)
3638 gfc_error ("Namelist %s cannot be renamed by USE "
3639 "association to %s", sym->name, check_name);
3642 m = NULL;
3643 while (peek_atom () != ATOM_RPAREN)
3645 n = gfc_get_namelist ();
3646 mio_symbol_ref (&n->sym);
3648 if (sym->namelist == NULL)
3649 sym->namelist = n;
3650 else
3651 m->next = n;
3653 m = n;
3655 sym->namelist_tail = m;
3658 mio_rparen ();
3662 /* Save/restore lists of gfc_interface structures. When loading an
3663 interface, we are really appending to the existing list of
3664 interfaces. Checking for duplicate and ambiguous interfaces has to
3665 be done later when all symbols have been loaded. */
3667 pointer_info *
3668 mio_interface_rest (gfc_interface **ip)
3670 gfc_interface *tail, *p;
3671 pointer_info *pi = NULL;
3673 if (iomode == IO_OUTPUT)
3675 if (ip != NULL)
3676 for (p = *ip; p; p = p->next)
3677 mio_symbol_ref (&p->sym);
3679 else
3681 if (*ip == NULL)
3682 tail = NULL;
3683 else
3685 tail = *ip;
3686 while (tail->next)
3687 tail = tail->next;
3690 for (;;)
3692 if (peek_atom () == ATOM_RPAREN)
3693 break;
3695 p = gfc_get_interface ();
3696 p->where = gfc_current_locus;
3697 pi = mio_symbol_ref (&p->sym);
3699 if (tail == NULL)
3700 *ip = p;
3701 else
3702 tail->next = p;
3704 tail = p;
3708 mio_rparen ();
3709 return pi;
3713 /* Save/restore a nameless operator interface. */
3715 static void
3716 mio_interface (gfc_interface **ip)
3718 mio_lparen ();
3719 mio_interface_rest (ip);
3723 /* Save/restore a named operator interface. */
3725 static void
3726 mio_symbol_interface (const char **name, const char **module,
3727 gfc_interface **ip)
3729 mio_lparen ();
3730 mio_pool_string (name);
3731 mio_pool_string (module);
3732 mio_interface_rest (ip);
3736 static void
3737 mio_namespace_ref (gfc_namespace **nsp)
3739 gfc_namespace *ns;
3740 pointer_info *p;
3742 p = mio_pointer_ref (nsp);
3744 if (p->type == P_UNKNOWN)
3745 p->type = P_NAMESPACE;
3747 if (iomode == IO_INPUT && p->integer != 0)
3749 ns = (gfc_namespace *) p->u.pointer;
3750 if (ns == NULL)
3752 ns = gfc_get_namespace (NULL, 0);
3753 associate_integer_pointer (p, ns);
3755 else
3756 ns->refs++;
3761 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3763 static gfc_namespace* current_f2k_derived;
3765 static void
3766 mio_typebound_proc (gfc_typebound_proc** proc)
3768 int flag;
3769 int overriding_flag;
3771 if (iomode == IO_INPUT)
3773 *proc = gfc_get_typebound_proc (NULL);
3774 (*proc)->where = gfc_current_locus;
3776 gcc_assert (*proc);
3778 mio_lparen ();
3780 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3782 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3783 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3784 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3785 overriding_flag = mio_name (overriding_flag, binding_overriding);
3786 (*proc)->deferred = ((overriding_flag & 2) != 0);
3787 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3788 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3790 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3791 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3792 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3794 mio_pool_string (&((*proc)->pass_arg));
3796 flag = (int) (*proc)->pass_arg_num;
3797 mio_integer (&flag);
3798 (*proc)->pass_arg_num = (unsigned) flag;
3800 if ((*proc)->is_generic)
3802 gfc_tbp_generic* g;
3803 int iop;
3805 mio_lparen ();
3807 if (iomode == IO_OUTPUT)
3808 for (g = (*proc)->u.generic; g; g = g->next)
3810 iop = (int) g->is_operator;
3811 mio_integer (&iop);
3812 mio_allocated_string (g->specific_st->name);
3814 else
3816 (*proc)->u.generic = NULL;
3817 while (peek_atom () != ATOM_RPAREN)
3819 gfc_symtree** sym_root;
3821 g = gfc_get_tbp_generic ();
3822 g->specific = NULL;
3824 mio_integer (&iop);
3825 g->is_operator = (bool) iop;
3827 require_atom (ATOM_STRING);
3828 sym_root = &current_f2k_derived->tb_sym_root;
3829 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3830 free (atom_string);
3832 g->next = (*proc)->u.generic;
3833 (*proc)->u.generic = g;
3837 mio_rparen ();
3839 else if (!(*proc)->ppc)
3840 mio_symtree_ref (&(*proc)->u.specific);
3842 mio_rparen ();
3845 /* Walker-callback function for this purpose. */
3846 static void
3847 mio_typebound_symtree (gfc_symtree* st)
3849 if (iomode == IO_OUTPUT && !st->n.tb)
3850 return;
3852 if (iomode == IO_OUTPUT)
3854 mio_lparen ();
3855 mio_allocated_string (st->name);
3857 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3859 mio_typebound_proc (&st->n.tb);
3860 mio_rparen ();
3863 /* IO a full symtree (in all depth). */
3864 static void
3865 mio_full_typebound_tree (gfc_symtree** root)
3867 mio_lparen ();
3869 if (iomode == IO_OUTPUT)
3870 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3871 else
3873 while (peek_atom () == ATOM_LPAREN)
3875 gfc_symtree* st;
3877 mio_lparen ();
3879 require_atom (ATOM_STRING);
3880 st = gfc_get_tbp_symtree (root, atom_string);
3881 free (atom_string);
3883 mio_typebound_symtree (st);
3887 mio_rparen ();
3890 static void
3891 mio_finalizer (gfc_finalizer **f)
3893 if (iomode == IO_OUTPUT)
3895 gcc_assert (*f);
3896 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3897 mio_symtree_ref (&(*f)->proc_tree);
3899 else
3901 *f = gfc_get_finalizer ();
3902 (*f)->where = gfc_current_locus; /* Value should not matter. */
3903 (*f)->next = NULL;
3905 mio_symtree_ref (&(*f)->proc_tree);
3906 (*f)->proc_sym = NULL;
3910 static void
3911 mio_f2k_derived (gfc_namespace *f2k)
3913 current_f2k_derived = f2k;
3915 /* Handle the list of finalizer procedures. */
3916 mio_lparen ();
3917 if (iomode == IO_OUTPUT)
3919 gfc_finalizer *f;
3920 for (f = f2k->finalizers; f; f = f->next)
3921 mio_finalizer (&f);
3923 else
3925 f2k->finalizers = NULL;
3926 while (peek_atom () != ATOM_RPAREN)
3928 gfc_finalizer *cur = NULL;
3929 mio_finalizer (&cur);
3930 cur->next = f2k->finalizers;
3931 f2k->finalizers = cur;
3934 mio_rparen ();
3936 /* Handle type-bound procedures. */
3937 mio_full_typebound_tree (&f2k->tb_sym_root);
3939 /* Type-bound user operators. */
3940 mio_full_typebound_tree (&f2k->tb_uop_root);
3942 /* Type-bound intrinsic operators. */
3943 mio_lparen ();
3944 if (iomode == IO_OUTPUT)
3946 int op;
3947 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3949 gfc_intrinsic_op realop;
3951 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3952 continue;
3954 mio_lparen ();
3955 realop = (gfc_intrinsic_op) op;
3956 mio_intrinsic_op (&realop);
3957 mio_typebound_proc (&f2k->tb_op[op]);
3958 mio_rparen ();
3961 else
3962 while (peek_atom () != ATOM_RPAREN)
3964 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3966 mio_lparen ();
3967 mio_intrinsic_op (&op);
3968 mio_typebound_proc (&f2k->tb_op[op]);
3969 mio_rparen ();
3971 mio_rparen ();
3974 static void
3975 mio_full_f2k_derived (gfc_symbol *sym)
3977 mio_lparen ();
3979 if (iomode == IO_OUTPUT)
3981 if (sym->f2k_derived)
3982 mio_f2k_derived (sym->f2k_derived);
3984 else
3986 if (peek_atom () != ATOM_RPAREN)
3988 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3989 mio_f2k_derived (sym->f2k_derived);
3991 else
3992 gcc_assert (!sym->f2k_derived);
3995 mio_rparen ();
3998 static const mstring omp_declare_simd_clauses[] =
4000 minit ("INBRANCH", 0),
4001 minit ("NOTINBRANCH", 1),
4002 minit ("SIMDLEN", 2),
4003 minit ("UNIFORM", 3),
4004 minit ("LINEAR", 4),
4005 minit ("ALIGNED", 5),
4006 minit (NULL, -1)
4009 /* Handle !$omp declare simd. */
4011 static void
4012 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4014 if (iomode == IO_OUTPUT)
4016 if (*odsp == NULL)
4017 return;
4019 else if (peek_atom () != ATOM_LPAREN)
4020 return;
4022 gfc_omp_declare_simd *ods = *odsp;
4024 mio_lparen ();
4025 if (iomode == IO_OUTPUT)
4027 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4028 if (ods->clauses)
4030 gfc_omp_namelist *n;
4032 if (ods->clauses->inbranch)
4033 mio_name (0, omp_declare_simd_clauses);
4034 if (ods->clauses->notinbranch)
4035 mio_name (1, omp_declare_simd_clauses);
4036 if (ods->clauses->simdlen_expr)
4038 mio_name (2, omp_declare_simd_clauses);
4039 mio_expr (&ods->clauses->simdlen_expr);
4041 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4043 mio_name (3, omp_declare_simd_clauses);
4044 mio_symbol_ref (&n->sym);
4046 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4048 mio_name (4, omp_declare_simd_clauses);
4049 mio_symbol_ref (&n->sym);
4050 mio_expr (&n->expr);
4052 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4054 mio_name (5, omp_declare_simd_clauses);
4055 mio_symbol_ref (&n->sym);
4056 mio_expr (&n->expr);
4060 else
4062 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4064 require_atom (ATOM_NAME);
4065 *odsp = ods = gfc_get_omp_declare_simd ();
4066 ods->where = gfc_current_locus;
4067 ods->proc_name = ns->proc_name;
4068 if (peek_atom () == ATOM_NAME)
4070 ods->clauses = gfc_get_omp_clauses ();
4071 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4072 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4073 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4075 while (peek_atom () == ATOM_NAME)
4077 gfc_omp_namelist *n;
4078 int t = mio_name (0, omp_declare_simd_clauses);
4080 switch (t)
4082 case 0: ods->clauses->inbranch = true; break;
4083 case 1: ods->clauses->notinbranch = true; break;
4084 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4085 case 3:
4086 case 4:
4087 case 5:
4088 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4089 ptrs[t - 3] = &n->next;
4090 mio_symbol_ref (&n->sym);
4091 if (t != 3)
4092 mio_expr (&n->expr);
4093 break;
4098 mio_omp_declare_simd (ns, &ods->next);
4100 mio_rparen ();
4104 static const mstring omp_declare_reduction_stmt[] =
4106 minit ("ASSIGN", 0),
4107 minit ("CALL", 1),
4108 minit (NULL, -1)
4112 static void
4113 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4114 gfc_namespace *ns, bool is_initializer)
4116 if (iomode == IO_OUTPUT)
4118 if ((*sym1)->module == NULL)
4120 (*sym1)->module = module_name;
4121 (*sym2)->module = module_name;
4123 mio_symbol_ref (sym1);
4124 mio_symbol_ref (sym2);
4125 if (ns->code->op == EXEC_ASSIGN)
4127 mio_name (0, omp_declare_reduction_stmt);
4128 mio_expr (&ns->code->expr1);
4129 mio_expr (&ns->code->expr2);
4131 else
4133 int flag;
4134 mio_name (1, omp_declare_reduction_stmt);
4135 mio_symtree_ref (&ns->code->symtree);
4136 mio_actual_arglist (&ns->code->ext.actual);
4138 flag = ns->code->resolved_isym != NULL;
4139 mio_integer (&flag);
4140 if (flag)
4141 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4142 else
4143 mio_symbol_ref (&ns->code->resolved_sym);
4146 else
4148 pointer_info *p1 = mio_symbol_ref (sym1);
4149 pointer_info *p2 = mio_symbol_ref (sym2);
4150 gfc_symbol *sym;
4151 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4152 gcc_assert (p1->u.rsym.sym == NULL);
4153 /* Add hidden symbols to the symtree. */
4154 pointer_info *q = get_integer (p1->u.rsym.ns);
4155 q->u.pointer = (void *) ns;
4156 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4157 sym->ts = udr->ts;
4158 sym->module = gfc_get_string (p1->u.rsym.module);
4159 associate_integer_pointer (p1, sym);
4160 sym->attr.omp_udr_artificial_var = 1;
4161 gcc_assert (p2->u.rsym.sym == NULL);
4162 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4163 sym->ts = udr->ts;
4164 sym->module = gfc_get_string (p2->u.rsym.module);
4165 associate_integer_pointer (p2, sym);
4166 sym->attr.omp_udr_artificial_var = 1;
4167 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4169 ns->code = gfc_get_code (EXEC_ASSIGN);
4170 mio_expr (&ns->code->expr1);
4171 mio_expr (&ns->code->expr2);
4173 else
4175 int flag;
4176 ns->code = gfc_get_code (EXEC_CALL);
4177 mio_symtree_ref (&ns->code->symtree);
4178 mio_actual_arglist (&ns->code->ext.actual);
4180 mio_integer (&flag);
4181 if (flag)
4183 require_atom (ATOM_STRING);
4184 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4185 free (atom_string);
4187 else
4188 mio_symbol_ref (&ns->code->resolved_sym);
4190 ns->code->loc = gfc_current_locus;
4191 ns->omp_udr_ns = 1;
4196 /* Unlike most other routines, the address of the symbol node is already
4197 fixed on input and the name/module has already been filled in.
4198 If you update the symbol format here, don't forget to update read_module
4199 as well (look for "seek to the symbol's component list"). */
4201 static void
4202 mio_symbol (gfc_symbol *sym)
4204 int intmod = INTMOD_NONE;
4206 mio_lparen ();
4208 mio_symbol_attribute (&sym->attr);
4210 /* Note that components are always saved, even if they are supposed
4211 to be private. Component access is checked during searching. */
4212 mio_component_list (&sym->components, sym->attr.vtype);
4213 if (sym->components != NULL)
4214 sym->component_access
4215 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4217 mio_typespec (&sym->ts);
4218 if (sym->ts.type == BT_CLASS)
4219 sym->attr.class_ok = 1;
4221 if (iomode == IO_OUTPUT)
4222 mio_namespace_ref (&sym->formal_ns);
4223 else
4225 mio_namespace_ref (&sym->formal_ns);
4226 if (sym->formal_ns)
4227 sym->formal_ns->proc_name = sym;
4230 /* Save/restore common block links. */
4231 mio_symbol_ref (&sym->common_next);
4233 mio_formal_arglist (&sym->formal);
4235 if (sym->attr.flavor == FL_PARAMETER)
4236 mio_expr (&sym->value);
4238 mio_array_spec (&sym->as);
4240 mio_symbol_ref (&sym->result);
4242 if (sym->attr.cray_pointee)
4243 mio_symbol_ref (&sym->cp_pointer);
4245 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4246 mio_full_f2k_derived (sym);
4248 mio_namelist (sym);
4250 /* Add the fields that say whether this is from an intrinsic module,
4251 and if so, what symbol it is within the module. */
4252 /* mio_integer (&(sym->from_intmod)); */
4253 if (iomode == IO_OUTPUT)
4255 intmod = sym->from_intmod;
4256 mio_integer (&intmod);
4258 else
4260 mio_integer (&intmod);
4261 if (current_intmod)
4262 sym->from_intmod = current_intmod;
4263 else
4264 sym->from_intmod = (intmod_id) intmod;
4267 mio_integer (&(sym->intmod_sym_id));
4269 if (gfc_fl_struct (sym->attr.flavor))
4270 mio_integer (&(sym->hash_value));
4272 if (sym->formal_ns
4273 && sym->formal_ns->proc_name == sym
4274 && sym->formal_ns->entries == NULL)
4275 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4277 mio_rparen ();
4281 /************************* Top level subroutines *************************/
4283 /* Given a root symtree node and a symbol, try to find a symtree that
4284 references the symbol that is not a unique name. */
4286 static gfc_symtree *
4287 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
4289 gfc_symtree *s = NULL;
4291 if (st == NULL)
4292 return s;
4294 s = find_symtree_for_symbol (st->right, sym);
4295 if (s != NULL)
4296 return s;
4297 s = find_symtree_for_symbol (st->left, sym);
4298 if (s != NULL)
4299 return s;
4301 if (st->n.sym == sym && !check_unique_name (st->name))
4302 return st;
4304 return s;
4308 /* A recursive function to look for a specific symbol by name and by
4309 module. Whilst several symtrees might point to one symbol, its
4310 is sufficient for the purposes here than one exist. Note that
4311 generic interfaces are distinguished as are symbols that have been
4312 renamed in another module. */
4313 static gfc_symtree *
4314 find_symbol (gfc_symtree *st, const char *name,
4315 const char *module, int generic)
4317 int c;
4318 gfc_symtree *retval, *s;
4320 if (st == NULL || st->n.sym == NULL)
4321 return NULL;
4323 c = strcmp (name, st->n.sym->name);
4324 if (c == 0 && st->n.sym->module
4325 && strcmp (module, st->n.sym->module) == 0
4326 && !check_unique_name (st->name))
4328 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4330 /* Detect symbols that are renamed by use association in another
4331 module by the absence of a symtree and null attr.use_rename,
4332 since the latter is not transmitted in the module file. */
4333 if (((!generic && !st->n.sym->attr.generic)
4334 || (generic && st->n.sym->attr.generic))
4335 && !(s == NULL && !st->n.sym->attr.use_rename))
4336 return st;
4339 retval = find_symbol (st->left, name, module, generic);
4341 if (retval == NULL)
4342 retval = find_symbol (st->right, name, module, generic);
4344 return retval;
4348 /* Skip a list between balanced left and right parens.
4349 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4350 have been already parsed by hand, and the remaining of the content is to be
4351 skipped here. The default value is 0 (balanced parens). */
4353 static void
4354 skip_list (int nest_level = 0)
4356 int level;
4358 level = nest_level;
4361 switch (parse_atom ())
4363 case ATOM_LPAREN:
4364 level++;
4365 break;
4367 case ATOM_RPAREN:
4368 level--;
4369 break;
4371 case ATOM_STRING:
4372 free (atom_string);
4373 break;
4375 case ATOM_NAME:
4376 case ATOM_INTEGER:
4377 break;
4380 while (level > 0);
4384 /* Load operator interfaces from the module. Interfaces are unusual
4385 in that they attach themselves to existing symbols. */
4387 static void
4388 load_operator_interfaces (void)
4390 const char *p;
4391 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4392 gfc_user_op *uop;
4393 pointer_info *pi = NULL;
4394 int n, i;
4396 mio_lparen ();
4398 while (peek_atom () != ATOM_RPAREN)
4400 mio_lparen ();
4402 mio_internal_string (name);
4403 mio_internal_string (module);
4405 n = number_use_names (name, true);
4406 n = n ? n : 1;
4408 for (i = 1; i <= n; i++)
4410 /* Decide if we need to load this one or not. */
4411 p = find_use_name_n (name, &i, true);
4413 if (p == NULL)
4415 while (parse_atom () != ATOM_RPAREN);
4416 continue;
4419 if (i == 1)
4421 uop = gfc_get_uop (p);
4422 pi = mio_interface_rest (&uop->op);
4424 else
4426 if (gfc_find_uop (p, NULL))
4427 continue;
4428 uop = gfc_get_uop (p);
4429 uop->op = gfc_get_interface ();
4430 uop->op->where = gfc_current_locus;
4431 add_fixup (pi->integer, &uop->op->sym);
4436 mio_rparen ();
4440 /* Load interfaces from the module. Interfaces are unusual in that
4441 they attach themselves to existing symbols. */
4443 static void
4444 load_generic_interfaces (void)
4446 const char *p;
4447 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4448 gfc_symbol *sym;
4449 gfc_interface *generic = NULL, *gen = NULL;
4450 int n, i, renamed;
4451 bool ambiguous_set = false;
4453 mio_lparen ();
4455 while (peek_atom () != ATOM_RPAREN)
4457 mio_lparen ();
4459 mio_internal_string (name);
4460 mio_internal_string (module);
4462 n = number_use_names (name, false);
4463 renamed = n ? 1 : 0;
4464 n = n ? n : 1;
4466 for (i = 1; i <= n; i++)
4468 gfc_symtree *st;
4469 /* Decide if we need to load this one or not. */
4470 p = find_use_name_n (name, &i, false);
4472 st = find_symbol (gfc_current_ns->sym_root,
4473 name, module_name, 1);
4475 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4477 /* Skip the specific names for these cases. */
4478 while (i == 1 && parse_atom () != ATOM_RPAREN);
4480 continue;
4483 /* If the symbol exists already and is being USEd without being
4484 in an ONLY clause, do not load a new symtree(11.3.2). */
4485 if (!only_flag && st)
4486 sym = st->n.sym;
4488 if (!sym)
4490 if (st)
4492 sym = st->n.sym;
4493 if (strcmp (st->name, p) != 0)
4495 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4496 st->n.sym = sym;
4497 sym->refs++;
4501 /* Since we haven't found a valid generic interface, we had
4502 better make one. */
4503 if (!sym)
4505 gfc_get_symbol (p, NULL, &sym);
4506 sym->name = gfc_get_string (name);
4507 sym->module = module_name;
4508 sym->attr.flavor = FL_PROCEDURE;
4509 sym->attr.generic = 1;
4510 sym->attr.use_assoc = 1;
4513 else
4515 /* Unless sym is a generic interface, this reference
4516 is ambiguous. */
4517 if (st == NULL)
4518 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4520 sym = st->n.sym;
4522 if (st && !sym->attr.generic
4523 && !st->ambiguous
4524 && sym->module
4525 && strcmp (module, sym->module))
4527 ambiguous_set = true;
4528 st->ambiguous = 1;
4532 sym->attr.use_only = only_flag;
4533 sym->attr.use_rename = renamed;
4535 if (i == 1)
4537 mio_interface_rest (&sym->generic);
4538 generic = sym->generic;
4540 else if (!sym->generic)
4542 sym->generic = generic;
4543 sym->attr.generic_copy = 1;
4546 /* If a procedure that is not generic has generic interfaces
4547 that include itself, it is generic! We need to take care
4548 to retain symbols ambiguous that were already so. */
4549 if (sym->attr.use_assoc
4550 && !sym->attr.generic
4551 && sym->attr.flavor == FL_PROCEDURE)
4553 for (gen = generic; gen; gen = gen->next)
4555 if (gen->sym == sym)
4557 sym->attr.generic = 1;
4558 if (ambiguous_set)
4559 st->ambiguous = 0;
4560 break;
4568 mio_rparen ();
4572 /* Load common blocks. */
4574 static void
4575 load_commons (void)
4577 char name[GFC_MAX_SYMBOL_LEN + 1];
4578 gfc_common_head *p;
4580 mio_lparen ();
4582 while (peek_atom () != ATOM_RPAREN)
4584 int flags;
4585 char* label;
4586 mio_lparen ();
4587 mio_internal_string (name);
4589 p = gfc_get_common (name, 1);
4591 mio_symbol_ref (&p->head);
4592 mio_integer (&flags);
4593 if (flags & 1)
4594 p->saved = 1;
4595 if (flags & 2)
4596 p->threadprivate = 1;
4597 p->use_assoc = 1;
4599 /* Get whether this was a bind(c) common or not. */
4600 mio_integer (&p->is_bind_c);
4601 /* Get the binding label. */
4602 label = read_string ();
4603 if (strlen (label))
4604 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4605 XDELETEVEC (label);
4607 mio_rparen ();
4610 mio_rparen ();
4614 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4615 so that unused variables are not loaded and so that the expression can
4616 be safely freed. */
4618 static void
4619 load_equiv (void)
4621 gfc_equiv *head, *tail, *end, *eq, *equiv;
4622 bool duplicate;
4624 mio_lparen ();
4625 in_load_equiv = true;
4627 end = gfc_current_ns->equiv;
4628 while (end != NULL && end->next != NULL)
4629 end = end->next;
4631 while (peek_atom () != ATOM_RPAREN) {
4632 mio_lparen ();
4633 head = tail = NULL;
4635 while(peek_atom () != ATOM_RPAREN)
4637 if (head == NULL)
4638 head = tail = gfc_get_equiv ();
4639 else
4641 tail->eq = gfc_get_equiv ();
4642 tail = tail->eq;
4645 mio_pool_string (&tail->module);
4646 mio_expr (&tail->expr);
4649 /* Check for duplicate equivalences being loaded from different modules */
4650 duplicate = false;
4651 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
4653 if (equiv->module && head->module
4654 && strcmp (equiv->module, head->module) == 0)
4656 duplicate = true;
4657 break;
4661 if (duplicate)
4663 for (eq = head; eq; eq = head)
4665 head = eq->eq;
4666 gfc_free_expr (eq->expr);
4667 free (eq);
4671 if (end == NULL)
4672 gfc_current_ns->equiv = head;
4673 else
4674 end->next = head;
4676 if (head != NULL)
4677 end = head;
4679 mio_rparen ();
4682 mio_rparen ();
4683 in_load_equiv = false;
4687 /* This function loads OpenMP user defined reductions. */
4688 static void
4689 load_omp_udrs (void)
4691 mio_lparen ();
4692 while (peek_atom () != ATOM_RPAREN)
4694 const char *name, *newname;
4695 char *altname;
4696 gfc_typespec ts;
4697 gfc_symtree *st;
4698 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
4700 mio_lparen ();
4701 mio_pool_string (&name);
4702 mio_typespec (&ts);
4703 if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
4705 const char *p = name + sizeof ("operator ") - 1;
4706 if (strcmp (p, "+") == 0)
4707 rop = OMP_REDUCTION_PLUS;
4708 else if (strcmp (p, "*") == 0)
4709 rop = OMP_REDUCTION_TIMES;
4710 else if (strcmp (p, "-") == 0)
4711 rop = OMP_REDUCTION_MINUS;
4712 else if (strcmp (p, ".and.") == 0)
4713 rop = OMP_REDUCTION_AND;
4714 else if (strcmp (p, ".or.") == 0)
4715 rop = OMP_REDUCTION_OR;
4716 else if (strcmp (p, ".eqv.") == 0)
4717 rop = OMP_REDUCTION_EQV;
4718 else if (strcmp (p, ".neqv.") == 0)
4719 rop = OMP_REDUCTION_NEQV;
4721 altname = NULL;
4722 if (rop == OMP_REDUCTION_USER && name[0] == '.')
4724 size_t len = strlen (name + 1);
4725 altname = XALLOCAVEC (char, len);
4726 gcc_assert (name[len] == '.');
4727 memcpy (altname, name + 1, len - 1);
4728 altname[len - 1] = '\0';
4730 newname = name;
4731 if (rop == OMP_REDUCTION_USER)
4732 newname = find_use_name (altname ? altname : name, !!altname);
4733 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
4734 newname = NULL;
4735 if (newname == NULL)
4737 skip_list (1);
4738 continue;
4740 if (altname && newname != altname)
4742 size_t len = strlen (newname);
4743 altname = XALLOCAVEC (char, len + 3);
4744 altname[0] = '.';
4745 memcpy (altname + 1, newname, len);
4746 altname[len + 1] = '.';
4747 altname[len + 2] = '\0';
4748 name = gfc_get_string (altname);
4750 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
4751 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
4752 if (udr)
4754 require_atom (ATOM_INTEGER);
4755 pointer_info *p = get_integer (atom_int);
4756 if (strcmp (p->u.rsym.module, udr->omp_out->module))
4758 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4759 "module %s at %L",
4760 p->u.rsym.module, &gfc_current_locus);
4761 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4762 "%s at %L",
4763 udr->omp_out->module, &udr->where);
4765 skip_list (1);
4766 continue;
4768 udr = gfc_get_omp_udr ();
4769 udr->name = name;
4770 udr->rop = rop;
4771 udr->ts = ts;
4772 udr->where = gfc_current_locus;
4773 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
4774 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
4775 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
4776 false);
4777 if (peek_atom () != ATOM_RPAREN)
4779 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
4780 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
4781 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
4782 udr->initializer_ns, true);
4784 if (st)
4786 udr->next = st->n.omp_udr;
4787 st->n.omp_udr = udr;
4789 else
4791 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
4792 st->n.omp_udr = udr;
4794 mio_rparen ();
4796 mio_rparen ();
4800 /* Recursive function to traverse the pointer_info tree and load a
4801 needed symbol. We return nonzero if we load a symbol and stop the
4802 traversal, because the act of loading can alter the tree. */
4804 static int
4805 load_needed (pointer_info *p)
4807 gfc_namespace *ns;
4808 pointer_info *q;
4809 gfc_symbol *sym;
4810 int rv;
4812 rv = 0;
4813 if (p == NULL)
4814 return rv;
4816 rv |= load_needed (p->left);
4817 rv |= load_needed (p->right);
4819 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4820 return rv;
4822 p->u.rsym.state = USED;
4824 set_module_locus (&p->u.rsym.where);
4826 sym = p->u.rsym.sym;
4827 if (sym == NULL)
4829 q = get_integer (p->u.rsym.ns);
4831 ns = (gfc_namespace *) q->u.pointer;
4832 if (ns == NULL)
4834 /* Create an interface namespace if necessary. These are
4835 the namespaces that hold the formal parameters of module
4836 procedures. */
4838 ns = gfc_get_namespace (NULL, 0);
4839 associate_integer_pointer (q, ns);
4842 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4843 doesn't go pear-shaped if the symbol is used. */
4844 if (!ns->proc_name)
4845 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4846 1, &ns->proc_name);
4848 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4849 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
4850 sym->module = gfc_get_string (p->u.rsym.module);
4851 if (p->u.rsym.binding_label)
4852 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4853 (p->u.rsym.binding_label));
4855 associate_integer_pointer (p, sym);
4858 mio_symbol (sym);
4859 sym->attr.use_assoc = 1;
4861 /* Unliked derived types, a STRUCTURE may share names with other symbols.
4862 We greedily converted the the symbol name to lowercase before we knew its
4863 type, so now we must fix it. */
4864 if (sym->attr.flavor == FL_STRUCT)
4865 sym->name = gfc_dt_upper_string (sym->name);
4867 /* Mark as only or rename for later diagnosis for explicitly imported
4868 but not used warnings; don't mark internal symbols such as __vtab,
4869 __def_init etc. Only mark them if they have been explicitly loaded. */
4871 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4873 gfc_use_rename *u;
4875 /* Search the use/rename list for the variable; if the variable is
4876 found, mark it. */
4877 for (u = gfc_rename_list; u; u = u->next)
4879 if (strcmp (u->use_name, sym->name) == 0)
4881 sym->attr.use_only = 1;
4882 break;
4887 if (p->u.rsym.renamed)
4888 sym->attr.use_rename = 1;
4890 return 1;
4894 /* Recursive function for cleaning up things after a module has been read. */
4896 static void
4897 read_cleanup (pointer_info *p)
4899 gfc_symtree *st;
4900 pointer_info *q;
4902 if (p == NULL)
4903 return;
4905 read_cleanup (p->left);
4906 read_cleanup (p->right);
4908 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4910 gfc_namespace *ns;
4911 /* Add hidden symbols to the symtree. */
4912 q = get_integer (p->u.rsym.ns);
4913 ns = (gfc_namespace *) q->u.pointer;
4915 if (!p->u.rsym.sym->attr.vtype
4916 && !p->u.rsym.sym->attr.vtab)
4917 st = gfc_get_unique_symtree (ns);
4918 else
4920 /* There is no reason to use 'unique_symtrees' for vtabs or
4921 vtypes - their name is fine for a symtree and reduces the
4922 namespace pollution. */
4923 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4924 if (!st)
4925 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4928 st->n.sym = p->u.rsym.sym;
4929 st->n.sym->refs++;
4931 /* Fixup any symtree references. */
4932 p->u.rsym.symtree = st;
4933 resolve_fixups (p->u.rsym.stfixup, st);
4934 p->u.rsym.stfixup = NULL;
4937 /* Free unused symbols. */
4938 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4939 gfc_free_symbol (p->u.rsym.sym);
4943 /* It is not quite enough to check for ambiguity in the symbols by
4944 the loaded symbol and the new symbol not being identical. */
4945 static bool
4946 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
4948 gfc_symbol *rsym;
4949 module_locus locus;
4950 symbol_attribute attr;
4951 gfc_symbol *st_sym;
4953 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
4955 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4956 "current program unit", st->name, module_name);
4957 return true;
4960 st_sym = st->n.sym;
4961 rsym = info->u.rsym.sym;
4962 if (st_sym == rsym)
4963 return false;
4965 if (st_sym->attr.vtab || st_sym->attr.vtype)
4966 return false;
4968 /* If the existing symbol is generic from a different module and
4969 the new symbol is generic there can be no ambiguity. */
4970 if (st_sym->attr.generic
4971 && st_sym->module
4972 && st_sym->module != module_name)
4974 /* The new symbol's attributes have not yet been read. Since
4975 we need attr.generic, read it directly. */
4976 get_module_locus (&locus);
4977 set_module_locus (&info->u.rsym.where);
4978 mio_lparen ();
4979 attr.generic = 0;
4980 mio_symbol_attribute (&attr);
4981 set_module_locus (&locus);
4982 if (attr.generic)
4983 return false;
4986 return true;
4990 /* Read a module file. */
4992 static void
4993 read_module (void)
4995 module_locus operator_interfaces, user_operators, omp_udrs;
4996 const char *p;
4997 char name[GFC_MAX_SYMBOL_LEN + 1];
4998 int i;
4999 /* Workaround -Wmaybe-uninitialized false positive during
5000 profiledbootstrap by initializing them. */
5001 int ambiguous = 0, j, nuse, symbol = 0;
5002 pointer_info *info, *q;
5003 gfc_use_rename *u = NULL;
5004 gfc_symtree *st;
5005 gfc_symbol *sym;
5007 get_module_locus (&operator_interfaces); /* Skip these for now. */
5008 skip_list ();
5010 get_module_locus (&user_operators);
5011 skip_list ();
5012 skip_list ();
5014 /* Skip commons and equivalences for now. */
5015 skip_list ();
5016 skip_list ();
5018 /* Skip OpenMP UDRs. */
5019 get_module_locus (&omp_udrs);
5020 skip_list ();
5022 mio_lparen ();
5024 /* Create the fixup nodes for all the symbols. */
5026 while (peek_atom () != ATOM_RPAREN)
5028 char* bind_label;
5029 require_atom (ATOM_INTEGER);
5030 info = get_integer (atom_int);
5032 info->type = P_SYMBOL;
5033 info->u.rsym.state = UNUSED;
5035 info->u.rsym.true_name = read_string ();
5036 info->u.rsym.module = read_string ();
5037 bind_label = read_string ();
5038 if (strlen (bind_label))
5039 info->u.rsym.binding_label = bind_label;
5040 else
5041 XDELETEVEC (bind_label);
5043 require_atom (ATOM_INTEGER);
5044 info->u.rsym.ns = atom_int;
5046 get_module_locus (&info->u.rsym.where);
5048 /* See if the symbol has already been loaded by a previous module.
5049 If so, we reference the existing symbol and prevent it from
5050 being loaded again. This should not happen if the symbol being
5051 read is an index for an assumed shape dummy array (ns != 1). */
5053 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5055 if (sym == NULL
5056 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5058 skip_list ();
5059 continue;
5062 info->u.rsym.state = USED;
5063 info->u.rsym.sym = sym;
5064 /* The current symbol has already been loaded, so we can avoid loading
5065 it again. However, if it is a derived type, some of its components
5066 can be used in expressions in the module. To avoid the module loading
5067 failing, we need to associate the module's component pointer indexes
5068 with the existing symbol's component pointers. */
5069 if (gfc_fl_struct (sym->attr.flavor))
5071 gfc_component *c;
5073 /* First seek to the symbol's component list. */
5074 mio_lparen (); /* symbol opening. */
5075 skip_list (); /* skip symbol attribute. */
5077 mio_lparen (); /* component list opening. */
5078 for (c = sym->components; c; c = c->next)
5080 pointer_info *p;
5081 const char *comp_name;
5082 int n;
5084 mio_lparen (); /* component opening. */
5085 mio_integer (&n);
5086 p = get_integer (n);
5087 if (p->u.pointer == NULL)
5088 associate_integer_pointer (p, c);
5089 mio_pool_string (&comp_name);
5090 gcc_assert (comp_name == c->name);
5091 skip_list (1); /* component end. */
5093 mio_rparen (); /* component list closing. */
5095 skip_list (1); /* symbol end. */
5097 else
5098 skip_list ();
5100 /* Some symbols do not have a namespace (eg. formal arguments),
5101 so the automatic "unique symtree" mechanism must be suppressed
5102 by marking them as referenced. */
5103 q = get_integer (info->u.rsym.ns);
5104 if (q->u.pointer == NULL)
5106 info->u.rsym.referenced = 1;
5107 continue;
5110 /* If possible recycle the symtree that references the symbol.
5111 If a symtree is not found and the module does not import one,
5112 a unique-name symtree is found by read_cleanup. */
5113 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
5114 if (st != NULL)
5116 info->u.rsym.symtree = st;
5117 info->u.rsym.referenced = 1;
5121 mio_rparen ();
5123 /* Parse the symtree lists. This lets us mark which symbols need to
5124 be loaded. Renaming is also done at this point by replacing the
5125 symtree name. */
5127 mio_lparen ();
5129 while (peek_atom () != ATOM_RPAREN)
5131 mio_internal_string (name);
5132 mio_integer (&ambiguous);
5133 mio_integer (&symbol);
5135 info = get_integer (symbol);
5137 /* See how many use names there are. If none, go through the start
5138 of the loop at least once. */
5139 nuse = number_use_names (name, false);
5140 info->u.rsym.renamed = nuse ? 1 : 0;
5142 if (nuse == 0)
5143 nuse = 1;
5145 for (j = 1; j <= nuse; j++)
5147 /* Get the jth local name for this symbol. */
5148 p = find_use_name_n (name, &j, false);
5150 if (p == NULL && strcmp (name, module_name) == 0)
5151 p = name;
5153 /* Exception: Always import vtabs & vtypes. */
5154 if (p == NULL && name[0] == '_'
5155 && (strncmp (name, "__vtab_", 5) == 0
5156 || strncmp (name, "__vtype_", 6) == 0))
5157 p = name;
5159 /* Skip symtree nodes not in an ONLY clause, unless there
5160 is an existing symtree loaded from another USE statement. */
5161 if (p == NULL)
5163 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5164 if (st != NULL
5165 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5166 && st->n.sym->module != NULL
5167 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5169 info->u.rsym.symtree = st;
5170 info->u.rsym.sym = st->n.sym;
5172 continue;
5175 /* If a symbol of the same name and module exists already,
5176 this symbol, which is not in an ONLY clause, must not be
5177 added to the namespace(11.3.2). Note that find_symbol
5178 only returns the first occurrence that it finds. */
5179 if (!only_flag && !info->u.rsym.renamed
5180 && strcmp (name, module_name) != 0
5181 && find_symbol (gfc_current_ns->sym_root, name,
5182 module_name, 0))
5183 continue;
5185 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5187 if (st != NULL
5188 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5190 /* Check for ambiguous symbols. */
5191 if (check_for_ambiguous (st, info))
5192 st->ambiguous = 1;
5193 else
5194 info->u.rsym.symtree = st;
5196 else
5198 if (st)
5200 /* This symbol is host associated from a module in a
5201 submodule. Hide it with a unique symtree. */
5202 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5203 s->n.sym = st->n.sym;
5204 st->n.sym = NULL;
5206 else
5208 /* Create a symtree node in the current namespace for this
5209 symbol. */
5210 st = check_unique_name (p)
5211 ? gfc_get_unique_symtree (gfc_current_ns)
5212 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5213 st->ambiguous = ambiguous;
5216 sym = info->u.rsym.sym;
5218 /* Create a symbol node if it doesn't already exist. */
5219 if (sym == NULL)
5221 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5222 gfc_current_ns);
5223 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5224 sym = info->u.rsym.sym;
5225 sym->module = gfc_get_string (info->u.rsym.module);
5227 if (info->u.rsym.binding_label)
5228 sym->binding_label =
5229 IDENTIFIER_POINTER (get_identifier
5230 (info->u.rsym.binding_label));
5233 st->n.sym = sym;
5234 st->n.sym->refs++;
5236 if (strcmp (name, p) != 0)
5237 sym->attr.use_rename = 1;
5239 if (name[0] != '_'
5240 || (strncmp (name, "__vtab_", 5) != 0
5241 && strncmp (name, "__vtype_", 6) != 0))
5242 sym->attr.use_only = only_flag;
5244 /* Store the symtree pointing to this symbol. */
5245 info->u.rsym.symtree = st;
5247 if (info->u.rsym.state == UNUSED)
5248 info->u.rsym.state = NEEDED;
5249 info->u.rsym.referenced = 1;
5254 mio_rparen ();
5256 /* Load intrinsic operator interfaces. */
5257 set_module_locus (&operator_interfaces);
5258 mio_lparen ();
5260 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5262 if (i == INTRINSIC_USER)
5263 continue;
5265 if (only_flag)
5267 u = find_use_operator ((gfc_intrinsic_op) i);
5269 if (u == NULL)
5271 skip_list ();
5272 continue;
5275 u->found = 1;
5278 mio_interface (&gfc_current_ns->op[i]);
5279 if (u && !gfc_current_ns->op[i])
5280 u->found = 0;
5283 mio_rparen ();
5285 /* Load generic and user operator interfaces. These must follow the
5286 loading of symtree because otherwise symbols can be marked as
5287 ambiguous. */
5289 set_module_locus (&user_operators);
5291 load_operator_interfaces ();
5292 load_generic_interfaces ();
5294 load_commons ();
5295 load_equiv ();
5297 /* Load OpenMP user defined reductions. */
5298 set_module_locus (&omp_udrs);
5299 load_omp_udrs ();
5301 /* At this point, we read those symbols that are needed but haven't
5302 been loaded yet. If one symbol requires another, the other gets
5303 marked as NEEDED if its previous state was UNUSED. */
5305 while (load_needed (pi_root));
5307 /* Make sure all elements of the rename-list were found in the module. */
5309 for (u = gfc_rename_list; u; u = u->next)
5311 if (u->found)
5312 continue;
5314 if (u->op == INTRINSIC_NONE)
5316 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5317 u->use_name, &u->where, module_name);
5318 continue;
5321 if (u->op == INTRINSIC_USER)
5323 gfc_error ("User operator %qs referenced at %L not found "
5324 "in module %qs", u->use_name, &u->where, module_name);
5325 continue;
5328 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5329 "in module %qs", gfc_op2string (u->op), &u->where,
5330 module_name);
5333 /* Clean up symbol nodes that were never loaded, create references
5334 to hidden symbols. */
5336 read_cleanup (pi_root);
5340 /* Given an access type that is specific to an entity and the default
5341 access, return nonzero if the entity is publicly accessible. If the
5342 element is declared as PUBLIC, then it is public; if declared
5343 PRIVATE, then private, and otherwise it is public unless the default
5344 access in this context has been declared PRIVATE. */
5346 static bool dump_smod = false;
5348 static bool
5349 check_access (gfc_access specific_access, gfc_access default_access)
5351 if (dump_smod)
5352 return true;
5354 if (specific_access == ACCESS_PUBLIC)
5355 return TRUE;
5356 if (specific_access == ACCESS_PRIVATE)
5357 return FALSE;
5359 if (flag_module_private)
5360 return default_access == ACCESS_PUBLIC;
5361 else
5362 return default_access != ACCESS_PRIVATE;
5366 bool
5367 gfc_check_symbol_access (gfc_symbol *sym)
5369 if (sym->attr.vtab || sym->attr.vtype)
5370 return true;
5371 else
5372 return check_access (sym->attr.access, sym->ns->default_access);
5376 /* A structure to remember which commons we've already written. */
5378 struct written_common
5380 BBT_HEADER(written_common);
5381 const char *name, *label;
5384 static struct written_common *written_commons = NULL;
5386 /* Comparison function used for balancing the binary tree. */
5388 static int
5389 compare_written_commons (void *a1, void *b1)
5391 const char *aname = ((struct written_common *) a1)->name;
5392 const char *alabel = ((struct written_common *) a1)->label;
5393 const char *bname = ((struct written_common *) b1)->name;
5394 const char *blabel = ((struct written_common *) b1)->label;
5395 int c = strcmp (aname, bname);
5397 return (c != 0 ? c : strcmp (alabel, blabel));
5400 /* Free a list of written commons. */
5402 static void
5403 free_written_common (struct written_common *w)
5405 if (!w)
5406 return;
5408 if (w->left)
5409 free_written_common (w->left);
5410 if (w->right)
5411 free_written_common (w->right);
5413 free (w);
5416 /* Write a common block to the module -- recursive helper function. */
5418 static void
5419 write_common_0 (gfc_symtree *st, bool this_module)
5421 gfc_common_head *p;
5422 const char * name;
5423 int flags;
5424 const char *label;
5425 struct written_common *w;
5426 bool write_me = true;
5428 if (st == NULL)
5429 return;
5431 write_common_0 (st->left, this_module);
5433 /* We will write out the binding label, or "" if no label given. */
5434 name = st->n.common->name;
5435 p = st->n.common;
5436 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5438 /* Check if we've already output this common. */
5439 w = written_commons;
5440 while (w)
5442 int c = strcmp (name, w->name);
5443 c = (c != 0 ? c : strcmp (label, w->label));
5444 if (c == 0)
5445 write_me = false;
5447 w = (c < 0) ? w->left : w->right;
5450 if (this_module && p->use_assoc)
5451 write_me = false;
5453 if (write_me)
5455 /* Write the common to the module. */
5456 mio_lparen ();
5457 mio_pool_string (&name);
5459 mio_symbol_ref (&p->head);
5460 flags = p->saved ? 1 : 0;
5461 if (p->threadprivate)
5462 flags |= 2;
5463 mio_integer (&flags);
5465 /* Write out whether the common block is bind(c) or not. */
5466 mio_integer (&(p->is_bind_c));
5468 mio_pool_string (&label);
5469 mio_rparen ();
5471 /* Record that we have written this common. */
5472 w = XCNEW (struct written_common);
5473 w->name = p->name;
5474 w->label = label;
5475 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5478 write_common_0 (st->right, this_module);
5482 /* Write a common, by initializing the list of written commons, calling
5483 the recursive function write_common_0() and cleaning up afterwards. */
5485 static void
5486 write_common (gfc_symtree *st)
5488 written_commons = NULL;
5489 write_common_0 (st, true);
5490 write_common_0 (st, false);
5491 free_written_common (written_commons);
5492 written_commons = NULL;
5496 /* Write the blank common block to the module. */
5498 static void
5499 write_blank_common (void)
5501 const char * name = BLANK_COMMON_NAME;
5502 int saved;
5503 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5504 this, but it hasn't been checked. Just making it so for now. */
5505 int is_bind_c = 0;
5507 if (gfc_current_ns->blank_common.head == NULL)
5508 return;
5510 mio_lparen ();
5512 mio_pool_string (&name);
5514 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5515 saved = gfc_current_ns->blank_common.saved;
5516 mio_integer (&saved);
5518 /* Write out whether the common block is bind(c) or not. */
5519 mio_integer (&is_bind_c);
5521 /* Write out an empty binding label. */
5522 write_atom (ATOM_STRING, "");
5524 mio_rparen ();
5528 /* Write equivalences to the module. */
5530 static void
5531 write_equiv (void)
5533 gfc_equiv *eq, *e;
5534 int num;
5536 num = 0;
5537 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5539 mio_lparen ();
5541 for (e = eq; e; e = e->eq)
5543 if (e->module == NULL)
5544 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5545 mio_allocated_string (e->module);
5546 mio_expr (&e->expr);
5549 num++;
5550 mio_rparen ();
5555 /* Write a symbol to the module. */
5557 static void
5558 write_symbol (int n, gfc_symbol *sym)
5560 const char *label;
5562 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5563 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5565 mio_integer (&n);
5567 if (gfc_fl_struct (sym->attr.flavor))
5569 const char *name;
5570 name = gfc_dt_upper_string (sym->name);
5571 mio_pool_string (&name);
5573 else
5574 mio_pool_string (&sym->name);
5576 mio_pool_string (&sym->module);
5577 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5579 label = sym->binding_label;
5580 mio_pool_string (&label);
5582 else
5583 write_atom (ATOM_STRING, "");
5585 mio_pointer_ref (&sym->ns);
5587 mio_symbol (sym);
5588 write_char ('\n');
5592 /* Recursive traversal function to write the initial set of symbols to
5593 the module. We check to see if the symbol should be written
5594 according to the access specification. */
5596 static void
5597 write_symbol0 (gfc_symtree *st)
5599 gfc_symbol *sym;
5600 pointer_info *p;
5601 bool dont_write = false;
5603 if (st == NULL)
5604 return;
5606 write_symbol0 (st->left);
5608 sym = st->n.sym;
5609 if (sym->module == NULL)
5610 sym->module = module_name;
5612 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5613 && !sym->attr.subroutine && !sym->attr.function)
5614 dont_write = true;
5616 if (!gfc_check_symbol_access (sym))
5617 dont_write = true;
5619 if (!dont_write)
5621 p = get_pointer (sym);
5622 if (p->type == P_UNKNOWN)
5623 p->type = P_SYMBOL;
5625 if (p->u.wsym.state != WRITTEN)
5627 write_symbol (p->integer, sym);
5628 p->u.wsym.state = WRITTEN;
5632 write_symbol0 (st->right);
5636 static void
5637 write_omp_udr (gfc_omp_udr *udr)
5639 switch (udr->rop)
5641 case OMP_REDUCTION_USER:
5642 /* Non-operators can't be used outside of the module. */
5643 if (udr->name[0] != '.')
5644 return;
5645 else
5647 gfc_symtree *st;
5648 size_t len = strlen (udr->name + 1);
5649 char *name = XALLOCAVEC (char, len);
5650 memcpy (name, udr->name, len - 1);
5651 name[len - 1] = '\0';
5652 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
5653 /* If corresponding user operator is private, don't write
5654 the UDR. */
5655 if (st != NULL)
5657 gfc_user_op *uop = st->n.uop;
5658 if (!check_access (uop->access, uop->ns->default_access))
5659 return;
5662 break;
5663 case OMP_REDUCTION_PLUS:
5664 case OMP_REDUCTION_MINUS:
5665 case OMP_REDUCTION_TIMES:
5666 case OMP_REDUCTION_AND:
5667 case OMP_REDUCTION_OR:
5668 case OMP_REDUCTION_EQV:
5669 case OMP_REDUCTION_NEQV:
5670 /* If corresponding operator is private, don't write the UDR. */
5671 if (!check_access (gfc_current_ns->operator_access[udr->rop],
5672 gfc_current_ns->default_access))
5673 return;
5674 break;
5675 default:
5676 break;
5678 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
5680 /* If derived type is private, don't write the UDR. */
5681 if (!gfc_check_symbol_access (udr->ts.u.derived))
5682 return;
5685 mio_lparen ();
5686 mio_pool_string (&udr->name);
5687 mio_typespec (&udr->ts);
5688 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
5689 if (udr->initializer_ns)
5690 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5691 udr->initializer_ns, true);
5692 mio_rparen ();
5696 static void
5697 write_omp_udrs (gfc_symtree *st)
5699 if (st == NULL)
5700 return;
5702 write_omp_udrs (st->left);
5703 gfc_omp_udr *udr;
5704 for (udr = st->n.omp_udr; udr; udr = udr->next)
5705 write_omp_udr (udr);
5706 write_omp_udrs (st->right);
5710 /* Type for the temporary tree used when writing secondary symbols. */
5712 struct sorted_pointer_info
5714 BBT_HEADER (sorted_pointer_info);
5716 pointer_info *p;
5719 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5721 /* Recursively traverse the temporary tree, free its contents. */
5723 static void
5724 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5726 if (!p)
5727 return;
5729 free_sorted_pointer_info_tree (p->left);
5730 free_sorted_pointer_info_tree (p->right);
5732 free (p);
5735 /* Comparison function for the temporary tree. */
5737 static int
5738 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5740 sorted_pointer_info *spi1, *spi2;
5741 spi1 = (sorted_pointer_info *)_spi1;
5742 spi2 = (sorted_pointer_info *)_spi2;
5744 if (spi1->p->integer < spi2->p->integer)
5745 return -1;
5746 if (spi1->p->integer > spi2->p->integer)
5747 return 1;
5748 return 0;
5752 /* Finds the symbols that need to be written and collects them in the
5753 sorted_pi tree so that they can be traversed in an order
5754 independent of memory addresses. */
5756 static void
5757 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5759 if (!p)
5760 return;
5762 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5764 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5765 sp->p = p;
5767 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5770 find_symbols_to_write (tree, p->left);
5771 find_symbols_to_write (tree, p->right);
5775 /* Recursive function that traverses the tree of symbols that need to be
5776 written and writes them in order. */
5778 static void
5779 write_symbol1_recursion (sorted_pointer_info *sp)
5781 if (!sp)
5782 return;
5784 write_symbol1_recursion (sp->left);
5786 pointer_info *p1 = sp->p;
5787 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5789 p1->u.wsym.state = WRITTEN;
5790 write_symbol (p1->integer, p1->u.wsym.sym);
5791 p1->u.wsym.sym->attr.public_used = 1;
5793 write_symbol1_recursion (sp->right);
5797 /* Write the secondary set of symbols to the module file. These are
5798 symbols that were not public yet are needed by the public symbols
5799 or another dependent symbol. The act of writing a symbol can add
5800 symbols to the pointer_info tree, so we return nonzero if a symbol
5801 was written and pass that information upwards. The caller will
5802 then call this function again until nothing was written. It uses
5803 the utility functions and a temporary tree to ensure a reproducible
5804 ordering of the symbol output and thus the module file. */
5806 static int
5807 write_symbol1 (pointer_info *p)
5809 if (!p)
5810 return 0;
5812 /* Put symbols that need to be written into a tree sorted on the
5813 integer field. */
5815 sorted_pointer_info *spi_root = NULL;
5816 find_symbols_to_write (&spi_root, p);
5818 /* No symbols to write, return. */
5819 if (!spi_root)
5820 return 0;
5822 /* Otherwise, write and free the tree again. */
5823 write_symbol1_recursion (spi_root);
5824 free_sorted_pointer_info_tree (spi_root);
5826 return 1;
5830 /* Write operator interfaces associated with a symbol. */
5832 static void
5833 write_operator (gfc_user_op *uop)
5835 static char nullstring[] = "";
5836 const char *p = nullstring;
5838 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5839 return;
5841 mio_symbol_interface (&uop->name, &p, &uop->op);
5845 /* Write generic interfaces from the namespace sym_root. */
5847 static void
5848 write_generic (gfc_symtree *st)
5850 gfc_symbol *sym;
5852 if (st == NULL)
5853 return;
5855 write_generic (st->left);
5857 sym = st->n.sym;
5858 if (sym && !check_unique_name (st->name)
5859 && sym->generic && gfc_check_symbol_access (sym))
5861 if (!sym->module)
5862 sym->module = module_name;
5864 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5867 write_generic (st->right);
5871 static void
5872 write_symtree (gfc_symtree *st)
5874 gfc_symbol *sym;
5875 pointer_info *p;
5877 sym = st->n.sym;
5879 /* A symbol in an interface body must not be visible in the
5880 module file. */
5881 if (sym->ns != gfc_current_ns
5882 && sym->ns->proc_name
5883 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5884 return;
5886 if (!gfc_check_symbol_access (sym)
5887 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5888 && !sym->attr.subroutine && !sym->attr.function))
5889 return;
5891 if (check_unique_name (st->name))
5892 return;
5894 p = find_pointer (sym);
5895 if (p == NULL)
5896 gfc_internal_error ("write_symtree(): Symbol not written");
5898 mio_pool_string (&st->name);
5899 mio_integer (&st->ambiguous);
5900 mio_integer (&p->integer);
5904 static void
5905 write_module (void)
5907 int i;
5909 /* Write the operator interfaces. */
5910 mio_lparen ();
5912 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5914 if (i == INTRINSIC_USER)
5915 continue;
5917 mio_interface (check_access (gfc_current_ns->operator_access[i],
5918 gfc_current_ns->default_access)
5919 ? &gfc_current_ns->op[i] : NULL);
5922 mio_rparen ();
5923 write_char ('\n');
5924 write_char ('\n');
5926 mio_lparen ();
5927 gfc_traverse_user_op (gfc_current_ns, write_operator);
5928 mio_rparen ();
5929 write_char ('\n');
5930 write_char ('\n');
5932 mio_lparen ();
5933 write_generic (gfc_current_ns->sym_root);
5934 mio_rparen ();
5935 write_char ('\n');
5936 write_char ('\n');
5938 mio_lparen ();
5939 write_blank_common ();
5940 write_common (gfc_current_ns->common_root);
5941 mio_rparen ();
5942 write_char ('\n');
5943 write_char ('\n');
5945 mio_lparen ();
5946 write_equiv ();
5947 mio_rparen ();
5948 write_char ('\n');
5949 write_char ('\n');
5951 mio_lparen ();
5952 write_omp_udrs (gfc_current_ns->omp_udr_root);
5953 mio_rparen ();
5954 write_char ('\n');
5955 write_char ('\n');
5957 /* Write symbol information. First we traverse all symbols in the
5958 primary namespace, writing those that need to be written.
5959 Sometimes writing one symbol will cause another to need to be
5960 written. A list of these symbols ends up on the write stack, and
5961 we end by popping the bottom of the stack and writing the symbol
5962 until the stack is empty. */
5964 mio_lparen ();
5966 write_symbol0 (gfc_current_ns->sym_root);
5967 while (write_symbol1 (pi_root))
5968 /* Nothing. */;
5970 mio_rparen ();
5972 write_char ('\n');
5973 write_char ('\n');
5975 mio_lparen ();
5976 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5977 mio_rparen ();
5981 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5982 true on success, false on failure. */
5984 static bool
5985 read_crc32_from_module_file (const char* filename, uLong* crc)
5987 FILE *file;
5988 char buf[4];
5989 unsigned int val;
5991 /* Open the file in binary mode. */
5992 if ((file = fopen (filename, "rb")) == NULL)
5993 return false;
5995 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5996 file. See RFC 1952. */
5997 if (fseek (file, -8, SEEK_END) != 0)
5999 fclose (file);
6000 return false;
6003 /* Read the CRC32. */
6004 if (fread (buf, 1, 4, file) != 4)
6006 fclose (file);
6007 return false;
6010 /* Close the file. */
6011 fclose (file);
6013 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6014 + ((buf[3] & 0xFF) << 24);
6015 *crc = val;
6017 /* For debugging, the CRC value printed in hexadecimal should match
6018 the CRC printed by "zcat -l -v filename".
6019 printf("CRC of file %s is %x\n", filename, val); */
6021 return true;
6025 /* Given module, dump it to disk. If there was an error while
6026 processing the module, dump_flag will be set to zero and we delete
6027 the module file, even if it was already there. */
6029 static void
6030 dump_module (const char *name, int dump_flag)
6032 int n;
6033 char *filename, *filename_tmp;
6034 uLong crc, crc_old;
6036 module_name = gfc_get_string (name);
6038 if (dump_smod)
6040 name = submodule_name;
6041 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6043 else
6044 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6046 if (gfc_option.module_dir != NULL)
6048 n += strlen (gfc_option.module_dir);
6049 filename = (char *) alloca (n);
6050 strcpy (filename, gfc_option.module_dir);
6051 strcat (filename, name);
6053 else
6055 filename = (char *) alloca (n);
6056 strcpy (filename, name);
6059 if (dump_smod)
6060 strcat (filename, SUBMODULE_EXTENSION);
6061 else
6062 strcat (filename, MODULE_EXTENSION);
6064 /* Name of the temporary file used to write the module. */
6065 filename_tmp = (char *) alloca (n + 1);
6066 strcpy (filename_tmp, filename);
6067 strcat (filename_tmp, "0");
6069 /* There was an error while processing the module. We delete the
6070 module file, even if it was already there. */
6071 if (!dump_flag)
6073 remove (filename);
6074 return;
6077 if (gfc_cpp_makedep ())
6078 gfc_cpp_add_target (filename);
6080 /* Write the module to the temporary file. */
6081 module_fp = gzopen (filename_tmp, "w");
6082 if (module_fp == NULL)
6083 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6084 filename_tmp, xstrerror (errno));
6086 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6087 MOD_VERSION, gfc_source_file);
6089 /* Write the module itself. */
6090 iomode = IO_OUTPUT;
6092 init_pi_tree ();
6094 write_module ();
6096 free_pi_tree (pi_root);
6097 pi_root = NULL;
6099 write_char ('\n');
6101 if (gzclose (module_fp))
6102 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6103 filename_tmp, xstrerror (errno));
6105 /* Read the CRC32 from the gzip trailers of the module files and
6106 compare. */
6107 if (!read_crc32_from_module_file (filename_tmp, &crc)
6108 || !read_crc32_from_module_file (filename, &crc_old)
6109 || crc_old != crc)
6111 /* Module file have changed, replace the old one. */
6112 if (remove (filename) && errno != ENOENT)
6113 gfc_fatal_error ("Can't delete module file %qs: %s", filename,
6114 xstrerror (errno));
6115 if (rename (filename_tmp, filename))
6116 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6117 filename_tmp, filename, xstrerror (errno));
6119 else
6121 if (remove (filename_tmp))
6122 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6123 filename_tmp, xstrerror (errno));
6128 void
6129 gfc_dump_module (const char *name, int dump_flag)
6131 if (gfc_state_stack->state == COMP_SUBMODULE)
6132 dump_smod = true;
6133 else
6134 dump_smod =false;
6136 no_module_procedures = true;
6137 dump_module (name, dump_flag);
6139 if (no_module_procedures || dump_smod)
6140 return;
6142 /* Write a submodule file from a module. The 'dump_smod' flag switches
6143 off the check for PRIVATE entities. */
6144 dump_smod = true;
6145 submodule_name = module_name;
6146 dump_module (name, dump_flag);
6147 dump_smod = false;
6150 static void
6151 create_intrinsic_function (const char *name, int id,
6152 const char *modname, intmod_id module,
6153 bool subroutine, gfc_symbol *result_type)
6155 gfc_intrinsic_sym *isym;
6156 gfc_symtree *tmp_symtree;
6157 gfc_symbol *sym;
6159 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6160 if (tmp_symtree)
6162 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6163 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6164 return;
6165 gfc_error ("Symbol %qs at %C already declared", name);
6166 return;
6169 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6170 sym = tmp_symtree->n.sym;
6172 if (subroutine)
6174 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6175 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6176 sym->attr.subroutine = 1;
6178 else
6180 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6181 isym = gfc_intrinsic_function_by_id (isym_id);
6183 sym->attr.function = 1;
6184 if (result_type)
6186 sym->ts.type = BT_DERIVED;
6187 sym->ts.u.derived = result_type;
6188 sym->ts.is_c_interop = 1;
6189 isym->ts.f90_type = BT_VOID;
6190 isym->ts.type = BT_DERIVED;
6191 isym->ts.f90_type = BT_VOID;
6192 isym->ts.u.derived = result_type;
6193 isym->ts.is_c_interop = 1;
6196 gcc_assert (isym);
6198 sym->attr.flavor = FL_PROCEDURE;
6199 sym->attr.intrinsic = 1;
6201 sym->module = gfc_get_string (modname);
6202 sym->attr.use_assoc = 1;
6203 sym->from_intmod = module;
6204 sym->intmod_sym_id = id;
6208 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6209 the current namespace for all named constants, pointer types, and
6210 procedures in the module unless the only clause was used or a rename
6211 list was provided. */
6213 static void
6214 import_iso_c_binding_module (void)
6216 gfc_symbol *mod_sym = NULL, *return_type;
6217 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6218 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6219 const char *iso_c_module_name = "__iso_c_binding";
6220 gfc_use_rename *u;
6221 int i;
6222 bool want_c_ptr = false, want_c_funptr = false;
6224 /* Look only in the current namespace. */
6225 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6227 if (mod_symtree == NULL)
6229 /* symtree doesn't already exist in current namespace. */
6230 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6231 false);
6233 if (mod_symtree != NULL)
6234 mod_sym = mod_symtree->n.sym;
6235 else
6236 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6237 "create symbol for %s", iso_c_module_name);
6239 mod_sym->attr.flavor = FL_MODULE;
6240 mod_sym->attr.intrinsic = 1;
6241 mod_sym->module = gfc_get_string (iso_c_module_name);
6242 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6245 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6246 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6247 need C_(FUN)PTR. */
6248 for (u = gfc_rename_list; u; u = u->next)
6250 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6251 u->use_name) == 0)
6252 want_c_ptr = true;
6253 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6254 u->use_name) == 0)
6255 want_c_ptr = true;
6256 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6257 u->use_name) == 0)
6258 want_c_funptr = true;
6259 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6260 u->use_name) == 0)
6261 want_c_funptr = true;
6262 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6263 u->use_name) == 0)
6265 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6266 (iso_c_binding_symbol)
6267 ISOCBINDING_PTR,
6268 u->local_name[0] ? u->local_name
6269 : u->use_name,
6270 NULL, false);
6272 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6273 u->use_name) == 0)
6275 c_funptr
6276 = generate_isocbinding_symbol (iso_c_module_name,
6277 (iso_c_binding_symbol)
6278 ISOCBINDING_FUNPTR,
6279 u->local_name[0] ? u->local_name
6280 : u->use_name,
6281 NULL, false);
6285 if ((want_c_ptr || !only_flag) && !c_ptr)
6286 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6287 (iso_c_binding_symbol)
6288 ISOCBINDING_PTR,
6289 NULL, NULL, only_flag);
6290 if ((want_c_funptr || !only_flag) && !c_funptr)
6291 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6292 (iso_c_binding_symbol)
6293 ISOCBINDING_FUNPTR,
6294 NULL, NULL, only_flag);
6296 /* Generate the symbols for the named constants representing
6297 the kinds for intrinsic data types. */
6298 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6300 bool found = false;
6301 for (u = gfc_rename_list; u; u = u->next)
6302 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6304 bool not_in_std;
6305 const char *name;
6306 u->found = 1;
6307 found = true;
6309 switch (i)
6311 #define NAMED_FUNCTION(a,b,c,d) \
6312 case a: \
6313 not_in_std = (gfc_option.allow_std & d) == 0; \
6314 name = b; \
6315 break;
6316 #define NAMED_SUBROUTINE(a,b,c,d) \
6317 case a: \
6318 not_in_std = (gfc_option.allow_std & d) == 0; \
6319 name = b; \
6320 break;
6321 #define NAMED_INTCST(a,b,c,d) \
6322 case a: \
6323 not_in_std = (gfc_option.allow_std & d) == 0; \
6324 name = b; \
6325 break;
6326 #define NAMED_REALCST(a,b,c,d) \
6327 case a: \
6328 not_in_std = (gfc_option.allow_std & d) == 0; \
6329 name = b; \
6330 break;
6331 #define NAMED_CMPXCST(a,b,c,d) \
6332 case a: \
6333 not_in_std = (gfc_option.allow_std & d) == 0; \
6334 name = b; \
6335 break;
6336 #include "iso-c-binding.def"
6337 default:
6338 not_in_std = false;
6339 name = "";
6342 if (not_in_std)
6344 gfc_error ("The symbol %qs, referenced at %L, is not "
6345 "in the selected standard", name, &u->where);
6346 continue;
6349 switch (i)
6351 #define NAMED_FUNCTION(a,b,c,d) \
6352 case a: \
6353 if (a == ISOCBINDING_LOC) \
6354 return_type = c_ptr->n.sym; \
6355 else if (a == ISOCBINDING_FUNLOC) \
6356 return_type = c_funptr->n.sym; \
6357 else \
6358 return_type = NULL; \
6359 create_intrinsic_function (u->local_name[0] \
6360 ? u->local_name : u->use_name, \
6361 a, iso_c_module_name, \
6362 INTMOD_ISO_C_BINDING, false, \
6363 return_type); \
6364 break;
6365 #define NAMED_SUBROUTINE(a,b,c,d) \
6366 case a: \
6367 create_intrinsic_function (u->local_name[0] ? u->local_name \
6368 : u->use_name, \
6369 a, iso_c_module_name, \
6370 INTMOD_ISO_C_BINDING, true, NULL); \
6371 break;
6372 #include "iso-c-binding.def"
6374 case ISOCBINDING_PTR:
6375 case ISOCBINDING_FUNPTR:
6376 /* Already handled above. */
6377 break;
6378 default:
6379 if (i == ISOCBINDING_NULL_PTR)
6380 tmp_symtree = c_ptr;
6381 else if (i == ISOCBINDING_NULL_FUNPTR)
6382 tmp_symtree = c_funptr;
6383 else
6384 tmp_symtree = NULL;
6385 generate_isocbinding_symbol (iso_c_module_name,
6386 (iso_c_binding_symbol) i,
6387 u->local_name[0]
6388 ? u->local_name : u->use_name,
6389 tmp_symtree, false);
6393 if (!found && !only_flag)
6395 /* Skip, if the symbol is not in the enabled standard. */
6396 switch (i)
6398 #define NAMED_FUNCTION(a,b,c,d) \
6399 case a: \
6400 if ((gfc_option.allow_std & d) == 0) \
6401 continue; \
6402 break;
6403 #define NAMED_SUBROUTINE(a,b,c,d) \
6404 case a: \
6405 if ((gfc_option.allow_std & d) == 0) \
6406 continue; \
6407 break;
6408 #define NAMED_INTCST(a,b,c,d) \
6409 case a: \
6410 if ((gfc_option.allow_std & d) == 0) \
6411 continue; \
6412 break;
6413 #define NAMED_REALCST(a,b,c,d) \
6414 case a: \
6415 if ((gfc_option.allow_std & d) == 0) \
6416 continue; \
6417 break;
6418 #define NAMED_CMPXCST(a,b,c,d) \
6419 case a: \
6420 if ((gfc_option.allow_std & d) == 0) \
6421 continue; \
6422 break;
6423 #include "iso-c-binding.def"
6424 default:
6425 ; /* Not GFC_STD_* versioned. */
6428 switch (i)
6430 #define NAMED_FUNCTION(a,b,c,d) \
6431 case a: \
6432 if (a == ISOCBINDING_LOC) \
6433 return_type = c_ptr->n.sym; \
6434 else if (a == ISOCBINDING_FUNLOC) \
6435 return_type = c_funptr->n.sym; \
6436 else \
6437 return_type = NULL; \
6438 create_intrinsic_function (b, a, iso_c_module_name, \
6439 INTMOD_ISO_C_BINDING, false, \
6440 return_type); \
6441 break;
6442 #define NAMED_SUBROUTINE(a,b,c,d) \
6443 case a: \
6444 create_intrinsic_function (b, a, iso_c_module_name, \
6445 INTMOD_ISO_C_BINDING, true, NULL); \
6446 break;
6447 #include "iso-c-binding.def"
6449 case ISOCBINDING_PTR:
6450 case ISOCBINDING_FUNPTR:
6451 /* Already handled above. */
6452 break;
6453 default:
6454 if (i == ISOCBINDING_NULL_PTR)
6455 tmp_symtree = c_ptr;
6456 else if (i == ISOCBINDING_NULL_FUNPTR)
6457 tmp_symtree = c_funptr;
6458 else
6459 tmp_symtree = NULL;
6460 generate_isocbinding_symbol (iso_c_module_name,
6461 (iso_c_binding_symbol) i, NULL,
6462 tmp_symtree, false);
6467 for (u = gfc_rename_list; u; u = u->next)
6469 if (u->found)
6470 continue;
6472 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6473 "module ISO_C_BINDING", u->use_name, &u->where);
6478 /* Add an integer named constant from a given module. */
6480 static void
6481 create_int_parameter (const char *name, int value, const char *modname,
6482 intmod_id module, int id)
6484 gfc_symtree *tmp_symtree;
6485 gfc_symbol *sym;
6487 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6488 if (tmp_symtree != NULL)
6490 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6491 return;
6492 else
6493 gfc_error ("Symbol %qs already declared", name);
6496 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6497 sym = tmp_symtree->n.sym;
6499 sym->module = gfc_get_string (modname);
6500 sym->attr.flavor = FL_PARAMETER;
6501 sym->ts.type = BT_INTEGER;
6502 sym->ts.kind = gfc_default_integer_kind;
6503 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6504 sym->attr.use_assoc = 1;
6505 sym->from_intmod = module;
6506 sym->intmod_sym_id = id;
6510 /* Value is already contained by the array constructor, but not
6511 yet the shape. */
6513 static void
6514 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6515 const char *modname, intmod_id module, int id)
6517 gfc_symtree *tmp_symtree;
6518 gfc_symbol *sym;
6520 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6521 if (tmp_symtree != NULL)
6523 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6524 return;
6525 else
6526 gfc_error ("Symbol %qs already declared", name);
6529 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6530 sym = tmp_symtree->n.sym;
6532 sym->module = gfc_get_string (modname);
6533 sym->attr.flavor = FL_PARAMETER;
6534 sym->ts.type = BT_INTEGER;
6535 sym->ts.kind = gfc_default_integer_kind;
6536 sym->attr.use_assoc = 1;
6537 sym->from_intmod = module;
6538 sym->intmod_sym_id = id;
6539 sym->attr.dimension = 1;
6540 sym->as = gfc_get_array_spec ();
6541 sym->as->rank = 1;
6542 sym->as->type = AS_EXPLICIT;
6543 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6544 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6546 sym->value = value;
6547 sym->value->shape = gfc_get_shape (1);
6548 mpz_init_set_ui (sym->value->shape[0], size);
6552 /* Add an derived type for a given module. */
6554 static void
6555 create_derived_type (const char *name, const char *modname,
6556 intmod_id module, int id)
6558 gfc_symtree *tmp_symtree;
6559 gfc_symbol *sym, *dt_sym;
6560 gfc_interface *intr, *head;
6562 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6563 if (tmp_symtree != NULL)
6565 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6566 return;
6567 else
6568 gfc_error ("Symbol %qs already declared", name);
6571 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6572 sym = tmp_symtree->n.sym;
6573 sym->module = gfc_get_string (modname);
6574 sym->from_intmod = module;
6575 sym->intmod_sym_id = id;
6576 sym->attr.flavor = FL_PROCEDURE;
6577 sym->attr.function = 1;
6578 sym->attr.generic = 1;
6580 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
6581 gfc_current_ns, &tmp_symtree, false);
6582 dt_sym = tmp_symtree->n.sym;
6583 dt_sym->name = gfc_get_string (sym->name);
6584 dt_sym->attr.flavor = FL_DERIVED;
6585 dt_sym->attr.private_comp = 1;
6586 dt_sym->attr.zero_comp = 1;
6587 dt_sym->attr.use_assoc = 1;
6588 dt_sym->module = gfc_get_string (modname);
6589 dt_sym->from_intmod = module;
6590 dt_sym->intmod_sym_id = id;
6592 head = sym->generic;
6593 intr = gfc_get_interface ();
6594 intr->sym = dt_sym;
6595 intr->where = gfc_current_locus;
6596 intr->next = head;
6597 sym->generic = intr;
6598 sym->attr.if_source = IFSRC_DECL;
6602 /* Read the contents of the module file into a temporary buffer. */
6604 static void
6605 read_module_to_tmpbuf ()
6607 /* We don't know the uncompressed size, so enlarge the buffer as
6608 needed. */
6609 int cursz = 4096;
6610 int rsize = cursz;
6611 int len = 0;
6613 module_content = XNEWVEC (char, cursz);
6615 while (1)
6617 int nread = gzread (module_fp, module_content + len, rsize);
6618 len += nread;
6619 if (nread < rsize)
6620 break;
6621 cursz *= 2;
6622 module_content = XRESIZEVEC (char, module_content, cursz);
6623 rsize = cursz - len;
6626 module_content = XRESIZEVEC (char, module_content, len + 1);
6627 module_content[len] = '\0';
6629 module_pos = 0;
6633 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6635 static void
6636 use_iso_fortran_env_module (void)
6638 static char mod[] = "iso_fortran_env";
6639 gfc_use_rename *u;
6640 gfc_symbol *mod_sym;
6641 gfc_symtree *mod_symtree;
6642 gfc_expr *expr;
6643 int i, j;
6645 intmod_sym symbol[] = {
6646 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6647 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6648 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6649 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6650 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6651 #include "iso-fortran-env.def"
6652 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6654 i = 0;
6655 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6656 #include "iso-fortran-env.def"
6658 /* Generate the symbol for the module itself. */
6659 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6660 if (mod_symtree == NULL)
6662 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6663 gcc_assert (mod_symtree);
6664 mod_sym = mod_symtree->n.sym;
6666 mod_sym->attr.flavor = FL_MODULE;
6667 mod_sym->attr.intrinsic = 1;
6668 mod_sym->module = gfc_get_string (mod);
6669 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6671 else
6672 if (!mod_symtree->n.sym->attr.intrinsic)
6673 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6674 "non-intrinsic module name used previously", mod);
6676 /* Generate the symbols for the module integer named constants. */
6678 for (i = 0; symbol[i].name; i++)
6680 bool found = false;
6681 for (u = gfc_rename_list; u; u = u->next)
6683 if (strcmp (symbol[i].name, u->use_name) == 0)
6685 found = true;
6686 u->found = 1;
6688 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
6689 "referenced at %L, is not in the selected "
6690 "standard", symbol[i].name, &u->where))
6691 continue;
6693 if ((flag_default_integer || flag_default_real)
6694 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6695 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
6696 "constant from intrinsic module "
6697 "ISO_FORTRAN_ENV at %L is incompatible with "
6698 "option %qs", &u->where,
6699 flag_default_integer
6700 ? "-fdefault-integer-8"
6701 : "-fdefault-real-8");
6702 switch (symbol[i].id)
6704 #define NAMED_INTCST(a,b,c,d) \
6705 case a:
6706 #include "iso-fortran-env.def"
6707 create_int_parameter (u->local_name[0] ? u->local_name
6708 : u->use_name,
6709 symbol[i].value, mod,
6710 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6711 break;
6713 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6714 case a:\
6715 expr = gfc_get_array_expr (BT_INTEGER, \
6716 gfc_default_integer_kind,\
6717 NULL); \
6718 for (j = 0; KINDS[j].kind != 0; j++) \
6719 gfc_constructor_append_expr (&expr->value.constructor, \
6720 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6721 KINDS[j].kind), NULL); \
6722 create_int_parameter_array (u->local_name[0] ? u->local_name \
6723 : u->use_name, \
6724 j, expr, mod, \
6725 INTMOD_ISO_FORTRAN_ENV, \
6726 symbol[i].id); \
6727 break;
6728 #include "iso-fortran-env.def"
6730 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6731 case a:
6732 #include "iso-fortran-env.def"
6733 create_derived_type (u->local_name[0] ? u->local_name
6734 : u->use_name,
6735 mod, INTMOD_ISO_FORTRAN_ENV,
6736 symbol[i].id);
6737 break;
6739 #define NAMED_FUNCTION(a,b,c,d) \
6740 case a:
6741 #include "iso-fortran-env.def"
6742 create_intrinsic_function (u->local_name[0] ? u->local_name
6743 : u->use_name,
6744 symbol[i].id, mod,
6745 INTMOD_ISO_FORTRAN_ENV, false,
6746 NULL);
6747 break;
6749 default:
6750 gcc_unreachable ();
6755 if (!found && !only_flag)
6757 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6758 continue;
6760 if ((flag_default_integer || flag_default_real)
6761 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6762 gfc_warning_now (0,
6763 "Use of the NUMERIC_STORAGE_SIZE named constant "
6764 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6765 "incompatible with option %s",
6766 flag_default_integer
6767 ? "-fdefault-integer-8" : "-fdefault-real-8");
6769 switch (symbol[i].id)
6771 #define NAMED_INTCST(a,b,c,d) \
6772 case a:
6773 #include "iso-fortran-env.def"
6774 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6775 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6776 break;
6778 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6779 case a:\
6780 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6781 NULL); \
6782 for (j = 0; KINDS[j].kind != 0; j++) \
6783 gfc_constructor_append_expr (&expr->value.constructor, \
6784 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6785 KINDS[j].kind), NULL); \
6786 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6787 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6788 break;
6789 #include "iso-fortran-env.def"
6791 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6792 case a:
6793 #include "iso-fortran-env.def"
6794 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6795 symbol[i].id);
6796 break;
6798 #define NAMED_FUNCTION(a,b,c,d) \
6799 case a:
6800 #include "iso-fortran-env.def"
6801 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6802 INTMOD_ISO_FORTRAN_ENV, false,
6803 NULL);
6804 break;
6806 default:
6807 gcc_unreachable ();
6812 for (u = gfc_rename_list; u; u = u->next)
6814 if (u->found)
6815 continue;
6817 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6818 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6823 /* Process a USE directive. */
6825 static void
6826 gfc_use_module (gfc_use_list *module)
6828 char *filename;
6829 gfc_state_data *p;
6830 int c, line, start;
6831 gfc_symtree *mod_symtree;
6832 gfc_use_list *use_stmt;
6833 locus old_locus = gfc_current_locus;
6835 gfc_current_locus = module->where;
6836 module_name = module->module_name;
6837 gfc_rename_list = module->rename;
6838 only_flag = module->only_flag;
6839 current_intmod = INTMOD_NONE;
6841 if (!only_flag)
6842 gfc_warning_now (OPT_Wuse_without_only,
6843 "USE statement at %C has no ONLY qualifier");
6845 if (gfc_state_stack->state == COMP_MODULE
6846 || module->submodule_name == NULL)
6848 filename = XALLOCAVEC (char, strlen (module_name)
6849 + strlen (MODULE_EXTENSION) + 1);
6850 strcpy (filename, module_name);
6851 strcat (filename, MODULE_EXTENSION);
6853 else
6855 filename = XALLOCAVEC (char, strlen (module->submodule_name)
6856 + strlen (SUBMODULE_EXTENSION) + 1);
6857 strcpy (filename, module->submodule_name);
6858 strcat (filename, SUBMODULE_EXTENSION);
6861 /* First, try to find an non-intrinsic module, unless the USE statement
6862 specified that the module is intrinsic. */
6863 module_fp = NULL;
6864 if (!module->intrinsic)
6865 module_fp = gzopen_included_file (filename, true, true);
6867 /* Then, see if it's an intrinsic one, unless the USE statement
6868 specified that the module is non-intrinsic. */
6869 if (module_fp == NULL && !module->non_intrinsic)
6871 if (strcmp (module_name, "iso_fortran_env") == 0
6872 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6873 "intrinsic module at %C"))
6875 use_iso_fortran_env_module ();
6876 free_rename (module->rename);
6877 module->rename = NULL;
6878 gfc_current_locus = old_locus;
6879 module->intrinsic = true;
6880 return;
6883 if (strcmp (module_name, "iso_c_binding") == 0
6884 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6886 import_iso_c_binding_module();
6887 free_rename (module->rename);
6888 module->rename = NULL;
6889 gfc_current_locus = old_locus;
6890 module->intrinsic = true;
6891 return;
6894 module_fp = gzopen_intrinsic_module (filename);
6896 if (module_fp == NULL && module->intrinsic)
6897 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6898 module_name);
6900 /* Check for the IEEE modules, so we can mark their symbols
6901 accordingly when we read them. */
6902 if (strcmp (module_name, "ieee_features") == 0
6903 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
6905 current_intmod = INTMOD_IEEE_FEATURES;
6907 else if (strcmp (module_name, "ieee_exceptions") == 0
6908 && gfc_notify_std (GFC_STD_F2003,
6909 "IEEE_EXCEPTIONS module at %C"))
6911 current_intmod = INTMOD_IEEE_EXCEPTIONS;
6913 else if (strcmp (module_name, "ieee_arithmetic") == 0
6914 && gfc_notify_std (GFC_STD_F2003,
6915 "IEEE_ARITHMETIC module at %C"))
6917 current_intmod = INTMOD_IEEE_ARITHMETIC;
6921 if (module_fp == NULL)
6922 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6923 filename, xstrerror (errno));
6925 /* Check that we haven't already USEd an intrinsic module with the
6926 same name. */
6928 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6929 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6930 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6931 "intrinsic module name used previously", module_name);
6933 iomode = IO_INPUT;
6934 module_line = 1;
6935 module_column = 1;
6936 start = 0;
6938 read_module_to_tmpbuf ();
6939 gzclose (module_fp);
6941 /* Skip the first line of the module, after checking that this is
6942 a gfortran module file. */
6943 line = 0;
6944 while (line < 1)
6946 c = module_char ();
6947 if (c == EOF)
6948 bad_module ("Unexpected end of module");
6949 if (start++ < 3)
6950 parse_name (c);
6951 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6952 || (start == 2 && strcmp (atom_name, " module") != 0))
6953 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6954 " module file", filename);
6955 if (start == 3)
6957 if (strcmp (atom_name, " version") != 0
6958 || module_char () != ' '
6959 || parse_atom () != ATOM_STRING
6960 || strcmp (atom_string, MOD_VERSION))
6961 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6962 " because it was created by a different"
6963 " version of GNU Fortran", filename);
6965 free (atom_string);
6968 if (c == '\n')
6969 line++;
6972 /* Make sure we're not reading the same module that we may be building. */
6973 for (p = gfc_state_stack; p; p = p->previous)
6974 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
6975 && strcmp (p->sym->name, module_name) == 0)
6976 gfc_fatal_error ("Can't USE the same %smodule we're building!",
6977 p->state == COMP_SUBMODULE ? "sub" : "");
6979 init_pi_tree ();
6980 init_true_name_tree ();
6982 read_module ();
6984 free_true_name (true_name_root);
6985 true_name_root = NULL;
6987 free_pi_tree (pi_root);
6988 pi_root = NULL;
6990 XDELETEVEC (module_content);
6991 module_content = NULL;
6993 use_stmt = gfc_get_use_list ();
6994 *use_stmt = *module;
6995 use_stmt->next = gfc_current_ns->use_stmts;
6996 gfc_current_ns->use_stmts = use_stmt;
6998 gfc_current_locus = old_locus;
7002 /* Remove duplicated intrinsic operators from the rename list. */
7004 static void
7005 rename_list_remove_duplicate (gfc_use_rename *list)
7007 gfc_use_rename *seek, *last;
7009 for (; list; list = list->next)
7010 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7012 last = list;
7013 for (seek = list->next; seek; seek = last->next)
7015 if (list->op == seek->op)
7017 last->next = seek->next;
7018 free (seek);
7020 else
7021 last = seek;
7027 /* Process all USE directives. */
7029 void
7030 gfc_use_modules (void)
7032 gfc_use_list *next, *seek, *last;
7034 for (next = module_list; next; next = next->next)
7036 bool non_intrinsic = next->non_intrinsic;
7037 bool intrinsic = next->intrinsic;
7038 bool neither = !non_intrinsic && !intrinsic;
7040 for (seek = next->next; seek; seek = seek->next)
7042 if (next->module_name != seek->module_name)
7043 continue;
7045 if (seek->non_intrinsic)
7046 non_intrinsic = true;
7047 else if (seek->intrinsic)
7048 intrinsic = true;
7049 else
7050 neither = true;
7053 if (intrinsic && neither && !non_intrinsic)
7055 char *filename;
7056 FILE *fp;
7058 filename = XALLOCAVEC (char,
7059 strlen (next->module_name)
7060 + strlen (MODULE_EXTENSION) + 1);
7061 strcpy (filename, next->module_name);
7062 strcat (filename, MODULE_EXTENSION);
7063 fp = gfc_open_included_file (filename, true, true);
7064 if (fp != NULL)
7066 non_intrinsic = true;
7067 fclose (fp);
7071 last = next;
7072 for (seek = next->next; seek; seek = last->next)
7074 if (next->module_name != seek->module_name)
7076 last = seek;
7077 continue;
7080 if ((!next->intrinsic && !seek->intrinsic)
7081 || (next->intrinsic && seek->intrinsic)
7082 || !non_intrinsic)
7084 if (!seek->only_flag)
7085 next->only_flag = false;
7086 if (seek->rename)
7088 gfc_use_rename *r = seek->rename;
7089 while (r->next)
7090 r = r->next;
7091 r->next = next->rename;
7092 next->rename = seek->rename;
7094 last->next = seek->next;
7095 free (seek);
7097 else
7098 last = seek;
7102 for (; module_list; module_list = next)
7104 next = module_list->next;
7105 rename_list_remove_duplicate (module_list->rename);
7106 gfc_use_module (module_list);
7107 free (module_list);
7109 gfc_rename_list = NULL;
7113 void
7114 gfc_free_use_stmts (gfc_use_list *use_stmts)
7116 gfc_use_list *next;
7117 for (; use_stmts; use_stmts = next)
7119 gfc_use_rename *next_rename;
7121 for (; use_stmts->rename; use_stmts->rename = next_rename)
7123 next_rename = use_stmts->rename->next;
7124 free (use_stmts->rename);
7126 next = use_stmts->next;
7127 free (use_stmts);
7132 void
7133 gfc_module_init_2 (void)
7135 last_atom = ATOM_LPAREN;
7136 gfc_rename_list = NULL;
7137 module_list = NULL;
7141 void
7142 gfc_module_done_2 (void)
7144 free_rename (gfc_rename_list);
7145 gfc_rename_list = NULL;