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