Daily bump.
[official-gcc.git] / gcc / fortran / module.cc
blob490eaa97a49db8bdd44425477cac60418cb2588e
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2025 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 "16"
88 /* Older mod versions we can still parse. */
89 #define COMPAT_MOD_VERSIONS { "15" }
92 /* Structure that describes a position within a module file. */
94 typedef struct
96 int column, line;
97 long pos;
99 module_locus;
101 /* Structure for list of symbols of intrinsic modules. */
102 typedef struct
104 int id;
105 const char *name;
106 int value;
107 int standard;
109 intmod_sym;
112 typedef enum
114 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
116 pointer_t;
118 /* The fixup structure lists pointers to pointers that have to
119 be updated when a pointer value becomes known. */
121 typedef struct fixup_t
123 void **pointer;
124 struct fixup_t *next;
126 fixup_t;
129 /* Structure for holding extra info needed for pointers being read. */
131 enum gfc_rsym_state
133 UNUSED,
134 NEEDED,
135 USED
138 enum gfc_wsym_state
140 UNREFERENCED = 0,
141 NEEDS_WRITE,
142 WRITTEN
145 typedef struct pointer_info
147 BBT_HEADER (pointer_info);
148 HOST_WIDE_INT integer;
149 pointer_t type;
151 /* The first component of each member of the union is the pointer
152 being stored. */
154 fixup_t *fixup;
156 union
158 void *pointer; /* Member for doing pointer searches. */
160 struct
162 gfc_symbol *sym;
163 char *true_name, *module, *binding_label;
164 fixup_t *stfixup;
165 gfc_symtree *symtree;
166 enum gfc_rsym_state state;
167 int ns, referenced, renamed;
168 module_locus where;
170 rsym;
172 struct
174 gfc_symbol *sym;
175 enum gfc_wsym_state state;
177 wsym;
182 pointer_info;
184 #define gfc_get_pointer_info() XCNEW (pointer_info)
187 /* Local variables */
189 /* The gzFile for the module we're reading or writing. */
190 static gzFile module_fp;
192 /* Fully qualified module path */
193 static char *module_fullpath = NULL;
195 /* The name of the module we're reading (USE'ing) or writing. */
196 static const char *module_name;
197 /* The name of the .smod file that the submodule will write to. */
198 static const char *submodule_name;
200 /* The list of use statements to apply to the current namespace
201 before parsing the non-use statements. */
202 static gfc_use_list *module_list;
203 /* The end of the MODULE_LIST list above at the time the recognition
204 of the current statement started. */
205 static gfc_use_list **old_module_list_tail;
207 /* If we're reading an intrinsic module, this is its ID. */
208 static intmod_id current_intmod;
210 /* Content of module. */
211 static char* module_content;
213 static long module_pos;
214 static int module_line, module_column, only_flag;
215 static int prev_module_line, prev_module_column;
217 static enum
218 { IO_INPUT, IO_OUTPUT }
219 iomode;
221 static gfc_use_rename *gfc_rename_list;
222 static pointer_info *pi_root;
223 static int symbol_number; /* Counter for assigning symbol numbers */
225 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
226 static bool in_load_equiv;
230 /*****************************************************************/
232 /* Pointer/integer conversion. Pointers between structures are stored
233 as integers in the module file. The next couple of subroutines
234 handle this translation for reading and writing. */
236 /* Recursively free the tree of pointer structures. */
238 static void
239 free_pi_tree (pointer_info *p)
241 if (p == NULL)
242 return;
244 if (p->fixup != NULL)
245 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
247 free_pi_tree (p->left);
248 free_pi_tree (p->right);
250 if (iomode == IO_INPUT)
252 XDELETEVEC (p->u.rsym.true_name);
253 XDELETEVEC (p->u.rsym.module);
254 XDELETEVEC (p->u.rsym.binding_label);
257 free (p);
261 /* Compare pointers when searching by pointer. Used when writing a
262 module. */
264 static int
265 compare_pointers (void *_sn1, void *_sn2)
267 pointer_info *sn1, *sn2;
269 sn1 = (pointer_info *) _sn1;
270 sn2 = (pointer_info *) _sn2;
272 if (sn1->u.pointer < sn2->u.pointer)
273 return -1;
274 if (sn1->u.pointer > sn2->u.pointer)
275 return 1;
277 return 0;
281 /* Compare integers when searching by integer. Used when reading a
282 module. */
284 static int
285 compare_integers (void *_sn1, void *_sn2)
287 pointer_info *sn1, *sn2;
289 sn1 = (pointer_info *) _sn1;
290 sn2 = (pointer_info *) _sn2;
292 if (sn1->integer < sn2->integer)
293 return -1;
294 if (sn1->integer > sn2->integer)
295 return 1;
297 return 0;
301 /* Initialize the pointer_info tree. */
303 static void
304 init_pi_tree (void)
306 compare_fn compare;
307 pointer_info *p;
309 pi_root = NULL;
310 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
312 /* Pointer 0 is the NULL pointer. */
313 p = gfc_get_pointer_info ();
314 p->u.pointer = NULL;
315 p->integer = 0;
316 p->type = P_OTHER;
318 gfc_insert_bbt (&pi_root, p, compare);
320 /* Pointer 1 is the current namespace. */
321 p = gfc_get_pointer_info ();
322 p->u.pointer = gfc_current_ns;
323 p->integer = 1;
324 p->type = P_NAMESPACE;
326 gfc_insert_bbt (&pi_root, p, compare);
328 symbol_number = 2;
332 /* During module writing, call here with a pointer to something,
333 returning the pointer_info node. */
335 static pointer_info *
336 find_pointer (void *gp)
338 pointer_info *p;
340 p = pi_root;
341 while (p != NULL)
343 if (p->u.pointer == gp)
344 break;
345 p = (gp < p->u.pointer) ? p->left : p->right;
348 return p;
352 /* Given a pointer while writing, returns the pointer_info tree node,
353 creating it if it doesn't exist. */
355 static pointer_info *
356 get_pointer (void *gp)
358 pointer_info *p;
360 p = find_pointer (gp);
361 if (p != NULL)
362 return p;
364 /* Pointer doesn't have an integer. Give it one. */
365 p = gfc_get_pointer_info ();
367 p->u.pointer = gp;
368 p->integer = symbol_number++;
370 gfc_insert_bbt (&pi_root, p, compare_pointers);
372 return p;
376 /* Given an integer during reading, find it in the pointer_info tree,
377 creating the node if not found. */
379 static pointer_info *
380 get_integer (HOST_WIDE_INT integer)
382 pointer_info *p, t;
383 int c;
385 t.integer = integer;
387 p = pi_root;
388 while (p != NULL)
390 c = compare_integers (&t, p);
391 if (c == 0)
392 break;
394 p = (c < 0) ? p->left : p->right;
397 if (p != NULL)
398 return p;
400 p = gfc_get_pointer_info ();
401 p->integer = integer;
402 p->u.pointer = NULL;
404 gfc_insert_bbt (&pi_root, p, compare_integers);
406 return p;
410 /* Resolve any fixups using a known pointer. */
412 static void
413 resolve_fixups (fixup_t *f, void *gp)
415 fixup_t *next;
417 for (; f; f = next)
419 next = f->next;
420 *(f->pointer) = gp;
421 free (f);
426 /* Convert a string such that it starts with a lower-case character. Used
427 to convert the symtree name of a derived-type to the symbol name or to
428 the name of the associated generic function. */
430 const char *
431 gfc_dt_lower_string (const char *name)
433 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
434 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
435 &name[1]);
436 return gfc_get_string ("%s", name);
440 /* Convert a string such that it starts with an upper-case character. Used to
441 return the symtree-name for a derived type; the symbol name itself and the
442 symtree/symbol name of the associated generic function start with a lower-
443 case character. */
445 const char *
446 gfc_dt_upper_string (const char *name)
448 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
449 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
450 &name[1]);
451 return gfc_get_string ("%s", name);
454 /* Call here during module reading when we know what pointer to
455 associate with an integer. Any fixups that exist are resolved at
456 this time. */
458 static void
459 associate_integer_pointer (pointer_info *p, void *gp)
461 if (p->u.pointer != NULL)
462 gfc_internal_error ("associate_integer_pointer(): Already associated");
464 p->u.pointer = gp;
466 resolve_fixups (p->fixup, gp);
468 p->fixup = NULL;
472 /* During module reading, given an integer and a pointer to a pointer,
473 either store the pointer from an already-known value or create a
474 fixup structure in order to store things later. Returns zero if
475 the reference has been actually stored, or nonzero if the reference
476 must be fixed later (i.e., associate_integer_pointer must be called
477 sometime later. Returns the pointer_info structure. */
479 static pointer_info *
480 add_fixup (HOST_WIDE_INT integer, void *gp)
482 pointer_info *p;
483 fixup_t *f;
484 char **cp;
486 p = get_integer (integer);
488 if (p->integer == 0 || p->u.pointer != NULL)
490 cp = (char **) gp;
491 *cp = (char *) p->u.pointer;
493 else
495 f = XCNEW (fixup_t);
497 f->next = p->fixup;
498 p->fixup = f;
500 f->pointer = (void **) gp;
503 return p;
507 /*****************************************************************/
509 /* Parser related subroutines */
511 /* Free the rename list left behind by a USE statement. */
513 static void
514 free_rename (gfc_use_rename *list)
516 gfc_use_rename *next;
518 for (; list; list = next)
520 next = list->next;
521 free (list);
526 /* Match a USE statement. */
528 match
529 gfc_match_use (void)
531 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
532 gfc_use_rename *tail = NULL, *new_use;
533 interface_type type, type2;
534 gfc_intrinsic_op op;
535 match m;
536 gfc_use_list *use_list;
537 gfc_symtree *st;
538 locus loc;
540 use_list = gfc_get_use_list ();
542 if (gfc_match (" , ") == MATCH_YES)
544 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
546 if (!gfc_notify_std (GFC_STD_F2003, "module "
547 "nature in USE statement at %C"))
548 goto cleanup;
550 if (strcmp (module_nature, "intrinsic") == 0)
551 use_list->intrinsic = true;
552 else
554 if (strcmp (module_nature, "non_intrinsic") == 0)
555 use_list->non_intrinsic = true;
556 else
558 gfc_error ("Module nature in USE statement at %C shall "
559 "be either INTRINSIC or NON_INTRINSIC");
560 goto cleanup;
564 else
566 /* Help output a better error message than "Unclassifiable
567 statement". */
568 gfc_match (" %n", module_nature);
569 if (strcmp (module_nature, "intrinsic") == 0
570 || strcmp (module_nature, "non_intrinsic") == 0)
571 gfc_error ("\"::\" was expected after module nature at %C "
572 "but was not found");
573 free (use_list);
574 return m;
577 else
579 m = gfc_match (" ::");
580 if (m == MATCH_YES &&
581 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
582 goto cleanup;
584 if (m != MATCH_YES)
586 m = gfc_match ("% ");
587 if (m != MATCH_YES)
589 free (use_list);
590 return m;
595 use_list->where = gfc_current_locus;
597 m = gfc_match_name (name);
598 if (m != MATCH_YES)
600 free (use_list);
601 return m;
604 use_list->module_name = gfc_get_string ("%s", name);
606 if (gfc_match_eos () == MATCH_YES)
607 goto done;
609 if (gfc_match_char (',') != MATCH_YES)
610 goto syntax;
612 if (gfc_match (" only :") == MATCH_YES)
613 use_list->only_flag = true;
615 if (gfc_match_eos () == MATCH_YES)
616 goto done;
618 for (;;)
620 /* Get a new rename struct and add it to the rename list. */
621 new_use = gfc_get_use_rename ();
622 new_use->where = gfc_current_locus;
623 new_use->found = 0;
625 if (use_list->rename == NULL)
626 use_list->rename = new_use;
627 else
628 tail->next = new_use;
629 tail = new_use;
631 /* See what kind of interface we're dealing with. Assume it is
632 not an operator. */
633 new_use->op = INTRINSIC_NONE;
634 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
635 goto cleanup;
637 switch (type)
639 case INTERFACE_NAMELESS:
640 gfc_error ("Missing generic specification in USE statement at %C");
641 goto cleanup;
643 case INTERFACE_USER_OP:
644 case INTERFACE_GENERIC:
645 case INTERFACE_DTIO:
646 loc = gfc_current_locus;
648 m = gfc_match (" =>");
650 if (type == INTERFACE_USER_OP && m == MATCH_YES
651 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
652 "operators in USE statements at %C")))
653 goto cleanup;
655 if (type == INTERFACE_USER_OP)
656 new_use->op = INTRINSIC_USER;
658 if (use_list->only_flag)
660 if (m != MATCH_YES)
661 strcpy (new_use->use_name, name);
662 else
664 strcpy (new_use->local_name, name);
665 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
666 if (type != type2)
667 goto syntax;
668 if (m == MATCH_NO)
669 goto syntax;
670 if (m == MATCH_ERROR)
671 goto cleanup;
674 else
676 if (m != MATCH_YES)
677 goto syntax;
678 strcpy (new_use->local_name, name);
680 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
681 if (type != type2)
682 goto syntax;
683 if (m == MATCH_NO)
684 goto syntax;
685 if (m == MATCH_ERROR)
686 goto cleanup;
689 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
690 if (st && type != INTERFACE_USER_OP
691 && (st->n.sym->module != use_list->module_name
692 || strcmp (st->n.sym->name, new_use->use_name) != 0))
694 if (m == MATCH_YES)
695 gfc_error ("Symbol %qs at %L conflicts with the rename symbol "
696 "at %L", name, &st->n.sym->declared_at, &loc);
697 else
698 gfc_error ("Symbol %qs at %L conflicts with the symbol "
699 "at %L", name, &st->n.sym->declared_at, &loc);
700 goto cleanup;
703 if (strcmp (new_use->use_name, use_list->module_name) == 0
704 || strcmp (new_use->local_name, use_list->module_name) == 0)
706 gfc_error ("The name %qs at %C has already been used as "
707 "an external module name", use_list->module_name);
708 goto cleanup;
710 break;
712 case INTERFACE_INTRINSIC_OP:
713 new_use->op = op;
714 break;
716 default:
717 gcc_unreachable ();
720 if (gfc_match_eos () == MATCH_YES)
721 break;
722 if (gfc_match_char (',') != MATCH_YES)
723 goto syntax;
726 done:
727 if (module_list)
729 gfc_use_list *last = module_list;
730 while (last->next)
731 last = last->next;
732 last->next = use_list;
734 else
735 module_list = use_list;
737 return MATCH_YES;
739 syntax:
740 gfc_syntax_error (ST_USE);
742 cleanup:
743 free_rename (use_list->rename);
744 free (use_list);
745 return MATCH_ERROR;
749 /* Match a SUBMODULE statement.
751 According to F2008:11.2.3.2, "The submodule identifier is the
752 ordered pair whose first element is the ancestor module name and
753 whose second element is the submodule name. 'Submodule_name' is
754 used for the submodule filename and uses '@' as a separator, whilst
755 the name of the symbol for the module uses '.' as a separator.
756 The reasons for these choices are:
757 (i) To follow another leading brand in the submodule filenames;
758 (ii) Since '.' is not particularly visible in the filenames; and
759 (iii) The linker does not permit '@' in mnemonics. */
761 match
762 gfc_match_submodule (void)
764 match m;
765 char name[GFC_MAX_SYMBOL_LEN + 1];
766 gfc_use_list *use_list;
767 bool seen_colon = false;
769 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
770 return MATCH_ERROR;
772 if (gfc_current_state () != COMP_NONE)
774 gfc_error ("SUBMODULE declaration at %C cannot appear within "
775 "another scoping unit");
776 return MATCH_ERROR;
779 gfc_new_block = NULL;
780 gcc_assert (module_list == NULL);
782 if (gfc_match_char ('(') != MATCH_YES)
783 goto syntax;
785 while (1)
787 m = gfc_match (" %n", name);
788 if (m != MATCH_YES)
789 goto syntax;
791 use_list = gfc_get_use_list ();
792 use_list->where = gfc_current_locus;
794 if (module_list)
796 gfc_use_list *last = module_list;
797 while (last->next)
798 last = last->next;
799 last->next = use_list;
800 use_list->module_name
801 = gfc_get_string ("%s.%s", module_list->module_name, name);
802 use_list->submodule_name
803 = gfc_get_string ("%s@%s", module_list->module_name, name);
805 else
807 module_list = use_list;
808 use_list->module_name = gfc_get_string ("%s", name);
809 use_list->submodule_name = use_list->module_name;
812 if (gfc_match_char (')') == MATCH_YES)
813 break;
815 if (gfc_match_char (':') != MATCH_YES
816 || seen_colon)
817 goto syntax;
819 seen_colon = true;
822 m = gfc_match (" %s%t", &gfc_new_block);
823 if (m != MATCH_YES)
824 goto syntax;
826 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
827 gfc_new_block->name);
829 gfc_new_block->name = gfc_get_string ("%s.%s",
830 module_list->module_name,
831 gfc_new_block->name);
833 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
834 gfc_new_block->name, NULL))
835 return MATCH_ERROR;
837 /* Just retain the ultimate .(s)mod file for reading, since it
838 contains all the information in its ancestors. */
839 use_list = module_list;
840 for (; module_list->next; use_list = module_list)
842 module_list = use_list->next;
843 free (use_list);
846 return MATCH_YES;
848 syntax:
849 gfc_error ("Syntax error in SUBMODULE statement at %C");
850 return MATCH_ERROR;
854 /* Given a name and a number, inst, return the inst name
855 under which to load this symbol. Returns NULL if this
856 symbol shouldn't be loaded. If inst is zero, returns
857 the number of instances of this name. If interface is
858 true, a user-defined operator is sought, otherwise only
859 non-operators are sought. */
861 static const char *
862 find_use_name_n (const char *name, int *inst, bool interface)
864 gfc_use_rename *u;
865 const char *low_name = NULL;
866 int i;
868 /* For derived types. */
869 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
870 low_name = gfc_dt_lower_string (name);
872 i = 0;
873 for (u = gfc_rename_list; u; u = u->next)
875 if ((!low_name && strcmp (u->use_name, name) != 0)
876 || (low_name && strcmp (u->use_name, low_name) != 0)
877 || (u->op == INTRINSIC_USER && !interface)
878 || (u->op != INTRINSIC_USER && interface))
879 continue;
880 if (++i == *inst)
881 break;
884 if (!*inst)
886 *inst = i;
887 return NULL;
890 if (u == NULL)
891 return only_flag ? NULL : name;
893 u->found = 1;
895 if (low_name)
897 if (u->local_name[0] == '\0')
898 return name;
899 return gfc_dt_upper_string (u->local_name);
902 return (u->local_name[0] != '\0') ? u->local_name : name;
906 /* Given a name, return the name under which to load this symbol.
907 Returns NULL if this symbol shouldn't be loaded. */
909 static const char *
910 find_use_name (const char *name, bool interface)
912 int i = 1;
913 return find_use_name_n (name, &i, interface);
917 /* Given a real name, return the number of use names associated with it. */
919 static int
920 number_use_names (const char *name, bool interface)
922 int i = 0;
923 find_use_name_n (name, &i, interface);
924 return i;
928 /* Try to find the operator in the current list. */
930 static gfc_use_rename *
931 find_use_operator (gfc_intrinsic_op op)
933 gfc_use_rename *u;
935 for (u = gfc_rename_list; u; u = u->next)
936 if (u->op == op)
937 return u;
939 return NULL;
943 /*****************************************************************/
945 /* The next couple of subroutines maintain a tree used to avoid a
946 brute-force search for a combination of true name and module name.
947 While symtree names, the name that a particular symbol is known by
948 can changed with USE statements, we still have to keep track of the
949 true names to generate the correct reference, and also avoid
950 loading the same real symbol twice in a program unit.
952 When we start reading, the true name tree is built and maintained
953 as symbols are read. The tree is searched as we load new symbols
954 to see if it already exists someplace in the namespace. */
956 typedef struct true_name
958 BBT_HEADER (true_name);
959 const char *name;
960 gfc_symbol *sym;
962 true_name;
964 static true_name *true_name_root;
967 /* Compare two true_name structures. */
969 static int
970 compare_true_names (void *_t1, void *_t2)
972 true_name *t1, *t2;
973 int c;
975 t1 = (true_name *) _t1;
976 t2 = (true_name *) _t2;
978 c = ((t1->sym->module > t2->sym->module)
979 - (t1->sym->module < t2->sym->module));
980 if (c != 0)
981 return c;
983 return strcmp (t1->name, t2->name);
987 /* Given a true name, search the true name tree to see if it exists
988 within the main namespace. */
990 static gfc_symbol *
991 find_true_name (const char *name, const char *module)
993 true_name t, *p;
994 gfc_symbol sym;
995 int c;
997 t.name = gfc_get_string ("%s", name);
998 if (module != NULL)
999 sym.module = gfc_get_string ("%s", module);
1000 else
1001 sym.module = NULL;
1002 t.sym = &sym;
1004 p = true_name_root;
1005 while (p != NULL)
1007 c = compare_true_names ((void *) (&t), (void *) p);
1008 if (c == 0)
1009 return p->sym;
1011 p = (c < 0) ? p->left : p->right;
1014 return NULL;
1018 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
1020 static void
1021 add_true_name (gfc_symbol *sym)
1023 true_name *t;
1025 t = XCNEW (true_name);
1026 t->sym = sym;
1027 if (gfc_fl_struct (sym->attr.flavor))
1028 t->name = gfc_dt_upper_string (sym->name);
1029 else
1030 t->name = sym->name;
1032 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1036 /* Recursive function to build the initial true name tree by
1037 recursively traversing the current namespace. */
1039 static void
1040 build_tnt (gfc_symtree *st)
1042 const char *name;
1043 if (st == NULL)
1044 return;
1046 build_tnt (st->left);
1047 build_tnt (st->right);
1049 if (gfc_fl_struct (st->n.sym->attr.flavor))
1050 name = gfc_dt_upper_string (st->n.sym->name);
1051 else
1052 name = st->n.sym->name;
1054 if (find_true_name (name, st->n.sym->module) != NULL)
1055 return;
1057 add_true_name (st->n.sym);
1061 /* Initialize the true name tree with the current namespace. */
1063 static void
1064 init_true_name_tree (void)
1066 true_name_root = NULL;
1067 build_tnt (gfc_current_ns->sym_root);
1071 /* Recursively free a true name tree node. */
1073 static void
1074 free_true_name (true_name *t)
1076 if (t == NULL)
1077 return;
1078 free_true_name (t->left);
1079 free_true_name (t->right);
1081 free (t);
1085 /*****************************************************************/
1087 /* Module reading and writing. */
1089 /* The following are versions similar to the ones in scanner.cc, but
1090 for dealing with compressed module files. */
1092 static gzFile
1093 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1094 bool module, bool system)
1096 char *fullname;
1097 gfc_directorylist *p;
1098 gzFile f;
1100 for (p = list; p; p = p->next)
1102 if (module && !p->use_for_modules)
1103 continue;
1105 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
1106 strcpy (fullname, p->path);
1107 strcat (fullname, "/");
1108 strcat (fullname, name);
1110 f = gzopen (fullname, "r");
1111 if (f != NULL)
1113 if (gfc_cpp_makedep ())
1114 gfc_cpp_add_dep (fullname, system);
1116 free (module_fullpath);
1117 module_fullpath = xstrdup (fullname);
1118 return f;
1122 return NULL;
1125 static gzFile
1126 gzopen_included_file (const char *name, bool include_cwd, bool module)
1128 gzFile f = NULL;
1130 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1132 f = gzopen (name, "r");
1133 if (f)
1135 if (gfc_cpp_makedep ())
1136 gfc_cpp_add_dep (name, false);
1138 free (module_fullpath);
1139 module_fullpath = xstrdup (name);
1143 if (!f)
1144 f = gzopen_included_file_1 (name, include_dirs, module, false);
1146 return f;
1149 static gzFile
1150 gzopen_intrinsic_module (const char* name)
1152 gzFile f = NULL;
1154 if (IS_ABSOLUTE_PATH (name))
1156 f = gzopen (name, "r");
1157 if (f)
1159 if (gfc_cpp_makedep ())
1160 gfc_cpp_add_dep (name, true);
1162 free (module_fullpath);
1163 module_fullpath = xstrdup (name);
1167 if (!f)
1168 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1170 return f;
1174 enum atom_type
1176 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1179 static atom_type last_atom;
1182 /* The name buffer must be at least as long as a symbol name. Right
1183 now it's not clear how we're going to store numeric constants--
1184 probably as a hexadecimal string, since this will allow the exact
1185 number to be preserved (this can't be done by a decimal
1186 representation). Worry about that later. TODO! */
1188 #define MAX_ATOM_SIZE 100
1190 static HOST_WIDE_INT atom_int;
1191 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1194 /* Report problems with a module. Error reporting is not very
1195 elaborate, since this sorts of errors shouldn't really happen.
1196 This subroutine never returns. */
1198 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1200 static void
1201 bad_module (const char *msgid)
1203 XDELETEVEC (module_content);
1204 module_content = NULL;
1206 switch (iomode)
1208 case IO_INPUT:
1209 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1210 module_fullpath, module_line, module_column, msgid);
1211 break;
1212 case IO_OUTPUT:
1213 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1214 module_name, module_line, module_column, msgid);
1215 break;
1216 default:
1217 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1218 module_name, module_line, module_column, msgid);
1219 break;
1224 /* Set the module's input pointer. */
1226 static void
1227 set_module_locus (module_locus *m)
1229 module_column = m->column;
1230 module_line = m->line;
1231 module_pos = m->pos;
1235 /* Get the module's input pointer so that we can restore it later. */
1237 static void
1238 get_module_locus (module_locus *m)
1240 m->column = module_column;
1241 m->line = module_line;
1242 m->pos = module_pos;
1245 /* Peek at the next character in the module. */
1247 static int
1248 module_peek_char (void)
1250 return module_content[module_pos];
1253 /* Get the next character in the module, updating our reckoning of
1254 where we are. */
1256 static int
1257 module_char (void)
1259 const char c = module_content[module_pos++];
1260 if (c == '\0')
1261 bad_module ("Unexpected EOF");
1263 prev_module_line = module_line;
1264 prev_module_column = module_column;
1266 if (c == '\n')
1268 module_line++;
1269 module_column = 0;
1272 module_column++;
1273 return c;
1276 /* Unget a character while remembering the line and column. Works for
1277 a single character only. */
1279 static void
1280 module_unget_char (void)
1282 module_line = prev_module_line;
1283 module_column = prev_module_column;
1284 module_pos--;
1287 /* Parse a string constant. The delimiter is guaranteed to be a
1288 single quote. */
1290 static void
1291 parse_string (void)
1293 int c;
1294 size_t cursz = 30;
1295 size_t len = 0;
1297 atom_string = XNEWVEC (char, cursz);
1299 for ( ; ; )
1301 c = module_char ();
1303 if (c == '\'')
1305 int c2 = module_char ();
1306 if (c2 != '\'')
1308 module_unget_char ();
1309 break;
1313 if (len >= cursz)
1315 cursz *= 2;
1316 atom_string = XRESIZEVEC (char, atom_string, cursz);
1318 atom_string[len] = c;
1319 len++;
1322 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1323 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1327 /* Parse an integer. Should fit in a HOST_WIDE_INT. */
1329 static void
1330 parse_integer (int c)
1332 int sign = 1;
1334 atom_int = 0;
1335 switch (c)
1337 case ('-'):
1338 sign = -1;
1339 case ('+'):
1340 break;
1341 default:
1342 atom_int = c - '0';
1343 break;
1346 for (;;)
1348 c = module_char ();
1349 if (!ISDIGIT (c))
1351 module_unget_char ();
1352 break;
1355 atom_int = 10 * atom_int + c - '0';
1358 atom_int *= sign;
1362 /* Parse a name. */
1364 static void
1365 parse_name (int c)
1367 char *p;
1368 int len;
1370 p = atom_name;
1372 *p++ = c;
1373 len = 1;
1375 for (;;)
1377 c = module_char ();
1378 if (!ISALNUM (c) && c != '_' && c != '-')
1380 module_unget_char ();
1381 break;
1384 *p++ = c;
1385 if (++len > GFC_MAX_SYMBOL_LEN)
1386 bad_module ("Name too long");
1389 *p = '\0';
1394 /* Read the next atom in the module's input stream. */
1396 static atom_type
1397 parse_atom (void)
1399 int c;
1403 c = module_char ();
1405 while (c == ' ' || c == '\r' || c == '\n');
1407 switch (c)
1409 case '(':
1410 return ATOM_LPAREN;
1412 case ')':
1413 return ATOM_RPAREN;
1415 case '\'':
1416 parse_string ();
1417 return ATOM_STRING;
1419 case '0':
1420 case '1':
1421 case '2':
1422 case '3':
1423 case '4':
1424 case '5':
1425 case '6':
1426 case '7':
1427 case '8':
1428 case '9':
1429 parse_integer (c);
1430 return ATOM_INTEGER;
1432 case '+':
1433 case '-':
1434 if (ISDIGIT (module_peek_char ()))
1436 parse_integer (c);
1437 return ATOM_INTEGER;
1439 else
1440 bad_module ("Bad name");
1442 case 'a':
1443 case 'b':
1444 case 'c':
1445 case 'd':
1446 case 'e':
1447 case 'f':
1448 case 'g':
1449 case 'h':
1450 case 'i':
1451 case 'j':
1452 case 'k':
1453 case 'l':
1454 case 'm':
1455 case 'n':
1456 case 'o':
1457 case 'p':
1458 case 'q':
1459 case 'r':
1460 case 's':
1461 case 't':
1462 case 'u':
1463 case 'v':
1464 case 'w':
1465 case 'x':
1466 case 'y':
1467 case 'z':
1468 case 'A':
1469 case 'B':
1470 case 'C':
1471 case 'D':
1472 case 'E':
1473 case 'F':
1474 case 'G':
1475 case 'H':
1476 case 'I':
1477 case 'J':
1478 case 'K':
1479 case 'L':
1480 case 'M':
1481 case 'N':
1482 case 'O':
1483 case 'P':
1484 case 'Q':
1485 case 'R':
1486 case 'S':
1487 case 'T':
1488 case 'U':
1489 case 'V':
1490 case 'W':
1491 case 'X':
1492 case 'Y':
1493 case 'Z':
1494 parse_name (c);
1495 return ATOM_NAME;
1497 default:
1498 bad_module ("Bad name");
1501 /* Not reached. */
1505 /* Peek at the next atom on the input. */
1507 static atom_type
1508 peek_atom (void)
1510 int c;
1514 c = module_char ();
1516 while (c == ' ' || c == '\r' || c == '\n');
1518 switch (c)
1520 case '(':
1521 module_unget_char ();
1522 return ATOM_LPAREN;
1524 case ')':
1525 module_unget_char ();
1526 return ATOM_RPAREN;
1528 case '\'':
1529 module_unget_char ();
1530 return ATOM_STRING;
1532 case '0':
1533 case '1':
1534 case '2':
1535 case '3':
1536 case '4':
1537 case '5':
1538 case '6':
1539 case '7':
1540 case '8':
1541 case '9':
1542 module_unget_char ();
1543 return ATOM_INTEGER;
1545 case '+':
1546 case '-':
1547 if (ISDIGIT (module_peek_char ()))
1549 module_unget_char ();
1550 return ATOM_INTEGER;
1552 else
1553 bad_module ("Bad name");
1555 case 'a':
1556 case 'b':
1557 case 'c':
1558 case 'd':
1559 case 'e':
1560 case 'f':
1561 case 'g':
1562 case 'h':
1563 case 'i':
1564 case 'j':
1565 case 'k':
1566 case 'l':
1567 case 'm':
1568 case 'n':
1569 case 'o':
1570 case 'p':
1571 case 'q':
1572 case 'r':
1573 case 's':
1574 case 't':
1575 case 'u':
1576 case 'v':
1577 case 'w':
1578 case 'x':
1579 case 'y':
1580 case 'z':
1581 case 'A':
1582 case 'B':
1583 case 'C':
1584 case 'D':
1585 case 'E':
1586 case 'F':
1587 case 'G':
1588 case 'H':
1589 case 'I':
1590 case 'J':
1591 case 'K':
1592 case 'L':
1593 case 'M':
1594 case 'N':
1595 case 'O':
1596 case 'P':
1597 case 'Q':
1598 case 'R':
1599 case 'S':
1600 case 'T':
1601 case 'U':
1602 case 'V':
1603 case 'W':
1604 case 'X':
1605 case 'Y':
1606 case 'Z':
1607 module_unget_char ();
1608 return ATOM_NAME;
1610 default:
1611 bad_module ("Bad name");
1616 /* Read the next atom from the input, requiring that it be a
1617 particular kind. */
1619 static void
1620 require_atom (atom_type type)
1622 atom_type t;
1623 const char *p;
1624 int column, line;
1626 column = module_column;
1627 line = module_line;
1629 t = parse_atom ();
1630 if (t != type)
1632 switch (type)
1634 case ATOM_NAME:
1635 p = _("Expected name");
1636 break;
1637 case ATOM_LPAREN:
1638 p = _("Expected left parenthesis");
1639 break;
1640 case ATOM_RPAREN:
1641 p = _("Expected right parenthesis");
1642 break;
1643 case ATOM_INTEGER:
1644 p = _("Expected integer");
1645 break;
1646 case ATOM_STRING:
1647 p = _("Expected string");
1648 break;
1649 default:
1650 gfc_internal_error ("require_atom(): bad atom type required");
1653 module_column = column;
1654 module_line = line;
1655 bad_module (p);
1660 /* Given a pointer to an mstring array, require that the current input
1661 be one of the strings in the array. We return the enum value. */
1663 static int
1664 find_enum (const mstring *m)
1666 int i;
1668 i = gfc_string2code (m, atom_name);
1669 if (i >= 0)
1670 return i;
1672 bad_module ("find_enum(): Enum not found");
1674 /* Not reached. */
1678 /* Read a string. The caller is responsible for freeing. */
1680 static char*
1681 read_string (void)
1683 char* p;
1684 require_atom (ATOM_STRING);
1685 p = atom_string;
1686 atom_string = NULL;
1687 return p;
1691 /**************** Module output subroutines ***************************/
1693 /* Output a character to a module file. */
1695 static void
1696 write_char (char out)
1698 if (gzputc (module_fp, out) == EOF)
1699 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1701 if (out != '\n')
1702 module_column++;
1703 else
1705 module_column = 1;
1706 module_line++;
1711 /* Write an atom to a module. The line wrapping isn't perfect, but it
1712 should work most of the time. This isn't that big of a deal, since
1713 the file really isn't meant to be read by people anyway. */
1715 static void
1716 write_atom (atom_type atom, const void *v)
1718 char buffer[32];
1720 /* Workaround -Wmaybe-uninitialized false positive during
1721 profiledbootstrap by initializing them. */
1722 int len;
1723 HOST_WIDE_INT i = 0;
1724 const char *p;
1726 switch (atom)
1728 case ATOM_STRING:
1729 case ATOM_NAME:
1730 p = (const char *) v;
1731 break;
1733 case ATOM_LPAREN:
1734 p = "(";
1735 break;
1737 case ATOM_RPAREN:
1738 p = ")";
1739 break;
1741 case ATOM_INTEGER:
1742 i = *((const HOST_WIDE_INT *) v);
1744 snprintf (buffer, sizeof (buffer), HOST_WIDE_INT_PRINT_DEC, i);
1745 p = buffer;
1746 break;
1748 default:
1749 gfc_internal_error ("write_atom(): Trying to write dab atom");
1753 if(p == NULL || *p == '\0')
1754 len = 0;
1755 else
1756 len = strlen (p);
1758 if (atom != ATOM_RPAREN)
1760 if (module_column + len > 72)
1761 write_char ('\n');
1762 else
1765 if (last_atom != ATOM_LPAREN && module_column != 1)
1766 write_char (' ');
1770 if (atom == ATOM_STRING)
1771 write_char ('\'');
1773 while (p != NULL && *p)
1775 if (atom == ATOM_STRING && *p == '\'')
1776 write_char ('\'');
1777 write_char (*p++);
1780 if (atom == ATOM_STRING)
1781 write_char ('\'');
1783 last_atom = atom;
1788 /***************** Mid-level I/O subroutines *****************/
1790 /* These subroutines let their caller read or write atoms without
1791 caring about which of the two is actually happening. This lets a
1792 subroutine concentrate on the actual format of the data being
1793 written. */
1795 static void mio_expr (gfc_expr **);
1796 pointer_info *mio_symbol_ref (gfc_symbol **);
1797 pointer_info *mio_interface_rest (gfc_interface **);
1798 static void mio_symtree_ref (gfc_symtree **);
1800 /* Read or write an enumerated value. On writing, we return the input
1801 value for the convenience of callers. We avoid using an integer
1802 pointer because enums are sometimes inside bitfields. */
1804 static int
1805 mio_name (int t, const mstring *m)
1807 if (iomode == IO_OUTPUT)
1808 write_atom (ATOM_NAME, gfc_code2string (m, t));
1809 else
1811 require_atom (ATOM_NAME);
1812 t = find_enum (m);
1815 return t;
1818 /* Specialization of mio_name. */
1820 #define DECL_MIO_NAME(TYPE) \
1821 static inline TYPE \
1822 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1824 return (TYPE) mio_name ((int) t, m); \
1826 #define MIO_NAME(TYPE) mio_name_##TYPE
1828 static void
1829 mio_lparen (void)
1831 if (iomode == IO_OUTPUT)
1832 write_atom (ATOM_LPAREN, NULL);
1833 else
1834 require_atom (ATOM_LPAREN);
1838 static void
1839 mio_rparen (void)
1841 if (iomode == IO_OUTPUT)
1842 write_atom (ATOM_RPAREN, NULL);
1843 else
1844 require_atom (ATOM_RPAREN);
1848 static void
1849 mio_integer (int *ip)
1851 if (iomode == IO_OUTPUT)
1853 HOST_WIDE_INT hwi = *ip;
1854 write_atom (ATOM_INTEGER, &hwi);
1856 else
1858 require_atom (ATOM_INTEGER);
1859 *ip = atom_int;
1863 static void
1864 mio_hwi (HOST_WIDE_INT *hwi)
1866 if (iomode == IO_OUTPUT)
1867 write_atom (ATOM_INTEGER, hwi);
1868 else
1870 require_atom (ATOM_INTEGER);
1871 *hwi = atom_int;
1876 /* Read or write a gfc_intrinsic_op value. */
1878 static void
1879 mio_intrinsic_op (gfc_intrinsic_op* op)
1881 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1882 if (iomode == IO_OUTPUT)
1884 HOST_WIDE_INT converted = (HOST_WIDE_INT) *op;
1885 write_atom (ATOM_INTEGER, &converted);
1887 else
1889 require_atom (ATOM_INTEGER);
1890 *op = (gfc_intrinsic_op) atom_int;
1895 /* Read or write a character pointer that points to a string on the heap. */
1897 static const char *
1898 mio_allocated_string (const char *s)
1900 if (iomode == IO_OUTPUT)
1902 write_atom (ATOM_STRING, s);
1903 return s;
1905 else
1907 require_atom (ATOM_STRING);
1908 return atom_string;
1913 /* Functions for quoting and unquoting strings. */
1915 static char *
1916 quote_string (const gfc_char_t *s, const size_t slength)
1918 const gfc_char_t *p;
1919 char *res, *q;
1920 size_t len = 0, i;
1922 /* Calculate the length we'll need: a backslash takes two ("\\"),
1923 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1924 for (p = s, i = 0; i < slength; p++, i++)
1926 if (*p == '\\')
1927 len += 2;
1928 else if (!gfc_wide_is_printable (*p))
1929 len += 10;
1930 else
1931 len++;
1934 q = res = XCNEWVEC (char, len + 1);
1935 for (p = s, i = 0; i < slength; p++, i++)
1937 if (*p == '\\')
1938 *q++ = '\\', *q++ = '\\';
1939 else if (!gfc_wide_is_printable (*p))
1941 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1942 (unsigned HOST_WIDE_INT) *p);
1943 q += 10;
1945 else
1946 *q++ = (unsigned char) *p;
1949 res[len] = '\0';
1950 return res;
1953 static gfc_char_t *
1954 unquote_string (const char *s)
1956 size_t len, i;
1957 const char *p;
1958 gfc_char_t *res;
1960 for (p = s, len = 0; *p; p++, len++)
1962 if (*p != '\\')
1963 continue;
1965 if (p[1] == '\\')
1966 p++;
1967 else if (p[1] == 'U')
1968 p += 9; /* That is a "\U????????". */
1969 else
1970 gfc_internal_error ("unquote_string(): got bad string");
1973 res = gfc_get_wide_string (len + 1);
1974 for (i = 0, p = s; i < len; i++, p++)
1976 gcc_assert (*p);
1978 if (*p != '\\')
1979 res[i] = (unsigned char) *p;
1980 else if (p[1] == '\\')
1982 res[i] = (unsigned char) '\\';
1983 p++;
1985 else
1987 /* We read the 8-digits hexadecimal constant that follows. */
1988 int j;
1989 unsigned n;
1990 gfc_char_t c = 0;
1992 gcc_assert (p[1] == 'U');
1993 for (j = 0; j < 8; j++)
1995 c = c << 4;
1996 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1997 c += n;
2000 res[i] = c;
2001 p += 9;
2005 res[len] = '\0';
2006 return res;
2010 /* Read or write a character pointer that points to a wide string on the
2011 heap, performing quoting/unquoting of nonprintable characters using the
2012 form \U???????? (where each ? is a hexadecimal digit).
2013 Length is the length of the string, only known and used in output mode. */
2015 static const gfc_char_t *
2016 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
2018 if (iomode == IO_OUTPUT)
2020 char *quoted = quote_string (s, length);
2021 write_atom (ATOM_STRING, quoted);
2022 free (quoted);
2023 return s;
2025 else
2027 gfc_char_t *unquoted;
2029 require_atom (ATOM_STRING);
2030 unquoted = unquote_string (atom_string);
2031 free (atom_string);
2032 return unquoted;
2037 /* Read or write a string that is in static memory. */
2039 static void
2040 mio_pool_string (const char **stringp)
2042 /* TODO: one could write the string only once, and refer to it via a
2043 fixup pointer. */
2045 /* As a special case we have to deal with a NULL string. This
2046 happens for the 'module' member of 'gfc_symbol's that are not in a
2047 module. We read / write these as the empty string. */
2048 if (iomode == IO_OUTPUT)
2050 const char *p = *stringp == NULL ? "" : *stringp;
2051 write_atom (ATOM_STRING, p);
2053 else
2055 require_atom (ATOM_STRING);
2056 *stringp = (atom_string[0] == '\0'
2057 ? NULL : gfc_get_string ("%s", atom_string));
2058 free (atom_string);
2063 /* Read or write a string that is inside of some already-allocated
2064 structure. */
2066 static void
2067 mio_internal_string (char *string)
2069 if (iomode == IO_OUTPUT)
2070 write_atom (ATOM_STRING, string);
2071 else
2073 require_atom (ATOM_STRING);
2074 strcpy (string, atom_string);
2075 free (atom_string);
2080 enum ab_attribute
2081 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
2082 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
2083 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
2084 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
2085 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
2086 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
2087 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
2088 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
2089 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
2090 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
2091 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
2092 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
2093 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
2094 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK,
2095 AB_OMP_DECLARE_TARGET_LINK, AB_PDT_KIND, AB_PDT_LEN, AB_PDT_TYPE,
2096 AB_PDT_TEMPLATE, AB_PDT_ARRAY, AB_PDT_STRING,
2097 AB_OACC_ROUTINE_LOP_GANG, AB_OACC_ROUTINE_LOP_WORKER,
2098 AB_OACC_ROUTINE_LOP_VECTOR, AB_OACC_ROUTINE_LOP_SEQ,
2099 AB_OACC_ROUTINE_NOHOST,
2100 AB_OMP_REQ_REVERSE_OFFLOAD, AB_OMP_REQ_UNIFIED_ADDRESS, AB_OMP_REQ_SELF_MAPS,
2101 AB_OMP_REQ_UNIFIED_SHARED_MEMORY, AB_OMP_REQ_DYNAMIC_ALLOCATORS,
2102 AB_OMP_REQ_MEM_ORDER_SEQ_CST, AB_OMP_REQ_MEM_ORDER_ACQ_REL,
2103 AB_OMP_REQ_MEM_ORDER_ACQUIRE, AB_OMP_REQ_MEM_ORDER_RELEASE,
2104 AB_OMP_REQ_MEM_ORDER_RELAXED, AB_OMP_DEVICE_TYPE_NOHOST,
2105 AB_OMP_DEVICE_TYPE_HOST, AB_OMP_DEVICE_TYPE_ANY
2108 static const mstring attr_bits[] =
2110 minit ("ALLOCATABLE", AB_ALLOCATABLE),
2111 minit ("ARTIFICIAL", AB_ARTIFICIAL),
2112 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
2113 minit ("DIMENSION", AB_DIMENSION),
2114 minit ("CODIMENSION", AB_CODIMENSION),
2115 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2116 minit ("EXTERNAL", AB_EXTERNAL),
2117 minit ("INTRINSIC", AB_INTRINSIC),
2118 minit ("OPTIONAL", AB_OPTIONAL),
2119 minit ("POINTER", AB_POINTER),
2120 minit ("VOLATILE", AB_VOLATILE),
2121 minit ("TARGET", AB_TARGET),
2122 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2123 minit ("DUMMY", AB_DUMMY),
2124 minit ("RESULT", AB_RESULT),
2125 minit ("DATA", AB_DATA),
2126 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2127 minit ("IN_COMMON", AB_IN_COMMON),
2128 minit ("FUNCTION", AB_FUNCTION),
2129 minit ("SUBROUTINE", AB_SUBROUTINE),
2130 minit ("SEQUENCE", AB_SEQUENCE),
2131 minit ("ELEMENTAL", AB_ELEMENTAL),
2132 minit ("PURE", AB_PURE),
2133 minit ("RECURSIVE", AB_RECURSIVE),
2134 minit ("GENERIC", AB_GENERIC),
2135 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2136 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2137 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2138 minit ("IS_BIND_C", AB_IS_BIND_C),
2139 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2140 minit ("IS_ISO_C", AB_IS_ISO_C),
2141 minit ("VALUE", AB_VALUE),
2142 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2143 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2144 minit ("LOCK_COMP", AB_LOCK_COMP),
2145 minit ("EVENT_COMP", AB_EVENT_COMP),
2146 minit ("POINTER_COMP", AB_POINTER_COMP),
2147 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2148 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2149 minit ("ZERO_COMP", AB_ZERO_COMP),
2150 minit ("PROTECTED", AB_PROTECTED),
2151 minit ("ABSTRACT", AB_ABSTRACT),
2152 minit ("IS_CLASS", AB_IS_CLASS),
2153 minit ("PROCEDURE", AB_PROCEDURE),
2154 minit ("PROC_POINTER", AB_PROC_POINTER),
2155 minit ("VTYPE", AB_VTYPE),
2156 minit ("VTAB", AB_VTAB),
2157 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2158 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2159 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2160 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2161 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2162 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2163 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2164 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2165 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2166 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2167 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2168 minit ("OMP_DECLARE_TARGET_LINK", AB_OMP_DECLARE_TARGET_LINK),
2169 minit ("PDT_KIND", AB_PDT_KIND),
2170 minit ("PDT_LEN", AB_PDT_LEN),
2171 minit ("PDT_TYPE", AB_PDT_TYPE),
2172 minit ("PDT_TEMPLATE", AB_PDT_TEMPLATE),
2173 minit ("PDT_ARRAY", AB_PDT_ARRAY),
2174 minit ("PDT_STRING", AB_PDT_STRING),
2175 minit ("OACC_ROUTINE_LOP_GANG", AB_OACC_ROUTINE_LOP_GANG),
2176 minit ("OACC_ROUTINE_LOP_WORKER", AB_OACC_ROUTINE_LOP_WORKER),
2177 minit ("OACC_ROUTINE_LOP_VECTOR", AB_OACC_ROUTINE_LOP_VECTOR),
2178 minit ("OACC_ROUTINE_LOP_SEQ", AB_OACC_ROUTINE_LOP_SEQ),
2179 minit ("OACC_ROUTINE_NOHOST", AB_OACC_ROUTINE_NOHOST),
2180 minit ("OMP_REQ_REVERSE_OFFLOAD", AB_OMP_REQ_REVERSE_OFFLOAD),
2181 minit ("OMP_REQ_UNIFIED_ADDRESS", AB_OMP_REQ_UNIFIED_ADDRESS),
2182 minit ("OMP_REQ_UNIFIED_SHARED_MEMORY", AB_OMP_REQ_UNIFIED_SHARED_MEMORY),
2183 minit ("OMP_REQ_SELF_MAPS", AB_OMP_REQ_SELF_MAPS),
2184 minit ("OMP_REQ_DYNAMIC_ALLOCATORS", AB_OMP_REQ_DYNAMIC_ALLOCATORS),
2185 minit ("OMP_REQ_MEM_ORDER_SEQ_CST", AB_OMP_REQ_MEM_ORDER_SEQ_CST),
2186 minit ("OMP_REQ_MEM_ORDER_ACQ_REL", AB_OMP_REQ_MEM_ORDER_ACQ_REL),
2187 minit ("OMP_REQ_MEM_ORDER_ACQUIRE", AB_OMP_REQ_MEM_ORDER_ACQUIRE),
2188 minit ("OMP_REQ_MEM_ORDER_RELAXED", AB_OMP_REQ_MEM_ORDER_RELAXED),
2189 minit ("OMP_REQ_MEM_ORDER_RELEASE", AB_OMP_REQ_MEM_ORDER_RELEASE),
2190 minit ("OMP_DEVICE_TYPE_HOST", AB_OMP_DEVICE_TYPE_HOST),
2191 minit ("OMP_DEVICE_TYPE_NOHOST", AB_OMP_DEVICE_TYPE_NOHOST),
2192 minit ("OMP_DEVICE_TYPE_ANYHOST", AB_OMP_DEVICE_TYPE_ANY),
2193 minit (NULL, -1)
2196 /* For binding attributes. */
2197 static const mstring binding_passing[] =
2199 minit ("PASS", 0),
2200 minit ("NOPASS", 1),
2201 minit (NULL, -1)
2203 static const mstring binding_overriding[] =
2205 minit ("OVERRIDABLE", 0),
2206 minit ("NON_OVERRIDABLE", 1),
2207 minit ("DEFERRED", 2),
2208 minit (NULL, -1)
2210 static const mstring binding_generic[] =
2212 minit ("SPECIFIC", 0),
2213 minit ("GENERIC", 1),
2214 minit (NULL, -1)
2216 static const mstring binding_ppc[] =
2218 minit ("NO_PPC", 0),
2219 minit ("PPC", 1),
2220 minit (NULL, -1)
2223 /* Specialization of mio_name. */
2224 DECL_MIO_NAME (ab_attribute)
2225 DECL_MIO_NAME (ar_type)
2226 DECL_MIO_NAME (array_type)
2227 DECL_MIO_NAME (bt)
2228 DECL_MIO_NAME (expr_t)
2229 DECL_MIO_NAME (gfc_access)
2230 DECL_MIO_NAME (gfc_intrinsic_op)
2231 DECL_MIO_NAME (ifsrc)
2232 DECL_MIO_NAME (save_state)
2233 DECL_MIO_NAME (procedure_type)
2234 DECL_MIO_NAME (ref_type)
2235 DECL_MIO_NAME (sym_flavor)
2236 DECL_MIO_NAME (sym_intent)
2237 DECL_MIO_NAME (inquiry_type)
2238 #undef DECL_MIO_NAME
2240 /* Verify OACC_ROUTINE_LOP_NONE. */
2242 static void
2243 verify_OACC_ROUTINE_LOP_NONE (enum oacc_routine_lop lop)
2245 if (lop != OACC_ROUTINE_LOP_NONE)
2246 bad_module ("Unsupported: multiple OpenACC 'routine' levels of parallelism");
2249 /* Symbol attributes are stored in list with the first three elements
2250 being the enumerated fields, while the remaining elements (if any)
2251 indicate the individual attribute bits. The access field is not
2252 saved-- it controls what symbols are exported when a module is
2253 written. */
2255 static void
2256 mio_symbol_attribute (symbol_attribute *attr)
2258 atom_type t;
2259 unsigned ext_attr,extension_level;
2261 mio_lparen ();
2263 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2264 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2265 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2266 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2267 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2269 ext_attr = attr->ext_attr;
2270 mio_integer ((int *) &ext_attr);
2271 attr->ext_attr = ext_attr;
2273 extension_level = attr->extension;
2274 mio_integer ((int *) &extension_level);
2275 attr->extension = extension_level;
2277 if (iomode == IO_OUTPUT)
2279 if (attr->allocatable)
2280 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2281 if (attr->artificial)
2282 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2283 if (attr->asynchronous)
2284 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2285 if (attr->dimension)
2286 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2287 if (attr->codimension)
2288 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2289 if (attr->contiguous)
2290 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2291 if (attr->external)
2292 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2293 if (attr->intrinsic)
2294 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2295 if (attr->optional)
2296 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2297 if (attr->pointer)
2298 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2299 if (attr->class_pointer)
2300 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2301 if (attr->is_protected)
2302 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2303 if (attr->value)
2304 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2305 if (attr->volatile_)
2306 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2307 if (attr->target)
2308 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2309 if (attr->threadprivate)
2310 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2311 if (attr->dummy)
2312 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2313 if (attr->result)
2314 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2315 /* We deliberately don't preserve the "entry" flag. */
2317 if (attr->data)
2318 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2319 if (attr->in_namelist)
2320 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2321 if (attr->in_common)
2322 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2324 if (attr->function)
2325 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2326 if (attr->subroutine)
2327 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2328 if (attr->generic)
2329 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2330 if (attr->abstract)
2331 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2333 if (attr->sequence)
2334 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2335 if (attr->elemental)
2336 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2337 if (attr->pure)
2338 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2339 if (attr->implicit_pure)
2340 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2341 if (attr->unlimited_polymorphic)
2342 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2343 if (attr->recursive)
2344 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2345 if (attr->always_explicit)
2346 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2347 if (attr->cray_pointer)
2348 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2349 if (attr->cray_pointee)
2350 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2351 if (attr->is_bind_c)
2352 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2353 if (attr->is_c_interop)
2354 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2355 if (attr->is_iso_c)
2356 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2357 if (attr->alloc_comp)
2358 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2359 if (attr->pointer_comp)
2360 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2361 if (attr->proc_pointer_comp)
2362 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2363 if (attr->private_comp)
2364 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2365 if (attr->coarray_comp)
2366 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2367 if (attr->lock_comp)
2368 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2369 if (attr->event_comp)
2370 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2371 if (attr->zero_comp)
2372 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2373 if (attr->is_class)
2374 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2375 if (attr->procedure)
2376 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2377 if (attr->proc_pointer)
2378 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2379 if (attr->vtype)
2380 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2381 if (attr->vtab)
2382 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2383 if (attr->omp_declare_target)
2384 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2385 if (attr->array_outer_dependency)
2386 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2387 if (attr->module_procedure)
2388 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2389 if (attr->oacc_declare_create)
2390 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2391 if (attr->oacc_declare_copyin)
2392 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2393 if (attr->oacc_declare_deviceptr)
2394 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2395 if (attr->oacc_declare_device_resident)
2396 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2397 if (attr->oacc_declare_link)
2398 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2399 if (attr->omp_declare_target_link)
2400 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET_LINK, attr_bits);
2401 if (attr->pdt_kind)
2402 MIO_NAME (ab_attribute) (AB_PDT_KIND, attr_bits);
2403 if (attr->pdt_len)
2404 MIO_NAME (ab_attribute) (AB_PDT_LEN, attr_bits);
2405 if (attr->pdt_type)
2406 MIO_NAME (ab_attribute) (AB_PDT_TYPE, attr_bits);
2407 if (attr->pdt_template)
2408 MIO_NAME (ab_attribute) (AB_PDT_TEMPLATE, attr_bits);
2409 if (attr->pdt_array)
2410 MIO_NAME (ab_attribute) (AB_PDT_ARRAY, attr_bits);
2411 if (attr->pdt_string)
2412 MIO_NAME (ab_attribute) (AB_PDT_STRING, attr_bits);
2413 switch (attr->oacc_routine_lop)
2415 case OACC_ROUTINE_LOP_NONE:
2416 /* This is the default anyway, and for maintaining compatibility with
2417 the current MOD_VERSION, we're not emitting anything in that
2418 case. */
2419 break;
2420 case OACC_ROUTINE_LOP_GANG:
2421 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_GANG, attr_bits);
2422 break;
2423 case OACC_ROUTINE_LOP_WORKER:
2424 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_WORKER, attr_bits);
2425 break;
2426 case OACC_ROUTINE_LOP_VECTOR:
2427 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_VECTOR, attr_bits);
2428 break;
2429 case OACC_ROUTINE_LOP_SEQ:
2430 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_SEQ, attr_bits);
2431 break;
2432 case OACC_ROUTINE_LOP_ERROR:
2433 /* ... intentionally omitted here; it's only used internally. */
2434 default:
2435 gcc_unreachable ();
2437 if (attr->oacc_routine_nohost)
2438 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_NOHOST, attr_bits);
2440 if (attr->flavor == FL_MODULE && gfc_current_ns->omp_requires)
2442 if (gfc_current_ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD)
2443 MIO_NAME (ab_attribute) (AB_OMP_REQ_REVERSE_OFFLOAD, attr_bits);
2444 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_ADDRESS)
2445 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_ADDRESS, attr_bits);
2446 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
2447 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_SHARED_MEMORY, attr_bits);
2448 if (gfc_current_ns->omp_requires & OMP_REQ_SELF_MAPS)
2449 MIO_NAME (ab_attribute) (AB_OMP_REQ_SELF_MAPS, attr_bits);
2450 if (gfc_current_ns->omp_requires & OMP_REQ_DYNAMIC_ALLOCATORS)
2451 MIO_NAME (ab_attribute) (AB_OMP_REQ_DYNAMIC_ALLOCATORS, attr_bits);
2452 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2453 == OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST)
2454 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_SEQ_CST, attr_bits);
2455 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2456 == OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL)
2457 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQ_REL, attr_bits);
2458 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2459 == OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE)
2460 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQUIRE, attr_bits);
2461 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2462 == OMP_REQ_ATOMIC_MEM_ORDER_RELAXED)
2463 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELAXED, attr_bits);
2464 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2465 == OMP_REQ_ATOMIC_MEM_ORDER_RELEASE)
2466 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELEASE, attr_bits);
2468 switch (attr->omp_device_type)
2470 case OMP_DEVICE_TYPE_UNSET:
2471 break;
2472 case OMP_DEVICE_TYPE_HOST:
2473 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_HOST, attr_bits);
2474 break;
2475 case OMP_DEVICE_TYPE_NOHOST:
2476 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_NOHOST, attr_bits);
2477 break;
2478 case OMP_DEVICE_TYPE_ANY:
2479 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_ANY, attr_bits);
2480 break;
2481 default:
2482 gcc_unreachable ();
2484 mio_rparen ();
2486 else
2488 for (;;)
2490 t = parse_atom ();
2491 if (t == ATOM_RPAREN)
2492 break;
2493 if (t != ATOM_NAME)
2494 bad_module ("Expected attribute bit name");
2496 switch ((ab_attribute) find_enum (attr_bits))
2498 case AB_ALLOCATABLE:
2499 attr->allocatable = 1;
2500 break;
2501 case AB_ARTIFICIAL:
2502 attr->artificial = 1;
2503 break;
2504 case AB_ASYNCHRONOUS:
2505 attr->asynchronous = 1;
2506 break;
2507 case AB_DIMENSION:
2508 attr->dimension = 1;
2509 break;
2510 case AB_CODIMENSION:
2511 attr->codimension = 1;
2512 break;
2513 case AB_CONTIGUOUS:
2514 attr->contiguous = 1;
2515 break;
2516 case AB_EXTERNAL:
2517 attr->external = 1;
2518 break;
2519 case AB_INTRINSIC:
2520 attr->intrinsic = 1;
2521 break;
2522 case AB_OPTIONAL:
2523 attr->optional = 1;
2524 break;
2525 case AB_POINTER:
2526 attr->pointer = 1;
2527 break;
2528 case AB_CLASS_POINTER:
2529 attr->class_pointer = 1;
2530 break;
2531 case AB_PROTECTED:
2532 attr->is_protected = 1;
2533 break;
2534 case AB_VALUE:
2535 attr->value = 1;
2536 break;
2537 case AB_VOLATILE:
2538 attr->volatile_ = 1;
2539 break;
2540 case AB_TARGET:
2541 attr->target = 1;
2542 break;
2543 case AB_THREADPRIVATE:
2544 attr->threadprivate = 1;
2545 break;
2546 case AB_DUMMY:
2547 attr->dummy = 1;
2548 break;
2549 case AB_RESULT:
2550 attr->result = 1;
2551 break;
2552 case AB_DATA:
2553 attr->data = 1;
2554 break;
2555 case AB_IN_NAMELIST:
2556 attr->in_namelist = 1;
2557 break;
2558 case AB_IN_COMMON:
2559 attr->in_common = 1;
2560 break;
2561 case AB_FUNCTION:
2562 attr->function = 1;
2563 break;
2564 case AB_SUBROUTINE:
2565 attr->subroutine = 1;
2566 break;
2567 case AB_GENERIC:
2568 attr->generic = 1;
2569 break;
2570 case AB_ABSTRACT:
2571 attr->abstract = 1;
2572 break;
2573 case AB_SEQUENCE:
2574 attr->sequence = 1;
2575 break;
2576 case AB_ELEMENTAL:
2577 attr->elemental = 1;
2578 break;
2579 case AB_PURE:
2580 attr->pure = 1;
2581 break;
2582 case AB_IMPLICIT_PURE:
2583 attr->implicit_pure = 1;
2584 break;
2585 case AB_UNLIMITED_POLY:
2586 attr->unlimited_polymorphic = 1;
2587 break;
2588 case AB_RECURSIVE:
2589 attr->recursive = 1;
2590 break;
2591 case AB_ALWAYS_EXPLICIT:
2592 attr->always_explicit = 1;
2593 break;
2594 case AB_CRAY_POINTER:
2595 attr->cray_pointer = 1;
2596 break;
2597 case AB_CRAY_POINTEE:
2598 attr->cray_pointee = 1;
2599 break;
2600 case AB_IS_BIND_C:
2601 attr->is_bind_c = 1;
2602 break;
2603 case AB_IS_C_INTEROP:
2604 attr->is_c_interop = 1;
2605 break;
2606 case AB_IS_ISO_C:
2607 attr->is_iso_c = 1;
2608 break;
2609 case AB_ALLOC_COMP:
2610 attr->alloc_comp = 1;
2611 break;
2612 case AB_COARRAY_COMP:
2613 attr->coarray_comp = 1;
2614 break;
2615 case AB_LOCK_COMP:
2616 attr->lock_comp = 1;
2617 break;
2618 case AB_EVENT_COMP:
2619 attr->event_comp = 1;
2620 break;
2621 case AB_POINTER_COMP:
2622 attr->pointer_comp = 1;
2623 break;
2624 case AB_PROC_POINTER_COMP:
2625 attr->proc_pointer_comp = 1;
2626 break;
2627 case AB_PRIVATE_COMP:
2628 attr->private_comp = 1;
2629 break;
2630 case AB_ZERO_COMP:
2631 attr->zero_comp = 1;
2632 break;
2633 case AB_IS_CLASS:
2634 attr->is_class = 1;
2635 break;
2636 case AB_PROCEDURE:
2637 attr->procedure = 1;
2638 break;
2639 case AB_PROC_POINTER:
2640 attr->proc_pointer = 1;
2641 break;
2642 case AB_VTYPE:
2643 attr->vtype = 1;
2644 break;
2645 case AB_VTAB:
2646 attr->vtab = 1;
2647 break;
2648 case AB_OMP_DECLARE_TARGET:
2649 attr->omp_declare_target = 1;
2650 break;
2651 case AB_OMP_DECLARE_TARGET_LINK:
2652 attr->omp_declare_target_link = 1;
2653 break;
2654 case AB_ARRAY_OUTER_DEPENDENCY:
2655 attr->array_outer_dependency =1;
2656 break;
2657 case AB_MODULE_PROCEDURE:
2658 attr->module_procedure =1;
2659 break;
2660 case AB_OACC_DECLARE_CREATE:
2661 attr->oacc_declare_create = 1;
2662 break;
2663 case AB_OACC_DECLARE_COPYIN:
2664 attr->oacc_declare_copyin = 1;
2665 break;
2666 case AB_OACC_DECLARE_DEVICEPTR:
2667 attr->oacc_declare_deviceptr = 1;
2668 break;
2669 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2670 attr->oacc_declare_device_resident = 1;
2671 break;
2672 case AB_OACC_DECLARE_LINK:
2673 attr->oacc_declare_link = 1;
2674 break;
2675 case AB_PDT_KIND:
2676 attr->pdt_kind = 1;
2677 break;
2678 case AB_PDT_LEN:
2679 attr->pdt_len = 1;
2680 break;
2681 case AB_PDT_TYPE:
2682 attr->pdt_type = 1;
2683 break;
2684 case AB_PDT_TEMPLATE:
2685 attr->pdt_template = 1;
2686 break;
2687 case AB_PDT_ARRAY:
2688 attr->pdt_array = 1;
2689 break;
2690 case AB_PDT_STRING:
2691 attr->pdt_string = 1;
2692 break;
2693 case AB_OACC_ROUTINE_LOP_GANG:
2694 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2695 attr->oacc_routine_lop = OACC_ROUTINE_LOP_GANG;
2696 break;
2697 case AB_OACC_ROUTINE_LOP_WORKER:
2698 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2699 attr->oacc_routine_lop = OACC_ROUTINE_LOP_WORKER;
2700 break;
2701 case AB_OACC_ROUTINE_LOP_VECTOR:
2702 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2703 attr->oacc_routine_lop = OACC_ROUTINE_LOP_VECTOR;
2704 break;
2705 case AB_OACC_ROUTINE_LOP_SEQ:
2706 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2707 attr->oacc_routine_lop = OACC_ROUTINE_LOP_SEQ;
2708 break;
2709 case AB_OACC_ROUTINE_NOHOST:
2710 attr->oacc_routine_nohost = 1;
2711 break;
2712 case AB_OMP_REQ_REVERSE_OFFLOAD:
2713 gfc_omp_requires_add_clause (OMP_REQ_REVERSE_OFFLOAD,
2714 "reverse_offload",
2715 &gfc_current_locus,
2716 module_name);
2717 break;
2718 case AB_OMP_REQ_UNIFIED_ADDRESS:
2719 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_ADDRESS,
2720 "unified_address",
2721 &gfc_current_locus,
2722 module_name);
2723 break;
2724 case AB_OMP_REQ_UNIFIED_SHARED_MEMORY:
2725 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_SHARED_MEMORY,
2726 "unified_shared_memory",
2727 &gfc_current_locus,
2728 module_name);
2729 break;
2730 case AB_OMP_REQ_SELF_MAPS:
2731 gfc_omp_requires_add_clause (OMP_REQ_SELF_MAPS,
2732 "self_maps",
2733 &gfc_current_locus,
2734 module_name);
2735 break;
2736 case AB_OMP_REQ_DYNAMIC_ALLOCATORS:
2737 gfc_omp_requires_add_clause (OMP_REQ_DYNAMIC_ALLOCATORS,
2738 "dynamic_allocators",
2739 &gfc_current_locus,
2740 module_name);
2741 break;
2742 case AB_OMP_REQ_MEM_ORDER_SEQ_CST:
2743 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST,
2744 "seq_cst", &gfc_current_locus,
2745 module_name);
2746 break;
2747 case AB_OMP_REQ_MEM_ORDER_ACQ_REL:
2748 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL,
2749 "acq_rel", &gfc_current_locus,
2750 module_name);
2751 break;
2752 case AB_OMP_REQ_MEM_ORDER_ACQUIRE:
2753 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE,
2754 "acquires", &gfc_current_locus,
2755 module_name);
2756 break;
2757 case AB_OMP_REQ_MEM_ORDER_RELAXED:
2758 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELAXED,
2759 "relaxed", &gfc_current_locus,
2760 module_name);
2761 break;
2762 case AB_OMP_REQ_MEM_ORDER_RELEASE:
2763 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELEASE,
2764 "release", &gfc_current_locus,
2765 module_name);
2766 break;
2767 case AB_OMP_DEVICE_TYPE_HOST:
2768 attr->omp_device_type = OMP_DEVICE_TYPE_HOST;
2769 break;
2770 case AB_OMP_DEVICE_TYPE_NOHOST:
2771 attr->omp_device_type = OMP_DEVICE_TYPE_NOHOST;
2772 break;
2773 case AB_OMP_DEVICE_TYPE_ANY:
2774 attr->omp_device_type = OMP_DEVICE_TYPE_ANY;
2775 break;
2782 static const mstring bt_types[] = {
2783 minit ("INTEGER", BT_INTEGER),
2784 minit ("REAL", BT_REAL),
2785 minit ("COMPLEX", BT_COMPLEX),
2786 minit ("LOGICAL", BT_LOGICAL),
2787 minit ("CHARACTER", BT_CHARACTER),
2788 minit ("UNION", BT_UNION),
2789 minit ("DERIVED", BT_DERIVED),
2790 minit ("CLASS", BT_CLASS),
2791 minit ("PROCEDURE", BT_PROCEDURE),
2792 minit ("UNKNOWN", BT_UNKNOWN),
2793 minit ("VOID", BT_VOID),
2794 minit ("ASSUMED", BT_ASSUMED),
2795 minit ("UNSIGNED", BT_UNSIGNED),
2796 minit (NULL, -1)
2800 static void
2801 mio_charlen (gfc_charlen **clp)
2803 gfc_charlen *cl;
2805 mio_lparen ();
2807 if (iomode == IO_OUTPUT)
2809 cl = *clp;
2810 if (cl != NULL)
2811 mio_expr (&cl->length);
2813 else
2815 if (peek_atom () != ATOM_RPAREN)
2817 cl = gfc_new_charlen (gfc_current_ns, NULL);
2818 mio_expr (&cl->length);
2819 *clp = cl;
2823 mio_rparen ();
2827 /* See if a name is a generated name. */
2829 static int
2830 check_unique_name (const char *name)
2832 return *name == '@';
2836 static void
2837 mio_typespec (gfc_typespec *ts)
2839 mio_lparen ();
2841 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2843 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2844 mio_integer (&ts->kind);
2845 else
2846 mio_symbol_ref (&ts->u.derived);
2848 mio_symbol_ref (&ts->interface);
2850 /* Add info for C interop and is_iso_c. */
2851 mio_integer (&ts->is_c_interop);
2852 mio_integer (&ts->is_iso_c);
2854 /* If the typespec is for an identifier either from iso_c_binding, or
2855 a constant that was initialized to an identifier from it, use the
2856 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2857 if (ts->is_iso_c)
2858 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2859 else
2860 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2862 if (ts->type != BT_CHARACTER)
2864 /* ts->u.cl is only valid for BT_CHARACTER. */
2865 mio_lparen ();
2866 mio_rparen ();
2868 else
2869 mio_charlen (&ts->u.cl);
2871 /* So as not to disturb the existing API, use an ATOM_NAME to
2872 transmit deferred characteristic for characters (F2003). */
2873 if (iomode == IO_OUTPUT)
2875 if (ts->type == BT_CHARACTER && ts->deferred)
2876 write_atom (ATOM_NAME, "DEFERRED_CL");
2878 else if (peek_atom () != ATOM_RPAREN)
2880 if (parse_atom () != ATOM_NAME)
2881 bad_module ("Expected string");
2882 ts->deferred = 1;
2885 mio_rparen ();
2889 static const mstring array_spec_types[] = {
2890 minit ("EXPLICIT", AS_EXPLICIT),
2891 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2892 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2893 minit ("DEFERRED", AS_DEFERRED),
2894 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2895 minit (NULL, -1)
2899 static void
2900 mio_array_spec (gfc_array_spec **asp)
2902 gfc_array_spec *as;
2903 int i;
2905 mio_lparen ();
2907 if (iomode == IO_OUTPUT)
2909 int rank;
2911 if (*asp == NULL)
2912 goto done;
2913 as = *asp;
2915 /* mio_integer expects nonnegative values. */
2916 rank = as->rank > 0 ? as->rank : 0;
2917 mio_integer (&rank);
2919 else
2921 if (peek_atom () == ATOM_RPAREN)
2923 *asp = NULL;
2924 goto done;
2927 *asp = as = gfc_get_array_spec ();
2928 mio_integer (&as->rank);
2931 mio_integer (&as->corank);
2932 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2934 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2935 as->rank = -1;
2936 if (iomode == IO_INPUT && as->corank)
2937 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2939 if (as->rank + as->corank > 0)
2940 for (i = 0; i < as->rank + as->corank; i++)
2942 mio_expr (&as->lower[i]);
2943 mio_expr (&as->upper[i]);
2946 done:
2947 mio_rparen ();
2951 /* Given a pointer to an array reference structure (which lives in a
2952 gfc_ref structure), find the corresponding array specification
2953 structure. Storing the pointer in the ref structure doesn't quite
2954 work when loading from a module. Generating code for an array
2955 reference also needs more information than just the array spec. */
2957 static const mstring array_ref_types[] = {
2958 minit ("FULL", AR_FULL),
2959 minit ("ELEMENT", AR_ELEMENT),
2960 minit ("SECTION", AR_SECTION),
2961 minit (NULL, -1)
2965 static void
2966 mio_array_ref (gfc_array_ref *ar)
2968 int i;
2970 mio_lparen ();
2971 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2972 mio_integer (&ar->dimen);
2974 switch (ar->type)
2976 case AR_FULL:
2977 break;
2979 case AR_ELEMENT:
2980 for (i = 0; i < ar->dimen; i++)
2981 mio_expr (&ar->start[i]);
2983 break;
2985 case AR_SECTION:
2986 for (i = 0; i < ar->dimen; i++)
2988 mio_expr (&ar->start[i]);
2989 mio_expr (&ar->end[i]);
2990 mio_expr (&ar->stride[i]);
2993 break;
2995 case AR_UNKNOWN:
2996 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2999 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
3000 we can't call mio_integer directly. Instead loop over each element
3001 and cast it to/from an integer. */
3002 if (iomode == IO_OUTPUT)
3004 for (i = 0; i < ar->dimen; i++)
3006 HOST_WIDE_INT tmp = (HOST_WIDE_INT)ar->dimen_type[i];
3007 write_atom (ATOM_INTEGER, &tmp);
3010 else
3012 for (i = 0; i < ar->dimen; i++)
3014 require_atom (ATOM_INTEGER);
3015 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
3019 if (iomode == IO_INPUT)
3021 ar->where = gfc_current_locus;
3023 for (i = 0; i < ar->dimen; i++)
3024 ar->c_where[i] = gfc_current_locus;
3027 mio_rparen ();
3031 /* Saves or restores a pointer. The pointer is converted back and
3032 forth from an integer. We return the pointer_info pointer so that
3033 the caller can take additional action based on the pointer type. */
3035 static pointer_info *
3036 mio_pointer_ref (void *gp)
3038 pointer_info *p;
3040 if (iomode == IO_OUTPUT)
3042 p = get_pointer (*((char **) gp));
3043 HOST_WIDE_INT hwi = p->integer;
3044 write_atom (ATOM_INTEGER, &hwi);
3046 else
3048 require_atom (ATOM_INTEGER);
3049 p = add_fixup (atom_int, gp);
3052 return p;
3056 /* Save and load references to components that occur within
3057 expressions. We have to describe these references by a number and
3058 by name. The number is necessary for forward references during
3059 reading, and the name is necessary if the symbol already exists in
3060 the namespace and is not loaded again. */
3062 static void
3063 mio_component_ref (gfc_component **cp)
3065 pointer_info *p;
3067 p = mio_pointer_ref (cp);
3068 if (p->type == P_UNKNOWN)
3069 p->type = P_COMPONENT;
3073 static void mio_namespace_ref (gfc_namespace **nsp);
3074 static void mio_formal_arglist (gfc_formal_arglist **formal);
3075 static void mio_typebound_proc (gfc_typebound_proc** proc);
3076 static void mio_actual_arglist (gfc_actual_arglist **ap, bool pdt);
3078 static void
3079 mio_component (gfc_component *c, int vtype)
3081 pointer_info *p;
3083 mio_lparen ();
3085 if (iomode == IO_OUTPUT)
3087 p = get_pointer (c);
3088 mio_hwi (&p->integer);
3090 else
3092 HOST_WIDE_INT n;
3093 mio_hwi (&n);
3094 p = get_integer (n);
3095 associate_integer_pointer (p, c);
3098 if (p->type == P_UNKNOWN)
3099 p->type = P_COMPONENT;
3101 mio_pool_string (&c->name);
3102 mio_typespec (&c->ts);
3103 mio_array_spec (&c->as);
3105 /* PDT templates store the expression for the kind of a component here. */
3106 mio_expr (&c->kind_expr);
3108 /* PDT types store the component specification list here. */
3109 mio_actual_arglist (&c->param_list, true);
3111 mio_symbol_attribute (&c->attr);
3112 if (c->ts.type == BT_CLASS)
3113 c->attr.class_ok = 1;
3114 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
3116 if (!vtype || strcmp (c->name, "_final") == 0
3117 || strcmp (c->name, "_hash") == 0)
3118 mio_expr (&c->initializer);
3120 if (c->attr.proc_pointer)
3121 mio_typebound_proc (&c->tb);
3123 c->loc = gfc_current_locus;
3125 mio_rparen ();
3129 static void
3130 mio_component_list (gfc_component **cp, int vtype)
3132 gfc_component *c, *tail;
3134 mio_lparen ();
3136 if (iomode == IO_OUTPUT)
3138 for (c = *cp; c; c = c->next)
3139 mio_component (c, vtype);
3141 else
3143 *cp = NULL;
3144 tail = NULL;
3146 for (;;)
3148 if (peek_atom () == ATOM_RPAREN)
3149 break;
3151 c = gfc_get_component ();
3152 mio_component (c, vtype);
3154 if (tail == NULL)
3155 *cp = c;
3156 else
3157 tail->next = c;
3159 tail = c;
3163 mio_rparen ();
3167 static void
3168 mio_actual_arg (gfc_actual_arglist *a, bool pdt)
3170 mio_lparen ();
3171 mio_pool_string (&a->name);
3172 mio_expr (&a->expr);
3173 if (pdt)
3174 mio_integer ((int *)&a->spec_type);
3175 mio_rparen ();
3179 static void
3180 mio_actual_arglist (gfc_actual_arglist **ap, bool pdt)
3182 gfc_actual_arglist *a, *tail;
3184 mio_lparen ();
3186 if (iomode == IO_OUTPUT)
3188 for (a = *ap; a; a = a->next)
3189 mio_actual_arg (a, pdt);
3192 else
3194 tail = NULL;
3196 for (;;)
3198 if (peek_atom () != ATOM_LPAREN)
3199 break;
3201 a = gfc_get_actual_arglist ();
3203 if (tail == NULL)
3204 *ap = a;
3205 else
3206 tail->next = a;
3208 tail = a;
3209 mio_actual_arg (a, pdt);
3213 mio_rparen ();
3217 /* Read and write formal argument lists. */
3219 static void
3220 mio_formal_arglist (gfc_formal_arglist **formal)
3222 gfc_formal_arglist *f, *tail;
3224 mio_lparen ();
3226 if (iomode == IO_OUTPUT)
3228 for (f = *formal; f; f = f->next)
3229 mio_symbol_ref (&f->sym);
3231 else
3233 *formal = tail = NULL;
3235 while (peek_atom () != ATOM_RPAREN)
3237 f = gfc_get_formal_arglist ();
3238 mio_symbol_ref (&f->sym);
3240 if (*formal == NULL)
3241 *formal = f;
3242 else
3243 tail->next = f;
3245 tail = f;
3249 mio_rparen ();
3253 /* Save or restore a reference to a symbol node. */
3255 pointer_info *
3256 mio_symbol_ref (gfc_symbol **symp)
3258 pointer_info *p;
3260 p = mio_pointer_ref (symp);
3261 if (p->type == P_UNKNOWN)
3262 p->type = P_SYMBOL;
3264 if (iomode == IO_OUTPUT)
3266 if (p->u.wsym.state == UNREFERENCED)
3267 p->u.wsym.state = NEEDS_WRITE;
3269 else
3271 if (p->u.rsym.state == UNUSED)
3272 p->u.rsym.state = NEEDED;
3274 return p;
3278 /* Save or restore a reference to a symtree node. */
3280 static void
3281 mio_symtree_ref (gfc_symtree **stp)
3283 pointer_info *p;
3284 fixup_t *f;
3286 if (iomode == IO_OUTPUT)
3287 mio_symbol_ref (&(*stp)->n.sym);
3288 else
3290 require_atom (ATOM_INTEGER);
3291 p = get_integer (atom_int);
3293 /* An unused equivalence member; make a symbol and a symtree
3294 for it. */
3295 if (in_load_equiv && p->u.rsym.symtree == NULL)
3297 /* Since this is not used, it must have a unique name. */
3298 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
3300 /* Make the symbol. */
3301 if (p->u.rsym.sym == NULL)
3303 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
3304 gfc_current_ns);
3305 p->u.rsym.sym->module = gfc_get_string ("%s", p->u.rsym.module);
3308 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
3309 p->u.rsym.symtree->n.sym->refs++;
3310 p->u.rsym.referenced = 1;
3312 /* If the symbol is PRIVATE and in COMMON, load_commons will
3313 generate a fixup symbol, which must be associated. */
3314 if (p->fixup)
3315 resolve_fixups (p->fixup, p->u.rsym.sym);
3316 p->fixup = NULL;
3319 if (p->type == P_UNKNOWN)
3320 p->type = P_SYMBOL;
3322 if (p->u.rsym.state == UNUSED)
3323 p->u.rsym.state = NEEDED;
3325 if (p->u.rsym.symtree != NULL)
3327 *stp = p->u.rsym.symtree;
3329 else
3331 f = XCNEW (fixup_t);
3333 f->next = p->u.rsym.stfixup;
3334 p->u.rsym.stfixup = f;
3336 f->pointer = (void **) stp;
3342 static void
3343 mio_iterator (gfc_iterator **ip)
3345 gfc_iterator *iter;
3347 mio_lparen ();
3349 if (iomode == IO_OUTPUT)
3351 if (*ip == NULL)
3352 goto done;
3354 else
3356 if (peek_atom () == ATOM_RPAREN)
3358 *ip = NULL;
3359 goto done;
3362 *ip = gfc_get_iterator ();
3365 iter = *ip;
3367 mio_expr (&iter->var);
3368 mio_expr (&iter->start);
3369 mio_expr (&iter->end);
3370 mio_expr (&iter->step);
3372 done:
3373 mio_rparen ();
3377 static void
3378 mio_constructor (gfc_constructor_base *cp)
3380 gfc_constructor *c;
3382 mio_lparen ();
3384 if (iomode == IO_OUTPUT)
3386 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3388 mio_lparen ();
3389 mio_expr (&c->expr);
3390 mio_iterator (&c->iterator);
3391 mio_rparen ();
3394 else
3396 while (peek_atom () != ATOM_RPAREN)
3398 c = gfc_constructor_append_expr (cp, NULL, NULL);
3400 mio_lparen ();
3401 mio_expr (&c->expr);
3402 mio_iterator (&c->iterator);
3403 mio_rparen ();
3407 mio_rparen ();
3411 static const mstring ref_types[] = {
3412 minit ("ARRAY", REF_ARRAY),
3413 minit ("COMPONENT", REF_COMPONENT),
3414 minit ("SUBSTRING", REF_SUBSTRING),
3415 minit ("INQUIRY", REF_INQUIRY),
3416 minit (NULL, -1)
3419 static const mstring inquiry_types[] = {
3420 minit ("RE", INQUIRY_RE),
3421 minit ("IM", INQUIRY_IM),
3422 minit ("KIND", INQUIRY_KIND),
3423 minit ("LEN", INQUIRY_LEN),
3424 minit (NULL, -1)
3428 static void
3429 mio_ref (gfc_ref **rp)
3431 gfc_ref *r;
3433 mio_lparen ();
3435 r = *rp;
3436 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3438 switch (r->type)
3440 case REF_ARRAY:
3441 mio_array_ref (&r->u.ar);
3442 break;
3444 case REF_COMPONENT:
3445 mio_symbol_ref (&r->u.c.sym);
3446 mio_component_ref (&r->u.c.component);
3447 break;
3449 case REF_SUBSTRING:
3450 mio_expr (&r->u.ss.start);
3451 mio_expr (&r->u.ss.end);
3452 mio_charlen (&r->u.ss.length);
3453 break;
3455 case REF_INQUIRY:
3456 r->u.i = MIO_NAME (inquiry_type) (r->u.i, inquiry_types);
3457 break;
3460 mio_rparen ();
3464 static void
3465 mio_ref_list (gfc_ref **rp)
3467 gfc_ref *ref, *head, *tail;
3469 mio_lparen ();
3471 if (iomode == IO_OUTPUT)
3473 for (ref = *rp; ref; ref = ref->next)
3474 mio_ref (&ref);
3476 else
3478 head = tail = NULL;
3480 while (peek_atom () != ATOM_RPAREN)
3482 if (head == NULL)
3483 head = tail = gfc_get_ref ();
3484 else
3486 tail->next = gfc_get_ref ();
3487 tail = tail->next;
3490 mio_ref (&tail);
3493 *rp = head;
3496 mio_rparen ();
3500 /* Read and write an integer value. */
3502 static void
3503 mio_gmp_integer (mpz_t *integer)
3505 char *p;
3507 if (iomode == IO_INPUT)
3509 if (parse_atom () != ATOM_STRING)
3510 bad_module ("Expected integer string");
3512 mpz_init (*integer);
3513 if (mpz_set_str (*integer, atom_string, 10))
3514 bad_module ("Error converting integer");
3516 free (atom_string);
3518 else
3520 p = mpz_get_str (NULL, 10, *integer);
3521 write_atom (ATOM_STRING, p);
3522 free (p);
3527 static void
3528 mio_gmp_real (mpfr_t *real)
3530 mpfr_exp_t exponent;
3531 char *p;
3533 if (iomode == IO_INPUT)
3535 if (parse_atom () != ATOM_STRING)
3536 bad_module ("Expected real string");
3538 mpfr_init (*real);
3539 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3540 free (atom_string);
3542 else
3544 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3546 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3548 write_atom (ATOM_STRING, p);
3549 free (p);
3550 return;
3553 atom_string = XCNEWVEC (char, strlen (p) + 20);
3555 sprintf (atom_string, "0.%s@%ld", p, exponent);
3557 /* Fix negative numbers. */
3558 if (atom_string[2] == '-')
3560 atom_string[0] = '-';
3561 atom_string[1] = '0';
3562 atom_string[2] = '.';
3565 write_atom (ATOM_STRING, atom_string);
3567 free (atom_string);
3568 free (p);
3573 /* Save and restore the shape of an array constructor. */
3575 static void
3576 mio_shape (mpz_t **pshape, int rank)
3578 mpz_t *shape;
3579 atom_type t;
3580 int n;
3582 /* A NULL shape is represented by (). */
3583 mio_lparen ();
3585 if (iomode == IO_OUTPUT)
3587 shape = *pshape;
3588 if (!shape)
3590 mio_rparen ();
3591 return;
3594 else
3596 t = peek_atom ();
3597 if (t == ATOM_RPAREN)
3599 *pshape = NULL;
3600 mio_rparen ();
3601 return;
3604 shape = gfc_get_shape (rank);
3605 *pshape = shape;
3608 for (n = 0; n < rank; n++)
3609 mio_gmp_integer (&shape[n]);
3611 mio_rparen ();
3615 static const mstring expr_types[] = {
3616 minit ("OP", EXPR_OP),
3617 minit ("FUNCTION", EXPR_FUNCTION),
3618 minit ("CONSTANT", EXPR_CONSTANT),
3619 minit ("VARIABLE", EXPR_VARIABLE),
3620 minit ("SUBSTRING", EXPR_SUBSTRING),
3621 minit ("STRUCTURE", EXPR_STRUCTURE),
3622 minit ("ARRAY", EXPR_ARRAY),
3623 minit ("NULL", EXPR_NULL),
3624 minit ("COMPCALL", EXPR_COMPCALL),
3625 minit (NULL, -1)
3628 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3629 generic operators, not in expressions. INTRINSIC_USER is also
3630 replaced by the correct function name by the time we see it. */
3632 static const mstring intrinsics[] =
3634 minit ("UPLUS", INTRINSIC_UPLUS),
3635 minit ("UMINUS", INTRINSIC_UMINUS),
3636 minit ("PLUS", INTRINSIC_PLUS),
3637 minit ("MINUS", INTRINSIC_MINUS),
3638 minit ("TIMES", INTRINSIC_TIMES),
3639 minit ("DIVIDE", INTRINSIC_DIVIDE),
3640 minit ("POWER", INTRINSIC_POWER),
3641 minit ("CONCAT", INTRINSIC_CONCAT),
3642 minit ("AND", INTRINSIC_AND),
3643 minit ("OR", INTRINSIC_OR),
3644 minit ("EQV", INTRINSIC_EQV),
3645 minit ("NEQV", INTRINSIC_NEQV),
3646 minit ("EQ_SIGN", INTRINSIC_EQ),
3647 minit ("EQ", INTRINSIC_EQ_OS),
3648 minit ("NE_SIGN", INTRINSIC_NE),
3649 minit ("NE", INTRINSIC_NE_OS),
3650 minit ("GT_SIGN", INTRINSIC_GT),
3651 minit ("GT", INTRINSIC_GT_OS),
3652 minit ("GE_SIGN", INTRINSIC_GE),
3653 minit ("GE", INTRINSIC_GE_OS),
3654 minit ("LT_SIGN", INTRINSIC_LT),
3655 minit ("LT", INTRINSIC_LT_OS),
3656 minit ("LE_SIGN", INTRINSIC_LE),
3657 minit ("LE", INTRINSIC_LE_OS),
3658 minit ("NOT", INTRINSIC_NOT),
3659 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3660 minit ("USER", INTRINSIC_USER),
3661 minit (NULL, -1)
3665 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3667 static void
3668 fix_mio_expr (gfc_expr *e)
3670 gfc_symtree *ns_st = NULL;
3671 const char *fname;
3673 if (iomode != IO_OUTPUT)
3674 return;
3676 if (e->symtree)
3678 /* If this is a symtree for a symbol that came from a contained module
3679 namespace, it has a unique name and we should look in the current
3680 namespace to see if the required, non-contained symbol is available
3681 yet. If so, the latter should be written. */
3682 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3684 const char *name = e->symtree->n.sym->name;
3685 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3686 name = gfc_dt_upper_string (name);
3687 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3690 /* On the other hand, if the existing symbol is the module name or the
3691 new symbol is a dummy argument, do not do the promotion. */
3692 if (ns_st && ns_st->n.sym
3693 && ns_st->n.sym->attr.flavor != FL_MODULE
3694 && !e->symtree->n.sym->attr.dummy)
3695 e->symtree = ns_st;
3697 else if (e->expr_type == EXPR_FUNCTION
3698 && (e->value.function.name || e->value.function.isym))
3700 gfc_symbol *sym;
3702 /* In some circumstances, a function used in an initialization
3703 expression, in one use associated module, can fail to be
3704 coupled to its symtree when used in a specification
3705 expression in another module. */
3706 fname = e->value.function.esym ? e->value.function.esym->name
3707 : e->value.function.isym->name;
3708 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3710 if (e->symtree)
3711 return;
3713 /* This is probably a reference to a private procedure from another
3714 module. To prevent a segfault, make a generic with no specific
3715 instances. If this module is used, without the required
3716 specific coming from somewhere, the appropriate error message
3717 is issued. */
3718 gfc_get_symbol (fname, gfc_current_ns, &sym);
3719 sym->attr.flavor = FL_PROCEDURE;
3720 sym->attr.generic = 1;
3721 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3722 gfc_commit_symbol (sym);
3727 /* Read and write expressions. The form "()" is allowed to indicate a
3728 NULL expression. */
3730 static void
3731 mio_expr (gfc_expr **ep)
3733 HOST_WIDE_INT hwi;
3734 gfc_expr *e;
3735 atom_type t;
3736 int flag;
3738 mio_lparen ();
3740 if (iomode == IO_OUTPUT)
3742 if (*ep == NULL)
3744 mio_rparen ();
3745 return;
3748 e = *ep;
3749 MIO_NAME (expr_t) (e->expr_type, expr_types);
3751 else
3753 t = parse_atom ();
3754 if (t == ATOM_RPAREN)
3756 *ep = NULL;
3757 return;
3760 if (t != ATOM_NAME)
3761 bad_module ("Expected expression type");
3763 e = *ep = gfc_get_expr ();
3764 e->where = gfc_current_locus;
3765 e->expr_type = (expr_t) find_enum (expr_types);
3768 mio_typespec (&e->ts);
3769 mio_integer (&e->rank);
3771 fix_mio_expr (e);
3773 switch (e->expr_type)
3775 case EXPR_OP:
3776 e->value.op.op
3777 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3779 switch (e->value.op.op)
3781 case INTRINSIC_UPLUS:
3782 case INTRINSIC_UMINUS:
3783 case INTRINSIC_NOT:
3784 case INTRINSIC_PARENTHESES:
3785 mio_expr (&e->value.op.op1);
3786 break;
3788 case INTRINSIC_PLUS:
3789 case INTRINSIC_MINUS:
3790 case INTRINSIC_TIMES:
3791 case INTRINSIC_DIVIDE:
3792 case INTRINSIC_POWER:
3793 case INTRINSIC_CONCAT:
3794 case INTRINSIC_AND:
3795 case INTRINSIC_OR:
3796 case INTRINSIC_EQV:
3797 case INTRINSIC_NEQV:
3798 case INTRINSIC_EQ:
3799 case INTRINSIC_EQ_OS:
3800 case INTRINSIC_NE:
3801 case INTRINSIC_NE_OS:
3802 case INTRINSIC_GT:
3803 case INTRINSIC_GT_OS:
3804 case INTRINSIC_GE:
3805 case INTRINSIC_GE_OS:
3806 case INTRINSIC_LT:
3807 case INTRINSIC_LT_OS:
3808 case INTRINSIC_LE:
3809 case INTRINSIC_LE_OS:
3810 mio_expr (&e->value.op.op1);
3811 mio_expr (&e->value.op.op2);
3812 break;
3814 case INTRINSIC_USER:
3815 /* INTRINSIC_USER should not appear in resolved expressions,
3816 though for UDRs we need to stream unresolved ones. */
3817 if (iomode == IO_OUTPUT)
3818 write_atom (ATOM_STRING, e->value.op.uop->name);
3819 else
3821 char *name = read_string ();
3822 const char *uop_name = find_use_name (name, true);
3823 if (uop_name == NULL)
3825 size_t len = strlen (name);
3826 char *name2 = XCNEWVEC (char, len + 2);
3827 memcpy (name2, name, len);
3828 name2[len] = ' ';
3829 name2[len + 1] = '\0';
3830 free (name);
3831 uop_name = name = name2;
3833 e->value.op.uop = gfc_get_uop (uop_name);
3834 free (name);
3836 mio_expr (&e->value.op.op1);
3837 mio_expr (&e->value.op.op2);
3838 break;
3840 default:
3841 bad_module ("Bad operator");
3844 break;
3846 case EXPR_FUNCTION:
3847 mio_symtree_ref (&e->symtree);
3848 mio_actual_arglist (&e->value.function.actual, false);
3850 if (iomode == IO_OUTPUT)
3852 e->value.function.name
3853 = mio_allocated_string (e->value.function.name);
3854 if (e->value.function.esym)
3855 flag = 1;
3856 else if (e->ref)
3857 flag = 2;
3858 else if (e->value.function.isym == NULL)
3859 flag = 3;
3860 else
3861 flag = 0;
3862 mio_integer (&flag);
3863 switch (flag)
3865 case 1:
3866 mio_symbol_ref (&e->value.function.esym);
3867 break;
3868 case 2:
3869 mio_ref_list (&e->ref);
3870 break;
3871 case 3:
3872 break;
3873 default:
3874 write_atom (ATOM_STRING, e->value.function.isym->name);
3877 else
3879 require_atom (ATOM_STRING);
3880 if (atom_string[0] == '\0')
3881 e->value.function.name = NULL;
3882 else
3883 e->value.function.name = gfc_get_string ("%s", atom_string);
3884 free (atom_string);
3886 mio_integer (&flag);
3887 switch (flag)
3889 case 1:
3890 mio_symbol_ref (&e->value.function.esym);
3891 break;
3892 case 2:
3893 mio_ref_list (&e->ref);
3894 break;
3895 case 3:
3896 break;
3897 default:
3898 require_atom (ATOM_STRING);
3899 e->value.function.isym = gfc_find_function (atom_string);
3900 free (atom_string);
3904 break;
3906 case EXPR_VARIABLE:
3907 mio_symtree_ref (&e->symtree);
3908 mio_ref_list (&e->ref);
3909 break;
3911 case EXPR_SUBSTRING:
3912 e->value.character.string
3913 = CONST_CAST (gfc_char_t *,
3914 mio_allocated_wide_string (e->value.character.string,
3915 e->value.character.length));
3916 mio_ref_list (&e->ref);
3917 break;
3919 case EXPR_STRUCTURE:
3920 case EXPR_ARRAY:
3921 mio_constructor (&e->value.constructor);
3922 mio_shape (&e->shape, e->rank);
3923 break;
3925 case EXPR_CONSTANT:
3926 switch (e->ts.type)
3928 case BT_INTEGER:
3929 case BT_UNSIGNED:
3930 mio_gmp_integer (&e->value.integer);
3931 break;
3933 case BT_REAL:
3934 gfc_set_model_kind (e->ts.kind);
3935 mio_gmp_real (&e->value.real);
3936 break;
3938 case BT_COMPLEX:
3939 gfc_set_model_kind (e->ts.kind);
3940 mio_gmp_real (&mpc_realref (e->value.complex));
3941 mio_gmp_real (&mpc_imagref (e->value.complex));
3942 break;
3944 case BT_LOGICAL:
3945 mio_integer (&e->value.logical);
3946 break;
3948 case BT_CHARACTER:
3949 hwi = e->value.character.length;
3950 mio_hwi (&hwi);
3951 e->value.character.length = hwi;
3952 e->value.character.string
3953 = CONST_CAST (gfc_char_t *,
3954 mio_allocated_wide_string (e->value.character.string,
3955 e->value.character.length));
3956 break;
3958 default:
3959 bad_module ("Bad type in constant expression");
3962 break;
3964 case EXPR_NULL:
3965 break;
3967 case EXPR_COMPCALL:
3968 case EXPR_PPC:
3969 case EXPR_UNKNOWN:
3970 gcc_unreachable ();
3971 break;
3974 /* PDT types store the expression specification list here. */
3975 mio_actual_arglist (&e->param_list, true);
3977 mio_rparen ();
3981 /* Read and write namelists. */
3983 static void
3984 mio_namelist (gfc_symbol *sym)
3986 gfc_namelist *n, *m;
3988 mio_lparen ();
3990 if (iomode == IO_OUTPUT)
3992 for (n = sym->namelist; n; n = n->next)
3993 mio_symbol_ref (&n->sym);
3995 else
3997 m = NULL;
3998 while (peek_atom () != ATOM_RPAREN)
4000 n = gfc_get_namelist ();
4001 mio_symbol_ref (&n->sym);
4003 if (sym->namelist == NULL)
4004 sym->namelist = n;
4005 else
4006 m->next = n;
4008 m = n;
4010 sym->namelist_tail = m;
4013 mio_rparen ();
4017 /* Save/restore lists of gfc_interface structures. When loading an
4018 interface, we are really appending to the existing list of
4019 interfaces. Checking for duplicate and ambiguous interfaces has to
4020 be done later when all symbols have been loaded. */
4022 pointer_info *
4023 mio_interface_rest (gfc_interface **ip)
4025 gfc_interface *tail, *p;
4026 pointer_info *pi = NULL;
4028 if (iomode == IO_OUTPUT)
4030 if (ip != NULL)
4031 for (p = *ip; p; p = p->next)
4032 mio_symbol_ref (&p->sym);
4034 else
4036 if (*ip == NULL)
4037 tail = NULL;
4038 else
4040 tail = *ip;
4041 while (tail->next)
4042 tail = tail->next;
4045 for (;;)
4047 if (peek_atom () == ATOM_RPAREN)
4048 break;
4050 p = gfc_get_interface ();
4051 p->where = gfc_current_locus;
4052 pi = mio_symbol_ref (&p->sym);
4054 if (tail == NULL)
4055 *ip = p;
4056 else
4057 tail->next = p;
4059 tail = p;
4063 mio_rparen ();
4064 return pi;
4068 /* Save/restore a nameless operator interface. */
4070 static void
4071 mio_interface (gfc_interface **ip)
4073 mio_lparen ();
4074 mio_interface_rest (ip);
4078 /* Save/restore a named operator interface. */
4080 static void
4081 mio_symbol_interface (const char **name, const char **module,
4082 gfc_interface **ip)
4084 mio_lparen ();
4085 mio_pool_string (name);
4086 mio_pool_string (module);
4087 mio_interface_rest (ip);
4091 static void
4092 mio_namespace_ref (gfc_namespace **nsp)
4094 gfc_namespace *ns;
4095 pointer_info *p;
4097 p = mio_pointer_ref (nsp);
4099 if (p->type == P_UNKNOWN)
4100 p->type = P_NAMESPACE;
4102 if (iomode == IO_INPUT && p->integer != 0)
4104 ns = (gfc_namespace *) p->u.pointer;
4105 if (ns == NULL)
4107 ns = gfc_get_namespace (NULL, 0);
4108 associate_integer_pointer (p, ns);
4110 else
4111 ns->refs++;
4116 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
4118 static gfc_namespace* current_f2k_derived;
4120 static void
4121 mio_typebound_proc (gfc_typebound_proc** proc)
4123 int flag;
4124 int overriding_flag;
4126 if (iomode == IO_INPUT)
4128 *proc = gfc_get_typebound_proc (NULL);
4129 (*proc)->where = gfc_current_locus;
4131 gcc_assert (*proc);
4133 mio_lparen ();
4135 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
4137 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
4138 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4139 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
4140 overriding_flag = mio_name (overriding_flag, binding_overriding);
4141 (*proc)->deferred = ((overriding_flag & 2) != 0);
4142 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
4143 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4145 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
4146 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
4147 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
4149 mio_pool_string (&((*proc)->pass_arg));
4151 flag = (int) (*proc)->pass_arg_num;
4152 mio_integer (&flag);
4153 (*proc)->pass_arg_num = (unsigned) flag;
4155 if ((*proc)->is_generic)
4157 gfc_tbp_generic* g;
4158 int iop;
4160 mio_lparen ();
4162 if (iomode == IO_OUTPUT)
4163 for (g = (*proc)->u.generic; g; g = g->next)
4165 iop = (int) g->is_operator;
4166 mio_integer (&iop);
4167 mio_allocated_string (g->specific_st->name);
4169 else
4171 (*proc)->u.generic = NULL;
4172 while (peek_atom () != ATOM_RPAREN)
4174 gfc_symtree** sym_root;
4176 g = gfc_get_tbp_generic ();
4177 g->specific = NULL;
4179 mio_integer (&iop);
4180 g->is_operator = (bool) iop;
4182 require_atom (ATOM_STRING);
4183 sym_root = &current_f2k_derived->tb_sym_root;
4184 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
4185 free (atom_string);
4187 g->next = (*proc)->u.generic;
4188 (*proc)->u.generic = g;
4192 mio_rparen ();
4194 else if (!(*proc)->ppc)
4195 mio_symtree_ref (&(*proc)->u.specific);
4197 mio_rparen ();
4200 /* Walker-callback function for this purpose. */
4201 static void
4202 mio_typebound_symtree (gfc_symtree* st)
4204 if (iomode == IO_OUTPUT && !st->n.tb)
4205 return;
4207 if (iomode == IO_OUTPUT)
4209 mio_lparen ();
4210 mio_allocated_string (st->name);
4212 /* For IO_INPUT, the above is done in mio_f2k_derived. */
4214 mio_typebound_proc (&st->n.tb);
4215 mio_rparen ();
4218 /* IO a full symtree (in all depth). */
4219 static void
4220 mio_full_typebound_tree (gfc_symtree** root)
4222 mio_lparen ();
4224 if (iomode == IO_OUTPUT)
4225 gfc_traverse_symtree (*root, &mio_typebound_symtree);
4226 else
4228 while (peek_atom () == ATOM_LPAREN)
4230 gfc_symtree* st;
4232 mio_lparen ();
4234 require_atom (ATOM_STRING);
4235 st = gfc_get_tbp_symtree (root, atom_string);
4236 free (atom_string);
4238 mio_typebound_symtree (st);
4242 mio_rparen ();
4245 static void
4246 mio_finalizer (gfc_finalizer **f)
4248 if (iomode == IO_OUTPUT)
4250 gcc_assert (*f);
4251 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
4252 mio_symtree_ref (&(*f)->proc_tree);
4254 else
4256 *f = gfc_get_finalizer ();
4257 (*f)->where = gfc_current_locus; /* Value should not matter. */
4258 (*f)->next = NULL;
4260 mio_symtree_ref (&(*f)->proc_tree);
4261 (*f)->proc_sym = NULL;
4265 static void
4266 mio_f2k_derived (gfc_namespace *f2k)
4268 current_f2k_derived = f2k;
4270 /* Handle the list of finalizer procedures. */
4271 mio_lparen ();
4272 if (iomode == IO_OUTPUT)
4274 gfc_finalizer *f;
4275 for (f = f2k->finalizers; f; f = f->next)
4276 mio_finalizer (&f);
4278 else
4280 f2k->finalizers = NULL;
4281 while (peek_atom () != ATOM_RPAREN)
4283 gfc_finalizer *cur = NULL;
4284 mio_finalizer (&cur);
4285 cur->next = f2k->finalizers;
4286 f2k->finalizers = cur;
4289 mio_rparen ();
4291 /* Handle type-bound procedures. */
4292 mio_full_typebound_tree (&f2k->tb_sym_root);
4294 /* Type-bound user operators. */
4295 mio_full_typebound_tree (&f2k->tb_uop_root);
4297 /* Type-bound intrinsic operators. */
4298 mio_lparen ();
4299 if (iomode == IO_OUTPUT)
4301 int op;
4302 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
4304 gfc_intrinsic_op realop;
4306 if (op == INTRINSIC_USER || !f2k->tb_op[op])
4307 continue;
4309 mio_lparen ();
4310 realop = (gfc_intrinsic_op) op;
4311 mio_intrinsic_op (&realop);
4312 mio_typebound_proc (&f2k->tb_op[op]);
4313 mio_rparen ();
4316 else
4317 while (peek_atom () != ATOM_RPAREN)
4319 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
4321 mio_lparen ();
4322 mio_intrinsic_op (&op);
4323 mio_typebound_proc (&f2k->tb_op[op]);
4324 mio_rparen ();
4326 mio_rparen ();
4329 static void
4330 mio_full_f2k_derived (gfc_symbol *sym)
4332 mio_lparen ();
4334 if (iomode == IO_OUTPUT)
4336 if (sym->f2k_derived)
4337 mio_f2k_derived (sym->f2k_derived);
4339 else
4341 if (peek_atom () != ATOM_RPAREN)
4343 gfc_namespace *ns;
4345 sym->f2k_derived = gfc_get_namespace (NULL, 0);
4347 /* PDT templates make use of the mechanisms for formal args
4348 and so the parameter symbols are stored in the formal
4349 namespace. Transfer the sym_root to f2k_derived and then
4350 free the formal namespace since it is uneeded. */
4351 if (sym->attr.pdt_template && sym->formal && sym->formal->sym)
4353 ns = sym->formal->sym->ns;
4354 sym->f2k_derived->sym_root = ns->sym_root;
4355 ns->sym_root = NULL;
4356 ns->refs++;
4357 gfc_free_namespace (ns);
4358 ns = NULL;
4361 mio_f2k_derived (sym->f2k_derived);
4363 else
4364 gcc_assert (!sym->f2k_derived);
4367 mio_rparen ();
4370 static const mstring omp_declare_simd_clauses[] =
4372 minit ("INBRANCH", 0),
4373 minit ("NOTINBRANCH", 1),
4374 minit ("SIMDLEN", 2),
4375 minit ("UNIFORM", 3),
4376 minit ("LINEAR", 4),
4377 minit ("ALIGNED", 5),
4378 minit ("LINEAR_REF", 33),
4379 minit ("LINEAR_VAL", 34),
4380 minit ("LINEAR_UVAL", 35),
4381 minit (NULL, -1)
4384 /* Handle !$omp declare simd. */
4386 static void
4387 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4389 if (iomode == IO_OUTPUT)
4391 if (*odsp == NULL)
4392 return;
4394 else if (peek_atom () != ATOM_LPAREN)
4395 return;
4397 gfc_omp_declare_simd *ods = *odsp;
4399 mio_lparen ();
4400 if (iomode == IO_OUTPUT)
4402 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4403 if (ods->clauses)
4405 gfc_omp_namelist *n;
4407 if (ods->clauses->inbranch)
4408 mio_name (0, omp_declare_simd_clauses);
4409 if (ods->clauses->notinbranch)
4410 mio_name (1, omp_declare_simd_clauses);
4411 if (ods->clauses->simdlen_expr)
4413 mio_name (2, omp_declare_simd_clauses);
4414 mio_expr (&ods->clauses->simdlen_expr);
4416 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4418 mio_name (3, omp_declare_simd_clauses);
4419 mio_symbol_ref (&n->sym);
4421 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4423 if (n->u.linear.op == OMP_LINEAR_DEFAULT)
4424 mio_name (4, omp_declare_simd_clauses);
4425 else
4426 mio_name (32 + n->u.linear.op, omp_declare_simd_clauses);
4427 mio_symbol_ref (&n->sym);
4428 mio_expr (&n->expr);
4430 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4432 mio_name (5, omp_declare_simd_clauses);
4433 mio_symbol_ref (&n->sym);
4434 mio_expr (&n->expr);
4438 else
4440 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4442 require_atom (ATOM_NAME);
4443 *odsp = ods = gfc_get_omp_declare_simd ();
4444 ods->where = gfc_current_locus;
4445 ods->proc_name = ns->proc_name;
4446 if (peek_atom () == ATOM_NAME)
4448 ods->clauses = gfc_get_omp_clauses ();
4449 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4450 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4451 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4453 while (peek_atom () == ATOM_NAME)
4455 gfc_omp_namelist *n;
4456 int t = mio_name (0, omp_declare_simd_clauses);
4458 switch (t)
4460 case 0: ods->clauses->inbranch = true; break;
4461 case 1: ods->clauses->notinbranch = true; break;
4462 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4463 case 3:
4464 case 4:
4465 case 5:
4466 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4467 finish_namelist:
4468 n->where = gfc_current_locus;
4469 ptrs[t - 3] = &n->next;
4470 mio_symbol_ref (&n->sym);
4471 if (t != 3)
4472 mio_expr (&n->expr);
4473 break;
4474 case 33:
4475 case 34:
4476 case 35:
4477 *ptrs[1] = n = gfc_get_omp_namelist ();
4478 n->u.linear.op = (enum gfc_omp_linear_op) (t - 32);
4479 t = 4;
4480 goto finish_namelist;
4485 mio_omp_declare_simd (ns, &ods->next);
4487 mio_rparen ();
4491 static const mstring omp_declare_reduction_stmt[] =
4493 minit ("ASSIGN", 0),
4494 minit ("CALL", 1),
4495 minit (NULL, -1)
4499 static void
4500 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4501 gfc_namespace *ns, bool is_initializer)
4503 if (iomode == IO_OUTPUT)
4505 if ((*sym1)->module == NULL)
4507 (*sym1)->module = module_name;
4508 (*sym2)->module = module_name;
4510 mio_symbol_ref (sym1);
4511 mio_symbol_ref (sym2);
4512 if (ns->code->op == EXEC_ASSIGN)
4514 mio_name (0, omp_declare_reduction_stmt);
4515 mio_expr (&ns->code->expr1);
4516 mio_expr (&ns->code->expr2);
4518 else
4520 int flag;
4521 mio_name (1, omp_declare_reduction_stmt);
4522 mio_symtree_ref (&ns->code->symtree);
4523 mio_actual_arglist (&ns->code->ext.actual, false);
4525 flag = ns->code->resolved_isym != NULL;
4526 mio_integer (&flag);
4527 if (flag)
4528 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4529 else
4530 mio_symbol_ref (&ns->code->resolved_sym);
4533 else
4535 pointer_info *p1 = mio_symbol_ref (sym1);
4536 pointer_info *p2 = mio_symbol_ref (sym2);
4537 gfc_symbol *sym;
4538 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4539 gcc_assert (p1->u.rsym.sym == NULL);
4540 /* Add hidden symbols to the symtree. */
4541 pointer_info *q = get_integer (p1->u.rsym.ns);
4542 q->u.pointer = (void *) ns;
4543 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4544 sym->ts = udr->ts;
4545 sym->module = gfc_get_string ("%s", p1->u.rsym.module);
4546 associate_integer_pointer (p1, sym);
4547 sym->attr.omp_udr_artificial_var = 1;
4548 gcc_assert (p2->u.rsym.sym == NULL);
4549 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4550 sym->ts = udr->ts;
4551 sym->module = gfc_get_string ("%s", p2->u.rsym.module);
4552 associate_integer_pointer (p2, sym);
4553 sym->attr.omp_udr_artificial_var = 1;
4554 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4556 ns->code = gfc_get_code (EXEC_ASSIGN);
4557 mio_expr (&ns->code->expr1);
4558 mio_expr (&ns->code->expr2);
4560 else
4562 int flag;
4563 ns->code = gfc_get_code (EXEC_CALL);
4564 mio_symtree_ref (&ns->code->symtree);
4565 mio_actual_arglist (&ns->code->ext.actual, false);
4567 mio_integer (&flag);
4568 if (flag)
4570 require_atom (ATOM_STRING);
4571 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4572 free (atom_string);
4574 else
4575 mio_symbol_ref (&ns->code->resolved_sym);
4577 ns->code->loc = gfc_current_locus;
4578 ns->omp_udr_ns = 1;
4583 /* Unlike most other routines, the address of the symbol node is already
4584 fixed on input and the name/module has already been filled in.
4585 If you update the symbol format here, don't forget to update read_module
4586 as well (look for "seek to the symbol's component list"). */
4588 static void
4589 mio_symbol (gfc_symbol *sym)
4591 int intmod = INTMOD_NONE;
4593 mio_lparen ();
4595 mio_symbol_attribute (&sym->attr);
4597 if (sym->attr.pdt_type)
4598 sym->name = gfc_dt_upper_string (sym->name);
4600 /* Note that components are always saved, even if they are supposed
4601 to be private. Component access is checked during searching. */
4602 mio_component_list (&sym->components, sym->attr.vtype);
4603 if (sym->components != NULL)
4604 sym->component_access
4605 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4607 mio_typespec (&sym->ts);
4608 if (sym->ts.type == BT_CLASS)
4609 sym->attr.class_ok = 1;
4611 if (iomode == IO_OUTPUT)
4612 mio_namespace_ref (&sym->formal_ns);
4613 else
4615 mio_namespace_ref (&sym->formal_ns);
4616 if (sym->formal_ns)
4617 sym->formal_ns->proc_name = sym;
4620 /* Save/restore common block links. */
4621 mio_symbol_ref (&sym->common_next);
4623 mio_formal_arglist (&sym->formal);
4625 if (sym->attr.flavor == FL_PARAMETER)
4626 mio_expr (&sym->value);
4628 mio_array_spec (&sym->as);
4630 mio_symbol_ref (&sym->result);
4632 if (sym->attr.cray_pointee)
4633 mio_symbol_ref (&sym->cp_pointer);
4635 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4636 mio_full_f2k_derived (sym);
4638 /* PDT types store the symbol specification list here. */
4639 mio_actual_arglist (&sym->param_list, true);
4641 mio_namelist (sym);
4643 /* Add the fields that say whether this is from an intrinsic module,
4644 and if so, what symbol it is within the module. */
4645 /* mio_integer (&(sym->from_intmod)); */
4646 if (iomode == IO_OUTPUT)
4648 intmod = sym->from_intmod;
4649 mio_integer (&intmod);
4651 else
4653 mio_integer (&intmod);
4654 if (current_intmod)
4655 sym->from_intmod = current_intmod;
4656 else
4657 sym->from_intmod = (intmod_id) intmod;
4660 mio_integer (&(sym->intmod_sym_id));
4662 if (gfc_fl_struct (sym->attr.flavor))
4663 mio_integer (&(sym->hash_value));
4665 if (sym->formal_ns
4666 && sym->formal_ns->proc_name == sym
4667 && sym->formal_ns->entries == NULL)
4668 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4670 mio_rparen ();
4674 /************************* Top level subroutines *************************/
4676 /* A recursive function to look for a specific symbol by name and by
4677 module. Whilst several symtrees might point to one symbol, its
4678 is sufficient for the purposes here than one exist. Note that
4679 generic interfaces are distinguished as are symbols that have been
4680 renamed in another module. */
4681 static gfc_symtree *
4682 find_symbol (gfc_symtree *st, const char *name,
4683 const char *module, int generic)
4685 int c;
4686 gfc_symtree *retval, *s;
4688 if (st == NULL || st->n.sym == NULL)
4689 return NULL;
4691 c = strcmp (name, st->n.sym->name);
4692 if (c == 0 && st->n.sym->module
4693 && strcmp (module, st->n.sym->module) == 0
4694 && !check_unique_name (st->name))
4696 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4698 /* Detect symbols that are renamed by use association in another
4699 module by the absence of a symtree and null attr.use_rename,
4700 since the latter is not transmitted in the module file. */
4701 if (((!generic && !st->n.sym->attr.generic)
4702 || (generic && st->n.sym->attr.generic))
4703 && !(s == NULL && !st->n.sym->attr.use_rename))
4704 return st;
4707 retval = find_symbol (st->left, name, module, generic);
4709 if (retval == NULL)
4710 retval = find_symbol (st->right, name, module, generic);
4712 return retval;
4716 /* Skip a list between balanced left and right parens.
4717 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4718 have been already parsed by hand, and the remaining of the content is to be
4719 skipped here. The default value is 0 (balanced parens). */
4721 static void
4722 skip_list (int nest_level = 0)
4724 int level;
4726 level = nest_level;
4729 switch (parse_atom ())
4731 case ATOM_LPAREN:
4732 level++;
4733 break;
4735 case ATOM_RPAREN:
4736 level--;
4737 break;
4739 case ATOM_STRING:
4740 free (atom_string);
4741 break;
4743 case ATOM_NAME:
4744 case ATOM_INTEGER:
4745 break;
4748 while (level > 0);
4752 /* Load operator interfaces from the module. Interfaces are unusual
4753 in that they attach themselves to existing symbols. */
4755 static void
4756 load_operator_interfaces (void)
4758 const char *p;
4759 /* "module" must be large enough for the case of submodules in which the name
4760 has the form module.submodule */
4761 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
4762 gfc_user_op *uop;
4763 pointer_info *pi = NULL;
4764 int n, i;
4766 mio_lparen ();
4768 while (peek_atom () != ATOM_RPAREN)
4770 mio_lparen ();
4772 mio_internal_string (name);
4773 mio_internal_string (module);
4775 n = number_use_names (name, true);
4776 n = n ? n : 1;
4778 for (i = 1; i <= n; i++)
4780 /* Decide if we need to load this one or not. */
4781 p = find_use_name_n (name, &i, true);
4783 if (p == NULL)
4785 while (parse_atom () != ATOM_RPAREN);
4786 continue;
4789 if (i == 1)
4791 uop = gfc_get_uop (p);
4792 pi = mio_interface_rest (&uop->op);
4794 else
4796 if (gfc_find_uop (p, NULL))
4797 continue;
4798 uop = gfc_get_uop (p);
4799 uop->op = gfc_get_interface ();
4800 uop->op->where = gfc_current_locus;
4801 add_fixup (pi->integer, &uop->op->sym);
4806 mio_rparen ();
4810 /* Load interfaces from the module. Interfaces are unusual in that
4811 they attach themselves to existing symbols. */
4813 static void
4814 load_generic_interfaces (void)
4816 const char *p;
4817 /* "module" must be large enough for the case of submodules in which the name
4818 has the form module.submodule */
4819 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
4820 gfc_symbol *sym;
4821 gfc_interface *generic = NULL, *gen = NULL;
4822 int n, i, renamed;
4823 bool ambiguous_set = false;
4825 mio_lparen ();
4827 while (peek_atom () != ATOM_RPAREN)
4829 mio_lparen ();
4831 mio_internal_string (name);
4832 mio_internal_string (module);
4834 n = number_use_names (name, false);
4835 renamed = n ? 1 : 0;
4836 n = n ? n : 1;
4838 for (i = 1; i <= n; i++)
4840 gfc_symtree *st;
4841 /* Decide if we need to load this one or not. */
4842 p = find_use_name_n (name, &i, false);
4844 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4846 /* Skip the specific names for these cases. */
4847 while (i == 1 && parse_atom () != ATOM_RPAREN);
4849 continue;
4852 st = find_symbol (gfc_current_ns->sym_root,
4853 name, module_name, 1);
4855 /* If the symbol exists already and is being USEd without being
4856 in an ONLY clause, do not load a new symtree(11.3.2). */
4857 if (!only_flag && st)
4858 sym = st->n.sym;
4860 if (!sym)
4862 if (st)
4864 sym = st->n.sym;
4865 if (strcmp (st->name, p) != 0)
4867 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4868 st->n.sym = sym;
4869 sym->refs++;
4873 /* Since we haven't found a valid generic interface, we had
4874 better make one. */
4875 if (!sym)
4877 gfc_get_symbol (p, NULL, &sym);
4878 sym->name = gfc_get_string ("%s", name);
4879 sym->module = module_name;
4880 sym->attr.flavor = FL_PROCEDURE;
4881 sym->attr.generic = 1;
4882 sym->attr.use_assoc = 1;
4885 else
4887 /* Unless sym is a generic interface, this reference
4888 is ambiguous. */
4889 if (st == NULL)
4890 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4892 sym = st->n.sym;
4894 if (st && !sym->attr.generic
4895 && !st->ambiguous
4896 && sym->module
4897 && strcmp (module, sym->module))
4899 ambiguous_set = true;
4900 st->ambiguous = 1;
4904 sym->attr.use_only = only_flag;
4905 sym->attr.use_rename = renamed;
4907 if (i == 1)
4909 mio_interface_rest (&sym->generic);
4910 generic = sym->generic;
4912 else if (!sym->generic)
4914 sym->generic = generic;
4915 sym->attr.generic_copy = 1;
4918 /* If a procedure that is not generic has generic interfaces
4919 that include itself, it is generic! We need to take care
4920 to retain symbols ambiguous that were already so. */
4921 if (sym->attr.use_assoc
4922 && !sym->attr.generic
4923 && sym->attr.flavor == FL_PROCEDURE)
4925 for (gen = generic; gen; gen = gen->next)
4927 if (gen->sym == sym)
4929 sym->attr.generic = 1;
4930 if (ambiguous_set)
4931 st->ambiguous = 0;
4932 break;
4940 mio_rparen ();
4944 /* Load common blocks. */
4946 static void
4947 load_commons (void)
4949 char name[GFC_MAX_SYMBOL_LEN + 1];
4950 gfc_common_head *p;
4952 mio_lparen ();
4954 while (peek_atom () != ATOM_RPAREN)
4956 int flags = 0;
4957 char* label;
4958 mio_lparen ();
4959 mio_internal_string (name);
4961 p = gfc_get_common (name, 1);
4963 mio_symbol_ref (&p->head);
4964 mio_integer (&flags);
4965 if (flags & 1)
4966 p->saved = 1;
4967 if (flags & 2)
4968 p->threadprivate = 1;
4969 p->omp_device_type = (gfc_omp_device_type) ((flags >> 2) & 3);
4970 p->use_assoc = 1;
4972 /* Get whether this was a bind(c) common or not. */
4973 mio_integer (&p->is_bind_c);
4974 /* Get the binding label. */
4975 label = read_string ();
4976 if (strlen (label))
4977 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4978 XDELETEVEC (label);
4980 mio_rparen ();
4983 mio_rparen ();
4987 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4988 so that unused variables are not loaded and so that the expression can
4989 be safely freed. */
4991 static void
4992 load_equiv (void)
4994 gfc_equiv *head, *tail, *end, *eq, *equiv;
4995 bool duplicate;
4997 mio_lparen ();
4998 in_load_equiv = true;
5000 end = gfc_current_ns->equiv;
5001 while (end != NULL && end->next != NULL)
5002 end = end->next;
5004 while (peek_atom () != ATOM_RPAREN) {
5005 mio_lparen ();
5006 head = tail = NULL;
5008 while(peek_atom () != ATOM_RPAREN)
5010 if (head == NULL)
5011 head = tail = gfc_get_equiv ();
5012 else
5014 tail->eq = gfc_get_equiv ();
5015 tail = tail->eq;
5018 mio_pool_string (&tail->module);
5019 mio_expr (&tail->expr);
5022 /* Check for duplicate equivalences being loaded from different modules */
5023 duplicate = false;
5024 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
5026 if (equiv->module && head->module
5027 && strcmp (equiv->module, head->module) == 0)
5029 duplicate = true;
5030 break;
5034 if (duplicate)
5036 for (eq = head; eq; eq = head)
5038 head = eq->eq;
5039 gfc_free_expr (eq->expr);
5040 free (eq);
5044 if (end == NULL)
5045 gfc_current_ns->equiv = head;
5046 else
5047 end->next = head;
5049 if (head != NULL)
5050 end = head;
5052 mio_rparen ();
5055 mio_rparen ();
5056 in_load_equiv = false;
5060 /* This function loads OpenMP user defined reductions. */
5061 static void
5062 load_omp_udrs (void)
5064 mio_lparen ();
5065 while (peek_atom () != ATOM_RPAREN)
5067 const char *name = NULL, *newname;
5068 char *altname;
5069 gfc_typespec ts;
5070 gfc_symtree *st;
5071 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
5073 mio_lparen ();
5074 mio_pool_string (&name);
5075 gfc_clear_ts (&ts);
5076 mio_typespec (&ts);
5077 if (startswith (name, "operator "))
5079 const char *p = name + sizeof ("operator ") - 1;
5080 if (strcmp (p, "+") == 0)
5081 rop = OMP_REDUCTION_PLUS;
5082 else if (strcmp (p, "*") == 0)
5083 rop = OMP_REDUCTION_TIMES;
5084 else if (strcmp (p, "-") == 0)
5085 rop = OMP_REDUCTION_MINUS;
5086 else if (strcmp (p, ".and.") == 0)
5087 rop = OMP_REDUCTION_AND;
5088 else if (strcmp (p, ".or.") == 0)
5089 rop = OMP_REDUCTION_OR;
5090 else if (strcmp (p, ".eqv.") == 0)
5091 rop = OMP_REDUCTION_EQV;
5092 else if (strcmp (p, ".neqv.") == 0)
5093 rop = OMP_REDUCTION_NEQV;
5095 altname = NULL;
5096 if (rop == OMP_REDUCTION_USER && name[0] == '.')
5098 size_t len = strlen (name + 1);
5099 altname = XALLOCAVEC (char, len);
5100 gcc_assert (name[len] == '.');
5101 memcpy (altname, name + 1, len - 1);
5102 altname[len - 1] = '\0';
5104 newname = name;
5105 if (rop == OMP_REDUCTION_USER)
5106 newname = find_use_name (altname ? altname : name, !!altname);
5107 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
5108 newname = NULL;
5109 if (newname == NULL)
5111 skip_list (1);
5112 continue;
5114 if (altname && newname != altname)
5116 size_t len = strlen (newname);
5117 altname = XALLOCAVEC (char, len + 3);
5118 altname[0] = '.';
5119 memcpy (altname + 1, newname, len);
5120 altname[len + 1] = '.';
5121 altname[len + 2] = '\0';
5122 name = gfc_get_string ("%s", altname);
5124 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
5125 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
5126 if (udr)
5128 require_atom (ATOM_INTEGER);
5129 pointer_info *p = get_integer (atom_int);
5130 if (strcmp (p->u.rsym.module, udr->omp_out->module))
5132 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
5133 "module %s at %L",
5134 p->u.rsym.module, &gfc_current_locus);
5135 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
5136 "%s at %L",
5137 udr->omp_out->module, &udr->where);
5139 skip_list (1);
5140 continue;
5142 udr = gfc_get_omp_udr ();
5143 udr->name = name;
5144 udr->rop = rop;
5145 udr->ts = ts;
5146 udr->where = gfc_current_locus;
5147 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
5148 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
5149 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
5150 false);
5151 if (peek_atom () != ATOM_RPAREN)
5153 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
5154 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
5155 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5156 udr->initializer_ns, true);
5158 if (st)
5160 udr->next = st->n.omp_udr;
5161 st->n.omp_udr = udr;
5163 else
5165 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
5166 st->n.omp_udr = udr;
5168 mio_rparen ();
5170 mio_rparen ();
5174 /* Recursive function to traverse the pointer_info tree and load a
5175 needed symbol. We return nonzero if we load a symbol and stop the
5176 traversal, because the act of loading can alter the tree. */
5178 static int
5179 load_needed (pointer_info *p)
5181 gfc_namespace *ns;
5182 pointer_info *q;
5183 gfc_symbol *sym;
5184 int rv;
5186 rv = 0;
5187 if (p == NULL)
5188 return rv;
5190 rv |= load_needed (p->left);
5191 rv |= load_needed (p->right);
5193 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
5194 return rv;
5196 p->u.rsym.state = USED;
5198 set_module_locus (&p->u.rsym.where);
5200 sym = p->u.rsym.sym;
5201 if (sym == NULL)
5203 q = get_integer (p->u.rsym.ns);
5205 ns = (gfc_namespace *) q->u.pointer;
5206 if (ns == NULL)
5208 /* Create an interface namespace if necessary. These are
5209 the namespaces that hold the formal parameters of module
5210 procedures. */
5212 ns = gfc_get_namespace (NULL, 0);
5213 associate_integer_pointer (q, ns);
5216 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
5217 doesn't go pear-shaped if the symbol is used. */
5218 if (!ns->proc_name)
5219 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
5220 1, &ns->proc_name);
5222 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
5223 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
5224 sym->module = gfc_get_string ("%s", p->u.rsym.module);
5225 if (p->u.rsym.binding_label)
5226 sym->binding_label = IDENTIFIER_POINTER (get_identifier
5227 (p->u.rsym.binding_label));
5229 associate_integer_pointer (p, sym);
5232 mio_symbol (sym);
5233 sym->attr.use_assoc = 1;
5235 /* Unliked derived types, a STRUCTURE may share names with other symbols.
5236 We greedily converted the symbol name to lowercase before we knew its
5237 type, so now we must fix it. */
5238 if (sym->attr.flavor == FL_STRUCT)
5239 sym->name = gfc_dt_upper_string (sym->name);
5241 /* Mark as only or rename for later diagnosis for explicitly imported
5242 but not used warnings; don't mark internal symbols such as __vtab,
5243 __def_init etc. Only mark them if they have been explicitly loaded. */
5245 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
5247 gfc_use_rename *u;
5249 /* Search the use/rename list for the variable; if the variable is
5250 found, mark it. */
5251 for (u = gfc_rename_list; u; u = u->next)
5253 if (strcmp (u->use_name, sym->name) == 0)
5255 sym->attr.use_only = 1;
5256 break;
5261 if (p->u.rsym.renamed)
5262 sym->attr.use_rename = 1;
5264 return 1;
5268 /* Recursive function for cleaning up things after a module has been read. */
5270 static void
5271 read_cleanup (pointer_info *p)
5273 gfc_symtree *st;
5274 pointer_info *q;
5276 if (p == NULL)
5277 return;
5279 read_cleanup (p->left);
5280 read_cleanup (p->right);
5282 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
5284 gfc_namespace *ns;
5285 /* Add hidden symbols to the symtree. */
5286 q = get_integer (p->u.rsym.ns);
5287 ns = (gfc_namespace *) q->u.pointer;
5289 if (!p->u.rsym.sym->attr.vtype
5290 && !p->u.rsym.sym->attr.vtab)
5291 st = gfc_get_unique_symtree (ns);
5292 else
5294 /* There is no reason to use 'unique_symtrees' for vtabs or
5295 vtypes - their name is fine for a symtree and reduces the
5296 namespace pollution. */
5297 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
5298 if (!st)
5299 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
5302 st->n.sym = p->u.rsym.sym;
5303 st->n.sym->refs++;
5305 /* Fixup any symtree references. */
5306 p->u.rsym.symtree = st;
5307 resolve_fixups (p->u.rsym.stfixup, st);
5308 p->u.rsym.stfixup = NULL;
5311 /* Free unused symbols. */
5312 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
5313 gfc_free_symbol (p->u.rsym.sym);
5317 /* It is not quite enough to check for ambiguity in the symbols by
5318 the loaded symbol and the new symbol not being identical. */
5319 static bool
5320 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
5322 gfc_symbol *rsym;
5323 module_locus locus;
5324 symbol_attribute attr;
5325 gfc_symbol *st_sym;
5327 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
5329 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
5330 "current program unit", st->name, module_name);
5331 return true;
5334 st_sym = st->n.sym;
5335 rsym = info->u.rsym.sym;
5336 if (st_sym == rsym)
5337 return false;
5339 if (st_sym->attr.vtab || st_sym->attr.vtype)
5340 return false;
5342 /* If the existing symbol is generic from a different module and
5343 the new symbol is generic there can be no ambiguity. */
5344 if (st_sym->attr.generic
5345 && st_sym->module
5346 && st_sym->module != module_name)
5348 /* The new symbol's attributes have not yet been read. Since
5349 we need attr.generic, read it directly. */
5350 get_module_locus (&locus);
5351 set_module_locus (&info->u.rsym.where);
5352 mio_lparen ();
5353 attr.generic = 0;
5354 mio_symbol_attribute (&attr);
5355 set_module_locus (&locus);
5356 if (attr.generic)
5357 return false;
5360 return true;
5364 /* Read a module file. */
5366 static void
5367 read_module (void)
5369 module_locus operator_interfaces, user_operators, omp_udrs;
5370 const char *p;
5371 char name[GFC_MAX_SYMBOL_LEN + 1];
5372 int i;
5373 /* Workaround -Wmaybe-uninitialized false positive during
5374 profiledbootstrap by initializing them. */
5375 int ambiguous = 0, j, nuse, symbol = 0;
5376 pointer_info *info, *q;
5377 gfc_use_rename *u = NULL;
5378 gfc_symtree *st;
5379 gfc_symbol *sym;
5381 get_module_locus (&operator_interfaces); /* Skip these for now. */
5382 skip_list ();
5384 get_module_locus (&user_operators);
5385 skip_list ();
5386 skip_list ();
5388 /* Skip commons and equivalences for now. */
5389 skip_list ();
5390 skip_list ();
5392 /* Skip OpenMP UDRs. */
5393 get_module_locus (&omp_udrs);
5394 skip_list ();
5396 mio_lparen ();
5398 /* Create the fixup nodes for all the symbols. */
5400 while (peek_atom () != ATOM_RPAREN)
5402 char* bind_label;
5403 require_atom (ATOM_INTEGER);
5404 info = get_integer (atom_int);
5406 info->type = P_SYMBOL;
5407 info->u.rsym.state = UNUSED;
5409 info->u.rsym.true_name = read_string ();
5410 info->u.rsym.module = read_string ();
5411 bind_label = read_string ();
5412 if (strlen (bind_label))
5413 info->u.rsym.binding_label = bind_label;
5414 else
5415 XDELETEVEC (bind_label);
5417 require_atom (ATOM_INTEGER);
5418 info->u.rsym.ns = atom_int;
5420 get_module_locus (&info->u.rsym.where);
5422 /* See if the symbol has already been loaded by a previous module.
5423 If so, we reference the existing symbol and prevent it from
5424 being loaded again. This should not happen if the symbol being
5425 read is an index for an assumed shape dummy array (ns != 1). */
5427 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5429 if (sym == NULL
5430 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5432 skip_list ();
5433 continue;
5436 info->u.rsym.state = USED;
5437 info->u.rsym.sym = sym;
5438 /* The current symbol has already been loaded, so we can avoid loading
5439 it again. However, if it is a derived type, some of its components
5440 can be used in expressions in the module. To avoid the module loading
5441 failing, we need to associate the module's component pointer indexes
5442 with the existing symbol's component pointers. */
5443 if (gfc_fl_struct (sym->attr.flavor))
5445 gfc_component *c;
5447 /* First seek to the symbol's component list. */
5448 mio_lparen (); /* symbol opening. */
5449 skip_list (); /* skip symbol attribute. */
5451 mio_lparen (); /* component list opening. */
5452 for (c = sym->components; c; c = c->next)
5454 pointer_info *p;
5455 const char *comp_name = NULL;
5456 int n = 0;
5458 mio_lparen (); /* component opening. */
5459 mio_integer (&n);
5460 p = get_integer (n);
5461 if (p->u.pointer == NULL)
5462 associate_integer_pointer (p, c);
5463 mio_pool_string (&comp_name);
5464 if (comp_name != c->name)
5466 gfc_fatal_error ("Mismatch in components of derived type "
5467 "%qs from %qs at %C: expecting %qs, "
5468 "but got %qs", sym->name, sym->module,
5469 c->name, comp_name);
5471 skip_list (1); /* component end. */
5473 mio_rparen (); /* component list closing. */
5475 skip_list (1); /* symbol end. */
5477 else
5478 skip_list ();
5480 /* Some symbols do not have a namespace (eg. formal arguments),
5481 so the automatic "unique symtree" mechanism must be suppressed
5482 by marking them as referenced. */
5483 q = get_integer (info->u.rsym.ns);
5484 if (q->u.pointer == NULL)
5486 info->u.rsym.referenced = 1;
5487 continue;
5491 mio_rparen ();
5493 /* Parse the symtree lists. This lets us mark which symbols need to
5494 be loaded. Renaming is also done at this point by replacing the
5495 symtree name. */
5497 mio_lparen ();
5499 while (peek_atom () != ATOM_RPAREN)
5501 mio_internal_string (name);
5502 mio_integer (&ambiguous);
5503 mio_integer (&symbol);
5505 info = get_integer (symbol);
5507 /* See how many use names there are. If none, go through the start
5508 of the loop at least once. */
5509 nuse = number_use_names (name, false);
5510 info->u.rsym.renamed = nuse ? 1 : 0;
5512 if (nuse == 0)
5513 nuse = 1;
5515 for (j = 1; j <= nuse; j++)
5517 /* Get the jth local name for this symbol. */
5518 p = find_use_name_n (name, &j, false);
5520 if (p == NULL && strcmp (name, module_name) == 0)
5521 p = name;
5523 /* Exception: Always import vtabs & vtypes. */
5524 if (p == NULL && name[0] == '_'
5525 && (startswith (name, "__vtab_")
5526 || startswith (name, "__vtype_")))
5527 p = name;
5529 /* Skip symtree nodes not in an ONLY clause, unless there
5530 is an existing symtree loaded from another USE statement. */
5531 if (p == NULL)
5533 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5534 if (st != NULL
5535 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5536 && st->n.sym->module != NULL
5537 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5539 info->u.rsym.symtree = st;
5540 info->u.rsym.sym = st->n.sym;
5542 continue;
5545 /* If a symbol of the same name and module exists already,
5546 this symbol, which is not in an ONLY clause, must not be
5547 added to the namespace(11.3.2). Note that find_symbol
5548 only returns the first occurrence that it finds. */
5549 if (!only_flag && !info->u.rsym.renamed
5550 && strcmp (name, module_name) != 0
5551 && find_symbol (gfc_current_ns->sym_root, name,
5552 module_name, 0))
5553 continue;
5555 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5557 if (st != NULL
5558 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5560 /* Check for ambiguous symbols. */
5561 if (check_for_ambiguous (st, info))
5562 st->ambiguous = 1;
5563 else
5564 info->u.rsym.symtree = st;
5566 else
5568 if (st)
5570 /* This symbol is host associated from a module in a
5571 submodule. Hide it with a unique symtree. */
5572 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5573 s->n.sym = st->n.sym;
5574 st->n.sym = NULL;
5576 else
5578 /* Create a symtree node in the current namespace for this
5579 symbol. */
5580 st = check_unique_name (p)
5581 ? gfc_get_unique_symtree (gfc_current_ns)
5582 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5583 st->ambiguous = ambiguous;
5586 sym = info->u.rsym.sym;
5588 /* Create a symbol node if it doesn't already exist. */
5589 if (sym == NULL)
5591 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5592 gfc_current_ns);
5593 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5594 sym = info->u.rsym.sym;
5595 sym->module = gfc_get_string ("%s", info->u.rsym.module);
5597 if (info->u.rsym.binding_label)
5599 tree id = get_identifier (info->u.rsym.binding_label);
5600 sym->binding_label = IDENTIFIER_POINTER (id);
5604 st->n.sym = sym;
5605 st->n.sym->refs++;
5607 if (strcmp (name, p) != 0)
5608 sym->attr.use_rename = 1;
5610 if (name[0] != '_'
5611 || (!startswith (name, "__vtab_")
5612 && !startswith (name, "__vtype_")))
5613 sym->attr.use_only = only_flag;
5615 /* Store the symtree pointing to this symbol. */
5616 info->u.rsym.symtree = st;
5618 if (info->u.rsym.state == UNUSED)
5619 info->u.rsym.state = NEEDED;
5620 info->u.rsym.referenced = 1;
5625 mio_rparen ();
5627 /* Load intrinsic operator interfaces. */
5628 set_module_locus (&operator_interfaces);
5629 mio_lparen ();
5631 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5633 gfc_use_rename *u = NULL, *v = NULL;
5634 int j = i;
5636 if (i == INTRINSIC_USER)
5637 continue;
5639 if (only_flag)
5641 u = find_use_operator ((gfc_intrinsic_op) i);
5643 /* F2018:10.1.5.5.1 requires same interpretation of old and new-style
5644 relational operators. Special handling for USE, ONLY. */
5645 switch (i)
5647 case INTRINSIC_EQ:
5648 j = INTRINSIC_EQ_OS;
5649 break;
5650 case INTRINSIC_EQ_OS:
5651 j = INTRINSIC_EQ;
5652 break;
5653 case INTRINSIC_NE:
5654 j = INTRINSIC_NE_OS;
5655 break;
5656 case INTRINSIC_NE_OS:
5657 j = INTRINSIC_NE;
5658 break;
5659 case INTRINSIC_GT:
5660 j = INTRINSIC_GT_OS;
5661 break;
5662 case INTRINSIC_GT_OS:
5663 j = INTRINSIC_GT;
5664 break;
5665 case INTRINSIC_GE:
5666 j = INTRINSIC_GE_OS;
5667 break;
5668 case INTRINSIC_GE_OS:
5669 j = INTRINSIC_GE;
5670 break;
5671 case INTRINSIC_LT:
5672 j = INTRINSIC_LT_OS;
5673 break;
5674 case INTRINSIC_LT_OS:
5675 j = INTRINSIC_LT;
5676 break;
5677 case INTRINSIC_LE:
5678 j = INTRINSIC_LE_OS;
5679 break;
5680 case INTRINSIC_LE_OS:
5681 j = INTRINSIC_LE;
5682 break;
5683 default:
5684 break;
5687 if (j != i)
5688 v = find_use_operator ((gfc_intrinsic_op) j);
5690 if (u == NULL && v == NULL)
5692 skip_list ();
5693 continue;
5696 if (u)
5697 u->found = 1;
5698 if (v)
5699 v->found = 1;
5702 mio_interface (&gfc_current_ns->op[i]);
5703 if (!gfc_current_ns->op[i] && !gfc_current_ns->op[j])
5705 if (u)
5706 u->found = 0;
5707 if (v)
5708 v->found = 0;
5712 mio_rparen ();
5714 /* Load generic and user operator interfaces. These must follow the
5715 loading of symtree because otherwise symbols can be marked as
5716 ambiguous. */
5718 set_module_locus (&user_operators);
5720 load_operator_interfaces ();
5721 load_generic_interfaces ();
5723 load_commons ();
5724 load_equiv ();
5726 /* Load OpenMP user defined reductions. */
5727 set_module_locus (&omp_udrs);
5728 load_omp_udrs ();
5730 /* At this point, we read those symbols that are needed but haven't
5731 been loaded yet. If one symbol requires another, the other gets
5732 marked as NEEDED if its previous state was UNUSED. */
5734 while (load_needed (pi_root));
5736 /* Make sure all elements of the rename-list were found in the module. */
5738 for (u = gfc_rename_list; u; u = u->next)
5740 if (u->found)
5741 continue;
5743 if (u->op == INTRINSIC_NONE)
5745 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5746 u->use_name, &u->where, module_name);
5747 continue;
5750 if (u->op == INTRINSIC_USER)
5752 gfc_error ("User operator %qs referenced at %L not found "
5753 "in module %qs", u->use_name, &u->where, module_name);
5754 continue;
5757 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5758 "in module %qs", gfc_op2string (u->op), &u->where,
5759 module_name);
5762 /* Clean up symbol nodes that were never loaded, create references
5763 to hidden symbols. */
5765 read_cleanup (pi_root);
5769 /* Given an access type that is specific to an entity and the default
5770 access, return nonzero if the entity is publicly accessible. If the
5771 element is declared as PUBLIC, then it is public; if declared
5772 PRIVATE, then private, and otherwise it is public unless the default
5773 access in this context has been declared PRIVATE. */
5775 static bool dump_smod = false;
5777 static bool
5778 check_access (gfc_access specific_access, gfc_access default_access)
5780 if (dump_smod)
5781 return true;
5783 if (specific_access == ACCESS_PUBLIC)
5784 return true;
5785 if (specific_access == ACCESS_PRIVATE)
5786 return false;
5788 if (flag_module_private)
5789 return default_access == ACCESS_PUBLIC;
5790 else
5791 return default_access != ACCESS_PRIVATE;
5795 bool
5796 gfc_check_symbol_access (gfc_symbol *sym)
5798 if (sym->attr.vtab || sym->attr.vtype)
5799 return true;
5800 else
5801 return check_access (sym->attr.access, sym->ns->default_access);
5805 /* A structure to remember which commons we've already written. */
5807 struct written_common
5809 BBT_HEADER(written_common);
5810 const char *name, *label;
5813 static struct written_common *written_commons = NULL;
5815 /* Comparison function used for balancing the binary tree. */
5817 static int
5818 compare_written_commons (void *a1, void *b1)
5820 const char *aname = ((struct written_common *) a1)->name;
5821 const char *alabel = ((struct written_common *) a1)->label;
5822 const char *bname = ((struct written_common *) b1)->name;
5823 const char *blabel = ((struct written_common *) b1)->label;
5824 int c = strcmp (aname, bname);
5826 return (c != 0 ? c : strcmp (alabel, blabel));
5829 /* Free a list of written commons. */
5831 static void
5832 free_written_common (struct written_common *w)
5834 if (!w)
5835 return;
5837 if (w->left)
5838 free_written_common (w->left);
5839 if (w->right)
5840 free_written_common (w->right);
5842 free (w);
5845 /* Write a common block to the module -- recursive helper function. */
5847 static void
5848 write_common_0 (gfc_symtree *st, bool this_module)
5850 gfc_common_head *p;
5851 const char * name;
5852 int flags;
5853 const char *label;
5854 struct written_common *w;
5855 bool write_me = true;
5857 if (st == NULL)
5858 return;
5860 write_common_0 (st->left, this_module);
5862 /* We will write out the binding label, or "" if no label given. */
5863 name = st->n.common->name;
5864 p = st->n.common;
5865 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5867 /* Check if we've already output this common. */
5868 w = written_commons;
5869 while (w)
5871 int c = strcmp (name, w->name);
5872 c = (c != 0 ? c : strcmp (label, w->label));
5873 if (c == 0)
5874 write_me = false;
5876 w = (c < 0) ? w->left : w->right;
5879 if (this_module && p->use_assoc)
5880 write_me = false;
5882 if (write_me)
5884 /* Write the common to the module. */
5885 mio_lparen ();
5886 mio_pool_string (&name);
5888 mio_symbol_ref (&p->head);
5889 flags = p->saved ? 1 : 0;
5890 if (p->threadprivate)
5891 flags |= 2;
5892 flags |= p->omp_device_type << 2;
5893 mio_integer (&flags);
5895 /* Write out whether the common block is bind(c) or not. */
5896 mio_integer (&(p->is_bind_c));
5898 mio_pool_string (&label);
5899 mio_rparen ();
5901 /* Record that we have written this common. */
5902 w = XCNEW (struct written_common);
5903 w->name = p->name;
5904 w->label = label;
5905 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5908 write_common_0 (st->right, this_module);
5912 /* Write a common, by initializing the list of written commons, calling
5913 the recursive function write_common_0() and cleaning up afterwards. */
5915 static void
5916 write_common (gfc_symtree *st)
5918 written_commons = NULL;
5919 write_common_0 (st, true);
5920 write_common_0 (st, false);
5921 free_written_common (written_commons);
5922 written_commons = NULL;
5926 /* Write the blank common block to the module. */
5928 static void
5929 write_blank_common (void)
5931 const char * name = BLANK_COMMON_NAME;
5932 int saved;
5933 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5934 this, but it hasn't been checked. Just making it so for now. */
5935 int is_bind_c = 0;
5937 if (gfc_current_ns->blank_common.head == NULL)
5938 return;
5940 mio_lparen ();
5942 mio_pool_string (&name);
5944 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5945 saved = gfc_current_ns->blank_common.saved;
5946 mio_integer (&saved);
5948 /* Write out whether the common block is bind(c) or not. */
5949 mio_integer (&is_bind_c);
5951 /* Write out an empty binding label. */
5952 write_atom (ATOM_STRING, "");
5954 mio_rparen ();
5958 /* Write equivalences to the module. */
5960 static void
5961 write_equiv (void)
5963 gfc_equiv *eq, *e;
5964 int num;
5966 num = 0;
5967 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5969 mio_lparen ();
5971 for (e = eq; e; e = e->eq)
5973 if (e->module == NULL)
5974 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5975 mio_allocated_string (e->module);
5976 mio_expr (&e->expr);
5979 num++;
5980 mio_rparen ();
5985 /* Write a symbol to the module. */
5987 static void
5988 write_symbol (int n, gfc_symbol *sym)
5990 const char *label;
5992 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5993 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5995 mio_integer (&n);
5997 if (gfc_fl_struct (sym->attr.flavor))
5999 const char *name;
6000 name = gfc_dt_upper_string (sym->name);
6001 mio_pool_string (&name);
6003 else
6004 mio_pool_string (&sym->name);
6006 mio_pool_string (&sym->module);
6007 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
6009 label = sym->binding_label;
6010 mio_pool_string (&label);
6012 else
6013 write_atom (ATOM_STRING, "");
6015 mio_pointer_ref (&sym->ns);
6017 mio_symbol (sym);
6018 write_char ('\n');
6022 /* Recursive traversal function to write the initial set of symbols to
6023 the module. We check to see if the symbol should be written
6024 according to the access specification. */
6026 static void
6027 write_symbol0 (gfc_symtree *st)
6029 gfc_symbol *sym;
6030 pointer_info *p;
6031 bool dont_write = false;
6033 if (st == NULL)
6034 return;
6036 write_symbol0 (st->left);
6038 sym = st->n.sym;
6039 if (sym->module == NULL)
6040 sym->module = module_name;
6042 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6043 && !sym->attr.subroutine && !sym->attr.function)
6044 dont_write = true;
6046 if (!gfc_check_symbol_access (sym))
6047 dont_write = true;
6049 if (!dont_write)
6051 p = get_pointer (sym);
6052 if (p->type == P_UNKNOWN)
6053 p->type = P_SYMBOL;
6055 if (p->u.wsym.state != WRITTEN)
6057 write_symbol (p->integer, sym);
6058 p->u.wsym.state = WRITTEN;
6062 write_symbol0 (st->right);
6066 static void
6067 write_omp_udr (gfc_omp_udr *udr)
6069 switch (udr->rop)
6071 case OMP_REDUCTION_USER:
6072 /* Non-operators can't be used outside of the module. */
6073 if (udr->name[0] != '.')
6074 return;
6075 else
6077 gfc_symtree *st;
6078 size_t len = strlen (udr->name + 1);
6079 char *name = XALLOCAVEC (char, len);
6080 memcpy (name, udr->name, len - 1);
6081 name[len - 1] = '\0';
6082 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
6083 /* If corresponding user operator is private, don't write
6084 the UDR. */
6085 if (st != NULL)
6087 gfc_user_op *uop = st->n.uop;
6088 if (!check_access (uop->access, uop->ns->default_access))
6089 return;
6092 break;
6093 case OMP_REDUCTION_PLUS:
6094 case OMP_REDUCTION_MINUS:
6095 case OMP_REDUCTION_TIMES:
6096 case OMP_REDUCTION_AND:
6097 case OMP_REDUCTION_OR:
6098 case OMP_REDUCTION_EQV:
6099 case OMP_REDUCTION_NEQV:
6100 /* If corresponding operator is private, don't write the UDR. */
6101 if (!check_access (gfc_current_ns->operator_access[udr->rop],
6102 gfc_current_ns->default_access))
6103 return;
6104 break;
6105 default:
6106 break;
6108 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
6110 /* If derived type is private, don't write the UDR. */
6111 if (!gfc_check_symbol_access (udr->ts.u.derived))
6112 return;
6115 mio_lparen ();
6116 mio_pool_string (&udr->name);
6117 mio_typespec (&udr->ts);
6118 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
6119 if (udr->initializer_ns)
6120 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
6121 udr->initializer_ns, true);
6122 mio_rparen ();
6126 static void
6127 write_omp_udrs (gfc_symtree *st)
6129 if (st == NULL)
6130 return;
6132 write_omp_udrs (st->left);
6133 gfc_omp_udr *udr;
6134 for (udr = st->n.omp_udr; udr; udr = udr->next)
6135 write_omp_udr (udr);
6136 write_omp_udrs (st->right);
6140 /* Type for the temporary tree used when writing secondary symbols. */
6142 struct sorted_pointer_info
6144 BBT_HEADER (sorted_pointer_info);
6146 pointer_info *p;
6149 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
6151 /* Recursively traverse the temporary tree, free its contents. */
6153 static void
6154 free_sorted_pointer_info_tree (sorted_pointer_info *p)
6156 if (!p)
6157 return;
6159 free_sorted_pointer_info_tree (p->left);
6160 free_sorted_pointer_info_tree (p->right);
6162 free (p);
6165 /* Comparison function for the temporary tree. */
6167 static int
6168 compare_sorted_pointer_info (void *_spi1, void *_spi2)
6170 sorted_pointer_info *spi1, *spi2;
6171 spi1 = (sorted_pointer_info *)_spi1;
6172 spi2 = (sorted_pointer_info *)_spi2;
6174 if (spi1->p->integer < spi2->p->integer)
6175 return -1;
6176 if (spi1->p->integer > spi2->p->integer)
6177 return 1;
6178 return 0;
6182 /* Finds the symbols that need to be written and collects them in the
6183 sorted_pi tree so that they can be traversed in an order
6184 independent of memory addresses. */
6186 static void
6187 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
6189 if (!p)
6190 return;
6192 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
6194 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
6195 sp->p = p;
6197 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
6200 find_symbols_to_write (tree, p->left);
6201 find_symbols_to_write (tree, p->right);
6205 /* Recursive function that traverses the tree of symbols that need to be
6206 written and writes them in order. */
6208 static void
6209 write_symbol1_recursion (sorted_pointer_info *sp)
6211 if (!sp)
6212 return;
6214 write_symbol1_recursion (sp->left);
6216 pointer_info *p1 = sp->p;
6217 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
6219 p1->u.wsym.state = WRITTEN;
6220 write_symbol (p1->integer, p1->u.wsym.sym);
6221 p1->u.wsym.sym->attr.public_used = 1;
6223 write_symbol1_recursion (sp->right);
6227 /* Write the secondary set of symbols to the module file. These are
6228 symbols that were not public yet are needed by the public symbols
6229 or another dependent symbol. The act of writing a symbol can add
6230 symbols to the pointer_info tree, so we return nonzero if a symbol
6231 was written and pass that information upwards. The caller will
6232 then call this function again until nothing was written. It uses
6233 the utility functions and a temporary tree to ensure a reproducible
6234 ordering of the symbol output and thus the module file. */
6236 static int
6237 write_symbol1 (pointer_info *p)
6239 if (!p)
6240 return 0;
6242 /* Put symbols that need to be written into a tree sorted on the
6243 integer field. */
6245 sorted_pointer_info *spi_root = NULL;
6246 find_symbols_to_write (&spi_root, p);
6248 /* No symbols to write, return. */
6249 if (!spi_root)
6250 return 0;
6252 /* Otherwise, write and free the tree again. */
6253 write_symbol1_recursion (spi_root);
6254 free_sorted_pointer_info_tree (spi_root);
6256 return 1;
6260 /* Write operator interfaces associated with a symbol. */
6262 static void
6263 write_operator (gfc_user_op *uop)
6265 static char nullstring[] = "";
6266 const char *p = nullstring;
6268 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
6269 return;
6271 mio_symbol_interface (&uop->name, &p, &uop->op);
6275 /* Write generic interfaces from the namespace sym_root. */
6277 static void
6278 write_generic (gfc_symtree *st)
6280 gfc_symbol *sym;
6282 if (st == NULL)
6283 return;
6285 write_generic (st->left);
6287 sym = st->n.sym;
6288 if (sym && !check_unique_name (st->name)
6289 && sym->generic && gfc_check_symbol_access (sym))
6291 if (!sym->module)
6292 sym->module = module_name;
6294 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
6297 write_generic (st->right);
6301 static void
6302 write_symtree (gfc_symtree *st)
6304 gfc_symbol *sym;
6305 pointer_info *p;
6307 sym = st->n.sym;
6309 /* A symbol in an interface body must not be visible in the
6310 module file. */
6311 if (sym->ns != gfc_current_ns
6312 && sym->ns->proc_name
6313 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
6314 return;
6316 if (!gfc_check_symbol_access (sym)
6317 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6318 && !sym->attr.subroutine && !sym->attr.function))
6319 return;
6321 if (check_unique_name (st->name))
6322 return;
6324 /* From F2003 onwards, intrinsic procedures are no longer subject to
6325 the restriction, "that an elemental intrinsic function here be of
6326 type integer or character and each argument must be an initialization
6327 expr of type integer or character" is lifted so that intrinsic
6328 procedures can be over-ridden. This requires that the intrinsic
6329 symbol not appear in the module file, thereby preventing ambiguity
6330 when USEd. */
6331 if (strcmp (sym->module, "(intrinsic)") == 0
6332 && (gfc_option.allow_std & GFC_STD_F2003))
6333 return;
6335 p = find_pointer (sym);
6336 if (p == NULL)
6337 gfc_internal_error ("write_symtree(): Symbol not written");
6339 mio_pool_string (&st->name);
6340 mio_integer (&st->ambiguous);
6341 mio_hwi (&p->integer);
6345 static void
6346 write_module (void)
6348 int i;
6350 /* Initialize the column counter. */
6351 module_column = 1;
6353 /* Write the operator interfaces. */
6354 mio_lparen ();
6356 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
6358 if (i == INTRINSIC_USER)
6359 continue;
6361 mio_interface (check_access (gfc_current_ns->operator_access[i],
6362 gfc_current_ns->default_access)
6363 ? &gfc_current_ns->op[i] : NULL);
6366 mio_rparen ();
6367 write_char ('\n');
6368 write_char ('\n');
6370 mio_lparen ();
6371 gfc_traverse_user_op (gfc_current_ns, write_operator);
6372 mio_rparen ();
6373 write_char ('\n');
6374 write_char ('\n');
6376 mio_lparen ();
6377 write_generic (gfc_current_ns->sym_root);
6378 mio_rparen ();
6379 write_char ('\n');
6380 write_char ('\n');
6382 mio_lparen ();
6383 write_blank_common ();
6384 write_common (gfc_current_ns->common_root);
6385 mio_rparen ();
6386 write_char ('\n');
6387 write_char ('\n');
6389 mio_lparen ();
6390 write_equiv ();
6391 mio_rparen ();
6392 write_char ('\n');
6393 write_char ('\n');
6395 mio_lparen ();
6396 write_omp_udrs (gfc_current_ns->omp_udr_root);
6397 mio_rparen ();
6398 write_char ('\n');
6399 write_char ('\n');
6401 /* Write symbol information. First we traverse all symbols in the
6402 primary namespace, writing those that need to be written.
6403 Sometimes writing one symbol will cause another to need to be
6404 written. A list of these symbols ends up on the write stack, and
6405 we end by popping the bottom of the stack and writing the symbol
6406 until the stack is empty. */
6408 mio_lparen ();
6410 write_symbol0 (gfc_current_ns->sym_root);
6411 while (write_symbol1 (pi_root))
6412 /* Nothing. */;
6414 mio_rparen ();
6416 write_char ('\n');
6417 write_char ('\n');
6419 mio_lparen ();
6420 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
6421 mio_rparen ();
6425 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
6426 true on success, false on failure. */
6428 static bool
6429 read_crc32_from_module_file (const char* filename, uLong* crc)
6431 FILE *file;
6432 char buf[4];
6433 unsigned int val;
6435 /* Open the file in binary mode. */
6436 if ((file = fopen (filename, "rb")) == NULL)
6437 return false;
6439 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
6440 file. See RFC 1952. */
6441 if (fseek (file, -8, SEEK_END) != 0)
6443 fclose (file);
6444 return false;
6447 /* Read the CRC32. */
6448 if (fread (buf, 1, 4, file) != 4)
6450 fclose (file);
6451 return false;
6454 /* Close the file. */
6455 fclose (file);
6457 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6458 + ((buf[3] & 0xFF) << 24);
6459 *crc = val;
6461 /* For debugging, the CRC value printed in hexadecimal should match
6462 the CRC printed by "zcat -l -v filename".
6463 printf("CRC of file %s is %x\n", filename, val); */
6465 return true;
6469 /* Given module, dump it to disk. If there was an error while
6470 processing the module, dump_flag will be set to zero and we delete
6471 the module file, even if it was already there. */
6473 static void
6474 dump_module (const char *name, int dump_flag)
6476 int n;
6477 char *filename, *filename_tmp;
6478 uLong crc, crc_old;
6480 module_name = gfc_get_string ("%s", name);
6482 if (dump_smod)
6484 name = submodule_name;
6485 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6487 else
6488 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6490 if (gfc_option.module_dir != NULL)
6492 n += strlen (gfc_option.module_dir);
6493 filename = (char *) alloca (n);
6494 strcpy (filename, gfc_option.module_dir);
6495 strcat (filename, name);
6497 else
6499 filename = (char *) alloca (n);
6500 strcpy (filename, name);
6503 if (dump_smod)
6504 strcat (filename, SUBMODULE_EXTENSION);
6505 else
6506 strcat (filename, MODULE_EXTENSION);
6508 /* Name of the temporary file used to write the module. */
6509 filename_tmp = (char *) alloca (n + 1);
6510 strcpy (filename_tmp, filename);
6511 strcat (filename_tmp, "0");
6513 /* There was an error while processing the module. We delete the
6514 module file, even if it was already there. */
6515 if (!dump_flag)
6517 remove (filename);
6518 return;
6521 if (gfc_cpp_makedep ())
6522 gfc_cpp_add_target (filename);
6524 /* Write the module to the temporary file. */
6525 module_fp = gzopen (filename_tmp, "w");
6526 if (module_fp == NULL)
6527 gfc_fatal_error ("Cannot open module file %qs for writing at %C: %s",
6528 filename_tmp, xstrerror (errno));
6530 /* Use lbasename to ensure module files are reproducible regardless
6531 of the build path (see the reproducible builds project). */
6532 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6533 MOD_VERSION, lbasename (gfc_source_file));
6535 /* Write the module itself. */
6536 iomode = IO_OUTPUT;
6538 init_pi_tree ();
6540 write_module ();
6542 free_pi_tree (pi_root);
6543 pi_root = NULL;
6545 write_char ('\n');
6547 if (gzclose (module_fp))
6548 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6549 filename_tmp, xstrerror (errno));
6551 /* Read the CRC32 from the gzip trailers of the module files and
6552 compare. */
6553 if (!read_crc32_from_module_file (filename_tmp, &crc)
6554 || !read_crc32_from_module_file (filename, &crc_old)
6555 || crc_old != crc)
6557 /* Module file have changed, replace the old one. */
6558 if (remove (filename) && errno != ENOENT)
6559 gfc_fatal_error ("Cannot delete module file %qs: %s", filename,
6560 xstrerror (errno));
6561 if (rename (filename_tmp, filename))
6562 gfc_fatal_error ("Cannot rename module file %qs to %qs: %s",
6563 filename_tmp, filename, xstrerror (errno));
6565 else
6567 if (remove (filename_tmp))
6568 gfc_fatal_error ("Cannot delete temporary module file %qs: %s",
6569 filename_tmp, xstrerror (errno));
6574 /* Suppress the output of a .smod file by module, if no module
6575 procedures have been seen. */
6576 static bool no_module_procedures;
6578 static void
6579 check_for_module_procedures (gfc_symbol *sym)
6581 if (sym && sym->attr.module_procedure)
6582 no_module_procedures = false;
6586 void
6587 gfc_dump_module (const char *name, int dump_flag)
6589 if (gfc_state_stack->state == COMP_SUBMODULE)
6590 dump_smod = true;
6591 else
6592 dump_smod =false;
6594 no_module_procedures = true;
6595 gfc_traverse_ns (gfc_current_ns, check_for_module_procedures);
6597 dump_module (name, dump_flag);
6599 if (no_module_procedures || dump_smod)
6600 return;
6602 /* Write a submodule file from a module. The 'dump_smod' flag switches
6603 off the check for PRIVATE entities. */
6604 dump_smod = true;
6605 submodule_name = module_name;
6606 dump_module (name, dump_flag);
6607 dump_smod = false;
6610 static void
6611 create_intrinsic_function (const char *name, int id,
6612 const char *modname, intmod_id module,
6613 bool subroutine, gfc_symbol *result_type)
6615 gfc_intrinsic_sym *isym;
6616 gfc_symtree *tmp_symtree;
6617 gfc_symbol *sym;
6619 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6620 if (tmp_symtree)
6622 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6623 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6624 return;
6625 gfc_error ("Symbol %qs at %C already declared", name);
6626 return;
6629 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6630 sym = tmp_symtree->n.sym;
6632 if (subroutine)
6634 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6635 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6636 sym->attr.subroutine = 1;
6638 else
6640 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6641 isym = gfc_intrinsic_function_by_id (isym_id);
6643 sym->attr.function = 1;
6644 if (result_type)
6646 sym->ts.type = BT_DERIVED;
6647 sym->ts.u.derived = result_type;
6648 sym->ts.is_c_interop = 1;
6649 isym->ts.f90_type = BT_VOID;
6650 isym->ts.type = BT_DERIVED;
6651 isym->ts.f90_type = BT_VOID;
6652 isym->ts.u.derived = result_type;
6653 isym->ts.is_c_interop = 1;
6656 gcc_assert (isym);
6658 sym->attr.flavor = FL_PROCEDURE;
6659 sym->attr.intrinsic = 1;
6661 sym->module = gfc_get_string ("%s", modname);
6662 sym->attr.use_assoc = 1;
6663 sym->from_intmod = module;
6664 sym->intmod_sym_id = id;
6668 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6669 the current namespace for all named constants, pointer types, and
6670 procedures in the module unless the only clause was used or a rename
6671 list was provided. */
6673 static void
6674 import_iso_c_binding_module (void)
6676 gfc_symbol *mod_sym = NULL, *return_type;
6677 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6678 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6679 const char *iso_c_module_name = "__iso_c_binding";
6680 gfc_use_rename *u;
6681 int i;
6682 bool want_c_ptr = false, want_c_funptr = false;
6684 /* Look only in the current namespace. */
6685 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6687 if (mod_symtree == NULL)
6689 /* symtree doesn't already exist in current namespace. */
6690 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6691 false);
6693 if (mod_symtree != NULL)
6694 mod_sym = mod_symtree->n.sym;
6695 else
6696 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6697 "create symbol for %s", iso_c_module_name);
6699 mod_sym->attr.flavor = FL_MODULE;
6700 mod_sym->attr.intrinsic = 1;
6701 mod_sym->module = gfc_get_string ("%s", iso_c_module_name);
6702 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6705 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6706 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6707 need C_(FUN)PTR. */
6708 for (u = gfc_rename_list; u; u = u->next)
6710 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6711 u->use_name) == 0)
6712 want_c_ptr = true;
6713 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6714 u->use_name) == 0)
6715 want_c_ptr = true;
6716 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6717 u->use_name) == 0)
6718 want_c_funptr = true;
6719 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6720 u->use_name) == 0)
6721 want_c_funptr = true;
6722 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6723 u->use_name) == 0)
6725 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6726 (iso_c_binding_symbol)
6727 ISOCBINDING_PTR,
6728 u->local_name[0] ? u->local_name
6729 : u->use_name,
6730 NULL, false);
6732 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6733 u->use_name) == 0)
6735 c_funptr
6736 = generate_isocbinding_symbol (iso_c_module_name,
6737 (iso_c_binding_symbol)
6738 ISOCBINDING_FUNPTR,
6739 u->local_name[0] ? u->local_name
6740 : u->use_name,
6741 NULL, false);
6745 if ((want_c_ptr || !only_flag) && !c_ptr)
6746 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6747 (iso_c_binding_symbol)
6748 ISOCBINDING_PTR,
6749 NULL, NULL, only_flag);
6750 if ((want_c_funptr || !only_flag) && !c_funptr)
6751 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6752 (iso_c_binding_symbol)
6753 ISOCBINDING_FUNPTR,
6754 NULL, NULL, only_flag);
6756 /* Generate the symbols for the named constants representing
6757 the kinds for intrinsic data types. */
6758 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6760 bool found = false;
6761 for (u = gfc_rename_list; u; u = u->next)
6762 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6764 bool not_in_std;
6765 const char *name;
6766 u->found = 1;
6767 found = true;
6769 switch (i)
6771 #define NAMED_FUNCTION(a,b,c,d) \
6772 case a: \
6773 not_in_std = (gfc_option.allow_std & d) == 0; \
6774 name = b; \
6775 break;
6776 #define NAMED_SUBROUTINE(a,b,c,d) \
6777 case a: \
6778 not_in_std = (gfc_option.allow_std & d) == 0; \
6779 name = b; \
6780 break;
6781 #define NAMED_INTCST(a,b,c,d) \
6782 case a: \
6783 not_in_std = (gfc_option.allow_std & d) == 0; \
6784 name = b; \
6785 break;
6786 #define NAMED_UINTCST(a,b,c,d) \
6787 case a: \
6788 not_in_std = (gfc_option.allow_std & d) == 0; \
6789 name = b; \
6790 break;
6791 #define NAMED_REALCST(a,b,c,d) \
6792 case a: \
6793 not_in_std = (gfc_option.allow_std & d) == 0; \
6794 name = b; \
6795 break;
6796 #define NAMED_CMPXCST(a,b,c,d) \
6797 case a: \
6798 not_in_std = (gfc_option.allow_std & d) == 0; \
6799 name = b; \
6800 break;
6801 #include "iso-c-binding.def"
6802 default:
6803 not_in_std = false;
6804 name = "";
6807 if (not_in_std)
6809 gfc_error ("The symbol %qs, referenced at %L, is not "
6810 "in the selected standard", name, &u->where);
6811 continue;
6814 switch (i)
6816 #define NAMED_FUNCTION(a,b,c,d) \
6817 case a: \
6818 if (a == ISOCBINDING_LOC) \
6819 return_type = c_ptr->n.sym; \
6820 else if (a == ISOCBINDING_FUNLOC) \
6821 return_type = c_funptr->n.sym; \
6822 else \
6823 return_type = NULL; \
6824 create_intrinsic_function (u->local_name[0] \
6825 ? u->local_name : u->use_name, \
6826 a, iso_c_module_name, \
6827 INTMOD_ISO_C_BINDING, false, \
6828 return_type); \
6829 break;
6830 #define NAMED_SUBROUTINE(a,b,c,d) \
6831 case a: \
6832 create_intrinsic_function (u->local_name[0] ? u->local_name \
6833 : u->use_name, \
6834 a, iso_c_module_name, \
6835 INTMOD_ISO_C_BINDING, true, NULL); \
6836 break;
6837 #include "iso-c-binding.def"
6839 case ISOCBINDING_PTR:
6840 case ISOCBINDING_FUNPTR:
6841 /* Already handled above. */
6842 break;
6843 default:
6844 if (i == ISOCBINDING_NULL_PTR)
6845 tmp_symtree = c_ptr;
6846 else if (i == ISOCBINDING_NULL_FUNPTR)
6847 tmp_symtree = c_funptr;
6848 else
6849 tmp_symtree = NULL;
6850 generate_isocbinding_symbol (iso_c_module_name,
6851 (iso_c_binding_symbol) i,
6852 u->local_name[0]
6853 ? u->local_name : u->use_name,
6854 tmp_symtree, false);
6858 if (!found && !only_flag)
6860 /* Skip, if the symbol is not in the enabled standard. */
6861 switch (i)
6863 #define NAMED_FUNCTION(a,b,c,d) \
6864 case a: \
6865 if ((gfc_option.allow_std & d) == 0) \
6866 continue; \
6867 break;
6868 #define NAMED_SUBROUTINE(a,b,c,d) \
6869 case a: \
6870 if ((gfc_option.allow_std & d) == 0) \
6871 continue; \
6872 break;
6873 #define NAMED_INTCST(a,b,c,d) \
6874 case a: \
6875 if ((gfc_option.allow_std & d) == 0) \
6876 continue; \
6877 break;
6878 #define NAMED_UINTCST(a,b,c,d) \
6879 case a: \
6880 if ((gfc_option.allow_std & d) == 0) \
6881 continue; \
6882 break;
6883 #define NAMED_REALCST(a,b,c,d) \
6884 case a: \
6885 if ((gfc_option.allow_std & d) == 0) \
6886 continue; \
6887 break;
6888 #define NAMED_CMPXCST(a,b,c,d) \
6889 case a: \
6890 if ((gfc_option.allow_std & d) == 0) \
6891 continue; \
6892 break;
6893 #include "iso-c-binding.def"
6894 default:
6895 ; /* Not GFC_STD_* versioned. */
6898 switch (i)
6900 #define NAMED_FUNCTION(a,b,c,d) \
6901 case a: \
6902 if (a == ISOCBINDING_LOC) \
6903 return_type = c_ptr->n.sym; \
6904 else if (a == ISOCBINDING_FUNLOC) \
6905 return_type = c_funptr->n.sym; \
6906 else \
6907 return_type = NULL; \
6908 create_intrinsic_function (b, a, iso_c_module_name, \
6909 INTMOD_ISO_C_BINDING, false, \
6910 return_type); \
6911 break;
6912 #define NAMED_SUBROUTINE(a,b,c,d) \
6913 case a: \
6914 create_intrinsic_function (b, a, iso_c_module_name, \
6915 INTMOD_ISO_C_BINDING, true, NULL); \
6916 break;
6917 #include "iso-c-binding.def"
6919 case ISOCBINDING_PTR:
6920 case ISOCBINDING_FUNPTR:
6921 /* Already handled above. */
6922 break;
6923 default:
6924 if (i == ISOCBINDING_NULL_PTR)
6925 tmp_symtree = c_ptr;
6926 else if (i == ISOCBINDING_NULL_FUNPTR)
6927 tmp_symtree = c_funptr;
6928 else
6929 tmp_symtree = NULL;
6930 generate_isocbinding_symbol (iso_c_module_name,
6931 (iso_c_binding_symbol) i, NULL,
6932 tmp_symtree, false);
6937 for (u = gfc_rename_list; u; u = u->next)
6939 if (u->found)
6940 continue;
6942 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6943 "module ISO_C_BINDING", u->use_name, &u->where);
6948 /* Add an integer named constant from a given module. */
6950 static void
6951 create_int_parameter (const char *name, int value, const char *modname,
6952 intmod_id module, int id)
6954 gfc_symtree *tmp_symtree;
6955 gfc_symbol *sym;
6957 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6958 if (tmp_symtree != NULL)
6960 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6961 return;
6962 else
6963 gfc_error ("Symbol %qs already declared", name);
6966 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6967 sym = tmp_symtree->n.sym;
6969 sym->module = gfc_get_string ("%s", modname);
6970 sym->attr.flavor = FL_PARAMETER;
6971 sym->ts.type = BT_INTEGER;
6972 sym->ts.kind = gfc_default_integer_kind;
6973 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6974 sym->attr.use_assoc = 1;
6975 sym->from_intmod = module;
6976 sym->intmod_sym_id = id;
6980 /* Value is already contained by the array constructor, but not
6981 yet the shape. */
6983 static void
6984 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6985 const char *modname, intmod_id module, int id)
6987 gfc_symtree *tmp_symtree;
6988 gfc_symbol *sym;
6990 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6991 if (tmp_symtree != NULL)
6993 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6994 return;
6995 else
6996 gfc_error ("Symbol %qs already declared", name);
6999 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7000 sym = tmp_symtree->n.sym;
7002 sym->module = gfc_get_string ("%s", modname);
7003 sym->attr.flavor = FL_PARAMETER;
7004 sym->ts.type = BT_INTEGER;
7005 sym->ts.kind = gfc_default_integer_kind;
7006 sym->attr.use_assoc = 1;
7007 sym->from_intmod = module;
7008 sym->intmod_sym_id = id;
7009 sym->attr.dimension = 1;
7010 sym->as = gfc_get_array_spec ();
7011 sym->as->rank = 1;
7012 sym->as->type = AS_EXPLICIT;
7013 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
7014 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
7016 sym->value = value;
7017 sym->value->shape = gfc_get_shape (1);
7018 mpz_init_set_ui (sym->value->shape[0], size);
7022 /* Add an derived type for a given module. */
7024 static void
7025 create_derived_type (const char *name, const char *modname,
7026 intmod_id module, int id)
7028 gfc_symtree *tmp_symtree;
7029 gfc_symbol *sym, *dt_sym;
7030 gfc_interface *intr, *head;
7032 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
7033 if (tmp_symtree != NULL)
7035 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
7036 return;
7037 else
7038 gfc_error ("Symbol %qs already declared", name);
7041 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7042 sym = tmp_symtree->n.sym;
7043 sym->module = gfc_get_string ("%s", modname);
7044 sym->from_intmod = module;
7045 sym->intmod_sym_id = id;
7046 sym->attr.flavor = FL_PROCEDURE;
7047 sym->attr.function = 1;
7048 sym->attr.generic = 1;
7050 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
7051 gfc_current_ns, &tmp_symtree, false);
7052 dt_sym = tmp_symtree->n.sym;
7053 dt_sym->name = gfc_get_string ("%s", sym->name);
7054 dt_sym->attr.flavor = FL_DERIVED;
7055 dt_sym->attr.private_comp = 1;
7056 dt_sym->attr.zero_comp = 1;
7057 dt_sym->attr.use_assoc = 1;
7058 dt_sym->module = gfc_get_string ("%s", modname);
7059 dt_sym->from_intmod = module;
7060 dt_sym->intmod_sym_id = id;
7062 head = sym->generic;
7063 intr = gfc_get_interface ();
7064 intr->sym = dt_sym;
7065 intr->where = gfc_current_locus;
7066 intr->next = head;
7067 sym->generic = intr;
7068 sym->attr.if_source = IFSRC_DECL;
7072 /* Read the contents of the module file into a temporary buffer. */
7074 static void
7075 read_module_to_tmpbuf ()
7077 /* We don't know the uncompressed size, so enlarge the buffer as
7078 needed. */
7079 int cursz = 4096;
7080 int rsize = cursz;
7081 int len = 0;
7083 module_content = XNEWVEC (char, cursz);
7085 while (1)
7087 int nread = gzread (module_fp, module_content + len, rsize);
7088 len += nread;
7089 if (nread < rsize)
7090 break;
7091 cursz *= 2;
7092 module_content = XRESIZEVEC (char, module_content, cursz);
7093 rsize = cursz - len;
7096 module_content = XRESIZEVEC (char, module_content, len + 1);
7097 module_content[len] = '\0';
7099 module_pos = 0;
7103 /* USE the ISO_FORTRAN_ENV intrinsic module. */
7105 static void
7106 use_iso_fortran_env_module (void)
7108 static char mod[] = "iso_fortran_env";
7109 gfc_use_rename *u;
7110 gfc_symbol *mod_sym;
7111 gfc_symtree *mod_symtree;
7112 gfc_expr *expr;
7113 int i, j;
7115 intmod_sym symbol[] = {
7116 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
7117 #define NAMED_UINTCST(a,b,c,d) { a, b, 0, d },
7118 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
7119 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
7120 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
7121 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
7122 #include "iso-fortran-env.def"
7123 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
7125 /* We could have used c in the NAMED_{,U}INTCST macros
7126 instead of 0, but then current g++ expands the initialization
7127 as clearing the whole object followed by explicit stores of
7128 all the non-zero elements (over 150), while by using 0s for
7129 the non-constant initializers and initializing them afterwards
7130 g++ will often copy everything from .rodata and then only override
7131 over 30 non-constant ones. */
7132 i = 0;
7133 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
7134 #define NAMED_UINTCST(a,b,c,d) symbol[i++].value = c;
7135 #define NAMED_KINDARRAY(a,b,c,d) i++;
7136 #define NAMED_DERIVED_TYPE(a,b,c,d) i++;
7137 #define NAMED_FUNCTION(a,b,c,d) i++;
7138 #define NAMED_SUBROUTINE(a,b,c,d) i++;
7139 #include "iso-fortran-env.def"
7140 gcc_checking_assert (i == (int) ARRAY_SIZE (symbol) - 1);
7142 /* Generate the symbol for the module itself. */
7143 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
7144 if (mod_symtree == NULL)
7146 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
7147 gcc_assert (mod_symtree);
7148 mod_sym = mod_symtree->n.sym;
7150 mod_sym->attr.flavor = FL_MODULE;
7151 mod_sym->attr.intrinsic = 1;
7152 mod_sym->module = gfc_get_string ("%s", mod);
7153 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
7155 else
7156 if (!mod_symtree->n.sym->attr.intrinsic)
7157 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
7158 "non-intrinsic module name used previously", mod);
7160 /* Generate the symbols for the module integer named constants. */
7162 for (i = 0; symbol[i].name; i++)
7164 bool found = false;
7165 for (u = gfc_rename_list; u; u = u->next)
7167 if (strcmp (symbol[i].name, u->use_name) == 0)
7169 found = true;
7170 u->found = 1;
7172 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
7173 "referenced at %L, is not in the selected "
7174 "standard", symbol[i].name, &u->where))
7175 continue;
7177 if ((flag_default_integer || flag_default_real_8)
7178 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7179 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
7180 "constant from intrinsic module "
7181 "ISO_FORTRAN_ENV at %L is incompatible with "
7182 "option %qs", &u->where,
7183 flag_default_integer
7184 ? "-fdefault-integer-8"
7185 : "-fdefault-real-8");
7186 switch (symbol[i].id)
7188 #define NAMED_INTCST(a,b,c,d) \
7189 case a:
7190 #include "iso-fortran-env.def"
7191 create_int_parameter (u->local_name[0] ? u->local_name
7192 : u->use_name,
7193 symbol[i].value, mod,
7194 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7195 break;
7197 #define NAMED_UINTCST(a,b,c,d) \
7198 case a:
7199 #include "iso-fortran-env.def"
7200 create_int_parameter (u->local_name[0] ? u->local_name
7201 : u->use_name,
7202 symbol[i].value, mod,
7203 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7204 break;
7206 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7207 case a:\
7208 expr = gfc_get_array_expr (BT_INTEGER, \
7209 gfc_default_integer_kind,\
7210 NULL); \
7211 for (j = 0; KINDS[j].kind != 0; j++) \
7212 gfc_constructor_append_expr (&expr->value.constructor, \
7213 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7214 KINDS[j].kind), NULL); \
7215 create_int_parameter_array (u->local_name[0] ? u->local_name \
7216 : u->use_name, \
7217 j, expr, mod, \
7218 INTMOD_ISO_FORTRAN_ENV, \
7219 symbol[i].id); \
7220 break;
7221 #include "iso-fortran-env.def"
7223 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7224 case a:
7225 #include "iso-fortran-env.def"
7226 create_derived_type (u->local_name[0] ? u->local_name
7227 : u->use_name,
7228 mod, INTMOD_ISO_FORTRAN_ENV,
7229 symbol[i].id);
7230 break;
7232 #define NAMED_FUNCTION(a,b,c,d) \
7233 case a:
7234 #include "iso-fortran-env.def"
7235 create_intrinsic_function (u->local_name[0] ? u->local_name
7236 : u->use_name,
7237 symbol[i].id, mod,
7238 INTMOD_ISO_FORTRAN_ENV, false,
7239 NULL);
7240 break;
7242 default:
7243 gcc_unreachable ();
7248 if (!found && !only_flag)
7250 if ((gfc_option.allow_std & symbol[i].standard) == 0)
7251 continue;
7253 if ((flag_default_integer || flag_default_real_8)
7254 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7255 gfc_warning_now (0,
7256 "Use of the NUMERIC_STORAGE_SIZE named constant "
7257 "from intrinsic module ISO_FORTRAN_ENV at %C is "
7258 "incompatible with option %s",
7259 flag_default_integer
7260 ? "-fdefault-integer-8" : "-fdefault-real-8");
7262 switch (symbol[i].id)
7264 #define NAMED_INTCST(a,b,c,d) \
7265 case a:
7266 #include "iso-fortran-env.def"
7267 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7268 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7269 break;
7271 #define NAMED_UINTCST(a,b,c,d) \
7272 case a:
7273 #include "iso-fortran-env.def"
7274 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7275 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7276 break;
7278 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7279 case a:\
7280 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
7281 NULL); \
7282 for (j = 0; KINDS[j].kind != 0; j++) \
7283 gfc_constructor_append_expr (&expr->value.constructor, \
7284 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7285 KINDS[j].kind), NULL); \
7286 create_int_parameter_array (symbol[i].name, j, expr, mod, \
7287 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
7288 break;
7289 #include "iso-fortran-env.def"
7291 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7292 case a:
7293 #include "iso-fortran-env.def"
7294 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
7295 symbol[i].id);
7296 break;
7298 #define NAMED_FUNCTION(a,b,c,d) \
7299 case a:
7300 #include "iso-fortran-env.def"
7301 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
7302 INTMOD_ISO_FORTRAN_ENV, false, NULL);
7303 break;
7305 default:
7306 gcc_unreachable ();
7311 for (u = gfc_rename_list; u; u = u->next)
7313 if (u->found)
7314 continue;
7316 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
7317 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
7322 /* Process a USE directive. */
7324 static void
7325 gfc_use_module (gfc_use_list *module)
7327 char *filename;
7328 gfc_state_data *p;
7329 int c, line, start;
7330 gfc_symtree *mod_symtree;
7331 gfc_use_list *use_stmt;
7332 locus old_locus = gfc_current_locus;
7334 gfc_current_locus = module->where;
7335 module_name = module->module_name;
7336 gfc_rename_list = module->rename;
7337 only_flag = module->only_flag;
7338 current_intmod = INTMOD_NONE;
7340 if (!only_flag)
7341 gfc_warning_now (OPT_Wuse_without_only,
7342 "USE statement at %C has no ONLY qualifier");
7344 if (gfc_state_stack->state == COMP_MODULE
7345 || module->submodule_name == NULL)
7347 filename = XALLOCAVEC (char, strlen (module_name)
7348 + strlen (MODULE_EXTENSION) + 1);
7349 strcpy (filename, module_name);
7350 strcat (filename, MODULE_EXTENSION);
7352 else
7354 filename = XALLOCAVEC (char, strlen (module->submodule_name)
7355 + strlen (SUBMODULE_EXTENSION) + 1);
7356 strcpy (filename, module->submodule_name);
7357 strcat (filename, SUBMODULE_EXTENSION);
7360 /* First, try to find an non-intrinsic module, unless the USE statement
7361 specified that the module is intrinsic. */
7362 module_fp = NULL;
7363 if (!module->intrinsic)
7364 module_fp = gzopen_included_file (filename, true, true);
7366 /* Then, see if it's an intrinsic one, unless the USE statement
7367 specified that the module is non-intrinsic. */
7368 if (module_fp == NULL && !module->non_intrinsic)
7370 if (strcmp (module_name, "iso_fortran_env") == 0
7371 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
7372 "intrinsic module at %C"))
7374 use_iso_fortran_env_module ();
7375 free_rename (module->rename);
7376 module->rename = NULL;
7377 gfc_current_locus = old_locus;
7378 module->intrinsic = true;
7379 return;
7382 if (strcmp (module_name, "iso_c_binding") == 0
7383 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
7385 import_iso_c_binding_module();
7386 free_rename (module->rename);
7387 module->rename = NULL;
7388 gfc_current_locus = old_locus;
7389 module->intrinsic = true;
7390 return;
7393 module_fp = gzopen_intrinsic_module (filename);
7395 if (module_fp == NULL && module->intrinsic)
7396 gfc_fatal_error ("Cannot find an intrinsic module named %qs at %C",
7397 module_name);
7399 /* Check for the IEEE modules, so we can mark their symbols
7400 accordingly when we read them. */
7401 if (strcmp (module_name, "ieee_features") == 0
7402 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
7404 current_intmod = INTMOD_IEEE_FEATURES;
7406 else if (strcmp (module_name, "ieee_exceptions") == 0
7407 && gfc_notify_std (GFC_STD_F2003,
7408 "IEEE_EXCEPTIONS module at %C"))
7410 current_intmod = INTMOD_IEEE_EXCEPTIONS;
7412 else if (strcmp (module_name, "ieee_arithmetic") == 0
7413 && gfc_notify_std (GFC_STD_F2003,
7414 "IEEE_ARITHMETIC module at %C"))
7416 current_intmod = INTMOD_IEEE_ARITHMETIC;
7420 if (module_fp == NULL)
7422 if (gfc_state_stack->state != COMP_SUBMODULE
7423 && module->submodule_name == NULL)
7424 gfc_fatal_error ("Cannot open module file %qs for reading at %C: %s",
7425 filename, xstrerror (errno));
7426 else
7427 gfc_fatal_error ("Module file %qs has not been generated, either "
7428 "because the module does not contain a MODULE "
7429 "PROCEDURE or there is an error in the module.",
7430 filename);
7433 /* Check that we haven't already USEd an intrinsic module with the
7434 same name. */
7436 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
7437 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
7438 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
7439 "intrinsic module name used previously", module_name);
7441 iomode = IO_INPUT;
7442 module_line = 1;
7443 module_column = 1;
7444 start = 0;
7446 read_module_to_tmpbuf ();
7447 gzclose (module_fp);
7449 /* Skip the first line of the module, after checking that this is
7450 a gfortran module file. */
7451 line = 0;
7452 while (line < 1)
7454 c = module_char ();
7455 if (c == EOF)
7456 bad_module ("Unexpected end of module");
7457 if (start++ < 3)
7458 parse_name (c);
7459 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
7460 || (start == 2 && strcmp (atom_name, " module") != 0))
7461 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
7462 " module file", module_fullpath);
7463 if (start == 3)
7465 bool fatal = false;
7466 if (strcmp (atom_name, " version") != 0
7467 || module_char () != ' '
7468 || parse_atom () != ATOM_STRING)
7469 fatal = true;
7470 else if (strcmp (atom_string, MOD_VERSION))
7472 static const char *compat_mod_versions[] = COMPAT_MOD_VERSIONS;
7473 fatal = true;
7474 for (unsigned i = 0; i < ARRAY_SIZE (compat_mod_versions); ++i)
7475 if (!strcmp (atom_string, compat_mod_versions[i]))
7477 fatal = false;
7478 break;
7481 if (fatal)
7482 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
7483 " because it was created by a different"
7484 " version of GNU Fortran", module_fullpath);
7486 free (atom_string);
7489 if (c == '\n')
7490 line++;
7493 /* Make sure we're not reading the same module that we may be building. */
7494 for (p = gfc_state_stack; p; p = p->previous)
7495 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
7496 && strcmp (p->sym->name, module_name) == 0)
7498 if (p->state == COMP_SUBMODULE)
7499 gfc_fatal_error ("Cannot USE a submodule that is currently built");
7500 else
7501 gfc_fatal_error ("Cannot USE a module that is currently built");
7504 init_pi_tree ();
7505 init_true_name_tree ();
7507 read_module ();
7509 free_true_name (true_name_root);
7510 true_name_root = NULL;
7512 free_pi_tree (pi_root);
7513 pi_root = NULL;
7515 XDELETEVEC (module_content);
7516 module_content = NULL;
7518 use_stmt = gfc_get_use_list ();
7519 *use_stmt = *module;
7520 use_stmt->next = gfc_current_ns->use_stmts;
7521 gfc_current_ns->use_stmts = use_stmt;
7523 gfc_current_locus = old_locus;
7527 /* Remove duplicated intrinsic operators from the rename list. */
7529 static void
7530 rename_list_remove_duplicate (gfc_use_rename *list)
7532 gfc_use_rename *seek, *last;
7534 for (; list; list = list->next)
7535 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7537 last = list;
7538 for (seek = list->next; seek; seek = last->next)
7540 if (list->op == seek->op)
7542 last->next = seek->next;
7543 free (seek);
7545 else
7546 last = seek;
7552 /* Process all USE directives. */
7554 void
7555 gfc_use_modules (void)
7557 gfc_use_list *next, *seek, *last;
7559 for (next = module_list; next; next = next->next)
7561 bool non_intrinsic = next->non_intrinsic;
7562 bool intrinsic = next->intrinsic;
7563 bool neither = !non_intrinsic && !intrinsic;
7565 for (seek = next->next; seek; seek = seek->next)
7567 if (next->module_name != seek->module_name)
7568 continue;
7570 if (seek->non_intrinsic)
7571 non_intrinsic = true;
7572 else if (seek->intrinsic)
7573 intrinsic = true;
7574 else
7575 neither = true;
7578 if (intrinsic && neither && !non_intrinsic)
7580 char *filename;
7581 FILE *fp;
7583 filename = XALLOCAVEC (char,
7584 strlen (next->module_name)
7585 + strlen (MODULE_EXTENSION) + 1);
7586 strcpy (filename, next->module_name);
7587 strcat (filename, MODULE_EXTENSION);
7588 fp = gfc_open_included_file (filename, true, true);
7589 if (fp != NULL)
7591 non_intrinsic = true;
7592 fclose (fp);
7596 last = next;
7597 for (seek = next->next; seek; seek = last->next)
7599 if (next->module_name != seek->module_name)
7601 last = seek;
7602 continue;
7605 if ((!next->intrinsic && !seek->intrinsic)
7606 || (next->intrinsic && seek->intrinsic)
7607 || !non_intrinsic)
7609 if (!seek->only_flag)
7610 next->only_flag = false;
7611 if (seek->rename)
7613 gfc_use_rename *r = seek->rename;
7614 while (r->next)
7615 r = r->next;
7616 r->next = next->rename;
7617 next->rename = seek->rename;
7619 last->next = seek->next;
7620 free (seek);
7622 else
7623 last = seek;
7627 for (; module_list; module_list = next)
7629 next = module_list->next;
7630 rename_list_remove_duplicate (module_list->rename);
7631 gfc_use_module (module_list);
7632 free (module_list);
7634 module_list = NULL;
7635 old_module_list_tail = &module_list;
7636 gfc_rename_list = NULL;
7640 void
7641 gfc_free_use_stmts (gfc_use_list *use_stmts)
7643 gfc_use_list *next;
7644 for (; use_stmts; use_stmts = next)
7646 gfc_use_rename *next_rename;
7648 for (; use_stmts->rename; use_stmts->rename = next_rename)
7650 next_rename = use_stmts->rename->next;
7651 free (use_stmts->rename);
7653 next = use_stmts->next;
7654 free (use_stmts);
7659 /* Remember the end of the MODULE_LIST list, so that the list can be restored
7660 to its previous state if the current statement is erroneous. */
7662 void
7663 gfc_save_module_list ()
7665 gfc_use_list **tail = &module_list;
7666 while (*tail != NULL)
7667 tail = &(*tail)->next;
7668 old_module_list_tail = tail;
7672 /* Restore the MODULE_LIST list to its previous value and free the use
7673 statements that are no longer part of the list. */
7675 void
7676 gfc_restore_old_module_list ()
7678 gfc_free_use_stmts (*old_module_list_tail);
7679 *old_module_list_tail = NULL;
7683 void
7684 gfc_module_init_2 (void)
7686 last_atom = ATOM_LPAREN;
7687 gfc_rename_list = NULL;
7688 module_list = NULL;
7692 void
7693 gfc_module_done_2 (void)
7695 free_rename (gfc_rename_list);
7696 gfc_rename_list = NULL;