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