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