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