]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/module.c
Factor unrelated declarations out of tree.h.
[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-2013 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 flag = e->value.function.esym != NULL;
3362 mio_integer (&flag);
3363 if (flag)
3364 mio_symbol_ref (&e->value.function.esym);
3365 else
3366 write_atom (ATOM_STRING, e->value.function.isym->name);
3367 }
3368 else
3369 {
3370 require_atom (ATOM_STRING);
3371 e->value.function.name = gfc_get_string (atom_string);
3372 free (atom_string);
3373
3374 mio_integer (&flag);
3375 if (flag)
3376 mio_symbol_ref (&e->value.function.esym);
3377 else
3378 {
3379 require_atom (ATOM_STRING);
3380 e->value.function.isym = gfc_find_function (atom_string);
3381 free (atom_string);
3382 }
3383 }
3384
3385 break;
3386
3387 case EXPR_VARIABLE:
3388 mio_symtree_ref (&e->symtree);
3389 mio_ref_list (&e->ref);
3390 break;
3391
3392 case EXPR_SUBSTRING:
3393 e->value.character.string
3394 = CONST_CAST (gfc_char_t *,
3395 mio_allocated_wide_string (e->value.character.string,
3396 e->value.character.length));
3397 mio_ref_list (&e->ref);
3398 break;
3399
3400 case EXPR_STRUCTURE:
3401 case EXPR_ARRAY:
3402 mio_constructor (&e->value.constructor);
3403 mio_shape (&e->shape, e->rank);
3404 break;
3405
3406 case EXPR_CONSTANT:
3407 switch (e->ts.type)
3408 {
3409 case BT_INTEGER:
3410 mio_gmp_integer (&e->value.integer);
3411 break;
3412
3413 case BT_REAL:
3414 gfc_set_model_kind (e->ts.kind);
3415 mio_gmp_real (&e->value.real);
3416 break;
3417
3418 case BT_COMPLEX:
3419 gfc_set_model_kind (e->ts.kind);
3420 mio_gmp_real (&mpc_realref (e->value.complex));
3421 mio_gmp_real (&mpc_imagref (e->value.complex));
3422 break;
3423
3424 case BT_LOGICAL:
3425 mio_integer (&e->value.logical);
3426 break;
3427
3428 case BT_CHARACTER:
3429 mio_integer (&e->value.character.length);
3430 e->value.character.string
3431 = CONST_CAST (gfc_char_t *,
3432 mio_allocated_wide_string (e->value.character.string,
3433 e->value.character.length));
3434 break;
3435
3436 default:
3437 bad_module ("Bad type in constant expression");
3438 }
3439
3440 break;
3441
3442 case EXPR_NULL:
3443 break;
3444
3445 case EXPR_COMPCALL:
3446 case EXPR_PPC:
3447 gcc_unreachable ();
3448 break;
3449 }
3450
3451 mio_rparen ();
3452 }
3453
3454
3455 /* Read and write namelists. */
3456
3457 static void
3458 mio_namelist (gfc_symbol *sym)
3459 {
3460 gfc_namelist *n, *m;
3461 const char *check_name;
3462
3463 mio_lparen ();
3464
3465 if (iomode == IO_OUTPUT)
3466 {
3467 for (n = sym->namelist; n; n = n->next)
3468 mio_symbol_ref (&n->sym);
3469 }
3470 else
3471 {
3472 /* This departure from the standard is flagged as an error.
3473 It does, in fact, work correctly. TODO: Allow it
3474 conditionally? */
3475 if (sym->attr.flavor == FL_NAMELIST)
3476 {
3477 check_name = find_use_name (sym->name, false);
3478 if (check_name && strcmp (check_name, sym->name) != 0)
3479 gfc_error ("Namelist %s cannot be renamed by USE "
3480 "association to %s", sym->name, check_name);
3481 }
3482
3483 m = NULL;
3484 while (peek_atom () != ATOM_RPAREN)
3485 {
3486 n = gfc_get_namelist ();
3487 mio_symbol_ref (&n->sym);
3488
3489 if (sym->namelist == NULL)
3490 sym->namelist = n;
3491 else
3492 m->next = n;
3493
3494 m = n;
3495 }
3496 sym->namelist_tail = m;
3497 }
3498
3499 mio_rparen ();
3500 }
3501
3502
3503 /* Save/restore lists of gfc_interface structures. When loading an
3504 interface, we are really appending to the existing list of
3505 interfaces. Checking for duplicate and ambiguous interfaces has to
3506 be done later when all symbols have been loaded. */
3507
3508 pointer_info *
3509 mio_interface_rest (gfc_interface **ip)
3510 {
3511 gfc_interface *tail, *p;
3512 pointer_info *pi = NULL;
3513
3514 if (iomode == IO_OUTPUT)
3515 {
3516 if (ip != NULL)
3517 for (p = *ip; p; p = p->next)
3518 mio_symbol_ref (&p->sym);
3519 }
3520 else
3521 {
3522 if (*ip == NULL)
3523 tail = NULL;
3524 else
3525 {
3526 tail = *ip;
3527 while (tail->next)
3528 tail = tail->next;
3529 }
3530
3531 for (;;)
3532 {
3533 if (peek_atom () == ATOM_RPAREN)
3534 break;
3535
3536 p = gfc_get_interface ();
3537 p->where = gfc_current_locus;
3538 pi = mio_symbol_ref (&p->sym);
3539
3540 if (tail == NULL)
3541 *ip = p;
3542 else
3543 tail->next = p;
3544
3545 tail = p;
3546 }
3547 }
3548
3549 mio_rparen ();
3550 return pi;
3551 }
3552
3553
3554 /* Save/restore a nameless operator interface. */
3555
3556 static void
3557 mio_interface (gfc_interface **ip)
3558 {
3559 mio_lparen ();
3560 mio_interface_rest (ip);
3561 }
3562
3563
3564 /* Save/restore a named operator interface. */
3565
3566 static void
3567 mio_symbol_interface (const char **name, const char **module,
3568 gfc_interface **ip)
3569 {
3570 mio_lparen ();
3571 mio_pool_string (name);
3572 mio_pool_string (module);
3573 mio_interface_rest (ip);
3574 }
3575
3576
3577 static void
3578 mio_namespace_ref (gfc_namespace **nsp)
3579 {
3580 gfc_namespace *ns;
3581 pointer_info *p;
3582
3583 p = mio_pointer_ref (nsp);
3584
3585 if (p->type == P_UNKNOWN)
3586 p->type = P_NAMESPACE;
3587
3588 if (iomode == IO_INPUT && p->integer != 0)
3589 {
3590 ns = (gfc_namespace *) p->u.pointer;
3591 if (ns == NULL)
3592 {
3593 ns = gfc_get_namespace (NULL, 0);
3594 associate_integer_pointer (p, ns);
3595 }
3596 else
3597 ns->refs++;
3598 }
3599 }
3600
3601
3602 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3603
3604 static gfc_namespace* current_f2k_derived;
3605
3606 static void
3607 mio_typebound_proc (gfc_typebound_proc** proc)
3608 {
3609 int flag;
3610 int overriding_flag;
3611
3612 if (iomode == IO_INPUT)
3613 {
3614 *proc = gfc_get_typebound_proc (NULL);
3615 (*proc)->where = gfc_current_locus;
3616 }
3617 gcc_assert (*proc);
3618
3619 mio_lparen ();
3620
3621 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3622
3623 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3624 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3625 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3626 overriding_flag = mio_name (overriding_flag, binding_overriding);
3627 (*proc)->deferred = ((overriding_flag & 2) != 0);
3628 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3629 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3630
3631 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3632 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3633 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3634
3635 mio_pool_string (&((*proc)->pass_arg));
3636
3637 flag = (int) (*proc)->pass_arg_num;
3638 mio_integer (&flag);
3639 (*proc)->pass_arg_num = (unsigned) flag;
3640
3641 if ((*proc)->is_generic)
3642 {
3643 gfc_tbp_generic* g;
3644 int iop;
3645
3646 mio_lparen ();
3647
3648 if (iomode == IO_OUTPUT)
3649 for (g = (*proc)->u.generic; g; g = g->next)
3650 {
3651 iop = (int) g->is_operator;
3652 mio_integer (&iop);
3653 mio_allocated_string (g->specific_st->name);
3654 }
3655 else
3656 {
3657 (*proc)->u.generic = NULL;
3658 while (peek_atom () != ATOM_RPAREN)
3659 {
3660 gfc_symtree** sym_root;
3661
3662 g = gfc_get_tbp_generic ();
3663 g->specific = NULL;
3664
3665 mio_integer (&iop);
3666 g->is_operator = (bool) iop;
3667
3668 require_atom (ATOM_STRING);
3669 sym_root = &current_f2k_derived->tb_sym_root;
3670 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3671 free (atom_string);
3672
3673 g->next = (*proc)->u.generic;
3674 (*proc)->u.generic = g;
3675 }
3676 }
3677
3678 mio_rparen ();
3679 }
3680 else if (!(*proc)->ppc)
3681 mio_symtree_ref (&(*proc)->u.specific);
3682
3683 mio_rparen ();
3684 }
3685
3686 /* Walker-callback function for this purpose. */
3687 static void
3688 mio_typebound_symtree (gfc_symtree* st)
3689 {
3690 if (iomode == IO_OUTPUT && !st->n.tb)
3691 return;
3692
3693 if (iomode == IO_OUTPUT)
3694 {
3695 mio_lparen ();
3696 mio_allocated_string (st->name);
3697 }
3698 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3699
3700 mio_typebound_proc (&st->n.tb);
3701 mio_rparen ();
3702 }
3703
3704 /* IO a full symtree (in all depth). */
3705 static void
3706 mio_full_typebound_tree (gfc_symtree** root)
3707 {
3708 mio_lparen ();
3709
3710 if (iomode == IO_OUTPUT)
3711 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3712 else
3713 {
3714 while (peek_atom () == ATOM_LPAREN)
3715 {
3716 gfc_symtree* st;
3717
3718 mio_lparen ();
3719
3720 require_atom (ATOM_STRING);
3721 st = gfc_get_tbp_symtree (root, atom_string);
3722 free (atom_string);
3723
3724 mio_typebound_symtree (st);
3725 }
3726 }
3727
3728 mio_rparen ();
3729 }
3730
3731 static void
3732 mio_finalizer (gfc_finalizer **f)
3733 {
3734 if (iomode == IO_OUTPUT)
3735 {
3736 gcc_assert (*f);
3737 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3738 mio_symtree_ref (&(*f)->proc_tree);
3739 }
3740 else
3741 {
3742 *f = gfc_get_finalizer ();
3743 (*f)->where = gfc_current_locus; /* Value should not matter. */
3744 (*f)->next = NULL;
3745
3746 mio_symtree_ref (&(*f)->proc_tree);
3747 (*f)->proc_sym = NULL;
3748 }
3749 }
3750
3751 static void
3752 mio_f2k_derived (gfc_namespace *f2k)
3753 {
3754 current_f2k_derived = f2k;
3755
3756 /* Handle the list of finalizer procedures. */
3757 mio_lparen ();
3758 if (iomode == IO_OUTPUT)
3759 {
3760 gfc_finalizer *f;
3761 for (f = f2k->finalizers; f; f = f->next)
3762 mio_finalizer (&f);
3763 }
3764 else
3765 {
3766 f2k->finalizers = NULL;
3767 while (peek_atom () != ATOM_RPAREN)
3768 {
3769 gfc_finalizer *cur = NULL;
3770 mio_finalizer (&cur);
3771 cur->next = f2k->finalizers;
3772 f2k->finalizers = cur;
3773 }
3774 }
3775 mio_rparen ();
3776
3777 /* Handle type-bound procedures. */
3778 mio_full_typebound_tree (&f2k->tb_sym_root);
3779
3780 /* Type-bound user operators. */
3781 mio_full_typebound_tree (&f2k->tb_uop_root);
3782
3783 /* Type-bound intrinsic operators. */
3784 mio_lparen ();
3785 if (iomode == IO_OUTPUT)
3786 {
3787 int op;
3788 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3789 {
3790 gfc_intrinsic_op realop;
3791
3792 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3793 continue;
3794
3795 mio_lparen ();
3796 realop = (gfc_intrinsic_op) op;
3797 mio_intrinsic_op (&realop);
3798 mio_typebound_proc (&f2k->tb_op[op]);
3799 mio_rparen ();
3800 }
3801 }
3802 else
3803 while (peek_atom () != ATOM_RPAREN)
3804 {
3805 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3806
3807 mio_lparen ();
3808 mio_intrinsic_op (&op);
3809 mio_typebound_proc (&f2k->tb_op[op]);
3810 mio_rparen ();
3811 }
3812 mio_rparen ();
3813 }
3814
3815 static void
3816 mio_full_f2k_derived (gfc_symbol *sym)
3817 {
3818 mio_lparen ();
3819
3820 if (iomode == IO_OUTPUT)
3821 {
3822 if (sym->f2k_derived)
3823 mio_f2k_derived (sym->f2k_derived);
3824 }
3825 else
3826 {
3827 if (peek_atom () != ATOM_RPAREN)
3828 {
3829 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3830 mio_f2k_derived (sym->f2k_derived);
3831 }
3832 else
3833 gcc_assert (!sym->f2k_derived);
3834 }
3835
3836 mio_rparen ();
3837 }
3838
3839
3840 /* Unlike most other routines, the address of the symbol node is already
3841 fixed on input and the name/module has already been filled in. */
3842
3843 static void
3844 mio_symbol (gfc_symbol *sym)
3845 {
3846 int intmod = INTMOD_NONE;
3847
3848 mio_lparen ();
3849
3850 mio_symbol_attribute (&sym->attr);
3851 mio_typespec (&sym->ts);
3852 if (sym->ts.type == BT_CLASS)
3853 sym->attr.class_ok = 1;
3854
3855 if (iomode == IO_OUTPUT)
3856 mio_namespace_ref (&sym->formal_ns);
3857 else
3858 {
3859 mio_namespace_ref (&sym->formal_ns);
3860 if (sym->formal_ns)
3861 sym->formal_ns->proc_name = sym;
3862 }
3863
3864 /* Save/restore common block links. */
3865 mio_symbol_ref (&sym->common_next);
3866
3867 mio_formal_arglist (&sym->formal);
3868
3869 if (sym->attr.flavor == FL_PARAMETER)
3870 mio_expr (&sym->value);
3871
3872 mio_array_spec (&sym->as);
3873
3874 mio_symbol_ref (&sym->result);
3875
3876 if (sym->attr.cray_pointee)
3877 mio_symbol_ref (&sym->cp_pointer);
3878
3879 /* Note that components are always saved, even if they are supposed
3880 to be private. Component access is checked during searching. */
3881
3882 mio_component_list (&sym->components, sym->attr.vtype);
3883
3884 if (sym->components != NULL)
3885 sym->component_access
3886 = MIO_NAME (gfc_access) (sym->component_access, access_types);
3887
3888 /* Load/save the f2k_derived namespace of a derived-type symbol. */
3889 mio_full_f2k_derived (sym);
3890
3891 mio_namelist (sym);
3892
3893 /* Add the fields that say whether this is from an intrinsic module,
3894 and if so, what symbol it is within the module. */
3895 /* mio_integer (&(sym->from_intmod)); */
3896 if (iomode == IO_OUTPUT)
3897 {
3898 intmod = sym->from_intmod;
3899 mio_integer (&intmod);
3900 }
3901 else
3902 {
3903 mio_integer (&intmod);
3904 sym->from_intmod = (intmod_id) intmod;
3905 }
3906
3907 mio_integer (&(sym->intmod_sym_id));
3908
3909 if (sym->attr.flavor == FL_DERIVED)
3910 mio_integer (&(sym->hash_value));
3911
3912 mio_rparen ();
3913 }
3914
3915
3916 /************************* Top level subroutines *************************/
3917
3918 /* Given a root symtree node and a symbol, try to find a symtree that
3919 references the symbol that is not a unique name. */
3920
3921 static gfc_symtree *
3922 find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
3923 {
3924 gfc_symtree *s = NULL;
3925
3926 if (st == NULL)
3927 return s;
3928
3929 s = find_symtree_for_symbol (st->right, sym);
3930 if (s != NULL)
3931 return s;
3932 s = find_symtree_for_symbol (st->left, sym);
3933 if (s != NULL)
3934 return s;
3935
3936 if (st->n.sym == sym && !check_unique_name (st->name))
3937 return st;
3938
3939 return s;
3940 }
3941
3942
3943 /* A recursive function to look for a specific symbol by name and by
3944 module. Whilst several symtrees might point to one symbol, its
3945 is sufficient for the purposes here than one exist. Note that
3946 generic interfaces are distinguished as are symbols that have been
3947 renamed in another module. */
3948 static gfc_symtree *
3949 find_symbol (gfc_symtree *st, const char *name,
3950 const char *module, int generic)
3951 {
3952 int c;
3953 gfc_symtree *retval, *s;
3954
3955 if (st == NULL || st->n.sym == NULL)
3956 return NULL;
3957
3958 c = strcmp (name, st->n.sym->name);
3959 if (c == 0 && st->n.sym->module
3960 && strcmp (module, st->n.sym->module) == 0
3961 && !check_unique_name (st->name))
3962 {
3963 s = gfc_find_symtree (gfc_current_ns->sym_root, name);
3964
3965 /* Detect symbols that are renamed by use association in another
3966 module by the absence of a symtree and null attr.use_rename,
3967 since the latter is not transmitted in the module file. */
3968 if (((!generic && !st->n.sym->attr.generic)
3969 || (generic && st->n.sym->attr.generic))
3970 && !(s == NULL && !st->n.sym->attr.use_rename))
3971 return st;
3972 }
3973
3974 retval = find_symbol (st->left, name, module, generic);
3975
3976 if (retval == NULL)
3977 retval = find_symbol (st->right, name, module, generic);
3978
3979 return retval;
3980 }
3981
3982
3983 /* Skip a list between balanced left and right parens. */
3984
3985 static void
3986 skip_list (void)
3987 {
3988 int level;
3989
3990 level = 0;
3991 do
3992 {
3993 switch (parse_atom ())
3994 {
3995 case ATOM_LPAREN:
3996 level++;
3997 break;
3998
3999 case ATOM_RPAREN:
4000 level--;
4001 break;
4002
4003 case ATOM_STRING:
4004 free (atom_string);
4005 break;
4006
4007 case ATOM_NAME:
4008 case ATOM_INTEGER:
4009 break;
4010 }
4011 }
4012 while (level > 0);
4013 }
4014
4015
4016 /* Load operator interfaces from the module. Interfaces are unusual
4017 in that they attach themselves to existing symbols. */
4018
4019 static void
4020 load_operator_interfaces (void)
4021 {
4022 const char *p;
4023 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4024 gfc_user_op *uop;
4025 pointer_info *pi = NULL;
4026 int n, i;
4027
4028 mio_lparen ();
4029
4030 while (peek_atom () != ATOM_RPAREN)
4031 {
4032 mio_lparen ();
4033
4034 mio_internal_string (name);
4035 mio_internal_string (module);
4036
4037 n = number_use_names (name, true);
4038 n = n ? n : 1;
4039
4040 for (i = 1; i <= n; i++)
4041 {
4042 /* Decide if we need to load this one or not. */
4043 p = find_use_name_n (name, &i, true);
4044
4045 if (p == NULL)
4046 {
4047 while (parse_atom () != ATOM_RPAREN);
4048 continue;
4049 }
4050
4051 if (i == 1)
4052 {
4053 uop = gfc_get_uop (p);
4054 pi = mio_interface_rest (&uop->op);
4055 }
4056 else
4057 {
4058 if (gfc_find_uop (p, NULL))
4059 continue;
4060 uop = gfc_get_uop (p);
4061 uop->op = gfc_get_interface ();
4062 uop->op->where = gfc_current_locus;
4063 add_fixup (pi->integer, &uop->op->sym);
4064 }
4065 }
4066 }
4067
4068 mio_rparen ();
4069 }
4070
4071
4072 /* Load interfaces from the module. Interfaces are unusual in that
4073 they attach themselves to existing symbols. */
4074
4075 static void
4076 load_generic_interfaces (void)
4077 {
4078 const char *p;
4079 char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
4080 gfc_symbol *sym;
4081 gfc_interface *generic = NULL, *gen = NULL;
4082 int n, i, renamed;
4083 bool ambiguous_set = false;
4084
4085 mio_lparen ();
4086
4087 while (peek_atom () != ATOM_RPAREN)
4088 {
4089 mio_lparen ();
4090
4091 mio_internal_string (name);
4092 mio_internal_string (module);
4093
4094 n = number_use_names (name, false);
4095 renamed = n ? 1 : 0;
4096 n = n ? n : 1;
4097
4098 for (i = 1; i <= n; i++)
4099 {
4100 gfc_symtree *st;
4101 /* Decide if we need to load this one or not. */
4102 p = find_use_name_n (name, &i, false);
4103
4104 st = find_symbol (gfc_current_ns->sym_root,
4105 name, module_name, 1);
4106
4107 if (!p || gfc_find_symbol (p, NULL, 0, &sym))
4108 {
4109 /* Skip the specific names for these cases. */
4110 while (i == 1 && parse_atom () != ATOM_RPAREN);
4111
4112 continue;
4113 }
4114
4115 /* If the symbol exists already and is being USEd without being
4116 in an ONLY clause, do not load a new symtree(11.3.2). */
4117 if (!only_flag && st)
4118 sym = st->n.sym;
4119
4120 if (!sym)
4121 {
4122 if (st)
4123 {
4124 sym = st->n.sym;
4125 if (strcmp (st->name, p) != 0)
4126 {
4127 st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
4128 st->n.sym = sym;
4129 sym->refs++;
4130 }
4131 }
4132
4133 /* Since we haven't found a valid generic interface, we had
4134 better make one. */
4135 if (!sym)
4136 {
4137 gfc_get_symbol (p, NULL, &sym);
4138 sym->name = gfc_get_string (name);
4139 sym->module = module_name;
4140 sym->attr.flavor = FL_PROCEDURE;
4141 sym->attr.generic = 1;
4142 sym->attr.use_assoc = 1;
4143 }
4144 }
4145 else
4146 {
4147 /* Unless sym is a generic interface, this reference
4148 is ambiguous. */
4149 if (st == NULL)
4150 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4151
4152 sym = st->n.sym;
4153
4154 if (st && !sym->attr.generic
4155 && !st->ambiguous
4156 && sym->module
4157 && strcmp (module, sym->module))
4158 {
4159 ambiguous_set = true;
4160 st->ambiguous = 1;
4161 }
4162 }
4163
4164 sym->attr.use_only = only_flag;
4165 sym->attr.use_rename = renamed;
4166
4167 if (i == 1)
4168 {
4169 mio_interface_rest (&sym->generic);
4170 generic = sym->generic;
4171 }
4172 else if (!sym->generic)
4173 {
4174 sym->generic = generic;
4175 sym->attr.generic_copy = 1;
4176 }
4177
4178 /* If a procedure that is not generic has generic interfaces
4179 that include itself, it is generic! We need to take care
4180 to retain symbols ambiguous that were already so. */
4181 if (sym->attr.use_assoc
4182 && !sym->attr.generic
4183 && sym->attr.flavor == FL_PROCEDURE)
4184 {
4185 for (gen = generic; gen; gen = gen->next)
4186 {
4187 if (gen->sym == sym)
4188 {
4189 sym->attr.generic = 1;
4190 if (ambiguous_set)
4191 st->ambiguous = 0;
4192 break;
4193 }
4194 }
4195 }
4196
4197 }
4198 }
4199
4200 mio_rparen ();
4201 }
4202
4203
4204 /* Load common blocks. */
4205
4206 static void
4207 load_commons (void)
4208 {
4209 char name[GFC_MAX_SYMBOL_LEN + 1];
4210 gfc_common_head *p;
4211
4212 mio_lparen ();
4213
4214 while (peek_atom () != ATOM_RPAREN)
4215 {
4216 int flags;
4217 char* label;
4218 mio_lparen ();
4219 mio_internal_string (name);
4220
4221 p = gfc_get_common (name, 1);
4222
4223 mio_symbol_ref (&p->head);
4224 mio_integer (&flags);
4225 if (flags & 1)
4226 p->saved = 1;
4227 if (flags & 2)
4228 p->threadprivate = 1;
4229 p->use_assoc = 1;
4230
4231 /* Get whether this was a bind(c) common or not. */
4232 mio_integer (&p->is_bind_c);
4233 /* Get the binding label. */
4234 label = read_string ();
4235 if (strlen (label))
4236 p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
4237 XDELETEVEC (label);
4238
4239 mio_rparen ();
4240 }
4241
4242 mio_rparen ();
4243 }
4244
4245
4246 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4247 so that unused variables are not loaded and so that the expression can
4248 be safely freed. */
4249
4250 static void
4251 load_equiv (void)
4252 {
4253 gfc_equiv *head, *tail, *end, *eq;
4254 bool unused;
4255
4256 mio_lparen ();
4257 in_load_equiv = true;
4258
4259 end = gfc_current_ns->equiv;
4260 while (end != NULL && end->next != NULL)
4261 end = end->next;
4262
4263 while (peek_atom () != ATOM_RPAREN) {
4264 mio_lparen ();
4265 head = tail = NULL;
4266
4267 while(peek_atom () != ATOM_RPAREN)
4268 {
4269 if (head == NULL)
4270 head = tail = gfc_get_equiv ();
4271 else
4272 {
4273 tail->eq = gfc_get_equiv ();
4274 tail = tail->eq;
4275 }
4276
4277 mio_pool_string (&tail->module);
4278 mio_expr (&tail->expr);
4279 }
4280
4281 /* Unused equivalence members have a unique name. In addition, it
4282 must be checked that the symbols are from the same module. */
4283 unused = true;
4284 for (eq = head; eq; eq = eq->eq)
4285 {
4286 if (eq->expr->symtree->n.sym->module
4287 && head->expr->symtree->n.sym->module
4288 && strcmp (head->expr->symtree->n.sym->module,
4289 eq->expr->symtree->n.sym->module) == 0
4290 && !check_unique_name (eq->expr->symtree->name))
4291 {
4292 unused = false;
4293 break;
4294 }
4295 }
4296
4297 if (unused)
4298 {
4299 for (eq = head; eq; eq = head)
4300 {
4301 head = eq->eq;
4302 gfc_free_expr (eq->expr);
4303 free (eq);
4304 }
4305 }
4306
4307 if (end == NULL)
4308 gfc_current_ns->equiv = head;
4309 else
4310 end->next = head;
4311
4312 if (head != NULL)
4313 end = head;
4314
4315 mio_rparen ();
4316 }
4317
4318 mio_rparen ();
4319 in_load_equiv = false;
4320 }
4321
4322
4323 /* This function loads the sym_root of f2k_derived with the extensions to
4324 the derived type. */
4325 static void
4326 load_derived_extensions (void)
4327 {
4328 int symbol, j;
4329 gfc_symbol *derived;
4330 gfc_symbol *dt;
4331 gfc_symtree *st;
4332 pointer_info *info;
4333 char name[GFC_MAX_SYMBOL_LEN + 1];
4334 char module[GFC_MAX_SYMBOL_LEN + 1];
4335 const char *p;
4336
4337 mio_lparen ();
4338 while (peek_atom () != ATOM_RPAREN)
4339 {
4340 mio_lparen ();
4341 mio_integer (&symbol);
4342 info = get_integer (symbol);
4343 derived = info->u.rsym.sym;
4344
4345 /* This one is not being loaded. */
4346 if (!info || !derived)
4347 {
4348 while (peek_atom () != ATOM_RPAREN)
4349 skip_list ();
4350 continue;
4351 }
4352
4353 gcc_assert (derived->attr.flavor == FL_DERIVED);
4354 if (derived->f2k_derived == NULL)
4355 derived->f2k_derived = gfc_get_namespace (NULL, 0);
4356
4357 while (peek_atom () != ATOM_RPAREN)
4358 {
4359 mio_lparen ();
4360 mio_internal_string (name);
4361 mio_internal_string (module);
4362
4363 /* Only use one use name to find the symbol. */
4364 j = 1;
4365 p = find_use_name_n (name, &j, false);
4366 if (p)
4367 {
4368 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4369 dt = st->n.sym;
4370 st = gfc_find_symtree (derived->f2k_derived->sym_root, name);
4371 if (st == NULL)
4372 {
4373 /* Only use the real name in f2k_derived to ensure a single
4374 symtree. */
4375 st = gfc_new_symtree (&derived->f2k_derived->sym_root, name);
4376 st->n.sym = dt;
4377 st->n.sym->refs++;
4378 }
4379 }
4380 mio_rparen ();
4381 }
4382 mio_rparen ();
4383 }
4384 mio_rparen ();
4385 }
4386
4387
4388 /* Recursive function to traverse the pointer_info tree and load a
4389 needed symbol. We return nonzero if we load a symbol and stop the
4390 traversal, because the act of loading can alter the tree. */
4391
4392 static int
4393 load_needed (pointer_info *p)
4394 {
4395 gfc_namespace *ns;
4396 pointer_info *q;
4397 gfc_symbol *sym;
4398 int rv;
4399
4400 rv = 0;
4401 if (p == NULL)
4402 return rv;
4403
4404 rv |= load_needed (p->left);
4405 rv |= load_needed (p->right);
4406
4407 if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
4408 return rv;
4409
4410 p->u.rsym.state = USED;
4411
4412 set_module_locus (&p->u.rsym.where);
4413
4414 sym = p->u.rsym.sym;
4415 if (sym == NULL)
4416 {
4417 q = get_integer (p->u.rsym.ns);
4418
4419 ns = (gfc_namespace *) q->u.pointer;
4420 if (ns == NULL)
4421 {
4422 /* Create an interface namespace if necessary. These are
4423 the namespaces that hold the formal parameters of module
4424 procedures. */
4425
4426 ns = gfc_get_namespace (NULL, 0);
4427 associate_integer_pointer (q, ns);
4428 }
4429
4430 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4431 doesn't go pear-shaped if the symbol is used. */
4432 if (!ns->proc_name)
4433 gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
4434 1, &ns->proc_name);
4435
4436 sym = gfc_new_symbol (p->u.rsym.true_name, ns);
4437 sym->name = dt_lower_string (p->u.rsym.true_name);
4438 sym->module = gfc_get_string (p->u.rsym.module);
4439 if (p->u.rsym.binding_label)
4440 sym->binding_label = IDENTIFIER_POINTER (get_identifier
4441 (p->u.rsym.binding_label));
4442
4443 associate_integer_pointer (p, sym);
4444 }
4445
4446 mio_symbol (sym);
4447 sym->attr.use_assoc = 1;
4448
4449 /* Mark as only or rename for later diagnosis for explicitly imported
4450 but not used warnings; don't mark internal symbols such as __vtab,
4451 __def_init etc. Only mark them if they have been explicitly loaded. */
4452
4453 if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
4454 {
4455 gfc_use_rename *u;
4456
4457 /* Search the use/rename list for the variable; if the variable is
4458 found, mark it. */
4459 for (u = gfc_rename_list; u; u = u->next)
4460 {
4461 if (strcmp (u->use_name, sym->name) == 0)
4462 {
4463 sym->attr.use_only = 1;
4464 break;
4465 }
4466 }
4467 }
4468
4469 if (p->u.rsym.renamed)
4470 sym->attr.use_rename = 1;
4471
4472 return 1;
4473 }
4474
4475
4476 /* Recursive function for cleaning up things after a module has been read. */
4477
4478 static void
4479 read_cleanup (pointer_info *p)
4480 {
4481 gfc_symtree *st;
4482 pointer_info *q;
4483
4484 if (p == NULL)
4485 return;
4486
4487 read_cleanup (p->left);
4488 read_cleanup (p->right);
4489
4490 if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
4491 {
4492 gfc_namespace *ns;
4493 /* Add hidden symbols to the symtree. */
4494 q = get_integer (p->u.rsym.ns);
4495 ns = (gfc_namespace *) q->u.pointer;
4496
4497 if (!p->u.rsym.sym->attr.vtype
4498 && !p->u.rsym.sym->attr.vtab)
4499 st = gfc_get_unique_symtree (ns);
4500 else
4501 {
4502 /* There is no reason to use 'unique_symtrees' for vtabs or
4503 vtypes - their name is fine for a symtree and reduces the
4504 namespace pollution. */
4505 st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
4506 if (!st)
4507 st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
4508 }
4509
4510 st->n.sym = p->u.rsym.sym;
4511 st->n.sym->refs++;
4512
4513 /* Fixup any symtree references. */
4514 p->u.rsym.symtree = st;
4515 resolve_fixups (p->u.rsym.stfixup, st);
4516 p->u.rsym.stfixup = NULL;
4517 }
4518
4519 /* Free unused symbols. */
4520 if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
4521 gfc_free_symbol (p->u.rsym.sym);
4522 }
4523
4524
4525 /* It is not quite enough to check for ambiguity in the symbols by
4526 the loaded symbol and the new symbol not being identical. */
4527 static bool
4528 check_for_ambiguous (gfc_symbol *st_sym, pointer_info *info)
4529 {
4530 gfc_symbol *rsym;
4531 module_locus locus;
4532 symbol_attribute attr;
4533
4534 if (gfc_current_ns->proc_name && st_sym->name == gfc_current_ns->proc_name->name)
4535 {
4536 gfc_error ("'%s' of module '%s', imported at %C, is also the name of the "
4537 "current program unit", st_sym->name, module_name);
4538 return true;
4539 }
4540
4541 rsym = info->u.rsym.sym;
4542 if (st_sym == rsym)
4543 return false;
4544
4545 if (st_sym->attr.vtab || st_sym->attr.vtype)
4546 return false;
4547
4548 /* If the existing symbol is generic from a different module and
4549 the new symbol is generic there can be no ambiguity. */
4550 if (st_sym->attr.generic
4551 && st_sym->module
4552 && st_sym->module != module_name)
4553 {
4554 /* The new symbol's attributes have not yet been read. Since
4555 we need attr.generic, read it directly. */
4556 get_module_locus (&locus);
4557 set_module_locus (&info->u.rsym.where);
4558 mio_lparen ();
4559 attr.generic = 0;
4560 mio_symbol_attribute (&attr);
4561 set_module_locus (&locus);
4562 if (attr.generic)
4563 return false;
4564 }
4565
4566 return true;
4567 }
4568
4569
4570 /* Read a module file. */
4571
4572 static void
4573 read_module (void)
4574 {
4575 module_locus operator_interfaces, user_operators, extensions;
4576 const char *p;
4577 char name[GFC_MAX_SYMBOL_LEN + 1];
4578 int i;
4579 int ambiguous, j, nuse, symbol;
4580 pointer_info *info, *q;
4581 gfc_use_rename *u = NULL;
4582 gfc_symtree *st;
4583 gfc_symbol *sym;
4584
4585 get_module_locus (&operator_interfaces); /* Skip these for now. */
4586 skip_list ();
4587
4588 get_module_locus (&user_operators);
4589 skip_list ();
4590 skip_list ();
4591
4592 /* Skip commons, equivalences and derived type extensions for now. */
4593 skip_list ();
4594 skip_list ();
4595
4596 get_module_locus (&extensions);
4597 skip_list ();
4598
4599 mio_lparen ();
4600
4601 /* Create the fixup nodes for all the symbols. */
4602
4603 while (peek_atom () != ATOM_RPAREN)
4604 {
4605 char* bind_label;
4606 require_atom (ATOM_INTEGER);
4607 info = get_integer (atom_int);
4608
4609 info->type = P_SYMBOL;
4610 info->u.rsym.state = UNUSED;
4611
4612 info->u.rsym.true_name = read_string ();
4613 info->u.rsym.module = read_string ();
4614 bind_label = read_string ();
4615 if (strlen (bind_label))
4616 info->u.rsym.binding_label = bind_label;
4617 else
4618 XDELETEVEC (bind_label);
4619
4620 require_atom (ATOM_INTEGER);
4621 info->u.rsym.ns = atom_int;
4622
4623 get_module_locus (&info->u.rsym.where);
4624 skip_list ();
4625
4626 /* See if the symbol has already been loaded by a previous module.
4627 If so, we reference the existing symbol and prevent it from
4628 being loaded again. This should not happen if the symbol being
4629 read is an index for an assumed shape dummy array (ns != 1). */
4630
4631 sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
4632
4633 if (sym == NULL
4634 || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
4635 continue;
4636
4637 info->u.rsym.state = USED;
4638 info->u.rsym.sym = sym;
4639
4640 /* Some symbols do not have a namespace (eg. formal arguments),
4641 so the automatic "unique symtree" mechanism must be suppressed
4642 by marking them as referenced. */
4643 q = get_integer (info->u.rsym.ns);
4644 if (q->u.pointer == NULL)
4645 {
4646 info->u.rsym.referenced = 1;
4647 continue;
4648 }
4649
4650 /* If possible recycle the symtree that references the symbol.
4651 If a symtree is not found and the module does not import one,
4652 a unique-name symtree is found by read_cleanup. */
4653 st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
4654 if (st != NULL)
4655 {
4656 info->u.rsym.symtree = st;
4657 info->u.rsym.referenced = 1;
4658 }
4659 }
4660
4661 mio_rparen ();
4662
4663 /* Parse the symtree lists. This lets us mark which symbols need to
4664 be loaded. Renaming is also done at this point by replacing the
4665 symtree name. */
4666
4667 mio_lparen ();
4668
4669 while (peek_atom () != ATOM_RPAREN)
4670 {
4671 mio_internal_string (name);
4672 mio_integer (&ambiguous);
4673 mio_integer (&symbol);
4674
4675 info = get_integer (symbol);
4676
4677 /* See how many use names there are. If none, go through the start
4678 of the loop at least once. */
4679 nuse = number_use_names (name, false);
4680 info->u.rsym.renamed = nuse ? 1 : 0;
4681
4682 if (nuse == 0)
4683 nuse = 1;
4684
4685 for (j = 1; j <= nuse; j++)
4686 {
4687 /* Get the jth local name for this symbol. */
4688 p = find_use_name_n (name, &j, false);
4689
4690 if (p == NULL && strcmp (name, module_name) == 0)
4691 p = name;
4692
4693 /* Exception: Always import vtabs & vtypes. */
4694 if (p == NULL && name[0] == '_'
4695 && (strncmp (name, "__vtab_", 5) == 0
4696 || strncmp (name, "__vtype_", 6) == 0))
4697 p = name;
4698
4699 /* Skip symtree nodes not in an ONLY clause, unless there
4700 is an existing symtree loaded from another USE statement. */
4701 if (p == NULL)
4702 {
4703 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4704 if (st != NULL
4705 && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
4706 && st->n.sym->module != NULL
4707 && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
4708 {
4709 info->u.rsym.symtree = st;
4710 info->u.rsym.sym = st->n.sym;
4711 }
4712 continue;
4713 }
4714
4715 /* If a symbol of the same name and module exists already,
4716 this symbol, which is not in an ONLY clause, must not be
4717 added to the namespace(11.3.2). Note that find_symbol
4718 only returns the first occurrence that it finds. */
4719 if (!only_flag && !info->u.rsym.renamed
4720 && strcmp (name, module_name) != 0
4721 && find_symbol (gfc_current_ns->sym_root, name,
4722 module_name, 0))
4723 continue;
4724
4725 st = gfc_find_symtree (gfc_current_ns->sym_root, p);
4726
4727 if (st != NULL)
4728 {
4729 /* Check for ambiguous symbols. */
4730 if (check_for_ambiguous (st->n.sym, info))
4731 st->ambiguous = 1;
4732 else
4733 info->u.rsym.symtree = st;
4734 }
4735 else
4736 {
4737 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
4738
4739 /* Create a symtree node in the current namespace for this
4740 symbol. */
4741 st = check_unique_name (p)
4742 ? gfc_get_unique_symtree (gfc_current_ns)
4743 : gfc_new_symtree (&gfc_current_ns->sym_root, p);
4744 st->ambiguous = ambiguous;
4745
4746 sym = info->u.rsym.sym;
4747
4748 /* Create a symbol node if it doesn't already exist. */
4749 if (sym == NULL)
4750 {
4751 info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
4752 gfc_current_ns);
4753 info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
4754 sym = info->u.rsym.sym;
4755 sym->module = gfc_get_string (info->u.rsym.module);
4756
4757 if (info->u.rsym.binding_label)
4758 sym->binding_label =
4759 IDENTIFIER_POINTER (get_identifier
4760 (info->u.rsym.binding_label));
4761 }
4762
4763 st->n.sym = sym;
4764 st->n.sym->refs++;
4765
4766 if (strcmp (name, p) != 0)
4767 sym->attr.use_rename = 1;
4768
4769 if (name[0] != '_'
4770 || (strncmp (name, "__vtab_", 5) != 0
4771 && strncmp (name, "__vtype_", 6) != 0))
4772 sym->attr.use_only = only_flag;
4773
4774 /* Store the symtree pointing to this symbol. */
4775 info->u.rsym.symtree = st;
4776
4777 if (info->u.rsym.state == UNUSED)
4778 info->u.rsym.state = NEEDED;
4779 info->u.rsym.referenced = 1;
4780 }
4781 }
4782 }
4783
4784 mio_rparen ();
4785
4786 /* Load intrinsic operator interfaces. */
4787 set_module_locus (&operator_interfaces);
4788 mio_lparen ();
4789
4790 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
4791 {
4792 if (i == INTRINSIC_USER)
4793 continue;
4794
4795 if (only_flag)
4796 {
4797 u = find_use_operator ((gfc_intrinsic_op) i);
4798
4799 if (u == NULL)
4800 {
4801 skip_list ();
4802 continue;
4803 }
4804
4805 u->found = 1;
4806 }
4807
4808 mio_interface (&gfc_current_ns->op[i]);
4809 if (u && !gfc_current_ns->op[i])
4810 u->found = 0;
4811 }
4812
4813 mio_rparen ();
4814
4815 /* Load generic and user operator interfaces. These must follow the
4816 loading of symtree because otherwise symbols can be marked as
4817 ambiguous. */
4818
4819 set_module_locus (&user_operators);
4820
4821 load_operator_interfaces ();
4822 load_generic_interfaces ();
4823
4824 load_commons ();
4825 load_equiv ();
4826
4827 /* At this point, we read those symbols that are needed but haven't
4828 been loaded yet. If one symbol requires another, the other gets
4829 marked as NEEDED if its previous state was UNUSED. */
4830
4831 while (load_needed (pi_root));
4832
4833 /* Make sure all elements of the rename-list were found in the module. */
4834
4835 for (u = gfc_rename_list; u; u = u->next)
4836 {
4837 if (u->found)
4838 continue;
4839
4840 if (u->op == INTRINSIC_NONE)
4841 {
4842 gfc_error ("Symbol '%s' referenced at %L not found in module '%s'",
4843 u->use_name, &u->where, module_name);
4844 continue;
4845 }
4846
4847 if (u->op == INTRINSIC_USER)
4848 {
4849 gfc_error ("User operator '%s' referenced at %L not found "
4850 "in module '%s'", u->use_name, &u->where, module_name);
4851 continue;
4852 }
4853
4854 gfc_error ("Intrinsic operator '%s' referenced at %L not found "
4855 "in module '%s'", gfc_op2string (u->op), &u->where,
4856 module_name);
4857 }
4858
4859 /* Now we should be in a position to fill f2k_derived with derived type
4860 extensions, since everything has been loaded. */
4861 set_module_locus (&extensions);
4862 load_derived_extensions ();
4863
4864 /* Clean up symbol nodes that were never loaded, create references
4865 to hidden symbols. */
4866
4867 read_cleanup (pi_root);
4868 }
4869
4870
4871 /* Given an access type that is specific to an entity and the default
4872 access, return nonzero if the entity is publicly accessible. If the
4873 element is declared as PUBLIC, then it is public; if declared
4874 PRIVATE, then private, and otherwise it is public unless the default
4875 access in this context has been declared PRIVATE. */
4876
4877 static bool
4878 check_access (gfc_access specific_access, gfc_access default_access)
4879 {
4880 if (specific_access == ACCESS_PUBLIC)
4881 return TRUE;
4882 if (specific_access == ACCESS_PRIVATE)
4883 return FALSE;
4884
4885 if (gfc_option.flag_module_private)
4886 return default_access == ACCESS_PUBLIC;
4887 else
4888 return default_access != ACCESS_PRIVATE;
4889 }
4890
4891
4892 bool
4893 gfc_check_symbol_access (gfc_symbol *sym)
4894 {
4895 if (sym->attr.vtab || sym->attr.vtype)
4896 return true;
4897 else
4898 return check_access (sym->attr.access, sym->ns->default_access);
4899 }
4900
4901
4902 /* A structure to remember which commons we've already written. */
4903
4904 struct written_common
4905 {
4906 BBT_HEADER(written_common);
4907 const char *name, *label;
4908 };
4909
4910 static struct written_common *written_commons = NULL;
4911
4912 /* Comparison function used for balancing the binary tree. */
4913
4914 static int
4915 compare_written_commons (void *a1, void *b1)
4916 {
4917 const char *aname = ((struct written_common *) a1)->name;
4918 const char *alabel = ((struct written_common *) a1)->label;
4919 const char *bname = ((struct written_common *) b1)->name;
4920 const char *blabel = ((struct written_common *) b1)->label;
4921 int c = strcmp (aname, bname);
4922
4923 return (c != 0 ? c : strcmp (alabel, blabel));
4924 }
4925
4926 /* Free a list of written commons. */
4927
4928 static void
4929 free_written_common (struct written_common *w)
4930 {
4931 if (!w)
4932 return;
4933
4934 if (w->left)
4935 free_written_common (w->left);
4936 if (w->right)
4937 free_written_common (w->right);
4938
4939 free (w);
4940 }
4941
4942 /* Write a common block to the module -- recursive helper function. */
4943
4944 static void
4945 write_common_0 (gfc_symtree *st, bool this_module)
4946 {
4947 gfc_common_head *p;
4948 const char * name;
4949 int flags;
4950 const char *label;
4951 struct written_common *w;
4952 bool write_me = true;
4953
4954 if (st == NULL)
4955 return;
4956
4957 write_common_0 (st->left, this_module);
4958
4959 /* We will write out the binding label, or "" if no label given. */
4960 name = st->n.common->name;
4961 p = st->n.common;
4962 label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
4963
4964 /* Check if we've already output this common. */
4965 w = written_commons;
4966 while (w)
4967 {
4968 int c = strcmp (name, w->name);
4969 c = (c != 0 ? c : strcmp (label, w->label));
4970 if (c == 0)
4971 write_me = false;
4972
4973 w = (c < 0) ? w->left : w->right;
4974 }
4975
4976 if (this_module && p->use_assoc)
4977 write_me = false;
4978
4979 if (write_me)
4980 {
4981 /* Write the common to the module. */
4982 mio_lparen ();
4983 mio_pool_string (&name);
4984
4985 mio_symbol_ref (&p->head);
4986 flags = p->saved ? 1 : 0;
4987 if (p->threadprivate)
4988 flags |= 2;
4989 mio_integer (&flags);
4990
4991 /* Write out whether the common block is bind(c) or not. */
4992 mio_integer (&(p->is_bind_c));
4993
4994 mio_pool_string (&label);
4995 mio_rparen ();
4996
4997 /* Record that we have written this common. */
4998 w = XCNEW (struct written_common);
4999 w->name = p->name;
5000 w->label = label;
5001 gfc_insert_bbt (&written_commons, w, compare_written_commons);
5002 }
5003
5004 write_common_0 (st->right, this_module);
5005 }
5006
5007
5008 /* Write a common, by initializing the list of written commons, calling
5009 the recursive function write_common_0() and cleaning up afterwards. */
5010
5011 static void
5012 write_common (gfc_symtree *st)
5013 {
5014 written_commons = NULL;
5015 write_common_0 (st, true);
5016 write_common_0 (st, false);
5017 free_written_common (written_commons);
5018 written_commons = NULL;
5019 }
5020
5021
5022 /* Write the blank common block to the module. */
5023
5024 static void
5025 write_blank_common (void)
5026 {
5027 const char * name = BLANK_COMMON_NAME;
5028 int saved;
5029 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5030 this, but it hasn't been checked. Just making it so for now. */
5031 int is_bind_c = 0;
5032
5033 if (gfc_current_ns->blank_common.head == NULL)
5034 return;
5035
5036 mio_lparen ();
5037
5038 mio_pool_string (&name);
5039
5040 mio_symbol_ref (&gfc_current_ns->blank_common.head);
5041 saved = gfc_current_ns->blank_common.saved;
5042 mio_integer (&saved);
5043
5044 /* Write out whether the common block is bind(c) or not. */
5045 mio_integer (&is_bind_c);
5046
5047 /* Write out an empty binding label. */
5048 write_atom (ATOM_STRING, "");
5049
5050 mio_rparen ();
5051 }
5052
5053
5054 /* Write equivalences to the module. */
5055
5056 static void
5057 write_equiv (void)
5058 {
5059 gfc_equiv *eq, *e;
5060 int num;
5061
5062 num = 0;
5063 for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
5064 {
5065 mio_lparen ();
5066
5067 for (e = eq; e; e = e->eq)
5068 {
5069 if (e->module == NULL)
5070 e->module = gfc_get_string ("%s.eq.%d", module_name, num);
5071 mio_allocated_string (e->module);
5072 mio_expr (&e->expr);
5073 }
5074
5075 num++;
5076 mio_rparen ();
5077 }
5078 }
5079
5080
5081 /* Write derived type extensions to the module. */
5082
5083 static void
5084 write_dt_extensions (gfc_symtree *st)
5085 {
5086 if (!gfc_check_symbol_access (st->n.sym))
5087 return;
5088 if (!(st->n.sym->ns && st->n.sym->ns->proc_name
5089 && st->n.sym->ns->proc_name->attr.flavor == FL_MODULE))
5090 return;
5091
5092 mio_lparen ();
5093 mio_pool_string (&st->name);
5094 if (st->n.sym->module != NULL)
5095 mio_pool_string (&st->n.sym->module);
5096 else
5097 {
5098 char name[GFC_MAX_SYMBOL_LEN + 1];
5099 if (iomode == IO_OUTPUT)
5100 strcpy (name, module_name);
5101 mio_internal_string (name);
5102 if (iomode == IO_INPUT)
5103 module_name = gfc_get_string (name);
5104 }
5105 mio_rparen ();
5106 }
5107
5108 static void
5109 write_derived_extensions (gfc_symtree *st)
5110 {
5111 if (!((st->n.sym->attr.flavor == FL_DERIVED)
5112 && (st->n.sym->f2k_derived != NULL)
5113 && (st->n.sym->f2k_derived->sym_root != NULL)))
5114 return;
5115
5116 mio_lparen ();
5117 mio_symbol_ref (&(st->n.sym));
5118 gfc_traverse_symtree (st->n.sym->f2k_derived->sym_root,
5119 write_dt_extensions);
5120 mio_rparen ();
5121 }
5122
5123
5124 /* Write a symbol to the module. */
5125
5126 static void
5127 write_symbol (int n, gfc_symbol *sym)
5128 {
5129 const char *label;
5130
5131 if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
5132 gfc_internal_error ("write_symbol(): bad module symbol '%s'", sym->name);
5133
5134 mio_integer (&n);
5135
5136 if (sym->attr.flavor == FL_DERIVED)
5137 {
5138 const char *name;
5139 name = dt_upper_string (sym->name);
5140 mio_pool_string (&name);
5141 }
5142 else
5143 mio_pool_string (&sym->name);
5144
5145 mio_pool_string (&sym->module);
5146 if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
5147 {
5148 label = sym->binding_label;
5149 mio_pool_string (&label);
5150 }
5151 else
5152 write_atom (ATOM_STRING, "");
5153
5154 mio_pointer_ref (&sym->ns);
5155
5156 mio_symbol (sym);
5157 write_char ('\n');
5158 }
5159
5160
5161 /* Recursive traversal function to write the initial set of symbols to
5162 the module. We check to see if the symbol should be written
5163 according to the access specification. */
5164
5165 static void
5166 write_symbol0 (gfc_symtree *st)
5167 {
5168 gfc_symbol *sym;
5169 pointer_info *p;
5170 bool dont_write = false;
5171
5172 if (st == NULL)
5173 return;
5174
5175 write_symbol0 (st->left);
5176
5177 sym = st->n.sym;
5178 if (sym->module == NULL)
5179 sym->module = module_name;
5180
5181 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5182 && !sym->attr.subroutine && !sym->attr.function)
5183 dont_write = true;
5184
5185 if (!gfc_check_symbol_access (sym))
5186 dont_write = true;
5187
5188 if (!dont_write)
5189 {
5190 p = get_pointer (sym);
5191 if (p->type == P_UNKNOWN)
5192 p->type = P_SYMBOL;
5193
5194 if (p->u.wsym.state != WRITTEN)
5195 {
5196 write_symbol (p->integer, sym);
5197 p->u.wsym.state = WRITTEN;
5198 }
5199 }
5200
5201 write_symbol0 (st->right);
5202 }
5203
5204
5205 /* Type for the temporary tree used when writing secondary symbols. */
5206
5207 struct sorted_pointer_info
5208 {
5209 BBT_HEADER (sorted_pointer_info);
5210
5211 pointer_info *p;
5212 };
5213
5214 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5215
5216 /* Recursively traverse the temporary tree, free its contents. */
5217
5218 static void
5219 free_sorted_pointer_info_tree (sorted_pointer_info *p)
5220 {
5221 if (!p)
5222 return;
5223
5224 free_sorted_pointer_info_tree (p->left);
5225 free_sorted_pointer_info_tree (p->right);
5226
5227 free (p);
5228 }
5229
5230 /* Comparison function for the temporary tree. */
5231
5232 static int
5233 compare_sorted_pointer_info (void *_spi1, void *_spi2)
5234 {
5235 sorted_pointer_info *spi1, *spi2;
5236 spi1 = (sorted_pointer_info *)_spi1;
5237 spi2 = (sorted_pointer_info *)_spi2;
5238
5239 if (spi1->p->integer < spi2->p->integer)
5240 return -1;
5241 if (spi1->p->integer > spi2->p->integer)
5242 return 1;
5243 return 0;
5244 }
5245
5246
5247 /* Finds the symbols that need to be written and collects them in the
5248 sorted_pi tree so that they can be traversed in an order
5249 independent of memory addresses. */
5250
5251 static void
5252 find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
5253 {
5254 if (!p)
5255 return;
5256
5257 if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
5258 {
5259 sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
5260 sp->p = p;
5261
5262 gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
5263 }
5264
5265 find_symbols_to_write (tree, p->left);
5266 find_symbols_to_write (tree, p->right);
5267 }
5268
5269
5270 /* Recursive function that traverses the tree of symbols that need to be
5271 written and writes them in order. */
5272
5273 static void
5274 write_symbol1_recursion (sorted_pointer_info *sp)
5275 {
5276 if (!sp)
5277 return;
5278
5279 write_symbol1_recursion (sp->left);
5280
5281 pointer_info *p1 = sp->p;
5282 gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
5283
5284 p1->u.wsym.state = WRITTEN;
5285 write_symbol (p1->integer, p1->u.wsym.sym);
5286 p1->u.wsym.sym->attr.public_used = 1;
5287
5288 write_symbol1_recursion (sp->right);
5289 }
5290
5291
5292 /* Write the secondary set of symbols to the module file. These are
5293 symbols that were not public yet are needed by the public symbols
5294 or another dependent symbol. The act of writing a symbol can add
5295 symbols to the pointer_info tree, so we return nonzero if a symbol
5296 was written and pass that information upwards. The caller will
5297 then call this function again until nothing was written. It uses
5298 the utility functions and a temporary tree to ensure a reproducible
5299 ordering of the symbol output and thus the module file. */
5300
5301 static int
5302 write_symbol1 (pointer_info *p)
5303 {
5304 if (!p)
5305 return 0;
5306
5307 /* Put symbols that need to be written into a tree sorted on the
5308 integer field. */
5309
5310 sorted_pointer_info *spi_root = NULL;
5311 find_symbols_to_write (&spi_root, p);
5312
5313 /* No symbols to write, return. */
5314 if (!spi_root)
5315 return 0;
5316
5317 /* Otherwise, write and free the tree again. */
5318 write_symbol1_recursion (spi_root);
5319 free_sorted_pointer_info_tree (spi_root);
5320
5321 return 1;
5322 }
5323
5324
5325 /* Write operator interfaces associated with a symbol. */
5326
5327 static void
5328 write_operator (gfc_user_op *uop)
5329 {
5330 static char nullstring[] = "";
5331 const char *p = nullstring;
5332
5333 if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
5334 return;
5335
5336 mio_symbol_interface (&uop->name, &p, &uop->op);
5337 }
5338
5339
5340 /* Write generic interfaces from the namespace sym_root. */
5341
5342 static void
5343 write_generic (gfc_symtree *st)
5344 {
5345 gfc_symbol *sym;
5346
5347 if (st == NULL)
5348 return;
5349
5350 write_generic (st->left);
5351
5352 sym = st->n.sym;
5353 if (sym && !check_unique_name (st->name)
5354 && sym->generic && gfc_check_symbol_access (sym))
5355 {
5356 if (!sym->module)
5357 sym->module = module_name;
5358
5359 mio_symbol_interface (&st->name, &sym->module, &sym->generic);
5360 }
5361
5362 write_generic (st->right);
5363 }
5364
5365
5366 static void
5367 write_symtree (gfc_symtree *st)
5368 {
5369 gfc_symbol *sym;
5370 pointer_info *p;
5371
5372 sym = st->n.sym;
5373
5374 /* A symbol in an interface body must not be visible in the
5375 module file. */
5376 if (sym->ns != gfc_current_ns
5377 && sym->ns->proc_name
5378 && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
5379 return;
5380
5381 if (!gfc_check_symbol_access (sym)
5382 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
5383 && !sym->attr.subroutine && !sym->attr.function))
5384 return;
5385
5386 if (check_unique_name (st->name))
5387 return;
5388
5389 p = find_pointer (sym);
5390 if (p == NULL)
5391 gfc_internal_error ("write_symtree(): Symbol not written");
5392
5393 mio_pool_string (&st->name);
5394 mio_integer (&st->ambiguous);
5395 mio_integer (&p->integer);
5396 }
5397
5398
5399 static void
5400 write_module (void)
5401 {
5402 int i;
5403
5404 /* Write the operator interfaces. */
5405 mio_lparen ();
5406
5407 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
5408 {
5409 if (i == INTRINSIC_USER)
5410 continue;
5411
5412 mio_interface (check_access (gfc_current_ns->operator_access[i],
5413 gfc_current_ns->default_access)
5414 ? &gfc_current_ns->op[i] : NULL);
5415 }
5416
5417 mio_rparen ();
5418 write_char ('\n');
5419 write_char ('\n');
5420
5421 mio_lparen ();
5422 gfc_traverse_user_op (gfc_current_ns, write_operator);
5423 mio_rparen ();
5424 write_char ('\n');
5425 write_char ('\n');
5426
5427 mio_lparen ();
5428 write_generic (gfc_current_ns->sym_root);
5429 mio_rparen ();
5430 write_char ('\n');
5431 write_char ('\n');
5432
5433 mio_lparen ();
5434 write_blank_common ();
5435 write_common (gfc_current_ns->common_root);
5436 mio_rparen ();
5437 write_char ('\n');
5438 write_char ('\n');
5439
5440 mio_lparen ();
5441 write_equiv ();
5442 mio_rparen ();
5443 write_char ('\n');
5444 write_char ('\n');
5445
5446 mio_lparen ();
5447 gfc_traverse_symtree (gfc_current_ns->sym_root,
5448 write_derived_extensions);
5449 mio_rparen ();
5450 write_char ('\n');
5451 write_char ('\n');
5452
5453 /* Write symbol information. First we traverse all symbols in the
5454 primary namespace, writing those that need to be written.
5455 Sometimes writing one symbol will cause another to need to be
5456 written. A list of these symbols ends up on the write stack, and
5457 we end by popping the bottom of the stack and writing the symbol
5458 until the stack is empty. */
5459
5460 mio_lparen ();
5461
5462 write_symbol0 (gfc_current_ns->sym_root);
5463 while (write_symbol1 (pi_root))
5464 /* Nothing. */;
5465
5466 mio_rparen ();
5467
5468 write_char ('\n');
5469 write_char ('\n');
5470
5471 mio_lparen ();
5472 gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
5473 mio_rparen ();
5474 }
5475
5476
5477 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5478 true on success, false on failure. */
5479
5480 static bool
5481 read_crc32_from_module_file (const char* filename, uLong* crc)
5482 {
5483 FILE *file;
5484 char buf[4];
5485 unsigned int val;
5486
5487 /* Open the file in binary mode. */
5488 if ((file = fopen (filename, "rb")) == NULL)
5489 return false;
5490
5491 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5492 file. See RFC 1952. */
5493 if (fseek (file, -8, SEEK_END) != 0)
5494 {
5495 fclose (file);
5496 return false;
5497 }
5498
5499 /* Read the CRC32. */
5500 if (fread (buf, 1, 4, file) != 4)
5501 {
5502 fclose (file);
5503 return false;
5504 }
5505
5506 /* Close the file. */
5507 fclose (file);
5508
5509 val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
5510 + ((buf[3] & 0xFF) << 24);
5511 *crc = val;
5512
5513 /* For debugging, the CRC value printed in hexadecimal should match
5514 the CRC printed by "zcat -l -v filename".
5515 printf("CRC of file %s is %x\n", filename, val); */
5516
5517 return true;
5518 }
5519
5520
5521 /* Given module, dump it to disk. If there was an error while
5522 processing the module, dump_flag will be set to zero and we delete
5523 the module file, even if it was already there. */
5524
5525 void
5526 gfc_dump_module (const char *name, int dump_flag)
5527 {
5528 int n;
5529 char *filename, *filename_tmp;
5530 uLong crc, crc_old;
5531
5532 n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
5533 if (gfc_option.module_dir != NULL)
5534 {
5535 n += strlen (gfc_option.module_dir);
5536 filename = (char *) alloca (n);
5537 strcpy (filename, gfc_option.module_dir);
5538 strcat (filename, name);
5539 }
5540 else
5541 {
5542 filename = (char *) alloca (n);
5543 strcpy (filename, name);
5544 }
5545 strcat (filename, MODULE_EXTENSION);
5546
5547 /* Name of the temporary file used to write the module. */
5548 filename_tmp = (char *) alloca (n + 1);
5549 strcpy (filename_tmp, filename);
5550 strcat (filename_tmp, "0");
5551
5552 /* There was an error while processing the module. We delete the
5553 module file, even if it was already there. */
5554 if (!dump_flag)
5555 {
5556 unlink (filename);
5557 return;
5558 }
5559
5560 if (gfc_cpp_makedep ())
5561 gfc_cpp_add_target (filename);
5562
5563 /* Write the module to the temporary file. */
5564 module_fp = gzopen (filename_tmp, "w");
5565 if (module_fp == NULL)
5566 gfc_fatal_error ("Can't open module file '%s' for writing at %C: %s",
5567 filename_tmp, xstrerror (errno));
5568
5569 gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
5570 MOD_VERSION, gfc_source_file);
5571
5572 /* Write the module itself. */
5573 iomode = IO_OUTPUT;
5574 module_name = gfc_get_string (name);
5575
5576 init_pi_tree ();
5577
5578 write_module ();
5579
5580 free_pi_tree (pi_root);
5581 pi_root = NULL;
5582
5583 write_char ('\n');
5584
5585 if (gzclose (module_fp))
5586 gfc_fatal_error ("Error writing module file '%s' for writing: %s",
5587 filename_tmp, xstrerror (errno));
5588
5589 /* Read the CRC32 from the gzip trailers of the module files and
5590 compare. */
5591 if (!read_crc32_from_module_file (filename_tmp, &crc)
5592 || !read_crc32_from_module_file (filename, &crc_old)
5593 || crc_old != crc)
5594 {
5595 /* Module file have changed, replace the old one. */
5596 if (rename (filename_tmp, filename))
5597 gfc_fatal_error ("Can't rename module file '%s' to '%s': %s",
5598 filename_tmp, filename, xstrerror (errno));
5599 }
5600 else
5601 {
5602 if (unlink (filename_tmp))
5603 gfc_fatal_error ("Can't delete temporary module file '%s': %s",
5604 filename_tmp, xstrerror (errno));
5605 }
5606 }
5607
5608
5609 static void
5610 create_intrinsic_function (const char *name, int id,
5611 const char *modname, intmod_id module,
5612 bool subroutine, gfc_symbol *result_type)
5613 {
5614 gfc_intrinsic_sym *isym;
5615 gfc_symtree *tmp_symtree;
5616 gfc_symbol *sym;
5617
5618 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5619 if (tmp_symtree)
5620 {
5621 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5622 return;
5623 gfc_error ("Symbol '%s' already declared", name);
5624 }
5625
5626 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5627 sym = tmp_symtree->n.sym;
5628
5629 if (subroutine)
5630 {
5631 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5632 isym = gfc_intrinsic_subroutine_by_id (isym_id);
5633 sym->attr.subroutine = 1;
5634 }
5635 else
5636 {
5637 gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
5638 isym = gfc_intrinsic_function_by_id (isym_id);
5639
5640 sym->attr.function = 1;
5641 if (result_type)
5642 {
5643 sym->ts.type = BT_DERIVED;
5644 sym->ts.u.derived = result_type;
5645 sym->ts.is_c_interop = 1;
5646 isym->ts.f90_type = BT_VOID;
5647 isym->ts.type = BT_DERIVED;
5648 isym->ts.f90_type = BT_VOID;
5649 isym->ts.u.derived = result_type;
5650 isym->ts.is_c_interop = 1;
5651 }
5652 }
5653 gcc_assert (isym);
5654
5655 sym->attr.flavor = FL_PROCEDURE;
5656 sym->attr.intrinsic = 1;
5657
5658 sym->module = gfc_get_string (modname);
5659 sym->attr.use_assoc = 1;
5660 sym->from_intmod = module;
5661 sym->intmod_sym_id = id;
5662 }
5663
5664
5665 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
5666 the current namespace for all named constants, pointer types, and
5667 procedures in the module unless the only clause was used or a rename
5668 list was provided. */
5669
5670 static void
5671 import_iso_c_binding_module (void)
5672 {
5673 gfc_symbol *mod_sym = NULL, *return_type;
5674 gfc_symtree *mod_symtree = NULL, *tmp_symtree;
5675 gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
5676 const char *iso_c_module_name = "__iso_c_binding";
5677 gfc_use_rename *u;
5678 int i;
5679 bool want_c_ptr = false, want_c_funptr = false;
5680
5681 /* Look only in the current namespace. */
5682 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
5683
5684 if (mod_symtree == NULL)
5685 {
5686 /* symtree doesn't already exist in current namespace. */
5687 gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
5688 false);
5689
5690 if (mod_symtree != NULL)
5691 mod_sym = mod_symtree->n.sym;
5692 else
5693 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
5694 "create symbol for %s", iso_c_module_name);
5695
5696 mod_sym->attr.flavor = FL_MODULE;
5697 mod_sym->attr.intrinsic = 1;
5698 mod_sym->module = gfc_get_string (iso_c_module_name);
5699 mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
5700 }
5701
5702 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
5703 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
5704 need C_(FUN)PTR. */
5705 for (u = gfc_rename_list; u; u = u->next)
5706 {
5707 if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
5708 u->use_name) == 0)
5709 want_c_ptr = true;
5710 else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
5711 u->use_name) == 0)
5712 want_c_ptr = true;
5713 else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
5714 u->use_name) == 0)
5715 want_c_funptr = true;
5716 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
5717 u->use_name) == 0)
5718 want_c_funptr = true;
5719 else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
5720 u->use_name) == 0)
5721 {
5722 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5723 (iso_c_binding_symbol)
5724 ISOCBINDING_PTR,
5725 u->local_name[0] ? u->local_name
5726 : u->use_name,
5727 NULL, false);
5728 }
5729 else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
5730 u->use_name) == 0)
5731 {
5732 c_funptr
5733 = generate_isocbinding_symbol (iso_c_module_name,
5734 (iso_c_binding_symbol)
5735 ISOCBINDING_FUNPTR,
5736 u->local_name[0] ? u->local_name
5737 : u->use_name,
5738 NULL, false);
5739 }
5740 }
5741
5742 if ((want_c_ptr || !only_flag) && !c_ptr)
5743 c_ptr = generate_isocbinding_symbol (iso_c_module_name,
5744 (iso_c_binding_symbol)
5745 ISOCBINDING_PTR,
5746 NULL, NULL, only_flag);
5747 if ((want_c_funptr || !only_flag) && !c_funptr)
5748 c_funptr = generate_isocbinding_symbol (iso_c_module_name,
5749 (iso_c_binding_symbol)
5750 ISOCBINDING_FUNPTR,
5751 NULL, NULL, only_flag);
5752
5753 /* Generate the symbols for the named constants representing
5754 the kinds for intrinsic data types. */
5755 for (i = 0; i < ISOCBINDING_NUMBER; i++)
5756 {
5757 bool found = false;
5758 for (u = gfc_rename_list; u; u = u->next)
5759 if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
5760 {
5761 bool not_in_std;
5762 const char *name;
5763 u->found = 1;
5764 found = true;
5765
5766 switch (i)
5767 {
5768 #define NAMED_FUNCTION(a,b,c,d) \
5769 case a: \
5770 not_in_std = (gfc_option.allow_std & d) == 0; \
5771 name = b; \
5772 break;
5773 #define NAMED_SUBROUTINE(a,b,c,d) \
5774 case a: \
5775 not_in_std = (gfc_option.allow_std & d) == 0; \
5776 name = b; \
5777 break;
5778 #define NAMED_INTCST(a,b,c,d) \
5779 case a: \
5780 not_in_std = (gfc_option.allow_std & d) == 0; \
5781 name = b; \
5782 break;
5783 #define NAMED_REALCST(a,b,c,d) \
5784 case a: \
5785 not_in_std = (gfc_option.allow_std & d) == 0; \
5786 name = b; \
5787 break;
5788 #define NAMED_CMPXCST(a,b,c,d) \
5789 case a: \
5790 not_in_std = (gfc_option.allow_std & d) == 0; \
5791 name = b; \
5792 break;
5793 #include "iso-c-binding.def"
5794 default:
5795 not_in_std = false;
5796 name = "";
5797 }
5798
5799 if (not_in_std)
5800 {
5801 gfc_error ("The symbol '%s', referenced at %L, is not "
5802 "in the selected standard", name, &u->where);
5803 continue;
5804 }
5805
5806 switch (i)
5807 {
5808 #define NAMED_FUNCTION(a,b,c,d) \
5809 case a: \
5810 if (a == ISOCBINDING_LOC) \
5811 return_type = c_ptr->n.sym; \
5812 else if (a == ISOCBINDING_FUNLOC) \
5813 return_type = c_funptr->n.sym; \
5814 else \
5815 return_type = NULL; \
5816 create_intrinsic_function (u->local_name[0] \
5817 ? u->local_name : u->use_name, \
5818 a, iso_c_module_name, \
5819 INTMOD_ISO_C_BINDING, false, \
5820 return_type); \
5821 break;
5822 #define NAMED_SUBROUTINE(a,b,c,d) \
5823 case a: \
5824 create_intrinsic_function (u->local_name[0] ? u->local_name \
5825 : u->use_name, \
5826 a, iso_c_module_name, \
5827 INTMOD_ISO_C_BINDING, true, NULL); \
5828 break;
5829 #include "iso-c-binding.def"
5830
5831 case ISOCBINDING_PTR:
5832 case ISOCBINDING_FUNPTR:
5833 /* Already handled above. */
5834 break;
5835 default:
5836 if (i == ISOCBINDING_NULL_PTR)
5837 tmp_symtree = c_ptr;
5838 else if (i == ISOCBINDING_NULL_FUNPTR)
5839 tmp_symtree = c_funptr;
5840 else
5841 tmp_symtree = NULL;
5842 generate_isocbinding_symbol (iso_c_module_name,
5843 (iso_c_binding_symbol) i,
5844 u->local_name[0]
5845 ? u->local_name : u->use_name,
5846 tmp_symtree, false);
5847 }
5848 }
5849
5850 if (!found && !only_flag)
5851 {
5852 /* Skip, if the symbol is not in the enabled standard. */
5853 switch (i)
5854 {
5855 #define NAMED_FUNCTION(a,b,c,d) \
5856 case a: \
5857 if ((gfc_option.allow_std & d) == 0) \
5858 continue; \
5859 break;
5860 #define NAMED_SUBROUTINE(a,b,c,d) \
5861 case a: \
5862 if ((gfc_option.allow_std & d) == 0) \
5863 continue; \
5864 break;
5865 #define NAMED_INTCST(a,b,c,d) \
5866 case a: \
5867 if ((gfc_option.allow_std & d) == 0) \
5868 continue; \
5869 break;
5870 #define NAMED_REALCST(a,b,c,d) \
5871 case a: \
5872 if ((gfc_option.allow_std & d) == 0) \
5873 continue; \
5874 break;
5875 #define NAMED_CMPXCST(a,b,c,d) \
5876 case a: \
5877 if ((gfc_option.allow_std & d) == 0) \
5878 continue; \
5879 break;
5880 #include "iso-c-binding.def"
5881 default:
5882 ; /* Not GFC_STD_* versioned. */
5883 }
5884
5885 switch (i)
5886 {
5887 #define NAMED_FUNCTION(a,b,c,d) \
5888 case a: \
5889 if (a == ISOCBINDING_LOC) \
5890 return_type = c_ptr->n.sym; \
5891 else if (a == ISOCBINDING_FUNLOC) \
5892 return_type = c_funptr->n.sym; \
5893 else \
5894 return_type = NULL; \
5895 create_intrinsic_function (b, a, iso_c_module_name, \
5896 INTMOD_ISO_C_BINDING, false, \
5897 return_type); \
5898 break;
5899 #define NAMED_SUBROUTINE(a,b,c,d) \
5900 case a: \
5901 create_intrinsic_function (b, a, iso_c_module_name, \
5902 INTMOD_ISO_C_BINDING, true, NULL); \
5903 break;
5904 #include "iso-c-binding.def"
5905
5906 case ISOCBINDING_PTR:
5907 case ISOCBINDING_FUNPTR:
5908 /* Already handled above. */
5909 break;
5910 default:
5911 if (i == ISOCBINDING_NULL_PTR)
5912 tmp_symtree = c_ptr;
5913 else if (i == ISOCBINDING_NULL_FUNPTR)
5914 tmp_symtree = c_funptr;
5915 else
5916 tmp_symtree = NULL;
5917 generate_isocbinding_symbol (iso_c_module_name,
5918 (iso_c_binding_symbol) i, NULL,
5919 tmp_symtree, false);
5920 }
5921 }
5922 }
5923
5924 for (u = gfc_rename_list; u; u = u->next)
5925 {
5926 if (u->found)
5927 continue;
5928
5929 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
5930 "module ISO_C_BINDING", u->use_name, &u->where);
5931 }
5932 }
5933
5934
5935 /* Add an integer named constant from a given module. */
5936
5937 static void
5938 create_int_parameter (const char *name, int value, const char *modname,
5939 intmod_id module, int id)
5940 {
5941 gfc_symtree *tmp_symtree;
5942 gfc_symbol *sym;
5943
5944 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5945 if (tmp_symtree != NULL)
5946 {
5947 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5948 return;
5949 else
5950 gfc_error ("Symbol '%s' already declared", name);
5951 }
5952
5953 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5954 sym = tmp_symtree->n.sym;
5955
5956 sym->module = gfc_get_string (modname);
5957 sym->attr.flavor = FL_PARAMETER;
5958 sym->ts.type = BT_INTEGER;
5959 sym->ts.kind = gfc_default_integer_kind;
5960 sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
5961 sym->attr.use_assoc = 1;
5962 sym->from_intmod = module;
5963 sym->intmod_sym_id = id;
5964 }
5965
5966
5967 /* Value is already contained by the array constructor, but not
5968 yet the shape. */
5969
5970 static void
5971 create_int_parameter_array (const char *name, int size, gfc_expr *value,
5972 const char *modname, intmod_id module, int id)
5973 {
5974 gfc_symtree *tmp_symtree;
5975 gfc_symbol *sym;
5976
5977 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5978 if (tmp_symtree != NULL)
5979 {
5980 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
5981 return;
5982 else
5983 gfc_error ("Symbol '%s' already declared", name);
5984 }
5985
5986 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5987 sym = tmp_symtree->n.sym;
5988
5989 sym->module = gfc_get_string (modname);
5990 sym->attr.flavor = FL_PARAMETER;
5991 sym->ts.type = BT_INTEGER;
5992 sym->ts.kind = gfc_default_integer_kind;
5993 sym->attr.use_assoc = 1;
5994 sym->from_intmod = module;
5995 sym->intmod_sym_id = id;
5996 sym->attr.dimension = 1;
5997 sym->as = gfc_get_array_spec ();
5998 sym->as->rank = 1;
5999 sym->as->type = AS_EXPLICIT;
6000 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6001 sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
6002
6003 sym->value = value;
6004 sym->value->shape = gfc_get_shape (1);
6005 mpz_init_set_ui (sym->value->shape[0], size);
6006 }
6007
6008
6009 /* Add an derived type for a given module. */
6010
6011 static void
6012 create_derived_type (const char *name, const char *modname,
6013 intmod_id module, int id)
6014 {
6015 gfc_symtree *tmp_symtree;
6016 gfc_symbol *sym, *dt_sym;
6017 gfc_interface *intr, *head;
6018
6019 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
6020 if (tmp_symtree != NULL)
6021 {
6022 if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
6023 return;
6024 else
6025 gfc_error ("Symbol '%s' already declared", name);
6026 }
6027
6028 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
6029 sym = tmp_symtree->n.sym;
6030 sym->module = gfc_get_string (modname);
6031 sym->from_intmod = module;
6032 sym->intmod_sym_id = id;
6033 sym->attr.flavor = FL_PROCEDURE;
6034 sym->attr.function = 1;
6035 sym->attr.generic = 1;
6036
6037 gfc_get_sym_tree (dt_upper_string (sym->name),
6038 gfc_current_ns, &tmp_symtree, false);
6039 dt_sym = tmp_symtree->n.sym;
6040 dt_sym->name = gfc_get_string (sym->name);
6041 dt_sym->attr.flavor = FL_DERIVED;
6042 dt_sym->attr.private_comp = 1;
6043 dt_sym->attr.zero_comp = 1;
6044 dt_sym->attr.use_assoc = 1;
6045 dt_sym->module = gfc_get_string (modname);
6046 dt_sym->from_intmod = module;
6047 dt_sym->intmod_sym_id = id;
6048
6049 head = sym->generic;
6050 intr = gfc_get_interface ();
6051 intr->sym = dt_sym;
6052 intr->where = gfc_current_locus;
6053 intr->next = head;
6054 sym->generic = intr;
6055 sym->attr.if_source = IFSRC_DECL;
6056 }
6057
6058
6059 /* Read the contents of the module file into a temporary buffer. */
6060
6061 static void
6062 read_module_to_tmpbuf ()
6063 {
6064 /* We don't know the uncompressed size, so enlarge the buffer as
6065 needed. */
6066 int cursz = 4096;
6067 int rsize = cursz;
6068 int len = 0;
6069
6070 module_content = XNEWVEC (char, cursz);
6071
6072 while (1)
6073 {
6074 int nread = gzread (module_fp, module_content + len, rsize);
6075 len += nread;
6076 if (nread < rsize)
6077 break;
6078 cursz *= 2;
6079 module_content = XRESIZEVEC (char, module_content, cursz);
6080 rsize = cursz - len;
6081 }
6082
6083 module_content = XRESIZEVEC (char, module_content, len + 1);
6084 module_content[len] = '\0';
6085
6086 module_pos = 0;
6087 }
6088
6089
6090 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6091
6092 static void
6093 use_iso_fortran_env_module (void)
6094 {
6095 static char mod[] = "iso_fortran_env";
6096 gfc_use_rename *u;
6097 gfc_symbol *mod_sym;
6098 gfc_symtree *mod_symtree;
6099 gfc_expr *expr;
6100 int i, j;
6101
6102 intmod_sym symbol[] = {
6103 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6104 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6105 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6106 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6107 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6108 #include "iso-fortran-env.def"
6109 { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
6110
6111 i = 0;
6112 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6113 #include "iso-fortran-env.def"
6114
6115 /* Generate the symbol for the module itself. */
6116 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
6117 if (mod_symtree == NULL)
6118 {
6119 gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
6120 gcc_assert (mod_symtree);
6121 mod_sym = mod_symtree->n.sym;
6122
6123 mod_sym->attr.flavor = FL_MODULE;
6124 mod_sym->attr.intrinsic = 1;
6125 mod_sym->module = gfc_get_string (mod);
6126 mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
6127 }
6128 else
6129 if (!mod_symtree->n.sym->attr.intrinsic)
6130 gfc_error ("Use of intrinsic module '%s' at %C conflicts with "
6131 "non-intrinsic module name used previously", mod);
6132
6133 /* Generate the symbols for the module integer named constants. */
6134
6135 for (i = 0; symbol[i].name; i++)
6136 {
6137 bool found = false;
6138 for (u = gfc_rename_list; u; u = u->next)
6139 {
6140 if (strcmp (symbol[i].name, u->use_name) == 0)
6141 {
6142 found = true;
6143 u->found = 1;
6144
6145 if (!gfc_notify_std (symbol[i].standard, "The symbol '%s', "
6146 "referenced at %L, is not in the selected "
6147 "standard", symbol[i].name, &u->where))
6148 continue;
6149
6150 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6151 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6152 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6153 "constant from intrinsic module "
6154 "ISO_FORTRAN_ENV at %L is incompatible with "
6155 "option %s", &u->where,
6156 gfc_option.flag_default_integer
6157 ? "-fdefault-integer-8"
6158 : "-fdefault-real-8");
6159 switch (symbol[i].id)
6160 {
6161 #define NAMED_INTCST(a,b,c,d) \
6162 case a:
6163 #include "iso-fortran-env.def"
6164 create_int_parameter (u->local_name[0] ? u->local_name
6165 : u->use_name,
6166 symbol[i].value, mod,
6167 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6168 break;
6169
6170 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6171 case a:\
6172 expr = gfc_get_array_expr (BT_INTEGER, \
6173 gfc_default_integer_kind,\
6174 NULL); \
6175 for (j = 0; KINDS[j].kind != 0; j++) \
6176 gfc_constructor_append_expr (&expr->value.constructor, \
6177 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6178 KINDS[j].kind), NULL); \
6179 create_int_parameter_array (u->local_name[0] ? u->local_name \
6180 : u->use_name, \
6181 j, expr, mod, \
6182 INTMOD_ISO_FORTRAN_ENV, \
6183 symbol[i].id); \
6184 break;
6185 #include "iso-fortran-env.def"
6186
6187 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6188 case a:
6189 #include "iso-fortran-env.def"
6190 create_derived_type (u->local_name[0] ? u->local_name
6191 : u->use_name,
6192 mod, INTMOD_ISO_FORTRAN_ENV,
6193 symbol[i].id);
6194 break;
6195
6196 #define NAMED_FUNCTION(a,b,c,d) \
6197 case a:
6198 #include "iso-fortran-env.def"
6199 create_intrinsic_function (u->local_name[0] ? u->local_name
6200 : u->use_name,
6201 symbol[i].id, mod,
6202 INTMOD_ISO_FORTRAN_ENV, false,
6203 NULL);
6204 break;
6205
6206 default:
6207 gcc_unreachable ();
6208 }
6209 }
6210 }
6211
6212 if (!found && !only_flag)
6213 {
6214 if ((gfc_option.allow_std & symbol[i].standard) == 0)
6215 continue;
6216
6217 if ((gfc_option.flag_default_integer || gfc_option.flag_default_real)
6218 && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
6219 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6220 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6221 "incompatible with option %s",
6222 gfc_option.flag_default_integer
6223 ? "-fdefault-integer-8" : "-fdefault-real-8");
6224
6225 switch (symbol[i].id)
6226 {
6227 #define NAMED_INTCST(a,b,c,d) \
6228 case a:
6229 #include "iso-fortran-env.def"
6230 create_int_parameter (symbol[i].name, symbol[i].value, mod,
6231 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
6232 break;
6233
6234 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6235 case a:\
6236 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6237 NULL); \
6238 for (j = 0; KINDS[j].kind != 0; j++) \
6239 gfc_constructor_append_expr (&expr->value.constructor, \
6240 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6241 KINDS[j].kind), NULL); \
6242 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6243 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6244 break;
6245 #include "iso-fortran-env.def"
6246
6247 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6248 case a:
6249 #include "iso-fortran-env.def"
6250 create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
6251 symbol[i].id);
6252 break;
6253
6254 #define NAMED_FUNCTION(a,b,c,d) \
6255 case a:
6256 #include "iso-fortran-env.def"
6257 create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
6258 INTMOD_ISO_FORTRAN_ENV, false,
6259 NULL);
6260 break;
6261
6262 default:
6263 gcc_unreachable ();
6264 }
6265 }
6266 }
6267
6268 for (u = gfc_rename_list; u; u = u->next)
6269 {
6270 if (u->found)
6271 continue;
6272
6273 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
6274 "module ISO_FORTRAN_ENV", u->use_name, &u->where);
6275 }
6276 }
6277
6278
6279 /* Process a USE directive. */
6280
6281 static void
6282 gfc_use_module (gfc_use_list *module)
6283 {
6284 char *filename;
6285 gfc_state_data *p;
6286 int c, line, start;
6287 gfc_symtree *mod_symtree;
6288 gfc_use_list *use_stmt;
6289 locus old_locus = gfc_current_locus;
6290
6291 gfc_current_locus = module->where;
6292 module_name = module->module_name;
6293 gfc_rename_list = module->rename;
6294 only_flag = module->only_flag;
6295
6296 filename = XALLOCAVEC (char, strlen (module_name) + strlen (MODULE_EXTENSION)
6297 + 1);
6298 strcpy (filename, module_name);
6299 strcat (filename, MODULE_EXTENSION);
6300
6301 /* First, try to find an non-intrinsic module, unless the USE statement
6302 specified that the module is intrinsic. */
6303 module_fp = NULL;
6304 if (!module->intrinsic)
6305 module_fp = gzopen_included_file (filename, true, true);
6306
6307 /* Then, see if it's an intrinsic one, unless the USE statement
6308 specified that the module is non-intrinsic. */
6309 if (module_fp == NULL && !module->non_intrinsic)
6310 {
6311 if (strcmp (module_name, "iso_fortran_env") == 0
6312 && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
6313 "intrinsic module at %C"))
6314 {
6315 use_iso_fortran_env_module ();
6316 free_rename (module->rename);
6317 module->rename = NULL;
6318 gfc_current_locus = old_locus;
6319 module->intrinsic = true;
6320 return;
6321 }
6322
6323 if (strcmp (module_name, "iso_c_binding") == 0
6324 && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
6325 {
6326 import_iso_c_binding_module();
6327 free_rename (module->rename);
6328 module->rename = NULL;
6329 gfc_current_locus = old_locus;
6330 module->intrinsic = true;
6331 return;
6332 }
6333
6334 module_fp = gzopen_intrinsic_module (filename);
6335
6336 if (module_fp == NULL && module->intrinsic)
6337 gfc_fatal_error ("Can't find an intrinsic module named '%s' at %C",
6338 module_name);
6339 }
6340
6341 if (module_fp == NULL)
6342 gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
6343 filename, xstrerror (errno));
6344
6345 /* Check that we haven't already USEd an intrinsic module with the
6346 same name. */
6347
6348 mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
6349 if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
6350 gfc_error ("Use of non-intrinsic module '%s' at %C conflicts with "
6351 "intrinsic module name used previously", module_name);
6352
6353 iomode = IO_INPUT;
6354 module_line = 1;
6355 module_column = 1;
6356 start = 0;
6357
6358 read_module_to_tmpbuf ();
6359 gzclose (module_fp);
6360
6361 /* Skip the first line of the module, after checking that this is
6362 a gfortran module file. */
6363 line = 0;
6364 while (line < 1)
6365 {
6366 c = module_char ();
6367 if (c == EOF)
6368 bad_module ("Unexpected end of module");
6369 if (start++ < 3)
6370 parse_name (c);
6371 if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
6372 || (start == 2 && strcmp (atom_name, " module") != 0))
6373 gfc_fatal_error ("File '%s' opened at %C is not a GNU Fortran"
6374 " module file", filename);
6375 if (start == 3)
6376 {
6377 if (strcmp (atom_name, " version") != 0
6378 || module_char () != ' '
6379 || parse_atom () != ATOM_STRING
6380 || strcmp (atom_string, MOD_VERSION))
6381 gfc_fatal_error ("Cannot read module file '%s' opened at %C,"
6382 " because it was created by a different"
6383 " version of GNU Fortran", filename);
6384
6385 free (atom_string);
6386 }
6387
6388 if (c == '\n')
6389 line++;
6390 }
6391
6392 /* Make sure we're not reading the same module that we may be building. */
6393 for (p = gfc_state_stack; p; p = p->previous)
6394 if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
6395 gfc_fatal_error ("Can't USE the same module we're building!");
6396
6397 init_pi_tree ();
6398 init_true_name_tree ();
6399
6400 read_module ();
6401
6402 free_true_name (true_name_root);
6403 true_name_root = NULL;
6404
6405 free_pi_tree (pi_root);
6406 pi_root = NULL;
6407
6408 XDELETEVEC (module_content);
6409 module_content = NULL;
6410
6411 use_stmt = gfc_get_use_list ();
6412 *use_stmt = *module;
6413 use_stmt->next = gfc_current_ns->use_stmts;
6414 gfc_current_ns->use_stmts = use_stmt;
6415
6416 gfc_current_locus = old_locus;
6417 }
6418
6419
6420 /* Remove duplicated intrinsic operators from the rename list. */
6421
6422 static void
6423 rename_list_remove_duplicate (gfc_use_rename *list)
6424 {
6425 gfc_use_rename *seek, *last;
6426
6427 for (; list; list = list->next)
6428 if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
6429 {
6430 last = list;
6431 for (seek = list->next; seek; seek = last->next)
6432 {
6433 if (list->op == seek->op)
6434 {
6435 last->next = seek->next;
6436 free (seek);
6437 }
6438 else
6439 last = seek;
6440 }
6441 }
6442 }
6443
6444
6445 /* Process all USE directives. */
6446
6447 void
6448 gfc_use_modules (void)
6449 {
6450 gfc_use_list *next, *seek, *last;
6451
6452 for (next = module_list; next; next = next->next)
6453 {
6454 bool non_intrinsic = next->non_intrinsic;
6455 bool intrinsic = next->intrinsic;
6456 bool neither = !non_intrinsic && !intrinsic;
6457
6458 for (seek = next->next; seek; seek = seek->next)
6459 {
6460 if (next->module_name != seek->module_name)
6461 continue;
6462
6463 if (seek->non_intrinsic)
6464 non_intrinsic = true;
6465 else if (seek->intrinsic)
6466 intrinsic = true;
6467 else
6468 neither = true;
6469 }
6470
6471 if (intrinsic && neither && !non_intrinsic)
6472 {
6473 char *filename;
6474 FILE *fp;
6475
6476 filename = XALLOCAVEC (char,
6477 strlen (next->module_name)
6478 + strlen (MODULE_EXTENSION) + 1);
6479 strcpy (filename, next->module_name);
6480 strcat (filename, MODULE_EXTENSION);
6481 fp = gfc_open_included_file (filename, true, true);
6482 if (fp != NULL)
6483 {
6484 non_intrinsic = true;
6485 fclose (fp);
6486 }
6487 }
6488
6489 last = next;
6490 for (seek = next->next; seek; seek = last->next)
6491 {
6492 if (next->module_name != seek->module_name)
6493 {
6494 last = seek;
6495 continue;
6496 }
6497
6498 if ((!next->intrinsic && !seek->intrinsic)
6499 || (next->intrinsic && seek->intrinsic)
6500 || !non_intrinsic)
6501 {
6502 if (!seek->only_flag)
6503 next->only_flag = false;
6504 if (seek->rename)
6505 {
6506 gfc_use_rename *r = seek->rename;
6507 while (r->next)
6508 r = r->next;
6509 r->next = next->rename;
6510 next->rename = seek->rename;
6511 }
6512 last->next = seek->next;
6513 free (seek);
6514 }
6515 else
6516 last = seek;
6517 }
6518 }
6519
6520 for (; module_list; module_list = next)
6521 {
6522 next = module_list->next;
6523 rename_list_remove_duplicate (module_list->rename);
6524 gfc_use_module (module_list);
6525 free (module_list);
6526 }
6527 gfc_rename_list = NULL;
6528 }
6529
6530
6531 void
6532 gfc_free_use_stmts (gfc_use_list *use_stmts)
6533 {
6534 gfc_use_list *next;
6535 for (; use_stmts; use_stmts = next)
6536 {
6537 gfc_use_rename *next_rename;
6538
6539 for (; use_stmts->rename; use_stmts->rename = next_rename)
6540 {
6541 next_rename = use_stmts->rename->next;
6542 free (use_stmts->rename);
6543 }
6544 next = use_stmts->next;
6545 free (use_stmts);
6546 }
6547 }
6548
6549
6550 void
6551 gfc_module_init_2 (void)
6552 {
6553 last_atom = ATOM_LPAREN;
6554 gfc_rename_list = NULL;
6555 module_list = NULL;
6556 }
6557
6558
6559 void
6560 gfc_module_done_2 (void)
6561 {
6562 free_rename (gfc_rename_list);
6563 gfc_rename_list = NULL;
6564 }