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