Debian Codegraph @

gcc/fortran/module.cc

raw
1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2025 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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.
29
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:
34
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
37 ...
38 )
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
40 ...
41 )
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
43 ...
44 )
45 ( ( <common name> <symbol> <saved flag>)
46 ...
47 )
48
49 ( equivalence list )
50
51 ( <Symbol Number (in no particular order)>
52 <True name of symbol>
53 <Module name of symbol>
54 ( <symbol information> )
55 ...
56 )
57 ( <Symtree name>
58 <Ambiguous flag>
59 <Symbol number>
60 ...
61 )
62
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. */
66
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>
81
82 #define MODULE_EXTENSION ".mod"
83 #define SUBMODULE_EXTENSION ".smod"
84
85 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
86 recognized. */
87 #define MOD_VERSION "16"
88 /* Older mod versions we can still parse. */
89 #define COMPAT_MOD_VERSIONS { "15" }
90
91
92 /* Structure that describes a position within a module file. */
93
94 typedef struct
95 {
96 int column, line;
97 long pos;
98 }
99 module_locus;
100
101 /* Structure for list of symbols of intrinsic modules. */
102 typedef struct
103 {
104 int id;
105 const char *name;
106 int value;
107 int standard;
108 }
109 intmod_sym;
110
111
112 typedef enum
113 {
114 P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
115 }
116 pointer_t;
117
118 /* The fixup structure lists pointers to pointers that have to
119 be updated when a pointer value becomes known. */
120
121 typedef struct fixup_t
122 {
123 void **pointer;
124 struct fixup_t *next;
125 }
126 fixup_t;
127
128
129 /* Structure for holding extra info needed for pointers being read. */
130
131 enum gfc_rsym_state
132 {
133 UNUSED,
134 NEEDED,
135 USED
136 };
137
138 enum gfc_wsym_state
139 {
140 UNREFERENCED = 0,
141 NEEDS_WRITE,
142 WRITTEN
143 };
144
145 typedef struct pointer_info
146 {
147 BBT_HEADER (pointer_info);
148 HOST_WIDE_INT integer;
149 pointer_t type;
150
151 /* The first component of each member of the union is the pointer
152 being stored. */
153
154 fixup_t *fixup;
155
156 union
157 {
158 void *pointer; /* Member for doing pointer searches. */
159
160 struct
161 {
162 gfc_symbol *sym;
163 char *true_name, *module, *binding_label;
164 fixup_t *stfixup;
165 gfc_symtree *symtree;
166 enum gfc_rsym_state state;
167 int ns, referenced, renamed;
168 module_locus where;
169 }
170 rsym;
171
172 struct
173 {
174 gfc_symbol *sym;
175 enum gfc_wsym_state state;
176 }
177 wsym;
178 }
179 u;
180
181 }
182 pointer_info;
183
184 #define gfc_get_pointer_info() XCNEW (pointer_info)
185
186
187 /* Local variables */
188
189 /* The gzFile for the module we're reading or writing. */
190 static gzFile module_fp;
191
192 /* Fully qualified module path */
193 static char *module_fullpath = NULL;
194
195 /* The name of the module we're reading (USE'ing) or writing. */
196 static const char *module_name;
197 /* The name of the .smod file that the submodule will write to. */
198 static const char *submodule_name;
199
200 /* The list of use statements to apply to the current namespace
201 before parsing the non-use statements. */
202 static gfc_use_list *module_list;
203 /* The end of the MODULE_LIST list above at the time the recognition
204 of the current statement started. */
205 static gfc_use_list **old_module_list_tail;
206
207 /* If we're reading an intrinsic module, this is its ID. */
208 static intmod_id current_intmod;
209
210 /* Content of module. */
211 static char* module_content;
212
213 static long module_pos;
214 static int module_line, module_column, only_flag;
215 static int prev_module_line, prev_module_column;
216
217 static enum
218 { IO_INPUT, IO_OUTPUT }
219 iomode;
220
221 static gfc_use_rename *gfc_rename_list;
222 static pointer_info *pi_root;
223 static int symbol_number; /* Counter for assigning symbol numbers */
224
225 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
226 static bool in_load_equiv;
227
228
229
230 /*****************************************************************/
231
232 /* Pointer/integer conversion. Pointers between structures are stored
233 as integers in the module file. The next couple of subroutines
234 handle this translation for reading and writing. */
235
236 /* Recursively free the tree of pointer structures. */
237
238 static void
239 free_pi_tree (pointer_info *p)
240 {
241 if (p == NULL)
242 return;
243
244 if (p->fixup != NULL)
245 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
246
247 free_pi_tree (p->left);
248 free_pi_tree (p->right);
249
250 if (iomode == IO_INPUT)
251 {
252 XDELETEVEC (p->u.rsym.true_name);
253 XDELETEVEC (p->u.rsym.module);
254 XDELETEVEC (p->u.rsym.binding_label);
255 }
256
257 free (p);
258 }
259
260
261 /* Compare pointers when searching by pointer. Used when writing a
262 module. */
263
264 static int
265 compare_pointers (void *_sn1, void *_sn2)
266 {
267 pointer_info *sn1, *sn2;
268
269 sn1 = (pointer_info *) _sn1;
270 sn2 = (pointer_info *) _sn2;
271
272 if (sn1->u.pointer < sn2->u.pointer)
273 return -1;
274 if (sn1->u.pointer > sn2->u.pointer)
275 return 1;
276
277 return 0;
278 }
279
280
281 /* Compare integers when searching by integer. Used when reading a
282 module. */
283
284 static int
285 compare_integers (void *_sn1, void *_sn2)
286 {
287 pointer_info *sn1, *sn2;
288
289 sn1 = (pointer_info *) _sn1;
290 sn2 = (pointer_info *) _sn2;
291
292 if (sn1->integer < sn2->integer)
293 return -1;
294 if (sn1->integer > sn2->integer)
295 return 1;
296
297 return 0;
298 }
299
300
301 /* Initialize the pointer_info tree. */
302
303 static void
304 init_pi_tree (void)
305 {
306 compare_fn compare;
307 pointer_info *p;
308
309 pi_root = NULL;
310 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
311
312 /* Pointer 0 is the NULL pointer. */
313 p = gfc_get_pointer_info ();
314 p->u.pointer = NULL;
315 p->integer = 0;
316 p->type = P_OTHER;
317
318 gfc_insert_bbt (&pi_root, p, compare);
319
320 /* Pointer 1 is the current namespace. */
321 p = gfc_get_pointer_info ();
322 p->u.pointer = gfc_current_ns;
323 p->integer = 1;
324 p->type = P_NAMESPACE;
325
326 gfc_insert_bbt (&pi_root, p, compare);
327
328 symbol_number = 2;
329 }
330
331
332 /* During module writing, call here with a pointer to something,
333 returning the pointer_info node. */
334
335 static pointer_info *
336 find_pointer (void *gp)
337 {
338 pointer_info *p;
339
340 p = pi_root;
341 while (p != NULL)
342 {
343 if (p->u.pointer == gp)
344 break;
345 p = (gp < p->u.pointer) ? p->left : p->right;
346 }
347
348 return p;
349 }
350
351
352 /* Given a pointer while writing, returns the pointer_info tree node,
353 creating it if it doesn't exist. */
354
355 static pointer_info *
356 get_pointer (void *gp)
357 {
358 pointer_info *p;
359
360 p = find_pointer (gp);
361 if (p != NULL)
362 return p;
363
364 /* Pointer doesn't have an integer. Give it one. */
365 p = gfc_get_pointer_info ();
366
367 p->u.pointer = gp;
368 p->integer = symbol_number++;
369
370 gfc_insert_bbt (&pi_root, p, compare_pointers);
371
372 return p;
373 }
374
375
376 /* Given an integer during reading, find it in the pointer_info tree,
377 creating the node if not found. */
378
379 static pointer_info *
380 get_integer (HOST_WIDE_INT integer)
381 {
382 pointer_info *p, t;
383 int c;
384
385 t.integer = integer;
386
387 p = pi_root;
388 while (p != NULL)
389 {
390 c = compare_integers (&t, p);
391 if (c == 0)
392 break;
393
394 p = (c < 0) ? p->left : p->right;
395 }
396
397 if (p != NULL)
398 return p;
399
400 p = gfc_get_pointer_info ();
401 p->integer = integer;
402 p->u.pointer = NULL;
403
404 gfc_insert_bbt (&pi_root, p, compare_integers);
405
406 return p;
407 }
408
409
410 /* Resolve any fixups using a known pointer. */
411
412 static void
413 resolve_fixups (fixup_t *f, void *gp)
414 {
415 fixup_t *next;
416
417 for (; f; f = next)
418 {
419 next = f->next;
420 *(f->pointer) = gp;
421 free (f);
422 }
423 }
424
425
426 /* Convert a string such that it starts with a lower-case character. Used
427 to convert the symtree name of a derived-type to the symbol name or to
428 the name of the associated generic function. */
429
430 const char *
431 gfc_dt_lower_string (const char *name)
432 {
433 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
434 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
435 &name[1]);
436 return gfc_get_string ("%s", name);
437 }
438
439
440 /* Convert a string such that it starts with an upper-case character. Used to
441 return the symtree-name for a derived type; the symbol name itself and the
442 symtree/symbol name of the associated generic function start with a lower-
443 case character. */
444
445 const char *
446 gfc_dt_upper_string (const char *name)
447 {
448 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
449 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
450 &name[1]);
451 return gfc_get_string ("%s", name);
452 }
453
454 /* Call here during module reading when we know what pointer to
455 associate with an integer. Any fixups that exist are resolved at
456 this time. */
457
458 static void
459 associate_integer_pointer (pointer_info *p, void *gp)
460 {
461 if (p->u.pointer != NULL)
462 gfc_internal_error ("associate_integer_pointer(): Already associated");
463
464 p->u.pointer = gp;
465
466 resolve_fixups (p->fixup, gp);
467
468 p->fixup = NULL;
469 }
470
471
472 /* During module reading, given an integer and a pointer to a pointer,
473 either store the pointer from an already-known value or create a
474 fixup structure in order to store things later. Returns zero if
475 the reference has been actually stored, or nonzero if the reference
476 must be fixed later (i.e., associate_integer_pointer must be called
477 sometime later. Returns the pointer_info structure. */
478
479 static pointer_info *
480 add_fixup (HOST_WIDE_INT integer, void *gp)
481 {
482 pointer_info *p;
483 fixup_t *f;
484 char **cp;
485
486 p = get_integer (integer);
487
488 if (p->integer == 0 || p->u.pointer != NULL)
489 {
490 cp = (char **) gp;
491 *cp = (char *) p->u.pointer;
492 }
493 else
494 {
495 f = XCNEW (fixup_t);
496
497 f->next = p->fixup;
498 p->fixup = f;
499
500 f->pointer = (void **) gp;
501 }
502
503 return p;
504 }
505
506
507 /*****************************************************************/
508
509 /* Parser related subroutines */
510
511 /* Free the rename list left behind by a USE statement. */
512
513 static void
514 free_rename (gfc_use_rename *list)
515 {
516 gfc_use_rename *next;
517
518 for (; list; list = next)
519 {
520 next = list->next;
521 free (list);
522 }
523 }
524
525
526 /* Match a USE statement. */
527
528 match
529 gfc_match_use (void)
530 {
531 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
532 gfc_use_rename *tail = NULL, *new_use;
533 interface_type type, type2;
534 gfc_intrinsic_op op;
535 match m;
536 gfc_use_list *use_list;
537 gfc_symtree *st;
538 locus loc;
539
540 use_list = gfc_get_use_list ();
541
542 if (gfc_match (" , ") == MATCH_YES)
543 {
544 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
545 {
546 if (!gfc_notify_std (GFC_STD_F2003, "module "
547 "nature in USE statement at %C"))
548 goto cleanup;
549
550 if (strcmp (module_nature, "intrinsic") == 0)
551 use_list->intrinsic = true;
552 else
553 {
554 if (strcmp (module_nature, "non_intrinsic") == 0)
555 use_list->non_intrinsic = true;
556 else
557 {
558 gfc_error ("Module nature in USE statement at %C shall "
559 "be either INTRINSIC or NON_INTRINSIC");
560 goto cleanup;
561 }
562 }
563 }
564 else
565 {
566 /* Help output a better error message than "Unclassifiable
567 statement". */
568 gfc_match (" %n", module_nature);
569 if (strcmp (module_nature, "intrinsic") == 0
570 || strcmp (module_nature, "non_intrinsic") == 0)
571 gfc_error ("\"::\" was expected after module nature at %C "
572 "but was not found");
573 free (use_list);
574 return m;
575 }
576 }
577 else
578 {
579 m = gfc_match (" ::");
580 if (m == MATCH_YES &&
581 !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
582 goto cleanup;
583
584 if (m != MATCH_YES)
585 {
586 m = gfc_match ("% ");
587 if (m != MATCH_YES)
588 {
589 free (use_list);
590 return m;
591 }
592 }
593 }
594
595 use_list->where = gfc_current_locus;
596
597 m = gfc_match_name (name);
598 if (m != MATCH_YES)
599 {
600 free (use_list);
601 return m;
602 }
603
604 use_list->module_name = gfc_get_string ("%s", name);
605
606 if (gfc_match_eos () == MATCH_YES)
607 goto done;
608
609 if (gfc_match_char (',') != MATCH_YES)
610 goto syntax;
611
612 if (gfc_match (" only :") == MATCH_YES)
613 use_list->only_flag = true;
614
615 if (gfc_match_eos () == MATCH_YES)
616 goto done;
617
618 for (;;)
619 {
620 /* Get a new rename struct and add it to the rename list. */
621 new_use = gfc_get_use_rename ();
622 new_use->where = gfc_current_locus;
623 new_use->found = 0;
624
625 if (use_list->rename == NULL)
626 use_list->rename = new_use;
627 else
628 tail->next = new_use;
629 tail = new_use;
630
631 /* See what kind of interface we're dealing with. Assume it is
632 not an operator. */
633 new_use->op = INTRINSIC_NONE;
634 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
635 goto cleanup;
636
637 switch (type)
638 {
639 case INTERFACE_NAMELESS:
640 gfc_error ("Missing generic specification in USE statement at %C");
641 goto cleanup;
642
643 case INTERFACE_USER_OP:
644 case INTERFACE_GENERIC:
645 case INTERFACE_DTIO:
646 loc = gfc_current_locus;
647
648 m = gfc_match (" =>");
649
650 if (type == INTERFACE_USER_OP && m == MATCH_YES
651 && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
652 "operators in USE statements at %C")))
653 goto cleanup;
654
655 if (type == INTERFACE_USER_OP)
656 new_use->op = INTRINSIC_USER;
657
658 if (use_list->only_flag)
659 {
660 if (m != MATCH_YES)
661 strcpy (new_use->use_name, name);
662 else
663 {
664 strcpy (new_use->local_name, name);
665 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
666 if (type != type2)
667 goto syntax;
668 if (m == MATCH_NO)
669 goto syntax;
670 if (m == MATCH_ERROR)
671 goto cleanup;
672 }
673 }
674 else
675 {
676 if (m != MATCH_YES)
677 goto syntax;
678 strcpy (new_use->local_name, name);
679
680 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
681 if (type != type2)
682 goto syntax;
683 if (m == MATCH_NO)
684 goto syntax;
685 if (m == MATCH_ERROR)
686 goto cleanup;
687 }
688
689 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
690 if (st && type != INTERFACE_USER_OP
691 && (st->n.sym->module != use_list->module_name
692 || strcmp (st->n.sym->name, new_use->use_name) != 0))
693 {
694 if (m == MATCH_YES)
695 gfc_error ("Symbol %qs at %L conflicts with the rename symbol "
696 "at %L", name, &st->n.sym->declared_at, &loc);
697 else
698 gfc_error ("Symbol %qs at %L conflicts with the symbol "
699 "at %L", name, &st->n.sym->declared_at, &loc);
700 goto cleanup;
701 }
702
703 if (strcmp (new_use->use_name, use_list->module_name) == 0
704 || strcmp (new_use->local_name, use_list->module_name) == 0)
705 {
706 gfc_error ("The name %qs at %C has already been used as "
707 "an external module name", use_list->module_name);
708 goto cleanup;
709 }
710 break;
711
712 case INTERFACE_INTRINSIC_OP:
713 new_use->op = op;
714 break;
715
716 default:
717 gcc_unreachable ();
718 }
719
720 if (gfc_match_eos () == MATCH_YES)
721 break;
722 if (gfc_match_char (',') != MATCH_YES)
723 goto syntax;
724 }
725
726 done:
727 if (module_list)
728 {
729 gfc_use_list *last = module_list;
730 while (last->next)
731 last = last->next;
732 last->next = use_list;
733 }
734 else
735 module_list = use_list;
736
737 return MATCH_YES;
738
739 syntax:
740 gfc_syntax_error (ST_USE);
741
742 cleanup:
743 free_rename (use_list->rename);
744 free (use_list);
745 return MATCH_ERROR;
746 }
747
748
749 /* Match a SUBMODULE statement.
750
751 According to F2008:11.2.3.2, "The submodule identifier is the
752 ordered pair whose first element is the ancestor module name and
753 whose second element is the submodule name. 'Submodule_name' is
754 used for the submodule filename and uses '@' as a separator, whilst
755 the name of the symbol for the module uses '.' as a separator.
756 The reasons for these choices are:
757 (i) To follow another leading brand in the submodule filenames;
758 (ii) Since '.' is not particularly visible in the filenames; and
759 (iii) The linker does not permit '@' in mnemonics. */
760
761 match
762 gfc_match_submodule (void)
763 {
764 match m;
765 char name[GFC_MAX_SYMBOL_LEN + 1];
766 gfc_use_list *use_list;
767 bool seen_colon = false;
768
769 if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
770 return MATCH_ERROR;
771
772 if (gfc_current_state () != COMP_NONE)
773 {
774 gfc_error ("SUBMODULE declaration at %C cannot appear within "
775 "another scoping unit");
776 return MATCH_ERROR;
777 }
778
779 gfc_new_block = NULL;
780 gcc_assert (module_list == NULL);
781
782 if (gfc_match_char ('(') != MATCH_YES)
783 goto syntax;
784
785 while (1)
786 {
787 m = gfc_match (" %n", name);
788 if (m != MATCH_YES)
789 goto syntax;
790
791 use_list = gfc_get_use_list ();
792 use_list->where = gfc_current_locus;
793
794 if (module_list)
795 {
796 gfc_use_list *last = module_list;
797 while (last->next)
798 last = last->next;
799 last->next = use_list;
800 use_list->module_name
801 = gfc_get_string ("%s.%s", module_list->module_name, name);
802 use_list->submodule_name
803 = gfc_get_string ("%s@%s", module_list->module_name, name);
804 }
805 else
806 {
807 module_list = use_list;
808 use_list->module_name = gfc_get_string ("%s", name);
809 use_list->submodule_name = use_list->module_name;
810 }
811
812 if (gfc_match_char (')') == MATCH_YES)
813 break;
814
815 if (gfc_match_char (':') != MATCH_YES
816 || seen_colon)
817 goto syntax;
818
819 seen_colon = true;
820 }
821
822 m = gfc_match (" %s%t", &gfc_new_block);
823 if (m != MATCH_YES)
824 goto syntax;
825
826 submodule_name = gfc_get_string ("%s@%s", module_list->module_name,
827 gfc_new_block->name);
828
829 gfc_new_block->name = gfc_get_string ("%s.%s",
830 module_list->module_name,
831 gfc_new_block->name);
832
833 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
834 gfc_new_block->name, NULL))
835 return MATCH_ERROR;
836
837 /* Just retain the ultimate .(s)mod file for reading, since it
838 contains all the information in its ancestors. */
839 use_list = module_list;
840 for (; module_list->next; use_list = module_list)
841 {
842 module_list = use_list->next;
843 free (use_list);
844 }
845
846 return MATCH_YES;
847
848 syntax:
849 gfc_error ("Syntax error in SUBMODULE statement at %C");
850 return MATCH_ERROR;
851 }
852
853
854 /* Given a name and a number, inst, return the inst name
855 under which to load this symbol. Returns NULL if this
856 symbol shouldn't be loaded. If inst is zero, returns
857 the number of instances of this name. If interface is
858 true, a user-defined operator is sought, otherwise only
859 non-operators are sought. */
860
861 static const char *
862 find_use_name_n (const char *name, int *inst, bool interface)
863 {
864 gfc_use_rename *u;
865 const char *low_name = NULL;
866 int i;
867
868 /* For derived types. */
869 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
870 low_name = gfc_dt_lower_string (name);
871
872 i = 0;
873 for (u = gfc_rename_list; u; u = u->next)
874 {
875 if ((!low_name && strcmp (u->use_name, name) != 0)
876 || (low_name && strcmp (u->use_name, low_name) != 0)
877 || (u->op == INTRINSIC_USER && !interface)
878 || (u->op != INTRINSIC_USER && interface))
879 continue;
880 if (++i == *inst)
881 break;
882 }
883
884 if (!*inst)
885 {
886 *inst = i;
887 return NULL;
888 }
889
890 if (u == NULL)
891 return only_flag ? NULL : name;
892
893 u->found = 1;
894
895 if (low_name)
896 {
897 if (u->local_name[0] == '\0')
898 return name;
899 return gfc_dt_upper_string (u->local_name);
900 }
901
902 return (u->local_name[0] != '\0') ? u->local_name : name;
903 }
904
905
906 /* Given a name, return the name under which to load this symbol.
907 Returns NULL if this symbol shouldn't be loaded. */
908
909 static const char *
910 find_use_name (const char *name, bool interface)
911 {
912 int i = 1;
913 return find_use_name_n (name, &i, interface);
914 }
915
916
917 /* Given a real name, return the number of use names associated with it. */
918
919 static int
920 number_use_names (const char *name, bool interface)
921 {
922 int i = 0;
923 find_use_name_n (name, &i, interface);
924 return i;
925 }
926
927
928 /* Try to find the operator in the current list. */
929
930 static gfc_use_rename *
931 find_use_operator (gfc_intrinsic_op op)
932 {
933 gfc_use_rename *u;
934
935 for (u = gfc_rename_list; u; u = u->next)
936 if (u->op == op)
937 return u;
938
939 return NULL;
940 }
941
942
943 /*****************************************************************/
944
945 /* The next couple of subroutines maintain a tree used to avoid a
946 brute-force search for a combination of true name and module name.
947 While symtree names, the name that a particular symbol is known by
948 can changed with USE statements, we still have to keep track of the
949 true names to generate the correct reference, and also avoid
950 loading the same real symbol twice in a program unit.
951
952 When we start reading, the true name tree is built and maintained
953 as symbols are read. The tree is searched as we load new symbols
954 to see if it already exists someplace in the namespace. */
955
956 typedef struct true_name
957 {
958 BBT_HEADER (true_name);
959 const char *name;
960 gfc_symbol *sym;
961 }
962 true_name;
963
964 static true_name *true_name_root;
965
966
967 /* Compare two true_name structures. */
968
969 static int
970 compare_true_names (void *_t1, void *_t2)
971 {
972 true_name *t1, *t2;
973 int c;
974
975 t1 = (true_name *) _t1;
976 t2 = (true_name *) _t2;
977
978 c = ((t1->sym->module > t2->sym->module)
979 - (t1->sym->module < t2->sym->module));
980 if (c != 0)
981 return c;
982
983 return strcmp (t1->name, t2->name);
984 }
985
986
987 /* Given a true name, search the true name tree to see if it exists
988 within the main namespace. */
989
990 static gfc_symbol *
991 find_true_name (const char *name, const char *module)
992 {
993 true_name t, *p;
994 gfc_symbol sym;
995 int c;
996
997 t.name = gfc_get_string ("%s", name);
998 if (module != NULL)
999 sym.module = gfc_get_string ("%s", module);
1000 else
1001 sym.module = NULL;
1002 t.sym = &sym;
1003
1004 p = true_name_root;
1005 while (p != NULL)
1006 {
1007 c = compare_true_names ((void *) (&t), (void *) p);
1008 if (c == 0)
1009 return p->sym;
1010
1011 p = (c < 0) ? p->left : p->right;
1012 }
1013
1014 return NULL;
1015 }
1016
1017
1018 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
1019
1020 static void
1021 add_true_name (gfc_symbol *sym)
1022 {
1023 true_name *t;
1024
1025 t = XCNEW (true_name);
1026 t->sym = sym;
1027 if (gfc_fl_struct (sym->attr.flavor))
1028 t->name = gfc_dt_upper_string (sym->name);
1029 else
1030 t->name = sym->name;
1031
1032 gfc_insert_bbt (&true_name_root, t, compare_true_names);
1033 }
1034
1035
1036 /* Recursive function to build the initial true name tree by
1037 recursively traversing the current namespace. */
1038
1039 static void
1040 build_tnt (gfc_symtree *st)
1041 {
1042 const char *name;
1043 if (st == NULL)
1044 return;
1045
1046 build_tnt (st->left);
1047 build_tnt (st->right);
1048
1049 if (gfc_fl_struct (st->n.sym->attr.flavor))
1050 name = gfc_dt_upper_string (st->n.sym->name);
1051 else
1052 name = st->n.sym->name;
1053
1054 if (find_true_name (name, st->n.sym->module) != NULL)
1055 return;
1056
1057 add_true_name (st->n.sym);
1058 }
1059
1060
1061 /* Initialize the true name tree with the current namespace. */
1062
1063 static void
1064 init_true_name_tree (void)
1065 {
1066 true_name_root = NULL;
1067 build_tnt (gfc_current_ns->sym_root);
1068 }
1069
1070
1071 /* Recursively free a true name tree node. */
1072
1073 static void
1074 free_true_name (true_name *t)
1075 {
1076 if (t == NULL)
1077 return;
1078 free_true_name (t->left);
1079 free_true_name (t->right);
1080
1081 free (t);
1082 }
1083
1084
1085 /*****************************************************************/
1086
1087 /* Module reading and writing. */
1088
1089 /* The following are versions similar to the ones in scanner.cc, but
1090 for dealing with compressed module files. */
1091
1092 static gzFile
1093 gzopen_included_file_1 (const char *name, gfc_directorylist *list,
1094 bool module, bool system)
1095 {
1096 char *fullname;
1097 gfc_directorylist *p;
1098 gzFile f;
1099
1100 for (p = list; p; p = p->next)
1101 {
1102 if (module && !p->use_for_modules)
1103 continue;
1104
1105 fullname = (char *) alloca(strlen (p->path) + strlen (name) + 2);
1106 strcpy (fullname, p->path);
1107 strcat (fullname, "/");
1108 strcat (fullname, name);
1109
1110 f = gzopen (fullname, "r");
1111 if (f != NULL)
1112 {
1113 if (gfc_cpp_makedep ())
1114 gfc_cpp_add_dep (fullname, system);
1115
1116 free (module_fullpath);
1117 module_fullpath = xstrdup (fullname);
1118 return f;
1119 }
1120 }
1121
1122 return NULL;
1123 }
1124
1125 static gzFile
1126 gzopen_included_file (const char *name, bool include_cwd, bool module)
1127 {
1128 gzFile f = NULL;
1129
1130 if (IS_ABSOLUTE_PATH (name) || include_cwd)
1131 {
1132 f = gzopen (name, "r");
1133 if (f)
1134 {
1135 if (gfc_cpp_makedep ())
1136 gfc_cpp_add_dep (name, false);
1137
1138 free (module_fullpath);
1139 module_fullpath = xstrdup (name);
1140 }
1141 }
1142
1143 if (!f)
1144 f = gzopen_included_file_1 (name, include_dirs, module, false);
1145
1146 return f;
1147 }
1148
1149 static gzFile
1150 gzopen_intrinsic_module (const char* name)
1151 {
1152 gzFile f = NULL;
1153
1154 if (IS_ABSOLUTE_PATH (name))
1155 {
1156 f = gzopen (name, "r");
1157 if (f)
1158 {
1159 if (gfc_cpp_makedep ())
1160 gfc_cpp_add_dep (name, true);
1161
1162 free (module_fullpath);
1163 module_fullpath = xstrdup (name);
1164 }
1165 }
1166
1167 if (!f)
1168 f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
1169
1170 return f;
1171 }
1172
1173
1174 enum atom_type
1175 {
1176 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
1177 };
1178
1179 static atom_type last_atom;
1180
1181
1182 /* The name buffer must be at least as long as a symbol name. Right
1183 now it's not clear how we're going to store numeric constants--
1184 probably as a hexadecimal string, since this will allow the exact
1185 number to be preserved (this can't be done by a decimal
1186 representation). Worry about that later. TODO! */
1187
1188 #define MAX_ATOM_SIZE 100
1189
1190 static HOST_WIDE_INT atom_int;
1191 static char *atom_string, atom_name[MAX_ATOM_SIZE];
1192
1193
1194 /* Report problems with a module. Error reporting is not very
1195 elaborate, since this sorts of errors shouldn't really happen.
1196 This subroutine never returns. */
1197
1198 static void bad_module (const char *) ATTRIBUTE_NORETURN;
1199
1200 static void
1201 bad_module (const char *msgid)
1202 {
1203 XDELETEVEC (module_content);
1204 module_content = NULL;
1205
1206 switch (iomode)
1207 {
1208 case IO_INPUT:
1209 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1210 module_fullpath, module_line, module_column, msgid);
1211 break;
1212 case IO_OUTPUT:
1213 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1214 module_name, module_line, module_column, msgid);
1215 break;
1216 default:
1217 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1218 module_name, module_line, module_column, msgid);
1219 break;
1220 }
1221 }
1222
1223
1224 /* Set the module's input pointer. */
1225
1226 static void
1227 set_module_locus (module_locus *m)
1228 {
1229 module_column = m->column;
1230 module_line = m->line;
1231 module_pos = m->pos;
1232 }
1233
1234
1235 /* Get the module's input pointer so that we can restore it later. */
1236
1237 static void
1238 get_module_locus (module_locus *m)
1239 {
1240 m->column = module_column;
1241 m->line = module_line;
1242 m->pos = module_pos;
1243 }
1244
1245 /* Peek at the next character in the module. */
1246
1247 static int
1248 module_peek_char (void)
1249 {
1250 return module_content[module_pos];
1251 }
1252
1253 /* Get the next character in the module, updating our reckoning of
1254 where we are. */
1255
1256 static int
1257 module_char (void)
1258 {
1259 const char c = module_content[module_pos++];
1260 if (c == '\0')
1261 bad_module ("Unexpected EOF");
1262
1263 prev_module_line = module_line;
1264 prev_module_column = module_column;
1265
1266 if (c == '\n')
1267 {
1268 module_line++;
1269 module_column = 0;
1270 }
1271
1272 module_column++;
1273 return c;
1274 }
1275
1276 /* Unget a character while remembering the line and column. Works for
1277 a single character only. */
1278
1279 static void
1280 module_unget_char (void)
1281 {
1282 module_line = prev_module_line;
1283 module_column = prev_module_column;
1284 module_pos--;
1285 }
1286
1287 /* Parse a string constant. The delimiter is guaranteed to be a
1288 single quote. */
1289
1290 static void
1291 parse_string (void)
1292 {
1293 int c;
1294 size_t cursz = 30;
1295 size_t len = 0;
1296
1297 atom_string = XNEWVEC (char, cursz);
1298
1299 for ( ; ; )
1300 {
1301 c = module_char ();
1302
1303 if (c == '\'')
1304 {
1305 int c2 = module_char ();
1306 if (c2 != '\'')
1307 {
1308 module_unget_char ();
1309 break;
1310 }
1311 }
1312
1313 if (len >= cursz)
1314 {
1315 cursz *= 2;
1316 atom_string = XRESIZEVEC (char, atom_string, cursz);
1317 }
1318 atom_string[len] = c;
1319 len++;
1320 }
1321
1322 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1323 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1324 }
1325
1326
1327 /* Parse an integer. Should fit in a HOST_WIDE_INT. */
1328
1329 static void
1330 parse_integer (int c)
1331 {
1332 int sign = 1;
1333
1334 atom_int = 0;
1335 switch (c)
1336 {
1337 case ('-'):
1338 sign = -1;
1339 case ('+'):
1340 break;
1341 default:
1342 atom_int = c - '0';
1343 break;
1344 }
1345
1346 for (;;)
1347 {
1348 c = module_char ();
1349 if (!ISDIGIT (c))
1350 {
1351 module_unget_char ();
1352 break;
1353 }
1354
1355 atom_int = 10 * atom_int + c - '0';
1356 }
1357
1358 atom_int *= sign;
1359 }
1360
1361
1362 /* Parse a name. */
1363
1364 static void
1365 parse_name (int c)
1366 {
1367 char *p;
1368 int len;
1369
1370 p = atom_name;
1371
1372 *p++ = c;
1373 len = 1;
1374
1375 for (;;)
1376 {
1377 c = module_char ();
1378 if (!ISALNUM (c) && c != '_' && c != '-')
1379 {
1380 module_unget_char ();
1381 break;
1382 }
1383
1384 *p++ = c;
1385 if (++len > GFC_MAX_SYMBOL_LEN)
1386 bad_module ("Name too long");
1387 }
1388
1389 *p = '\0';
1390
1391 }
1392
1393
1394 /* Read the next atom in the module's input stream. */
1395
1396 static atom_type
1397 parse_atom (void)
1398 {
1399 int c;
1400
1401 do
1402 {
1403 c = module_char ();
1404 }
1405 while (c == ' ' || c == '\r' || c == '\n');
1406
1407 switch (c)
1408 {
1409 case '(':
1410 return ATOM_LPAREN;
1411
1412 case ')':
1413 return ATOM_RPAREN;
1414
1415 case '\'':
1416 parse_string ();
1417 return ATOM_STRING;
1418
1419 case '0':
1420 case '1':
1421 case '2':
1422 case '3':
1423 case '4':
1424 case '5':
1425 case '6':
1426 case '7':
1427 case '8':
1428 case '9':
1429 parse_integer (c);
1430 return ATOM_INTEGER;
1431
1432 case '+':
1433 case '-':
1434 if (ISDIGIT (module_peek_char ()))
1435 {
1436 parse_integer (c);
1437 return ATOM_INTEGER;
1438 }
1439 else
1440 bad_module ("Bad name");
1441
1442 case 'a':
1443 case 'b':
1444 case 'c':
1445 case 'd':
1446 case 'e':
1447 case 'f':
1448 case 'g':
1449 case 'h':
1450 case 'i':
1451 case 'j':
1452 case 'k':
1453 case 'l':
1454 case 'm':
1455 case 'n':
1456 case 'o':
1457 case 'p':
1458 case 'q':
1459 case 'r':
1460 case 's':
1461 case 't':
1462 case 'u':
1463 case 'v':
1464 case 'w':
1465 case 'x':
1466 case 'y':
1467 case 'z':
1468 case 'A':
1469 case 'B':
1470 case 'C':
1471 case 'D':
1472 case 'E':
1473 case 'F':
1474 case 'G':
1475 case 'H':
1476 case 'I':
1477 case 'J':
1478 case 'K':
1479 case 'L':
1480 case 'M':
1481 case 'N':
1482 case 'O':
1483 case 'P':
1484 case 'Q':
1485 case 'R':
1486 case 'S':
1487 case 'T':
1488 case 'U':
1489 case 'V':
1490 case 'W':
1491 case 'X':
1492 case 'Y':
1493 case 'Z':
1494 parse_name (c);
1495 return ATOM_NAME;
1496
1497 default:
1498 bad_module ("Bad name");
1499 }
1500
1501 /* Not reached. */
1502 }
1503
1504
1505 /* Peek at the next atom on the input. */
1506
1507 static atom_type
1508 peek_atom (void)
1509 {
1510 int c;
1511
1512 do
1513 {
1514 c = module_char ();
1515 }
1516 while (c == ' ' || c == '\r' || c == '\n');
1517
1518 switch (c)
1519 {
1520 case '(':
1521 module_unget_char ();
1522 return ATOM_LPAREN;
1523
1524 case ')':
1525 module_unget_char ();
1526 return ATOM_RPAREN;
1527
1528 case '\'':
1529 module_unget_char ();
1530 return ATOM_STRING;
1531
1532 case '0':
1533 case '1':
1534 case '2':
1535 case '3':
1536 case '4':
1537 case '5':
1538 case '6':
1539 case '7':
1540 case '8':
1541 case '9':
1542 module_unget_char ();
1543 return ATOM_INTEGER;
1544
1545 case '+':
1546 case '-':
1547 if (ISDIGIT (module_peek_char ()))
1548 {
1549 module_unget_char ();
1550 return ATOM_INTEGER;
1551 }
1552 else
1553 bad_module ("Bad name");
1554
1555 case 'a':
1556 case 'b':
1557 case 'c':
1558 case 'd':
1559 case 'e':
1560 case 'f':
1561 case 'g':
1562 case 'h':
1563 case 'i':
1564 case 'j':
1565 case 'k':
1566 case 'l':
1567 case 'm':
1568 case 'n':
1569 case 'o':
1570 case 'p':
1571 case 'q':
1572 case 'r':
1573 case 's':
1574 case 't':
1575 case 'u':
1576 case 'v':
1577 case 'w':
1578 case 'x':
1579 case 'y':
1580 case 'z':
1581 case 'A':
1582 case 'B':
1583 case 'C':
1584 case 'D':
1585 case 'E':
1586 case 'F':
1587 case 'G':
1588 case 'H':
1589 case 'I':
1590 case 'J':
1591 case 'K':
1592 case 'L':
1593 case 'M':
1594 case 'N':
1595 case 'O':
1596 case 'P':
1597 case 'Q':
1598 case 'R':
1599 case 'S':
1600 case 'T':
1601 case 'U':
1602 case 'V':
1603 case 'W':
1604 case 'X':
1605 case 'Y':
1606 case 'Z':
1607 module_unget_char ();
1608 return ATOM_NAME;
1609
1610 default:
1611 bad_module ("Bad name");
1612 }
1613 }
1614
1615
1616 /* Read the next atom from the input, requiring that it be a
1617 particular kind. */
1618
1619 static void
1620 require_atom (atom_type type)
1621 {
1622 atom_type t;
1623 const char *p;
1624 int column, line;
1625
1626 column = module_column;
1627 line = module_line;
1628
1629 t = parse_atom ();
1630 if (t != type)
1631 {
1632 switch (type)
1633 {
1634 case ATOM_NAME:
1635 p = _("Expected name");
1636 break;
1637 case ATOM_LPAREN:
1638 p = _("Expected left parenthesis");
1639 break;
1640 case ATOM_RPAREN:
1641 p = _("Expected right parenthesis");
1642 break;
1643 case ATOM_INTEGER:
1644 p = _("Expected integer");
1645 break;
1646 case ATOM_STRING:
1647 p = _("Expected string");
1648 break;
1649 default:
1650 gfc_internal_error ("require_atom(): bad atom type required");
1651 }
1652
1653 module_column = column;
1654 module_line = line;
1655 bad_module (p);
1656 }
1657 }
1658
1659
1660 /* Given a pointer to an mstring array, require that the current input
1661 be one of the strings in the array. We return the enum value. */
1662
1663 static int
1664 find_enum (const mstring *m)
1665 {
1666 int i;
1667
1668 i = gfc_string2code (m, atom_name);
1669 if (i >= 0)
1670 return i;
1671
1672 bad_module ("find_enum(): Enum not found");
1673
1674 /* Not reached. */
1675 }
1676
1677
1678 /* Read a string. The caller is responsible for freeing. */
1679
1680 static char*
1681 read_string (void)
1682 {
1683 char* p;
1684 require_atom (ATOM_STRING);
1685 p = atom_string;
1686 atom_string = NULL;
1687 return p;
1688 }
1689
1690
1691 /**************** Module output subroutines ***************************/
1692
1693 /* Output a character to a module file. */
1694
1695 static void
1696 write_char (char out)
1697 {
1698 if (gzputc (module_fp, out) == EOF)
1699 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1700
1701 if (out != '\n')
1702 module_column++;
1703 else
1704 {
1705 module_column = 1;
1706 module_line++;
1707 }
1708 }
1709
1710
1711 /* Write an atom to a module. The line wrapping isn't perfect, but it
1712 should work most of the time. This isn't that big of a deal, since
1713 the file really isn't meant to be read by people anyway. */
1714
1715 static void
1716 write_atom (atom_type atom, const void *v)
1717 {
1718 char buffer[32];
1719
1720 /* Workaround -Wmaybe-uninitialized false positive during
1721 profiledbootstrap by initializing them. */
1722 int len;
1723 HOST_WIDE_INT i = 0;
1724 const char *p;
1725
1726 switch (atom)
1727 {
1728 case ATOM_STRING:
1729 case ATOM_NAME:
1730 p = (const char *) v;
1731 break;
1732
1733 case ATOM_LPAREN:
1734 p = "(";
1735 break;
1736
1737 case ATOM_RPAREN:
1738 p = ")";
1739 break;
1740
1741 case ATOM_INTEGER:
1742 i = *((const HOST_WIDE_INT *) v);
1743
1744 snprintf (buffer, sizeof (buffer), HOST_WIDE_INT_PRINT_DEC, i);
1745 p = buffer;
1746 break;
1747
1748 default:
1749 gfc_internal_error ("write_atom(): Trying to write dab atom");
1750
1751 }
1752
1753 if(p == NULL || *p == '\0')
1754 len = 0;
1755 else
1756 len = strlen (p);
1757
1758 if (atom != ATOM_RPAREN)
1759 {
1760 if (module_column + len > 72)
1761 write_char ('\n');
1762 else
1763 {
1764
1765 if (last_atom != ATOM_LPAREN && module_column != 1)
1766 write_char (' ');
1767 }
1768 }
1769
1770 if (atom == ATOM_STRING)
1771 write_char ('\'');
1772
1773 while (p != NULL && *p)
1774 {
1775 if (atom == ATOM_STRING && *p == '\'')
1776 write_char ('\'');
1777 write_char (*p++);
1778 }
1779
1780 if (atom == ATOM_STRING)
1781 write_char ('\'');
1782
1783 last_atom = atom;
1784 }
1785
1786
1787
1788 /***************** Mid-level I/O subroutines *****************/
1789
1790 /* These subroutines let their caller read or write atoms without
1791 caring about which of the two is actually happening. This lets a
1792 subroutine concentrate on the actual format of the data being
1793 written. */
1794
1795 static void mio_expr (gfc_expr **);
1796 pointer_info *mio_symbol_ref (gfc_symbol **);
1797 pointer_info *mio_interface_rest (gfc_interface **);
1798 static void mio_symtree_ref (gfc_symtree **);
1799
1800 /* Read or write an enumerated value. On writing, we return the input
1801 value for the convenience of callers. We avoid using an integer
1802 pointer because enums are sometimes inside bitfields. */
1803
1804 static int
1805 mio_name (int t, const mstring *m)
1806 {
1807 if (iomode == IO_OUTPUT)
1808 write_atom (ATOM_NAME, gfc_code2string (m, t));
1809 else
1810 {
1811 require_atom (ATOM_NAME);
1812 t = find_enum (m);
1813 }
1814
1815 return t;
1816 }
1817
1818 /* Specialization of mio_name. */
1819
1820 #define DECL_MIO_NAME(TYPE) \
1821 static inline TYPE \
1822 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1823 { \
1824 return (TYPE) mio_name ((int) t, m); \
1825 }
1826 #define MIO_NAME(TYPE) mio_name_##TYPE
1827
1828 static void
1829 mio_lparen (void)
1830 {
1831 if (iomode == IO_OUTPUT)
1832 write_atom (ATOM_LPAREN, NULL);
1833 else
1834 require_atom (ATOM_LPAREN);
1835 }
1836
1837
1838 static void
1839 mio_rparen (void)
1840 {
1841 if (iomode == IO_OUTPUT)
1842 write_atom (ATOM_RPAREN, NULL);
1843 else
1844 require_atom (ATOM_RPAREN);
1845 }
1846
1847
1848 static void
1849 mio_integer (int *ip)
1850 {
1851 if (iomode == IO_OUTPUT)
1852 {
1853 HOST_WIDE_INT hwi = *ip;
1854 write_atom (ATOM_INTEGER, &hwi);
1855 }
1856 else
1857 {
1858 require_atom (ATOM_INTEGER);
1859 *ip = atom_int;
1860 }
1861 }
1862
1863 static void
1864 mio_hwi (HOST_WIDE_INT *hwi)
1865 {
1866 if (iomode == IO_OUTPUT)
1867 write_atom (ATOM_INTEGER, hwi);
1868 else
1869 {
1870 require_atom (ATOM_INTEGER);
1871 *hwi = atom_int;
1872 }
1873 }
1874
1875
1876 /* Read or write a gfc_intrinsic_op value. */
1877
1878 static void
1879 mio_intrinsic_op (gfc_intrinsic_op* op)
1880 {
1881 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1882 if (iomode == IO_OUTPUT)
1883 {
1884 HOST_WIDE_INT converted = (HOST_WIDE_INT) *op;
1885 write_atom (ATOM_INTEGER, &converted);
1886 }
1887 else
1888 {
1889 require_atom (ATOM_INTEGER);
1890 *op = (gfc_intrinsic_op) atom_int;
1891 }
1892 }
1893
1894
1895 /* Read or write a character pointer that points to a string on the heap. */
1896
1897 static const char *
1898 mio_allocated_string (const char *s)
1899 {
1900 if (iomode == IO_OUTPUT)
1901 {
1902 write_atom (ATOM_STRING, s);
1903 return s;
1904 }
1905 else
1906 {
1907 require_atom (ATOM_STRING);
1908 return atom_string;
1909 }
1910 }
1911
1912
1913 /* Functions for quoting and unquoting strings. */
1914
1915 static char *
1916 quote_string (const gfc_char_t *s, const size_t slength)
1917 {
1918 const gfc_char_t *p;
1919 char *res, *q;
1920 size_t len = 0, i;
1921
1922 /* Calculate the length we'll need: a backslash takes two ("\\"),
1923 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1924 for (p = s, i = 0; i < slength; p++, i++)
1925 {
1926 if (*p == '\\')
1927 len += 2;
1928 else if (!gfc_wide_is_printable (*p))
1929 len += 10;
1930 else
1931 len++;
1932 }
1933
1934 q = res = XCNEWVEC (char, len + 1);
1935 for (p = s, i = 0; i < slength; p++, i++)
1936 {
1937 if (*p == '\\')
1938 *q++ = '\\', *q++ = '\\';
1939 else if (!gfc_wide_is_printable (*p))
1940 {
1941 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1942 (unsigned HOST_WIDE_INT) *p);
1943 q += 10;
1944 }
1945 else
1946 *q++ = (unsigned char) *p;
1947 }
1948
1949 res[len] = '\0';
1950 return res;
1951 }
1952
1953 static gfc_char_t *
1954 unquote_string (const char *s)
1955 {
1956 size_t len, i;
1957 const char *p;
1958 gfc_char_t *res;
1959
1960 for (p = s, len = 0; *p; p++, len++)
1961 {
1962 if (*p != '\\')
1963 continue;
1964
1965 if (p[1] == '\\')
1966 p++;
1967 else if (p[1] == 'U')
1968 p += 9; /* That is a "\U????????". */
1969 else
1970 gfc_internal_error ("unquote_string(): got bad string");
1971 }
1972
1973 res = gfc_get_wide_string (len + 1);
1974 for (i = 0, p = s; i < len; i++, p++)
1975 {
1976 gcc_assert (*p);
1977
1978 if (*p != '\\')
1979 res[i] = (unsigned char) *p;
1980 else if (p[1] == '\\')
1981 {
1982 res[i] = (unsigned char) '\\';
1983 p++;
1984 }
1985 else
1986 {
1987 /* We read the 8-digits hexadecimal constant that follows. */
1988 int j;
1989 unsigned n;
1990 gfc_char_t c = 0;
1991
1992 gcc_assert (p[1] == 'U');
1993 for (j = 0; j < 8; j++)
1994 {
1995 c = c << 4;
1996 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1997 c += n;
1998 }
1999
2000 res[i] = c;
2001 p += 9;
2002 }
2003 }
2004
2005 res[len] = '\0';
2006 return res;
2007 }
2008
2009
2010 /* Read or write a character pointer that points to a wide string on the
2011 heap, performing quoting/unquoting of nonprintable characters using the
2012 form \U???????? (where each ? is a hexadecimal digit).
2013 Length is the length of the string, only known and used in output mode. */
2014
2015 static const gfc_char_t *
2016 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
2017 {
2018 if (iomode == IO_OUTPUT)
2019 {
2020 char *quoted = quote_string (s, length);
2021 write_atom (ATOM_STRING, quoted);
2022 free (quoted);
2023 return s;
2024 }
2025 else
2026 {
2027 gfc_char_t *unquoted;
2028
2029 require_atom (ATOM_STRING);
2030 unquoted = unquote_string (atom_string);
2031 free (atom_string);
2032 return unquoted;
2033 }
2034 }
2035
2036
2037 /* Read or write a string that is in static memory. */
2038
2039 static void
2040 mio_pool_string (const char **stringp)
2041 {
2042 /* TODO: one could write the string only once, and refer to it via a
2043 fixup pointer. */
2044
2045 /* As a special case we have to deal with a NULL string. This
2046 happens for the 'module' member of 'gfc_symbol's that are not in a
2047 module. We read / write these as the empty string. */
2048 if (iomode == IO_OUTPUT)
2049 {
2050 const char *p = *stringp == NULL ? "" : *stringp;
2051 write_atom (ATOM_STRING, p);
2052 }
2053 else
2054 {
2055 require_atom (ATOM_STRING);
2056 *stringp = (atom_string[0] == '\0'
2057 ? NULL : gfc_get_string ("%s", atom_string));
2058 free (atom_string);
2059 }
2060 }
2061
2062
2063 /* Read or write a string that is inside of some already-allocated
2064 structure. */
2065
2066 static void
2067 mio_internal_string (char *string)
2068 {
2069 if (iomode == IO_OUTPUT)
2070 write_atom (ATOM_STRING, string);
2071 else
2072 {
2073 require_atom (ATOM_STRING);
2074 strcpy (string, atom_string);
2075 free (atom_string);
2076 }
2077 }
2078
2079
2080 enum ab_attribute
2081 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
2082 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
2083 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
2084 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
2085 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
2086 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
2087 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP, AB_EVENT_COMP,
2088 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
2089 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
2090 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
2091 AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
2092 AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE, AB_OACC_DECLARE_CREATE,
2093 AB_OACC_DECLARE_COPYIN, AB_OACC_DECLARE_DEVICEPTR,
2094 AB_OACC_DECLARE_DEVICE_RESIDENT, AB_OACC_DECLARE_LINK,
2095 AB_OMP_DECLARE_TARGET_LINK, AB_PDT_KIND, AB_PDT_LEN, AB_PDT_TYPE,
2096 AB_PDT_TEMPLATE, AB_PDT_ARRAY, AB_PDT_STRING,
2097 AB_OACC_ROUTINE_LOP_GANG, AB_OACC_ROUTINE_LOP_WORKER,
2098 AB_OACC_ROUTINE_LOP_VECTOR, AB_OACC_ROUTINE_LOP_SEQ,
2099 AB_OACC_ROUTINE_NOHOST,
2100 AB_OMP_REQ_REVERSE_OFFLOAD, AB_OMP_REQ_UNIFIED_ADDRESS, AB_OMP_REQ_SELF_MAPS,
2101 AB_OMP_REQ_UNIFIED_SHARED_MEMORY, AB_OMP_REQ_DYNAMIC_ALLOCATORS,
2102 AB_OMP_REQ_MEM_ORDER_SEQ_CST, AB_OMP_REQ_MEM_ORDER_ACQ_REL,
2103 AB_OMP_REQ_MEM_ORDER_ACQUIRE, AB_OMP_REQ_MEM_ORDER_RELEASE,
2104 AB_OMP_REQ_MEM_ORDER_RELAXED, AB_OMP_DEVICE_TYPE_NOHOST,
2105 AB_OMP_DEVICE_TYPE_HOST, AB_OMP_DEVICE_TYPE_ANY
2106 };
2107
2108 static const mstring attr_bits[] =
2109 {
2110 minit ("ALLOCATABLE", AB_ALLOCATABLE),
2111 minit ("ARTIFICIAL", AB_ARTIFICIAL),
2112 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
2113 minit ("DIMENSION", AB_DIMENSION),
2114 minit ("CODIMENSION", AB_CODIMENSION),
2115 minit ("CONTIGUOUS", AB_CONTIGUOUS),
2116 minit ("EXTERNAL", AB_EXTERNAL),
2117 minit ("INTRINSIC", AB_INTRINSIC),
2118 minit ("OPTIONAL", AB_OPTIONAL),
2119 minit ("POINTER", AB_POINTER),
2120 minit ("VOLATILE", AB_VOLATILE),
2121 minit ("TARGET", AB_TARGET),
2122 minit ("THREADPRIVATE", AB_THREADPRIVATE),
2123 minit ("DUMMY", AB_DUMMY),
2124 minit ("RESULT", AB_RESULT),
2125 minit ("DATA", AB_DATA),
2126 minit ("IN_NAMELIST", AB_IN_NAMELIST),
2127 minit ("IN_COMMON", AB_IN_COMMON),
2128 minit ("FUNCTION", AB_FUNCTION),
2129 minit ("SUBROUTINE", AB_SUBROUTINE),
2130 minit ("SEQUENCE", AB_SEQUENCE),
2131 minit ("ELEMENTAL", AB_ELEMENTAL),
2132 minit ("PURE", AB_PURE),
2133 minit ("RECURSIVE", AB_RECURSIVE),
2134 minit ("GENERIC", AB_GENERIC),
2135 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
2136 minit ("CRAY_POINTER", AB_CRAY_POINTER),
2137 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
2138 minit ("IS_BIND_C", AB_IS_BIND_C),
2139 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
2140 minit ("IS_ISO_C", AB_IS_ISO_C),
2141 minit ("VALUE", AB_VALUE),
2142 minit ("ALLOC_COMP", AB_ALLOC_COMP),
2143 minit ("COARRAY_COMP", AB_COARRAY_COMP),
2144 minit ("LOCK_COMP", AB_LOCK_COMP),
2145 minit ("EVENT_COMP", AB_EVENT_COMP),
2146 minit ("POINTER_COMP", AB_POINTER_COMP),
2147 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
2148 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
2149 minit ("ZERO_COMP", AB_ZERO_COMP),
2150 minit ("PROTECTED", AB_PROTECTED),
2151 minit ("ABSTRACT", AB_ABSTRACT),
2152 minit ("IS_CLASS", AB_IS_CLASS),
2153 minit ("PROCEDURE", AB_PROCEDURE),
2154 minit ("PROC_POINTER", AB_PROC_POINTER),
2155 minit ("VTYPE", AB_VTYPE),
2156 minit ("VTAB", AB_VTAB),
2157 minit ("CLASS_POINTER", AB_CLASS_POINTER),
2158 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
2159 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
2160 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
2161 minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
2162 minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
2163 minit ("OACC_DECLARE_CREATE", AB_OACC_DECLARE_CREATE),
2164 minit ("OACC_DECLARE_COPYIN", AB_OACC_DECLARE_COPYIN),
2165 minit ("OACC_DECLARE_DEVICEPTR", AB_OACC_DECLARE_DEVICEPTR),
2166 minit ("OACC_DECLARE_DEVICE_RESIDENT", AB_OACC_DECLARE_DEVICE_RESIDENT),
2167 minit ("OACC_DECLARE_LINK", AB_OACC_DECLARE_LINK),
2168 minit ("OMP_DECLARE_TARGET_LINK", AB_OMP_DECLARE_TARGET_LINK),
2169 minit ("PDT_KIND", AB_PDT_KIND),
2170 minit ("PDT_LEN", AB_PDT_LEN),
2171 minit ("PDT_TYPE", AB_PDT_TYPE),
2172 minit ("PDT_TEMPLATE", AB_PDT_TEMPLATE),
2173 minit ("PDT_ARRAY", AB_PDT_ARRAY),
2174 minit ("PDT_STRING", AB_PDT_STRING),
2175 minit ("OACC_ROUTINE_LOP_GANG", AB_OACC_ROUTINE_LOP_GANG),
2176 minit ("OACC_ROUTINE_LOP_WORKER", AB_OACC_ROUTINE_LOP_WORKER),
2177 minit ("OACC_ROUTINE_LOP_VECTOR", AB_OACC_ROUTINE_LOP_VECTOR),
2178 minit ("OACC_ROUTINE_LOP_SEQ", AB_OACC_ROUTINE_LOP_SEQ),
2179 minit ("OACC_ROUTINE_NOHOST", AB_OACC_ROUTINE_NOHOST),
2180 minit ("OMP_REQ_REVERSE_OFFLOAD", AB_OMP_REQ_REVERSE_OFFLOAD),
2181 minit ("OMP_REQ_UNIFIED_ADDRESS", AB_OMP_REQ_UNIFIED_ADDRESS),
2182 minit ("OMP_REQ_UNIFIED_SHARED_MEMORY", AB_OMP_REQ_UNIFIED_SHARED_MEMORY),
2183 minit ("OMP_REQ_SELF_MAPS", AB_OMP_REQ_SELF_MAPS),
2184 minit ("OMP_REQ_DYNAMIC_ALLOCATORS", AB_OMP_REQ_DYNAMIC_ALLOCATORS),
2185 minit ("OMP_REQ_MEM_ORDER_SEQ_CST", AB_OMP_REQ_MEM_ORDER_SEQ_CST),
2186 minit ("OMP_REQ_MEM_ORDER_ACQ_REL", AB_OMP_REQ_MEM_ORDER_ACQ_REL),
2187 minit ("OMP_REQ_MEM_ORDER_ACQUIRE", AB_OMP_REQ_MEM_ORDER_ACQUIRE),
2188 minit ("OMP_REQ_MEM_ORDER_RELAXED", AB_OMP_REQ_MEM_ORDER_RELAXED),
2189 minit ("OMP_REQ_MEM_ORDER_RELEASE", AB_OMP_REQ_MEM_ORDER_RELEASE),
2190 minit ("OMP_DEVICE_TYPE_HOST", AB_OMP_DEVICE_TYPE_HOST),
2191 minit ("OMP_DEVICE_TYPE_NOHOST", AB_OMP_DEVICE_TYPE_NOHOST),
2192 minit ("OMP_DEVICE_TYPE_ANYHOST", AB_OMP_DEVICE_TYPE_ANY),
2193 minit (NULL, -1)
2194 };
2195
2196 /* For binding attributes. */
2197 static const mstring binding_passing[] =
2198 {
2199 minit ("PASS", 0),
2200 minit ("NOPASS", 1),
2201 minit (NULL, -1)
2202 };
2203 static const mstring binding_overriding[] =
2204 {
2205 minit ("OVERRIDABLE", 0),
2206 minit ("NON_OVERRIDABLE", 1),
2207 minit ("DEFERRED", 2),
2208 minit (NULL, -1)
2209 };
2210 static const mstring binding_generic[] =
2211 {
2212 minit ("SPECIFIC", 0),
2213 minit ("GENERIC", 1),
2214 minit (NULL, -1)
2215 };
2216 static const mstring binding_ppc[] =
2217 {
2218 minit ("NO_PPC", 0),
2219 minit ("PPC", 1),
2220 minit (NULL, -1)
2221 };
2222
2223 /* Specialization of mio_name. */
2224 DECL_MIO_NAME (ab_attribute)
2225 DECL_MIO_NAME (ar_type)
2226 DECL_MIO_NAME (array_type)
2227 DECL_MIO_NAME (bt)
2228 DECL_MIO_NAME (expr_t)
2229 DECL_MIO_NAME (gfc_access)
2230 DECL_MIO_NAME (gfc_intrinsic_op)
2231 DECL_MIO_NAME (ifsrc)
2232 DECL_MIO_NAME (save_state)
2233 DECL_MIO_NAME (procedure_type)
2234 DECL_MIO_NAME (ref_type)
2235 DECL_MIO_NAME (sym_flavor)
2236 DECL_MIO_NAME (sym_intent)
2237 DECL_MIO_NAME (inquiry_type)
2238 #undef DECL_MIO_NAME
2239
2240 /* Verify OACC_ROUTINE_LOP_NONE. */
2241
2242 static void
2243 verify_OACC_ROUTINE_LOP_NONE (enum oacc_routine_lop lop)
2244 {
2245 if (lop != OACC_ROUTINE_LOP_NONE)
2246 bad_module ("Unsupported: multiple OpenACC 'routine' levels of parallelism");
2247 }
2248
2249 /* Symbol attributes are stored in list with the first three elements
2250 being the enumerated fields, while the remaining elements (if any)
2251 indicate the individual attribute bits. The access field is not
2252 saved-- it controls what symbols are exported when a module is
2253 written. */
2254
2255 static void
2256 mio_symbol_attribute (symbol_attribute *attr)
2257 {
2258 atom_type t;
2259 unsigned ext_attr,extension_level;
2260
2261 mio_lparen ();
2262
2263 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
2264 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
2265 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
2266 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
2267 attr->save = MIO_NAME (save_state) (attr->save, save_status);
2268
2269 ext_attr = attr->ext_attr;
2270 mio_integer ((int *) &ext_attr);
2271 attr->ext_attr = ext_attr;
2272
2273 extension_level = attr->extension;
2274 mio_integer ((int *) &extension_level);
2275 attr->extension = extension_level;
2276
2277 if (iomode == IO_OUTPUT)
2278 {
2279 if (attr->allocatable)
2280 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
2281 if (attr->artificial)
2282 MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
2283 if (attr->asynchronous)
2284 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
2285 if (attr->dimension)
2286 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
2287 if (attr->codimension)
2288 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
2289 if (attr->contiguous)
2290 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
2291 if (attr->external)
2292 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
2293 if (attr->intrinsic)
2294 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
2295 if (attr->optional)
2296 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
2297 if (attr->pointer)
2298 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
2299 if (attr->class_pointer)
2300 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
2301 if (attr->is_protected)
2302 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
2303 if (attr->value)
2304 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
2305 if (attr->volatile_)
2306 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
2307 if (attr->target)
2308 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
2309 if (attr->threadprivate)
2310 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
2311 if (attr->dummy)
2312 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
2313 if (attr->result)
2314 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
2315 /* We deliberately don't preserve the "entry" flag. */
2316
2317 if (attr->data)
2318 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
2319 if (attr->in_namelist)
2320 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
2321 if (attr->in_common)
2322 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
2323
2324 if (attr->function)
2325 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
2326 if (attr->subroutine)
2327 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
2328 if (attr->generic)
2329 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
2330 if (attr->abstract)
2331 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
2332
2333 if (attr->sequence)
2334 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
2335 if (attr->elemental)
2336 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2337 if (attr->pure)
2338 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2339 if (attr->implicit_pure)
2340 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2341 if (attr->unlimited_polymorphic)
2342 MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
2343 if (attr->recursive)
2344 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2345 if (attr->always_explicit)
2346 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2347 if (attr->cray_pointer)
2348 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2349 if (attr->cray_pointee)
2350 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2351 if (attr->is_bind_c)
2352 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2353 if (attr->is_c_interop)
2354 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2355 if (attr->is_iso_c)
2356 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2357 if (attr->alloc_comp)
2358 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2359 if (attr->pointer_comp)
2360 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2361 if (attr->proc_pointer_comp)
2362 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2363 if (attr->private_comp)
2364 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2365 if (attr->coarray_comp)
2366 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2367 if (attr->lock_comp)
2368 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2369 if (attr->event_comp)
2370 MIO_NAME (ab_attribute) (AB_EVENT_COMP, attr_bits);
2371 if (attr->zero_comp)
2372 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2373 if (attr->is_class)
2374 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2375 if (attr->procedure)
2376 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2377 if (attr->proc_pointer)
2378 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2379 if (attr->vtype)
2380 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2381 if (attr->vtab)
2382 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2383 if (attr->omp_declare_target)
2384 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
2385 if (attr->array_outer_dependency)
2386 MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
2387 if (attr->module_procedure)
2388 MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
2389 if (attr->oacc_declare_create)
2390 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_CREATE, attr_bits);
2391 if (attr->oacc_declare_copyin)
2392 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_COPYIN, attr_bits);
2393 if (attr->oacc_declare_deviceptr)
2394 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICEPTR, attr_bits);
2395 if (attr->oacc_declare_device_resident)
2396 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_DEVICE_RESIDENT, attr_bits);
2397 if (attr->oacc_declare_link)
2398 MIO_NAME (ab_attribute) (AB_OACC_DECLARE_LINK, attr_bits);
2399 if (attr->omp_declare_target_link)
2400 MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET_LINK, attr_bits);
2401 if (attr->pdt_kind)
2402 MIO_NAME (ab_attribute) (AB_PDT_KIND, attr_bits);
2403 if (attr->pdt_len)
2404 MIO_NAME (ab_attribute) (AB_PDT_LEN, attr_bits);
2405 if (attr->pdt_type)
2406 MIO_NAME (ab_attribute) (AB_PDT_TYPE, attr_bits);
2407 if (attr->pdt_template)
2408 MIO_NAME (ab_attribute) (AB_PDT_TEMPLATE, attr_bits);
2409 if (attr->pdt_array)
2410 MIO_NAME (ab_attribute) (AB_PDT_ARRAY, attr_bits);
2411 if (attr->pdt_string)
2412 MIO_NAME (ab_attribute) (AB_PDT_STRING, attr_bits);
2413 switch (attr->oacc_routine_lop)
2414 {
2415 case OACC_ROUTINE_LOP_NONE:
2416 /* This is the default anyway, and for maintaining compatibility with
2417 the current MOD_VERSION, we're not emitting anything in that
2418 case. */
2419 break;
2420 case OACC_ROUTINE_LOP_GANG:
2421 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_GANG, attr_bits);
2422 break;
2423 case OACC_ROUTINE_LOP_WORKER:
2424 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_WORKER, attr_bits);
2425 break;
2426 case OACC_ROUTINE_LOP_VECTOR:
2427 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_VECTOR, attr_bits);
2428 break;
2429 case OACC_ROUTINE_LOP_SEQ:
2430 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_LOP_SEQ, attr_bits);
2431 break;
2432 case OACC_ROUTINE_LOP_ERROR:
2433 /* ... intentionally omitted here; it's only used internally. */
2434 default:
2435 gcc_unreachable ();
2436 }
2437 if (attr->oacc_routine_nohost)
2438 MIO_NAME (ab_attribute) (AB_OACC_ROUTINE_NOHOST, attr_bits);
2439
2440 if (attr->flavor == FL_MODULE && gfc_current_ns->omp_requires)
2441 {
2442 if (gfc_current_ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD)
2443 MIO_NAME (ab_attribute) (AB_OMP_REQ_REVERSE_OFFLOAD, attr_bits);
2444 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_ADDRESS)
2445 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_ADDRESS, attr_bits);
2446 if (gfc_current_ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
2447 MIO_NAME (ab_attribute) (AB_OMP_REQ_UNIFIED_SHARED_MEMORY, attr_bits);
2448 if (gfc_current_ns->omp_requires & OMP_REQ_SELF_MAPS)
2449 MIO_NAME (ab_attribute) (AB_OMP_REQ_SELF_MAPS, attr_bits);
2450 if (gfc_current_ns->omp_requires & OMP_REQ_DYNAMIC_ALLOCATORS)
2451 MIO_NAME (ab_attribute) (AB_OMP_REQ_DYNAMIC_ALLOCATORS, attr_bits);
2452 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2453 == OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST)
2454 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_SEQ_CST, attr_bits);
2455 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2456 == OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL)
2457 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQ_REL, attr_bits);
2458 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2459 == OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE)
2460 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_ACQUIRE, attr_bits);
2461 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2462 == OMP_REQ_ATOMIC_MEM_ORDER_RELAXED)
2463 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELAXED, attr_bits);
2464 if ((gfc_current_ns->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
2465 == OMP_REQ_ATOMIC_MEM_ORDER_RELEASE)
2466 MIO_NAME (ab_attribute) (AB_OMP_REQ_MEM_ORDER_RELEASE, attr_bits);
2467 }
2468 switch (attr->omp_device_type)
2469 {
2470 case OMP_DEVICE_TYPE_UNSET:
2471 break;
2472 case OMP_DEVICE_TYPE_HOST:
2473 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_HOST, attr_bits);
2474 break;
2475 case OMP_DEVICE_TYPE_NOHOST:
2476 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_NOHOST, attr_bits);
2477 break;
2478 case OMP_DEVICE_TYPE_ANY:
2479 MIO_NAME (ab_attribute) (AB_OMP_DEVICE_TYPE_ANY, attr_bits);
2480 break;
2481 default:
2482 gcc_unreachable ();
2483 }
2484 mio_rparen ();
2485 }
2486 else
2487 {
2488 for (;;)
2489 {
2490 t = parse_atom ();
2491 if (t == ATOM_RPAREN)
2492 break;
2493 if (t != ATOM_NAME)
2494 bad_module ("Expected attribute bit name");
2495
2496 switch ((ab_attribute) find_enum (attr_bits))
2497 {
2498 case AB_ALLOCATABLE:
2499 attr->allocatable = 1;
2500 break;
2501 case AB_ARTIFICIAL:
2502 attr->artificial = 1;
2503 break;
2504 case AB_ASYNCHRONOUS:
2505 attr->asynchronous = 1;
2506 break;
2507 case AB_DIMENSION:
2508 attr->dimension = 1;
2509 break;
2510 case AB_CODIMENSION:
2511 attr->codimension = 1;
2512 break;
2513 case AB_CONTIGUOUS:
2514 attr->contiguous = 1;
2515 break;
2516 case AB_EXTERNAL:
2517 attr->external = 1;
2518 break;
2519 case AB_INTRINSIC:
2520 attr->intrinsic = 1;
2521 break;
2522 case AB_OPTIONAL:
2523 attr->optional = 1;
2524 break;
2525 case AB_POINTER:
2526 attr->pointer = 1;
2527 break;
2528 case AB_CLASS_POINTER:
2529 attr->class_pointer = 1;
2530 break;
2531 case AB_PROTECTED:
2532 attr->is_protected = 1;
2533 break;
2534 case AB_VALUE:
2535 attr->value = 1;
2536 break;
2537 case AB_VOLATILE:
2538 attr->volatile_ = 1;
2539 break;
2540 case AB_TARGET:
2541 attr->target = 1;
2542 break;
2543 case AB_THREADPRIVATE:
2544 attr->threadprivate = 1;
2545 break;
2546 case AB_DUMMY:
2547 attr->dummy = 1;
2548 break;
2549 case AB_RESULT:
2550 attr->result = 1;
2551 break;
2552 case AB_DATA:
2553 attr->data = 1;
2554 break;
2555 case AB_IN_NAMELIST:
2556 attr->in_namelist = 1;
2557 break;
2558 case AB_IN_COMMON:
2559 attr->in_common = 1;
2560 break;
2561 case AB_FUNCTION:
2562 attr->function = 1;
2563 break;
2564 case AB_SUBROUTINE:
2565 attr->subroutine = 1;
2566 break;
2567 case AB_GENERIC:
2568 attr->generic = 1;
2569 break;
2570 case AB_ABSTRACT:
2571 attr->abstract = 1;
2572 break;
2573 case AB_SEQUENCE:
2574 attr->sequence = 1;
2575 break;
2576 case AB_ELEMENTAL:
2577 attr->elemental = 1;
2578 break;
2579 case AB_PURE:
2580 attr->pure = 1;
2581 break;
2582 case AB_IMPLICIT_PURE:
2583 attr->implicit_pure = 1;
2584 break;
2585 case AB_UNLIMITED_POLY:
2586 attr->unlimited_polymorphic = 1;
2587 break;
2588 case AB_RECURSIVE:
2589 attr->recursive = 1;
2590 break;
2591 case AB_ALWAYS_EXPLICIT:
2592 attr->always_explicit = 1;
2593 break;
2594 case AB_CRAY_POINTER:
2595 attr->cray_pointer = 1;
2596 break;
2597 case AB_CRAY_POINTEE:
2598 attr->cray_pointee = 1;
2599 break;
2600 case AB_IS_BIND_C:
2601 attr->is_bind_c = 1;
2602 break;
2603 case AB_IS_C_INTEROP:
2604 attr->is_c_interop = 1;
2605 break;
2606 case AB_IS_ISO_C:
2607 attr->is_iso_c = 1;
2608 break;
2609 case AB_ALLOC_COMP:
2610 attr->alloc_comp = 1;
2611 break;
2612 case AB_COARRAY_COMP:
2613 attr->coarray_comp = 1;
2614 break;
2615 case AB_LOCK_COMP:
2616 attr->lock_comp = 1;
2617 break;
2618 case AB_EVENT_COMP:
2619 attr->event_comp = 1;
2620 break;
2621 case AB_POINTER_COMP:
2622 attr->pointer_comp = 1;
2623 break;
2624 case AB_PROC_POINTER_COMP:
2625 attr->proc_pointer_comp = 1;
2626 break;
2627 case AB_PRIVATE_COMP:
2628 attr->private_comp = 1;
2629 break;
2630 case AB_ZERO_COMP:
2631 attr->zero_comp = 1;
2632 break;
2633 case AB_IS_CLASS:
2634 attr->is_class = 1;
2635 break;
2636 case AB_PROCEDURE:
2637 attr->procedure = 1;
2638 break;
2639 case AB_PROC_POINTER:
2640 attr->proc_pointer = 1;
2641 break;
2642 case AB_VTYPE:
2643 attr->vtype = 1;
2644 break;
2645 case AB_VTAB:
2646 attr->vtab = 1;
2647 break;
2648 case AB_OMP_DECLARE_TARGET:
2649 attr->omp_declare_target = 1;
2650 break;
2651 case AB_OMP_DECLARE_TARGET_LINK:
2652 attr->omp_declare_target_link = 1;
2653 break;
2654 case AB_ARRAY_OUTER_DEPENDENCY:
2655 attr->array_outer_dependency =1;
2656 break;
2657 case AB_MODULE_PROCEDURE:
2658 attr->module_procedure =1;
2659 break;
2660 case AB_OACC_DECLARE_CREATE:
2661 attr->oacc_declare_create = 1;
2662 break;
2663 case AB_OACC_DECLARE_COPYIN:
2664 attr->oacc_declare_copyin = 1;
2665 break;
2666 case AB_OACC_DECLARE_DEVICEPTR:
2667 attr->oacc_declare_deviceptr = 1;
2668 break;
2669 case AB_OACC_DECLARE_DEVICE_RESIDENT:
2670 attr->oacc_declare_device_resident = 1;
2671 break;
2672 case AB_OACC_DECLARE_LINK:
2673 attr->oacc_declare_link = 1;
2674 break;
2675 case AB_PDT_KIND:
2676 attr->pdt_kind = 1;
2677 break;
2678 case AB_PDT_LEN:
2679 attr->pdt_len = 1;
2680 break;
2681 case AB_PDT_TYPE:
2682 attr->pdt_type = 1;
2683 break;
2684 case AB_PDT_TEMPLATE:
2685 attr->pdt_template = 1;
2686 break;
2687 case AB_PDT_ARRAY:
2688 attr->pdt_array = 1;
2689 break;
2690 case AB_PDT_STRING:
2691 attr->pdt_string = 1;
2692 break;
2693 case AB_OACC_ROUTINE_LOP_GANG:
2694 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2695 attr->oacc_routine_lop = OACC_ROUTINE_LOP_GANG;
2696 break;
2697 case AB_OACC_ROUTINE_LOP_WORKER:
2698 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2699 attr->oacc_routine_lop = OACC_ROUTINE_LOP_WORKER;
2700 break;
2701 case AB_OACC_ROUTINE_LOP_VECTOR:
2702 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2703 attr->oacc_routine_lop = OACC_ROUTINE_LOP_VECTOR;
2704 break;
2705 case AB_OACC_ROUTINE_LOP_SEQ:
2706 verify_OACC_ROUTINE_LOP_NONE (attr->oacc_routine_lop);
2707 attr->oacc_routine_lop = OACC_ROUTINE_LOP_SEQ;
2708 break;
2709 case AB_OACC_ROUTINE_NOHOST:
2710 attr->oacc_routine_nohost = 1;
2711 break;
2712 case AB_OMP_REQ_REVERSE_OFFLOAD:
2713 gfc_omp_requires_add_clause (OMP_REQ_REVERSE_OFFLOAD,
2714 "reverse_offload",
2715 &gfc_current_locus,
2716 module_name);
2717 break;
2718 case AB_OMP_REQ_UNIFIED_ADDRESS:
2719 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_ADDRESS,
2720 "unified_address",
2721 &gfc_current_locus,
2722 module_name);
2723 break;
2724 case AB_OMP_REQ_UNIFIED_SHARED_MEMORY:
2725 gfc_omp_requires_add_clause (OMP_REQ_UNIFIED_SHARED_MEMORY,
2726 "unified_shared_memory",
2727 &gfc_current_locus,
2728 module_name);
2729 break;
2730 case AB_OMP_REQ_SELF_MAPS:
2731 gfc_omp_requires_add_clause (OMP_REQ_SELF_MAPS,
2732 "self_maps",
2733 &gfc_current_locus,
2734 module_name);
2735 break;
2736 case AB_OMP_REQ_DYNAMIC_ALLOCATORS:
2737 gfc_omp_requires_add_clause (OMP_REQ_DYNAMIC_ALLOCATORS,
2738 "dynamic_allocators",
2739 &gfc_current_locus,
2740 module_name);
2741 break;
2742 case AB_OMP_REQ_MEM_ORDER_SEQ_CST:
2743 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST,
2744 "seq_cst", &gfc_current_locus,
2745 module_name);
2746 break;
2747 case AB_OMP_REQ_MEM_ORDER_ACQ_REL:
2748 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL,
2749 "acq_rel", &gfc_current_locus,
2750 module_name);
2751 break;
2752 case AB_OMP_REQ_MEM_ORDER_ACQUIRE:
2753 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE,
2754 "acquires", &gfc_current_locus,
2755 module_name);
2756 break;
2757 case AB_OMP_REQ_MEM_ORDER_RELAXED:
2758 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELAXED,
2759 "relaxed", &gfc_current_locus,
2760 module_name);
2761 break;
2762 case AB_OMP_REQ_MEM_ORDER_RELEASE:
2763 gfc_omp_requires_add_clause (OMP_REQ_ATOMIC_MEM_ORDER_RELEASE,
2764 "release", &gfc_current_locus,
2765 module_name);
2766 break;
2767 case AB_OMP_DEVICE_TYPE_HOST:
2768 attr->omp_device_type = OMP_DEVICE_TYPE_HOST;
2769 break;
2770 case AB_OMP_DEVICE_TYPE_NOHOST:
2771 attr->omp_device_type = OMP_DEVICE_TYPE_NOHOST;
2772 break;
2773 case AB_OMP_DEVICE_TYPE_ANY:
2774 attr->omp_device_type = OMP_DEVICE_TYPE_ANY;
2775 break;
2776 }
2777 }
2778 }
2779 }
2780
2781
2782 static const mstring bt_types[] = {
2783 minit ("INTEGER", BT_INTEGER),
2784 minit ("REAL", BT_REAL),
2785 minit ("COMPLEX", BT_COMPLEX),
2786 minit ("LOGICAL", BT_LOGICAL),
2787 minit ("CHARACTER", BT_CHARACTER),
2788 minit ("UNION", BT_UNION),
2789 minit ("DERIVED", BT_DERIVED),
2790 minit ("CLASS", BT_CLASS),
2791 minit ("PROCEDURE", BT_PROCEDURE),
2792 minit ("UNKNOWN", BT_UNKNOWN),
2793 minit ("VOID", BT_VOID),
2794 minit ("ASSUMED", BT_ASSUMED),
2795 minit ("UNSIGNED", BT_UNSIGNED),
2796 minit (NULL, -1)
2797 };
2798
2799
2800 static void
2801 mio_charlen (gfc_charlen **clp)
2802 {
2803 gfc_charlen *cl;
2804
2805 mio_lparen ();
2806
2807 if (iomode == IO_OUTPUT)
2808 {
2809 cl = *clp;
2810 if (cl != NULL)
2811 mio_expr (&cl->length);
2812 }
2813 else
2814 {
2815 if (peek_atom () != ATOM_RPAREN)
2816 {
2817 cl = gfc_new_charlen (gfc_current_ns, NULL);
2818 mio_expr (&cl->length);
2819 *clp = cl;
2820 }
2821 }
2822
2823 mio_rparen ();
2824 }
2825
2826
2827 /* See if a name is a generated name. */
2828
2829 static int
2830 check_unique_name (const char *name)
2831 {
2832 return *name == '@';
2833 }
2834
2835
2836 static void
2837 mio_typespec (gfc_typespec *ts)
2838 {
2839 mio_lparen ();
2840
2841 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2842
2843 if (!gfc_bt_struct (ts->type) && ts->type != BT_CLASS)
2844 mio_integer (&ts->kind);
2845 else
2846 mio_symbol_ref (&ts->u.derived);
2847
2848 mio_symbol_ref (&ts->interface);
2849
2850 /* Add info for C interop and is_iso_c. */
2851 mio_integer (&ts->is_c_interop);
2852 mio_integer (&ts->is_iso_c);
2853
2854 /* If the typespec is for an identifier either from iso_c_binding, or
2855 a constant that was initialized to an identifier from it, use the
2856 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2857 if (ts->is_iso_c)
2858 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2859 else
2860 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2861
2862 if (ts->type != BT_CHARACTER)
2863 {
2864 /* ts->u.cl is only valid for BT_CHARACTER. */
2865 mio_lparen ();
2866 mio_rparen ();
2867 }
2868 else
2869 mio_charlen (&ts->u.cl);
2870
2871 /* So as not to disturb the existing API, use an ATOM_NAME to
2872 transmit deferred characteristic for characters (F2003). */
2873 if (iomode == IO_OUTPUT)
2874 {
2875 if (ts->type == BT_CHARACTER && ts->deferred)
2876 write_atom (ATOM_NAME, "DEFERRED_CL");
2877 }
2878 else if (peek_atom () != ATOM_RPAREN)
2879 {
2880 if (parse_atom () != ATOM_NAME)
2881 bad_module ("Expected string");
2882 ts->deferred = 1;
2883 }
2884
2885 mio_rparen ();
2886 }
2887
2888
2889 static const mstring array_spec_types[] = {
2890 minit ("EXPLICIT", AS_EXPLICIT),
2891 minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
2892 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2893 minit ("DEFERRED", AS_DEFERRED),
2894 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2895 minit (NULL, -1)
2896 };
2897
2898
2899 static void
2900 mio_array_spec (gfc_array_spec **asp)
2901 {
2902 gfc_array_spec *as;
2903 int i;
2904
2905 mio_lparen ();
2906
2907 if (iomode == IO_OUTPUT)
2908 {
2909 int rank;
2910
2911 if (*asp == NULL)
2912 goto done;
2913 as = *asp;
2914
2915 /* mio_integer expects nonnegative values. */
2916 rank = as->rank > 0 ? as->rank : 0;
2917 mio_integer (&rank);
2918 }
2919 else
2920 {
2921 if (peek_atom () == ATOM_RPAREN)
2922 {
2923 *asp = NULL;
2924 goto done;
2925 }
2926
2927 *asp = as = gfc_get_array_spec ();
2928 mio_integer (&as->rank);
2929 }
2930
2931 mio_integer (&as->corank);
2932 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2933
2934 if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
2935 as->rank = -1;
2936 if (iomode == IO_INPUT && as->corank)
2937 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2938
2939 if (as->rank + as->corank > 0)
2940 for (i = 0; i < as->rank + as->corank; i++)
2941 {
2942 mio_expr (&as->lower[i]);
2943 mio_expr (&as->upper[i]);
2944 }
2945
2946 done:
2947 mio_rparen ();
2948 }
2949
2950
2951 /* Given a pointer to an array reference structure (which lives in a
2952 gfc_ref structure), find the corresponding array specification
2953 structure. Storing the pointer in the ref structure doesn't quite
2954 work when loading from a module. Generating code for an array
2955 reference also needs more information than just the array spec. */
2956
2957 static const mstring array_ref_types[] = {
2958 minit ("FULL", AR_FULL),
2959 minit ("ELEMENT", AR_ELEMENT),
2960 minit ("SECTION", AR_SECTION),
2961 minit (NULL, -1)
2962 };
2963
2964
2965 static void
2966 mio_array_ref (gfc_array_ref *ar)
2967 {
2968 int i;
2969
2970 mio_lparen ();
2971 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2972 mio_integer (&ar->dimen);
2973
2974 switch (ar->type)
2975 {
2976 case AR_FULL:
2977 break;
2978
2979 case AR_ELEMENT:
2980 for (i = 0; i < ar->dimen; i++)
2981 mio_expr (&ar->start[i]);
2982
2983 break;
2984
2985 case AR_SECTION:
2986 for (i = 0; i < ar->dimen; i++)
2987 {
2988 mio_expr (&ar->start[i]);
2989 mio_expr (&ar->end[i]);
2990 mio_expr (&ar->stride[i]);
2991 }
2992
2993 break;
2994
2995 case AR_UNKNOWN:
2996 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2997 }
2998
2999 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
3000 we can't call mio_integer directly. Instead loop over each element
3001 and cast it to/from an integer. */
3002 if (iomode == IO_OUTPUT)
3003 {
3004 for (i = 0; i < ar->dimen; i++)
3005 {
3006 HOST_WIDE_INT tmp = (HOST_WIDE_INT)ar->dimen_type[i];
3007 write_atom (ATOM_INTEGER, &tmp);
3008 }
3009 }
3010 else
3011 {
3012 for (i = 0; i < ar->dimen; i++)
3013 {
3014 require_atom (ATOM_INTEGER);
3015 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
3016 }
3017 }
3018
3019 if (iomode == IO_INPUT)
3020 {
3021 ar->where = gfc_current_locus;
3022
3023 for (i = 0; i < ar->dimen; i++)
3024 ar->c_where[i] = gfc_current_locus;
3025 }
3026
3027 mio_rparen ();
3028 }
3029
3030
3031 /* Saves or restores a pointer. The pointer is converted back and
3032 forth from an integer. We return the pointer_info pointer so that
3033 the caller can take additional action based on the pointer type. */
3034
3035 static pointer_info *
3036 mio_pointer_ref (void *gp)
3037 {
3038 pointer_info *p;
3039
3040 if (iomode == IO_OUTPUT)
3041 {
3042 p = get_pointer (*((char **) gp));
3043 HOST_WIDE_INT hwi = p->integer;
3044 write_atom (ATOM_INTEGER, &hwi);
3045 }
3046 else
3047 {
3048 require_atom (ATOM_INTEGER);
3049 p = add_fixup (atom_int, gp);
3050 }
3051
3052 return p;
3053 }
3054
3055
3056 /* Save and load references to components that occur within
3057 expressions. We have to describe these references by a number and
3058 by name. The number is necessary for forward references during
3059 reading, and the name is necessary if the symbol already exists in
3060 the namespace and is not loaded again. */
3061
3062 static void
3063 mio_component_ref (gfc_component **cp)
3064 {
3065 pointer_info *p;
3066
3067 p = mio_pointer_ref (cp);
3068 if (p->type == P_UNKNOWN)
3069 p->type = P_COMPONENT;
3070 }
3071
3072
3073 static void mio_namespace_ref (gfc_namespace **nsp);
3074 static void mio_formal_arglist (gfc_formal_arglist **formal);
3075 static void mio_typebound_proc (gfc_typebound_proc** proc);
3076 static void mio_actual_arglist (gfc_actual_arglist **ap, bool pdt);
3077
3078 static void
3079 mio_component (gfc_component *c, int vtype)
3080 {
3081 pointer_info *p;
3082
3083 mio_lparen ();
3084
3085 if (iomode == IO_OUTPUT)
3086 {
3087 p = get_pointer (c);
3088 mio_hwi (&p->integer);
3089 }
3090 else
3091 {
3092 HOST_WIDE_INT n;
3093 mio_hwi (&n);
3094 p = get_integer (n);
3095 associate_integer_pointer (p, c);
3096 }
3097
3098 if (p->type == P_UNKNOWN)
3099 p->type = P_COMPONENT;
3100
3101 mio_pool_string (&c->name);
3102 mio_typespec (&c->ts);
3103 mio_array_spec (&c->as);
3104
3105 /* PDT templates store the expression for the kind of a component here. */
3106 mio_expr (&c->kind_expr);
3107
3108 /* PDT types store the component specification list here. */
3109 mio_actual_arglist (&c->param_list, true);
3110
3111 mio_symbol_attribute (&c->attr);
3112 if (c->ts.type == BT_CLASS)
3113 c->attr.class_ok = 1;
3114 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
3115
3116 if (!vtype || strcmp (c->name, "_final") == 0
3117 || strcmp (c->name, "_hash") == 0)
3118 mio_expr (&c->initializer);
3119
3120 if (c->attr.proc_pointer)
3121 mio_typebound_proc (&c->tb);
3122
3123 c->loc = gfc_current_locus;
3124
3125 mio_rparen ();
3126 }
3127
3128
3129 static void
3130 mio_component_list (gfc_component **cp, int vtype)
3131 {
3132 gfc_component *c, *tail;
3133
3134 mio_lparen ();
3135
3136 if (iomode == IO_OUTPUT)
3137 {
3138 for (c = *cp; c; c = c->next)
3139 mio_component (c, vtype);
3140 }
3141 else
3142 {
3143 *cp = NULL;
3144 tail = NULL;
3145
3146 for (;;)
3147 {
3148 if (peek_atom () == ATOM_RPAREN)
3149 break;
3150
3151 c = gfc_get_component ();
3152 mio_component (c, vtype);
3153
3154 if (tail == NULL)
3155 *cp = c;
3156 else
3157 tail->next = c;
3158
3159 tail = c;
3160 }
3161 }
3162
3163 mio_rparen ();
3164 }
3165
3166
3167 static void
3168 mio_actual_arg (gfc_actual_arglist *a, bool pdt)
3169 {
3170 mio_lparen ();
3171 mio_pool_string (&a->name);
3172 mio_expr (&a->expr);
3173 if (pdt)
3174 mio_integer ((int *)&a->spec_type);
3175 mio_rparen ();
3176 }
3177
3178
3179 static void
3180 mio_actual_arglist (gfc_actual_arglist **ap, bool pdt)
3181 {
3182 gfc_actual_arglist *a, *tail;
3183
3184 mio_lparen ();
3185
3186 if (iomode == IO_OUTPUT)
3187 {
3188 for (a = *ap; a; a = a->next)
3189 mio_actual_arg (a, pdt);
3190
3191 }
3192 else
3193 {
3194 tail = NULL;
3195
3196 for (;;)
3197 {
3198 if (peek_atom () != ATOM_LPAREN)
3199 break;
3200
3201 a = gfc_get_actual_arglist ();
3202
3203 if (tail == NULL)
3204 *ap = a;
3205 else
3206 tail->next = a;
3207
3208 tail = a;
3209 mio_actual_arg (a, pdt);
3210 }
3211 }
3212
3213 mio_rparen ();
3214 }
3215
3216
3217 /* Read and write formal argument lists. */
3218
3219 static void
3220 mio_formal_arglist (gfc_formal_arglist **formal)
3221 {
3222 gfc_formal_arglist *f, *tail;
3223
3224 mio_lparen ();
3225
3226 if (iomode == IO_OUTPUT)
3227 {
3228 for (f = *formal; f; f = f->next)
3229 mio_symbol_ref (&f->sym);
3230 }
3231 else
3232 {
3233 *formal = tail = NULL;
3234
3235 while (peek_atom () != ATOM_RPAREN)
3236 {
3237 f = gfc_get_formal_arglist ();
3238 mio_symbol_ref (&f->sym);
3239
3240 if (*formal == NULL)
3241 *formal = f;
3242 else
3243 tail->next = f;
3244
3245 tail = f;
3246 }
3247 }
3248
3249 mio_rparen ();
3250 }
3251
3252
3253 /* Save or restore a reference to a symbol node. */
3254
3255 pointer_info *
3256 mio_symbol_ref (gfc_symbol **symp)
3257 {
3258 pointer_info *p;
3259
3260 p = mio_pointer_ref (symp);
3261 if (p->type == P_UNKNOWN)
3262 p->type = P_SYMBOL;
3263
3264 if (iomode == IO_OUTPUT)
3265 {
3266 if (p->u.wsym.state == UNREFERENCED)
3267 p->u.wsym.state = NEEDS_WRITE;
3268 }
3269 else
3270 {
3271 if (p->u.rsym.state == UNUSED)
3272 p->u.rsym.state = NEEDED;
3273 }
3274 return p;
3275 }
3276
3277
3278 /* Save or restore a reference to a symtree node. */
3279
3280 static void
3281 mio_symtree_ref (gfc_symtree **stp)
3282 {
3283 pointer_info *p;
3284 fixup_t *f;
3285
3286 if (iomode == IO_OUTPUT)
3287 mio_symbol_ref (&(*stp)->n.sym);
3288 else
3289 {
3290 require_atom (ATOM_INTEGER);
3291 p = get_integer (atom_int);
3292
3293 /* An unused equivalence member; make a symbol and a symtree
3294 for it. */
3295 if (in_load_equiv && p->u.rsym.symtree == NULL)
3296 {
3297 /* Since this is not used, it must have a unique name. */
3298 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
3299
3300 /* Make the symbol. */
3301 if (p->u.rsym.sym == NULL)
3302 {
3303 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
3304 gfc_current_ns);
3305 p->u.rsym.sym->module = gfc_get_string ("%s", p->u.rsym.module);
3306 }
3307
3308 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
3309 p->u.rsym.symtree->n.sym->refs++;
3310 p->u.rsym.referenced = 1;
3311
3312 /* If the symbol is PRIVATE and in COMMON, load_commons will
3313 generate a fixup symbol, which must be associated. */
3314 if (p->fixup)
3315 resolve_fixups (p->fixup, p->u.rsym.sym);
3316 p->fixup = NULL;
3317 }
3318
3319 if (p->type == P_UNKNOWN)
3320 p->type = P_SYMBOL;
3321
3322 if (p->u.rsym.state == UNUSED)
3323 p->u.rsym.state = NEEDED;
3324
3325 if (p->u.rsym.symtree != NULL)
3326 {
3327 *stp = p->u.rsym.symtree;
3328 }
3329 else
3330 {
3331 f = XCNEW (fixup_t);
3332
3333 f->next = p->u.rsym.stfixup;
3334 p->u.rsym.stfixup = f;
3335
3336 f->pointer = (void **) stp;
3337 }
3338 }
3339 }
3340
3341
3342 static void
3343 mio_iterator (gfc_iterator **ip)
3344 {
3345 gfc_iterator *iter;
3346
3347 mio_lparen ();
3348
3349 if (iomode == IO_OUTPUT)
3350 {
3351 if (*ip == NULL)
3352 goto done;
3353 }
3354 else
3355 {
3356 if (peek_atom () == ATOM_RPAREN)
3357 {
3358 *ip = NULL;
3359 goto done;
3360 }
3361
3362 *ip = gfc_get_iterator ();
3363 }
3364
3365 iter = *ip;
3366
3367 mio_expr (&iter->var);
3368 mio_expr (&iter->start);
3369 mio_expr (&iter->end);
3370 mio_expr (&iter->step);
3371
3372 done:
3373 mio_rparen ();
3374 }
3375
3376
3377 static void
3378 mio_constructor (gfc_constructor_base *cp)
3379 {
3380 gfc_constructor *c;
3381
3382 mio_lparen ();
3383
3384 if (iomode == IO_OUTPUT)
3385 {
3386 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
3387 {
3388 mio_lparen ();
3389 mio_expr (&c->expr);
3390 mio_iterator (&c->iterator);
3391 mio_rparen ();
3392 }
3393 }
3394 else
3395 {
3396 while (peek_atom () != ATOM_RPAREN)
3397 {
3398 c = gfc_constructor_append_expr (cp, NULL, NULL);
3399
3400 mio_lparen ();
3401 mio_expr (&c->expr);
3402 mio_iterator (&c->iterator);
3403 mio_rparen ();
3404 }
3405 }
3406
3407 mio_rparen ();
3408 }
3409
3410
3411 static const mstring ref_types[] = {
3412 minit ("ARRAY", REF_ARRAY),
3413 minit ("COMPONENT", REF_COMPONENT),
3414 minit ("SUBSTRING", REF_SUBSTRING),
3415 minit ("INQUIRY", REF_INQUIRY),
3416 minit (NULL, -1)
3417 };
3418
3419 static const mstring inquiry_types[] = {
3420 minit ("RE", INQUIRY_RE),
3421 minit ("IM", INQUIRY_IM),
3422 minit ("KIND", INQUIRY_KIND),
3423 minit ("LEN", INQUIRY_LEN),
3424 minit (NULL, -1)
3425 };
3426
3427
3428 static void
3429 mio_ref (gfc_ref **rp)
3430 {
3431 gfc_ref *r;
3432
3433 mio_lparen ();
3434
3435 r = *rp;
3436 r->type = MIO_NAME (ref_type) (r->type, ref_types);
3437
3438 switch (r->type)
3439 {
3440 case REF_ARRAY:
3441 mio_array_ref (&r->u.ar);
3442 break;
3443
3444 case REF_COMPONENT:
3445 mio_symbol_ref (&r->u.c.sym);
3446 mio_component_ref (&r->u.c.component);
3447 break;
3448
3449 case REF_SUBSTRING:
3450 mio_expr (&r->u.ss.start);
3451 mio_expr (&r->u.ss.end);
3452 mio_charlen (&r->u.ss.length);
3453 break;
3454
3455 case REF_INQUIRY:
3456 r->u.i = MIO_NAME (inquiry_type) (r->u.i, inquiry_types);
3457 break;
3458 }
3459
3460 mio_rparen ();
3461 }
3462
3463
3464 static void
3465 mio_ref_list (gfc_ref **rp)
3466 {
3467 gfc_ref *ref, *head, *tail;
3468
3469 mio_lparen ();
3470
3471 if (iomode == IO_OUTPUT)
3472 {
3473 for (ref = *rp; ref; ref = ref->next)
3474 mio_ref (&ref);
3475 }
3476 else
3477 {
3478 head = tail = NULL;
3479
3480 while (peek_atom () != ATOM_RPAREN)
3481 {
3482 if (head == NULL)
3483 head = tail = gfc_get_ref ();
3484 else
3485 {
3486 tail->next = gfc_get_ref ();
3487 tail = tail->next;
3488 }
3489
3490 mio_ref (&tail);
3491 }
3492
3493 *rp = head;
3494 }
3495
3496 mio_rparen ();
3497 }
3498
3499
3500 /* Read and write an integer value. */
3501
3502 static void
3503 mio_gmp_integer (mpz_t *integer)
3504 {
3505 char *p;
3506
3507 if (iomode == IO_INPUT)
3508 {
3509 if (parse_atom () != ATOM_STRING)
3510 bad_module ("Expected integer string");
3511
3512 mpz_init (*integer);
3513 if (mpz_set_str (*integer, atom_string, 10))
3514 bad_module ("Error converting integer");
3515
3516 free (atom_string);
3517 }
3518 else
3519 {
3520 p = mpz_get_str (NULL, 10, *integer);
3521 write_atom (ATOM_STRING, p);
3522 free (p);
3523 }
3524 }
3525
3526
3527 static void
3528 mio_gmp_real (mpfr_t *real)
3529 {
3530 mpfr_exp_t exponent;
3531 char *p;
3532
3533 if (iomode == IO_INPUT)
3534 {
3535 if (parse_atom () != ATOM_STRING)
3536 bad_module ("Expected real string");
3537
3538 mpfr_init (*real);
3539 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
3540 free (atom_string);
3541 }
3542 else
3543 {
3544 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
3545
3546 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
3547 {
3548 write_atom (ATOM_STRING, p);
3549 free (p);
3550 return;
3551 }
3552
3553 atom_string = XCNEWVEC (char, strlen (p) + 20);
3554
3555 sprintf (atom_string, "0.%s@%ld", p, exponent);
3556
3557 /* Fix negative numbers. */
3558 if (atom_string[2] == '-')
3559 {
3560 atom_string[0] = '-';
3561 atom_string[1] = '0';
3562 atom_string[2] = '.';
3563 }
3564
3565 write_atom (ATOM_STRING, atom_string);
3566
3567 free (atom_string);
3568 free (p);
3569 }
3570 }
3571
3572
3573 /* Save and restore the shape of an array constructor. */
3574
3575 static void
3576 mio_shape (mpz_t **pshape, int rank)
3577 {
3578 mpz_t *shape;
3579 atom_type t;
3580 int n;
3581
3582 /* A NULL shape is represented by (). */
3583 mio_lparen ();
3584
3585 if (iomode == IO_OUTPUT)
3586 {
3587 shape = *pshape;
3588 if (!shape)
3589 {
3590 mio_rparen ();
3591 return;
3592 }
3593 }
3594 else
3595 {
3596 t = peek_atom ();
3597 if (t == ATOM_RPAREN)
3598 {
3599 *pshape = NULL;
3600 mio_rparen ();
3601 return;
3602 }
3603
3604 shape = gfc_get_shape (rank);
3605 *pshape = shape;
3606 }
3607
3608 for (n = 0; n < rank; n++)
3609 mio_gmp_integer (&shape[n]);
3610
3611 mio_rparen ();
3612 }
3613
3614
3615 static const mstring expr_types[] = {
3616 minit ("OP", EXPR_OP),
3617 minit ("FUNCTION", EXPR_FUNCTION),
3618 minit ("CONSTANT", EXPR_CONSTANT),
3619 minit ("VARIABLE", EXPR_VARIABLE),
3620 minit ("SUBSTRING", EXPR_SUBSTRING),
3621 minit ("STRUCTURE", EXPR_STRUCTURE),
3622 minit ("ARRAY", EXPR_ARRAY),
3623 minit ("NULL", EXPR_NULL),
3624 minit ("COMPCALL", EXPR_COMPCALL),
3625 minit (NULL, -1)
3626 };
3627
3628 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3629 generic operators, not in expressions. INTRINSIC_USER is also
3630 replaced by the correct function name by the time we see it. */
3631
3632 static const mstring intrinsics[] =
3633 {
3634 minit ("UPLUS", INTRINSIC_UPLUS),
3635 minit ("UMINUS", INTRINSIC_UMINUS),
3636 minit ("PLUS", INTRINSIC_PLUS),
3637 minit ("MINUS", INTRINSIC_MINUS),
3638 minit ("TIMES", INTRINSIC_TIMES),
3639 minit ("DIVIDE", INTRINSIC_DIVIDE),
3640 minit ("POWER", INTRINSIC_POWER),
3641 minit ("CONCAT", INTRINSIC_CONCAT),
3642 minit ("AND", INTRINSIC_AND),
3643 minit ("OR", INTRINSIC_OR),
3644 minit ("EQV", INTRINSIC_EQV),
3645 minit ("NEQV", INTRINSIC_NEQV),
3646 minit ("EQ_SIGN", INTRINSIC_EQ),
3647 minit ("EQ", INTRINSIC_EQ_OS),
3648 minit ("NE_SIGN", INTRINSIC_NE),
3649 minit ("NE", INTRINSIC_NE_OS),
3650 minit ("GT_SIGN", INTRINSIC_GT),
3651 minit ("GT", INTRINSIC_GT_OS),
3652 minit ("GE_SIGN", INTRINSIC_GE),
3653 minit ("GE", INTRINSIC_GE_OS),
3654 minit ("LT_SIGN", INTRINSIC_LT),
3655 minit ("LT", INTRINSIC_LT_OS),
3656 minit ("LE_SIGN", INTRINSIC_LE),
3657 minit ("LE", INTRINSIC_LE_OS),
3658 minit ("NOT", INTRINSIC_NOT),
3659 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3660 minit ("USER", INTRINSIC_USER),
3661 minit (NULL, -1)
3662 };
3663
3664
3665 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3666
3667 static void
3668 fix_mio_expr (gfc_expr *e)
3669 {
3670 gfc_symtree *ns_st = NULL;
3671 const char *fname;
3672
3673 if (iomode != IO_OUTPUT)
3674 return;
3675
3676 if (e->symtree)
3677 {
3678 /* If this is a symtree for a symbol that came from a contained module
3679 namespace, it has a unique name and we should look in the current
3680 namespace to see if the required, non-contained symbol is available
3681 yet. If so, the latter should be written. */
3682 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3683 {
3684 const char *name = e->symtree->n.sym->name;
3685 if (gfc_fl_struct (e->symtree->n.sym->attr.flavor))
3686 name = gfc_dt_upper_string (name);
3687 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3688 }
3689
3690 /* On the other hand, if the existing symbol is the module name or the
3691 new symbol is a dummy argument, do not do the promotion. */
3692 if (ns_st && ns_st->n.sym
3693 && ns_st->n.sym->attr.flavor != FL_MODULE
3694 && !e->symtree->n.sym->attr.dummy)
3695 e->symtree = ns_st;
3696 }
3697 else if (e->expr_type == EXPR_FUNCTION
3698 && (e->value.function.name || e->value.function.isym))
3699 {
3700 gfc_symbol *sym;
3701
3702 /* In some circumstances, a function used in an initialization
3703 expression, in one use associated module, can fail to be
3704 coupled to its symtree when used in a specification
3705 expression in another module. */
3706 fname = e->value.function.esym ? e->value.function.esym->name
3707 : e->value.function.isym->name;
3708 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3709
3710 if (e->symtree)
3711 return;
3712
3713 /* This is probably a reference to a private procedure from another
3714 module. To prevent a segfault, make a generic with no specific
3715 instances. If this module is used, without the required
3716 specific coming from somewhere, the appropriate error message
3717 is issued. */
3718 gfc_get_symbol (fname, gfc_current_ns, &sym);
3719 sym->attr.flavor = FL_PROCEDURE;
3720 sym->attr.generic = 1;
3721 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3722 gfc_commit_symbol (sym);
3723 }
3724 }
3725
3726
3727 /* Read and write expressions. The form "()" is allowed to indicate a
3728 NULL expression. */
3729
3730 static void
3731 mio_expr (gfc_expr **ep)
3732 {
3733 HOST_WIDE_INT hwi;
3734 gfc_expr *e;
3735 atom_type t;
3736 int flag;
3737
3738 mio_lparen ();
3739
3740 if (iomode == IO_OUTPUT)
3741 {
3742 if (*ep == NULL)
3743 {
3744 mio_rparen ();
3745 return;
3746 }
3747
3748 e = *ep;
3749 MIO_NAME (expr_t) (e->expr_type, expr_types);
3750 }
3751 else
3752 {
3753 t = parse_atom ();
3754 if (t == ATOM_RPAREN)
3755 {
3756 *ep = NULL;
3757 return;
3758 }
3759
3760 if (t != ATOM_NAME)
3761 bad_module ("Expected expression type");
3762
3763 e = *ep = gfc_get_expr ();
3764 e->where = gfc_current_locus;
3765 e->expr_type = (expr_t) find_enum (expr_types);
3766 }
3767
3768 mio_typespec (&e->ts);
3769 mio_integer (&e->rank);
3770
3771 fix_mio_expr (e);
3772
3773 switch (e->expr_type)
3774 {
3775 case EXPR_OP:
3776 e->value.op.op
3777 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3778
3779 switch (e->value.op.op)
3780 {
3781 case INTRINSIC_UPLUS:
3782 case INTRINSIC_UMINUS:
3783 case INTRINSIC_NOT:
3784 case INTRINSIC_PARENTHESES:
3785 mio_expr (&e->value.op.op1);
3786 break;
3787
3788 case INTRINSIC_PLUS:
3789 case INTRINSIC_MINUS:
3790 case INTRINSIC_TIMES:
3791 case INTRINSIC_DIVIDE:
3792 case INTRINSIC_POWER:
3793 case INTRINSIC_CONCAT:
3794 case INTRINSIC_AND:
3795 case INTRINSIC_OR:
3796 case INTRINSIC_EQV:
3797 case INTRINSIC_NEQV:
3798 case INTRINSIC_EQ:
3799 case INTRINSIC_EQ_OS:
3800 case INTRINSIC_NE:
3801 case INTRINSIC_NE_OS:
3802 case INTRINSIC_GT:
3803 case INTRINSIC_GT_OS:
3804 case INTRINSIC_GE:
3805 case INTRINSIC_GE_OS:
3806 case INTRINSIC_LT:
3807 case INTRINSIC_LT_OS:
3808 case INTRINSIC_LE:
3809 case INTRINSIC_LE_OS:
3810 mio_expr (&e->value.op.op1);
3811 mio_expr (&e->value.op.op2);
3812 break;
3813
3814 case INTRINSIC_USER:
3815 /* INTRINSIC_USER should not appear in resolved expressions,
3816 though for UDRs we need to stream unresolved ones. */
3817 if (iomode == IO_OUTPUT)
3818 write_atom (ATOM_STRING, e->value.op.uop->name);
3819 else
3820 {
3821 char *name = read_string ();
3822 const char *uop_name = find_use_name (name, true);
3823 if (uop_name == NULL)
3824 {
3825 size_t len = strlen (name);
3826 char *name2 = XCNEWVEC (char, len + 2);
3827 memcpy (name2, name, len);
3828 name2[len] = ' ';
3829 name2[len + 1] = '\0';
3830 free (name);
3831 uop_name = name = name2;
3832 }
3833 e->value.op.uop = gfc_get_uop (uop_name);
3834 free (name);
3835 }
3836 mio_expr (&e->value.op.op1);
3837 mio_expr (&e->value.op.op2);
3838 break;
3839
3840 default:
3841 bad_module ("Bad operator");
3842 }
3843
3844 break;
3845
3846 case EXPR_FUNCTION:
3847 mio_symtree_ref (&e->symtree);
3848 mio_actual_arglist (&e->value.function.actual, false);
3849
3850 if (iomode == IO_OUTPUT)
3851 {
3852 e->value.function.name
3853 = mio_allocated_string (e->value.function.name);
3854 if (e->value.function.esym)
3855 flag = 1;
3856 else if (e->ref)
3857 flag = 2;
3858 else if (e->value.function.isym == NULL)
3859 flag = 3;
3860 else
3861 flag = 0;
3862 mio_integer (&flag);
3863 switch (flag)
3864 {
3865 case 1:
3866 mio_symbol_ref (&e->value.function.esym);
3867 break;
3868 case 2:
3869 mio_ref_list (&e->ref);
3870 break;
3871 case 3:
3872 break;
3873 default:
3874 write_atom (ATOM_STRING, e->value.function.isym->name);
3875 }
3876 }
3877 else
3878 {
3879 require_atom (ATOM_STRING);
3880 if (atom_string[0] == '\0')
3881 e->value.function.name = NULL;
3882 else
3883 e->value.function.name = gfc_get_string ("%s", atom_string);
3884 free (atom_string);
3885
3886 mio_integer (&flag);
3887 switch (flag)
3888 {
3889 case 1:
3890 mio_symbol_ref (&e->value.function.esym);
3891 break;
3892 case 2:
3893 mio_ref_list (&e->ref);
3894 break;
3895 case 3:
3896 break;
3897 default:
3898 require_atom (ATOM_STRING);
3899 e->value.function.isym = gfc_find_function (atom_string);
3900 free (atom_string);
3901 }
3902 }
3903
3904 break;
3905
3906 case EXPR_VARIABLE:
3907 mio_symtree_ref (&e->symtree);
3908 mio_ref_list (&e->ref);
3909 break;
3910
3911 case EXPR_SUBSTRING:
3912 e->value.character.string
3913 = CONST_CAST (gfc_char_t *,
3914 mio_allocated_wide_string (e->value.character.string,
3915 e->value.character.length));
3916 mio_ref_list (&e->ref);
3917 break;
3918
3919 case EXPR_STRUCTURE:
3920 case EXPR_ARRAY:
3921 mio_constructor (&e->value.constructor);
3922 mio_shape (&e->shape, e->rank);
3923 break;
3924
3925 case EXPR_CONSTANT:
3926 switch (e->ts.type)
3927 {
3928 case BT_INTEGER:
3929 case BT_UNSIGNED:
3930 mio_gmp_integer (&e->value.integer);
3931 break;
3932
3933 case BT_REAL:
3934 gfc_set_model_kind (e->ts.kind);
3935 mio_gmp_real (&e->value.real);
3936 break;
3937
3938 case BT_COMPLEX:
3939 gfc_set_model_kind (e->ts.kind);
3940 mio_gmp_real (&mpc_realref (e->value.complex));
3941 mio_gmp_real (&mpc_imagref (e->value.complex));
3942 break;
3943
3944 case BT_LOGICAL:
3945 mio_integer (&e->value.logical);
3946 break;
3947
3948 case BT_CHARACTER:
3949 hwi = e->value.character.length;
3950 mio_hwi (&hwi);
3951 e->value.character.length = hwi;
3952 e->value.character.string
3953 = CONST_CAST (gfc_char_t *,
3954 mio_allocated_wide_string (e->value.character.string,
3955 e->value.character.length));
3956 break;
3957
3958 default:
3959 bad_module ("Bad type in constant expression");
3960 }
3961
3962 break;
3963
3964 case EXPR_NULL:
3965 break;
3966
3967 case EXPR_COMPCALL:
3968 case EXPR_PPC:
3969 case EXPR_UNKNOWN:
3970 gcc_unreachable ();
3971 break;
3972 }
3973
3974 /* PDT types store the expression specification list here. */
3975 mio_actual_arglist (&e->param_list, true);
3976
3977 mio_rparen ();
3978 }
3979
3980
3981 /* Read and write namelists. */
3982
3983 static void
3984 mio_namelist (gfc_symbol *sym)
3985 {
3986 gfc_namelist *n, *m;
3987
3988 mio_lparen ();
3989
3990 if (iomode == IO_OUTPUT)
3991 {
3992 for (n = sym->namelist; n; n = n->next)
3993 mio_symbol_ref (&n->sym);
3994 }
3995 else
3996 {
3997 m = NULL;
3998 while (peek_atom () != ATOM_RPAREN)
3999 {
4000 n = gfc_get_namelist ();
4001 mio_symbol_ref (&n->sym);
4002
4003 if (sym->namelist == NULL)
4004 sym->namelist = n;
4005 else
4006 m->next = n;
4007
4008 m = n;
4009 }
4010 sym->namelist_tail = m;
4011 }
4012
4013 mio_rparen ();
4014 }
4015
4016
4017 /* Save/restore lists of gfc_interface structures. When loading an
4018 interface, we are really appending to the existing list of
4019 interfaces. Checking for duplicate and ambiguous interfaces has to
4020 be done later when all symbols have been loaded. */
4021
4022 pointer_info *
4023 mio_interface_rest (gfc_interface **ip)
4024 {
4025 gfc_interface *tail, *p;
4026 pointer_info *pi = NULL;
4027
4028 if (iomode == IO_OUTPUT)
4029 {
4030 if (ip != NULL)
4031 for (p = *ip; p; p = p->next)
4032 mio_symbol_ref (&p->sym);
4033 }
4034 else
4035 {
4036 if (*ip == NULL)
4037 tail = NULL;
4038 else
4039 {
4040 tail = *ip;
4041 while (tail->next)
4042 tail = tail->next;
4043 }
4044
4045 for (;;)
4046 {
4047 if (peek_atom () == ATOM_RPAREN)
4048 break;
4049
4050 p = gfc_get_interface ();
4051 p->where = gfc_current_locus;
4052 pi = mio_symbol_ref (&p->sym);
4053
4054 if (tail == NULL)
4055 *ip = p;
4056 else
4057 tail->next = p;
4058
4059 tail = p;
4060 }
4061 }
4062
4063 mio_rparen ();
4064 return pi;
4065 }
4066
4067
4068 /* Save/restore a nameless operator interface. */
4069
4070 static void
4071 mio_interface (gfc_interface **ip)
4072 {
4073 mio_lparen ();
4074 mio_interface_rest (ip);
4075 }
4076
4077
4078 /* Save/restore a named operator interface. */
4079
4080 static void
4081 mio_symbol_interface (const char **name, const char **module,
4082 gfc_interface **ip)
4083 {
4084 mio_lparen ();
4085 mio_pool_string (name);
4086 mio_pool_string (module);
4087 mio_interface_rest (ip);
4088 }
4089
4090
4091 static void
4092 mio_namespace_ref (gfc_namespace **nsp)
4093 {
4094 gfc_namespace *ns;
4095 pointer_info *p;
4096
4097 p = mio_pointer_ref (nsp);
4098
4099 if (p->type == P_UNKNOWN)
4100 p->type = P_NAMESPACE;
4101
4102 if (iomode == IO_INPUT && p->integer != 0)
4103 {
4104 ns = (gfc_namespace *) p->u.pointer;
4105 if (ns == NULL)
4106 {
4107 ns = gfc_get_namespace (NULL, 0);
4108 associate_integer_pointer (p, ns);
4109 }
4110 else
4111 ns->refs++;
4112 }
4113 }
4114
4115
4116 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
4117
4118 static gfc_namespace* current_f2k_derived;
4119
4120 static void
4121 mio_typebound_proc (gfc_typebound_proc** proc)
4122 {
4123 int flag;
4124 int overriding_flag;
4125
4126 if (iomode == IO_INPUT)
4127 {
4128 *proc = gfc_get_typebound_proc (NULL);
4129 (*proc)->where = gfc_current_locus;
4130 }
4131 gcc_assert (*proc);
4132
4133 mio_lparen ();
4134
4135 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
4136
4137 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
4138 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4139 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
4140 overriding_flag = mio_name (overriding_flag, binding_overriding);
4141 (*proc)->deferred = ((overriding_flag & 2) != 0);
4142 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
4143 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
4144
4145 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
4146 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
4147 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
4148
4149 mio_pool_string (&((*proc)->pass_arg));
4150
4151 flag = (int) (*proc)->pass_arg_num;
4152 mio_integer (&flag);
4153 (*proc)->pass_arg_num = (unsigned) flag;
4154
4155 if ((*proc)->is_generic)
4156 {
4157 gfc_tbp_generic* g;
4158 int iop;
4159
4160 mio_lparen ();
4161
4162 if (iomode == IO_OUTPUT)
4163 for (g = (*proc)->u.generic; g; g = g->next)
4164 {
4165 iop = (int) g->is_operator;
4166 mio_integer (&iop);
4167 mio_allocated_string (g->specific_st->name);
4168 }
4169 else
4170 {
4171 (*proc)->u.generic = NULL;
4172 while (peek_atom () != ATOM_RPAREN)
4173 {
4174 gfc_symtree** sym_root;
4175
4176 g = gfc_get_tbp_generic ();
4177 g->specific = NULL;
4178
4179 mio_integer (&iop);
4180 g->is_operator = (bool) iop;
4181
4182 require_atom (ATOM_STRING);
4183 sym_root = &current_f2k_derived->tb_sym_root;
4184 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
4185 free (atom_string);
4186
4187 g->next = (*proc)->u.generic;
4188 (*proc)->u.generic = g;
4189 }
4190 }
4191
4192 mio_rparen ();
4193 }
4194 else if (!(*proc)->ppc)
4195 mio_symtree_ref (&(*proc)->u.specific);
4196
4197 mio_rparen ();
4198 }
4199
4200 /* Walker-callback function for this purpose. */
4201 static void
4202 mio_typebound_symtree (gfc_symtree* st)
4203 {
4204 if (iomode == IO_OUTPUT && !st->n.tb)
4205 return;
4206
4207 if (iomode == IO_OUTPUT)
4208 {
4209 mio_lparen ();
4210 mio_allocated_string (st->name);
4211 }
4212 /* For IO_INPUT, the above is done in mio_f2k_derived. */
4213
4214 mio_typebound_proc (&st->n.tb);
4215 mio_rparen ();
4216 }
4217
4218 /* IO a full symtree (in all depth). */
4219 static void
4220 mio_full_typebound_tree (gfc_symtree** root)
4221 {
4222 mio_lparen ();
4223
4224 if (iomode == IO_OUTPUT)
4225 gfc_traverse_symtree (*root, &mio_typebound_symtree);
4226 else
4227 {
4228 while (peek_atom () == ATOM_LPAREN)
4229 {
4230 gfc_symtree* st;
4231
4232 mio_lparen ();
4233
4234 require_atom (ATOM_STRING);
4235 st = gfc_get_tbp_symtree (root, atom_string);
4236 free (atom_string);
4237
4238 mio_typebound_symtree (st);
4239 }
4240 }
4241
4242 mio_rparen ();
4243 }
4244
4245 static void
4246 mio_finalizer (gfc_finalizer **f)
4247 {
4248 if (iomode == IO_OUTPUT)
4249 {
4250 gcc_assert (*f);
4251 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
4252 mio_symtree_ref (&(*f)->proc_tree);
4253 }
4254 else
4255 {
4256 *f = gfc_get_finalizer ();
4257 (*f)->where = gfc_current_locus; /* Value should not matter. */
4258 (*f)->next = NULL;
4259
4260 mio_symtree_ref (&(*f)->proc_tree);
4261 (*f)->proc_sym = NULL;
4262 }
4263 }
4264
4265 static void
4266 mio_f2k_derived (gfc_namespace *f2k)
4267 {
4268 current_f2k_derived = f2k;
4269
4270 /* Handle the list of finalizer procedures. */
4271 mio_lparen ();
4272 if (iomode == IO_OUTPUT)
4273 {
4274 gfc_finalizer *f;
4275 for (f = f2k->finalizers; f; f = f->next)
4276 mio_finalizer (&f);
4277 }
4278 else
4279 {
4280 f2k->finalizers = NULL;
4281 while (peek_atom () != ATOM_RPAREN)
4282 {
4283 gfc_finalizer *cur = NULL;
4284 mio_finalizer (&cur);
4285 cur->next = f2k->finalizers;
4286 f2k->finalizers = cur;
4287 }
4288 }
4289 mio_rparen ();
4290
4291 /* Handle type-bound procedures. */
4292 mio_full_typebound_tree (&f2k->tb_sym_root);
4293
4294 /* Type-bound user operators. */
4295 mio_full_typebound_tree (&f2k->tb_uop_root);
4296
4297 /* Type-bound intrinsic operators. */
4298 mio_lparen ();
4299 if (iomode == IO_OUTPUT)
4300 {
4301 int op;
4302 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
4303 {
4304 gfc_intrinsic_op realop;
4305
4306 if (op == INTRINSIC_USER || !f2k->tb_op[op])
4307 continue;
4308
4309 mio_lparen ();
4310 realop = (gfc_intrinsic_op) op;
4311 mio_intrinsic_op (&realop);
4312 mio_typebound_proc (&f2k->tb_op[op]);
4313 mio_rparen ();
4314 }
4315 }
4316 else
4317 while (peek_atom () != ATOM_RPAREN)
4318 {
4319 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
4320
4321 mio_lparen ();
4322 mio_intrinsic_op (&op);
4323 mio_typebound_proc (&f2k->tb_op[op]);
4324 mio_rparen ();
4325 }
4326 mio_rparen ();
4327 }
4328
4329 static void
4330 mio_full_f2k_derived (gfc_symbol *sym)
4331 {
4332 mio_lparen ();
4333
4334 if (iomode == IO_OUTPUT)
4335 {
4336 if (sym->f2k_derived)
4337 mio_f2k_derived (sym->f2k_derived);
4338 }
4339 else
4340 {
4341 if (peek_atom () != ATOM_RPAREN)
4342 {
4343 gfc_namespace *ns;
4344
4345 sym->f2k_derived = gfc_get_namespace (NULL, 0);
4346
4347 /* PDT templates make use of the mechanisms for formal args
4348 and so the parameter symbols are stored in the formal
4349 namespace. Transfer the sym_root to f2k_derived and then
4350 free the formal namespace since it is uneeded. */
4351 if (sym->attr.pdt_template && sym->formal && sym->formal->sym)
4352 {
4353 ns = sym->formal->sym->ns;
4354 sym->f2k_derived->sym_root = ns->sym_root;
4355 ns->sym_root = NULL;
4356 ns->refs++;
4357 gfc_free_namespace (ns);
4358 ns = NULL;
4359 }
4360
4361 mio_f2k_derived (sym->f2k_derived);
4362 }
4363 else
4364 gcc_assert (!sym->f2k_derived);
4365 }
4366
4367 mio_rparen ();
4368 }
4369
4370 static const mstring omp_declare_simd_clauses[] =
4371 {
4372 minit ("INBRANCH", 0),
4373 minit ("NOTINBRANCH", 1),
4374 minit ("SIMDLEN", 2),
4375 minit ("UNIFORM", 3),
4376 minit ("LINEAR", 4),
4377 minit ("ALIGNED", 5),
4378 minit ("LINEAR_REF", 33),
4379 minit ("LINEAR_VAL", 34),
4380 minit ("LINEAR_UVAL", 35),
4381 minit (NULL, -1)
4382 };
4383
4384 /* Handle OpenMP's declare-simd clauses. */
4385
4386 static void
4387 mio_omp_declare_simd_clauses (gfc_omp_clauses **clausesp)
4388 {
4389 if (iomode == IO_OUTPUT)
4390 {
4391 gfc_omp_clauses *clauses = *clausesp;
4392 gfc_omp_namelist *n;
4393
4394 write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
4395 if (clauses->inbranch)
4396 mio_name (0, omp_declare_simd_clauses);
4397 if (clauses->notinbranch)
4398 mio_name (1, omp_declare_simd_clauses);
4399 if (clauses->simdlen_expr)
4400 {
4401 mio_name (2, omp_declare_simd_clauses);
4402 mio_expr (&clauses->simdlen_expr);
4403 }
4404 for (n = clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
4405 {
4406 mio_name (3, omp_declare_simd_clauses);
4407 mio_symbol_ref (&n->sym);
4408 }
4409 for (n = clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
4410 {
4411 if (n->u.linear.op == OMP_LINEAR_DEFAULT)
4412 mio_name (4, omp_declare_simd_clauses);
4413 else
4414 mio_name (32 + n->u.linear.op, omp_declare_simd_clauses);
4415 mio_symbol_ref (&n->sym);
4416 mio_expr (&n->expr);
4417 }
4418 for (n = clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
4419 {
4420 mio_name (5, omp_declare_simd_clauses);
4421 mio_symbol_ref (&n->sym);
4422 mio_expr (&n->expr);
4423 }
4424 }
4425 else
4426 {
4427 if (peek_atom () != ATOM_NAME)
4428 return;
4429
4430 gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
4431 gfc_omp_clauses *clauses = *clausesp = gfc_get_omp_clauses ();
4432 ptrs[0] = &clauses->lists[OMP_LIST_UNIFORM];
4433 ptrs[1] = &clauses->lists[OMP_LIST_LINEAR];
4434 ptrs[2] = &clauses->lists[OMP_LIST_ALIGNED];
4435
4436 while (peek_atom () == ATOM_NAME)
4437 {
4438 gfc_omp_namelist *n;
4439 int t = mio_name (0, omp_declare_simd_clauses);
4440
4441 switch (t)
4442 {
4443 case 0: clauses->inbranch = true; break;
4444 case 1: clauses->notinbranch = true; break;
4445 case 2: mio_expr (&clauses->simdlen_expr); break;
4446 case 3:
4447 case 4:
4448 case 5:
4449 *ptrs[t - 3] = n = gfc_get_omp_namelist ();
4450 finish_namelist:
4451 n->where = gfc_current_locus;
4452 ptrs[t - 3] = &n->next;
4453 mio_symbol_ref (&n->sym);
4454 if (t != 3)
4455 mio_expr (&n->expr);
4456 break;
4457 case 33:
4458 case 34:
4459 case 35:
4460 *ptrs[1] = n = gfc_get_omp_namelist ();
4461 n->u.linear.op = (enum gfc_omp_linear_op) (t - 32);
4462 t = 4;
4463 goto finish_namelist;
4464 }
4465 }
4466 }
4467 }
4468
4469
4470 /* Handle !$omp declare simd. */
4471
4472 static void
4473 mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
4474 {
4475 if (iomode == IO_OUTPUT)
4476 {
4477 if (*odsp == NULL)
4478 {
4479 if (ns->omp_declare_variant)
4480 {
4481 mio_lparen ();
4482 mio_rparen ();
4483 }
4484 return;
4485 }
4486 }
4487 else if (peek_atom () != ATOM_LPAREN)
4488 return;
4489
4490 gfc_omp_declare_simd *ods = *odsp;
4491
4492 mio_lparen ();
4493 if (iomode == IO_OUTPUT)
4494 {
4495 if (ods->clauses)
4496 mio_omp_declare_simd_clauses (&ods->clauses);
4497 }
4498 else
4499 {
4500 if (peek_atom () == ATOM_RPAREN)
4501 {
4502 mio_rparen ();
4503 return;
4504 }
4505
4506 require_atom (ATOM_NAME);
4507 *odsp = ods = gfc_get_omp_declare_simd ();
4508 ods->where = gfc_current_locus;
4509 ods->proc_name = ns->proc_name;
4510 mio_omp_declare_simd_clauses (&ods->clauses);
4511 }
4512
4513 mio_omp_declare_simd (ns, &ods->next);
4514
4515 mio_rparen ();
4516 }
4517
4518 /* Handle !$omp declare variant. */
4519
4520 static void
4521 mio_omp_declare_variant (gfc_namespace *ns, gfc_omp_declare_variant **odvp)
4522 {
4523 if (iomode == IO_OUTPUT)
4524 {
4525 if (*odvp == NULL)
4526 return;
4527 }
4528 else if (peek_atom () != ATOM_LPAREN)
4529 return;
4530
4531 gfc_omp_declare_variant *odv;
4532
4533 mio_lparen ();
4534 if (iomode == IO_OUTPUT)
4535 {
4536 odv = *odvp;
4537 write_atom (ATOM_NAME, "OMP_DECLARE_VARIANT");
4538 gfc_symtree *st;
4539 st = (odv->base_proc_symtree
4540 ? odv->base_proc_symtree
4541 : gfc_find_symtree (ns->sym_root, ns->proc_name->name));
4542 mio_symtree_ref (&st);
4543 st = (st->n.sym->attr.if_source == IFSRC_IFBODY
4544 && st->n.sym->formal_ns == ns
4545 ? gfc_find_symtree (ns->parent->sym_root,
4546 odv->variant_proc_symtree->name)
4547 : odv->variant_proc_symtree);
4548 mio_symtree_ref (&st);
4549
4550 mio_lparen ();
4551 write_atom (ATOM_NAME, "SEL");
4552 for (gfc_omp_set_selector *set = odv->set_selectors; set; set = set->next)
4553 {
4554 int set_code = set->code;
4555 mio_integer (&set_code);
4556 mio_lparen ();
4557 for (gfc_omp_selector *sel = set->trait_selectors; sel;
4558 sel = sel->next)
4559 {
4560 int sel_code = sel->code;
4561 mio_integer (&sel_code);
4562 mio_expr (&sel->score);
4563 mio_lparen ();
4564 for (gfc_omp_trait_property *prop = sel->properties; prop;
4565 prop = prop->next)
4566 {
4567 int kind = prop->property_kind;
4568 mio_integer (&kind);
4569 int is_name = prop->is_name;
4570 mio_integer (&is_name);
4571 switch (prop->property_kind)
4572 {
4573 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
4574 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
4575 mio_expr (&prop->expr);
4576 break;
4577 case OMP_TRAIT_PROPERTY_ID:
4578 write_atom (ATOM_STRING, prop->name);
4579 break;
4580 case OMP_TRAIT_PROPERTY_NAME_LIST:
4581 if (prop->is_name)
4582 write_atom (ATOM_STRING, prop->name);
4583 else
4584 mio_expr (&prop->expr);
4585 break;
4586 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
4587 {
4588 /* Currently only declare simd. */
4589 mio_lparen ();
4590 mio_omp_declare_simd_clauses (&prop->clauses);
4591 mio_rparen ();
4592 }
4593 break;
4594 default:
4595 gcc_unreachable ();
4596 }
4597 }
4598 mio_rparen ();
4599 }
4600 mio_rparen ();
4601 }
4602 mio_rparen ();
4603
4604 mio_lparen ();
4605 write_atom (ATOM_NAME, "ADJ");
4606 for (gfc_omp_namelist *arg = odv->adjust_args_list; arg; arg = arg->next)
4607 {
4608 int need_ptr = arg->u.adj_args.need_ptr;
4609 int need_addr = arg->u.adj_args.need_addr;
4610 int range_start = arg->u.adj_args.range_start;
4611 int omp_num_args_plus = arg->u.adj_args.omp_num_args_plus;
4612 int omp_num_args_minus = arg->u.adj_args.omp_num_args_minus;
4613 mio_integer (&need_ptr);
4614 mio_integer (&need_addr);
4615 mio_integer (&range_start);
4616 mio_integer (&omp_num_args_plus);
4617 mio_integer (&omp_num_args_minus);
4618 mio_expr (&arg->expr);
4619 }
4620 mio_rparen ();
4621
4622 mio_lparen ();
4623 write_atom (ATOM_NAME, "APP");
4624 for (gfc_omp_namelist *arg = odv->append_args_list; arg; arg = arg->next)
4625 {
4626 int target = arg->u.init.target;
4627 int targetsync = arg->u.init.targetsync;
4628 mio_integer (&target);
4629 mio_integer (&targetsync);
4630 mio_integer (&arg->u.init.len);
4631 gfc_char_t *p = XALLOCAVEC (gfc_char_t, arg->u.init.len);
4632 for (int i = 0; i < arg->u.init.len; i++)
4633 p[i] = arg->u2.init_interop[i];
4634 mio_allocated_wide_string (p, arg->u.init.len);
4635 }
4636 mio_rparen ();
4637 }
4638 else
4639 {
4640 if (peek_atom () == ATOM_RPAREN)
4641 {
4642 mio_rparen ();
4643 return;
4644 }
4645
4646 require_atom (ATOM_NAME);
4647 odv = *odvp = gfc_get_omp_declare_variant ();
4648 odv->where = gfc_current_locus;
4649
4650 mio_symtree_ref (&odv->base_proc_symtree);
4651 mio_symtree_ref (&odv->variant_proc_symtree);
4652
4653 mio_lparen ();
4654 require_atom (ATOM_NAME); /* SEL */
4655 gfc_omp_set_selector **set = &odv->set_selectors;
4656 while (peek_atom () != ATOM_RPAREN)
4657 {
4658 *set = gfc_get_omp_set_selector ();
4659 int set_code;
4660 mio_integer (&set_code);
4661 (*set)->code = (enum omp_tss_code) set_code;
4662
4663 mio_lparen ();
4664 gfc_omp_selector **sel = &(*set)->trait_selectors;
4665 while (peek_atom () != ATOM_RPAREN)
4666 {
4667 *sel = gfc_get_omp_selector ();
4668 int sel_code = 0;
4669 mio_integer (&sel_code);
4670 (*sel)->code = (enum omp_ts_code) sel_code;
4671 mio_expr (&(*sel)->score);
4672
4673 mio_lparen ();
4674 gfc_omp_trait_property **prop = &(*sel)->properties;
4675 while (peek_atom () != ATOM_RPAREN)
4676 {
4677 *prop = gfc_get_omp_trait_property ();
4678 int kind = 0, is_name = 0;
4679 mio_integer (&kind);
4680 mio_integer (&is_name);
4681 (*prop)->property_kind = (enum omp_tp_type) kind;
4682 (*prop)->is_name = is_name;
4683 switch ((*prop)->property_kind)
4684 {
4685 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
4686 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
4687 mio_expr (&(*prop)->expr);
4688 break;
4689 case OMP_TRAIT_PROPERTY_ID:
4690 (*prop)->name = read_string ();
4691 break;
4692 case OMP_TRAIT_PROPERTY_NAME_LIST:
4693 if ((*prop)->is_name)
4694 (*prop)->name = read_string ();
4695 else
4696 mio_expr (&(*prop)->expr);
4697 break;
4698 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
4699 {
4700 /* Currently only declare simd. */
4701 mio_lparen ();
4702 mio_omp_declare_simd_clauses (&(*prop)->clauses);
4703 mio_rparen ();
4704 }
4705 break;
4706 default:
4707 gcc_unreachable ();
4708 }
4709 prop = &(*prop)->next;
4710 }
4711 mio_rparen ();
4712 sel = &(*sel)->next;
4713 }
4714 mio_rparen ();
4715 set = &(*set)->next;
4716 }
4717 mio_rparen ();
4718
4719 mio_lparen ();
4720 require_atom (ATOM_NAME); /* ADJ */
4721 gfc_omp_namelist **nl = &odv->adjust_args_list;
4722 while (peek_atom () != ATOM_RPAREN)
4723 {
4724 *nl = gfc_get_omp_namelist ();
4725 (*nl)->where = gfc_current_locus;
4726 int need_ptr, need_addr, range_start;
4727 int omp_num_args_plus, omp_num_args_minus;
4728 mio_integer (&need_ptr);
4729 mio_integer (&need_addr);
4730 mio_integer (&range_start);
4731 mio_integer (&omp_num_args_plus);
4732 mio_integer (&omp_num_args_minus);
4733 (*nl)->u.adj_args.need_ptr = need_ptr;
4734 (*nl)->u.adj_args.need_addr = need_addr;
4735 (*nl)->u.adj_args.range_start = range_start;
4736 (*nl)->u.adj_args.omp_num_args_plus = omp_num_args_minus;
4737 (*nl)->u.adj_args.omp_num_args_plus = omp_num_args_minus;
4738 mio_expr (&(*nl)->expr);
4739 nl = &(*nl)->next;
4740 }
4741 mio_rparen ();
4742
4743 mio_lparen ();
4744 require_atom (ATOM_NAME); /* APP */
4745 nl = &odv->append_args_list;
4746 while (peek_atom () != ATOM_RPAREN)
4747 {
4748 *nl = gfc_get_omp_namelist ();
4749 (*nl)->where = gfc_current_locus;
4750 int target, targetsync;
4751 mio_integer (&target);
4752 mio_integer (&targetsync);
4753 mio_integer (&(*nl)->u.init.len);
4754 (*nl)->u.init.target = target;
4755 (*nl)->u.init.targetsync = targetsync;
4756 const gfc_char_t *p = XALLOCAVEC (gfc_char_t, (*nl)->u.init.len); // FIXME: memory handling?
4757 (*nl)->u2.init_interop = XCNEWVEC (char, (*nl)->u.init.len);
4758 p = mio_allocated_wide_string (NULL, (*nl)->u.init.len);
4759 for (int i = 0; i < (*nl)->u.init.len; i++)
4760 (*nl)->u2.init_interop[i] = p[i];
4761 nl = &(*nl)->next;
4762 }
4763 mio_rparen ();
4764 }
4765
4766 mio_omp_declare_variant (ns, &odv->next);
4767
4768 mio_rparen ();
4769 }
4770
4771 static const mstring omp_declare_reduction_stmt[] =
4772 {
4773 minit ("ASSIGN", 0),
4774 minit ("CALL", 1),
4775 minit (NULL, -1)
4776 };
4777
4778
4779 static void
4780 mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
4781 gfc_namespace *ns, bool is_initializer)
4782 {
4783 if (iomode == IO_OUTPUT)
4784 {
4785 if ((*sym1)->module == NULL)
4786 {
4787 (*sym1)->module = module_name;
4788 (*sym2)->module = module_name;
4789 }
4790 mio_symbol_ref (sym1);
4791 mio_symbol_ref (sym2);
4792 if (ns->code->op == EXEC_ASSIGN)
4793 {
4794 mio_name (0, omp_declare_reduction_stmt);
4795 mio_expr (&ns->code->expr1);
4796 mio_expr (&ns->code->expr2);
4797 }
4798 else
4799 {
4800 int flag;
4801 mio_name (1, omp_declare_reduction_stmt);
4802 mio_symtree_ref (&ns->code->symtree);
4803 mio_actual_arglist (&ns->code->ext.actual, false);
4804
4805 flag = ns->code->resolved_isym != NULL;
4806 mio_integer (&flag);
4807 if (flag)
4808 write_atom (ATOM_STRING, ns->code->resolved_isym->name);
4809 else
4810 mio_symbol_ref (&ns->code->resolved_sym);
4811 }
4812 }
4813 else
4814 {
4815 pointer_info *p1 = mio_symbol_ref (sym1);
4816 pointer_info *p2 = mio_symbol_ref (sym2);
4817 gfc_symbol *sym;
4818 gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
4819 gcc_assert (p1->u.rsym.sym == NULL);
4820 /* Add hidden symbols to the symtree. */
4821 pointer_info *q = get_integer (p1->u.rsym.ns);
4822 q->u.pointer = (void *) ns;
4823 sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
4824 sym->ts = udr->ts;
4825 sym->module = gfc_get_string ("%s", p1->u.rsym.module);
4826 associate_integer_pointer (p1, sym);
4827 sym->attr.omp_udr_artificial_var = 1;
4828 gcc_assert (p2->u.rsym.sym == NULL);
4829 sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
4830 sym->ts = udr->ts;
4831 sym->module = gfc_get_string ("%s", p2->u.rsym.module);
4832 associate_integer_pointer (p2, sym);
4833 sym->attr.omp_udr_artificial_var = 1;
4834 if (mio_name (0, omp_declare_reduction_stmt) == 0)
4835 {
4836 ns->code = gfc_get_code (EXEC_ASSIGN);
4837 mio_expr (&ns->code->expr1);
4838 mio_expr (&ns->code->expr2);
4839 }
4840 else
4841 {
4842 int flag;
4843 ns->code = gfc_get_code (EXEC_CALL);
4844 mio_symtree_ref (&ns->code->symtree);
4845 mio_actual_arglist (&ns->code->ext.actual, false);
4846
4847 mio_integer (&flag);
4848 if (flag)
4849 {
4850 require_atom (ATOM_STRING);
4851 ns->code->resolved_isym = gfc_find_subroutine (atom_string);
4852 free (atom_string);
4853 }
4854 else
4855 mio_symbol_ref (&ns->code->resolved_sym);
4856 }
4857 ns->code->loc = gfc_current_locus;
4858 ns->omp_udr_ns = 1;
4859 }
4860 }
4861
4862
4863 /* Unlike most other routines, the address of the symbol node is already
4864 fixed on input and the name/module has already been filled in.
4865 If you update the symbol format here, don't forget to update read_module
4866 as well (look for "seek to the symbol's component list"). */
4867
4868 static void
4869 mio_symbol (gfc_symbol *sym)
4870 {
4871 int intmod = INTMOD_NONE;
4872
4873 mio_lparen ();
4874
4875 mio_symbol_attribute (&sym->attr);
4876
4877 if (sym->attr.pdt_type)
4878 sym->name = gfc_dt_upper_string (sym->name);
4879
4880 /* Note that components are always saved, even if they are supposed
4881 to be private. Component access is checked during searching. */
4882 mio_component_list (&sym->components, sym->attr.vtype);
4883 if (sym->components != NULL)
4884 sym->component_access
4885 = MIO_NAME (gfc_access) (sym->component_access, access_types);
4886
4887 mio_typespec (&sym->ts);
4888 if (sym->ts.type == BT_CLASS)
4889 sym->attr.class_ok = 1;
4890
4891 if (iomode == IO_OUTPUT)
4892 mio_namespace_ref (&sym->formal_ns);
4893 else
4894 {
4895 mio_namespace_ref (&sym->formal_ns);
4896 if (sym->formal_ns)
4897 sym->formal_ns->proc_name = sym;
4898 }
4899
4900 /* Save/restore common block links. */
4901 mio_symbol_ref (&sym->common_next);
4902
4903 mio_formal_arglist (&sym->formal);
4904
4905 if (sym->attr.flavor == FL_PARAMETER)
4906 mio_expr (&sym->value);
4907
4908 mio_array_spec (&sym->as);
4909
4910 mio_symbol_ref (&sym->result);
4911
4912 if (sym->attr.cray_pointee)
4913 mio_symbol_ref (&sym->cp_pointer);
4914
4915 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4916 mio_full_f2k_derived (sym);
4917
4918 /* PDT types store the symbol specification list here. */
4919 mio_actual_arglist (&sym->param_list, true);
4920
4921 mio_namelist (sym);
4922
4923 /* Add the fields that say whether this is from an intrinsic module,
4924 and if so, what symbol it is within the module. */
4925 /* mio_integer (&(sym->from_intmod)); */
4926 if (iomode == IO_OUTPUT)
4927 {
4928 intmod = sym->from_intmod;
4929 mio_integer (&intmod);
4930 }
4931 else
4932 {
4933 mio_integer (&intmod);
4934 if (current_intmod)
4935 sym->from_intmod = current_intmod;
4936 else
4937 sym->from_intmod = (intmod_id) intmod;
4938 }
4939
4940 mio_integer (&(sym->intmod_sym_id));
4941
4942 if (gfc_fl_struct (sym->attr.flavor))
4943 mio_integer (&(sym->hash_value));
4944
4945 if (sym->formal_ns
4946 && sym->formal_ns->proc_name == sym
4947 && sym->formal_ns->entries == NULL)
4948 {
4949 mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
4950 mio_omp_declare_variant (sym->formal_ns,
4951 &sym->formal_ns->omp_declare_variant);
4952 }
4953 else if ((iomode == IO_OUTPUT && sym->ns->proc_name == sym)
4954 || (iomode == IO_INPUT && peek_atom () == ATOM_LPAREN))
4955 mio_omp_declare_variant (sym->ns, &sym->ns->omp_declare_variant);
4956
4957 mio_rparen ();
4958 }
4959
4960
4961 /************************* Top level subroutines *************************/
4962
4963 /* A recursive function to look for a specific symbol by name and by
4964 module. Whilst several symtrees might point to one symbol, its
4965 is sufficient for the purposes here than one exist. Note that
4966 generic interfaces are distinguished as are symbols that have been
4967 renamed in another module. */
4968 static gfc_symtree *
4969 find_symbol (gfc_symtree *st, const char *name,
4970 const char *module, int generic)
4971 {
4972 int c;
4973 gfc_symtree *retval, *s;
4974
4975 if (st == NULL || st->n.sym == NULL)
4976 return NULL;
4977
4978 c = strcmp (name, st->n.sym->name);
4979 if (c == 0 && st->n.sym->module
4980 && strcmp (module, st->n.sym->module) == 0
4981 && !check_unique_name (st->name))
4982 {
4983 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
4984
4985 /* Detect symbols that are renamed by use association in another
4986 module by the absence of a symtree and null attr.use_rename,
4987 since the latter is not transmitted in the module file. */
4988 if (((!generic && !st->n.sym->attr.generic)
4989 || (generic && st->n.sym->attr.generic))
4990 && !(s == NULL && !st->n.sym->attr.use_rename))
4991 return st;
4992 }
4993
4994 retval = find_symbol (st->left, name, module, generic);
4995
4996 if (retval == NULL)
4997 retval = find_symbol (st->right, name, module, generic);
4998
4999 return retval;
5000 }
5001
5002
5003 /* Skip a list between balanced left and right parens.
5004 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
5005 have been already parsed by hand, and the remaining of the content is to be
5006 skipped here. The default value is 0 (balanced parens). */
5007
5008 static void
5009 skip_list (int nest_level = 0)
5010 {
5011 int level;
5012
5013 level = nest_level;
5014 do
5015 {
5016 switch (parse_atom ())
5017 {
5018 case ATOM_LPAREN:
5019 level++;
5020 break;
5021
5022 case ATOM_RPAREN:
5023 level--;
5024 break;
5025
5026 case ATOM_STRING:
5027 free (atom_string);
5028 break;
5029
5030 case ATOM_NAME:
5031 case ATOM_INTEGER:
5032 break;
5033 }
5034 }
5035 while (level > 0);
5036 }
5037
5038
5039 /* Load operator interfaces from the module. Interfaces are unusual
5040 in that they attach themselves to existing symbols. */
5041
5042 static void
5043 load_operator_interfaces (void)
5044 {
5045 const char *p;
5046 /* "module" must be large enough for the case of submodules in which the name
5047 has the form module.submodule */
5048 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
5049 gfc_user_op *uop;
5050 pointer_info *pi = NULL;
5051 int n, i;
5052
5053 mio_lparen ();
5054
5055 while (peek_atom () != ATOM_RPAREN)
5056 {
5057 mio_lparen ();
5058
5059 mio_internal_string (name);
5060 mio_internal_string (module);
5061
5062 n = number_use_names (name, true);
5063 n = n ? n : 1;
5064
5065 for (i = 1; i <= n; i++)
5066 {
5067 /* Decide if we need to load this one or not. */
5068 p = find_use_name_n (name, &i, true);
5069
5070 if (p == NULL)
5071 {
5072 while (parse_atom () != ATOM_RPAREN);
5073 continue;
5074 }
5075
5076 if (i == 1)
5077 {
5078 uop = gfc_get_uop (p);
5079 pi = mio_interface_rest (&uop->op);
5080 }
5081 else
5082 {
5083 if (gfc_find_uop (p, NULL))
5084 continue;
5085 uop = gfc_get_uop (p);
5086 uop->op = gfc_get_interface ();
5087 uop->op->where = gfc_current_locus;
5088 add_fixup (pi->integer, &uop->op->sym);
5089 }
5090 }
5091 }
5092
5093 mio_rparen ();
5094 }
5095
5096
5097 /* Load interfaces from the module. Interfaces are unusual in that
5098 they attach themselves to existing symbols. */
5099
5100 static void
5101 load_generic_interfaces (void)
5102 {
5103 const char *p;
5104 /* "module" must be large enough for the case of submodules in which the name
5105 has the form module.submodule */
5106 char name[GFC_MAX_SYMBOL_LEN + 1], module[2 * GFC_MAX_SYMBOL_LEN + 2];
5107 gfc_symbol *sym;
5108 gfc_interface *generic = NULL, *gen = NULL;
5109 int n, i, renamed;
5110 bool ambiguous_set = false;
5111
5112 mio_lparen ();
5113
5114 while (peek_atom () != ATOM_RPAREN)
5115 {
5116 mio_lparen ();
5117
5118 mio_internal_string (name);
5119 mio_internal_string (module);
5120
5121 n = number_use_names (name, false);
5122 renamed = n ? 1 : 0;
5123 n = n ? n : 1;
5124
5125 for (i = 1; i <= n; i++)
5126 {
5127 gfc_symtree *st;
5128 /* Decide if we need to load this one or not. */
5129 p = find_use_name_n (name, &i, false);
5130
5131 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
5132 {
5133 /* Skip the specific names for these cases. */
5134 while (i == 1 && parse_atom () != ATOM_RPAREN);
5135
5136 continue;
5137 }
5138
5139 st = find_symbol (gfc_current_ns->sym_root,
5140 name, module_name, 1);
5141
5142 /* If the symbol exists already and is being USEd without being
5143 in an ONLY clause, do not load a new symtree(11.3.2). */
5144 if (!only_flag && st)
5145 sym = st->n.sym;
5146
5147 if (!sym)
5148 {
5149 if (st)
5150 {
5151 sym = st->n.sym;
5152 if (strcmp (st->name, p) != 0)
5153 {
5154 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
5155 st->n.sym = sym;
5156 sym->refs++;
5157 }
5158 }
5159
5160 /* Since we haven't found a valid generic interface, we had
5161 better make one. */
5162 if (!sym)
5163 {
5164 gfc_get_symbol (p, NULL, &sym);
5165 sym->name = gfc_get_string ("%s", name);
5166 sym->module = module_name;
5167 sym->attr.flavor = FL_PROCEDURE;
5168 sym->attr.generic = 1;
5169 sym->attr.use_assoc = 1;
5170 }
5171 }
5172 else
5173 {
5174 /* Unless sym is a generic interface, this reference
5175 is ambiguous. */
5176 if (st == NULL)
5177 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5178
5179 sym = st->n.sym;
5180
5181 if (st && !sym->attr.generic
5182 && !st->ambiguous
5183 && sym->module
5184 && strcmp (module, sym->module))
5185 {
5186 ambiguous_set = true;
5187 st->ambiguous = 1;
5188 }
5189 }
5190
5191 sym->attr.use_only = only_flag;
5192 sym->attr.use_rename = renamed;
5193
5194 if (i == 1)
5195 {
5196 mio_interface_rest (&sym->generic);
5197 generic = sym->generic;
5198 }
5199 else if (!sym->generic)
5200 {
5201 sym->generic = generic;
5202 sym->attr.generic_copy = 1;
5203 }
5204
5205 /* If a procedure that is not generic has generic interfaces
5206 that include itself, it is generic! We need to take care
5207 to retain symbols ambiguous that were already so. */
5208 if (sym->attr.use_assoc
5209 && !sym->attr.generic
5210 && sym->attr.flavor == FL_PROCEDURE)
5211 {
5212 for (gen = generic; gen; gen = gen->next)
5213 {
5214 if (gen->sym == sym)
5215 {
5216 sym->attr.generic = 1;
5217 if (ambiguous_set)
5218 st->ambiguous = 0;
5219 break;
5220 }
5221 }
5222 }
5223
5224 }
5225 }
5226
5227 mio_rparen ();
5228 }
5229
5230
5231 /* Load common blocks. */
5232
5233 static void
5234 load_commons (void)
5235 {
5236 char name[GFC_MAX_SYMBOL_LEN + 1];
5237 gfc_common_head *p;
5238
5239 mio_lparen ();
5240
5241 while (peek_atom () != ATOM_RPAREN)
5242 {
5243 int flags = 0;
5244 char* label;
5245 mio_lparen ();
5246 mio_internal_string (name);
5247
5248 p = gfc_get_common (name, 1);
5249
5250 mio_symbol_ref (&p->head);
5251 mio_integer (&flags);
5252 if (flags & 1)
5253 p->saved = 1;
5254 if (flags & 2)
5255 p->threadprivate = 1;
5256 p->omp_device_type = (gfc_omp_device_type) ((flags >> 2) & 3);
5257 p->use_assoc = 1;
5258
5259 /* Get whether this was a bind(c) common or not. */
5260 mio_integer (&p->is_bind_c);
5261 /* Get the binding label. */
5262 label = read_string ();
5263 if (strlen (label))
5264 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
5265 XDELETEVEC (label);
5266
5267 mio_rparen ();
5268 }
5269
5270 mio_rparen ();
5271 }
5272
5273
5274 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
5275 so that unused variables are not loaded and so that the expression can
5276 be safely freed. */
5277
5278 static void
5279 load_equiv (void)
5280 {
5281 gfc_equiv *head, *tail, *end, *eq, *equiv;
5282 bool duplicate;
5283
5284 mio_lparen ();
5285 in_load_equiv = true;
5286
5287 end = gfc_current_ns->equiv;
5288 while (end != NULL && end->next != NULL)
5289 end = end->next;
5290
5291 while (peek_atom () != ATOM_RPAREN) {
5292 mio_lparen ();
5293 head = tail = NULL;
5294
5295 while(peek_atom () != ATOM_RPAREN)
5296 {
5297 if (head == NULL)
5298 head = tail = gfc_get_equiv ();
5299 else
5300 {
5301 tail->eq = gfc_get_equiv ();
5302 tail = tail->eq;
5303 }
5304
5305 mio_pool_string (&tail->module);
5306 mio_expr (&tail->expr);
5307 }
5308
5309 /* Check for duplicate equivalences being loaded from different modules */
5310 duplicate = false;
5311 for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
5312 {
5313 if (equiv->module && head->module
5314 && strcmp (equiv->module, head->module) == 0)
5315 {
5316 duplicate = true;
5317 break;
5318 }
5319 }
5320
5321 if (duplicate)
5322 {
5323 for (eq = head; eq; eq = head)
5324 {
5325 head = eq->eq;
5326 gfc_free_expr (eq->expr);
5327 free (eq);
5328 }
5329 }
5330
5331 if (end == NULL)
5332 gfc_current_ns->equiv = head;
5333 else
5334 end->next = head;
5335
5336 if (head != NULL)
5337 end = head;
5338
5339 mio_rparen ();
5340 }
5341
5342 mio_rparen ();
5343 in_load_equiv = false;
5344 }
5345
5346
5347 /* This function loads OpenMP user defined reductions. */
5348 static void
5349 load_omp_udrs (void)
5350 {
5351 mio_lparen ();
5352 while (peek_atom () != ATOM_RPAREN)
5353 {
5354 const char *name = NULL, *newname;
5355 char *altname;
5356 gfc_typespec ts;
5357 gfc_symtree *st;
5358 gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
5359
5360 mio_lparen ();
5361 mio_pool_string (&name);
5362 gfc_clear_ts (&ts);
5363 mio_typespec (&ts);
5364 if (startswith (name, "operator "))
5365 {
5366 const char *p = name + sizeof ("operator ") - 1;
5367 if (strcmp (p, "+") == 0)
5368 rop = OMP_REDUCTION_PLUS;
5369 else if (strcmp (p, "*") == 0)
5370 rop = OMP_REDUCTION_TIMES;
5371 else if (strcmp (p, "-") == 0)
5372 rop = OMP_REDUCTION_MINUS;
5373 else if (strcmp (p, ".and.") == 0)
5374 rop = OMP_REDUCTION_AND;
5375 else if (strcmp (p, ".or.") == 0)
5376 rop = OMP_REDUCTION_OR;
5377 else if (strcmp (p, ".eqv.") == 0)
5378 rop = OMP_REDUCTION_EQV;
5379 else if (strcmp (p, ".neqv.") == 0)
5380 rop = OMP_REDUCTION_NEQV;
5381 }
5382 altname = NULL;
5383 if (rop == OMP_REDUCTION_USER && name[0] == '.')
5384 {
5385 size_t len = strlen (name + 1);
5386 altname = XALLOCAVEC (char, len);
5387 gcc_assert (name[len] == '.');
5388 memcpy (altname, name + 1, len - 1);
5389 altname[len - 1] = '\0';
5390 }
5391 newname = name;
5392 if (rop == OMP_REDUCTION_USER)
5393 newname = find_use_name (altname ? altname : name, !!altname);
5394 else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
5395 newname = NULL;
5396 if (newname == NULL)
5397 {
5398 skip_list (1);
5399 continue;
5400 }
5401 if (altname && newname != altname)
5402 {
5403 size_t len = strlen (newname);
5404 altname = XALLOCAVEC (char, len + 3);
5405 altname[0] = '.';
5406 memcpy (altname + 1, newname, len);
5407 altname[len + 1] = '.';
5408 altname[len + 2] = '\0';
5409 name = gfc_get_string ("%s", altname);
5410 }
5411 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
5412 gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
5413 if (udr)
5414 {
5415 require_atom (ATOM_INTEGER);
5416 pointer_info *p = get_integer (atom_int);
5417 if (strcmp (p->u.rsym.module, udr->omp_out->module))
5418 {
5419 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
5420 "module %s at %L",
5421 p->u.rsym.module, &gfc_current_locus);
5422 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
5423 "%s at %L",
5424 udr->omp_out->module, &udr->where);
5425 }
5426 skip_list (1);
5427 continue;
5428 }
5429 udr = gfc_get_omp_udr ();
5430 udr->name = name;
5431 udr->rop = rop;
5432 udr->ts = ts;
5433 udr->where = gfc_current_locus;
5434 udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
5435 udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
5436 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
5437 false);
5438 if (peek_atom () != ATOM_RPAREN)
5439 {
5440 udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
5441 udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
5442 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
5443 udr->initializer_ns, true);
5444 }
5445 if (st)
5446 {
5447 udr->next = st->n.omp_udr;
5448 st->n.omp_udr = udr;
5449 }
5450 else
5451 {
5452 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
5453 st->n.omp_udr = udr;
5454 }
5455 mio_rparen ();
5456 }
5457 mio_rparen ();
5458 }
5459
5460
5461 /* Recursive function to traverse the pointer_info tree and load a
5462 needed symbol. We return nonzero if we load a symbol and stop the
5463 traversal, because the act of loading can alter the tree. */
5464
5465 static int
5466 load_needed (pointer_info *p)
5467 {
5468 gfc_namespace *ns;
5469 pointer_info *q;
5470 gfc_symbol *sym;
5471 int rv;
5472
5473 rv = 0;
5474 if (p == NULL)
5475 return rv;
5476
5477 rv |= load_needed (p->left);
5478 rv |= load_needed (p->right);
5479
5480 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
5481 return rv;
5482
5483 p->u.rsym.state = USED;
5484
5485 set_module_locus (&p->u.rsym.where);
5486
5487 sym = p->u.rsym.sym;
5488 if (sym == NULL)
5489 {
5490 q = get_integer (p->u.rsym.ns);
5491
5492 ns = (gfc_namespace *) q->u.pointer;
5493 if (ns == NULL)
5494 {
5495 /* Create an interface namespace if necessary. These are
5496 the namespaces that hold the formal parameters of module
5497 procedures. */
5498
5499 ns = gfc_get_namespace (NULL, 0);
5500 associate_integer_pointer (q, ns);
5501 }
5502
5503 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
5504 doesn't go pear-shaped if the symbol is used. */
5505 if (!ns->proc_name)
5506 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
5507 1, &ns->proc_name);
5508
5509 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
5510 sym->name = gfc_dt_lower_string (p->u.rsym.true_name);
5511 sym->module = gfc_get_string ("%s", p->u.rsym.module);
5512 if (p->u.rsym.binding_label)
5513 sym->binding_label = IDENTIFIER_POINTER (get_identifier
5514 (p->u.rsym.binding_label));
5515
5516 associate_integer_pointer (p, sym);
5517 }
5518
5519 mio_symbol (sym);
5520 sym->attr.use_assoc = 1;
5521
5522 /* Unliked derived types, a STRUCTURE may share names with other symbols.
5523 We greedily converted the symbol name to lowercase before we knew its
5524 type, so now we must fix it. */
5525 if (sym->attr.flavor == FL_STRUCT)
5526 sym->name = gfc_dt_upper_string (sym->name);
5527
5528 /* Mark as only or rename for later diagnosis for explicitly imported
5529 but not used warnings; don't mark internal symbols such as __vtab,
5530 __def_init etc. Only mark them if they have been explicitly loaded. */
5531
5532 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
5533 {
5534 gfc_use_rename *u;
5535
5536 /* Search the use/rename list for the variable; if the variable is
5537 found, mark it. */
5538 for (u = gfc_rename_list; u; u = u->next)
5539 {
5540 if (strcmp (u->use_name, sym->name) == 0)
5541 {
5542 sym->attr.use_only = 1;
5543 break;
5544 }
5545 }
5546 }
5547
5548 if (p->u.rsym.renamed)
5549 sym->attr.use_rename = 1;
5550
5551 return 1;
5552 }
5553
5554
5555 /* Recursive function for cleaning up things after a module has been read. */
5556
5557 static void
5558 read_cleanup (pointer_info *p)
5559 {
5560 gfc_symtree *st;
5561 pointer_info *q;
5562
5563 if (p == NULL)
5564 return;
5565
5566 read_cleanup (p->left);
5567 read_cleanup (p->right);
5568
5569 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
5570 {
5571 gfc_namespace *ns;
5572 /* Add hidden symbols to the symtree. */
5573 q = get_integer (p->u.rsym.ns);
5574 ns = (gfc_namespace *) q->u.pointer;
5575
5576 if (!p->u.rsym.sym->attr.vtype
5577 && !p->u.rsym.sym->attr.vtab)
5578 st = gfc_get_unique_symtree (ns);
5579 else
5580 {
5581 /* There is no reason to use 'unique_symtrees' for vtabs or
5582 vtypes - their name is fine for a symtree and reduces the
5583 namespace pollution. */
5584 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
5585 if (!st)
5586 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
5587 }
5588
5589 st->n.sym = p->u.rsym.sym;
5590 st->n.sym->refs++;
5591
5592 /* Fixup any symtree references. */
5593 p->u.rsym.symtree = st;
5594 resolve_fixups (p->u.rsym.stfixup, st);
5595 p->u.rsym.stfixup = NULL;
5596 }
5597
5598 /* Free unused symbols. */
5599 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
5600 gfc_free_symbol (p->u.rsym.sym);
5601 }
5602
5603
5604 /* It is not quite enough to check for ambiguity in the symbols by
5605 the loaded symbol and the new symbol not being identical. */
5606 static bool
5607 check_for_ambiguous (gfc_symtree *st, pointer_info *info)
5608 {
5609 gfc_symbol *rsym;
5610 module_locus locus;
5611 symbol_attribute attr;
5612 gfc_symbol *st_sym;
5613
5614 if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
5615 {
5616 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
5617 "current program unit", st->name, module_name);
5618 return true;
5619 }
5620
5621 st_sym = st->n.sym;
5622 rsym = info->u.rsym.sym;
5623 if (st_sym == rsym)
5624 return false;
5625
5626 if (st_sym->attr.vtab || st_sym->attr.vtype)
5627 return false;
5628
5629 /* If the existing symbol is generic from a different module and
5630 the new symbol is generic there can be no ambiguity. */
5631 if (st_sym->attr.generic
5632 && st_sym->module
5633 && st_sym->module != module_name)
5634 {
5635 /* The new symbol's attributes have not yet been read. Since
5636 we need attr.generic, read it directly. */
5637 get_module_locus (&locus);
5638 set_module_locus (&info->u.rsym.where);
5639 mio_lparen ();
5640 attr.generic = 0;
5641 mio_symbol_attribute (&attr);
5642 set_module_locus (&locus);
5643 if (attr.generic)
5644 return false;
5645 }
5646
5647 return true;
5648 }
5649
5650
5651 /* Read a module file. */
5652
5653 static void
5654 read_module (void)
5655 {
5656 module_locus operator_interfaces, user_operators, omp_udrs;
5657 const char *p;
5658 char name[GFC_MAX_SYMBOL_LEN + 1];
5659 int i;
5660 /* Workaround -Wmaybe-uninitialized false positive during
5661 profiledbootstrap by initializing them. */
5662 int ambiguous = 0, j, nuse, symbol = 0;
5663 pointer_info *info, *q;
5664 gfc_use_rename *u = NULL;
5665 gfc_symtree *st;
5666 gfc_symbol *sym;
5667
5668 get_module_locus (&operator_interfaces); /* Skip these for now. */
5669 skip_list ();
5670
5671 get_module_locus (&user_operators);
5672 skip_list ();
5673 skip_list ();
5674
5675 /* Skip commons and equivalences for now. */
5676 skip_list ();
5677 skip_list ();
5678
5679 /* Skip OpenMP UDRs. */
5680 get_module_locus (&omp_udrs);
5681 skip_list ();
5682
5683 mio_lparen ();
5684
5685 /* Create the fixup nodes for all the symbols. */
5686
5687 while (peek_atom () != ATOM_RPAREN)
5688 {
5689 char* bind_label;
5690 require_atom (ATOM_INTEGER);
5691 info = get_integer (atom_int);
5692
5693 info->type = P_SYMBOL;
5694 info->u.rsym.state = UNUSED;
5695
5696 info->u.rsym.true_name = read_string ();
5697 info->u.rsym.module = read_string ();
5698 bind_label = read_string ();
5699 if (strlen (bind_label))
5700 info->u.rsym.binding_label = bind_label;
5701 else
5702 XDELETEVEC (bind_label);
5703
5704 require_atom (ATOM_INTEGER);
5705 info->u.rsym.ns = atom_int;
5706
5707 get_module_locus (&info->u.rsym.where);
5708
5709 /* See if the symbol has already been loaded by a previous module.
5710 If so, we reference the existing symbol and prevent it from
5711 being loaded again. This should not happen if the symbol being
5712 read is an index for an assumed shape dummy array (ns != 1). */
5713
5714 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
5715
5716 if (sym == NULL
5717 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
5718 {
5719 skip_list ();
5720 continue;
5721 }
5722
5723 info->u.rsym.state = USED;
5724 info->u.rsym.sym = sym;
5725 /* The current symbol has already been loaded, so we can avoid loading
5726 it again. However, if it is a derived type, some of its components
5727 can be used in expressions in the module. To avoid the module loading
5728 failing, we need to associate the module's component pointer indexes
5729 with the existing symbol's component pointers. */
5730 if (gfc_fl_struct (sym->attr.flavor))
5731 {
5732 gfc_component *c;
5733
5734 /* First seek to the symbol's component list. */
5735 mio_lparen (); /* symbol opening. */
5736 skip_list (); /* skip symbol attribute. */
5737
5738 mio_lparen (); /* component list opening. */
5739 for (c = sym->components; c; c = c->next)
5740 {
5741 pointer_info *p;
5742 const char *comp_name = NULL;
5743 int n = 0;
5744
5745 mio_lparen (); /* component opening. */
5746 mio_integer (&n);
5747 p = get_integer (n);
5748 if (p->u.pointer == NULL)
5749 associate_integer_pointer (p, c);
5750 mio_pool_string (&comp_name);
5751 if (comp_name != c->name)
5752 {
5753 gfc_fatal_error ("Mismatch in components of derived type "
5754 "%qs from %qs at %C: expecting %qs, "
5755 "but got %qs", sym->name, sym->module,
5756 c->name, comp_name);
5757 }
5758 skip_list (1); /* component end. */
5759 }
5760 mio_rparen (); /* component list closing. */
5761
5762 skip_list (1); /* symbol end. */
5763 }
5764 else
5765 skip_list ();
5766
5767 /* Some symbols do not have a namespace (eg. formal arguments),
5768 so the automatic "unique symtree" mechanism must be suppressed
5769 by marking them as referenced. */
5770 q = get_integer (info->u.rsym.ns);
5771 if (q->u.pointer == NULL)
5772 {
5773 info->u.rsym.referenced = 1;
5774 continue;
5775 }
5776 }
5777
5778 mio_rparen ();
5779
5780 /* Parse the symtree lists. This lets us mark which symbols need to
5781 be loaded. Renaming is also done at this point by replacing the
5782 symtree name. */
5783
5784 mio_lparen ();
5785
5786 while (peek_atom () != ATOM_RPAREN)
5787 {
5788 mio_internal_string (name);
5789 mio_integer (&ambiguous);
5790 mio_integer (&symbol);
5791
5792 info = get_integer (symbol);
5793
5794 /* See how many use names there are. If none, go through the start
5795 of the loop at least once. */
5796 nuse = number_use_names (name, false);
5797 info->u.rsym.renamed = nuse ? 1 : 0;
5798
5799 if (nuse == 0)
5800 nuse = 1;
5801
5802 for (j = 1; j <= nuse; j++)
5803 {
5804 /* Get the jth local name for this symbol. */
5805 p = find_use_name_n (name, &j, false);
5806
5807 if (p == NULL && strcmp (name, module_name) == 0)
5808 p = name;
5809
5810 /* Exception: Always import vtabs & vtypes. */
5811 if (p == NULL && name[0] == '_'
5812 && (startswith (name, "__vtab_")
5813 || startswith (name, "__vtype_")))
5814 p = name;
5815
5816 /* Skip symtree nodes not in an ONLY clause, unless there
5817 is an existing symtree loaded from another USE statement. */
5818 if (p == NULL)
5819 {
5820 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5821 if (st != NULL
5822 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
5823 && st->n.sym->module != NULL
5824 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
5825 {
5826 info->u.rsym.symtree = st;
5827 info->u.rsym.sym = st->n.sym;
5828 }
5829 continue;
5830 }
5831
5832 /* If a symbol of the same name and module exists already,
5833 this symbol, which is not in an ONLY clause, must not be
5834 added to the namespace(11.3.2). Note that find_symbol
5835 only returns the first occurrence that it finds. */
5836 if (!only_flag && !info->u.rsym.renamed
5837 && strcmp (name, module_name) != 0
5838 && find_symbol (gfc_current_ns->sym_root, name,
5839 module_name, 0))
5840 continue;
5841
5842 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
5843
5844 if (st != NULL
5845 && !(st->n.sym && st->n.sym->attr.used_in_submodule))
5846 {
5847 /* Check for ambiguous symbols. */
5848 if (check_for_ambiguous (st, info))
5849 st->ambiguous = 1;
5850 else
5851 info->u.rsym.symtree = st;
5852 }
5853 else
5854 {
5855 if (st)
5856 {
5857 /* This symbol is host associated from a module in a
5858 submodule. Hide it with a unique symtree. */
5859 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
5860 s->n.sym = st->n.sym;
5861 st->n.sym = NULL;
5862 }
5863 else
5864 {
5865 /* Create a symtree node in the current namespace for this
5866 symbol. */
5867 st = check_unique_name (p)
5868 ? gfc_get_unique_symtree (gfc_current_ns)
5869 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
5870 st->ambiguous = ambiguous;
5871 }
5872
5873 sym = info->u.rsym.sym;
5874
5875 /* Create a symbol node if it doesn't already exist. */
5876 if (sym == NULL)
5877 {
5878 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
5879 gfc_current_ns);
5880 info->u.rsym.sym->name = gfc_dt_lower_string (info->u.rsym.true_name);
5881 sym = info->u.rsym.sym;
5882 sym->module = gfc_get_string ("%s", info->u.rsym.module);
5883
5884 if (info->u.rsym.binding_label)
5885 {
5886 tree id = get_identifier (info->u.rsym.binding_label);
5887 sym->binding_label = IDENTIFIER_POINTER (id);
5888 }
5889 }
5890
5891 st->n.sym = sym;
5892 st->n.sym->refs++;
5893
5894 if (strcmp (name, p) != 0)
5895 sym->attr.use_rename = 1;
5896
5897 if (name[0] != '_'
5898 || (!startswith (name, "__vtab_")
5899 && !startswith (name, "__vtype_")))
5900 sym->attr.use_only = only_flag;
5901
5902 /* Store the symtree pointing to this symbol. */
5903 info->u.rsym.symtree = st;
5904
5905 if (info->u.rsym.state == UNUSED)
5906 info->u.rsym.state = NEEDED;
5907 info->u.rsym.referenced = 1;
5908 }
5909 }
5910 }
5911
5912 mio_rparen ();
5913
5914 /* Load intrinsic operator interfaces. */
5915 set_module_locus (&operator_interfaces);
5916 mio_lparen ();
5917
5918 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5919 {
5920 gfc_use_rename *u = NULL, *v = NULL;
5921 int j = i;
5922
5923 if (i == INTRINSIC_USER)
5924 continue;
5925
5926 if (only_flag)
5927 {
5928 u = find_use_operator ((gfc_intrinsic_op) i);
5929
5930 /* F2018:10.1.5.5.1 requires same interpretation of old and new-style
5931 relational operators. Special handling for USE, ONLY. */
5932 switch (i)
5933 {
5934 case INTRINSIC_EQ:
5935 j = INTRINSIC_EQ_OS;
5936 break;
5937 case INTRINSIC_EQ_OS:
5938 j = INTRINSIC_EQ;
5939 break;
5940 case INTRINSIC_NE:
5941 j = INTRINSIC_NE_OS;
5942 break;
5943 case INTRINSIC_NE_OS:
5944 j = INTRINSIC_NE;
5945 break;
5946 case INTRINSIC_GT:
5947 j = INTRINSIC_GT_OS;
5948 break;
5949 case INTRINSIC_GT_OS:
5950 j = INTRINSIC_GT;
5951 break;
5952 case INTRINSIC_GE:
5953 j = INTRINSIC_GE_OS;
5954 break;
5955 case INTRINSIC_GE_OS:
5956 j = INTRINSIC_GE;
5957 break;
5958 case INTRINSIC_LT:
5959 j = INTRINSIC_LT_OS;
5960 break;
5961 case INTRINSIC_LT_OS:
5962 j = INTRINSIC_LT;
5963 break;
5964 case INTRINSIC_LE:
5965 j = INTRINSIC_LE_OS;
5966 break;
5967 case INTRINSIC_LE_OS:
5968 j = INTRINSIC_LE;
5969 break;
5970 default:
5971 break;
5972 }
5973
5974 if (j != i)
5975 v = find_use_operator ((gfc_intrinsic_op) j);
5976
5977 if (u == NULL && v == NULL)
5978 {
5979 skip_list ();
5980 continue;
5981 }
5982
5983 if (u)
5984 u->found = 1;
5985 if (v)
5986 v->found = 1;
5987 }
5988
5989 mio_interface (&gfc_current_ns->op[i]);
5990 if (!gfc_current_ns->op[i] && !gfc_current_ns->op[j])
5991 {
5992 if (u)
5993 u->found = 0;
5994 if (v)
5995 v->found = 0;
5996 }
5997 }
5998
5999 mio_rparen ();
6000
6001 /* Load generic and user operator interfaces. These must follow the
6002 loading of symtree because otherwise symbols can be marked as
6003 ambiguous. */
6004
6005 set_module_locus (&user_operators);
6006
6007 load_operator_interfaces ();
6008 load_generic_interfaces ();
6009
6010 load_commons ();
6011 load_equiv ();
6012
6013 /* Load OpenMP user defined reductions. */
6014 set_module_locus (&omp_udrs);
6015 load_omp_udrs ();
6016
6017 /* At this point, we read those symbols that are needed but haven't
6018 been loaded yet. If one symbol requires another, the other gets
6019 marked as NEEDED if its previous state was UNUSED. */
6020
6021 while (load_needed (pi_root));
6022
6023 /* Make sure all elements of the rename-list were found in the module. */
6024
6025 for (u = gfc_rename_list; u; u = u->next)
6026 {
6027 if (u->found)
6028 continue;
6029
6030 if (u->op == INTRINSIC_NONE)
6031 {
6032 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
6033 u->use_name, &u->where, module_name);
6034 continue;
6035 }
6036
6037 if (u->op == INTRINSIC_USER)
6038 {
6039 gfc_error ("User operator %qs referenced at %L not found "
6040 "in module %qs", u->use_name, &u->where, module_name);
6041 continue;
6042 }
6043
6044 gfc_error ("Intrinsic operator %qs referenced at %L not found "
6045 "in module %qs", gfc_op2string (u->op), &u->where,
6046 module_name);
6047 }
6048
6049 /* Clean up symbol nodes that were never loaded, create references
6050 to hidden symbols. */
6051
6052 read_cleanup (pi_root);
6053 }
6054
6055
6056 /* Given an access type that is specific to an entity and the default
6057 access, return nonzero if the entity is publicly accessible. If the
6058 element is declared as PUBLIC, then it is public; if declared
6059 PRIVATE, then private, and otherwise it is public unless the default
6060 access in this context has been declared PRIVATE. */
6061
6062 static bool dump_smod = false;
6063
6064 static bool
6065 check_access (gfc_access specific_access, gfc_access default_access)
6066 {
6067 if (dump_smod)
6068 return true;
6069
6070 if (specific_access == ACCESS_PUBLIC)
6071 return true;
6072 if (specific_access == ACCESS_PRIVATE)
6073 return false;
6074
6075 if (flag_module_private)
6076 return default_access == ACCESS_PUBLIC;
6077 else
6078 return default_access != ACCESS_PRIVATE;
6079 }
6080
6081
6082 bool
6083 gfc_check_symbol_access (gfc_symbol *sym)
6084 {
6085 if (sym->attr.vtab || sym->attr.vtype)
6086 return true;
6087 else
6088 return check_access (sym->attr.access, sym->ns->default_access);
6089 }
6090
6091
6092 /* A structure to remember which commons we've already written. */
6093
6094 struct written_common
6095 {
6096 BBT_HEADER(written_common);
6097 const char *name, *label;
6098 };
6099
6100 static struct written_common *written_commons = NULL;
6101
6102 /* Comparison function used for balancing the binary tree. */
6103
6104 static int
6105 compare_written_commons (void *a1, void *b1)
6106 {
6107 const char *aname = ((struct written_common *) a1)->name;
6108 const char *alabel = ((struct written_common *) a1)->label;
6109 const char *bname = ((struct written_common *) b1)->name;
6110 const char *blabel = ((struct written_common *) b1)->label;
6111 int c = strcmp (aname, bname);
6112
6113 return (c != 0 ? c : strcmp (alabel, blabel));
6114 }
6115
6116 /* Free a list of written commons. */
6117
6118 static void
6119 free_written_common (struct written_common *w)
6120 {
6121 if (!w)
6122 return;
6123
6124 if (w->left)
6125 free_written_common (w->left);
6126 if (w->right)
6127 free_written_common (w->right);
6128
6129 free (w);
6130 }
6131
6132 /* Write a common block to the module -- recursive helper function. */
6133
6134 static void
6135 write_common_0 (gfc_symtree *st, bool this_module)
6136 {
6137 gfc_common_head *p;
6138 const char * name;
6139 int flags;
6140 const char *label;
6141 struct written_common *w;
6142 bool write_me = true;
6143
6144 if (st == NULL)
6145 return;
6146
6147 write_common_0 (st->left, this_module);
6148
6149 /* We will write out the binding label, or "" if no label given. */
6150 name = st->n.common->name;
6151 p = st->n.common;
6152 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
6153
6154 /* Check if we've already output this common. */
6155 w = written_commons;
6156 while (w)
6157 {
6158 int c = strcmp (name, w->name);
6159 c = (c != 0 ? c : strcmp (label, w->label));
6160 if (c == 0)
6161 write_me = false;
6162
6163 w = (c < 0) ? w->left : w->right;
6164 }
6165
6166 if (this_module && p->use_assoc)
6167 write_me = false;
6168
6169 if (write_me)
6170 {
6171 /* Write the common to the module. */
6172 mio_lparen ();
6173 mio_pool_string (&name);
6174
6175 mio_symbol_ref (&p->head);
6176 flags = p->saved ? 1 : 0;
6177 if (p->threadprivate)
6178 flags |= 2;
6179 flags |= p->omp_device_type << 2;
6180 mio_integer (&flags);
6181
6182 /* Write out whether the common block is bind(c) or not. */
6183 mio_integer (&(p->is_bind_c));
6184
6185 mio_pool_string (&label);
6186 mio_rparen ();
6187
6188 /* Record that we have written this common. */
6189 w = XCNEW (struct written_common);
6190 w->name = p->name;
6191 w->label = label;
6192 gfc_insert_bbt (&written_commons, w, compare_written_commons);
6193 }
6194
6195 write_common_0 (st->right, this_module);
6196 }
6197
6198
6199 /* Write a common, by initializing the list of written commons, calling
6200 the recursive function write_common_0() and cleaning up afterwards. */
6201
6202 static void
6203 write_common (gfc_symtree *st)
6204 {
6205 written_commons = NULL;
6206 write_common_0 (st, true);
6207 write_common_0 (st, false);
6208 free_written_common (written_commons);
6209 written_commons = NULL;
6210 }
6211
6212
6213 /* Write the blank common block to the module. */
6214
6215 static void
6216 write_blank_common (void)
6217 {
6218 const char * name = BLANK_COMMON_NAME;
6219 int saved;
6220 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
6221 this, but it hasn't been checked. Just making it so for now. */
6222 int is_bind_c = 0;
6223
6224 if (gfc_current_ns->blank_common.head == NULL)
6225 return;
6226
6227 mio_lparen ();
6228
6229 mio_pool_string (&name);
6230
6231 mio_symbol_ref (&gfc_current_ns->blank_common.head);
6232 saved = gfc_current_ns->blank_common.saved;
6233 mio_integer (&saved);
6234
6235 /* Write out whether the common block is bind(c) or not. */
6236 mio_integer (&is_bind_c);
6237
6238 /* Write out an empty binding label. */
6239 write_atom (ATOM_STRING, "");
6240
6241 mio_rparen ();
6242 }
6243
6244
6245 /* Write equivalences to the module. */
6246
6247 static void
6248 write_equiv (void)
6249 {
6250 gfc_equiv *eq, *e;
6251 int num;
6252
6253 num = 0;
6254 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
6255 {
6256 mio_lparen ();
6257
6258 for (e = eq; e; e = e->eq)
6259 {
6260 if (e->module == NULL)
6261 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
6262 mio_allocated_string (e->module);
6263 mio_expr (&e->expr);
6264 }
6265
6266 num++;
6267 mio_rparen ();
6268 }
6269 }
6270
6271
6272 /* Write a symbol to the module. */
6273
6274 static void
6275 write_symbol (int n, gfc_symbol *sym)
6276 {
6277 const char *label;
6278
6279 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
6280 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
6281
6282 mio_integer (&n);
6283
6284 if (gfc_fl_struct (sym->attr.flavor))
6285 {
6286 const char *name;
6287 name = gfc_dt_upper_string (sym->name);
6288 mio_pool_string (&name);
6289 }
6290 else
6291 mio_pool_string (&sym->name);
6292
6293 mio_pool_string (&sym->module);
6294 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
6295 {
6296 label = sym->binding_label;
6297 mio_pool_string (&label);
6298 }
6299 else
6300 write_atom (ATOM_STRING, "");
6301
6302 mio_pointer_ref (&sym->ns);
6303
6304 mio_symbol (sym);
6305 write_char ('\n');
6306 }
6307
6308
6309 /* Recursive traversal function to write the initial set of symbols to
6310 the module. We check to see if the symbol should be written
6311 according to the access specification. */
6312
6313 static void
6314 write_symbol0 (gfc_symtree *st)
6315 {
6316 gfc_symbol *sym;
6317 pointer_info *p;
6318 bool dont_write = false;
6319
6320 if (st == NULL)
6321 return;
6322
6323 write_symbol0 (st->left);
6324
6325 sym = st->n.sym;
6326 if (sym->module == NULL)
6327 sym->module = module_name;
6328
6329 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6330 && !sym->attr.subroutine && !sym->attr.function)
6331 dont_write = true;
6332
6333 if (!gfc_check_symbol_access (sym))
6334 dont_write = true;
6335
6336 if (!dont_write)
6337 {
6338 p = get_pointer (sym);
6339 if (p->type == P_UNKNOWN)
6340 p->type = P_SYMBOL;
6341
6342 if (p->u.wsym.state != WRITTEN)
6343 {
6344 write_symbol (p->integer, sym);
6345 p->u.wsym.state = WRITTEN;
6346 }
6347 }
6348
6349 write_symbol0 (st->right);
6350 }
6351
6352
6353 static void
6354 write_omp_udr (gfc_omp_udr *udr)
6355 {
6356 switch (udr->rop)
6357 {
6358 case OMP_REDUCTION_USER:
6359 /* Non-operators can't be used outside of the module. */
6360 if (udr->name[0] != '.')
6361 return;
6362 else
6363 {
6364 gfc_symtree *st;
6365 size_t len = strlen (udr->name + 1);
6366 char *name = XALLOCAVEC (char, len);
6367 memcpy (name, udr->name, len - 1);
6368 name[len - 1] = '\0';
6369 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
6370 /* If corresponding user operator is private, don't write
6371 the UDR. */
6372 if (st != NULL)
6373 {
6374 gfc_user_op *uop = st->n.uop;
6375 if (!check_access (uop->access, uop->ns->default_access))
6376 return;
6377 }
6378 }
6379 break;
6380 case OMP_REDUCTION_PLUS:
6381 case OMP_REDUCTION_MINUS:
6382 case OMP_REDUCTION_TIMES:
6383 case OMP_REDUCTION_AND:
6384 case OMP_REDUCTION_OR:
6385 case OMP_REDUCTION_EQV:
6386 case OMP_REDUCTION_NEQV:
6387 /* If corresponding operator is private, don't write the UDR. */
6388 if (!check_access (gfc_current_ns->operator_access[udr->rop],
6389 gfc_current_ns->default_access))
6390 return;
6391 break;
6392 default:
6393 break;
6394 }
6395 if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
6396 {
6397 /* If derived type is private, don't write the UDR. */
6398 if (!gfc_check_symbol_access (udr->ts.u.derived))
6399 return;
6400 }
6401
6402 mio_lparen ();
6403 mio_pool_string (&udr->name);
6404 mio_typespec (&udr->ts);
6405 mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
6406 if (udr->initializer_ns)
6407 mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
6408 udr->initializer_ns, true);
6409 mio_rparen ();
6410 }
6411
6412
6413 static void
6414 write_omp_udrs (gfc_symtree *st)
6415 {
6416 if (st == NULL)
6417 return;
6418
6419 write_omp_udrs (st->left);
6420 gfc_omp_udr *udr;
6421 for (udr = st->n.omp_udr; udr; udr = udr->next)
6422 write_omp_udr (udr);
6423 write_omp_udrs (st->right);
6424 }
6425
6426
6427 /* Type for the temporary tree used when writing secondary symbols. */
6428
6429 struct sorted_pointer_info
6430 {
6431 BBT_HEADER (sorted_pointer_info);
6432
6433 pointer_info *p;
6434 };
6435
6436 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
6437
6438 /* Recursively traverse the temporary tree, free its contents. */
6439
6440 static void
6441 free_sorted_pointer_info_tree (sorted_pointer_info *p)
6442 {
6443 if (!p)
6444 return;
6445
6446 free_sorted_pointer_info_tree (p->left);
6447 free_sorted_pointer_info_tree (p->right);
6448
6449 free (p);
6450 }
6451
6452 /* Comparison function for the temporary tree. */
6453
6454 static int
6455 compare_sorted_pointer_info (void *_spi1, void *_spi2)
6456 {
6457 sorted_pointer_info *spi1, *spi2;
6458 spi1 = (sorted_pointer_info *)_spi1;
6459 spi2 = (sorted_pointer_info *)_spi2;
6460
6461 if (spi1->p->integer < spi2->p->integer)
6462 return -1;
6463 if (spi1->p->integer > spi2->p->integer)
6464 return 1;
6465 return 0;
6466 }
6467
6468
6469 /* Finds the symbols that need to be written and collects them in the
6470 sorted_pi tree so that they can be traversed in an order
6471 independent of memory addresses. */
6472
6473 static void
6474 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
6475 {
6476 if (!p)
6477 return;
6478
6479 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
6480 {
6481 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
6482 sp->p = p;
6483
6484 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
6485 }
6486
6487 find_symbols_to_write (tree, p->left);
6488 find_symbols_to_write (tree, p->right);
6489 }
6490
6491
6492 /* Recursive function that traverses the tree of symbols that need to be
6493 written and writes them in order. */
6494
6495 static void
6496 write_symbol1_recursion (sorted_pointer_info *sp)
6497 {
6498 if (!sp)
6499 return;
6500
6501 write_symbol1_recursion (sp->left);
6502
6503 pointer_info *p1 = sp->p;
6504 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
6505
6506 p1->u.wsym.state = WRITTEN;
6507 write_symbol (p1->integer, p1->u.wsym.sym);
6508 p1->u.wsym.sym->attr.public_used = 1;
6509
6510 write_symbol1_recursion (sp->right);
6511 }
6512
6513
6514 /* Write the secondary set of symbols to the module file. These are
6515 symbols that were not public yet are needed by the public symbols
6516 or another dependent symbol. The act of writing a symbol can add
6517 symbols to the pointer_info tree, so we return nonzero if a symbol
6518 was written and pass that information upwards. The caller will
6519 then call this function again until nothing was written. It uses
6520 the utility functions and a temporary tree to ensure a reproducible
6521 ordering of the symbol output and thus the module file. */
6522
6523 static int
6524 write_symbol1 (pointer_info *p)
6525 {
6526 if (!p)
6527 return 0;
6528
6529 /* Put symbols that need to be written into a tree sorted on the
6530 integer field. */
6531
6532 sorted_pointer_info *spi_root = NULL;
6533 find_symbols_to_write (&spi_root, p);
6534
6535 /* No symbols to write, return. */
6536 if (!spi_root)
6537 return 0;
6538
6539 /* Otherwise, write and free the tree again. */
6540 write_symbol1_recursion (spi_root);
6541 free_sorted_pointer_info_tree (spi_root);
6542
6543 return 1;
6544 }
6545
6546
6547 /* Write operator interfaces associated with a symbol. */
6548
6549 static void
6550 write_operator (gfc_user_op *uop)
6551 {
6552 static char nullstring[] = "";
6553 const char *p = nullstring;
6554
6555 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
6556 return;
6557
6558 mio_symbol_interface (&uop->name, &p, &uop->op);
6559 }
6560
6561
6562 /* Write generic interfaces from the namespace sym_root. */
6563
6564 static void
6565 write_generic (gfc_symtree *st)
6566 {
6567 gfc_symbol *sym;
6568
6569 if (st == NULL)
6570 return;
6571
6572 write_generic (st->left);
6573
6574 sym = st->n.sym;
6575 if (sym && !check_unique_name (st->name)
6576 && sym->generic && gfc_check_symbol_access (sym))
6577 {
6578 if (!sym->module)
6579 sym->module = module_name;
6580
6581 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
6582 }
6583
6584 write_generic (st->right);
6585 }
6586
6587
6588 static void
6589 write_symtree (gfc_symtree *st)
6590 {
6591 gfc_symbol *sym;
6592 pointer_info *p;
6593
6594 sym = st->n.sym;
6595
6596 /* A symbol in an interface body must not be visible in the
6597 module file. */
6598 if (sym->ns != gfc_current_ns
6599 && sym->ns->proc_name
6600 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
6601 return;
6602
6603 if (!gfc_check_symbol_access (sym)
6604 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
6605 && !sym->attr.subroutine && !sym->attr.function))
6606 return;
6607
6608 if (check_unique_name (st->name))
6609 return;
6610
6611 /* From F2003 onwards, intrinsic procedures are no longer subject to
6612 the restriction, "that an elemental intrinsic function here be of
6613 type integer or character and each argument must be an initialization
6614 expr of type integer or character" is lifted so that intrinsic
6615 procedures can be over-ridden. This requires that the intrinsic
6616 symbol not appear in the module file, thereby preventing ambiguity
6617 when USEd. */
6618 if (strcmp (sym->module, "(intrinsic)") == 0
6619 && (gfc_option.allow_std & GFC_STD_F2003))
6620 return;
6621
6622 p = find_pointer (sym);
6623 if (p == NULL)
6624 gfc_internal_error ("write_symtree(): Symbol not written");
6625
6626 mio_pool_string (&st->name);
6627 mio_integer (&st->ambiguous);
6628 mio_hwi (&p->integer);
6629 }
6630
6631
6632 static void
6633 write_module (void)
6634 {
6635 int i;
6636
6637 /* Initialize the column counter. */
6638 module_column = 1;
6639
6640 /* Write the operator interfaces. */
6641 mio_lparen ();
6642
6643 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
6644 {
6645 if (i == INTRINSIC_USER)
6646 continue;
6647
6648 mio_interface (check_access (gfc_current_ns->operator_access[i],
6649 gfc_current_ns->default_access)
6650 ? &gfc_current_ns->op[i] : NULL);
6651 }
6652
6653 mio_rparen ();
6654 write_char ('\n');
6655 write_char ('\n');
6656
6657 mio_lparen ();
6658 gfc_traverse_user_op (gfc_current_ns, write_operator);
6659 mio_rparen ();
6660 write_char ('\n');
6661 write_char ('\n');
6662
6663 mio_lparen ();
6664 write_generic (gfc_current_ns->sym_root);
6665 mio_rparen ();
6666 write_char ('\n');
6667 write_char ('\n');
6668
6669 mio_lparen ();
6670 write_blank_common ();
6671 write_common (gfc_current_ns->common_root);
6672 mio_rparen ();
6673 write_char ('\n');
6674 write_char ('\n');
6675
6676 mio_lparen ();
6677 write_equiv ();
6678 mio_rparen ();
6679 write_char ('\n');
6680 write_char ('\n');
6681
6682 mio_lparen ();
6683 write_omp_udrs (gfc_current_ns->omp_udr_root);
6684 mio_rparen ();
6685 write_char ('\n');
6686 write_char ('\n');
6687
6688 /* Write symbol information. First we traverse all symbols in the
6689 primary namespace, writing those that need to be written.
6690 Sometimes writing one symbol will cause another to need to be
6691 written. A list of these symbols ends up on the write stack, and
6692 we end by popping the bottom of the stack and writing the symbol
6693 until the stack is empty. */
6694
6695 mio_lparen ();
6696
6697 write_symbol0 (gfc_current_ns->sym_root);
6698 while (write_symbol1 (pi_root))
6699 /* Nothing. */;
6700
6701 mio_rparen ();
6702
6703 write_char ('\n');
6704 write_char ('\n');
6705
6706 mio_lparen ();
6707 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
6708 mio_rparen ();
6709 }
6710
6711
6712 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
6713 true on success, false on failure. */
6714
6715 static bool
6716 read_crc32_from_module_file (const char* filename, uLong* crc)
6717 {
6718 FILE *file;
6719 char buf[4];
6720 unsigned int val;
6721
6722 /* Open the file in binary mode. */
6723 if ((file = fopen (filename, "rb")) == NULL)
6724 return false;
6725
6726 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
6727 file. See RFC 1952. */
6728 if (fseek (file, -8, SEEK_END) != 0)
6729 {
6730 fclose (file);
6731 return false;
6732 }
6733
6734 /* Read the CRC32. */
6735 if (fread (buf, 1, 4, file) != 4)
6736 {
6737 fclose (file);
6738 return false;
6739 }
6740
6741 /* Close the file. */
6742 fclose (file);
6743
6744 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
6745 + ((buf[3] & 0xFF) << 24);
6746 *crc = val;
6747
6748 /* For debugging, the CRC value printed in hexadecimal should match
6749 the CRC printed by "zcat -l -v filename".
6750 printf("CRC of file %s is %x\n", filename, val); */
6751
6752 return true;
6753 }
6754
6755
6756 /* Given module, dump it to disk. If there was an error while
6757 processing the module, dump_flag will be set to zero and we delete
6758 the module file, even if it was already there. */
6759
6760 static void
6761 dump_module (const char *name, int dump_flag)
6762 {
6763 int n;
6764 char *filename, *filename_tmp;
6765 uLong crc, crc_old;
6766
6767 module_name = gfc_get_string ("%s", name);
6768
6769 if (dump_smod)
6770 {
6771 name = submodule_name;
6772 n = strlen (name) + strlen (SUBMODULE_EXTENSION) + 1;
6773 }
6774 else
6775 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
6776
6777 if (gfc_option.module_dir != NULL)
6778 {
6779 n += strlen (gfc_option.module_dir);
6780 filename = (char *) alloca (n);
6781 strcpy (filename, gfc_option.module_dir);
6782 strcat (filename, name);
6783 }
6784 else
6785 {
6786 filename = (char *) alloca (n);
6787 strcpy (filename, name);
6788 }
6789
6790 if (dump_smod)
6791 strcat (filename, SUBMODULE_EXTENSION);
6792 else
6793 strcat (filename, MODULE_EXTENSION);
6794
6795 /* Name of the temporary file used to write the module. */
6796 filename_tmp = (char *) alloca (n + 1);
6797 strcpy (filename_tmp, filename);
6798 strcat (filename_tmp, "0");
6799
6800 /* There was an error while processing the module. We delete the
6801 module file, even if it was already there. */
6802 if (!dump_flag)
6803 {
6804 remove (filename);
6805 return;
6806 }
6807
6808 if (gfc_cpp_makedep ())
6809 gfc_cpp_add_target (filename);
6810
6811 /* Write the module to the temporary file. */
6812 module_fp = gzopen (filename_tmp, "w");
6813 if (module_fp == NULL)
6814 gfc_fatal_error ("Cannot open module file %qs for writing at %C: %s",
6815 filename_tmp, xstrerror (errno));
6816
6817 /* Use lbasename to ensure module files are reproducible regardless
6818 of the build path (see the reproducible builds project). */
6819 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
6820 MOD_VERSION, lbasename (gfc_source_file));
6821
6822 /* Write the module itself. */
6823 iomode = IO_OUTPUT;
6824
6825 init_pi_tree ();
6826
6827 write_module ();
6828
6829 free_pi_tree (pi_root);
6830 pi_root = NULL;
6831
6832 write_char ('\n');
6833
6834 if (gzclose (module_fp))
6835 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6836 filename_tmp, xstrerror (errno));
6837
6838 /* Read the CRC32 from the gzip trailers of the module files and
6839 compare. */
6840 if (!read_crc32_from_module_file (filename_tmp, &crc)
6841 || !read_crc32_from_module_file (filename, &crc_old)
6842 || crc_old != crc)
6843 {
6844 /* Module file have changed, replace the old one. */
6845 if (remove (filename) && errno != ENOENT)
6846 gfc_fatal_error ("Cannot delete module file %qs: %s", filename,
6847 xstrerror (errno));
6848 if (rename (filename_tmp, filename))
6849 gfc_fatal_error ("Cannot rename module file %qs to %qs: %s",
6850 filename_tmp, filename, xstrerror (errno));
6851 }
6852 else
6853 {
6854 if (remove (filename_tmp))
6855 gfc_fatal_error ("Cannot delete temporary module file %qs: %s",
6856 filename_tmp, xstrerror (errno));
6857 }
6858 }
6859
6860
6861 /* Suppress the output of a .smod file by module, if no module
6862 procedures have been seen. */
6863 static bool no_module_procedures;
6864
6865 static void
6866 check_for_module_procedures (gfc_symbol *sym)
6867 {
6868 if (sym && sym->attr.module_procedure)
6869 no_module_procedures = false;
6870 }
6871
6872
6873 void
6874 gfc_dump_module (const char *name, int dump_flag)
6875 {
6876 if (gfc_state_stack->state == COMP_SUBMODULE)
6877 dump_smod = true;
6878 else
6879 dump_smod =false;
6880
6881 no_module_procedures = true;
6882 gfc_traverse_ns (gfc_current_ns, check_for_module_procedures);
6883
6884 dump_module (name, dump_flag);
6885
6886 if (no_module_procedures || dump_smod)
6887 return;
6888
6889 /* Write a submodule file from a module. The 'dump_smod' flag switches
6890 off the check for PRIVATE entities. */
6891 dump_smod = true;
6892 submodule_name = module_name;
6893 dump_module (name, dump_flag);
6894 dump_smod = false;
6895 }
6896
6897 static void
6898 create_intrinsic_function (const char *name, int id,
6899 const char *modname, intmod_id module,
6900 bool subroutine, gfc_symbol *result_type)
6901 {
6902 gfc_intrinsic_sym *isym;
6903 gfc_symtree *tmp_symtree;
6904 gfc_symbol *sym;
6905
6906 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6907 if (tmp_symtree)
6908 {
6909 if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
6910 && strcmp (modname, tmp_symtree->n.sym->module) == 0)
6911 return;
6912 gfc_error ("Symbol %qs at %C already declared", name);
6913 return;
6914 }
6915
6916 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6917 sym = tmp_symtree->n.sym;
6918
6919 if (subroutine)
6920 {
6921 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6922 isym = gfc_intrinsic_subroutine_by_id (isym_id);
6923 sym->attr.subroutine = 1;
6924 }
6925 else
6926 {
6927 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
6928 isym = gfc_intrinsic_function_by_id (isym_id);
6929
6930 sym->attr.function = 1;
6931 if (result_type)
6932 {
6933 sym->ts.type = BT_DERIVED;
6934 sym->ts.u.derived = result_type;
6935 sym->ts.is_c_interop = 1;
6936 isym->ts.f90_type = BT_VOID;
6937 isym->ts.type = BT_DERIVED;
6938 isym->ts.f90_type = BT_VOID;
6939 isym->ts.u.derived = result_type;
6940 isym->ts.is_c_interop = 1;
6941 }
6942 }
6943 gcc_assert (isym);
6944
6945 sym->attr.flavor = FL_PROCEDURE;
6946 sym->attr.intrinsic = 1;
6947
6948 sym->module = gfc_get_string ("%s", modname);
6949 sym->attr.use_assoc = 1;
6950 sym->from_intmod = module;
6951 sym->intmod_sym_id = id;
6952 }
6953
6954
6955 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6956 the current namespace for all named constants, pointer types, and
6957 procedures in the module unless the only clause was used or a rename
6958 list was provided. */
6959
6960 static void
6961 import_iso_c_binding_module (void)
6962 {
6963 gfc_symbol *mod_sym = NULL, *return_type;
6964 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
6965 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
6966 const char *iso_c_module_name = "__iso_c_binding";
6967 gfc_use_rename *u;
6968 int i;
6969 bool want_c_ptr = false, want_c_funptr = false;
6970
6971 /* Look only in the current namespace. */
6972 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
6973
6974 if (mod_symtree == NULL)
6975 {
6976 /* symtree doesn't already exist in current namespace. */
6977 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
6978 false);
6979
6980 if (mod_symtree != NULL)
6981 mod_sym = mod_symtree->n.sym;
6982 else
6983 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6984 "create symbol for %s", iso_c_module_name);
6985
6986 mod_sym->attr.flavor = FL_MODULE;
6987 mod_sym->attr.intrinsic = 1;
6988 mod_sym->module = gfc_get_string ("%s", iso_c_module_name);
6989 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
6990 }
6991
6992 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6993 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6994 need C_(FUN)PTR. */
6995 for (u = gfc_rename_list; u; u = u->next)
6996 {
6997 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
6998 u->use_name) == 0)
6999 want_c_ptr = true;
7000 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
7001 u->use_name) == 0)
7002 want_c_ptr = true;
7003 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
7004 u->use_name) == 0)
7005 want_c_funptr = true;
7006 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
7007 u->use_name) == 0)
7008 want_c_funptr = true;
7009 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
7010 u->use_name) == 0)
7011 {
7012 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
7013 (iso_c_binding_symbol)
7014 ISOCBINDING_PTR,
7015 u->local_name[0] ? u->local_name
7016 : u->use_name,
7017 NULL, false);
7018 }
7019 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
7020 u->use_name) == 0)
7021 {
7022 c_funptr
7023 = generate_isocbinding_symbol (iso_c_module_name,
7024 (iso_c_binding_symbol)
7025 ISOCBINDING_FUNPTR,
7026 u->local_name[0] ? u->local_name
7027 : u->use_name,
7028 NULL, false);
7029 }
7030 }
7031
7032 if ((want_c_ptr || !only_flag) && !c_ptr)
7033 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
7034 (iso_c_binding_symbol)
7035 ISOCBINDING_PTR,
7036 NULL, NULL, only_flag);
7037 if ((want_c_funptr || !only_flag) && !c_funptr)
7038 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
7039 (iso_c_binding_symbol)
7040 ISOCBINDING_FUNPTR,
7041 NULL, NULL, only_flag);
7042
7043 /* Generate the symbols for the named constants representing
7044 the kinds for intrinsic data types. */
7045 for (i = 0; i < ISOCBINDING_NUMBER; i++)
7046 {
7047 bool found = false;
7048 for (u = gfc_rename_list; u; u = u->next)
7049 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
7050 {
7051 bool not_in_std;
7052 const char *name;
7053 u->found = 1;
7054 found = true;
7055
7056 switch (i)
7057 {
7058 #define NAMED_FUNCTION(a,b,c,d) \
7059 case a: \
7060 not_in_std = (gfc_option.allow_std & d) == 0; \
7061 name = b; \
7062 break;
7063 #define NAMED_SUBROUTINE(a,b,c,d) \
7064 case a: \
7065 not_in_std = (gfc_option.allow_std & d) == 0; \
7066 name = b; \
7067 break;
7068 #define NAMED_INTCST(a,b,c,d) \
7069 case a: \
7070 not_in_std = (gfc_option.allow_std & d) == 0; \
7071 name = b; \
7072 break;
7073 #define NAMED_UINTCST(a,b,c,d) \
7074 case a: \
7075 not_in_std = (gfc_option.allow_std & d) == 0; \
7076 name = b; \
7077 break;
7078 #define NAMED_REALCST(a,b,c,d) \
7079 case a: \
7080 not_in_std = (gfc_option.allow_std & d) == 0; \
7081 name = b; \
7082 break;
7083 #define NAMED_CMPXCST(a,b,c,d) \
7084 case a: \
7085 not_in_std = (gfc_option.allow_std & d) == 0; \
7086 name = b; \
7087 break;
7088 #include "iso-c-binding.def"
7089 default:
7090 not_in_std = false;
7091 name = "";
7092 }
7093
7094 if (not_in_std)
7095 {
7096 gfc_error ("The symbol %qs, referenced at %L, is not "
7097 "in the selected standard", name, &u->where);
7098 continue;
7099 }
7100
7101 switch (i)
7102 {
7103 #define NAMED_FUNCTION(a,b,c,d) \
7104 case a: \
7105 if (a == ISOCBINDING_LOC) \
7106 return_type = c_ptr->n.sym; \
7107 else if (a == ISOCBINDING_FUNLOC) \
7108 return_type = c_funptr->n.sym; \
7109 else \
7110 return_type = NULL; \
7111 create_intrinsic_function (u->local_name[0] \
7112 ? u->local_name : u->use_name, \
7113 a, iso_c_module_name, \
7114 INTMOD_ISO_C_BINDING, false, \
7115 return_type); \
7116 break;
7117 #define NAMED_SUBROUTINE(a,b,c,d) \
7118 case a: \
7119 create_intrinsic_function (u->local_name[0] ? u->local_name \
7120 : u->use_name, \
7121 a, iso_c_module_name, \
7122 INTMOD_ISO_C_BINDING, true, NULL); \
7123 break;
7124 #include "iso-c-binding.def"
7125
7126 case ISOCBINDING_PTR:
7127 case ISOCBINDING_FUNPTR:
7128 /* Already handled above. */
7129 break;
7130 default:
7131 if (i == ISOCBINDING_NULL_PTR)
7132 tmp_symtree = c_ptr;
7133 else if (i == ISOCBINDING_NULL_FUNPTR)
7134 tmp_symtree = c_funptr;
7135 else
7136 tmp_symtree = NULL;
7137 generate_isocbinding_symbol (iso_c_module_name,
7138 (iso_c_binding_symbol) i,
7139 u->local_name[0]
7140 ? u->local_name : u->use_name,
7141 tmp_symtree, false);
7142 }
7143 }
7144
7145 if (!found && !only_flag)
7146 {
7147 /* Skip, if the symbol is not in the enabled standard. */
7148 switch (i)
7149 {
7150 #define NAMED_FUNCTION(a,b,c,d) \
7151 case a: \
7152 if ((gfc_option.allow_std & d) == 0) \
7153 continue; \
7154 break;
7155 #define NAMED_SUBROUTINE(a,b,c,d) \
7156 case a: \
7157 if ((gfc_option.allow_std & d) == 0) \
7158 continue; \
7159 break;
7160 #define NAMED_INTCST(a,b,c,d) \
7161 case a: \
7162 if ((gfc_option.allow_std & d) == 0) \
7163 continue; \
7164 break;
7165 #define NAMED_UINTCST(a,b,c,d) \
7166 case a: \
7167 if ((gfc_option.allow_std & d) == 0) \
7168 continue; \
7169 break;
7170 #define NAMED_REALCST(a,b,c,d) \
7171 case a: \
7172 if ((gfc_option.allow_std & d) == 0) \
7173 continue; \
7174 break;
7175 #define NAMED_CMPXCST(a,b,c,d) \
7176 case a: \
7177 if ((gfc_option.allow_std & d) == 0) \
7178 continue; \
7179 break;
7180 #include "iso-c-binding.def"
7181 default:
7182 ; /* Not GFC_STD_* versioned. */
7183 }
7184
7185 switch (i)
7186 {
7187 #define NAMED_FUNCTION(a,b,c,d) \
7188 case a: \
7189 if (a == ISOCBINDING_LOC) \
7190 return_type = c_ptr->n.sym; \
7191 else if (a == ISOCBINDING_FUNLOC) \
7192 return_type = c_funptr->n.sym; \
7193 else \
7194 return_type = NULL; \
7195 create_intrinsic_function (b, a, iso_c_module_name, \
7196 INTMOD_ISO_C_BINDING, false, \
7197 return_type); \
7198 break;
7199 #define NAMED_SUBROUTINE(a,b,c,d) \
7200 case a: \
7201 create_intrinsic_function (b, a, iso_c_module_name, \
7202 INTMOD_ISO_C_BINDING, true, NULL); \
7203 break;
7204 #include "iso-c-binding.def"
7205
7206 case ISOCBINDING_PTR:
7207 case ISOCBINDING_FUNPTR:
7208 /* Already handled above. */
7209 break;
7210 default:
7211 if (i == ISOCBINDING_NULL_PTR)
7212 tmp_symtree = c_ptr;
7213 else if (i == ISOCBINDING_NULL_FUNPTR)
7214 tmp_symtree = c_funptr;
7215 else
7216 tmp_symtree = NULL;
7217 generate_isocbinding_symbol (iso_c_module_name,
7218 (iso_c_binding_symbol) i, NULL,
7219 tmp_symtree, false);
7220 }
7221 }
7222 }
7223
7224 for (u = gfc_rename_list; u; u = u->next)
7225 {
7226 if (u->found)
7227 continue;
7228
7229 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
7230 "module ISO_C_BINDING", u->use_name, &u->where);
7231 }
7232 }
7233
7234
7235 /* Add an integer named constant from a given module. */
7236
7237 static void
7238 create_int_parameter (const char *name, int value, const char *modname,
7239 intmod_id module, int id)
7240 {
7241 gfc_symtree *tmp_symtree;
7242 gfc_symbol *sym;
7243
7244 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
7245 if (tmp_symtree != NULL)
7246 {
7247 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
7248 return;
7249 else
7250 gfc_error ("Symbol %qs already declared", name);
7251 }
7252
7253 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7254 sym = tmp_symtree->n.sym;
7255
7256 sym->module = gfc_get_string ("%s", modname);
7257 sym->attr.flavor = FL_PARAMETER;
7258 sym->ts.type = BT_INTEGER;
7259 sym->ts.kind = gfc_default_integer_kind;
7260 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
7261 sym->attr.use_assoc = 1;
7262 sym->from_intmod = module;
7263 sym->intmod_sym_id = id;
7264 }
7265
7266
7267 /* Value is already contained by the array constructor, but not
7268 yet the shape. */
7269
7270 static void
7271 create_int_parameter_array (const char *name, int size, gfc_expr *value,
7272 const char *modname, intmod_id module, int id)
7273 {
7274 gfc_symtree *tmp_symtree;
7275 gfc_symbol *sym;
7276
7277 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
7278 if (tmp_symtree != NULL)
7279 {
7280 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
7281 return;
7282 else
7283 gfc_error ("Symbol %qs already declared", name);
7284 }
7285
7286 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7287 sym = tmp_symtree->n.sym;
7288
7289 sym->module = gfc_get_string ("%s", modname);
7290 sym->attr.flavor = FL_PARAMETER;
7291 sym->ts.type = BT_INTEGER;
7292 sym->ts.kind = gfc_default_integer_kind;
7293 sym->attr.use_assoc = 1;
7294 sym->from_intmod = module;
7295 sym->intmod_sym_id = id;
7296 sym->attr.dimension = 1;
7297 sym->as = gfc_get_array_spec ();
7298 sym->as->rank = 1;
7299 sym->as->type = AS_EXPLICIT;
7300 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
7301 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
7302
7303 sym->value = value;
7304 sym->value->shape = gfc_get_shape (1);
7305 mpz_init_set_ui (sym->value->shape[0], size);
7306 }
7307
7308
7309 /* Add an derived type for a given module. */
7310
7311 static void
7312 create_derived_type (const char *name, const char *modname,
7313 intmod_id module, int id)
7314 {
7315 gfc_symtree *tmp_symtree;
7316 gfc_symbol *sym, *dt_sym;
7317 gfc_interface *intr, *head;
7318
7319 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
7320 if (tmp_symtree != NULL)
7321 {
7322 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
7323 return;
7324 else
7325 gfc_error ("Symbol %qs already declared", name);
7326 }
7327
7328 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
7329 sym = tmp_symtree->n.sym;
7330 sym->module = gfc_get_string ("%s", modname);
7331 sym->from_intmod = module;
7332 sym->intmod_sym_id = id;
7333 sym->attr.flavor = FL_PROCEDURE;
7334 sym->attr.function = 1;
7335 sym->attr.generic = 1;
7336
7337 gfc_get_sym_tree (gfc_dt_upper_string (sym->name),
7338 gfc_current_ns, &tmp_symtree, false);
7339 dt_sym = tmp_symtree->n.sym;
7340 dt_sym->name = gfc_get_string ("%s", sym->name);
7341 dt_sym->attr.flavor = FL_DERIVED;
7342 dt_sym->attr.private_comp = 1;
7343 dt_sym->attr.zero_comp = 1;
7344 dt_sym->attr.use_assoc = 1;
7345 dt_sym->module = gfc_get_string ("%s", modname);
7346 dt_sym->from_intmod = module;
7347 dt_sym->intmod_sym_id = id;
7348
7349 head = sym->generic;
7350 intr = gfc_get_interface ();
7351 intr->sym = dt_sym;
7352 intr->where = gfc_current_locus;
7353 intr->next = head;
7354 sym->generic = intr;
7355 sym->attr.if_source = IFSRC_DECL;
7356 }
7357
7358
7359 /* Read the contents of the module file into a temporary buffer. */
7360
7361 static void
7362 read_module_to_tmpbuf ()
7363 {
7364 /* We don't know the uncompressed size, so enlarge the buffer as
7365 needed. */
7366 int cursz = 4096;
7367 int rsize = cursz;
7368 int len = 0;
7369
7370 module_content = XNEWVEC (char, cursz);
7371
7372 while (1)
7373 {
7374 int nread = gzread (module_fp, module_content + len, rsize);
7375 len += nread;
7376 if (nread < rsize)
7377 break;
7378 cursz *= 2;
7379 module_content = XRESIZEVEC (char, module_content, cursz);
7380 rsize = cursz - len;
7381 }
7382
7383 module_content = XRESIZEVEC (char, module_content, len + 1);
7384 module_content[len] = '\0';
7385
7386 module_pos = 0;
7387 }
7388
7389
7390 /* USE the ISO_FORTRAN_ENV intrinsic module. */
7391
7392 static void
7393 use_iso_fortran_env_module (void)
7394 {
7395 static char mod[] = "iso_fortran_env";
7396 gfc_use_rename *u;
7397 gfc_symbol *mod_sym;
7398 gfc_symtree *mod_symtree;
7399 gfc_expr *expr;
7400 int i, j;
7401
7402 intmod_sym symbol[] = {
7403 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
7404 #define NAMED_UINTCST(a,b,c,d) { a, b, 0, d },
7405 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
7406 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
7407 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
7408 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
7409 #include "iso-fortran-env.def"
7410 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
7411
7412 /* We could have used c in the NAMED_{,U}INTCST macros
7413 instead of 0, but then current g++ expands the initialization
7414 as clearing the whole object followed by explicit stores of
7415 all the non-zero elements (over 150), while by using 0s for
7416 the non-constant initializers and initializing them afterwards
7417 g++ will often copy everything from .rodata and then only override
7418 over 30 non-constant ones. */
7419 i = 0;
7420 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
7421 #define NAMED_UINTCST(a,b,c,d) symbol[i++].value = c;
7422 #define NAMED_KINDARRAY(a,b,c,d) i++;
7423 #define NAMED_DERIVED_TYPE(a,b,c,d) i++;
7424 #define NAMED_FUNCTION(a,b,c,d) i++;
7425 #define NAMED_SUBROUTINE(a,b,c,d) i++;
7426 #include "iso-fortran-env.def"
7427 gcc_checking_assert (i == (int) ARRAY_SIZE (symbol) - 1);
7428
7429 /* Generate the symbol for the module itself. */
7430 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
7431 if (mod_symtree == NULL)
7432 {
7433 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
7434 gcc_assert (mod_symtree);
7435 mod_sym = mod_symtree->n.sym;
7436
7437 mod_sym->attr.flavor = FL_MODULE;
7438 mod_sym->attr.intrinsic = 1;
7439 mod_sym->module = gfc_get_string ("%s", mod);
7440 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
7441 }
7442 else
7443 if (!mod_symtree->n.sym->attr.intrinsic)
7444 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
7445 "non-intrinsic module name used previously", mod);
7446
7447 /* Generate the symbols for the module integer named constants. */
7448
7449 for (i = 0; symbol[i].name; i++)
7450 {
7451 bool found = false;
7452 for (u = gfc_rename_list; u; u = u->next)
7453 {
7454 if (strcmp (symbol[i].name, u->use_name) == 0)
7455 {
7456 found = true;
7457 u->found = 1;
7458
7459 if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
7460 "referenced at %L, is not in the selected "
7461 "standard", symbol[i].name, &u->where))
7462 continue;
7463
7464 if ((flag_default_integer || flag_default_real_8)
7465 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7466 gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
7467 "constant from intrinsic module "
7468 "ISO_FORTRAN_ENV at %L is incompatible with "
7469 "option %qs", &u->where,
7470 flag_default_integer
7471 ? "-fdefault-integer-8"
7472 : "-fdefault-real-8");
7473 switch (symbol[i].id)
7474 {
7475 #define NAMED_INTCST(a,b,c,d) \
7476 case a:
7477 #include "iso-fortran-env.def"
7478 create_int_parameter (u->local_name[0] ? u->local_name
7479 : u->use_name,
7480 symbol[i].value, mod,
7481 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7482 break;
7483
7484 #define NAMED_UINTCST(a,b,c,d) \
7485 case a:
7486 #include "iso-fortran-env.def"
7487 create_int_parameter (u->local_name[0] ? u->local_name
7488 : u->use_name,
7489 symbol[i].value, mod,
7490 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7491 break;
7492
7493 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7494 case a:\
7495 expr = gfc_get_array_expr (BT_INTEGER, \
7496 gfc_default_integer_kind,\
7497 NULL); \
7498 for (j = 0; KINDS[j].kind != 0; j++) \
7499 gfc_constructor_append_expr (&expr->value.constructor, \
7500 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7501 KINDS[j].kind), NULL); \
7502 create_int_parameter_array (u->local_name[0] ? u->local_name \
7503 : u->use_name, \
7504 j, expr, mod, \
7505 INTMOD_ISO_FORTRAN_ENV, \
7506 symbol[i].id); \
7507 break;
7508 #include "iso-fortran-env.def"
7509
7510 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7511 case a:
7512 #include "iso-fortran-env.def"
7513 create_derived_type (u->local_name[0] ? u->local_name
7514 : u->use_name,
7515 mod, INTMOD_ISO_FORTRAN_ENV,
7516 symbol[i].id);
7517 break;
7518
7519 #define NAMED_FUNCTION(a,b,c,d) \
7520 case a:
7521 #include "iso-fortran-env.def"
7522 create_intrinsic_function (u->local_name[0] ? u->local_name
7523 : u->use_name,
7524 symbol[i].id, mod,
7525 INTMOD_ISO_FORTRAN_ENV, false,
7526 NULL);
7527 break;
7528
7529 default:
7530 gcc_unreachable ();
7531 }
7532 }
7533 }
7534
7535 if (!found && !only_flag)
7536 {
7537 if ((gfc_option.allow_std & symbol[i].standard) == 0)
7538 continue;
7539
7540 if ((flag_default_integer || flag_default_real_8)
7541 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
7542 gfc_warning_now (0,
7543 "Use of the NUMERIC_STORAGE_SIZE named constant "
7544 "from intrinsic module ISO_FORTRAN_ENV at %C is "
7545 "incompatible with option %s",
7546 flag_default_integer
7547 ? "-fdefault-integer-8" : "-fdefault-real-8");
7548
7549 switch (symbol[i].id)
7550 {
7551 #define NAMED_INTCST(a,b,c,d) \
7552 case a:
7553 #include "iso-fortran-env.def"
7554 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7555 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7556 break;
7557
7558 #define NAMED_UINTCST(a,b,c,d) \
7559 case a:
7560 #include "iso-fortran-env.def"
7561 create_int_parameter (symbol[i].name, symbol[i].value, mod,
7562 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
7563 break;
7564
7565 #define NAMED_KINDARRAY(a,b,KINDS,d) \
7566 case a:\
7567 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
7568 NULL); \
7569 for (j = 0; KINDS[j].kind != 0; j++) \
7570 gfc_constructor_append_expr (&expr->value.constructor, \
7571 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
7572 KINDS[j].kind), NULL); \
7573 create_int_parameter_array (symbol[i].name, j, expr, mod, \
7574 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
7575 break;
7576 #include "iso-fortran-env.def"
7577
7578 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
7579 case a:
7580 #include "iso-fortran-env.def"
7581 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
7582 symbol[i].id);
7583 break;
7584
7585 #define NAMED_FUNCTION(a,b,c,d) \
7586 case a:
7587 #include "iso-fortran-env.def"
7588 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
7589 INTMOD_ISO_FORTRAN_ENV, false, NULL);
7590 break;
7591
7592 default:
7593 gcc_unreachable ();
7594 }
7595 }
7596 }
7597
7598 for (u = gfc_rename_list; u; u = u->next)
7599 {
7600 if (u->found)
7601 continue;
7602
7603 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
7604 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
7605 }
7606 }
7607
7608
7609 /* Process a USE directive. */
7610
7611 static void
7612 gfc_use_module (gfc_use_list *module)
7613 {
7614 char *filename;
7615 gfc_state_data *p;
7616 int c, line, start;
7617 gfc_symtree *mod_symtree;
7618 gfc_use_list *use_stmt;
7619 locus old_locus = gfc_current_locus;
7620
7621 gfc_current_locus = module->where;
7622 module_name = module->module_name;
7623 gfc_rename_list = module->rename;
7624 only_flag = module->only_flag;
7625 current_intmod = INTMOD_NONE;
7626
7627 if (!only_flag)
7628 gfc_warning_now (OPT_Wuse_without_only,
7629 "USE statement at %C has no ONLY qualifier");
7630
7631 if (gfc_state_stack->state == COMP_MODULE
7632 || module->submodule_name == NULL)
7633 {
7634 filename = XALLOCAVEC (char, strlen (module_name)
7635 + strlen (MODULE_EXTENSION) + 1);
7636 strcpy (filename, module_name);
7637 strcat (filename, MODULE_EXTENSION);
7638 }
7639 else
7640 {
7641 filename = XALLOCAVEC (char, strlen (module->submodule_name)
7642 + strlen (SUBMODULE_EXTENSION) + 1);
7643 strcpy (filename, module->submodule_name);
7644 strcat (filename, SUBMODULE_EXTENSION);
7645 }
7646
7647 /* First, try to find an non-intrinsic module, unless the USE statement
7648 specified that the module is intrinsic. */
7649 module_fp = NULL;
7650 if (!module->intrinsic)
7651 module_fp = gzopen_included_file (filename, true, true);
7652
7653 /* Then, see if it's an intrinsic one, unless the USE statement
7654 specified that the module is non-intrinsic. */
7655 if (module_fp == NULL && !module->non_intrinsic)
7656 {
7657 if (strcmp (module_name, "iso_fortran_env") == 0
7658 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
7659 "intrinsic module at %C"))
7660 {
7661 use_iso_fortran_env_module ();
7662 free_rename (module->rename);
7663 module->rename = NULL;
7664 gfc_current_locus = old_locus;
7665 module->intrinsic = true;
7666 return;
7667 }
7668
7669 if (strcmp (module_name, "iso_c_binding") == 0
7670 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
7671 {
7672 import_iso_c_binding_module();
7673 free_rename (module->rename);
7674 module->rename = NULL;
7675 gfc_current_locus = old_locus;
7676 module->intrinsic = true;
7677 return;
7678 }
7679
7680 module_fp = gzopen_intrinsic_module (filename);
7681
7682 if (module_fp == NULL && module->intrinsic)
7683 gfc_fatal_error ("Cannot find an intrinsic module named %qs at %C",
7684 module_name);
7685
7686 /* Check for the IEEE modules, so we can mark their symbols
7687 accordingly when we read them. */
7688 if (strcmp (module_name, "ieee_features") == 0
7689 && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
7690 {
7691 current_intmod = INTMOD_IEEE_FEATURES;
7692 }
7693 else if (strcmp (module_name, "ieee_exceptions") == 0
7694 && gfc_notify_std (GFC_STD_F2003,
7695 "IEEE_EXCEPTIONS module at %C"))
7696 {
7697 current_intmod = INTMOD_IEEE_EXCEPTIONS;
7698 }
7699 else if (strcmp (module_name, "ieee_arithmetic") == 0
7700 && gfc_notify_std (GFC_STD_F2003,
7701 "IEEE_ARITHMETIC module at %C"))
7702 {
7703 current_intmod = INTMOD_IEEE_ARITHMETIC;
7704 }
7705 }
7706
7707 if (module_fp == NULL)
7708 {
7709 if (gfc_state_stack->state != COMP_SUBMODULE
7710 && module->submodule_name == NULL)
7711 gfc_fatal_error ("Cannot open module file %qs for reading at %C: %s",
7712 filename, xstrerror (errno));
7713 else
7714 gfc_fatal_error ("Module file %qs has not been generated, either "
7715 "because the module does not contain a MODULE "
7716 "PROCEDURE or there is an error in the module.",
7717 filename);
7718 }
7719
7720 /* Check that we haven't already USEd an intrinsic module with the
7721 same name. */
7722
7723 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
7724 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
7725 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
7726 "intrinsic module name used previously", module_name);
7727
7728 iomode = IO_INPUT;
7729 module_line = 1;
7730 module_column = 1;
7731 start = 0;
7732
7733 read_module_to_tmpbuf ();
7734 gzclose (module_fp);
7735
7736 /* Skip the first line of the module, after checking that this is
7737 a gfortran module file. */
7738 line = 0;
7739 while (line < 1)
7740 {
7741 c = module_char ();
7742 if (c == EOF)
7743 bad_module ("Unexpected end of module");
7744 if (start++ < 3)
7745 parse_name (c);
7746 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
7747 || (start == 2 && strcmp (atom_name, " module") != 0))
7748 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
7749 " module file", module_fullpath);
7750 if (start == 3)
7751 {
7752 bool fatal = false;
7753 if (strcmp (atom_name, " version") != 0
7754 || module_char () != ' '
7755 || parse_atom () != ATOM_STRING)
7756 fatal = true;
7757 else if (strcmp (atom_string, MOD_VERSION))
7758 {
7759 static const char *compat_mod_versions[] = COMPAT_MOD_VERSIONS;
7760 fatal = true;
7761 for (unsigned i = 0; i < ARRAY_SIZE (compat_mod_versions); ++i)
7762 if (!strcmp (atom_string, compat_mod_versions[i]))
7763 {
7764 fatal = false;
7765 break;
7766 }
7767 }
7768 if (fatal)
7769 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
7770 " because it was created by a different"
7771 " version of GNU Fortran", module_fullpath);
7772
7773 free (atom_string);
7774 }
7775
7776 if (c == '\n')
7777 line++;
7778 }
7779
7780 /* Make sure we're not reading the same module that we may be building. */
7781 for (p = gfc_state_stack; p; p = p->previous)
7782 if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
7783 && strcmp (p->sym->name, module_name) == 0)
7784 {
7785 if (p->state == COMP_SUBMODULE)
7786 gfc_fatal_error ("Cannot USE a submodule that is currently built");
7787 else
7788 gfc_fatal_error ("Cannot USE a module that is currently built");
7789 }
7790
7791 init_pi_tree ();
7792 init_true_name_tree ();
7793
7794 read_module ();
7795
7796 free_true_name (true_name_root);
7797 true_name_root = NULL;
7798
7799 free_pi_tree (pi_root);
7800 pi_root = NULL;
7801
7802 XDELETEVEC (module_content);
7803 module_content = NULL;
7804
7805 use_stmt = gfc_get_use_list ();
7806 *use_stmt = *module;
7807 use_stmt->next = gfc_current_ns->use_stmts;
7808 gfc_current_ns->use_stmts = use_stmt;
7809
7810 gfc_current_locus = old_locus;
7811 }
7812
7813
7814 /* Remove duplicated intrinsic operators from the rename list. */
7815
7816 static void
7817 rename_list_remove_duplicate (gfc_use_rename *list)
7818 {
7819 gfc_use_rename *seek, *last;
7820
7821 for (; list; list = list->next)
7822 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
7823 {
7824 last = list;
7825 for (seek = list->next; seek; seek = last->next)
7826 {
7827 if (list->op == seek->op)
7828 {
7829 last->next = seek->next;
7830 free (seek);
7831 }
7832 else
7833 last = seek;
7834 }
7835 }
7836 }
7837
7838
7839 /* Process all USE directives. */
7840
7841 void
7842 gfc_use_modules (void)
7843 {
7844 gfc_use_list *next, *seek, *last;
7845
7846 for (next = module_list; next; next = next->next)
7847 {
7848 bool non_intrinsic = next->non_intrinsic;
7849 bool intrinsic = next->intrinsic;
7850 bool neither = !non_intrinsic && !intrinsic;
7851
7852 for (seek = next->next; seek; seek = seek->next)
7853 {
7854 if (next->module_name != seek->module_name)
7855 continue;
7856
7857 if (seek->non_intrinsic)
7858 non_intrinsic = true;
7859 else if (seek->intrinsic)
7860 intrinsic = true;
7861 else
7862 neither = true;
7863 }
7864
7865 if (intrinsic && neither && !non_intrinsic)
7866 {
7867 char *filename;
7868 FILE *fp;
7869
7870 filename = XALLOCAVEC (char,
7871 strlen (next->module_name)
7872 + strlen (MODULE_EXTENSION) + 1);
7873 strcpy (filename, next->module_name);
7874 strcat (filename, MODULE_EXTENSION);
7875 fp = gfc_open_included_file (filename, true, true);
7876 if (fp != NULL)
7877 {
7878 non_intrinsic = true;
7879 fclose (fp);
7880 }
7881 }
7882
7883 last = next;
7884 for (seek = next->next; seek; seek = last->next)
7885 {
7886 if (next->module_name != seek->module_name)
7887 {
7888 last = seek;
7889 continue;
7890 }
7891
7892 if ((!next->intrinsic && !seek->intrinsic)
7893 || (next->intrinsic && seek->intrinsic)
7894 || !non_intrinsic)
7895 {
7896 if (!seek->only_flag)
7897 next->only_flag = false;
7898 if (seek->rename)
7899 {
7900 gfc_use_rename *r = seek->rename;
7901 while (r->next)
7902 r = r->next;
7903 r->next = next->rename;
7904 next->rename = seek->rename;
7905 }
7906 last->next = seek->next;
7907 free (seek);
7908 }
7909 else
7910 last = seek;
7911 }
7912 }
7913
7914 for (; module_list; module_list = next)
7915 {
7916 next = module_list->next;
7917 rename_list_remove_duplicate (module_list->rename);
7918 gfc_use_module (module_list);
7919 free (module_list);
7920 }
7921 module_list = NULL;
7922 old_module_list_tail = &module_list;
7923 gfc_rename_list = NULL;
7924 }
7925
7926
7927 void
7928 gfc_free_use_stmts (gfc_use_list *use_stmts)
7929 {
7930 gfc_use_list *next;
7931 for (; use_stmts; use_stmts = next)
7932 {
7933 gfc_use_rename *next_rename;
7934
7935 for (; use_stmts->rename; use_stmts->rename = next_rename)
7936 {
7937 next_rename = use_stmts->rename->next;
7938 free (use_stmts->rename);
7939 }
7940 next = use_stmts->next;
7941 free (use_stmts);
7942 }
7943 }
7944
7945
7946 /* Remember the end of the MODULE_LIST list, so that the list can be restored
7947 to its previous state if the current statement is erroneous. */
7948
7949 void
7950 gfc_save_module_list ()
7951 {
7952 gfc_use_list **tail = &module_list;
7953 while (*tail != NULL)
7954 tail = &(*tail)->next;
7955 old_module_list_tail = tail;
7956 }
7957
7958
7959 /* Restore the MODULE_LIST list to its previous value and free the use
7960 statements that are no longer part of the list. */
7961
7962 void
7963 gfc_restore_old_module_list ()
7964 {
7965 gfc_free_use_stmts (*old_module_list_tail);
7966 *old_module_list_tail = NULL;
7967 }
7968
7969
7970 void
7971 gfc_module_init_2 (void)
7972 {
7973 last_atom = ATOM_LPAREN;
7974 gfc_rename_list = NULL;
7975 module_list = NULL;
7976 }
7977
7978
7979 void
7980 gfc_module_done_2 (void)
7981 {
7982 free_rename (gfc_rename_list);
7983 gfc_rename_list = NULL;
7984 }