libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / module.cc
blob880aef2c7a89198dee697c739ec6fb53e058cc24
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
23 sequence of atoms, which can be left or right parenthesis, names,
24 integers or strings. Parenthesis are always matched which allows
25 us to skip over sections at high speed without having to know
26 anything about the internal structure of the lists. A "name" is
27 usually a fortran 95 identifier, but can also start with '@' in
28 order to reference a hidden symbol.
30 The first line of a module is an informational message about what
31 created the module, the file it came from and when it was created.
32 The second line is a warning for people not to edit the module.
33 The rest of the module looks like:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
45 ( ( <common name> <symbol> <saved flag>)
46 ...
49 ( equivalence list )
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
63 In general, symbols refer to other symbols by their symbol number,
64 which are zero based. Symbols are written to the module in no
65 particular order. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "options.h"
71 #include "tree.h"
72 #include "gfortran.h"
73 #include "stringpool.h"
74 #include "arith.h"
75 #include "match.h"
76 #include "parse.h" /* FIXME */
77 #include "constructor.h"
78 #include "cpp.h"
79 #include "scanner.h"
80 #include <zlib.h>
82 #define MODULE_EXTENSION ".mod"
83 #define SUBMODULE_EXTENSION ".smod"
85 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
86 recognized. */
87 #define MOD_VERSION "15"
90 /* Structure that describes a position within a module file. */
92 typedef struct
94 int column, line;
95 long pos;
97 module_locus;
99 /* Structure for list of symbols of intrinsic modules. */
100 typedef struct
102 int id;
103 const char *name;
104 int value;
105 int standard;
107 intmod_sym;
110 typedef enum
112 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
114 pointer_t;
116 /* The fixup structure lists pointers to pointers that have to
117 be updated when a pointer value becomes known. */
119 typedef struct fixup_t
121 void **pointer;
122 struct fixup_t *next;
124 fixup_t;
127 /* Structure for holding extra info needed for pointers being read. */
129 enum gfc_rsym_state
131 UNUSED,
132 NEEDED,
133 USED
136 enum gfc_wsym_state
138 UNREFERENCED = 0,
139 NEEDS_WRITE,
140 WRITTEN
143 typedef struct pointer_info
145 BBT_HEADER (pointer_info);
146 HOST_WIDE_INT integer;
147 pointer_t type;
149 /* The first component of each member of the union is the pointer
150 being stored. */
152 fixup_t *fixup;
154 union
156 void *pointer; /* Member for doing pointer searches. */
158 struct
160 gfc_symbol *sym;
161 char *true_name, *module, *binding_label;
162 fixup_t *stfixup;
163 gfc_symtree *symtree;
164 enum gfc_rsym_state state;
165 int ns, referenced, renamed;
166 module_locus where;
168 rsym;
170 struct
172 gfc_symbol *sym;
173 enum gfc_wsym_state state;
175 wsym;
180 pointer_info;
182 #define gfc_get_pointer_info() XCNEW (pointer_info)
185 /* Local variables */
187 /* The gzFile for the module we're reading or writing. */
188 static gzFile module_fp;
190 /* Fully qualified module path */
191 static char *module_fullpath = NULL;
193 /* The name of the module we're reading (USE'ing) or writing. */
194 static const char *module_name;
195 /* The name of the .smod file that the submodule will write to. */
196 static const char *submodule_name;
198 /* The list of use statements to apply to the current namespace
199 before parsing the non-use statements. */
200 static gfc_use_list *module_list;
201 /* The end of the MODULE_LIST list above at the time the recognition
202 of the current statement started. */
203 static gfc_use_list **old_module_list_tail;
205 /* If we're reading an intrinsic module, this is its ID. */
206 static intmod_id current_intmod;
208 /* Content of module. */
209 static char* module_content;
211 static long module_pos;
212 static int module_line, module_column, only_flag;
213 static int prev_module_line, prev_module_column;
215 static enum
216 { IO_INPUT, IO_OUTPUT }
217 iomode;
219 static gfc_use_rename *gfc_rename_list;
220 static pointer_info *pi_root;
221 static int symbol_number; /* Counter for assigning symbol numbers */
223 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
224 static bool in_load_equiv;
228 /*****************************************************************/
230 /* Pointer/integer conversion. Pointers between structures are stored
231 as integers in the module file. The next couple of subroutines
232 handle this translation for reading and writing. */
234 /* Recursively free the tree of pointer structures. */
236 static void
237 free_pi_tree (pointer_info *p)
239 if (p == NULL)
240 return;
242 if (p->fixup != NULL)
243 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
245 free_pi_tree (p->left);
246 free_pi_tree (p->right);
248 if (iomode == IO_INPUT)
250 XDELETEVEC (p->u.rsym.true_name);
251 XDELETEVEC (p->u.rsym.module);
252 XDELETEVEC (p->u.rsym.binding_label);
255 free (p);
259 /* Compare pointers when searching by pointer. Used when writing a
260 module. */
262 static int
263 compare_pointers (void *_sn1, void *_sn2)
265 pointer_info *sn1, *sn2;
267 sn1 = (pointer_info *) _sn1;
268 sn2 = (pointer_info *) _sn2;
270 if (sn1->u.pointer < sn2->u.pointer)
271 return -1;
272 if (sn1->u.pointer > sn2->u.pointer)
273 return 1;
275 return 0;
279 /* Compare integers when searching by integer. Used when reading a
280 module. */
282 static int
283 compare_integers (void *_sn1, void *_sn2)
285 pointer_info *sn1, *sn2;
287 sn1 = (pointer_info *) _sn1;
288 sn2 = (pointer_info *) _sn2;
290 if (sn1->integer < sn2->integer)
291 return -1;
292 if (sn1->integer > sn2->integer)
293 return 1;
295 return 0;
299 /* Initialize the pointer_info tree. */
301 static void
302 init_pi_tree (void)
304 compare_fn compare;
305 pointer_info *p;
307 pi_root = NULL;
308 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
310 /* Pointer 0 is the NULL pointer. */
311 p = gfc_get_pointer_info ();
312 p->u.pointer = NULL;
313 p->integer = 0;
314 p->type = P_OTHER;
316 gfc_insert_bbt (&pi_root, p, compare);
318 /* Pointer 1 is the current namespace. */
319 p = gfc_get_pointer_info ();
320 p->u.pointer = gfc_current_ns;
321 p->integer = 1;
322 p->type = P_NAMESPACE;
324 gfc_insert_bbt (&pi_root, p, compare);
326 symbol_number = 2;
330 /* During module writing, call here with a pointer to something,
331 returning the pointer_info node. */
333 static pointer_info *
334 find_pointer (void *gp)
336 pointer_info *p;
338 p = pi_root;
339 while (p != NULL)
341 if (p->u.pointer == gp)
342 break;
343 p = (gp < p->u.pointer) ? p->left : p->right;
346 return p;
350 /* Given a pointer while writing, returns the pointer_info tree node,
351 creating it if it doesn't exist. */
353 static pointer_info *
354 get_pointer (void *gp)
356 pointer_info *p;
358 p = find_pointer (gp);
359 if (p != NULL)
360 return p;
362 /* Pointer doesn't have an integer. Give it one. */
363 p = gfc_get_pointer_info ();
365 p->u.pointer = gp;
366 p->integer = symbol_number++;
368 gfc_insert_bbt (&pi_root, p, compare_pointers);
370 return p;
374 /* Given an integer during reading, find it in the pointer_info tree,
375 creating the node if not found. */
377 static pointer_info *
378 get_integer (HOST_WIDE_INT integer)
380 pointer_info *p, t;
381 int c;
383 t.integer = integer;
385 p = pi_root;
386 while (p != NULL)
388 c = compare_integers (&t, p);
389 if (c == 0)
390 break;
392 p = (c < 0) ? p->left : p->right;
395 if (p != NULL)
396 return p;
398 p = gfc_get_pointer_info ();
399 p->integer = integer;
400 p->u.pointer = NULL;
402 gfc_insert_bbt (&pi_root, p, compare_integers);
404 return p;
408 /* Resolve any fixups using a known pointer. */
410 static void
411 resolve_fixups (fixup_t *f, void *gp)
413 fixup_t *next;
415 for (; f; f = next)
417 next = f->next;
418 *(f->pointer) = gp;
419 free (f);
424 /* Convert a string such that it starts with a lower-case character. Used
425 to convert the symtree name of a derived-type to the symbol name or to
426 the name of the associated generic function. */
428 const char *
429 gfc_dt_lower_string (const char *name)
431 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
432 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
433 &name[1]);
434 return gfc_get_string ("%s", name);
438 /* Convert a string such that it starts with an upper-case character. Used to
439 return the symtree-name for a derived type; the symbol name itself and the
440 symtree/symbol name of the associated generic function start with a lower-
441 case character. */
443 const char *
444 gfc_dt_upper_string (const char *name)
446 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
447 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
448 &name[1]);
449 return gfc_get_string ("%s", name);
452 /* Call here during module reading when we know what pointer to
453 associate with an integer. Any fixups that exist are resolved at
454 this time. */
456 static void
457 associate_integer_pointer (pointer_info *p, void *gp)
459 if (p->u.pointer != NULL)
460 gfc_internal_error ("associate_integer_pointer(): Already associated");
462 p->u.pointer = gp;
464 resolve_fixups (p->fixup, gp);
466 p->fixup = NULL;
470 /* During module reading, given an integer and a pointer to a pointer,
471 either store the pointer from an already-known value or create a
472 fixup structure in order to store things later. Returns zero if
473 the reference has been actually stored, or nonzero if the reference
474 must be fixed later (i.e., associate_integer_pointer must be called
475 sometime later. Returns the pointer_info structure. */
477 static pointer_info *
478 add_fixup (HOST_WIDE_INT integer, void *gp)
480 pointer_info *p;
481 fixup_t *f;
482 char **cp;
484 p = get_integer (integer);
486 if (p->integer == 0 || p->u.pointer != NULL)
488 cp = (char **) gp;
489 *cp = (char *) p->u.pointer;
491 else
493 f = XCNEW (fixup_t);
495 f->next = p->fixup;
496 p->fixup = f;
498 f->pointer = (void **) gp;
501 return p;
505 /*****************************************************************/
507 /* Parser related subroutines */
509 /* Free the rename list left behind by a USE statement. */
511 static void
512 free_rename (gfc_use_rename *list)
514 gfc_use_rename *next;
516 for (; list; list = next)
518 next = list->next;
519 free (list);
524 /* Match a USE statement. */
526 match
527 gfc_match_use (void)
529 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
530 gfc_use_rename *tail = NULL, *new_use;
531 interface_type type, type2;
532 gfc_intrinsic_op op;
533 match m;
534 gfc_use_list *use_list;
535 gfc_symtree *st;
536 locus loc;
538 use_list = gfc_get_use_list ();
540 if (gfc_match (" , ") == MATCH_YES)
542 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
544 if (!gfc_notify_std (GFC_STD_F2003, "module "
545 "nature in USE statement at %C"))
546 goto cleanup;
548 if (strcmp (module_nature, "intrinsic") == 0)
549 use_list->intrinsic = true;
550 else
552 if (strcmp (module_nature, "non_intrinsic") == 0)
553 use_list->non_intrinsic = true;
554 else
556 gfc_error ("Module nature in USE statement at %C shall "
557 "be either INTRINSIC or NON_INTRINSIC");
558 goto cleanup;
562 else
564 /* Help output a better error message than "Unclassifiable
565 statement". */
566 gfc_match (" %n", module_nature);
567 if (strcmp (module_nature, "intrinsic") == 0
568 || strcmp (module_nature, "non_intrinsic") == 0)
569 gfc_error ("\"::\" was expected after module nature at %C "
570 "but was not found");
571 free (use_list);
572 return m;
575 else
577 m = gfc_match (" ::");
578 if (m == MATCH_YES &&
579 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
580 goto cleanup;
582 if (m != MATCH_YES)
584 m = gfc_match ("% ");
585 if (m != MATCH_YES)
587 free (use_list);
588 return m;
593 use_list->where = gfc_current_locus;
595 m = gfc_match_name (name);
596 if (m != MATCH_YES)
598 free (use_list);
599 return m;
602 use_list->module_name = gfc_get_string ("%s", name);
604 if (gfc_match_eos () == MATCH_YES)
605 goto done;
607 if (gfc_match_char (',') != MATCH_YES)
608 goto syntax;
610 if (gfc_match (" only :") == MATCH_YES)
611 use_list->only_flag = true;
613 if (gfc_match_eos () == MATCH_YES)
614 goto done;
616 for (;;)
618 /* Get a new rename struct and add it to the rename list. */
619 new_use = gfc_get_use_rename ();
620 new_use->where = gfc_current_locus;
621 new_use->found = 0;
623 if (use_list->rename == NULL)
624 use_list->rename = new_use;
625 else
626 tail->next = new_use;
627 tail = new_use;
629 /* See what kind of interface we're dealing with. Assume it is
630 not an operator. */
631 new_use->op = INTRINSIC_NONE;
632 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
633 goto cleanup;
635 switch (type)
637 case INTERFACE_NAMELESS:
638 gfc_error ("Missing generic specification in USE statement at %C");
639 goto cleanup;
641 case INTERFACE_USER_OP:
642 case INTERFACE_GENERIC:
643 case INTERFACE_DTIO:
644 loc = gfc_current_locus;
646 m = gfc_match (" =>");
648 if (type == INTERFACE_USER_OP && m == MATCH_YES
649 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
650 "operators in USE statements at %C")))
651 goto cleanup;
653 if (type == INTERFACE_USER_OP)
654 new_use->op = INTRINSIC_USER;
656 if (use_list->only_flag)
658 if (m != MATCH_YES)
659 strcpy (new_use->use_name, name);
660 else
662 strcpy (new_use->local_name, name);
663 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
664 if (type != type2)
665 goto syntax;
666 if (m == MATCH_NO)
667 goto syntax;
668 if (m == MATCH_ERROR)
669 goto cleanup;
672 else
674 if (m != MATCH_YES)
675 goto syntax;
676 strcpy (new_use->local_name, name);
678 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
679 if (type != type2)
680 goto syntax;
681 if (m == MATCH_NO)
682 goto syntax;
683 if (m == MATCH_ERROR)
684 goto cleanup;
687 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
688 if (st && type != INTERFACE_USER_OP
689 && (st->n.sym->module != use_list->module_name
690 || strcmp (st->n.sym->name, new_use->use_name) != 0))
692 if (m == MATCH_YES)
693 gfc_error ("Symbol %qs at %L conflicts with the rename symbol "
694 "at %L", name, &st->n.sym->declared_at, &loc);
695 else
696 gfc_error ("Symbol %qs at %L conflicts with the symbol "
697 "at %L", name, &st->n.sym->declared_at, &loc);
698 goto cleanup;
701 if (strcmp (new_use->use_name, use_list->module_name) == 0
702 || strcmp (new_use->local_name, use_list->module_name) == 0)
704 gfc_error ("The name %qs at %C has already been used as "
705 "an external module name", use_list->module_name);
706 goto cleanup;
708 break;
710 case INTERFACE_INTRINSIC_OP:
711 new_use->op = op;
712 break;
714 default:
715 gcc_unreachable ();
718 if (gfc_match_eos () == MATCH_YES)
719 break;
720 if (gfc_match_char (',') != MATCH_YES)
721 goto syntax;
724 done:
725 if (module_list)
727 gfc_use_list *last = module_list;
728 while (last->next)
729 last = last->next;
730 last->next = use_list;
732 else
733 module_list = use_list;
735 return MATCH_YES;
737 syntax:
738 gfc_syntax_error (ST_USE);
740 cleanup:
741 free_rename (use_list->rename);
742 free (use_list);
743 return MATCH_ERROR;
747 /* Match a SUBMODULE statement.
749 According to F2008:11.2.3.2, "The submodule identifier is the
750 ordered pair whose first element is the ancestor module name and
751 whose second element is the submodule name. 'Submodule_name' is
752 used for the submodule filename and uses '@' as a separator, whilst
753 the name of the symbol for the module uses '.' as a separator.
754 The reasons for these choices are:
755 (i) To follow another leading brand in the submodule filenames;
756 (ii) Since '.' is not particularly visible in the filenames; and
757 (iii) The linker does not permit '@' in mnemonics. */
759 match
760 gfc_match_submodule (void)
762 match m;
763 char name[GFC_MAX_SYMBOL_LEN + 1];
764 gfc_use_list *use_list;
765 bool seen_colon = false;
767 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
768 return MATCH_ERROR;
770 if (gfc_current_state () != COMP_NONE)
772 gfc_error ("SUBMODULE declaration at %C cannot appear within "
773 "another scoping unit");
774 return MATCH_ERROR;
777 gfc_new_block = NULL;
778 gcc_assert (module_list == NULL);
780 if (gfc_match_char ('(') != MATCH_YES)
781 goto syntax;
783 while (1)
785 m = gfc_match (" %n", name);
786 if (m != MATCH_YES)
787 goto syntax;
789 use_list = gfc_get_use_list ();
790 use_list->where = gfc_current_locus;
792 if (module_list)
794 gfc_use_list *last = module_list;
795 while (last->next)
796 last = last->next;
797 last->next = use_list;
798 use_list->module_name
799 = gfc_get_string ("%s.%s", module_list->module_name, name);
800 use_list->submodule_name
801 = gfc_get_string ("%s@%s", module_list->module_name, name);
803 else
805 module_list = use_list;
806 use_list->module_name = gfc_get_string ("%s", name);
807 use_list->submodule_name = use_list->module_name;
810 if (gfc_match_char (')') == MATCH_YES)
811 break;
813 if (gfc_match_char (':') != MATCH_YES
814 || seen_colon)
815 goto syntax;
817 seen_colon = true;
820 m = gfc_match (" %s%t", &gfc_new_block);
821 if (m != MATCH_YES)
822 goto syntax;
824 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
825 gfc_new_block->name);
827 gfc_new_block->name = gfc_get_string ("%s.%s",
828 module_list->module_name,
829 gfc_new_block->name);
831 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
832 gfc_new_block->name, NULL))
833 return MATCH_ERROR;
835 /* Just retain the ultimate .(s)mod file for reading, since it
836 contains all the information in its ancestors. */
837 use_list = module_list;
838 for (; module_list->next; use_list = module_list)
840 module_list = use_list->next;
841 free (use_list);
844 return MATCH_YES;
846 syntax:
847 gfc_error ("Syntax error in SUBMODULE statement at %C");
848 return MATCH_ERROR;
852 /* Given a name and a number, inst, return the inst name
853 under which to load this symbol. Returns NULL if this
854 symbol shouldn't be loaded. If inst is zero, returns
855 the number of instances of this name. If interface is
856 true, a user-defined operator is sought, otherwise only
857 non-operators are sought. */
859 static const char *
860 find_use_name_n (const char *name, int *inst, bool interface)
862 gfc_use_rename *u;
863 const char *low_name = NULL;
864 int i;
866 /* For derived types. */
867 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
868 low_name = gfc_dt_lower_string (name);
870 i = 0;
871 for (u = gfc_rename_list; u; u = u->next)
873 if ((!low_name && strcmp (u->use_name, name) != 0)
874 || (low_name && strcmp (u->use_name, low_name) != 0)
875 || (u->op == INTRINSIC_USER && !interface)
876 || (u->op != INTRINSIC_USER && interface))
877 continue;
878 if (++i == *inst)
879 break;
882 if (!*inst)
884 *inst = i;
885 return NULL;
888 if (u == NULL)
889 return only_flag ? NULL : name;
891 u->found = 1;
893 if (low_name)
895 if (u->local_name[0] == '\0')
896 return name;
897 return gfc_dt_upper_string (u->local_name);
900 return (u->local_name[0] != '\0') ? u->local_name : name;
904 /* Given a name, return the name under which to load this symbol.
905 Returns NULL if this symbol shouldn't be loaded. */
907 static const char *
908 find_use_name (const char *name, bool interface)
910 int i = 1;
911 return find_use_name_n (name, &i, interface);
915 /* Given a real name, return the number of use names associated with it. */
917 static int
918 number_use_names (const char *name, bool interface)
920 int i = 0;
921 find_use_name_n (name, &i, interface);
922 return i;
926 /* Try to find the operator in the current list. */
928 static gfc_use_rename *
929 find_use_operator (gfc_intrinsic_op op)
931 gfc_use_rename *u;
933 for (u = gfc_rename_list; u; u = u->next)
934 if (u->op == op)
935 return u;
937 return NULL;
941 /*****************************************************************/
943 /* The next couple of subroutines maintain a tree used to avoid a
944 brute-force search for a combination of true name and module name.
945 While symtree names, the name that a particular symbol is known by
946 can changed with USE statements, we still have to keep track of the
947 true names to generate the correct reference, and also avoid
948 loading the same real symbol twice in a program unit.
950 When we start reading, the true name tree is built and maintained
951 as symbols are read. The tree is searched as we load new symbols
952 to see if it already exists someplace in the namespace. */
954 typedef struct true_name
956 BBT_HEADER (true_name);
957 const char *name;
958 gfc_symbol *sym;
960 true_name;
962 static true_name *true_name_root;
965 /* Compare two true_name structures. */
967 static int
968 compare_true_names (void *_t1, void *_t2)
970 true_name *t1, *t2;
971 int c;
973 t1 = (true_name *) _t1;
974 t2 = (true_name *) _t2;
976 c = ((t1->sym->module > t2->sym->module)
977 - (t1->sym->module < t2->sym->module));
978 if (c != 0)
979 return c;
981 return strcmp (t1->name, t2->name);
985 /* Given a true name, search the true name tree to see if it exists
986 within the main namespace. */
988 static gfc_symbol *
989 find_true_name (const char *name, const char *module)
991 true_name t, *p;
992 gfc_symbol sym;
993 int c;
995 t.name = gfc_get_string ("%s", name);
996 if (module != NULL)
997 sym.module = gfc_get_string ("%s", module);
998 else
999 sym.module = NULL;
1000 t.sym = &sym;
1002 p = true_name_root;
1003 while (p != NULL)
1005 c = compare_true_names ((void *) (&t), (void *) p);
1006 if (c == 0)
1007 return p->sym;
1009 p = (c < 0) ? p->left : p->right;
1012 return NULL;
1016 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
1018 static void
1019 add_true_name (gfc_symbol *sym)
1021 true_name *t;
1023 t = XCNEW (true_name);
1024 t->sym = sym;
1025 if (gfc_fl_struct (sym->attr.flavor))
1026 t->name = gfc_dt_upper_string (sym->name);
1027 else
1028 t->name = sym->name;
1030 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1034 /* Recursive function to build the initial true name tree by
1035 recursively traversing the current namespace. */
1037 static void
1038 build_tnt (gfc_symtree *st)
1040 const char *name;
1041 if (st == NULL)
1042 return;
1044 build_tnt (st->left);
1045 build_tnt (st->right);
1047 if (gfc_fl_struct (st->n.sym->attr.flavor))
1048 name = gfc_dt_upper_string (st->n.sym->name);
1049 else
1050 name = st->n.sym->name;
1052 if (find_true_name (name, st->n.sym->module) != NULL)
1053 return;
1055 add_true_name (st->n.sym);
1059 /* Initialize the true name tree with the current namespace. */
1061 static void
1062 init_true_name_tree (void)
1064 true_name_root = NULL;
1065 build_tnt (gfc_current_ns->sym_root);
1069 /* Recursively free a true name tree node. */
1071 static void
1072 free_true_name (true_name *t)
1074 if (t == NULL)
1075 return;
1076 free_true_name (t->left);
1077 free_true_name (t->right);
1079 free (t);
1083 /*****************************************************************/
1085 /* Module reading and writing. */
1087 /* The following are versions similar to the ones in scanner.cc, but
1088 for dealing with compressed module files. */
1090 static gzFile
1091 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1092 bool module, bool system)
1094 char *fullname;
1095 gfc_directorylist *p;
1096 gzFile f;
1098 for (p = list; p; p = p->next)
1100 if (module && !p->use_for_modules)
1101 continue;
1103 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
1104 strcpy (fullname, p->path);
1105 strcat (fullname, "/");
1106 strcat (fullname, name);
1108 f = gzopen (fullname, "r");
1109 if (f != NULL)
1111 if (gfc_cpp_makedep ())
1112 gfc_cpp_add_dep (fullname, system);
1114 free (module_fullpath);
1115 module_fullpath = xstrdup (fullname);
1116 return f;
1120 return NULL;
1123 static gzFile
1124 gzopen_included_file (const char *name, bool include_cwd, bool module)
1126 gzFile f = NULL;
1128 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1130 f = gzopen (name, "r");
1131 if (f)
1133 if (gfc_cpp_makedep ())
1134 gfc_cpp_add_dep (name, false);
1136 free (module_fullpath);
1137 module_fullpath = xstrdup (name);
1141 if (!f)
1142 f = gzopen_included_file_1 (name, include_dirs, module, false);
1144 return f;
1147 static gzFile
1148 gzopen_intrinsic_module (const char* name)
1150 gzFile f = NULL;
1152 if (IS_ABSOLUTE_PATH (name))
1154 f = gzopen (name, "r");
1155 if (f)
1157 if (gfc_cpp_makedep ())
1158 gfc_cpp_add_dep (name, true);
1160 free (module_fullpath);
1161 module_fullpath = xstrdup (name);
1165 if (!f)
1166 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1168 return f;
1172 enum atom_type
1174 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1177 static atom_type last_atom;
1180 /* The name buffer must be at least as long as a symbol name. Right
1181 now it's not clear how we're going to store numeric constants--
1182 probably as a hexadecimal string, since this will allow the exact
1183 number to be preserved (this can't be done by a decimal
1184 representation). Worry about that later. TODO! */
1186 #define MAX_ATOM_SIZE 100
1188 static HOST_WIDE_INT atom_int;
1189 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1192 /* Report problems with a module. Error reporting is not very
1193 elaborate, since this sorts of errors shouldn't really happen.
1194 This subroutine never returns. */
1196 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1198 static void
1199 bad_module (const char *msgid)
1201 XDELETEVEC (module_content);
1202 module_content = NULL;
1204 switch (iomode)
1206 case IO_INPUT:
1207 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1208 module_fullpath, module_line, module_column, msgid);
1209 break;
1210 case IO_OUTPUT:
1211 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1212 module_name, module_line, module_column, msgid);
1213 break;
1214 default:
1215 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1216 module_name, module_line, module_column, msgid);
1217 break;
1222 /* Set the module's input pointer. */
1224 static void
1225 set_module_locus (module_locus *m)
1227 module_column = m->column;
1228 module_line = m->line;
1229 module_pos = m->pos;
1233 /* Get the module's input pointer so that we can restore it later. */
1235 static void
1236 get_module_locus (module_locus *m)
1238 m->column = module_column;
1239 m->line = module_line;
1240 m->pos = module_pos;
1243 /* Peek at the next character in the module. */
1245 static int
1246 module_peek_char (void)
1248 return module_content[module_pos];
1251 /* Get the next character in the module, updating our reckoning of
1252 where we are. */
1254 static int
1255 module_char (void)
1257 const char c = module_content[module_pos++];
1258 if (c == '\0')
1259 bad_module ("Unexpected EOF");
1261 prev_module_line = module_line;
1262 prev_module_column = module_column;
1264 if (c == '\n')
1266 module_line++;
1267 module_column = 0;
1270 module_column++;
1271 return c;
1274 /* Unget a character while remembering the line and column. Works for
1275 a single character only. */
1277 static void
1278 module_unget_char (void)
1280 module_line = prev_module_line;
1281 module_column = prev_module_column;
1282 module_pos--;
1285 /* Parse a string constant. The delimiter is guaranteed to be a
1286 single quote. */
1288 static void
1289 parse_string (void)
1291 int c;
1292 size_t cursz = 30;
1293 size_t len = 0;
1295 atom_string = XNEWVEC (char, cursz);
1297 for ( ; ; )
1299 c = module_char ();
1301 if (c == '\'')
1303 int c2 = module_char ();
1304 if (c2 != '\'')
1306 module_unget_char ();
1307 break;
1311 if (len >= cursz)
1313 cursz *= 2;
1314 atom_string = XRESIZEVEC (char, atom_string, cursz);
1316 atom_string[len] = c;
1317 len++;
1320 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1321 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1325 /* Parse an integer. Should fit in a HOST_WIDE_INT. */
1327 static void
1328 parse_integer (int c)
1330 int sign = 1;
1332 atom_int = 0;
1333 switch (c)
1335 case ('-'):
1336 sign = -1;
1337 case ('+'):
1338 break;
1339 default:
1340 atom_int = c - '0';
1341 break;
1344 for (;;)
1346 c = module_char ();
1347 if (!ISDIGIT (c))
1349 module_unget_char ();
1350 break;
1353 atom_int = 10 * atom_int + c - '0';
1356 atom_int *= sign;
1360 /* Parse a name. */
1362 static void
1363 parse_name (int c)
1365 char *p;
1366 int len;
1368 p = atom_name;
1370 *p++ = c;
1371 len = 1;
1373 for (;;)
1375 c = module_char ();
1376 if (!ISALNUM (c) && c != '_' && c != '-')
1378 module_unget_char ();
1379 break;
1382 *p++ = c;
1383 if (++len > GFC_MAX_SYMBOL_LEN)
1384 bad_module ("Name too long");
1387 *p = '\0';
1392 /* Read the next atom in the module's input stream. */
1394 static atom_type
1395 parse_atom (void)
1397 int c;
1401 c = module_char ();
1403 while (c == ' ' || c == '\r' || c == '\n');
1405 switch (c)
1407 case '(':
1408 return ATOM_LPAREN;
1410 case ')':
1411 return ATOM_RPAREN;
1413 case '\'':
1414 parse_string ();
1415 return ATOM_STRING;
1417 case '0':
1418 case '1':
1419 case '2':
1420 case '3':
1421 case '4':
1422 case '5':
1423 case '6':
1424 case '7':
1425 case '8':
1426 case '9':
1427 parse_integer (c);
1428 return ATOM_INTEGER;
1430 case '+':
1431 case '-':
1432 if (ISDIGIT (module_peek_char ()))
1434 parse_integer (c);
1435 return ATOM_INTEGER;
1437 else
1438 bad_module ("Bad name");
1440 case 'a':
1441 case 'b':
1442 case 'c':
1443 case 'd':
1444 case 'e':
1445 case 'f':
1446 case 'g':
1447 case 'h':
1448 case 'i':
1449 case 'j':
1450 case 'k':
1451 case 'l':
1452 case 'm':
1453 case 'n':
1454 case 'o':
1455 case 'p':
1456 case 'q':
1457 case 'r':
1458 case 's':
1459 case 't':
1460 case 'u':
1461 case 'v':
1462 case 'w':
1463 case 'x':
1464 case 'y':
1465 case 'z':
1466 case 'A':
1467 case 'B':
1468 case 'C':
1469 case 'D':
1470 case 'E':
1471 case 'F':
1472 case 'G':
1473 case 'H':
1474 case 'I':
1475 case 'J':
1476 case 'K':
1477 case 'L':
1478 case 'M':
1479 case 'N':
1480 case 'O':
1481 case 'P':
1482 case 'Q':
1483 case 'R':
1484 case 'S':
1485 case 'T':
1486 case 'U':
1487 case 'V':
1488 case 'W':
1489 case 'X':
1490 case 'Y':
1491 case 'Z':
1492 parse_name (c);
1493 return ATOM_NAME;
1495 default:
1496 bad_module ("Bad name");
1499 /* Not reached. */
1503 /* Peek at the next atom on the input. */
1505 static atom_type
1506 peek_atom (void)
1508 int c;
1512 c = module_char ();
1514 while (c == ' ' || c == '\r' || c == '\n');
1516 switch (c)
1518 case '(':
1519 module_unget_char ();
1520 return ATOM_LPAREN;
1522 case ')':
1523 module_unget_char ();
1524 return ATOM_RPAREN;
1526 case '\'':
1527 module_unget_char ();
1528 return ATOM_STRING;
1530 case '0':
1531 case '1':
1532 case '2':
1533 case '3':
1534 case '4':
1535 case '5':
1536 case '6':
1537 case '7':
1538 case '8':
1539 case '9':
1540 module_unget_char ();
1541 return ATOM_INTEGER;
1543 case '+':
1544 case '-':
1545 if (ISDIGIT (module_peek_char ()))
1547 module_unget_char ();
1548 return ATOM_INTEGER;
1550 else
1551 bad_module ("Bad name");
1553 case 'a':
1554 case 'b':
1555 case 'c':
1556 case 'd':
1557 case 'e':
1558 case 'f':
1559 case 'g':
1560 case 'h':
1561 case 'i':
1562 case 'j':
1563 case 'k':
1564 case 'l':
1565 case 'm':
1566 case 'n':
1567 case 'o':
1568 case 'p':
1569 case 'q':
1570 case 'r':
1571 case 's':
1572 case 't':
1573 case 'u':
1574 case 'v':
1575 case 'w':
1576 case 'x':
1577 case 'y':
1578 case 'z':
1579 case 'A':
1580 case 'B':
1581 case 'C':
1582 case 'D':
1583 case 'E':
1584 case 'F':
1585 case 'G':
1586 case 'H':
1587 case 'I':
1588 case 'J':
1589 case 'K':
1590 case 'L':
1591 case 'M':
1592 case 'N':
1593 case 'O':
1594 case 'P':
1595 case 'Q':
1596 case 'R':
1597 case 'S':
1598 case 'T':
1599 case 'U':
1600 case 'V':
1601 case 'W':
1602 case 'X':
1603 case 'Y':
1604 case 'Z':
1605 module_unget_char ();
1606 return ATOM_NAME;
1608 default:
1609 bad_module ("Bad name");
1614 /* Read the next atom from the input, requiring that it be a
1615 particular kind. */
1617 static void
1618 require_atom (atom_type type)
1620 atom_type t;
1621 const char *p;
1622 int column, line;
1624 column = module_column;
1625 line = module_line;
1627 t = parse_atom ();
1628 if (t != type)
1630 switch (type)
1632 case ATOM_NAME:
1633 p = _("Expected name");
1634 break;
1635 case ATOM_LPAREN:
1636 p = _("Expected left parenthesis");
1637 break;
1638 case ATOM_RPAREN:
1639 p = _("Expected right parenthesis");
1640 break;
1641 case ATOM_INTEGER:
1642 p = _("Expected integer");
1643 break;
1644 case ATOM_STRING:
1645 p = _("Expected string");
1646 break;
1647 default:
1648 gfc_internal_error ("require_atom(): bad atom type required");
1651 module_column = column;
1652 module_line = line;
1653 bad_module (p);
1658 /* Given a pointer to an mstring array, require that the current input
1659 be one of the strings in the array. We return the enum value. */
1661 static int
1662 find_enum (const mstring *m)
1664 int i;
1666 i = gfc_string2code (m, atom_name);
1667 if (i >= 0)
1668 return i;
1670 bad_module ("find_enum(): Enum not found");
1672 /* Not reached. */
1676 /* Read a string. The caller is responsible for freeing. */
1678 static char*
1679 read_string (void)
1681 char* p;
1682 require_atom (ATOM_STRING);
1683 p = atom_string;
1684 atom_string = NULL;
1685 return p;
1689 /**************** Module output subroutines ***************************/
1691 /* Output a character to a module file. */
1693 static void
1694 write_char (char out)
1696 if (gzputc (module_fp, out) == EOF)
1697 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1699 if (out != '\n')
1700 module_column++;
1701 else
1703 module_column = 1;
1704 module_line++;
1709 /* Write an atom to a module. The line wrapping isn't perfect, but it
1710 should work most of the time. This isn't that big of a deal, since
1711 the file really isn't meant to be read by people anyway. */
1713 static void
1714 write_atom (atom_type atom, const void *v)
1716 char buffer[32];
1718 /* Workaround -Wmaybe-uninitialized false positive during
1719 profiledbootstrap by initializing them. */
1720 int len;
1721 HOST_WIDE_INT i = 0;
1722 const char *p;
1724 switch (atom)
1726 case ATOM_STRING:
1727 case ATOM_NAME:
1728 p = (const char *) v;
1729 break;
1731 case ATOM_LPAREN:
1732 p = "(";
1733 break;
1735 case ATOM_RPAREN:
1736 p = ")";
1737 break;
1739 case ATOM_INTEGER:
1740 i = *((const HOST_WIDE_INT *) v);
1742 snprintf (buffer, sizeof (buffer), HOST_WIDE_INT_PRINT_DEC, i);
1743 p = buffer;
1744 break;
1746 default:
1747 gfc_internal_error ("write_atom(): Trying to write dab atom");
1751 if(p == NULL || *p == '\0')
1752 len = 0;
1753 else
1754 len = strlen (p);
1756 if (atom != ATOM_RPAREN)
1758 if (module_column + len > 72)
1759 write_char ('\n');
1760 else
1763 if (last_atom != ATOM_LPAREN && module_column != 1)
1764 write_char (' ');
1768 if (atom == ATOM_STRING)
1769 write_char ('\'');
1771 while (p != NULL && *p)
1773 if (atom == ATOM_STRING && *p == '\'')
1774 write_char ('\'');
1775 write_char (*p++);
1778 if (atom == ATOM_STRING)
1779 write_char ('\'');
1781 last_atom = atom;
1786 /***************** Mid-level I/O subroutines *****************/
1788 /* These subroutines let their caller read or write atoms without
1789 caring about which of the two is actually happening. This lets a
1790 subroutine concentrate on the actual format of the data being
1791 written. */
1793 static void mio_expr (gfc_expr **);
1794 pointer_info *mio_symbol_ref (gfc_symbol **);
1795 pointer_info *mio_interface_rest (gfc_interface **);
1796 static void mio_symtree_ref (gfc_symtree **);
1798 /* Read or write an enumerated value. On writing, we return the input
1799 value for the convenience of callers. We avoid using an integer
1800 pointer because enums are sometimes inside bitfields. */
1802 static int
1803 mio_name (int t, const mstring *m)
1805 if (iomode == IO_OUTPUT)
1806 write_atom (ATOM_NAME, gfc_code2string (m, t));
1807 else
1809 require_atom (ATOM_NAME);
1810 t = find_enum (m);
1813 return t;
1816 /* Specialization of mio_name. */
1818 #define DECL_MIO_NAME(TYPE) \
1819 static inline TYPE \
1820 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1822 return (TYPE) mio_name ((int) t, m); \
1824 #define MIO_NAME(TYPE) mio_name_##TYPE
1826 static void
1827 mio_lparen (void)
1829 if (iomode == IO_OUTPUT)
1830 write_atom (ATOM_LPAREN, NULL);
1831 else
1832 require_atom (ATOM_LPAREN);
1836 static void
1837 mio_rparen (void)
1839 if (iomode == IO_OUTPUT)
1840 write_atom (ATOM_RPAREN, NULL);
1841 else
1842 require_atom (ATOM_RPAREN);
1846 static void
1847 mio_integer (int *ip)
1849 if (iomode == IO_OUTPUT)
1851 HOST_WIDE_INT hwi = *ip;
1852 write_atom (ATOM_INTEGER, &hwi);
1854 else
1856 require_atom (ATOM_INTEGER);
1857 *ip = atom_int;
1861 static void
1862 mio_hwi (HOST_WIDE_INT *hwi)
1864 if (iomode == IO_OUTPUT)
1865 write_atom (ATOM_INTEGER, hwi);
1866 else
1868 require_atom (ATOM_INTEGER);
1869 *hwi = atom_int;
1874 /* Read or write a gfc_intrinsic_op value. */
1876 static void
1877 mio_intrinsic_op (gfc_intrinsic_op* op)
1879 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1880 if (iomode == IO_OUTPUT)
1882 HOST_WIDE_INT converted = (HOST_WIDE_INT) *op;
1883 write_atom (ATOM_INTEGER, &converted);
1885 else
1887 require_atom (ATOM_INTEGER);
1888 *op = (gfc_intrinsic_op) atom_int;
1893 /* Read or write a character pointer that points to a string on the heap. */
1895 static const char *
1896 mio_allocated_string (const char *s)
1898 if (iomode == IO_OUTPUT)
1900 write_atom (ATOM_STRING, s);
1901 return s;
1903 else
1905 require_atom (ATOM_STRING);
1906 return atom_string;
1911 /* Functions for quoting and unquoting strings. */
1913 static char *
1914 quote_string (const gfc_char_t *s, const size_t slength)
1916 const gfc_char_t *p;
1917 char *res, *q;
1918 size_t len = 0, i;
1920 /* Calculate the length we'll need: a backslash takes two ("\\"),
1921 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1922 for (p = s, i = 0; i < slength; p++, i++)
1924 if (*p == '\\')
1925 len += 2;
1926 else if (!gfc_wide_is_printable (*p))
1927 len += 10;
1928 else
1929 len++;
1932 q = res = XCNEWVEC (char, len + 1);
1933 for (p = s, i = 0; i < slength; p++, i++)
1935 if (*p == '\\')
1936 *q++ = '\\', *q++ = '\\';
1937 else if (!gfc_wide_is_printable (*p))
1939 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1940 (unsigned HOST_WIDE_INT) *p);
1941 q += 10;
1943 else
1944 *q++ = (unsigned char) *p;
1947 res[len] = '\0';
1948 return res;
1951 static gfc_char_t *
1952 unquote_string (const char *s)
1954 size_t len, i;
1955 const char *p;
1956 gfc_char_t *res;
1958 for (p = s, len = 0; *p; p++, len++)
1960 if (*p != '\\')
1961 continue;
1963 if (p[1] == '\\')
1964 p++;
1965 else if (p[1] == 'U')
1966 p += 9; /* That is a "\U????????". */
1967 else
1968 gfc_internal_error ("unquote_string(): got bad string");
1971 res = gfc_get_wide_string (len + 1);
1972 for (i = 0, p = s; i < len; i++, p++)
1974 gcc_assert (*p);
1976 if (*p != '\\')
1977 res[i] = (unsigned char) *p;
1978 else if (p[1] == '\\')
1980 res[i] = (unsigned char) '\\';
1981 p++;
1983 else
1985 /* We read the 8-digits hexadecimal constant that follows. */
1986 int j;
1987 unsigned n;
1988 gfc_char_t c = 0;
1990 gcc_assert (p[1] == 'U');
1991 for (j = 0; j < 8; j++)
1993 c = c << 4;
1994 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1995 c += n;
1998 res[i] = c;
1999 p += 9;
2003 res[len] = '\0';
2004 return res;
2008 /* Read or write a character pointer that points to a wide string on the
2009 heap, performing quoting/unquoting of nonprintable characters using the
2010 form \U???????? (where each ? is a hexadecimal digit).
2011 Length is the length of the string, only known and used in output mode. */
2013 static const gfc_char_t *
2014 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
2016 if (iomode == IO_OUTPUT)
2018 char *quoted = quote_string (s, length);
2019 write_atom (ATOM_STRING, quoted);
2020 free (quoted);
2021 return s;
2023 else
2025 gfc_char_t *unquoted;
2027 require_atom (ATOM_STRING);
2028 unquoted = unquote_string (atom_string);
2029 free (atom_string);
2030 return unquoted;
2035 /* Read or write a string that is in static memory. */
2037 static void
2038 mio_pool_string (const char **stringp)
2040 /* TODO: one could write the string only once, and refer to it via a
2041 fixup pointer. */
2043 /* As a special case we have to deal with a NULL string. This
2044 happens for the 'module' member of 'gfc_symbol's that are not in a
2045 module. We read / write these as the empty string. */
2046 if (iomode == IO_OUTPUT)
2048 const char *p = *stringp == NULL ? "" : *stringp;
2049 write_atom (ATOM_STRING, p);
2051 else
2053 require_atom (ATOM_STRING);
2054 *stringp = (atom_string[0] == '\0'
2055 ? NULL : gfc_get_string ("%s", atom_string));
2056 free (atom_string);
2061 /* Read or write a string that is inside of some already-allocated
2062 structure. */
2064 static void
2065 mio_internal_string (char *string)
2067 if (iomode == IO_OUTPUT)
2068 write_atom (ATOM_STRING, string);
2069 else
2071 require_atom (ATOM_STRING);
2072 strcpy (string, atom_string);
2073 free (atom_string);
2078 enum ab_attribute
2079 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
2080 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
2081 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
2082 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
2083 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
2084 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
2085 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
2086 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
2087 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
2088 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
2089 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
2090 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
2091 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
2092 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK,
2093 AB_OMP_DECLARE_TARGET_LINK, AB_PDT_KIND, AB_PDT_LEN, AB_PDT_TYPE,
2094 AB_PDT_TEMPLATE, AB_PDT_ARRAY, AB_PDT_STRING,
2095 AB_OACC_ROUTINE_LOP_GANG, AB_OACC_ROUTINE_LOP_WORKER,
2096 AB_OACC_ROUTINE_LOP_VECTOR, AB_OACC_ROUTINE_LOP_SEQ,
2097 AB_OACC_ROUTINE_NOHOST,
2098 AB_OMP_REQ_REVERSE_OFFLOAD, AB_OMP_REQ_UNIFIED_ADDRESS, AB_OMP_REQ_SELF_MAPS,
2099 AB_OMP_REQ_UNIFIED_SHARED_MEMORY, AB_OMP_REQ_DYNAMIC_ALLOCATORS,
2100 AB_OMP_REQ_MEM_ORDER_SEQ_CST, AB_OMP_REQ_MEM_ORDER_ACQ_REL,
2101 AB_OMP_REQ_MEM_ORDER_ACQUIRE, AB_OMP_REQ_MEM_ORDER_RELEASE,
2102 AB_OMP_REQ_MEM_ORDER_RELAXED, AB_OMP_DEVICE_TYPE_NOHOST,
2103 AB_OMP_DEVICE_TYPE_HOST, AB_OMP_DEVICE_TYPE_ANY
2106 static const mstring attr_bits[] =
2108 minit ("ALLOCATABLE", AB_ALLOCATABLE),
2109 minit ("ARTIFICIAL", AB_ARTIFICIAL),
2110 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
2111 minit ("DIMENSION", AB_DIMENSION),
2112 minit ("CODIMENSION", AB_CODIMENSION),
2113 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2114 minit ("EXTERNAL", AB_EXTERNAL),
2115 minit ("INTRINSIC", AB_INTRINSIC),
2116 minit ("OPTIONAL", AB_OPTIONAL),
2117 minit ("POINTER", AB_POINTER),
2118 minit ("VOLATILE", AB_VOLATILE),
2119 minit ("TARGET", AB_TARGET),
2120 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2121 minit ("DUMMY", AB_DUMMY),
2122 minit ("RESULT", AB_RESULT),
2123 minit ("DATA", AB_DATA),
2124 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2125 minit ("IN_COMMON", AB_IN_COMMON),
2126 minit ("FUNCTION", AB_FUNCTION),
2127 minit ("SUBROUTINE", AB_SUBROUTINE),
2128 minit ("SEQUENCE", AB_SEQUENCE),
2129 minit ("ELEMENTAL", AB_ELEMENTAL),
2130 minit ("PURE", AB_PURE),
2131 minit ("RECURSIVE", AB_RECURSIVE),
2132 minit ("GENERIC", AB_GENERIC),
2133 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2134 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2135 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2136 minit ("IS_BIND_C", AB_IS_BIND_C),
2137 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2138 minit ("IS_ISO_C", AB_IS_ISO_C),
2139 minit ("VALUE", AB_VALUE),
2140 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2141 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2142 minit ("LOCK_COMP", AB_LOCK_COMP),
2143 minit ("EVENT_COMP", AB_EVENT_COMP),
2144 minit ("POINTER_COMP", AB_POINTER_COMP),
2145 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2146 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2147 minit ("ZERO_COMP", AB_ZERO_COMP),
2148 minit ("PROTECTED", AB_PROTECTED),
2149 minit ("ABSTRACT", AB_ABSTRACT),
2150 minit ("IS_CLASS", AB_IS_CLASS),
2151 minit ("PROCEDURE", AB_PROCEDURE),
2152 minit ("PROC_POINTER", AB_PROC_POINTER),
2153 minit ("VTYPE", AB_VTYPE),
2154 minit ("VTAB", AB_VTAB),
2155 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2156 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2157 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2158 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2159 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2160 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2161 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2162 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2163 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2164 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2165 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2166 minit ("OMP_DECLARE_TARGET_LINK", AB_OMP_DECLARE_TARGET_LINK),
2167 minit ("PDT_KIND", AB_PDT_KIND),
2168 minit ("PDT_LEN", AB_PDT_LEN),
2169 minit ("PDT_TYPE", AB_PDT_TYPE),
2170 minit ("PDT_TEMPLATE", AB_PDT_TEMPLATE),
2171 minit ("PDT_ARRAY", AB_PDT_ARRAY),
2172 minit ("PDT_STRING", AB_PDT_STRING),
2173 minit ("OACC_ROUTINE_LOP_GANG", AB_OACC_ROUTINE_LOP_GANG),
2174 minit ("OACC_ROUTINE_LOP_WORKER", AB_OACC_ROUTINE_LOP_WORKER),
2175 minit ("OACC_ROUTINE_LOP_VECTOR", AB_OACC_ROUTINE_LOP_VECTOR),
2176 minit ("OACC_ROUTINE_LOP_SEQ", AB_OACC_ROUTINE_LOP_SEQ),
2177 minit ("OACC_ROUTINE_NOHOST", AB_OACC_ROUTINE_NOHOST),
2178 minit ("OMP_REQ_REVERSE_OFFLOAD", AB_OMP_REQ_REVERSE_OFFLOAD),
2179 minit ("OMP_REQ_UNIFIED_ADDRESS", AB_OMP_REQ_UNIFIED_ADDRESS),
2180 minit ("OMP_REQ_UNIFIED_SHARED_MEMORY", AB_OMP_REQ_UNIFIED_SHARED_MEMORY),
2181 minit ("OMP_REQ_SELF_MAPS", AB_OMP_REQ_SELF_MAPS),
2182 minit ("OMP_REQ_DYNAMIC_ALLOCATORS", AB_OMP_REQ_DYNAMIC_ALLOCATORS),
2183 minit ("OMP_REQ_MEM_ORDER_SEQ_CST", AB_OMP_REQ_MEM_ORDER_SEQ_CST),
2184 minit ("OMP_REQ_MEM_ORDER_ACQ_REL", AB_OMP_REQ_MEM_ORDER_ACQ_REL),
2185 minit ("OMP_REQ_MEM_ORDER_ACQUIRE", AB_OMP_REQ_MEM_ORDER_ACQUIRE),
2186 minit ("OMP_REQ_MEM_ORDER_RELAXED", AB_OMP_REQ_MEM_ORDER_RELAXED),
2187 minit ("OMP_REQ_MEM_ORDER_RELEASE", AB_OMP_REQ_MEM_ORDER_RELEASE),
2188 minit ("OMP_DEVICE_TYPE_HOST", AB_OMP_DEVICE_TYPE_HOST),
2189 minit ("OMP_DEVICE_TYPE_NOHOST", AB_OMP_DEVICE_TYPE_NOHOST),
2190 minit ("OMP_DEVICE_TYPE_ANYHOST", AB_OMP_DEVICE_TYPE_ANY),
2191 minit (NULL, -1)
2194 /* For binding attributes. */
2195 static const mstring binding_passing[] =
2197 minit ("PASS", 0),
2198 minit ("NOPASS", 1),
2199 minit (NULL, -1)
2201 static const mstring binding_overriding[] =
2203 minit ("OVERRIDABLE", 0),
2204 minit ("NON_OVERRIDABLE", 1),
2205 minit ("DEFERRED", 2),
2206 minit (NULL, -1)
2208 static const mstring binding_generic[] =
2210 minit ("SPECIFIC", 0),
2211 minit ("GENERIC", 1),
2212 minit (NULL, -1)
2214 static const mstring binding_ppc[] =
2216 minit ("NO_PPC", 0),
2217 minit ("PPC", 1),
2218 minit (NULL, -1)
2221 /* Specialization of mio_name. */
2222 DECL_MIO_NAME (ab_attribute)
2223 DECL_MIO_NAME (ar_type)
2224 DECL_MIO_NAME (array_type)
2225 DECL_MIO_NAME (bt)
2226 DECL_MIO_NAME (expr_t)
2227 DECL_MIO_NAME (gfc_access)
2228 DECL_MIO_NAME (gfc_intrinsic_op)
2229 DECL_MIO_NAME (ifsrc)
2230 DECL_MIO_NAME (save_state)
2231 DECL_MIO_NAME (procedure_type)
2232 DECL_MIO_NAME (ref_type)
2233 DECL_MIO_NAME (sym_flavor)
2234 DECL_MIO_NAME (sym_intent)
2235 DECL_MIO_NAME (inquiry_type)
2236 #undef DECL_MIO_NAME
2238 /* Verify OACC_ROUTINE_LOP_NONE. */
2240 static void
2241 verify_OACC_ROUTINE_LOP_NONE (enum oacc_routine_lop lop)
2243 if (lop != OACC_ROUTINE_LOP_NONE)
2244 bad_module ("Unsupported: multiple OpenACC 'routine' levels of parallelism");
2247 /* Symbol attributes are stored in list with the first three elements
2248 being the enumerated fields, while the remaining elements (if any)
2249 indicate the individual attribute bits. The access field is not
2250 saved-- it controls what symbols are exported when a module is
2251 written. */
2253 static void
2254 mio_symbol_attribute (symbol_attribute *attr)
2256 atom_type t;
2257 unsigned ext_attr,extension_level;
2259 mio_lparen ();
2261 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2262 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2263 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2264 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2265 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2267 ext_attr = attr->ext_attr;
2268 mio_integer ((int *) &ext_attr);
2269 attr->ext_attr = ext_attr;
2271 extension_level = attr->extension;
2272 mio_integer ((int *) &extension_level);
2273 attr->extension = extension_level;
2275 if (iomode == IO_OUTPUT)
2277 if (attr->allocatable)
2278 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2279 if (attr->artificial)
2280 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2281 if (attr->asynchronous)
2282 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2283 if (attr->dimension)
2284 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2285 if (attr->codimension)
2286 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2287 if (attr->contiguous)
2288 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2289 if (attr->external)
2290 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2291 if (attr->intrinsic)
2292 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2293 if (attr->optional)
2294 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2295 if (attr->pointer)
2296 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2297 if (attr->class_pointer)
2298 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2299 if (attr->is_protected)
2300 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2301 if (attr->value)
2302 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2303 if (attr->volatile_)
2304 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2305 if (attr->target)
2306 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2307 if (attr->threadprivate)
2308 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2309 if (attr->dummy)
2310 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2311 if (attr->result)
2312 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2313 /* We deliberately don't preserve the "entry" flag. */
2315 if (attr->data)
2316 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2317 if (attr->in_namelist)
2318 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2319 if (attr->in_common)
2320 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2322 if (attr->function)
2323 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2324 if (attr->subroutine)
2325 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2326 if (attr->generic)
2327 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2328 if (attr->abstract)
2329 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2331 if (attr->sequence)
2332 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2333 if (attr->elemental)
2334 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2335 if (attr->pure)
2336 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2337 if (attr->implicit_pure)
2338 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2339 if (attr->unlimited_polymorphic)
2340 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2341 if (attr->recursive)
2342 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2343 if (attr->always_explicit)
2344 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2345 if (attr->cray_pointer)
2346 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2347 if (attr->cray_pointee)
2348 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2349 if (attr->is_bind_c)
2350 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2351 if (attr->is_c_interop)
2352 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2353 if (attr->is_iso_c)
2354 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2355 if (attr->alloc_comp)
2356 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2357 if (attr->pointer_comp)
2358 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2359 if (attr->proc_pointer_comp)
2360 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2361 if (attr->private_comp)
2362 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2363 if (attr->coarray_comp)
2364 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2365 if (attr->lock_comp)
2366 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2367 if (attr->event_comp)
2368 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2369 if (attr->zero_comp)
2370 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2371 if (attr->is_class)
2372 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2373 if (attr->procedure)
2374 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2375 if (attr->proc_pointer)
2376 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2377 if (attr->vtype)
2378 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2379 if (attr->vtab)
2380 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2381 if (attr->omp_declare_target)
2382 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2383 if (attr->array_outer_dependency)
2384 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2385 if (attr->module_procedure)
2386 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2387 if (attr->oacc_declare_create)
2388 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2389 if (attr->oacc_declare_copyin)
2390 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2391 if (attr->oacc_declare_deviceptr)
2392 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2393 if (attr->oacc_declare_device_resident)
2394 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2395 if (attr->oacc_declare_link)
2396 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2397 if (attr->omp_declare_target_link)
2398 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET_LINK, attr_bits);
2399 if (attr->pdt_kind)
2400 MIO_NAME (ab_attribute) (AB_PDT_KIND, attr_bits);
2401 if (attr->pdt_len)
2402 MIO_NAME (ab_attribute) (AB_PDT_LEN, attr_bits);
2403 if (attr->pdt_type)
2404 MIO_NAME (ab_attribute) (AB_PDT_TYPE, attr_bits);
2405 if (attr->pdt_template)
2406 MIO_NAME (ab_attribute) (AB_PDT_TEMPLATE, attr_bits);
2407 if (attr->pdt_array)
2408 MIO_NAME (ab_attribute) (AB_PDT_ARRAY, attr_bits);
2409 if (attr->pdt_string)
2410 MIO_NAME (ab_attribute) (AB_PDT_STRING, attr_bits);
2411 switch (attr->oacc_routine_lop)
2413 case OACC_ROUTINE_LOP_NONE:
2414 /* This is the default anyway, and for maintaining compatibility with
2415 the current MOD_VERSION, we're not emitting anything in that
2416 case. */
2417 break;
2418 case OACC_ROUTINE_LOP_GANG:
2419 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_GANG, attr_bits);
2420 break;
2421 case OACC_ROUTINE_LOP_WORKER:
2422 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_WORKER, attr_bits);
2423 break;
2424 case OACC_ROUTINE_LOP_VECTOR:
2425 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_VECTOR, attr_bits);
2426 break;
2427 case OACC_ROUTINE_LOP_SEQ:
2428 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_SEQ, attr_bits);
2429 break;
2430 case OACC_ROUTINE_LOP_ERROR:
2431 /* ... intentionally omitted here; it's only used internally. */
2432 default:
2433 gcc_unreachable ();
2435 if (attr->oacc_routine_nohost)
2436 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_NOHOST, attr_bits);
2438 if (attr->flavor == FL_MODULE && gfc_current_ns->omp_requires)
2440 if (gfc_current_ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD)
2441 MIO_NAME (ab_attribute) (AB_OMP_REQ_REVERSE_OFFLOAD, attr_bits);
2442 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_ADDRESS)
2443 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_ADDRESS, attr_bits);
2444 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
2445 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_SHARED_MEMORY, attr_bits);
2446 if (gfc_current_ns->omp_requires & OMP_REQ_SELF_MAPS)
2447 MIO_NAME (ab_attribute) (AB_OMP_REQ_SELF_MAPS, attr_bits);
2448 if (gfc_current_ns->omp_requires & OMP_REQ_DYNAMIC_ALLOCATORS)
2449 MIO_NAME (ab_attribute) (AB_OMP_REQ_DYNAMIC_ALLOCATORS, attr_bits);
2450 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2451 == OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST)
2452 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_SEQ_CST, attr_bits);
2453 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2454 == OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL)
2455 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQ_REL, attr_bits);
2456 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2457 == OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE)
2458 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQUIRE, attr_bits);
2459 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2460 == OMP_REQ_ATOMIC_MEM_ORDER_RELAXED)
2461 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELAXED, attr_bits);
2462 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2463 == OMP_REQ_ATOMIC_MEM_ORDER_RELEASE)
2464 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELEASE, attr_bits);
2466 switch (attr->omp_device_type)
2468 case OMP_DEVICE_TYPE_UNSET:
2469 break;
2470 case OMP_DEVICE_TYPE_HOST:
2471 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_HOST, attr_bits);
2472 break;
2473 case OMP_DEVICE_TYPE_NOHOST:
2474 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_NOHOST, attr_bits);
2475 break;
2476 case OMP_DEVICE_TYPE_ANY:
2477 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_ANY, attr_bits);
2478 break;
2479 default:
2480 gcc_unreachable ();
2482 mio_rparen ();
2484 else
2486 for (;;)
2488 t = parse_atom ();
2489 if (t == ATOM_RPAREN)
2490 break;
2491 if (t != ATOM_NAME)
2492 bad_module ("Expected attribute bit name");
2494 switch ((ab_attribute) find_enum (attr_bits))
2496 case AB_ALLOCATABLE:
2497 attr->allocatable = 1;
2498 break;
2499 case AB_ARTIFICIAL:
2500 attr->artificial = 1;
2501 break;
2502 case AB_ASYNCHRONOUS:
2503 attr->asynchronous = 1;
2504 break;
2505 case AB_DIMENSION:
2506 attr->dimension = 1;
2507 break;
2508 case AB_CODIMENSION:
2509 attr->codimension = 1;
2510 break;
2511 case AB_CONTIGUOUS:
2512 attr->contiguous = 1;
2513 break;
2514 case AB_EXTERNAL:
2515 attr->external = 1;
2516 break;
2517 case AB_INTRINSIC:
2518 attr->intrinsic = 1;
2519 break;
2520 case AB_OPTIONAL:
2521 attr->optional = 1;
2522 break;
2523 case AB_POINTER:
2524 attr->pointer = 1;
2525 break;
2526 case AB_CLASS_POINTER:
2527 attr->class_pointer = 1;
2528 break;
2529 case AB_PROTECTED:
2530 attr->is_protected = 1;
2531 break;
2532 case AB_VALUE:
2533 attr->value = 1;
2534 break;
2535 case AB_VOLATILE:
2536 attr->volatile_ = 1;
2537 break;
2538 case AB_TARGET:
2539 attr->target = 1;
2540 break;
2541 case AB_THREADPRIVATE:
2542 attr->threadprivate = 1;
2543 break;
2544 case AB_DUMMY:
2545 attr->dummy = 1;
2546 break;
2547 case AB_RESULT:
2548 attr->result = 1;
2549 break;
2550 case AB_DATA:
2551 attr->data = 1;
2552 break;
2553 case AB_IN_NAMELIST:
2554 attr->in_namelist = 1;
2555 break;
2556 case AB_IN_COMMON:
2557 attr->in_common = 1;
2558 break;
2559 case AB_FUNCTION:
2560 attr->function = 1;
2561 break;
2562 case AB_SUBROUTINE:
2563 attr->subroutine = 1;
2564 break;
2565 case AB_GENERIC:
2566 attr->generic = 1;
2567 break;
2568 case AB_ABSTRACT:
2569 attr->abstract = 1;
2570 break;
2571 case AB_SEQUENCE:
2572 attr->sequence = 1;
2573 break;
2574 case AB_ELEMENTAL:
2575 attr->elemental = 1;
2576 break;
2577 case AB_PURE:
2578 attr->pure = 1;
2579 break;
2580 case AB_IMPLICIT_PURE:
2581 attr->implicit_pure = 1;
2582 break;
2583 case AB_UNLIMITED_POLY:
2584 attr->unlimited_polymorphic = 1;
2585 break;
2586 case AB_RECURSIVE:
2587 attr->recursive = 1;
2588 break;
2589 case AB_ALWAYS_EXPLICIT:
2590 attr->always_explicit = 1;
2591 break;
2592 case AB_CRAY_POINTER:
2593 attr->cray_pointer = 1;
2594 break;
2595 case AB_CRAY_POINTEE:
2596 attr->cray_pointee = 1;
2597 break;
2598 case AB_IS_BIND_C:
2599 attr->is_bind_c = 1;
2600 break;
2601 case AB_IS_C_INTEROP:
2602 attr->is_c_interop = 1;
2603 break;
2604 case AB_IS_ISO_C:
2605 attr->is_iso_c = 1;
2606 break;
2607 case AB_ALLOC_COMP:
2608 attr->alloc_comp = 1;
2609 break;
2610 case AB_COARRAY_COMP:
2611 attr->coarray_comp = 1;
2612 break;
2613 case AB_LOCK_COMP:
2614 attr->lock_comp = 1;
2615 break;
2616 case AB_EVENT_COMP:
2617 attr->event_comp = 1;
2618 break;
2619 case AB_POINTER_COMP:
2620 attr->pointer_comp = 1;
2621 break;
2622 case AB_PROC_POINTER_COMP:
2623 attr->proc_pointer_comp = 1;
2624 break;
2625 case AB_PRIVATE_COMP:
2626 attr->private_comp = 1;
2627 break;
2628 case AB_ZERO_COMP:
2629 attr->zero_comp = 1;
2630 break;
2631 case AB_IS_CLASS:
2632 attr->is_class = 1;
2633 break;
2634 case AB_PROCEDURE:
2635 attr->procedure = 1;
2636 break;
2637 case AB_PROC_POINTER:
2638 attr->proc_pointer = 1;
2639 break;
2640 case AB_VTYPE:
2641 attr->vtype = 1;
2642 break;
2643 case AB_VTAB:
2644 attr->vtab = 1;
2645 break;
2646 case AB_OMP_DECLARE_TARGET:
2647 attr->omp_declare_target = 1;
2648 break;
2649 case AB_OMP_DECLARE_TARGET_LINK:
2650 attr->omp_declare_target_link = 1;
2651 break;
2652 case AB_ARRAY_OUTER_DEPENDENCY:
2653 attr->array_outer_dependency =1;
2654 break;
2655 case AB_MODULE_PROCEDURE:
2656 attr->module_procedure =1;
2657 break;
2658 case AB_OACC_DECLARE_CREATE:
2659 attr->oacc_declare_create = 1;
2660 break;
2661 case AB_OACC_DECLARE_COPYIN:
2662 attr->oacc_declare_copyin = 1;
2663 break;
2664 case AB_OACC_DECLARE_DEVICEPTR:
2665 attr->oacc_declare_deviceptr = 1;
2666 break;
2667 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2668 attr->oacc_declare_device_resident = 1;
2669 break;
2670 case AB_OACC_DECLARE_LINK:
2671 attr->oacc_declare_link = 1;
2672 break;
2673 case AB_PDT_KIND:
2674 attr->pdt_kind = 1;
2675 break;
2676 case AB_PDT_LEN:
2677 attr->pdt_len = 1;
2678 break;
2679 case AB_PDT_TYPE:
2680 attr->pdt_type = 1;
2681 break;
2682 case AB_PDT_TEMPLATE:
2683 attr->pdt_template = 1;
2684 break;
2685 case AB_PDT_ARRAY:
2686 attr->pdt_array = 1;
2687 break;
2688 case AB_PDT_STRING:
2689 attr->pdt_string = 1;
2690 break;
2691 case AB_OACC_ROUTINE_LOP_GANG:
2692 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2693 attr->oacc_routine_lop = OACC_ROUTINE_LOP_GANG;
2694 break;
2695 case AB_OACC_ROUTINE_LOP_WORKER:
2696 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2697 attr->oacc_routine_lop = OACC_ROUTINE_LOP_WORKER;
2698 break;
2699 case AB_OACC_ROUTINE_LOP_VECTOR:
2700 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2701 attr->oacc_routine_lop = OACC_ROUTINE_LOP_VECTOR;
2702 break;
2703 case AB_OACC_ROUTINE_LOP_SEQ:
2704 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2705 attr->oacc_routine_lop = OACC_ROUTINE_LOP_SEQ;
2706 break;
2707 case AB_OACC_ROUTINE_NOHOST:
2708 attr->oacc_routine_nohost = 1;
2709 break;
2710 case AB_OMP_REQ_REVERSE_OFFLOAD:
2711 gfc_omp_requires_add_clause (OMP_REQ_REVERSE_OFFLOAD,
2712 "reverse_offload",
2713 &gfc_current_locus,
2714 module_name);
2715 break;
2716 case AB_OMP_REQ_UNIFIED_ADDRESS:
2717 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_ADDRESS,
2718 "unified_address",
2719 &gfc_current_locus,
2720 module_name);
2721 break;
2722 case AB_OMP_REQ_UNIFIED_SHARED_MEMORY:
2723 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_SHARED_MEMORY,
2724 "unified_shared_memory",
2725 &gfc_current_locus,
2726 module_name);
2727 break;
2728 case AB_OMP_REQ_SELF_MAPS:
2729 gfc_omp_requires_add_clause (OMP_REQ_SELF_MAPS,
2730 "self_maps",
2731 &gfc_current_locus,
2732 module_name);
2733 break;
2734 case AB_OMP_REQ_DYNAMIC_ALLOCATORS:
2735 gfc_omp_requires_add_clause (OMP_REQ_DYNAMIC_ALLOCATORS,
2736 "dynamic_allocators",
2737 &gfc_current_locus,
2738 module_name);
2739 break;
2740 case AB_OMP_REQ_MEM_ORDER_SEQ_CST:
2741 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST,
2742 "seq_cst", &gfc_current_locus,
2743 module_name);
2744 break;
2745 case AB_OMP_REQ_MEM_ORDER_ACQ_REL:
2746 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL,
2747 "acq_rel", &gfc_current_locus,
2748 module_name);
2749 break;
2750 case AB_OMP_REQ_MEM_ORDER_ACQUIRE:
2751 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE,
2752 "acquires", &gfc_current_locus,
2753 module_name);
2754 break;
2755 case AB_OMP_REQ_MEM_ORDER_RELAXED:
2756 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELAXED,
2757 "relaxed", &gfc_current_locus,
2758 module_name);
2759 break;
2760 case AB_OMP_REQ_MEM_ORDER_RELEASE:
2761 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELEASE,
2762 "release", &gfc_current_locus,
2763 module_name);
2764 break;
2765 case AB_OMP_DEVICE_TYPE_HOST:
2766 attr->omp_device_type = OMP_DEVICE_TYPE_HOST;
2767 break;
2768 case AB_OMP_DEVICE_TYPE_NOHOST:
2769 attr->omp_device_type = OMP_DEVICE_TYPE_NOHOST;
2770 break;
2771 case AB_OMP_DEVICE_TYPE_ANY:
2772 attr->omp_device_type = OMP_DEVICE_TYPE_ANY;
2773 break;
2780 static const mstring bt_types[] = {
2781 minit ("INTEGER", BT_INTEGER),
2782 minit ("REAL", BT_REAL),
2783 minit ("COMPLEX", BT_COMPLEX),
2784 minit ("LOGICAL", BT_LOGICAL),
2785 minit ("CHARACTER", BT_CHARACTER),
2786 minit ("UNION", BT_UNION),
2787 minit ("DERIVED", BT_DERIVED),
2788 minit ("CLASS", BT_CLASS),
2789 minit ("PROCEDURE", BT_PROCEDURE),
2790 minit ("UNKNOWN", BT_UNKNOWN),
2791 minit ("VOID", BT_VOID),
2792 minit ("ASSUMED", BT_ASSUMED),
2793 minit ("UNSIGNED", BT_UNSIGNED),
2794 minit (NULL, -1)
2798 static void
2799 mio_charlen (gfc_charlen **clp)
2801 gfc_charlen *cl;
2803 mio_lparen ();
2805 if (iomode == IO_OUTPUT)
2807 cl = *clp;
2808 if (cl != NULL)
2809 mio_expr (&cl->length);
2811 else
2813 if (peek_atom () != ATOM_RPAREN)
2815 cl = gfc_new_charlen (gfc_current_ns, NULL);
2816 mio_expr (&cl->length);
2817 *clp = cl;
2821 mio_rparen ();
2825 /* See if a name is a generated name. */
2827 static int
2828 check_unique_name (const char *name)
2830 return *name == '@';
2834 static void
2835 mio_typespec (gfc_typespec *ts)
2837 mio_lparen ();
2839 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2841 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2842 mio_integer (&ts->kind);
2843 else
2844 mio_symbol_ref (&ts->u.derived);
2846 mio_symbol_ref (&ts->interface);
2848 /* Add info for C interop and is_iso_c. */
2849 mio_integer (&ts->is_c_interop);
2850 mio_integer (&ts->is_iso_c);
2852 /* If the typespec is for an identifier either from iso_c_binding, or
2853 a constant that was initialized to an identifier from it, use the
2854 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2855 if (ts->is_iso_c)
2856 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2857 else
2858 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2860 if (ts->type != BT_CHARACTER)
2862 /* ts->u.cl is only valid for BT_CHARACTER. */
2863 mio_lparen ();
2864 mio_rparen ();
2866 else
2867 mio_charlen (&ts->u.cl);
2869 /* So as not to disturb the existing API, use an ATOM_NAME to
2870 transmit deferred characteristic for characters (F2003). */
2871 if (iomode == IO_OUTPUT)
2873 if (ts->type == BT_CHARACTER && ts->deferred)
2874 write_atom (ATOM_NAME, "DEFERRED_CL");
2876 else if (peek_atom () != ATOM_RPAREN)
2878 if (parse_atom () != ATOM_NAME)
2879 bad_module ("Expected string");
2880 ts->deferred = 1;
2883 mio_rparen ();
2887 static const mstring array_spec_types[] = {
2888 minit ("EXPLICIT", AS_EXPLICIT),
2889 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2890 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2891 minit ("DEFERRED", AS_DEFERRED),
2892 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2893 minit (NULL, -1)
2897 static void
2898 mio_array_spec (gfc_array_spec **asp)
2900 gfc_array_spec *as;
2901 int i;
2903 mio_lparen ();
2905 if (iomode == IO_OUTPUT)
2907 int rank;
2909 if (*asp == NULL)
2910 goto done;
2911 as = *asp;
2913 /* mio_integer expects nonnegative values. */
2914 rank = as->rank > 0 ? as->rank : 0;
2915 mio_integer (&rank);
2917 else
2919 if (peek_atom () == ATOM_RPAREN)
2921 *asp = NULL;
2922 goto done;
2925 *asp = as = gfc_get_array_spec ();
2926 mio_integer (&as->rank);
2929 mio_integer (&as->corank);
2930 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2932 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2933 as->rank = -1;
2934 if (iomode == IO_INPUT && as->corank)
2935 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2937 if (as->rank + as->corank > 0)
2938 for (i = 0; i < as->rank + as->corank; i++)
2940 mio_expr (&as->lower[i]);
2941 mio_expr (&as->upper[i]);
2944 done:
2945 mio_rparen ();
2949 /* Given a pointer to an array reference structure (which lives in a
2950 gfc_ref structure), find the corresponding array specification
2951 structure. Storing the pointer in the ref structure doesn't quite
2952 work when loading from a module. Generating code for an array
2953 reference also needs more information than just the array spec. */
2955 static const mstring array_ref_types[] = {
2956 minit ("FULL", AR_FULL),
2957 minit ("ELEMENT", AR_ELEMENT),
2958 minit ("SECTION", AR_SECTION),
2959 minit (NULL, -1)
2963 static void
2964 mio_array_ref (gfc_array_ref *ar)
2966 int i;
2968 mio_lparen ();
2969 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2970 mio_integer (&ar->dimen);
2972 switch (ar->type)
2974 case AR_FULL:
2975 break;
2977 case AR_ELEMENT:
2978 for (i = 0; i < ar->dimen; i++)
2979 mio_expr (&ar->start[i]);
2981 break;
2983 case AR_SECTION:
2984 for (i = 0; i < ar->dimen; i++)
2986 mio_expr (&ar->start[i]);
2987 mio_expr (&ar->end[i]);
2988 mio_expr (&ar->stride[i]);
2991 break;
2993 case AR_UNKNOWN:
2994 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2997 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2998 we can't call mio_integer directly. Instead loop over each element
2999 and cast it to/from an integer. */
3000 if (iomode == IO_OUTPUT)
3002 for (i = 0; i < ar->dimen; i++)
3004 HOST_WIDE_INT tmp = (HOST_WIDE_INT)ar->dimen_type[i];
3005 write_atom (ATOM_INTEGER, &tmp);
3008 else
3010 for (i = 0; i < ar->dimen; i++)
3012 require_atom (ATOM_INTEGER);
3013 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
3017 if (iomode == IO_INPUT)
3019 ar->where = gfc_current_locus;
3021 for (i = 0; i < ar->dimen; i++)
3022 ar->c_where[i] = gfc_current_locus;
3025 mio_rparen ();
3029 /* Saves or restores a pointer. The pointer is converted back and
3030 forth from an integer. We return the pointer_info pointer so that
3031 the caller can take additional action based on the pointer type. */
3033 static pointer_info *
3034 mio_pointer_ref (void *gp)
3036 pointer_info *p;
3038 if (iomode == IO_OUTPUT)
3040 p = get_pointer (*((char **) gp));
3041 HOST_WIDE_INT hwi = p->integer;
3042 write_atom (ATOM_INTEGER, &hwi);
3044 else
3046 require_atom (ATOM_INTEGER);
3047 p = add_fixup (atom_int, gp);
3050 return p;
3054 /* Save and load references to components that occur within
3055 expressions. We have to describe these references by a number and
3056 by name. The number is necessary for forward references during
3057 reading, and the name is necessary if the symbol already exists in
3058 the namespace and is not loaded again. */
3060 static void
3061 mio_component_ref (gfc_component **cp)
3063 pointer_info *p;
3065 p = mio_pointer_ref (cp);
3066 if (p->type == P_UNKNOWN)
3067 p->type = P_COMPONENT;
3071 static void mio_namespace_ref (gfc_namespace **nsp);
3072 static void mio_formal_arglist (gfc_formal_arglist **formal);
3073 static void mio_typebound_proc (gfc_typebound_proc** proc);
3074 static void mio_actual_arglist (gfc_actual_arglist **ap, bool pdt);
3076 static void
3077 mio_component (gfc_component *c, int vtype)
3079 pointer_info *p;
3081 mio_lparen ();
3083 if (iomode == IO_OUTPUT)
3085 p = get_pointer (c);
3086 mio_hwi (&p->integer);
3088 else
3090 HOST_WIDE_INT n;
3091 mio_hwi (&n);
3092 p = get_integer (n);
3093 associate_integer_pointer (p, c);
3096 if (p->type == P_UNKNOWN)
3097 p->type = P_COMPONENT;
3099 mio_pool_string (&c->name);
3100 mio_typespec (&c->ts);
3101 mio_array_spec (&c->as);
3103 /* PDT templates store the expression for the kind of a component here. */
3104 mio_expr (&c->kind_expr);
3106 /* PDT types store the component specification list here. */
3107 mio_actual_arglist (&c->param_list, true);
3109 mio_symbol_attribute (&c->attr);
3110 if (c->ts.type == BT_CLASS)
3111 c->attr.class_ok = 1;
3112 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
3114 if (!vtype || strcmp (c->name, "_final") == 0
3115 || strcmp (c->name, "_hash") == 0)
3116 mio_expr (&c->initializer);
3118 if (c->attr.proc_pointer)
3119 mio_typebound_proc (&c->tb);
3121 c->loc = gfc_current_locus;
3123 mio_rparen ();
3127 static void
3128 mio_component_list (gfc_component **cp, int vtype)
3130 gfc_component *c, *tail;
3132 mio_lparen ();
3134 if (iomode == IO_OUTPUT)
3136 for (c = *cp; c; c = c->next)
3137 mio_component (c, vtype);
3139 else
3141 *cp = NULL;
3142 tail = NULL;
3144 for (;;)
3146 if (peek_atom () == ATOM_RPAREN)
3147 break;
3149 c = gfc_get_component ();
3150 mio_component (c, vtype);
3152 if (tail == NULL)
3153 *cp = c;
3154 else
3155 tail->next = c;
3157 tail = c;
3161 mio_rparen ();
3165 static void
3166 mio_actual_arg (gfc_actual_arglist *a, bool pdt)
3168 mio_lparen ();
3169 mio_pool_string (&a->name);
3170 mio_expr (&a->expr);
3171 if (pdt)
3172 mio_integer ((int *)&a->spec_type);
3173 mio_rparen ();
3177 static void
3178 mio_actual_arglist (gfc_actual_arglist **ap, bool pdt)
3180 gfc_actual_arglist *a, *tail;
3182 mio_lparen ();
3184 if (iomode == IO_OUTPUT)
3186 for (a = *ap; a; a = a->next)
3187 mio_actual_arg (a, pdt);
3190 else
3192 tail = NULL;
3194 for (;;)
3196 if (peek_atom () != ATOM_LPAREN)
3197 break;
3199 a = gfc_get_actual_arglist ();
3201 if (tail == NULL)
3202 *ap = a;
3203 else
3204 tail->next = a;
3206 tail = a;
3207 mio_actual_arg (a, pdt);
3211 mio_rparen ();
3215 /* Read and write formal argument lists. */
3217 static void
3218 mio_formal_arglist (gfc_formal_arglist **formal)
3220 gfc_formal_arglist *f, *tail;
3222 mio_lparen ();
3224 if (iomode == IO_OUTPUT)
3226 for (f = *formal; f; f = f->next)
3227 mio_symbol_ref (&f->sym);
3229 else
3231 *formal = tail = NULL;
3233 while (peek_atom () != ATOM_RPAREN)
3235 f = gfc_get_formal_arglist ();
3236 mio_symbol_ref (&f->sym);
3238 if (*formal == NULL)
3239 *formal = f;
3240 else
3241 tail->next = f;
3243 tail = f;
3247 mio_rparen ();
3251 /* Save or restore a reference to a symbol node. */
3253 pointer_info *
3254 mio_symbol_ref (gfc_symbol **symp)
3256 pointer_info *p;
3258 p = mio_pointer_ref (symp);
3259 if (p->type == P_UNKNOWN)
3260 p->type = P_SYMBOL;
3262 if (iomode == IO_OUTPUT)
3264 if (p->u.wsym.state == UNREFERENCED)
3265 p->u.wsym.state = NEEDS_WRITE;
3267 else
3269 if (p->u.rsym.state == UNUSED)
3270 p->u.rsym.state = NEEDED;
3272 return p;
3276 /* Save or restore a reference to a symtree node. */
3278 static void
3279 mio_symtree_ref (gfc_symtree **stp)
3281 pointer_info *p;
3282 fixup_t *f;
3284 if (iomode == IO_OUTPUT)
3285 mio_symbol_ref (&(*stp)->n.sym);
3286 else
3288 require_atom (ATOM_INTEGER);
3289 p = get_integer (atom_int);
3291 /* An unused equivalence member; make a symbol and a symtree
3292 for it. */
3293 if (in_load_equiv && p->u.rsym.symtree == NULL)
3295 /* Since this is not used, it must have a unique name. */
3296 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
3298 /* Make the symbol. */
3299 if (p->u.rsym.sym == NULL)
3301 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
3302 gfc_current_ns);
3303 p->u.rsym.sym->module = gfc_get_string ("%s", p->u.rsym.module);
3306 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
3307 p->u.rsym.symtree->n.sym->refs++;
3308 p->u.rsym.referenced = 1;
3310 /* If the symbol is PRIVATE and in COMMON, load_commons will
3311 generate a fixup symbol, which must be associated. */
3312 if (p->fixup)
3313 resolve_fixups (p->fixup, p->u.rsym.sym);
3314 p->fixup = NULL;
3317 if (p->type == P_UNKNOWN)
3318 p->type = P_SYMBOL;
3320 if (p->u.rsym.state == UNUSED)
3321 p->u.rsym.state = NEEDED;
3323 if (p->u.rsym.symtree != NULL)
3325 *stp = p->u.rsym.symtree;
3327 else
3329 f = XCNEW (fixup_t);
3331 f->next = p->u.rsym.stfixup;
3332 p->u.rsym.stfixup = f;
3334 f->pointer = (void **) stp;
3340 static void
3341 mio_iterator (gfc_iterator **ip)
3343 gfc_iterator *iter;
3345 mio_lparen ();
3347 if (iomode == IO_OUTPUT)
3349 if (*ip == NULL)
3350 goto done;
3352 else
3354 if (peek_atom () == ATOM_RPAREN)
3356 *ip = NULL;
3357 goto done;
3360 *ip = gfc_get_iterator ();
3363 iter = *ip;
3365 mio_expr (&iter->var);
3366 mio_expr (&iter->start);
3367 mio_expr (&iter->end);
3368 mio_expr (&iter->step);
3370 done:
3371 mio_rparen ();
3375 static void
3376 mio_constructor (gfc_constructor_base *cp)
3378 gfc_constructor *c;
3380 mio_lparen ();
3382 if (iomode == IO_OUTPUT)
3384 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3386 mio_lparen ();
3387 mio_expr (&c->expr);
3388 mio_iterator (&c->iterator);
3389 mio_rparen ();
3392 else
3394 while (peek_atom () != ATOM_RPAREN)
3396 c = gfc_constructor_append_expr (cp, NULL, NULL);
3398 mio_lparen ();
3399 mio_expr (&c->expr);
3400 mio_iterator (&c->iterator);
3401 mio_rparen ();
3405 mio_rparen ();
3409 static const mstring ref_types[] = {
3410 minit ("ARRAY", REF_ARRAY),
3411 minit ("COMPONENT", REF_COMPONENT),
3412 minit ("SUBSTRING", REF_SUBSTRING),
3413 minit ("INQUIRY", REF_INQUIRY),
3414 minit (NULL, -1)
3417 static const mstring inquiry_types[] = {
3418 minit ("RE", INQUIRY_RE),
3419 minit ("IM", INQUIRY_IM),
3420 minit ("KIND", INQUIRY_KIND),
3421 minit ("LEN", INQUIRY_LEN),
3422 minit (NULL, -1)
3426 static void
3427 mio_ref (gfc_ref **rp)
3429 gfc_ref *r;
3431 mio_lparen ();
3433 r = *rp;
3434 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3436 switch (r->type)
3438 case REF_ARRAY:
3439 mio_array_ref (&r->u.ar);
3440 break;
3442 case REF_COMPONENT:
3443 mio_symbol_ref (&r->u.c.sym);
3444 mio_component_ref (&r->u.c.component);
3445 break;
3447 case REF_SUBSTRING:
3448 mio_expr (&r->u.ss.start);
3449 mio_expr (&r->u.ss.end);
3450 mio_charlen (&r->u.ss.length);
3451 break;
3453 case REF_INQUIRY:
3454 r->u.i = MIO_NAME (inquiry_type) (r->u.i, inquiry_types);
3455 break;
3458 mio_rparen ();
3462 static void
3463 mio_ref_list (gfc_ref **rp)
3465 gfc_ref *ref, *head, *tail;
3467 mio_lparen ();
3469 if (iomode == IO_OUTPUT)
3471 for (ref = *rp; ref; ref = ref->next)
3472 mio_ref (&ref);
3474 else
3476 head = tail = NULL;
3478 while (peek_atom () != ATOM_RPAREN)
3480 if (head == NULL)
3481 head = tail = gfc_get_ref ();
3482 else
3484 tail->next = gfc_get_ref ();
3485 tail = tail->next;
3488 mio_ref (&tail);
3491 *rp = head;
3494 mio_rparen ();
3498 /* Read and write an integer value. */
3500 static void
3501 mio_gmp_integer (mpz_t *integer)
3503 char *p;
3505 if (iomode == IO_INPUT)
3507 if (parse_atom () != ATOM_STRING)
3508 bad_module ("Expected integer string");
3510 mpz_init (*integer);
3511 if (mpz_set_str (*integer, atom_string, 10))
3512 bad_module ("Error converting integer");
3514 free (atom_string);
3516 else
3518 p = mpz_get_str (NULL, 10, *integer);
3519 write_atom (ATOM_STRING, p);
3520 free (p);
3525 static void
3526 mio_gmp_real (mpfr_t *real)
3528 mpfr_exp_t exponent;
3529 char *p;
3531 if (iomode == IO_INPUT)
3533 if (parse_atom () != ATOM_STRING)
3534 bad_module ("Expected real string");
3536 mpfr_init (*real);
3537 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3538 free (atom_string);
3540 else
3542 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3544 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3546 write_atom (ATOM_STRING, p);
3547 free (p);
3548 return;
3551 atom_string = XCNEWVEC (char, strlen (p) + 20);
3553 sprintf (atom_string, "0.%s@%ld", p, exponent);
3555 /* Fix negative numbers. */
3556 if (atom_string[2] == '-')
3558 atom_string[0] = '-';
3559 atom_string[1] = '0';
3560 atom_string[2] = '.';
3563 write_atom (ATOM_STRING, atom_string);
3565 free (atom_string);
3566 free (p);
3571 /* Save and restore the shape of an array constructor. */
3573 static void
3574 mio_shape (mpz_t **pshape, int rank)
3576 mpz_t *shape;
3577 atom_type t;
3578 int n;
3580 /* A NULL shape is represented by (). */
3581 mio_lparen ();
3583 if (iomode == IO_OUTPUT)
3585 shape = *pshape;
3586 if (!shape)
3588 mio_rparen ();
3589 return;
3592 else
3594 t = peek_atom ();
3595 if (t == ATOM_RPAREN)
3597 *pshape = NULL;
3598 mio_rparen ();
3599 return;
3602 shape = gfc_get_shape (rank);
3603 *pshape = shape;
3606 for (n = 0; n < rank; n++)
3607 mio_gmp_integer (&shape[n]);
3609 mio_rparen ();
3613 static const mstring expr_types[] = {
3614 minit ("OP", EXPR_OP),
3615 minit ("FUNCTION", EXPR_FUNCTION),
3616 minit ("CONSTANT", EXPR_CONSTANT),
3617 minit ("VARIABLE", EXPR_VARIABLE),
3618 minit ("SUBSTRING", EXPR_SUBSTRING),
3619 minit ("STRUCTURE", EXPR_STRUCTURE),
3620 minit ("ARRAY", EXPR_ARRAY),
3621 minit ("NULL", EXPR_NULL),
3622 minit ("COMPCALL", EXPR_COMPCALL),
3623 minit (NULL, -1)
3626 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3627 generic operators, not in expressions. INTRINSIC_USER is also
3628 replaced by the correct function name by the time we see it. */
3630 static const mstring intrinsics[] =
3632 minit ("UPLUS", INTRINSIC_UPLUS),
3633 minit ("UMINUS", INTRINSIC_UMINUS),
3634 minit ("PLUS", INTRINSIC_PLUS),
3635 minit ("MINUS", INTRINSIC_MINUS),
3636 minit ("TIMES", INTRINSIC_TIMES),
3637 minit ("DIVIDE", INTRINSIC_DIVIDE),
3638 minit ("POWER", INTRINSIC_POWER),
3639 minit ("CONCAT", INTRINSIC_CONCAT),
3640 minit ("AND", INTRINSIC_AND),
3641 minit ("OR", INTRINSIC_OR),
3642 minit ("EQV", INTRINSIC_EQV),
3643 minit ("NEQV", INTRINSIC_NEQV),
3644 minit ("EQ_SIGN", INTRINSIC_EQ),
3645 minit ("EQ", INTRINSIC_EQ_OS),
3646 minit ("NE_SIGN", INTRINSIC_NE),
3647 minit ("NE", INTRINSIC_NE_OS),
3648 minit ("GT_SIGN", INTRINSIC_GT),
3649 minit ("GT", INTRINSIC_GT_OS),
3650 minit ("GE_SIGN", INTRINSIC_GE),
3651 minit ("GE", INTRINSIC_GE_OS),
3652 minit ("LT_SIGN", INTRINSIC_LT),
3653 minit ("LT", INTRINSIC_LT_OS),
3654 minit ("LE_SIGN", INTRINSIC_LE),
3655 minit ("LE", INTRINSIC_LE_OS),
3656 minit ("NOT", INTRINSIC_NOT),
3657 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3658 minit ("USER", INTRINSIC_USER),
3659 minit (NULL, -1)
3663 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3665 static void
3666 fix_mio_expr (gfc_expr *e)
3668 gfc_symtree *ns_st = NULL;
3669 const char *fname;
3671 if (iomode != IO_OUTPUT)
3672 return;
3674 if (e->symtree)
3676 /* If this is a symtree for a symbol that came from a contained module
3677 namespace, it has a unique name and we should look in the current
3678 namespace to see if the required, non-contained symbol is available
3679 yet. If so, the latter should be written. */
3680 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3682 const char *name = e->symtree->n.sym->name;
3683 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3684 name = gfc_dt_upper_string (name);
3685 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3688 /* On the other hand, if the existing symbol is the module name or the
3689 new symbol is a dummy argument, do not do the promotion. */
3690 if (ns_st && ns_st->n.sym
3691 && ns_st->n.sym->attr.flavor != FL_MODULE
3692 && !e->symtree->n.sym->attr.dummy)
3693 e->symtree = ns_st;
3695 else if (e->expr_type == EXPR_FUNCTION
3696 && (e->value.function.name || e->value.function.isym))
3698 gfc_symbol *sym;
3700 /* In some circumstances, a function used in an initialization
3701 expression, in one use associated module, can fail to be
3702 coupled to its symtree when used in a specification
3703 expression in another module. */
3704 fname = e->value.function.esym ? e->value.function.esym->name
3705 : e->value.function.isym->name;
3706 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3708 if (e->symtree)
3709 return;
3711 /* This is probably a reference to a private procedure from another
3712 module. To prevent a segfault, make a generic with no specific
3713 instances. If this module is used, without the required
3714 specific coming from somewhere, the appropriate error message
3715 is issued. */
3716 gfc_get_symbol (fname, gfc_current_ns, &sym);
3717 sym->attr.flavor = FL_PROCEDURE;
3718 sym->attr.generic = 1;
3719 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3720 gfc_commit_symbol (sym);
3725 /* Read and write expressions. The form "()" is allowed to indicate a
3726 NULL expression. */
3728 static void
3729 mio_expr (gfc_expr **ep)
3731 HOST_WIDE_INT hwi;
3732 gfc_expr *e;
3733 atom_type t;
3734 int flag;
3736 mio_lparen ();
3738 if (iomode == IO_OUTPUT)
3740 if (*ep == NULL)
3742 mio_rparen ();
3743 return;
3746 e = *ep;
3747 MIO_NAME (expr_t) (e->expr_type, expr_types);
3749 else
3751 t = parse_atom ();
3752 if (t == ATOM_RPAREN)
3754 *ep = NULL;
3755 return;
3758 if (t != ATOM_NAME)
3759 bad_module ("Expected expression type");
3761 e = *ep = gfc_get_expr ();
3762 e->where = gfc_current_locus;
3763 e->expr_type = (expr_t) find_enum (expr_types);
3766 mio_typespec (&e->ts);
3767 mio_integer (&e->rank);
3769 fix_mio_expr (e);
3771 switch (e->expr_type)
3773 case EXPR_OP:
3774 e->value.op.op
3775 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3777 switch (e->value.op.op)
3779 case INTRINSIC_UPLUS:
3780 case INTRINSIC_UMINUS:
3781 case INTRINSIC_NOT:
3782 case INTRINSIC_PARENTHESES:
3783 mio_expr (&e->value.op.op1);
3784 break;
3786 case INTRINSIC_PLUS:
3787 case INTRINSIC_MINUS:
3788 case INTRINSIC_TIMES:
3789 case INTRINSIC_DIVIDE:
3790 case INTRINSIC_POWER:
3791 case INTRINSIC_CONCAT:
3792 case INTRINSIC_AND:
3793 case INTRINSIC_OR:
3794 case INTRINSIC_EQV:
3795 case INTRINSIC_NEQV:
3796 case INTRINSIC_EQ:
3797 case INTRINSIC_EQ_OS:
3798 case INTRINSIC_NE:
3799 case INTRINSIC_NE_OS:
3800 case INTRINSIC_GT:
3801 case INTRINSIC_GT_OS:
3802 case INTRINSIC_GE:
3803 case INTRINSIC_GE_OS:
3804 case INTRINSIC_LT:
3805 case INTRINSIC_LT_OS:
3806 case INTRINSIC_LE:
3807 case INTRINSIC_LE_OS:
3808 mio_expr (&e->value.op.op1);
3809 mio_expr (&e->value.op.op2);
3810 break;
3812 case INTRINSIC_USER:
3813 /* INTRINSIC_USER should not appear in resolved expressions,
3814 though for UDRs we need to stream unresolved ones. */
3815 if (iomode == IO_OUTPUT)
3816 write_atom (ATOM_STRING, e->value.op.uop->name);
3817 else
3819 char *name = read_string ();
3820 const char *uop_name = find_use_name (name, true);
3821 if (uop_name == NULL)
3823 size_t len = strlen (name);
3824 char *name2 = XCNEWVEC (char, len + 2);
3825 memcpy (name2, name, len);
3826 name2[len] = ' ';
3827 name2[len + 1] = '\0';
3828 free (name);
3829 uop_name = name = name2;
3831 e->value.op.uop = gfc_get_uop (uop_name);
3832 free (name);
3834 mio_expr (&e->value.op.op1);
3835 mio_expr (&e->value.op.op2);
3836 break;
3838 default:
3839 bad_module ("Bad operator");
3842 break;
3844 case EXPR_FUNCTION:
3845 mio_symtree_ref (&e->symtree);
3846 mio_actual_arglist (&e->value.function.actual, false);
3848 if (iomode == IO_OUTPUT)
3850 e->value.function.name
3851 = mio_allocated_string (e->value.function.name);
3852 if (e->value.function.esym)
3853 flag = 1;
3854 else if (e->ref)
3855 flag = 2;
3856 else if (e->value.function.isym == NULL)
3857 flag = 3;
3858 else
3859 flag = 0;
3860 mio_integer (&flag);
3861 switch (flag)
3863 case 1:
3864 mio_symbol_ref (&e->value.function.esym);
3865 break;
3866 case 2:
3867 mio_ref_list (&e->ref);
3868 break;
3869 case 3:
3870 break;
3871 default:
3872 write_atom (ATOM_STRING, e->value.function.isym->name);
3875 else
3877 require_atom (ATOM_STRING);
3878 if (atom_string[0] == '\0')
3879 e->value.function.name = NULL;
3880 else
3881 e->value.function.name = gfc_get_string ("%s", atom_string);
3882 free (atom_string);
3884 mio_integer (&flag);
3885 switch (flag)
3887 case 1:
3888 mio_symbol_ref (&e->value.function.esym);
3889 break;
3890 case 2:
3891 mio_ref_list (&e->ref);
3892 break;
3893 case 3:
3894 break;
3895 default:
3896 require_atom (ATOM_STRING);
3897 e->value.function.isym = gfc_find_function (atom_string);
3898 free (atom_string);
3902 break;
3904 case EXPR_VARIABLE:
3905 mio_symtree_ref (&e->symtree);
3906 mio_ref_list (&e->ref);
3907 break;
3909 case EXPR_SUBSTRING:
3910 e->value.character.string
3911 = CONST_CAST (gfc_char_t *,
3912 mio_allocated_wide_string (e->value.character.string,
3913 e->value.character.length));
3914 mio_ref_list (&e->ref);
3915 break;
3917 case EXPR_STRUCTURE:
3918 case EXPR_ARRAY:
3919 mio_constructor (&e->value.constructor);
3920 mio_shape (&e->shape, e->rank);
3921 break;
3923 case EXPR_CONSTANT:
3924 switch (e->ts.type)
3926 case BT_INTEGER:
3927 mio_gmp_integer (&e->value.integer);
3928 break;
3930 case BT_REAL:
3931 gfc_set_model_kind (e->ts.kind);
3932 mio_gmp_real (&e->value.real);
3933 break;
3935 case BT_COMPLEX:
3936 gfc_set_model_kind (e->ts.kind);
3937 mio_gmp_real (&mpc_realref (e->value.complex));
3938 mio_gmp_real (&mpc_imagref (e->value.complex));
3939 break;
3941 case BT_LOGICAL:
3942 mio_integer (&e->value.logical);
3943 break;
3945 case BT_CHARACTER:
3946 hwi = e->value.character.length;
3947 mio_hwi (&hwi);
3948 e->value.character.length = hwi;
3949 e->value.character.string
3950 = CONST_CAST (gfc_char_t *,
3951 mio_allocated_wide_string (e->value.character.string,
3952 e->value.character.length));
3953 break;
3955 default:
3956 bad_module ("Bad type in constant expression");
3959 break;
3961 case EXPR_NULL:
3962 break;
3964 case EXPR_COMPCALL:
3965 case EXPR_PPC:
3966 case EXPR_UNKNOWN:
3967 gcc_unreachable ();
3968 break;
3971 /* PDT types store the expression specification list here. */
3972 mio_actual_arglist (&e->param_list, true);
3974 mio_rparen ();
3978 /* Read and write namelists. */
3980 static void
3981 mio_namelist (gfc_symbol *sym)
3983 gfc_namelist *n, *m;
3985 mio_lparen ();
3987 if (iomode == IO_OUTPUT)
3989 for (n = sym->namelist; n; n = n->next)
3990 mio_symbol_ref (&n->sym);
3992 else
3994 m = NULL;
3995 while (peek_atom () != ATOM_RPAREN)
3997 n = gfc_get_namelist ();
3998 mio_symbol_ref (&n->sym);
4000 if (sym->namelist == NULL)
4001 sym->namelist = n;
4002 else
4003 m->next = n;
4005 m = n;
4007 sym->namelist_tail = m;
4010 mio_rparen ();
4014 /* Save/restore lists of gfc_interface structures. When loading an
4015 interface, we are really appending to the existing list of
4016 interfaces. Checking for duplicate and ambiguous interfaces has to
4017 be done later when all symbols have been loaded. */
4019 pointer_info *
4020 mio_interface_rest (gfc_interface **ip)
4022 gfc_interface *tail, *p;
4023 pointer_info *pi = NULL;
4025 if (iomode == IO_OUTPUT)
4027 if (ip != NULL)
4028 for (p = *ip; p; p = p->next)
4029 mio_symbol_ref (&p->sym);
4031 else
4033 if (*ip == NULL)
4034 tail = NULL;
4035 else
4037 tail = *ip;
4038 while (tail->next)
4039 tail = tail->next;
4042 for (;;)
4044 if (peek_atom () == ATOM_RPAREN)
4045 break;
4047 p = gfc_get_interface ();
4048 p->where = gfc_current_locus;
4049 pi = mio_symbol_ref (&p->sym);
4051 if (tail == NULL)
4052 *ip = p;
4053 else
4054 tail->next = p;
4056 tail = p;
4060 mio_rparen ();
4061 return pi;
4065 /* Save/restore a nameless operator interface. */
4067 static void
4068 mio_interface (gfc_interface **ip)
4070 mio_lparen ();
4071 mio_interface_rest (ip);
4075 /* Save/restore a named operator interface. */
4077 static void
4078 mio_symbol_interface (const char **name, const char **module,
4079 gfc_interface **ip)
4081 mio_lparen ();
4082 mio_pool_string (name);
4083 mio_pool_string (module);
4084 mio_interface_rest (ip);
4088 static void
4089 mio_namespace_ref (gfc_namespace **nsp)
4091 gfc_namespace *ns;
4092 pointer_info *p;
4094 p = mio_pointer_ref (nsp);
4096 if (p->type == P_UNKNOWN)
4097 p->type = P_NAMESPACE;
4099 if (iomode == IO_INPUT && p->integer != 0)
4101 ns = (gfc_namespace *) p->u.pointer;
4102 if (ns == NULL)
4104 ns = gfc_get_namespace (NULL, 0);
4105 associate_integer_pointer (p, ns);
4107 else
4108 ns->refs++;
4113 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
4115 static gfc_namespace* current_f2k_derived;
4117 static void
4118 mio_typebound_proc (gfc_typebound_proc** proc)
4120 int flag;
4121 int overriding_flag;
4123 if (iomode == IO_INPUT)
4125 *proc = gfc_get_typebound_proc (NULL);
4126 (*proc)->where = gfc_current_locus;
4128 gcc_assert (*proc);
4130 mio_lparen ();
4132 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
4134 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
4135 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4136 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
4137 overriding_flag = mio_name (overriding_flag, binding_overriding);
4138 (*proc)->deferred = ((overriding_flag & 2) != 0);
4139 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
4140 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4142 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
4143 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
4144 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
4146 mio_pool_string (&((*proc)->pass_arg));
4148 flag = (int) (*proc)->pass_arg_num;
4149 mio_integer (&flag);
4150 (*proc)->pass_arg_num = (unsigned) flag;
4152 if ((*proc)->is_generic)
4154 gfc_tbp_generic* g;
4155 int iop;
4157 mio_lparen ();
4159 if (iomode == IO_OUTPUT)
4160 for (g = (*proc)->u.generic; g; g = g->next)
4162 iop = (int) g->is_operator;
4163 mio_integer (&iop);
4164 mio_allocated_string (g->specific_st->name);
4166 else
4168 (*proc)->u.generic = NULL;
4169 while (peek_atom () != ATOM_RPAREN)
4171 gfc_symtree** sym_root;
4173 g = gfc_get_tbp_generic ();
4174 g->specific = NULL;
4176 mio_integer (&iop);
4177 g->is_operator = (bool) iop;
4179 require_atom (ATOM_STRING);
4180 sym_root = &current_f2k_derived->tb_sym_root;
4181 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
4182 free (atom_string);
4184 g->next = (*proc)->u.generic;
4185 (*proc)->u.generic = g;
4189 mio_rparen ();
4191 else if (!(*proc)->ppc)
4192 mio_symtree_ref (&(*proc)->u.specific);
4194 mio_rparen ();
4197 /* Walker-callback function for this purpose. */
4198 static void
4199 mio_typebound_symtree (gfc_symtree* st)
4201 if (iomode == IO_OUTPUT && !st->n.tb)
4202 return;
4204 if (iomode == IO_OUTPUT)
4206 mio_lparen ();
4207 mio_allocated_string (st->name);
4209 /* For IO_INPUT, the above is done in mio_f2k_derived. */
4211 mio_typebound_proc (&st->n.tb);
4212 mio_rparen ();
4215 /* IO a full symtree (in all depth). */
4216 static void
4217 mio_full_typebound_tree (gfc_symtree** root)
4219 mio_lparen ();
4221 if (iomode == IO_OUTPUT)
4222 gfc_traverse_symtree (*root, &mio_typebound_symtree);
4223 else
4225 while (peek_atom () == ATOM_LPAREN)
4227 gfc_symtree* st;
4229 mio_lparen ();
4231 require_atom (ATOM_STRING);
4232 st = gfc_get_tbp_symtree (root, atom_string);
4233 free (atom_string);
4235 mio_typebound_symtree (st);
4239 mio_rparen ();
4242 static void
4243 mio_finalizer (gfc_finalizer **f)
4245 if (iomode == IO_OUTPUT)
4247 gcc_assert (*f);
4248 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
4249 mio_symtree_ref (&(*f)->proc_tree);
4251 else
4253 *f = gfc_get_finalizer ();
4254 (*f)->where = gfc_current_locus; /* Value should not matter. */
4255 (*f)->next = NULL;
4257 mio_symtree_ref (&(*f)->proc_tree);
4258 (*f)->proc_sym = NULL;
4262 static void
4263 mio_f2k_derived (gfc_namespace *f2k)
4265 current_f2k_derived = f2k;
4267 /* Handle the list of finalizer procedures. */
4268 mio_lparen ();
4269 if (iomode == IO_OUTPUT)
4271 gfc_finalizer *f;
4272 for (f = f2k->finalizers; f; f = f->next)
4273 mio_finalizer (&f);
4275 else
4277 f2k->finalizers = NULL;
4278 while (peek_atom () != ATOM_RPAREN)
4280 gfc_finalizer *cur = NULL;
4281 mio_finalizer (&cur);
4282 cur->next = f2k->finalizers;
4283 f2k->finalizers = cur;
4286 mio_rparen ();
4288 /* Handle type-bound procedures. */
4289 mio_full_typebound_tree (&f2k->tb_sym_root);
4291 /* Type-bound user operators. */
4292 mio_full_typebound_tree (&f2k->tb_uop_root);
4294 /* Type-bound intrinsic operators. */
4295 mio_lparen ();
4296 if (iomode == IO_OUTPUT)
4298 int op;
4299 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
4301 gfc_intrinsic_op realop;
4303 if (op == INTRINSIC_USER || !f2k->tb_op[op])
4304 continue;
4306 mio_lparen ();
4307 realop = (gfc_intrinsic_op) op;
4308 mio_intrinsic_op (&realop);
4309 mio_typebound_proc (&f2k->tb_op[op]);
4310 mio_rparen ();
4313 else
4314 while (peek_atom () != ATOM_RPAREN)
4316 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
4318 mio_lparen ();
4319 mio_intrinsic_op (&op);
4320 mio_typebound_proc (&f2k->tb_op[op]);
4321 mio_rparen ();
4323 mio_rparen ();
4326 static void
4327 mio_full_f2k_derived (gfc_symbol *sym)
4329 mio_lparen ();
4331 if (iomode == IO_OUTPUT)
4333 if (sym->f2k_derived)
4334 mio_f2k_derived (sym->f2k_derived);
4336 else
4338 if (peek_atom () != ATOM_RPAREN)
4340 gfc_namespace *ns;
4342 sym->f2k_derived = gfc_get_namespace (NULL, 0);
4344 /* PDT templates make use of the mechanisms for formal args
4345 and so the parameter symbols are stored in the formal
4346 namespace. Transfer the sym_root to f2k_derived and then
4347 free the formal namespace since it is uneeded. */
4348 if (sym->attr.pdt_template && sym->formal && sym->formal->sym)
4350 ns = sym->formal->sym->ns;
4351 sym->f2k_derived->sym_root = ns->sym_root;
4352 ns->sym_root = NULL;
4353 ns->refs++;
4354 gfc_free_namespace (ns);
4355 ns = NULL;
4358 mio_f2k_derived (sym->f2k_derived);
4360 else
4361 gcc_assert (!sym->f2k_derived);
4364 mio_rparen ();
4367 static const mstring omp_declare_simd_clauses[] =
4369 minit ("INBRANCH", 0),
4370 minit ("NOTINBRANCH", 1),
4371 minit ("SIMDLEN", 2),
4372 minit ("UNIFORM", 3),
4373 minit ("LINEAR", 4),
4374 minit ("ALIGNED", 5),
4375 minit ("LINEAR_REF", 33),
4376 minit ("LINEAR_VAL", 34),
4377 minit ("LINEAR_UVAL", 35),
4378 minit (NULL, -1)
4381 /* Handle !$omp declare simd. */
4383 static void
4384 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4386 if (iomode == IO_OUTPUT)
4388 if (*odsp == NULL)
4389 return;
4391 else if (peek_atom () != ATOM_LPAREN)
4392 return;
4394 gfc_omp_declare_simd *ods = *odsp;
4396 mio_lparen ();
4397 if (iomode == IO_OUTPUT)
4399 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4400 if (ods->clauses)
4402 gfc_omp_namelist *n;
4404 if (ods->clauses->inbranch)
4405 mio_name (0, omp_declare_simd_clauses);
4406 if (ods->clauses->notinbranch)
4407 mio_name (1, omp_declare_simd_clauses);
4408 if (ods->clauses->simdlen_expr)
4410 mio_name (2, omp_declare_simd_clauses);
4411 mio_expr (&ods->clauses->simdlen_expr);
4413 for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4415 mio_name (3, omp_declare_simd_clauses);
4416 mio_symbol_ref (&n->sym);
4418 for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4420 if (n->u.linear.op == OMP_LINEAR_DEFAULT)
4421 mio_name (4, omp_declare_simd_clauses);
4422 else
4423 mio_name (32 + n->u.linear.op, omp_declare_simd_clauses);
4424 mio_symbol_ref (&n->sym);
4425 mio_expr (&n->expr);
4427 for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4429 mio_name (5, omp_declare_simd_clauses);
4430 mio_symbol_ref (&n->sym);
4431 mio_expr (&n->expr);
4435 else
4437 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4439 require_atom (ATOM_NAME);
4440 *odsp = ods = gfc_get_omp_declare_simd ();
4441 ods->where = gfc_current_locus;
4442 ods->proc_name = ns->proc_name;
4443 if (peek_atom () == ATOM_NAME)
4445 ods->clauses = gfc_get_omp_clauses ();
4446 ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
4447 ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
4448 ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
4450 while (peek_atom () == ATOM_NAME)
4452 gfc_omp_namelist *n;
4453 int t = mio_name (0, omp_declare_simd_clauses);
4455 switch (t)
4457 case 0: ods->clauses->inbranch = true; break;
4458 case 1: ods->clauses->notinbranch = true; break;
4459 case 2: mio_expr (&ods->clauses->simdlen_expr); break;
4460 case 3:
4461 case 4:
4462 case 5:
4463 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4464 finish_namelist:
4465 n->where = gfc_current_locus;
4466 ptrs[t - 3] = &n->next;
4467 mio_symbol_ref (&n->sym);
4468 if (t != 3)
4469 mio_expr (&n->expr);
4470 break;
4471 case 33:
4472 case 34:
4473 case 35:
4474 *ptrs[1] = n = gfc_get_omp_namelist ();
4475 n->u.linear.op = (enum gfc_omp_linear_op) (t - 32);
4476 t = 4;
4477 goto finish_namelist;
4482 mio_omp_declare_simd (ns, &ods->next);
4484 mio_rparen ();
4488 static const mstring omp_declare_reduction_stmt[] =
4490 minit ("ASSIGN", 0),
4491 minit ("CALL", 1),
4492 minit (NULL, -1)
4496 static void
4497 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4498 gfc_namespace *ns, bool is_initializer)
4500 if (iomode == IO_OUTPUT)
4502 if ((*sym1)->module == NULL)
4504 (*sym1)->module = module_name;
4505 (*sym2)->module = module_name;
4507 mio_symbol_ref (sym1);
4508 mio_symbol_ref (sym2);
4509 if (ns->code->op == EXEC_ASSIGN)
4511 mio_name (0, omp_declare_reduction_stmt);
4512 mio_expr (&ns->code->expr1);
4513 mio_expr (&ns->code->expr2);
4515 else
4517 int flag;
4518 mio_name (1, omp_declare_reduction_stmt);
4519 mio_symtree_ref (&ns->code->symtree);
4520 mio_actual_arglist (&ns->code->ext.actual, false);
4522 flag = ns->code->resolved_isym != NULL;
4523 mio_integer (&flag);
4524 if (flag)
4525 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4526 else
4527 mio_symbol_ref (&ns->code->resolved_sym);
4530 else
4532 pointer_info *p1 = mio_symbol_ref (sym1);
4533 pointer_info *p2 = mio_symbol_ref (sym2);
4534 gfc_symbol *sym;
4535 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4536 gcc_assert (p1->u.rsym.sym == NULL);
4537 /* Add hidden symbols to the symtree. */
4538 pointer_info *q = get_integer (p1->u.rsym.ns);
4539 q->u.pointer = (void *) ns;
4540 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4541 sym->ts = udr->ts;
4542 sym->module = gfc_get_string ("%s", p1->u.rsym.module);
4543 associate_integer_pointer (p1, sym);
4544 sym->attr.omp_udr_artificial_var = 1;
4545 gcc_assert (p2->u.rsym.sym == NULL);
4546 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4547 sym->ts = udr->ts;
4548 sym->module = gfc_get_string ("%s", p2->u.rsym.module);
4549 associate_integer_pointer (p2, sym);
4550 sym->attr.omp_udr_artificial_var = 1;
4551 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4553 ns->code = gfc_get_code (EXEC_ASSIGN);
4554 mio_expr (&ns->code->expr1);
4555 mio_expr (&ns->code->expr2);
4557 else
4559 int flag;
4560 ns->code = gfc_get_code (EXEC_CALL);
4561 mio_symtree_ref (&ns->code->symtree);
4562 mio_actual_arglist (&ns->code->ext.actual, false);
4564 mio_integer (&flag);
4565 if (flag)
4567 require_atom (ATOM_STRING);
4568 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4569 free (atom_string);
4571 else
4572 mio_symbol_ref (&ns->code->resolved_sym);
4574 ns->code->loc = gfc_current_locus;
4575 ns->omp_udr_ns = 1;
4580 /* Unlike most other routines, the address of the symbol node is already
4581 fixed on input and the name/module has already been filled in.
4582 If you update the symbol format here, don't forget to update read_module
4583 as well (look for "seek to the symbol's component list"). */
4585 static void
4586 mio_symbol (gfc_symbol *sym)
4588 int intmod = INTMOD_NONE;
4590 mio_lparen ();
4592 mio_symbol_attribute (&sym->attr);
4594 if (sym->attr.pdt_type)
4595 sym->name = gfc_dt_upper_string (sym->name);
4597 /* Note that components are always saved, even if they are supposed
4598 to be private. Component access is checked during searching. */
4599 mio_component_list (&sym->components, sym->attr.vtype);
4600 if (sym->components != NULL)
4601 sym->component_access
4602 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4604 mio_typespec (&sym->ts);
4605 if (sym->ts.type == BT_CLASS)
4606 sym->attr.class_ok = 1;
4608 if (iomode == IO_OUTPUT)
4609 mio_namespace_ref (&sym->formal_ns);
4610 else
4612 mio_namespace_ref (&sym->formal_ns);
4613 if (sym->formal_ns)
4614 sym->formal_ns->proc_name = sym;
4617 /* Save/restore common block links. */
4618 mio_symbol_ref (&sym->common_next);
4620 mio_formal_arglist (&sym->formal);
4622 if (sym->attr.flavor == FL_PARAMETER)
4623 mio_expr (&sym->value);
4625 mio_array_spec (&sym->as);
4627 mio_symbol_ref (&sym->result);
4629 if (sym->attr.cray_pointee)
4630 mio_symbol_ref (&sym->cp_pointer);
4632 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4633 mio_full_f2k_derived (sym);
4635 /* PDT types store the symbol specification list here. */
4636 mio_actual_arglist (&sym->param_list, true);
4638 mio_namelist (sym);
4640 /* Add the fields that say whether this is from an intrinsic module,
4641 and if so, what symbol it is within the module. */
4642 /* mio_integer (&(sym->from_intmod)); */
4643 if (iomode == IO_OUTPUT)
4645 intmod = sym->from_intmod;
4646 mio_integer (&intmod);
4648 else
4650 mio_integer (&intmod);
4651 if (current_intmod)
4652 sym->from_intmod = current_intmod;
4653 else
4654 sym->from_intmod = (intmod_id) intmod;
4657 mio_integer (&(sym->intmod_sym_id));
4659 if (gfc_fl_struct (sym->attr.flavor))
4660 mio_integer (&(sym->hash_value));
4662 if (sym->formal_ns
4663 && sym->formal_ns->proc_name == sym
4664 && sym->formal_ns->entries == NULL)
4665 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4667 mio_rparen ();
4671 /************************* Top level subroutines *************************/
4673 /* A recursive function to look for a specific symbol by name and by
4674 module. Whilst several symtrees might point to one symbol, its
4675 is sufficient for the purposes here than one exist. Note that
4676 generic interfaces are distinguished as are symbols that have been
4677 renamed in another module. */
4678 static gfc_symtree *
4679 find_symbol (gfc_symtree *st, const char *name,
4680 const char *module, int generic)
4682 int c;
4683 gfc_symtree *retval, *s;
4685 if (st == NULL || st->n.sym == NULL)
4686 return NULL;
4688 c = strcmp (name, st->n.sym->name);
4689 if (c == 0 && st->n.sym->module
4690 && strcmp (module, st->n.sym->module) == 0
4691 && !check_unique_name (st->name))
4693 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4695 /* Detect symbols that are renamed by use association in another
4696 module by the absence of a symtree and null attr.use_rename,
4697 since the latter is not transmitted in the module file. */
4698 if (((!generic && !st->n.sym->attr.generic)
4699 || (generic && st->n.sym->attr.generic))
4700 && !(s == NULL && !st->n.sym->attr.use_rename))
4701 return st;
4704 retval = find_symbol (st->left, name, module, generic);
4706 if (retval == NULL)
4707 retval = find_symbol (st->right, name, module, generic);
4709 return retval;
4713 /* Skip a list between balanced left and right parens.
4714 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4715 have been already parsed by hand, and the remaining of the content is to be
4716 skipped here. The default value is 0 (balanced parens). */
4718 static void
4719 skip_list (int nest_level = 0)
4721 int level;
4723 level = nest_level;
4726 switch (parse_atom ())
4728 case ATOM_LPAREN:
4729 level++;
4730 break;
4732 case ATOM_RPAREN:
4733 level--;
4734 break;
4736 case ATOM_STRING:
4737 free (atom_string);
4738 break;
4740 case ATOM_NAME:
4741 case ATOM_INTEGER:
4742 break;
4745 while (level > 0);
4749 /* Load operator interfaces from the module. Interfaces are unusual
4750 in that they attach themselves to existing symbols. */
4752 static void
4753 load_operator_interfaces (void)
4755 const char *p;
4756 /* "module" must be large enough for the case of submodules in which the name
4757 has the form module.submodule */
4758 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
4759 gfc_user_op *uop;
4760 pointer_info *pi = NULL;
4761 int n, i;
4763 mio_lparen ();
4765 while (peek_atom () != ATOM_RPAREN)
4767 mio_lparen ();
4769 mio_internal_string (name);
4770 mio_internal_string (module);
4772 n = number_use_names (name, true);
4773 n = n ? n : 1;
4775 for (i = 1; i <= n; i++)
4777 /* Decide if we need to load this one or not. */
4778 p = find_use_name_n (name, &i, true);
4780 if (p == NULL)
4782 while (parse_atom () != ATOM_RPAREN);
4783 continue;
4786 if (i == 1)
4788 uop = gfc_get_uop (p);
4789 pi = mio_interface_rest (&uop->op);
4791 else
4793 if (gfc_find_uop (p, NULL))
4794 continue;
4795 uop = gfc_get_uop (p);
4796 uop->op = gfc_get_interface ();
4797 uop->op->where = gfc_current_locus;
4798 add_fixup (pi->integer, &uop->op->sym);
4803 mio_rparen ();
4807 /* Load interfaces from the module. Interfaces are unusual in that
4808 they attach themselves to existing symbols. */
4810 static void
4811 load_generic_interfaces (void)
4813 const char *p;
4814 /* "module" must be large enough for the case of submodules in which the name
4815 has the form module.submodule */
4816 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
4817 gfc_symbol *sym;
4818 gfc_interface *generic = NULL, *gen = NULL;
4819 int n, i, renamed;
4820 bool ambiguous_set = false;
4822 mio_lparen ();
4824 while (peek_atom () != ATOM_RPAREN)
4826 mio_lparen ();
4828 mio_internal_string (name);
4829 mio_internal_string (module);
4831 n = number_use_names (name, false);
4832 renamed = n ? 1 : 0;
4833 n = n ? n : 1;
4835 for (i = 1; i <= n; i++)
4837 gfc_symtree *st;
4838 /* Decide if we need to load this one or not. */
4839 p = find_use_name_n (name, &i, false);
4841 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4843 /* Skip the specific names for these cases. */
4844 while (i == 1 && parse_atom () != ATOM_RPAREN);
4846 continue;
4849 st = find_symbol (gfc_current_ns->sym_root,
4850 name, module_name, 1);
4852 /* If the symbol exists already and is being USEd without being
4853 in an ONLY clause, do not load a new symtree(11.3.2). */
4854 if (!only_flag && st)
4855 sym = st->n.sym;
4857 if (!sym)
4859 if (st)
4861 sym = st->n.sym;
4862 if (strcmp (st->name, p) != 0)
4864 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4865 st->n.sym = sym;
4866 sym->refs++;
4870 /* Since we haven't found a valid generic interface, we had
4871 better make one. */
4872 if (!sym)
4874 gfc_get_symbol (p, NULL, &sym);
4875 sym->name = gfc_get_string ("%s", name);
4876 sym->module = module_name;
4877 sym->attr.flavor = FL_PROCEDURE;
4878 sym->attr.generic = 1;
4879 sym->attr.use_assoc = 1;
4882 else
4884 /* Unless sym is a generic interface, this reference
4885 is ambiguous. */
4886 if (st == NULL)
4887 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4889 sym = st->n.sym;
4891 if (st && !sym->attr.generic
4892 && !st->ambiguous
4893 && sym->module
4894 && strcmp (module, sym->module))
4896 ambiguous_set = true;
4897 st->ambiguous = 1;
4901 sym->attr.use_only = only_flag;
4902 sym->attr.use_rename = renamed;
4904 if (i == 1)
4906 mio_interface_rest (&sym->generic);
4907 generic = sym->generic;
4909 else if (!sym->generic)
4911 sym->generic = generic;
4912 sym->attr.generic_copy = 1;
4915 /* If a procedure that is not generic has generic interfaces
4916 that include itself, it is generic! We need to take care
4917 to retain symbols ambiguous that were already so. */
4918 if (sym->attr.use_assoc
4919 && !sym->attr.generic
4920 && sym->attr.flavor == FL_PROCEDURE)
4922 for (gen = generic; gen; gen = gen->next)
4924 if (gen->sym == sym)
4926 sym->attr.generic = 1;
4927 if (ambiguous_set)
4928 st->ambiguous = 0;
4929 break;
4937 mio_rparen ();
4941 /* Load common blocks. */
4943 static void
4944 load_commons (void)
4946 char name[GFC_MAX_SYMBOL_LEN + 1];
4947 gfc_common_head *p;
4949 mio_lparen ();
4951 while (peek_atom () != ATOM_RPAREN)
4953 int flags = 0;
4954 char* label;
4955 mio_lparen ();
4956 mio_internal_string (name);
4958 p = gfc_get_common (name, 1);
4960 mio_symbol_ref (&p->head);
4961 mio_integer (&flags);
4962 if (flags & 1)
4963 p->saved = 1;
4964 if (flags & 2)
4965 p->threadprivate = 1;
4966 p->omp_device_type = (gfc_omp_device_type) ((flags >> 2) & 3);
4967 p->use_assoc = 1;
4969 /* Get whether this was a bind(c) common or not. */
4970 mio_integer (&p->is_bind_c);
4971 /* Get the binding label. */
4972 label = read_string ();
4973 if (strlen (label))
4974 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4975 XDELETEVEC (label);
4977 mio_rparen ();
4980 mio_rparen ();
4984 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4985 so that unused variables are not loaded and so that the expression can
4986 be safely freed. */
4988 static void
4989 load_equiv (void)
4991 gfc_equiv *head, *tail, *end, *eq, *equiv;
4992 bool duplicate;
4994 mio_lparen ();
4995 in_load_equiv = true;
4997 end = gfc_current_ns->equiv;
4998 while (end != NULL && end->next != NULL)
4999 end = end->next;
5001 while (peek_atom () != ATOM_RPAREN) {
5002 mio_lparen ();
5003 head = tail = NULL;
5005 while(peek_atom () != ATOM_RPAREN)
5007 if (head == NULL)
5008 head = tail = gfc_get_equiv ();
5009 else
5011 tail->eq = gfc_get_equiv ();
5012 tail = tail->eq;
5015 mio_pool_string (&tail->module);
5016 mio_expr (&tail->expr);
5019 /* Check for duplicate equivalences being loaded from different modules */
5020 duplicate = false;
5021 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
5023 if (equiv->module && head->module
5024 && strcmp (equiv->module, head->module) == 0)
5026 duplicate = true;
5027 break;
5031 if (duplicate)
5033 for (eq = head; eq; eq = head)
5035 head = eq->eq;
5036 gfc_free_expr (eq->expr);
5037 free (eq);
5041 if (end == NULL)
5042 gfc_current_ns->equiv = head;
5043 else
5044 end->next = head;
5046 if (head != NULL)
5047 end = head;
5049 mio_rparen ();
5052 mio_rparen ();
5053 in_load_equiv = false;
5057 /* This function loads OpenMP user defined reductions. */
5058 static void
5059 load_omp_udrs (void)
5061 mio_lparen ();
5062 while (peek_atom () != ATOM_RPAREN)
5064 const char *name = NULL, *newname;
5065 char *altname;
5066 gfc_typespec ts;
5067 gfc_symtree *st;
5068 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
5070 mio_lparen ();
5071 mio_pool_string (&name);
5072 gfc_clear_ts (&ts);
5073 mio_typespec (&ts);
5074 if (startswith (name, "operator "))
5076 const char *p = name + sizeof ("operator ") - 1;
5077 if (strcmp (p, "+") == 0)
5078 rop = OMP_REDUCTION_PLUS;
5079 else if (strcmp (p, "*") == 0)
5080 rop = OMP_REDUCTION_TIMES;
5081 else if (strcmp (p, "-") == 0)
5082 rop = OMP_REDUCTION_MINUS;
5083 else if (strcmp (p, ".and.") == 0)
5084 rop = OMP_REDUCTION_AND;
5085 else if (strcmp (p, ".or.") == 0)
5086 rop = OMP_REDUCTION_OR;
5087 else if (strcmp (p, ".eqv.") == 0)
5088 rop = OMP_REDUCTION_EQV;
5089 else if (strcmp (p, ".neqv.") == 0)
5090 rop = OMP_REDUCTION_NEQV;
5092 altname = NULL;
5093 if (rop == OMP_REDUCTION_USER && name[0] == '.')
5095 size_t len = strlen (name + 1);
5096 altname = XALLOCAVEC (char, len);
5097 gcc_assert (name[len] == '.');
5098 memcpy (altname, name + 1, len - 1);
5099 altname[len - 1] = '\0';
5101 newname = name;
5102 if (rop == OMP_REDUCTION_USER)
5103 newname = find_use_name (altname ? altname : name, !!altname);
5104 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
5105 newname = NULL;
5106 if (newname == NULL)
5108 skip_list (1);
5109 continue;
5111 if (altname && newname != altname)
5113 size_t len = strlen (newname);
5114 altname = XALLOCAVEC (char, len + 3);
5115 altname[0] = '.';
5116 memcpy (altname + 1, newname, len);
5117 altname[len + 1] = '.';
5118 altname[len + 2] = '\0';
5119 name = gfc_get_string ("%s", altname);
5121 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
5122 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
5123 if (udr)
5125 require_atom (ATOM_INTEGER);
5126 pointer_info *p = get_integer (atom_int);
5127 if (strcmp (p->u.rsym.module, udr->omp_out->module))
5129 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
5130 "module %s at %L",
5131 p->u.rsym.module, &gfc_current_locus);
5132 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
5133 "%s at %L",
5134 udr->omp_out->module, &udr->where);
5136 skip_list (1);
5137 continue;
5139 udr = gfc_get_omp_udr ();
5140 udr->name = name;
5141 udr->rop = rop;
5142 udr->ts = ts;
5143 udr->where = gfc_current_locus;
5144 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
5145 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
5146 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
5147 false);
5148 if (peek_atom () != ATOM_RPAREN)
5150 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
5151 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
5152 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5153 udr->initializer_ns, true);
5155 if (st)
5157 udr->next = st->n.omp_udr;
5158 st->n.omp_udr = udr;
5160 else
5162 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
5163 st->n.omp_udr = udr;
5165 mio_rparen ();
5167 mio_rparen ();
5171 /* Recursive function to traverse the pointer_info tree and load a
5172 needed symbol. We return nonzero if we load a symbol and stop the
5173 traversal, because the act of loading can alter the tree. */
5175 static int
5176 load_needed (pointer_info *p)
5178 gfc_namespace *ns;
5179 pointer_info *q;
5180 gfc_symbol *sym;
5181 int rv;
5183 rv = 0;
5184 if (p == NULL)
5185 return rv;
5187 rv |= load_needed (p->left);
5188 rv |= load_needed (p->right);
5190 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
5191 return rv;
5193 p->u.rsym.state = USED;
5195 set_module_locus (&p->u.rsym.where);
5197 sym = p->u.rsym.sym;
5198 if (sym == NULL)
5200 q = get_integer (p->u.rsym.ns);
5202 ns = (gfc_namespace *) q->u.pointer;
5203 if (ns == NULL)
5205 /* Create an interface namespace if necessary. These are
5206 the namespaces that hold the formal parameters of module
5207 procedures. */
5209 ns = gfc_get_namespace (NULL, 0);
5210 associate_integer_pointer (q, ns);
5213 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
5214 doesn't go pear-shaped if the symbol is used. */
5215 if (!ns->proc_name)
5216 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
5217 1, &ns->proc_name);
5219 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
5220 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
5221 sym->module = gfc_get_string ("%s", p->u.rsym.module);
5222 if (p->u.rsym.binding_label)
5223 sym->binding_label = IDENTIFIER_POINTER (get_identifier
5224 (p->u.rsym.binding_label));
5226 associate_integer_pointer (p, sym);
5229 mio_symbol (sym);
5230 sym->attr.use_assoc = 1;
5232 /* Unliked derived types, a STRUCTURE may share names with other symbols.
5233 We greedily converted the symbol name to lowercase before we knew its
5234 type, so now we must fix it. */
5235 if (sym->attr.flavor == FL_STRUCT)
5236 sym->name = gfc_dt_upper_string (sym->name);
5238 /* Mark as only or rename for later diagnosis for explicitly imported
5239 but not used warnings; don't mark internal symbols such as __vtab,
5240 __def_init etc. Only mark them if they have been explicitly loaded. */
5242 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
5244 gfc_use_rename *u;
5246 /* Search the use/rename list for the variable; if the variable is
5247 found, mark it. */
5248 for (u = gfc_rename_list; u; u = u->next)
5250 if (strcmp (u->use_name, sym->name) == 0)
5252 sym->attr.use_only = 1;
5253 break;
5258 if (p->u.rsym.renamed)
5259 sym->attr.use_rename = 1;
5261 return 1;
5265 /* Recursive function for cleaning up things after a module has been read. */
5267 static void
5268 read_cleanup (pointer_info *p)
5270 gfc_symtree *st;
5271 pointer_info *q;
5273 if (p == NULL)
5274 return;
5276 read_cleanup (p->left);
5277 read_cleanup (p->right);
5279 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
5281 gfc_namespace *ns;
5282 /* Add hidden symbols to the symtree. */
5283 q = get_integer (p->u.rsym.ns);
5284 ns = (gfc_namespace *) q->u.pointer;
5286 if (!p->u.rsym.sym->attr.vtype
5287 && !p->u.rsym.sym->attr.vtab)
5288 st = gfc_get_unique_symtree (ns);
5289 else
5291 /* There is no reason to use 'unique_symtrees' for vtabs or
5292 vtypes - their name is fine for a symtree and reduces the
5293 namespace pollution. */
5294 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
5295 if (!st)
5296 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
5299 st->n.sym = p->u.rsym.sym;
5300 st->n.sym->refs++;
5302 /* Fixup any symtree references. */
5303 p->u.rsym.symtree = st;
5304 resolve_fixups (p->u.rsym.stfixup, st);
5305 p->u.rsym.stfixup = NULL;
5308 /* Free unused symbols. */
5309 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
5310 gfc_free_symbol (p->u.rsym.sym);
5314 /* It is not quite enough to check for ambiguity in the symbols by
5315 the loaded symbol and the new symbol not being identical. */
5316 static bool
5317 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
5319 gfc_symbol *rsym;
5320 module_locus locus;
5321 symbol_attribute attr;
5322 gfc_symbol *st_sym;
5324 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
5326 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
5327 "current program unit", st->name, module_name);
5328 return true;
5331 st_sym = st->n.sym;
5332 rsym = info->u.rsym.sym;
5333 if (st_sym == rsym)
5334 return false;
5336 if (st_sym->attr.vtab || st_sym->attr.vtype)
5337 return false;
5339 /* If the existing symbol is generic from a different module and
5340 the new symbol is generic there can be no ambiguity. */
5341 if (st_sym->attr.generic
5342 && st_sym->module
5343 && st_sym->module != module_name)
5345 /* The new symbol's attributes have not yet been read. Since
5346 we need attr.generic, read it directly. */
5347 get_module_locus (&locus);
5348 set_module_locus (&info->u.rsym.where);
5349 mio_lparen ();
5350 attr.generic = 0;
5351 mio_symbol_attribute (&attr);
5352 set_module_locus (&locus);
5353 if (attr.generic)
5354 return false;
5357 return true;
5361 /* Read a module file. */
5363 static void
5364 read_module (void)
5366 module_locus operator_interfaces, user_operators, omp_udrs;
5367 const char *p;
5368 char name[GFC_MAX_SYMBOL_LEN + 1];
5369 int i;
5370 /* Workaround -Wmaybe-uninitialized false positive during
5371 profiledbootstrap by initializing them. */
5372 int ambiguous = 0, j, nuse, symbol = 0;
5373 pointer_info *info, *q;
5374 gfc_use_rename *u = NULL;
5375 gfc_symtree *st;
5376 gfc_symbol *sym;
5378 get_module_locus (&operator_interfaces); /* Skip these for now. */
5379 skip_list ();
5381 get_module_locus (&user_operators);
5382 skip_list ();
5383 skip_list ();
5385 /* Skip commons and equivalences for now. */
5386 skip_list ();
5387 skip_list ();
5389 /* Skip OpenMP UDRs. */
5390 get_module_locus (&omp_udrs);
5391 skip_list ();
5393 mio_lparen ();
5395 /* Create the fixup nodes for all the symbols. */
5397 while (peek_atom () != ATOM_RPAREN)
5399 char* bind_label;
5400 require_atom (ATOM_INTEGER);
5401 info = get_integer (atom_int);
5403 info->type = P_SYMBOL;
5404 info->u.rsym.state = UNUSED;
5406 info->u.rsym.true_name = read_string ();
5407 info->u.rsym.module = read_string ();
5408 bind_label = read_string ();
5409 if (strlen (bind_label))
5410 info->u.rsym.binding_label = bind_label;
5411 else
5412 XDELETEVEC (bind_label);
5414 require_atom (ATOM_INTEGER);
5415 info->u.rsym.ns = atom_int;
5417 get_module_locus (&info->u.rsym.where);
5419 /* See if the symbol has already been loaded by a previous module.
5420 If so, we reference the existing symbol and prevent it from
5421 being loaded again. This should not happen if the symbol being
5422 read is an index for an assumed shape dummy array (ns != 1). */
5424 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5426 if (sym == NULL
5427 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5429 skip_list ();
5430 continue;
5433 info->u.rsym.state = USED;
5434 info->u.rsym.sym = sym;
5435 /* The current symbol has already been loaded, so we can avoid loading
5436 it again. However, if it is a derived type, some of its components
5437 can be used in expressions in the module. To avoid the module loading
5438 failing, we need to associate the module's component pointer indexes
5439 with the existing symbol's component pointers. */
5440 if (gfc_fl_struct (sym->attr.flavor))
5442 gfc_component *c;
5444 /* First seek to the symbol's component list. */
5445 mio_lparen (); /* symbol opening. */
5446 skip_list (); /* skip symbol attribute. */
5448 mio_lparen (); /* component list opening. */
5449 for (c = sym->components; c; c = c->next)
5451 pointer_info *p;
5452 const char *comp_name = NULL;
5453 int n = 0;
5455 mio_lparen (); /* component opening. */
5456 mio_integer (&n);
5457 p = get_integer (n);
5458 if (p->u.pointer == NULL)
5459 associate_integer_pointer (p, c);
5460 mio_pool_string (&comp_name);
5461 if (comp_name != c->name)
5463 gfc_fatal_error ("Mismatch in components of derived type "
5464 "%qs from %qs at %C: expecting %qs, "
5465 "but got %qs", sym->name, sym->module,
5466 c->name, comp_name);
5468 skip_list (1); /* component end. */
5470 mio_rparen (); /* component list closing. */
5472 skip_list (1); /* symbol end. */
5474 else
5475 skip_list ();
5477 /* Some symbols do not have a namespace (eg. formal arguments),
5478 so the automatic "unique symtree" mechanism must be suppressed
5479 by marking them as referenced. */
5480 q = get_integer (info->u.rsym.ns);
5481 if (q->u.pointer == NULL)
5483 info->u.rsym.referenced = 1;
5484 continue;
5488 mio_rparen ();
5490 /* Parse the symtree lists. This lets us mark which symbols need to
5491 be loaded. Renaming is also done at this point by replacing the
5492 symtree name. */
5494 mio_lparen ();
5496 while (peek_atom () != ATOM_RPAREN)
5498 mio_internal_string (name);
5499 mio_integer (&ambiguous);
5500 mio_integer (&symbol);
5502 info = get_integer (symbol);
5504 /* See how many use names there are. If none, go through the start
5505 of the loop at least once. */
5506 nuse = number_use_names (name, false);
5507 info->u.rsym.renamed = nuse ? 1 : 0;
5509 if (nuse == 0)
5510 nuse = 1;
5512 for (j = 1; j <= nuse; j++)
5514 /* Get the jth local name for this symbol. */
5515 p = find_use_name_n (name, &j, false);
5517 if (p == NULL && strcmp (name, module_name) == 0)
5518 p = name;
5520 /* Exception: Always import vtabs & vtypes. */
5521 if (p == NULL && name[0] == '_'
5522 && (startswith (name, "__vtab_")
5523 || startswith (name, "__vtype_")))
5524 p = name;
5526 /* Skip symtree nodes not in an ONLY clause, unless there
5527 is an existing symtree loaded from another USE statement. */
5528 if (p == NULL)
5530 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5531 if (st != NULL
5532 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5533 && st->n.sym->module != NULL
5534 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5536 info->u.rsym.symtree = st;
5537 info->u.rsym.sym = st->n.sym;
5539 continue;
5542 /* If a symbol of the same name and module exists already,
5543 this symbol, which is not in an ONLY clause, must not be
5544 added to the namespace(11.3.2). Note that find_symbol
5545 only returns the first occurrence that it finds. */
5546 if (!only_flag && !info->u.rsym.renamed
5547 && strcmp (name, module_name) != 0
5548 && find_symbol (gfc_current_ns->sym_root, name,
5549 module_name, 0))
5550 continue;
5552 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5554 if (st != NULL
5555 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5557 /* Check for ambiguous symbols. */
5558 if (check_for_ambiguous (st, info))
5559 st->ambiguous = 1;
5560 else
5561 info->u.rsym.symtree = st;
5563 else
5565 if (st)
5567 /* This symbol is host associated from a module in a
5568 submodule. Hide it with a unique symtree. */
5569 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5570 s->n.sym = st->n.sym;
5571 st->n.sym = NULL;
5573 else
5575 /* Create a symtree node in the current namespace for this
5576 symbol. */
5577 st = check_unique_name (p)
5578 ? gfc_get_unique_symtree (gfc_current_ns)
5579 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5580 st->ambiguous = ambiguous;
5583 sym = info->u.rsym.sym;
5585 /* Create a symbol node if it doesn't already exist. */
5586 if (sym == NULL)
5588 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5589 gfc_current_ns);
5590 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5591 sym = info->u.rsym.sym;
5592 sym->module = gfc_get_string ("%s", info->u.rsym.module);
5594 if (info->u.rsym.binding_label)
5596 tree id = get_identifier (info->u.rsym.binding_label);
5597 sym->binding_label = IDENTIFIER_POINTER (id);
5601 st->n.sym = sym;
5602 st->n.sym->refs++;
5604 if (strcmp (name, p) != 0)
5605 sym->attr.use_rename = 1;
5607 if (name[0] != '_'
5608 || (!startswith (name, "__vtab_")
5609 && !startswith (name, "__vtype_")))
5610 sym->attr.use_only = only_flag;
5612 /* Store the symtree pointing to this symbol. */
5613 info->u.rsym.symtree = st;
5615 if (info->u.rsym.state == UNUSED)
5616 info->u.rsym.state = NEEDED;
5617 info->u.rsym.referenced = 1;
5622 mio_rparen ();
5624 /* Load intrinsic operator interfaces. */
5625 set_module_locus (&operator_interfaces);
5626 mio_lparen ();
5628 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5630 gfc_use_rename *u = NULL, *v = NULL;
5631 int j = i;
5633 if (i == INTRINSIC_USER)
5634 continue;
5636 if (only_flag)
5638 u = find_use_operator ((gfc_intrinsic_op) i);
5640 /* F2018:10.1.5.5.1 requires same interpretation of old and new-style
5641 relational operators. Special handling for USE, ONLY. */
5642 switch (i)
5644 case INTRINSIC_EQ:
5645 j = INTRINSIC_EQ_OS;
5646 break;
5647 case INTRINSIC_EQ_OS:
5648 j = INTRINSIC_EQ;
5649 break;
5650 case INTRINSIC_NE:
5651 j = INTRINSIC_NE_OS;
5652 break;
5653 case INTRINSIC_NE_OS:
5654 j = INTRINSIC_NE;
5655 break;
5656 case INTRINSIC_GT:
5657 j = INTRINSIC_GT_OS;
5658 break;
5659 case INTRINSIC_GT_OS:
5660 j = INTRINSIC_GT;
5661 break;
5662 case INTRINSIC_GE:
5663 j = INTRINSIC_GE_OS;
5664 break;
5665 case INTRINSIC_GE_OS:
5666 j = INTRINSIC_GE;
5667 break;
5668 case INTRINSIC_LT:
5669 j = INTRINSIC_LT_OS;
5670 break;
5671 case INTRINSIC_LT_OS:
5672 j = INTRINSIC_LT;
5673 break;
5674 case INTRINSIC_LE:
5675 j = INTRINSIC_LE_OS;
5676 break;
5677 case INTRINSIC_LE_OS:
5678 j = INTRINSIC_LE;
5679 break;
5680 default:
5681 break;
5684 if (j != i)
5685 v = find_use_operator ((gfc_intrinsic_op) j);
5687 if (u == NULL && v == NULL)
5689 skip_list ();
5690 continue;
5693 if (u)
5694 u->found = 1;
5695 if (v)
5696 v->found = 1;
5699 mio_interface (&gfc_current_ns->op[i]);
5700 if (!gfc_current_ns->op[i] && !gfc_current_ns->op[j])
5702 if (u)
5703 u->found = 0;
5704 if (v)
5705 v->found = 0;
5709 mio_rparen ();
5711 /* Load generic and user operator interfaces. These must follow the
5712 loading of symtree because otherwise symbols can be marked as
5713 ambiguous. */
5715 set_module_locus (&user_operators);
5717 load_operator_interfaces ();
5718 load_generic_interfaces ();
5720 load_commons ();
5721 load_equiv ();
5723 /* Load OpenMP user defined reductions. */
5724 set_module_locus (&omp_udrs);
5725 load_omp_udrs ();
5727 /* At this point, we read those symbols that are needed but haven't
5728 been loaded yet. If one symbol requires another, the other gets
5729 marked as NEEDED if its previous state was UNUSED. */
5731 while (load_needed (pi_root));
5733 /* Make sure all elements of the rename-list were found in the module. */
5735 for (u = gfc_rename_list; u; u = u->next)
5737 if (u->found)
5738 continue;
5740 if (u->op == INTRINSIC_NONE)
5742 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5743 u->use_name, &u->where, module_name);
5744 continue;
5747 if (u->op == INTRINSIC_USER)
5749 gfc_error ("User operator %qs referenced at %L not found "
5750 "in module %qs", u->use_name, &u->where, module_name);
5751 continue;
5754 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5755 "in module %qs", gfc_op2string (u->op), &u->where,
5756 module_name);
5759 /* Clean up symbol nodes that were never loaded, create references
5760 to hidden symbols. */
5762 read_cleanup (pi_root);
5766 /* Given an access type that is specific to an entity and the default
5767 access, return nonzero if the entity is publicly accessible. If the
5768 element is declared as PUBLIC, then it is public; if declared
5769 PRIVATE, then private, and otherwise it is public unless the default
5770 access in this context has been declared PRIVATE. */
5772 static bool dump_smod = false;
5774 static bool
5775 check_access (gfc_access specific_access, gfc_access default_access)
5777 if (dump_smod)
5778 return true;
5780 if (specific_access == ACCESS_PUBLIC)
5781 return true;
5782 if (specific_access == ACCESS_PRIVATE)
5783 return false;
5785 if (flag_module_private)
5786 return default_access == ACCESS_PUBLIC;
5787 else
5788 return default_access != ACCESS_PRIVATE;
5792 bool
5793 gfc_check_symbol_access (gfc_symbol *sym)
5795 if (sym->attr.vtab || sym->attr.vtype)
5796 return true;
5797 else
5798 return check_access (sym->attr.access, sym->ns->default_access);
5802 /* A structure to remember which commons we've already written. */
5804 struct written_common
5806 BBT_HEADER(written_common);
5807 const char *name, *label;
5810 static struct written_common *written_commons = NULL;
5812 /* Comparison function used for balancing the binary tree. */
5814 static int
5815 compare_written_commons (void *a1, void *b1)
5817 const char *aname = ((struct written_common *) a1)->name;
5818 const char *alabel = ((struct written_common *) a1)->label;
5819 const char *bname = ((struct written_common *) b1)->name;
5820 const char *blabel = ((struct written_common *) b1)->label;
5821 int c = strcmp (aname, bname);
5823 return (c != 0 ? c : strcmp (alabel, blabel));
5826 /* Free a list of written commons. */
5828 static void
5829 free_written_common (struct written_common *w)
5831 if (!w)
5832 return;
5834 if (w->left)
5835 free_written_common (w->left);
5836 if (w->right)
5837 free_written_common (w->right);
5839 free (w);
5842 /* Write a common block to the module -- recursive helper function. */
5844 static void
5845 write_common_0 (gfc_symtree *st, bool this_module)
5847 gfc_common_head *p;
5848 const char * name;
5849 int flags;
5850 const char *label;
5851 struct written_common *w;
5852 bool write_me = true;
5854 if (st == NULL)
5855 return;
5857 write_common_0 (st->left, this_module);
5859 /* We will write out the binding label, or "" if no label given. */
5860 name = st->n.common->name;
5861 p = st->n.common;
5862 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
5864 /* Check if we've already output this common. */
5865 w = written_commons;
5866 while (w)
5868 int c = strcmp (name, w->name);
5869 c = (c != 0 ? c : strcmp (label, w->label));
5870 if (c == 0)
5871 write_me = false;
5873 w = (c < 0) ? w->left : w->right;
5876 if (this_module && p->use_assoc)
5877 write_me = false;
5879 if (write_me)
5881 /* Write the common to the module. */
5882 mio_lparen ();
5883 mio_pool_string (&name);
5885 mio_symbol_ref (&p->head);
5886 flags = p->saved ? 1 : 0;
5887 if (p->threadprivate)
5888 flags |= 2;
5889 flags |= p->omp_device_type << 2;
5890 mio_integer (&flags);
5892 /* Write out whether the common block is bind(c) or not. */
5893 mio_integer (&(p->is_bind_c));
5895 mio_pool_string (&label);
5896 mio_rparen ();
5898 /* Record that we have written this common. */
5899 w = XCNEW (struct written_common);
5900 w->name = p->name;
5901 w->label = label;
5902 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5905 write_common_0 (st->right, this_module);
5909 /* Write a common, by initializing the list of written commons, calling
5910 the recursive function write_common_0() and cleaning up afterwards. */
5912 static void
5913 write_common (gfc_symtree *st)
5915 written_commons = NULL;
5916 write_common_0 (st, true);
5917 write_common_0 (st, false);
5918 free_written_common (written_commons);
5919 written_commons = NULL;
5923 /* Write the blank common block to the module. */
5925 static void
5926 write_blank_common (void)
5928 const char * name = BLANK_COMMON_NAME;
5929 int saved;
5930 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5931 this, but it hasn't been checked. Just making it so for now. */
5932 int is_bind_c = 0;
5934 if (gfc_current_ns->blank_common.head == NULL)
5935 return;
5937 mio_lparen ();
5939 mio_pool_string (&name);
5941 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5942 saved = gfc_current_ns->blank_common.saved;
5943 mio_integer (&saved);
5945 /* Write out whether the common block is bind(c) or not. */
5946 mio_integer (&is_bind_c);
5948 /* Write out an empty binding label. */
5949 write_atom (ATOM_STRING, "");
5951 mio_rparen ();
5955 /* Write equivalences to the module. */
5957 static void
5958 write_equiv (void)
5960 gfc_equiv *eq, *e;
5961 int num;
5963 num = 0;
5964 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5966 mio_lparen ();
5968 for (e = eq; e; e = e->eq)
5970 if (e->module == NULL)
5971 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5972 mio_allocated_string (e->module);
5973 mio_expr (&e->expr);
5976 num++;
5977 mio_rparen ();
5982 /* Write a symbol to the module. */
5984 static void
5985 write_symbol (int n, gfc_symbol *sym)
5987 const char *label;
5989 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5990 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
5992 mio_integer (&n);
5994 if (gfc_fl_struct (sym->attr.flavor))
5996 const char *name;
5997 name = gfc_dt_upper_string (sym->name);
5998 mio_pool_string (&name);
6000 else
6001 mio_pool_string (&sym->name);
6003 mio_pool_string (&sym->module);
6004 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
6006 label = sym->binding_label;
6007 mio_pool_string (&label);
6009 else
6010 write_atom (ATOM_STRING, "");
6012 mio_pointer_ref (&sym->ns);
6014 mio_symbol (sym);
6015 write_char ('\n');
6019 /* Recursive traversal function to write the initial set of symbols to
6020 the module. We check to see if the symbol should be written
6021 according to the access specification. */
6023 static void
6024 write_symbol0 (gfc_symtree *st)
6026 gfc_symbol *sym;
6027 pointer_info *p;
6028 bool dont_write = false;
6030 if (st == NULL)
6031 return;
6033 write_symbol0 (st->left);
6035 sym = st->n.sym;
6036 if (sym->module == NULL)
6037 sym->module = module_name;
6039 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6040 && !sym->attr.subroutine && !sym->attr.function)
6041 dont_write = true;
6043 if (!gfc_check_symbol_access (sym))
6044 dont_write = true;
6046 if (!dont_write)
6048 p = get_pointer (sym);
6049 if (p->type == P_UNKNOWN)
6050 p->type = P_SYMBOL;
6052 if (p->u.wsym.state != WRITTEN)
6054 write_symbol (p->integer, sym);
6055 p->u.wsym.state = WRITTEN;
6059 write_symbol0 (st->right);
6063 static void
6064 write_omp_udr (gfc_omp_udr *udr)
6066 switch (udr->rop)
6068 case OMP_REDUCTION_USER:
6069 /* Non-operators can't be used outside of the module. */
6070 if (udr->name[0] != '.')
6071 return;
6072 else
6074 gfc_symtree *st;
6075 size_t len = strlen (udr->name + 1);
6076 char *name = XALLOCAVEC (char, len);
6077 memcpy (name, udr->name, len - 1);
6078 name[len - 1] = '\0';
6079 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
6080 /* If corresponding user operator is private, don't write
6081 the UDR. */
6082 if (st != NULL)
6084 gfc_user_op *uop = st->n.uop;
6085 if (!check_access (uop->access, uop->ns->default_access))
6086 return;
6089 break;
6090 case OMP_REDUCTION_PLUS:
6091 case OMP_REDUCTION_MINUS:
6092 case OMP_REDUCTION_TIMES:
6093 case OMP_REDUCTION_AND:
6094 case OMP_REDUCTION_OR:
6095 case OMP_REDUCTION_EQV:
6096 case OMP_REDUCTION_NEQV:
6097 /* If corresponding operator is private, don't write the UDR. */
6098 if (!check_access (gfc_current_ns->operator_access[udr->rop],
6099 gfc_current_ns->default_access))
6100 return;
6101 break;
6102 default:
6103 break;
6105 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
6107 /* If derived type is private, don't write the UDR. */
6108 if (!gfc_check_symbol_access (udr->ts.u.derived))
6109 return;
6112 mio_lparen ();
6113 mio_pool_string (&udr->name);
6114 mio_typespec (&udr->ts);
6115 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
6116 if (udr->initializer_ns)
6117 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
6118 udr->initializer_ns, true);
6119 mio_rparen ();
6123 static void
6124 write_omp_udrs (gfc_symtree *st)
6126 if (st == NULL)
6127 return;
6129 write_omp_udrs (st->left);
6130 gfc_omp_udr *udr;
6131 for (udr = st->n.omp_udr; udr; udr = udr->next)
6132 write_omp_udr (udr);
6133 write_omp_udrs (st->right);
6137 /* Type for the temporary tree used when writing secondary symbols. */
6139 struct sorted_pointer_info
6141 BBT_HEADER (sorted_pointer_info);
6143 pointer_info *p;
6146 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
6148 /* Recursively traverse the temporary tree, free its contents. */
6150 static void
6151 free_sorted_pointer_info_tree (sorted_pointer_info *p)
6153 if (!p)
6154 return;
6156 free_sorted_pointer_info_tree (p->left);
6157 free_sorted_pointer_info_tree (p->right);
6159 free (p);
6162 /* Comparison function for the temporary tree. */
6164 static int
6165 compare_sorted_pointer_info (void *_spi1, void *_spi2)
6167 sorted_pointer_info *spi1, *spi2;
6168 spi1 = (sorted_pointer_info *)_spi1;
6169 spi2 = (sorted_pointer_info *)_spi2;
6171 if (spi1->p->integer < spi2->p->integer)
6172 return -1;
6173 if (spi1->p->integer > spi2->p->integer)
6174 return 1;
6175 return 0;
6179 /* Finds the symbols that need to be written and collects them in the
6180 sorted_pi tree so that they can be traversed in an order
6181 independent of memory addresses. */
6183 static void
6184 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
6186 if (!p)
6187 return;
6189 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
6191 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
6192 sp->p = p;
6194 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
6197 find_symbols_to_write (tree, p->left);
6198 find_symbols_to_write (tree, p->right);
6202 /* Recursive function that traverses the tree of symbols that need to be
6203 written and writes them in order. */
6205 static void
6206 write_symbol1_recursion (sorted_pointer_info *sp)
6208 if (!sp)
6209 return;
6211 write_symbol1_recursion (sp->left);
6213 pointer_info *p1 = sp->p;
6214 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
6216 p1->u.wsym.state = WRITTEN;
6217 write_symbol (p1->integer, p1->u.wsym.sym);
6218 p1->u.wsym.sym->attr.public_used = 1;
6220 write_symbol1_recursion (sp->right);
6224 /* Write the secondary set of symbols to the module file. These are
6225 symbols that were not public yet are needed by the public symbols
6226 or another dependent symbol. The act of writing a symbol can add
6227 symbols to the pointer_info tree, so we return nonzero if a symbol
6228 was written and pass that information upwards. The caller will
6229 then call this function again until nothing was written. It uses
6230 the utility functions and a temporary tree to ensure a reproducible
6231 ordering of the symbol output and thus the module file. */
6233 static int
6234 write_symbol1 (pointer_info *p)
6236 if (!p)
6237 return 0;
6239 /* Put symbols that need to be written into a tree sorted on the
6240 integer field. */
6242 sorted_pointer_info *spi_root = NULL;
6243 find_symbols_to_write (&spi_root, p);
6245 /* No symbols to write, return. */
6246 if (!spi_root)
6247 return 0;
6249 /* Otherwise, write and free the tree again. */
6250 write_symbol1_recursion (spi_root);
6251 free_sorted_pointer_info_tree (spi_root);
6253 return 1;
6257 /* Write operator interfaces associated with a symbol. */
6259 static void
6260 write_operator (gfc_user_op *uop)
6262 static char nullstring[] = "";
6263 const char *p = nullstring;
6265 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
6266 return;
6268 mio_symbol_interface (&uop->name, &p, &uop->op);
6272 /* Write generic interfaces from the namespace sym_root. */
6274 static void
6275 write_generic (gfc_symtree *st)
6277 gfc_symbol *sym;
6279 if (st == NULL)
6280 return;
6282 write_generic (st->left);
6284 sym = st->n.sym;
6285 if (sym && !check_unique_name (st->name)
6286 && sym->generic && gfc_check_symbol_access (sym))
6288 if (!sym->module)
6289 sym->module = module_name;
6291 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
6294 write_generic (st->right);
6298 static void
6299 write_symtree (gfc_symtree *st)
6301 gfc_symbol *sym;
6302 pointer_info *p;
6304 sym = st->n.sym;
6306 /* A symbol in an interface body must not be visible in the
6307 module file. */
6308 if (sym->ns != gfc_current_ns
6309 && sym->ns->proc_name
6310 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
6311 return;
6313 if (!gfc_check_symbol_access (sym)
6314 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6315 && !sym->attr.subroutine && !sym->attr.function))
6316 return;
6318 if (check_unique_name (st->name))
6319 return;
6321 /* From F2003 onwards, intrinsic procedures are no longer subject to
6322 the restriction, "that an elemental intrinsic function here be of
6323 type integer or character and each argument must be an initialization
6324 expr of type integer or character" is lifted so that intrinsic
6325 procedures can be over-ridden. This requires that the intrinsic
6326 symbol not appear in the module file, thereby preventing ambiguity
6327 when USEd. */
6328 if (strcmp (sym->module, "(intrinsic)") == 0
6329 && (gfc_option.allow_std & GFC_STD_F2003))
6330 return;
6332 p = find_pointer (sym);
6333 if (p == NULL)
6334 gfc_internal_error ("write_symtree(): Symbol not written");
6336 mio_pool_string (&st->name);
6337 mio_integer (&st->ambiguous);
6338 mio_hwi (&p->integer);
6342 static void
6343 write_module (void)
6345 int i;
6347 /* Initialize the column counter. */
6348 module_column = 1;
6350 /* Write the operator interfaces. */
6351 mio_lparen ();
6353 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
6355 if (i == INTRINSIC_USER)
6356 continue;
6358 mio_interface (check_access (gfc_current_ns->operator_access[i],
6359 gfc_current_ns->default_access)
6360 ? &gfc_current_ns->op[i] : NULL);
6363 mio_rparen ();
6364 write_char ('\n');
6365 write_char ('\n');
6367 mio_lparen ();
6368 gfc_traverse_user_op (gfc_current_ns, write_operator);
6369 mio_rparen ();
6370 write_char ('\n');
6371 write_char ('\n');
6373 mio_lparen ();
6374 write_generic (gfc_current_ns->sym_root);
6375 mio_rparen ();
6376 write_char ('\n');
6377 write_char ('\n');
6379 mio_lparen ();
6380 write_blank_common ();
6381 write_common (gfc_current_ns->common_root);
6382 mio_rparen ();
6383 write_char ('\n');
6384 write_char ('\n');
6386 mio_lparen ();
6387 write_equiv ();
6388 mio_rparen ();
6389 write_char ('\n');
6390 write_char ('\n');
6392 mio_lparen ();
6393 write_omp_udrs (gfc_current_ns->omp_udr_root);
6394 mio_rparen ();
6395 write_char ('\n');
6396 write_char ('\n');
6398 /* Write symbol information. First we traverse all symbols in the
6399 primary namespace, writing those that need to be written.
6400 Sometimes writing one symbol will cause another to need to be
6401 written. A list of these symbols ends up on the write stack, and
6402 we end by popping the bottom of the stack and writing the symbol
6403 until the stack is empty. */
6405 mio_lparen ();
6407 write_symbol0 (gfc_current_ns->sym_root);
6408 while (write_symbol1 (pi_root))
6409 /* Nothing. */;
6411 mio_rparen ();
6413 write_char ('\n');
6414 write_char ('\n');
6416 mio_lparen ();
6417 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
6418 mio_rparen ();
6422 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
6423 true on success, false on failure. */
6425 static bool
6426 read_crc32_from_module_file (const char* filename, uLong* crc)
6428 FILE *file;
6429 char buf[4];
6430 unsigned int val;
6432 /* Open the file in binary mode. */
6433 if ((file = fopen (filename, "rb")) == NULL)
6434 return false;
6436 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
6437 file. See RFC 1952. */
6438 if (fseek (file, -8, SEEK_END) != 0)
6440 fclose (file);
6441 return false;
6444 /* Read the CRC32. */
6445 if (fread (buf, 1, 4, file) != 4)
6447 fclose (file);
6448 return false;
6451 /* Close the file. */
6452 fclose (file);
6454 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6455 + ((buf[3] & 0xFF) << 24);
6456 *crc = val;
6458 /* For debugging, the CRC value printed in hexadecimal should match
6459 the CRC printed by "zcat -l -v filename".
6460 printf("CRC of file %s is %x\n", filename, val); */
6462 return true;
6466 /* Given module, dump it to disk. If there was an error while
6467 processing the module, dump_flag will be set to zero and we delete
6468 the module file, even if it was already there. */
6470 static void
6471 dump_module (const char *name, int dump_flag)
6473 int n;
6474 char *filename, *filename_tmp;
6475 uLong crc, crc_old;
6477 module_name = gfc_get_string ("%s", name);
6479 if (dump_smod)
6481 name = submodule_name;
6482 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6484 else
6485 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6487 if (gfc_option.module_dir != NULL)
6489 n += strlen (gfc_option.module_dir);
6490 filename = (char *) alloca (n);
6491 strcpy (filename, gfc_option.module_dir);
6492 strcat (filename, name);
6494 else
6496 filename = (char *) alloca (n);
6497 strcpy (filename, name);
6500 if (dump_smod)
6501 strcat (filename, SUBMODULE_EXTENSION);
6502 else
6503 strcat (filename, MODULE_EXTENSION);
6505 /* Name of the temporary file used to write the module. */
6506 filename_tmp = (char *) alloca (n + 1);
6507 strcpy (filename_tmp, filename);
6508 strcat (filename_tmp, "0");
6510 /* There was an error while processing the module. We delete the
6511 module file, even if it was already there. */
6512 if (!dump_flag)
6514 remove (filename);
6515 return;
6518 if (gfc_cpp_makedep ())
6519 gfc_cpp_add_target (filename);
6521 /* Write the module to the temporary file. */
6522 module_fp = gzopen (filename_tmp, "w");
6523 if (module_fp == NULL)
6524 gfc_fatal_error ("Cannot open module file %qs for writing at %C: %s",
6525 filename_tmp, xstrerror (errno));
6527 /* Use lbasename to ensure module files are reproducible regardless
6528 of the build path (see the reproducible builds project). */
6529 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6530 MOD_VERSION, lbasename (gfc_source_file));
6532 /* Write the module itself. */
6533 iomode = IO_OUTPUT;
6535 init_pi_tree ();
6537 write_module ();
6539 free_pi_tree (pi_root);
6540 pi_root = NULL;
6542 write_char ('\n');
6544 if (gzclose (module_fp))
6545 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6546 filename_tmp, xstrerror (errno));
6548 /* Read the CRC32 from the gzip trailers of the module files and
6549 compare. */
6550 if (!read_crc32_from_module_file (filename_tmp, &crc)
6551 || !read_crc32_from_module_file (filename, &crc_old)
6552 || crc_old != crc)
6554 /* Module file have changed, replace the old one. */
6555 if (remove (filename) && errno != ENOENT)
6556 gfc_fatal_error ("Cannot delete module file %qs: %s", filename,
6557 xstrerror (errno));
6558 if (rename (filename_tmp, filename))
6559 gfc_fatal_error ("Cannot rename module file %qs to %qs: %s",
6560 filename_tmp, filename, xstrerror (errno));
6562 else
6564 if (remove (filename_tmp))
6565 gfc_fatal_error ("Cannot delete temporary module file %qs: %s",
6566 filename_tmp, xstrerror (errno));
6571 /* Suppress the output of a .smod file by module, if no module
6572 procedures have been seen. */
6573 static bool no_module_procedures;
6575 static void
6576 check_for_module_procedures (gfc_symbol *sym)
6578 if (sym && sym->attr.module_procedure)
6579 no_module_procedures = false;
6583 void
6584 gfc_dump_module (const char *name, int dump_flag)
6586 if (gfc_state_stack->state == COMP_SUBMODULE)
6587 dump_smod = true;
6588 else
6589 dump_smod =false;
6591 no_module_procedures = true;
6592 gfc_traverse_ns (gfc_current_ns, check_for_module_procedures);
6594 dump_module (name, dump_flag);
6596 if (no_module_procedures || dump_smod)
6597 return;
6599 /* Write a submodule file from a module. The 'dump_smod' flag switches
6600 off the check for PRIVATE entities. */
6601 dump_smod = true;
6602 submodule_name = module_name;
6603 dump_module (name, dump_flag);
6604 dump_smod = false;
6607 static void
6608 create_intrinsic_function (const char *name, int id,
6609 const char *modname, intmod_id module,
6610 bool subroutine, gfc_symbol *result_type)
6612 gfc_intrinsic_sym *isym;
6613 gfc_symtree *tmp_symtree;
6614 gfc_symbol *sym;
6616 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6617 if (tmp_symtree)
6619 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6620 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6621 return;
6622 gfc_error ("Symbol %qs at %C already declared", name);
6623 return;
6626 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6627 sym = tmp_symtree->n.sym;
6629 if (subroutine)
6631 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6632 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6633 sym->attr.subroutine = 1;
6635 else
6637 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6638 isym = gfc_intrinsic_function_by_id (isym_id);
6640 sym->attr.function = 1;
6641 if (result_type)
6643 sym->ts.type = BT_DERIVED;
6644 sym->ts.u.derived = result_type;
6645 sym->ts.is_c_interop = 1;
6646 isym->ts.f90_type = BT_VOID;
6647 isym->ts.type = BT_DERIVED;
6648 isym->ts.f90_type = BT_VOID;
6649 isym->ts.u.derived = result_type;
6650 isym->ts.is_c_interop = 1;
6653 gcc_assert (isym);
6655 sym->attr.flavor = FL_PROCEDURE;
6656 sym->attr.intrinsic = 1;
6658 sym->module = gfc_get_string ("%s", modname);
6659 sym->attr.use_assoc = 1;
6660 sym->from_intmod = module;
6661 sym->intmod_sym_id = id;
6665 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6666 the current namespace for all named constants, pointer types, and
6667 procedures in the module unless the only clause was used or a rename
6668 list was provided. */
6670 static void
6671 import_iso_c_binding_module (void)
6673 gfc_symbol *mod_sym = NULL, *return_type;
6674 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6675 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6676 const char *iso_c_module_name = "__iso_c_binding";
6677 gfc_use_rename *u;
6678 int i;
6679 bool want_c_ptr = false, want_c_funptr = false;
6681 /* Look only in the current namespace. */
6682 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6684 if (mod_symtree == NULL)
6686 /* symtree doesn't already exist in current namespace. */
6687 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6688 false);
6690 if (mod_symtree != NULL)
6691 mod_sym = mod_symtree->n.sym;
6692 else
6693 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6694 "create symbol for %s", iso_c_module_name);
6696 mod_sym->attr.flavor = FL_MODULE;
6697 mod_sym->attr.intrinsic = 1;
6698 mod_sym->module = gfc_get_string ("%s", iso_c_module_name);
6699 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6702 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6703 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6704 need C_(FUN)PTR. */
6705 for (u = gfc_rename_list; u; u = u->next)
6707 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6708 u->use_name) == 0)
6709 want_c_ptr = true;
6710 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
6711 u->use_name) == 0)
6712 want_c_ptr = true;
6713 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
6714 u->use_name) == 0)
6715 want_c_funptr = true;
6716 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
6717 u->use_name) == 0)
6718 want_c_funptr = true;
6719 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
6720 u->use_name) == 0)
6722 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6723 (iso_c_binding_symbol)
6724 ISOCBINDING_PTR,
6725 u->local_name[0] ? u->local_name
6726 : u->use_name,
6727 NULL, false);
6729 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
6730 u->use_name) == 0)
6732 c_funptr
6733 = generate_isocbinding_symbol (iso_c_module_name,
6734 (iso_c_binding_symbol)
6735 ISOCBINDING_FUNPTR,
6736 u->local_name[0] ? u->local_name
6737 : u->use_name,
6738 NULL, false);
6742 if ((want_c_ptr || !only_flag) && !c_ptr)
6743 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
6744 (iso_c_binding_symbol)
6745 ISOCBINDING_PTR,
6746 NULL, NULL, only_flag);
6747 if ((want_c_funptr || !only_flag) && !c_funptr)
6748 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
6749 (iso_c_binding_symbol)
6750 ISOCBINDING_FUNPTR,
6751 NULL, NULL, only_flag);
6753 /* Generate the symbols for the named constants representing
6754 the kinds for intrinsic data types. */
6755 for (i = 0; i < ISOCBINDING_NUMBER; i++)
6757 bool found = false;
6758 for (u = gfc_rename_list; u; u = u->next)
6759 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
6761 bool not_in_std;
6762 const char *name;
6763 u->found = 1;
6764 found = true;
6766 switch (i)
6768 #define NAMED_FUNCTION(a,b,c,d) \
6769 case a: \
6770 not_in_std = (gfc_option.allow_std & d) == 0; \
6771 name = b; \
6772 break;
6773 #define NAMED_SUBROUTINE(a,b,c,d) \
6774 case a: \
6775 not_in_std = (gfc_option.allow_std & d) == 0; \
6776 name = b; \
6777 break;
6778 #define NAMED_INTCST(a,b,c,d) \
6779 case a: \
6780 not_in_std = (gfc_option.allow_std & d) == 0; \
6781 name = b; \
6782 break;
6783 #define NAMED_UINTCST(a,b,c,d) \
6784 case a: \
6785 not_in_std = (gfc_option.allow_std & d) == 0; \
6786 name = b; \
6787 break;
6788 #define NAMED_REALCST(a,b,c,d) \
6789 case a: \
6790 not_in_std = (gfc_option.allow_std & d) == 0; \
6791 name = b; \
6792 break;
6793 #define NAMED_CMPXCST(a,b,c,d) \
6794 case a: \
6795 not_in_std = (gfc_option.allow_std & d) == 0; \
6796 name = b; \
6797 break;
6798 #include "iso-c-binding.def"
6799 default:
6800 not_in_std = false;
6801 name = "";
6804 if (not_in_std)
6806 gfc_error ("The symbol %qs, referenced at %L, is not "
6807 "in the selected standard", name, &u->where);
6808 continue;
6811 switch (i)
6813 #define NAMED_FUNCTION(a,b,c,d) \
6814 case a: \
6815 if (a == ISOCBINDING_LOC) \
6816 return_type = c_ptr->n.sym; \
6817 else if (a == ISOCBINDING_FUNLOC) \
6818 return_type = c_funptr->n.sym; \
6819 else \
6820 return_type = NULL; \
6821 create_intrinsic_function (u->local_name[0] \
6822 ? u->local_name : u->use_name, \
6823 a, iso_c_module_name, \
6824 INTMOD_ISO_C_BINDING, false, \
6825 return_type); \
6826 break;
6827 #define NAMED_SUBROUTINE(a,b,c,d) \
6828 case a: \
6829 create_intrinsic_function (u->local_name[0] ? u->local_name \
6830 : u->use_name, \
6831 a, iso_c_module_name, \
6832 INTMOD_ISO_C_BINDING, true, NULL); \
6833 break;
6834 #include "iso-c-binding.def"
6836 case ISOCBINDING_PTR:
6837 case ISOCBINDING_FUNPTR:
6838 /* Already handled above. */
6839 break;
6840 default:
6841 if (i == ISOCBINDING_NULL_PTR)
6842 tmp_symtree = c_ptr;
6843 else if (i == ISOCBINDING_NULL_FUNPTR)
6844 tmp_symtree = c_funptr;
6845 else
6846 tmp_symtree = NULL;
6847 generate_isocbinding_symbol (iso_c_module_name,
6848 (iso_c_binding_symbol) i,
6849 u->local_name[0]
6850 ? u->local_name : u->use_name,
6851 tmp_symtree, false);
6855 if (!found && !only_flag)
6857 /* Skip, if the symbol is not in the enabled standard. */
6858 switch (i)
6860 #define NAMED_FUNCTION(a,b,c,d) \
6861 case a: \
6862 if ((gfc_option.allow_std & d) == 0) \
6863 continue; \
6864 break;
6865 #define NAMED_SUBROUTINE(a,b,c,d) \
6866 case a: \
6867 if ((gfc_option.allow_std & d) == 0) \
6868 continue; \
6869 break;
6870 #define NAMED_INTCST(a,b,c,d) \
6871 case a: \
6872 if ((gfc_option.allow_std & d) == 0) \
6873 continue; \
6874 break;
6875 #define NAMED_UINTCST(a,b,c,d) \
6876 case a: \
6877 if ((gfc_option.allow_std & d) == 0) \
6878 continue; \
6879 break;
6880 #define NAMED_REALCST(a,b,c,d) \
6881 case a: \
6882 if ((gfc_option.allow_std & d) == 0) \
6883 continue; \
6884 break;
6885 #define NAMED_CMPXCST(a,b,c,d) \
6886 case a: \
6887 if ((gfc_option.allow_std & d) == 0) \
6888 continue; \
6889 break;
6890 #include "iso-c-binding.def"
6891 default:
6892 ; /* Not GFC_STD_* versioned. */
6895 switch (i)
6897 #define NAMED_FUNCTION(a,b,c,d) \
6898 case a: \
6899 if (a == ISOCBINDING_LOC) \
6900 return_type = c_ptr->n.sym; \
6901 else if (a == ISOCBINDING_FUNLOC) \
6902 return_type = c_funptr->n.sym; \
6903 else \
6904 return_type = NULL; \
6905 create_intrinsic_function (b, a, iso_c_module_name, \
6906 INTMOD_ISO_C_BINDING, false, \
6907 return_type); \
6908 break;
6909 #define NAMED_SUBROUTINE(a,b,c,d) \
6910 case a: \
6911 create_intrinsic_function (b, a, iso_c_module_name, \
6912 INTMOD_ISO_C_BINDING, true, NULL); \
6913 break;
6914 #include "iso-c-binding.def"
6916 case ISOCBINDING_PTR:
6917 case ISOCBINDING_FUNPTR:
6918 /* Already handled above. */
6919 break;
6920 default:
6921 if (i == ISOCBINDING_NULL_PTR)
6922 tmp_symtree = c_ptr;
6923 else if (i == ISOCBINDING_NULL_FUNPTR)
6924 tmp_symtree = c_funptr;
6925 else
6926 tmp_symtree = NULL;
6927 generate_isocbinding_symbol (iso_c_module_name,
6928 (iso_c_binding_symbol) i, NULL,
6929 tmp_symtree, false);
6934 for (u = gfc_rename_list; u; u = u->next)
6936 if (u->found)
6937 continue;
6939 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6940 "module ISO_C_BINDING", u->use_name, &u->where);
6945 /* Add an integer named constant from a given module. */
6947 static void
6948 create_int_parameter (const char *name, int value, const char *modname,
6949 intmod_id module, int id)
6951 gfc_symtree *tmp_symtree;
6952 gfc_symbol *sym;
6954 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6955 if (tmp_symtree != NULL)
6957 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6958 return;
6959 else
6960 gfc_error ("Symbol %qs already declared", name);
6963 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6964 sym = tmp_symtree->n.sym;
6966 sym->module = gfc_get_string ("%s", modname);
6967 sym->attr.flavor = FL_PARAMETER;
6968 sym->ts.type = BT_INTEGER;
6969 sym->ts.kind = gfc_default_integer_kind;
6970 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
6971 sym->attr.use_assoc = 1;
6972 sym->from_intmod = module;
6973 sym->intmod_sym_id = id;
6977 /* Value is already contained by the array constructor, but not
6978 yet the shape. */
6980 static void
6981 create_int_parameter_array (const char *name, int size, gfc_expr *value,
6982 const char *modname, intmod_id module, int id)
6984 gfc_symtree *tmp_symtree;
6985 gfc_symbol *sym;
6987 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6988 if (tmp_symtree != NULL)
6990 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6991 return;
6992 else
6993 gfc_error ("Symbol %qs already declared", name);
6996 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6997 sym = tmp_symtree->n.sym;
6999 sym->module = gfc_get_string ("%s", modname);
7000 sym->attr.flavor = FL_PARAMETER;
7001 sym->ts.type = BT_INTEGER;
7002 sym->ts.kind = gfc_default_integer_kind;
7003 sym->attr.use_assoc = 1;
7004 sym->from_intmod = module;
7005 sym->intmod_sym_id = id;
7006 sym->attr.dimension = 1;
7007 sym->as = gfc_get_array_spec ();
7008 sym->as->rank = 1;
7009 sym->as->type = AS_EXPLICIT;
7010 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
7011 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
7013 sym->value = value;
7014 sym->value->shape = gfc_get_shape (1);
7015 mpz_init_set_ui (sym->value->shape[0], size);
7019 /* Add an derived type for a given module. */
7021 static void
7022 create_derived_type (const char *name, const char *modname,
7023 intmod_id module, int id)
7025 gfc_symtree *tmp_symtree;
7026 gfc_symbol *sym, *dt_sym;
7027 gfc_interface *intr, *head;
7029 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
7030 if (tmp_symtree != NULL)
7032 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
7033 return;
7034 else
7035 gfc_error ("Symbol %qs already declared", name);
7038 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7039 sym = tmp_symtree->n.sym;
7040 sym->module = gfc_get_string ("%s", modname);
7041 sym->from_intmod = module;
7042 sym->intmod_sym_id = id;
7043 sym->attr.flavor = FL_PROCEDURE;
7044 sym->attr.function = 1;
7045 sym->attr.generic = 1;
7047 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
7048 gfc_current_ns, &tmp_symtree, false);
7049 dt_sym = tmp_symtree->n.sym;
7050 dt_sym->name = gfc_get_string ("%s", sym->name);
7051 dt_sym->attr.flavor = FL_DERIVED;
7052 dt_sym->attr.private_comp = 1;
7053 dt_sym->attr.zero_comp = 1;
7054 dt_sym->attr.use_assoc = 1;
7055 dt_sym->module = gfc_get_string ("%s", modname);
7056 dt_sym->from_intmod = module;
7057 dt_sym->intmod_sym_id = id;
7059 head = sym->generic;
7060 intr = gfc_get_interface ();
7061 intr->sym = dt_sym;
7062 intr->where = gfc_current_locus;
7063 intr->next = head;
7064 sym->generic = intr;
7065 sym->attr.if_source = IFSRC_DECL;
7069 /* Read the contents of the module file into a temporary buffer. */
7071 static void
7072 read_module_to_tmpbuf ()
7074 /* We don't know the uncompressed size, so enlarge the buffer as
7075 needed. */
7076 int cursz = 4096;
7077 int rsize = cursz;
7078 int len = 0;
7080 module_content = XNEWVEC (char, cursz);
7082 while (1)
7084 int nread = gzread (module_fp, module_content + len, rsize);
7085 len += nread;
7086 if (nread < rsize)
7087 break;
7088 cursz *= 2;
7089 module_content = XRESIZEVEC (char, module_content, cursz);
7090 rsize = cursz - len;
7093 module_content = XRESIZEVEC (char, module_content, len + 1);
7094 module_content[len] = '\0';
7096 module_pos = 0;
7100 /* USE the ISO_FORTRAN_ENV intrinsic module. */
7102 static void
7103 use_iso_fortran_env_module (void)
7105 static char mod[] = "iso_fortran_env";
7106 gfc_use_rename *u;
7107 gfc_symbol *mod_sym;
7108 gfc_symtree *mod_symtree;
7109 gfc_expr *expr;
7110 int i, j;
7112 intmod_sym symbol[] = {
7113 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
7114 #define NAMED_UINTCST(a,b,c,d) { a, b, 0, d },
7115 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
7116 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
7117 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
7118 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
7119 #include "iso-fortran-env.def"
7120 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
7122 i = 0;
7123 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
7124 #include "iso-fortran-env.def"
7126 #define NAMED_UINTCST(a,b,c,d) symbol[i++].value = c;
7127 #include "iso-fortran-env.def"
7129 /* Generate the symbol for the module itself. */
7130 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
7131 if (mod_symtree == NULL)
7133 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
7134 gcc_assert (mod_symtree);
7135 mod_sym = mod_symtree->n.sym;
7137 mod_sym->attr.flavor = FL_MODULE;
7138 mod_sym->attr.intrinsic = 1;
7139 mod_sym->module = gfc_get_string ("%s", mod);
7140 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
7142 else
7143 if (!mod_symtree->n.sym->attr.intrinsic)
7144 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
7145 "non-intrinsic module name used previously", mod);
7147 /* Generate the symbols for the module integer named constants. */
7149 for (i = 0; symbol[i].name; i++)
7151 bool found = false;
7152 for (u = gfc_rename_list; u; u = u->next)
7154 if (strcmp (symbol[i].name, u->use_name) == 0)
7156 found = true;
7157 u->found = 1;
7159 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
7160 "referenced at %L, is not in the selected "
7161 "standard", symbol[i].name, &u->where))
7162 continue;
7164 if ((flag_default_integer || flag_default_real_8)
7165 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7166 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
7167 "constant from intrinsic module "
7168 "ISO_FORTRAN_ENV at %L is incompatible with "
7169 "option %qs", &u->where,
7170 flag_default_integer
7171 ? "-fdefault-integer-8"
7172 : "-fdefault-real-8");
7173 switch (symbol[i].id)
7175 #define NAMED_INTCST(a,b,c,d) \
7176 case a:
7177 #include "iso-fortran-env.def"
7178 create_int_parameter (u->local_name[0] ? u->local_name
7179 : u->use_name,
7180 symbol[i].value, mod,
7181 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7182 break;
7184 #define NAMED_UINTCST(a,b,c,d) \
7185 case a:
7186 #include "iso-fortran-env.def"
7187 create_int_parameter (u->local_name[0] ? u->local_name
7188 : u->use_name,
7189 symbol[i].value, mod,
7190 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7191 break;
7193 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7194 case a:\
7195 expr = gfc_get_array_expr (BT_INTEGER, \
7196 gfc_default_integer_kind,\
7197 NULL); \
7198 for (j = 0; KINDS[j].kind != 0; j++) \
7199 gfc_constructor_append_expr (&expr->value.constructor, \
7200 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7201 KINDS[j].kind), NULL); \
7202 create_int_parameter_array (u->local_name[0] ? u->local_name \
7203 : u->use_name, \
7204 j, expr, mod, \
7205 INTMOD_ISO_FORTRAN_ENV, \
7206 symbol[i].id); \
7207 break;
7208 #include "iso-fortran-env.def"
7210 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7211 case a:
7212 #include "iso-fortran-env.def"
7213 create_derived_type (u->local_name[0] ? u->local_name
7214 : u->use_name,
7215 mod, INTMOD_ISO_FORTRAN_ENV,
7216 symbol[i].id);
7217 break;
7219 #define NAMED_FUNCTION(a,b,c,d) \
7220 case a:
7221 #include "iso-fortran-env.def"
7222 create_intrinsic_function (u->local_name[0] ? u->local_name
7223 : u->use_name,
7224 symbol[i].id, mod,
7225 INTMOD_ISO_FORTRAN_ENV, false,
7226 NULL);
7227 break;
7229 default:
7230 gcc_unreachable ();
7235 if (!found && !only_flag)
7237 if ((gfc_option.allow_std & symbol[i].standard) == 0)
7238 continue;
7240 if ((flag_default_integer || flag_default_real_8)
7241 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7242 gfc_warning_now (0,
7243 "Use of the NUMERIC_STORAGE_SIZE named constant "
7244 "from intrinsic module ISO_FORTRAN_ENV at %C is "
7245 "incompatible with option %s",
7246 flag_default_integer
7247 ? "-fdefault-integer-8" : "-fdefault-real-8");
7249 switch (symbol[i].id)
7251 #define NAMED_INTCST(a,b,c,d) \
7252 case a:
7253 #include "iso-fortran-env.def"
7254 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7255 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7256 break;
7258 #define NAMED_UINTCST(a,b,c,d) \
7259 case a:
7260 #include "iso-fortran-env.def"
7261 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7262 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7263 break;
7265 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7266 case a:\
7267 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
7268 NULL); \
7269 for (j = 0; KINDS[j].kind != 0; j++) \
7270 gfc_constructor_append_expr (&expr->value.constructor, \
7271 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7272 KINDS[j].kind), NULL); \
7273 create_int_parameter_array (symbol[i].name, j, expr, mod, \
7274 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
7275 break;
7276 #include "iso-fortran-env.def"
7278 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7279 case a:
7280 #include "iso-fortran-env.def"
7281 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
7282 symbol[i].id);
7283 break;
7285 #define NAMED_FUNCTION(a,b,c,d) \
7286 case a:
7287 #include "iso-fortran-env.def"
7288 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
7289 INTMOD_ISO_FORTRAN_ENV, false,
7290 NULL);
7291 break;
7293 default:
7294 gcc_unreachable ();
7299 for (u = gfc_rename_list; u; u = u->next)
7301 if (u->found)
7302 continue;
7304 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
7305 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
7310 /* Process a USE directive. */
7312 static void
7313 gfc_use_module (gfc_use_list *module)
7315 char *filename;
7316 gfc_state_data *p;
7317 int c, line, start;
7318 gfc_symtree *mod_symtree;
7319 gfc_use_list *use_stmt;
7320 locus old_locus = gfc_current_locus;
7322 gfc_current_locus = module->where;
7323 module_name = module->module_name;
7324 gfc_rename_list = module->rename;
7325 only_flag = module->only_flag;
7326 current_intmod = INTMOD_NONE;
7328 if (!only_flag)
7329 gfc_warning_now (OPT_Wuse_without_only,
7330 "USE statement at %C has no ONLY qualifier");
7332 if (gfc_state_stack->state == COMP_MODULE
7333 || module->submodule_name == NULL)
7335 filename = XALLOCAVEC (char, strlen (module_name)
7336 + strlen (MODULE_EXTENSION) + 1);
7337 strcpy (filename, module_name);
7338 strcat (filename, MODULE_EXTENSION);
7340 else
7342 filename = XALLOCAVEC (char, strlen (module->submodule_name)
7343 + strlen (SUBMODULE_EXTENSION) + 1);
7344 strcpy (filename, module->submodule_name);
7345 strcat (filename, SUBMODULE_EXTENSION);
7348 /* First, try to find an non-intrinsic module, unless the USE statement
7349 specified that the module is intrinsic. */
7350 module_fp = NULL;
7351 if (!module->intrinsic)
7352 module_fp = gzopen_included_file (filename, true, true);
7354 /* Then, see if it's an intrinsic one, unless the USE statement
7355 specified that the module is non-intrinsic. */
7356 if (module_fp == NULL && !module->non_intrinsic)
7358 if (strcmp (module_name, "iso_fortran_env") == 0
7359 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
7360 "intrinsic module at %C"))
7362 use_iso_fortran_env_module ();
7363 free_rename (module->rename);
7364 module->rename = NULL;
7365 gfc_current_locus = old_locus;
7366 module->intrinsic = true;
7367 return;
7370 if (strcmp (module_name, "iso_c_binding") == 0
7371 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
7373 import_iso_c_binding_module();
7374 free_rename (module->rename);
7375 module->rename = NULL;
7376 gfc_current_locus = old_locus;
7377 module->intrinsic = true;
7378 return;
7381 module_fp = gzopen_intrinsic_module (filename);
7383 if (module_fp == NULL && module->intrinsic)
7384 gfc_fatal_error ("Cannot find an intrinsic module named %qs at %C",
7385 module_name);
7387 /* Check for the IEEE modules, so we can mark their symbols
7388 accordingly when we read them. */
7389 if (strcmp (module_name, "ieee_features") == 0
7390 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
7392 current_intmod = INTMOD_IEEE_FEATURES;
7394 else if (strcmp (module_name, "ieee_exceptions") == 0
7395 && gfc_notify_std (GFC_STD_F2003,
7396 "IEEE_EXCEPTIONS module at %C"))
7398 current_intmod = INTMOD_IEEE_EXCEPTIONS;
7400 else if (strcmp (module_name, "ieee_arithmetic") == 0
7401 && gfc_notify_std (GFC_STD_F2003,
7402 "IEEE_ARITHMETIC module at %C"))
7404 current_intmod = INTMOD_IEEE_ARITHMETIC;
7408 if (module_fp == NULL)
7410 if (gfc_state_stack->state != COMP_SUBMODULE
7411 && module->submodule_name == NULL)
7412 gfc_fatal_error ("Cannot open module file %qs for reading at %C: %s",
7413 filename, xstrerror (errno));
7414 else
7415 gfc_fatal_error ("Module file %qs has not been generated, either "
7416 "because the module does not contain a MODULE "
7417 "PROCEDURE or there is an error in the module.",
7418 filename);
7421 /* Check that we haven't already USEd an intrinsic module with the
7422 same name. */
7424 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
7425 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
7426 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
7427 "intrinsic module name used previously", module_name);
7429 iomode = IO_INPUT;
7430 module_line = 1;
7431 module_column = 1;
7432 start = 0;
7434 read_module_to_tmpbuf ();
7435 gzclose (module_fp);
7437 /* Skip the first line of the module, after checking that this is
7438 a gfortran module file. */
7439 line = 0;
7440 while (line < 1)
7442 c = module_char ();
7443 if (c == EOF)
7444 bad_module ("Unexpected end of module");
7445 if (start++ < 3)
7446 parse_name (c);
7447 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
7448 || (start == 2 && strcmp (atom_name, " module") != 0))
7449 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
7450 " module file", module_fullpath);
7451 if (start == 3)
7453 if (strcmp (atom_name, " version") != 0
7454 || module_char () != ' '
7455 || parse_atom () != ATOM_STRING
7456 || strcmp (atom_string, MOD_VERSION))
7457 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
7458 " because it was created by a different"
7459 " version of GNU Fortran", module_fullpath);
7461 free (atom_string);
7464 if (c == '\n')
7465 line++;
7468 /* Make sure we're not reading the same module that we may be building. */
7469 for (p = gfc_state_stack; p; p = p->previous)
7470 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
7471 && strcmp (p->sym->name, module_name) == 0)
7473 if (p->state == COMP_SUBMODULE)
7474 gfc_fatal_error ("Cannot USE a submodule that is currently built");
7475 else
7476 gfc_fatal_error ("Cannot USE a module that is currently built");
7479 init_pi_tree ();
7480 init_true_name_tree ();
7482 read_module ();
7484 free_true_name (true_name_root);
7485 true_name_root = NULL;
7487 free_pi_tree (pi_root);
7488 pi_root = NULL;
7490 XDELETEVEC (module_content);
7491 module_content = NULL;
7493 use_stmt = gfc_get_use_list ();
7494 *use_stmt = *module;
7495 use_stmt->next = gfc_current_ns->use_stmts;
7496 gfc_current_ns->use_stmts = use_stmt;
7498 gfc_current_locus = old_locus;
7502 /* Remove duplicated intrinsic operators from the rename list. */
7504 static void
7505 rename_list_remove_duplicate (gfc_use_rename *list)
7507 gfc_use_rename *seek, *last;
7509 for (; list; list = list->next)
7510 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7512 last = list;
7513 for (seek = list->next; seek; seek = last->next)
7515 if (list->op == seek->op)
7517 last->next = seek->next;
7518 free (seek);
7520 else
7521 last = seek;
7527 /* Process all USE directives. */
7529 void
7530 gfc_use_modules (void)
7532 gfc_use_list *next, *seek, *last;
7534 for (next = module_list; next; next = next->next)
7536 bool non_intrinsic = next->non_intrinsic;
7537 bool intrinsic = next->intrinsic;
7538 bool neither = !non_intrinsic && !intrinsic;
7540 for (seek = next->next; seek; seek = seek->next)
7542 if (next->module_name != seek->module_name)
7543 continue;
7545 if (seek->non_intrinsic)
7546 non_intrinsic = true;
7547 else if (seek->intrinsic)
7548 intrinsic = true;
7549 else
7550 neither = true;
7553 if (intrinsic && neither && !non_intrinsic)
7555 char *filename;
7556 FILE *fp;
7558 filename = XALLOCAVEC (char,
7559 strlen (next->module_name)
7560 + strlen (MODULE_EXTENSION) + 1);
7561 strcpy (filename, next->module_name);
7562 strcat (filename, MODULE_EXTENSION);
7563 fp = gfc_open_included_file (filename, true, true);
7564 if (fp != NULL)
7566 non_intrinsic = true;
7567 fclose (fp);
7571 last = next;
7572 for (seek = next->next; seek; seek = last->next)
7574 if (next->module_name != seek->module_name)
7576 last = seek;
7577 continue;
7580 if ((!next->intrinsic && !seek->intrinsic)
7581 || (next->intrinsic && seek->intrinsic)
7582 || !non_intrinsic)
7584 if (!seek->only_flag)
7585 next->only_flag = false;
7586 if (seek->rename)
7588 gfc_use_rename *r = seek->rename;
7589 while (r->next)
7590 r = r->next;
7591 r->next = next->rename;
7592 next->rename = seek->rename;
7594 last->next = seek->next;
7595 free (seek);
7597 else
7598 last = seek;
7602 for (; module_list; module_list = next)
7604 next = module_list->next;
7605 rename_list_remove_duplicate (module_list->rename);
7606 gfc_use_module (module_list);
7607 free (module_list);
7609 module_list = NULL;
7610 old_module_list_tail = &module_list;
7611 gfc_rename_list = NULL;
7615 void
7616 gfc_free_use_stmts (gfc_use_list *use_stmts)
7618 gfc_use_list *next;
7619 for (; use_stmts; use_stmts = next)
7621 gfc_use_rename *next_rename;
7623 for (; use_stmts->rename; use_stmts->rename = next_rename)
7625 next_rename = use_stmts->rename->next;
7626 free (use_stmts->rename);
7628 next = use_stmts->next;
7629 free (use_stmts);
7634 /* Remember the end of the MODULE_LIST list, so that the list can be restored
7635 to its previous state if the current statement is erroneous. */
7637 void
7638 gfc_save_module_list ()
7640 gfc_use_list **tail = &module_list;
7641 while (*tail != NULL)
7642 tail = &(*tail)->next;
7643 old_module_list_tail = tail;
7647 /* Restore the MODULE_LIST list to its previous value and free the use
7648 statements that are no longer part of the list. */
7650 void
7651 gfc_restore_old_module_list ()
7653 gfc_free_use_stmts (*old_module_list_tail);
7654 *old_module_list_tail = NULL;
7658 void
7659 gfc_module_init_2 (void)
7661 last_atom = ATOM_LPAREN;
7662 gfc_rename_list = NULL;
7663 module_list = NULL;
7667 void
7668 gfc_module_done_2 (void)
7670 free_rename (gfc_rename_list);
7671 gfc_rename_list = NULL;