1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
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
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
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/>. */
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.
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:
35 ( ( <Interface info for UPLUS> )
36 ( <Interface info for UMINUS> )
39 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
42 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
45 ( ( <common name> <symbol> <saved flag>)
51 ( <Symbol Number (in no particular order)>
53 <Module name of symbol>
54 ( <symbol information> )
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
69 #include "coretypes.h"
73 #include "parse.h" /* FIXME */
74 #include "constructor.h"
79 #include "double-int.h"
87 #include "stringpool.h"
91 #define MODULE_EXTENSION ".mod"
93 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
95 #define MOD_VERSION "13"
98 /* Structure that describes a position within a module file. */
107 /* Structure for list of symbols of intrinsic modules. */
120 P_UNKNOWN
= 0, P_OTHER
, P_NAMESPACE
, P_COMPONENT
, P_SYMBOL
124 /* The fixup structure lists pointers to pointers that have to
125 be updated when a pointer value becomes known. */
127 typedef struct fixup_t
130 struct fixup_t
*next
;
135 /* Structure for holding extra info needed for pointers being read. */
151 typedef struct pointer_info
153 BBT_HEADER (pointer_info
);
157 /* The first component of each member of the union is the pointer
164 void *pointer
; /* Member for doing pointer searches. */
169 char *true_name
, *module
, *binding_label
;
171 gfc_symtree
*symtree
;
172 enum gfc_rsym_state state
;
173 int ns
, referenced
, renamed
;
181 enum gfc_wsym_state state
;
190 #define gfc_get_pointer_info() XCNEW (pointer_info)
193 /* Local variables */
195 /* The gzFile for the module we're reading or writing. */
196 static gzFile module_fp
;
199 /* The name of the module we're reading (USE'ing) or writing. */
200 static const char *module_name
;
201 static gfc_use_list
*module_list
;
203 /* If we're reading an intrinsic module, this is its ID. */
204 static intmod_id current_intmod
;
206 /* Content of module. */
207 static char* module_content
;
209 static long module_pos
;
210 static int module_line
, module_column
, only_flag
;
211 static int prev_module_line
, prev_module_column
;
214 { IO_INPUT
, IO_OUTPUT
}
217 static gfc_use_rename
*gfc_rename_list
;
218 static pointer_info
*pi_root
;
219 static int symbol_number
; /* Counter for assigning symbol numbers */
221 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
222 static bool in_load_equiv
;
226 /*****************************************************************/
228 /* Pointer/integer conversion. Pointers between structures are stored
229 as integers in the module file. The next couple of subroutines
230 handle this translation for reading and writing. */
232 /* Recursively free the tree of pointer structures. */
235 free_pi_tree (pointer_info
*p
)
240 if (p
->fixup
!= NULL
)
241 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
243 free_pi_tree (p
->left
);
244 free_pi_tree (p
->right
);
246 if (iomode
== IO_INPUT
)
248 XDELETEVEC (p
->u
.rsym
.true_name
);
249 XDELETEVEC (p
->u
.rsym
.module
);
250 XDELETEVEC (p
->u
.rsym
.binding_label
);
257 /* Compare pointers when searching by pointer. Used when writing a
261 compare_pointers (void *_sn1
, void *_sn2
)
263 pointer_info
*sn1
, *sn2
;
265 sn1
= (pointer_info
*) _sn1
;
266 sn2
= (pointer_info
*) _sn2
;
268 if (sn1
->u
.pointer
< sn2
->u
.pointer
)
270 if (sn1
->u
.pointer
> sn2
->u
.pointer
)
277 /* Compare integers when searching by integer. Used when reading a
281 compare_integers (void *_sn1
, void *_sn2
)
283 pointer_info
*sn1
, *sn2
;
285 sn1
= (pointer_info
*) _sn1
;
286 sn2
= (pointer_info
*) _sn2
;
288 if (sn1
->integer
< sn2
->integer
)
290 if (sn1
->integer
> sn2
->integer
)
297 /* Initialize the pointer_info tree. */
306 compare
= (iomode
== IO_INPUT
) ? compare_integers
: compare_pointers
;
308 /* Pointer 0 is the NULL pointer. */
309 p
= gfc_get_pointer_info ();
314 gfc_insert_bbt (&pi_root
, p
, compare
);
316 /* Pointer 1 is the current namespace. */
317 p
= gfc_get_pointer_info ();
318 p
->u
.pointer
= gfc_current_ns
;
320 p
->type
= P_NAMESPACE
;
322 gfc_insert_bbt (&pi_root
, p
, compare
);
328 /* During module writing, call here with a pointer to something,
329 returning the pointer_info node. */
331 static pointer_info
*
332 find_pointer (void *gp
)
339 if (p
->u
.pointer
== gp
)
341 p
= (gp
< p
->u
.pointer
) ? p
->left
: p
->right
;
348 /* Given a pointer while writing, returns the pointer_info tree node,
349 creating it if it doesn't exist. */
351 static pointer_info
*
352 get_pointer (void *gp
)
356 p
= find_pointer (gp
);
360 /* Pointer doesn't have an integer. Give it one. */
361 p
= gfc_get_pointer_info ();
364 p
->integer
= symbol_number
++;
366 gfc_insert_bbt (&pi_root
, p
, compare_pointers
);
372 /* Given an integer during reading, find it in the pointer_info tree,
373 creating the node if not found. */
375 static pointer_info
*
376 get_integer (int integer
)
386 c
= compare_integers (&t
, p
);
390 p
= (c
< 0) ? p
->left
: p
->right
;
396 p
= gfc_get_pointer_info ();
397 p
->integer
= integer
;
400 gfc_insert_bbt (&pi_root
, p
, compare_integers
);
406 /* Resolve any fixups using a known pointer. */
409 resolve_fixups (fixup_t
*f
, void *gp
)
422 /* Convert a string such that it starts with a lower-case character. Used
423 to convert the symtree name of a derived-type to the symbol name or to
424 the name of the associated generic function. */
427 dt_lower_string (const char *name
)
429 if (name
[0] != (char) TOLOWER ((unsigned char) name
[0]))
430 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name
[0]),
432 return gfc_get_string (name
);
436 /* Convert a string such that it starts with an upper-case character. Used to
437 return the symtree-name for a derived type; the symbol name itself and the
438 symtree/symbol name of the associated generic function start with a lower-
442 dt_upper_string (const char *name
)
444 if (name
[0] != (char) TOUPPER ((unsigned char) name
[0]))
445 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name
[0]),
447 return gfc_get_string (name
);
450 /* Call here during module reading when we know what pointer to
451 associate with an integer. Any fixups that exist are resolved at
455 associate_integer_pointer (pointer_info
*p
, void *gp
)
457 if (p
->u
.pointer
!= NULL
)
458 gfc_internal_error ("associate_integer_pointer(): Already associated");
462 resolve_fixups (p
->fixup
, gp
);
468 /* During module reading, given an integer and a pointer to a pointer,
469 either store the pointer from an already-known value or create a
470 fixup structure in order to store things later. Returns zero if
471 the reference has been actually stored, or nonzero if the reference
472 must be fixed later (i.e., associate_integer_pointer must be called
473 sometime later. Returns the pointer_info structure. */
475 static pointer_info
*
476 add_fixup (int integer
, void *gp
)
482 p
= get_integer (integer
);
484 if (p
->integer
== 0 || p
->u
.pointer
!= NULL
)
487 *cp
= (char *) p
->u
.pointer
;
496 f
->pointer
= (void **) gp
;
503 /*****************************************************************/
505 /* Parser related subroutines */
507 /* Free the rename list left behind by a USE statement. */
510 free_rename (gfc_use_rename
*list
)
512 gfc_use_rename
*next
;
514 for (; list
; list
= next
)
522 /* Match a USE statement. */
527 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module_nature
[GFC_MAX_SYMBOL_LEN
+ 1];
528 gfc_use_rename
*tail
= NULL
, *new_use
;
529 interface_type type
, type2
;
532 gfc_use_list
*use_list
;
534 use_list
= gfc_get_use_list ();
536 if (gfc_match (" , ") == MATCH_YES
)
538 if ((m
= gfc_match (" %n ::", module_nature
)) == MATCH_YES
)
540 if (!gfc_notify_std (GFC_STD_F2003
, "module "
541 "nature in USE statement at %C"))
544 if (strcmp (module_nature
, "intrinsic") == 0)
545 use_list
->intrinsic
= true;
548 if (strcmp (module_nature
, "non_intrinsic") == 0)
549 use_list
->non_intrinsic
= true;
552 gfc_error ("Module nature in USE statement at %C shall "
553 "be either INTRINSIC or NON_INTRINSIC");
560 /* Help output a better error message than "Unclassifiable
562 gfc_match (" %n", module_nature
);
563 if (strcmp (module_nature
, "intrinsic") == 0
564 || strcmp (module_nature
, "non_intrinsic") == 0)
565 gfc_error ("\"::\" was expected after module nature at %C "
566 "but was not found");
573 m
= gfc_match (" ::");
574 if (m
== MATCH_YES
&&
575 !gfc_notify_std(GFC_STD_F2003
, "\"USE :: module\" at %C"))
580 m
= gfc_match ("% ");
589 use_list
->where
= gfc_current_locus
;
591 m
= gfc_match_name (name
);
598 use_list
->module_name
= gfc_get_string (name
);
600 if (gfc_match_eos () == MATCH_YES
)
603 if (gfc_match_char (',') != MATCH_YES
)
606 if (gfc_match (" only :") == MATCH_YES
)
607 use_list
->only_flag
= true;
609 if (gfc_match_eos () == MATCH_YES
)
614 /* Get a new rename struct and add it to the rename list. */
615 new_use
= gfc_get_use_rename ();
616 new_use
->where
= gfc_current_locus
;
619 if (use_list
->rename
== NULL
)
620 use_list
->rename
= new_use
;
622 tail
->next
= new_use
;
625 /* See what kind of interface we're dealing with. Assume it is
627 new_use
->op
= INTRINSIC_NONE
;
628 if (gfc_match_generic_spec (&type
, name
, &op
) == MATCH_ERROR
)
633 case INTERFACE_NAMELESS
:
634 gfc_error ("Missing generic specification in USE statement at %C");
637 case INTERFACE_USER_OP
:
638 case INTERFACE_GENERIC
:
639 m
= gfc_match (" =>");
641 if (type
== INTERFACE_USER_OP
&& m
== MATCH_YES
642 && (!gfc_notify_std(GFC_STD_F2003
, "Renaming "
643 "operators in USE statements at %C")))
646 if (type
== INTERFACE_USER_OP
)
647 new_use
->op
= INTRINSIC_USER
;
649 if (use_list
->only_flag
)
652 strcpy (new_use
->use_name
, name
);
655 strcpy (new_use
->local_name
, name
);
656 m
= gfc_match_generic_spec (&type2
, new_use
->use_name
, &op
);
661 if (m
== MATCH_ERROR
)
669 strcpy (new_use
->local_name
, name
);
671 m
= gfc_match_generic_spec (&type2
, new_use
->use_name
, &op
);
676 if (m
== MATCH_ERROR
)
680 if (strcmp (new_use
->use_name
, use_list
->module_name
) == 0
681 || strcmp (new_use
->local_name
, use_list
->module_name
) == 0)
683 gfc_error ("The name %qs at %C has already been used as "
684 "an external module name.", use_list
->module_name
);
689 case INTERFACE_INTRINSIC_OP
:
697 if (gfc_match_eos () == MATCH_YES
)
699 if (gfc_match_char (',') != MATCH_YES
)
706 gfc_use_list
*last
= module_list
;
709 last
->next
= use_list
;
712 module_list
= use_list
;
717 gfc_syntax_error (ST_USE
);
720 free_rename (use_list
->rename
);
726 /* Given a name and a number, inst, return the inst name
727 under which to load this symbol. Returns NULL if this
728 symbol shouldn't be loaded. If inst is zero, returns
729 the number of instances of this name. If interface is
730 true, a user-defined operator is sought, otherwise only
731 non-operators are sought. */
734 find_use_name_n (const char *name
, int *inst
, bool interface
)
737 const char *low_name
= NULL
;
740 /* For derived types. */
741 if (name
[0] != (char) TOLOWER ((unsigned char) name
[0]))
742 low_name
= dt_lower_string (name
);
745 for (u
= gfc_rename_list
; u
; u
= u
->next
)
747 if ((!low_name
&& strcmp (u
->use_name
, name
) != 0)
748 || (low_name
&& strcmp (u
->use_name
, low_name
) != 0)
749 || (u
->op
== INTRINSIC_USER
&& !interface
)
750 || (u
->op
!= INTRINSIC_USER
&& interface
))
763 return only_flag
? NULL
: name
;
769 if (u
->local_name
[0] == '\0')
771 return dt_upper_string (u
->local_name
);
774 return (u
->local_name
[0] != '\0') ? u
->local_name
: name
;
778 /* Given a name, return the name under which to load this symbol.
779 Returns NULL if this symbol shouldn't be loaded. */
782 find_use_name (const char *name
, bool interface
)
785 return find_use_name_n (name
, &i
, interface
);
789 /* Given a real name, return the number of use names associated with it. */
792 number_use_names (const char *name
, bool interface
)
795 find_use_name_n (name
, &i
, interface
);
800 /* Try to find the operator in the current list. */
802 static gfc_use_rename
*
803 find_use_operator (gfc_intrinsic_op op
)
807 for (u
= gfc_rename_list
; u
; u
= u
->next
)
815 /*****************************************************************/
817 /* The next couple of subroutines maintain a tree used to avoid a
818 brute-force search for a combination of true name and module name.
819 While symtree names, the name that a particular symbol is known by
820 can changed with USE statements, we still have to keep track of the
821 true names to generate the correct reference, and also avoid
822 loading the same real symbol twice in a program unit.
824 When we start reading, the true name tree is built and maintained
825 as symbols are read. The tree is searched as we load new symbols
826 to see if it already exists someplace in the namespace. */
828 typedef struct true_name
830 BBT_HEADER (true_name
);
836 static true_name
*true_name_root
;
839 /* Compare two true_name structures. */
842 compare_true_names (void *_t1
, void *_t2
)
847 t1
= (true_name
*) _t1
;
848 t2
= (true_name
*) _t2
;
850 c
= ((t1
->sym
->module
> t2
->sym
->module
)
851 - (t1
->sym
->module
< t2
->sym
->module
));
855 return strcmp (t1
->name
, t2
->name
);
859 /* Given a true name, search the true name tree to see if it exists
860 within the main namespace. */
863 find_true_name (const char *name
, const char *module
)
869 t
.name
= gfc_get_string (name
);
871 sym
.module
= gfc_get_string (module
);
879 c
= compare_true_names ((void *) (&t
), (void *) p
);
883 p
= (c
< 0) ? p
->left
: p
->right
;
890 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
893 add_true_name (gfc_symbol
*sym
)
897 t
= XCNEW (true_name
);
899 if (sym
->attr
.flavor
== FL_DERIVED
)
900 t
->name
= dt_upper_string (sym
->name
);
904 gfc_insert_bbt (&true_name_root
, t
, compare_true_names
);
908 /* Recursive function to build the initial true name tree by
909 recursively traversing the current namespace. */
912 build_tnt (gfc_symtree
*st
)
918 build_tnt (st
->left
);
919 build_tnt (st
->right
);
921 if (st
->n
.sym
->attr
.flavor
== FL_DERIVED
)
922 name
= dt_upper_string (st
->n
.sym
->name
);
924 name
= st
->n
.sym
->name
;
926 if (find_true_name (name
, st
->n
.sym
->module
) != NULL
)
929 add_true_name (st
->n
.sym
);
933 /* Initialize the true name tree with the current namespace. */
936 init_true_name_tree (void)
938 true_name_root
= NULL
;
939 build_tnt (gfc_current_ns
->sym_root
);
943 /* Recursively free a true name tree node. */
946 free_true_name (true_name
*t
)
950 free_true_name (t
->left
);
951 free_true_name (t
->right
);
957 /*****************************************************************/
959 /* Module reading and writing. */
961 /* The following are versions similar to the ones in scanner.c, but
962 for dealing with compressed module files. */
965 gzopen_included_file_1 (const char *name
, gfc_directorylist
*list
,
966 bool module
, bool system
)
969 gfc_directorylist
*p
;
972 for (p
= list
; p
; p
= p
->next
)
974 if (module
&& !p
->use_for_modules
)
977 fullname
= (char *) alloca(strlen (p
->path
) + strlen (name
) + 1);
978 strcpy (fullname
, p
->path
);
979 strcat (fullname
, name
);
981 f
= gzopen (fullname
, "r");
984 if (gfc_cpp_makedep ())
985 gfc_cpp_add_dep (fullname
, system
);
995 gzopen_included_file (const char *name
, bool include_cwd
, bool module
)
999 if (IS_ABSOLUTE_PATH (name
) || include_cwd
)
1001 f
= gzopen (name
, "r");
1002 if (f
&& gfc_cpp_makedep ())
1003 gfc_cpp_add_dep (name
, false);
1007 f
= gzopen_included_file_1 (name
, include_dirs
, module
, false);
1013 gzopen_intrinsic_module (const char* name
)
1017 if (IS_ABSOLUTE_PATH (name
))
1019 f
= gzopen (name
, "r");
1020 if (f
&& gfc_cpp_makedep ())
1021 gfc_cpp_add_dep (name
, true);
1025 f
= gzopen_included_file_1 (name
, intrinsic_modules_dirs
, true, true);
1033 ATOM_NAME
, ATOM_LPAREN
, ATOM_RPAREN
, ATOM_INTEGER
, ATOM_STRING
1037 static atom_type last_atom
;
1040 /* The name buffer must be at least as long as a symbol name. Right
1041 now it's not clear how we're going to store numeric constants--
1042 probably as a hexadecimal string, since this will allow the exact
1043 number to be preserved (this can't be done by a decimal
1044 representation). Worry about that later. TODO! */
1046 #define MAX_ATOM_SIZE 100
1048 static int atom_int
;
1049 static char *atom_string
, atom_name
[MAX_ATOM_SIZE
];
1052 /* Report problems with a module. Error reporting is not very
1053 elaborate, since this sorts of errors shouldn't really happen.
1054 This subroutine never returns. */
1056 static void bad_module (const char *) ATTRIBUTE_NORETURN
;
1059 bad_module (const char *msgid
)
1061 XDELETEVEC (module_content
);
1062 module_content
= NULL
;
1067 gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
1068 module_name
, module_line
, module_column
, msgid
);
1071 gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
1072 module_name
, module_line
, module_column
, msgid
);
1075 gfc_fatal_error ("Module %qs at line %d column %d: %s",
1076 module_name
, module_line
, module_column
, msgid
);
1082 /* Set the module's input pointer. */
1085 set_module_locus (module_locus
*m
)
1087 module_column
= m
->column
;
1088 module_line
= m
->line
;
1089 module_pos
= m
->pos
;
1093 /* Get the module's input pointer so that we can restore it later. */
1096 get_module_locus (module_locus
*m
)
1098 m
->column
= module_column
;
1099 m
->line
= module_line
;
1100 m
->pos
= module_pos
;
1104 /* Get the next character in the module, updating our reckoning of
1110 const char c
= module_content
[module_pos
++];
1112 bad_module ("Unexpected EOF");
1114 prev_module_line
= module_line
;
1115 prev_module_column
= module_column
;
1127 /* Unget a character while remembering the line and column. Works for
1128 a single character only. */
1131 module_unget_char (void)
1133 module_line
= prev_module_line
;
1134 module_column
= prev_module_column
;
1138 /* Parse a string constant. The delimiter is guaranteed to be a
1148 atom_string
= XNEWVEC (char, cursz
);
1156 int c2
= module_char ();
1159 module_unget_char ();
1167 atom_string
= XRESIZEVEC (char, atom_string
, cursz
);
1169 atom_string
[len
] = c
;
1173 atom_string
= XRESIZEVEC (char, atom_string
, len
+ 1);
1174 atom_string
[len
] = '\0'; /* C-style string for debug purposes. */
1178 /* Parse a small integer. */
1181 parse_integer (int c
)
1190 module_unget_char ();
1194 atom_int
= 10 * atom_int
+ c
- '0';
1195 if (atom_int
> 99999999)
1196 bad_module ("Integer overflow");
1218 if (!ISALNUM (c
) && c
!= '_' && c
!= '-')
1220 module_unget_char ();
1225 if (++len
> GFC_MAX_SYMBOL_LEN
)
1226 bad_module ("Name too long");
1234 /* Read the next atom in the module's input stream. */
1245 while (c
== ' ' || c
== '\r' || c
== '\n');
1270 return ATOM_INTEGER
;
1328 bad_module ("Bad name");
1335 /* Peek at the next atom on the input. */
1346 while (c
== ' ' || c
== '\r' || c
== '\n');
1351 module_unget_char ();
1355 module_unget_char ();
1359 module_unget_char ();
1372 module_unget_char ();
1373 return ATOM_INTEGER
;
1427 module_unget_char ();
1431 bad_module ("Bad name");
1436 /* Read the next atom from the input, requiring that it be a
1440 require_atom (atom_type type
)
1446 column
= module_column
;
1455 p
= _("Expected name");
1458 p
= _("Expected left parenthesis");
1461 p
= _("Expected right parenthesis");
1464 p
= _("Expected integer");
1467 p
= _("Expected string");
1470 gfc_internal_error ("require_atom(): bad atom type required");
1473 module_column
= column
;
1480 /* Given a pointer to an mstring array, require that the current input
1481 be one of the strings in the array. We return the enum value. */
1484 find_enum (const mstring
*m
)
1488 i
= gfc_string2code (m
, atom_name
);
1492 bad_module ("find_enum(): Enum not found");
1498 /* Read a string. The caller is responsible for freeing. */
1504 require_atom (ATOM_STRING
);
1511 /**************** Module output subroutines ***************************/
1513 /* Output a character to a module file. */
1516 write_char (char out
)
1518 if (gzputc (module_fp
, out
) == EOF
)
1519 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno
));
1531 /* Write an atom to a module. The line wrapping isn't perfect, but it
1532 should work most of the time. This isn't that big of a deal, since
1533 the file really isn't meant to be read by people anyway. */
1536 write_atom (atom_type atom
, const void *v
)
1546 p
= (const char *) v
;
1558 i
= *((const int *) v
);
1560 gfc_internal_error ("write_atom(): Writing negative integer");
1562 sprintf (buffer
, "%d", i
);
1567 gfc_internal_error ("write_atom(): Trying to write dab atom");
1571 if(p
== NULL
|| *p
== '\0')
1576 if (atom
!= ATOM_RPAREN
)
1578 if (module_column
+ len
> 72)
1583 if (last_atom
!= ATOM_LPAREN
&& module_column
!= 1)
1588 if (atom
== ATOM_STRING
)
1591 while (p
!= NULL
&& *p
)
1593 if (atom
== ATOM_STRING
&& *p
== '\'')
1598 if (atom
== ATOM_STRING
)
1606 /***************** Mid-level I/O subroutines *****************/
1608 /* These subroutines let their caller read or write atoms without
1609 caring about which of the two is actually happening. This lets a
1610 subroutine concentrate on the actual format of the data being
1613 static void mio_expr (gfc_expr
**);
1614 pointer_info
*mio_symbol_ref (gfc_symbol
**);
1615 pointer_info
*mio_interface_rest (gfc_interface
**);
1616 static void mio_symtree_ref (gfc_symtree
**);
1618 /* Read or write an enumerated value. On writing, we return the input
1619 value for the convenience of callers. We avoid using an integer
1620 pointer because enums are sometimes inside bitfields. */
1623 mio_name (int t
, const mstring
*m
)
1625 if (iomode
== IO_OUTPUT
)
1626 write_atom (ATOM_NAME
, gfc_code2string (m
, t
));
1629 require_atom (ATOM_NAME
);
1636 /* Specialization of mio_name. */
1638 #define DECL_MIO_NAME(TYPE) \
1639 static inline TYPE \
1640 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1642 return (TYPE) mio_name ((int) t, m); \
1644 #define MIO_NAME(TYPE) mio_name_##TYPE
1649 if (iomode
== IO_OUTPUT
)
1650 write_atom (ATOM_LPAREN
, NULL
);
1652 require_atom (ATOM_LPAREN
);
1659 if (iomode
== IO_OUTPUT
)
1660 write_atom (ATOM_RPAREN
, NULL
);
1662 require_atom (ATOM_RPAREN
);
1667 mio_integer (int *ip
)
1669 if (iomode
== IO_OUTPUT
)
1670 write_atom (ATOM_INTEGER
, ip
);
1673 require_atom (ATOM_INTEGER
);
1679 /* Read or write a gfc_intrinsic_op value. */
1682 mio_intrinsic_op (gfc_intrinsic_op
* op
)
1684 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1685 if (iomode
== IO_OUTPUT
)
1687 int converted
= (int) *op
;
1688 write_atom (ATOM_INTEGER
, &converted
);
1692 require_atom (ATOM_INTEGER
);
1693 *op
= (gfc_intrinsic_op
) atom_int
;
1698 /* Read or write a character pointer that points to a string on the heap. */
1701 mio_allocated_string (const char *s
)
1703 if (iomode
== IO_OUTPUT
)
1705 write_atom (ATOM_STRING
, s
);
1710 require_atom (ATOM_STRING
);
1716 /* Functions for quoting and unquoting strings. */
1719 quote_string (const gfc_char_t
*s
, const size_t slength
)
1721 const gfc_char_t
*p
;
1725 /* Calculate the length we'll need: a backslash takes two ("\\"),
1726 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1727 for (p
= s
, i
= 0; i
< slength
; p
++, i
++)
1731 else if (!gfc_wide_is_printable (*p
))
1737 q
= res
= XCNEWVEC (char, len
+ 1);
1738 for (p
= s
, i
= 0; i
< slength
; p
++, i
++)
1741 *q
++ = '\\', *q
++ = '\\';
1742 else if (!gfc_wide_is_printable (*p
))
1744 sprintf (q
, "\\U%08" HOST_WIDE_INT_PRINT
"x",
1745 (unsigned HOST_WIDE_INT
) *p
);
1749 *q
++ = (unsigned char) *p
;
1757 unquote_string (const char *s
)
1763 for (p
= s
, len
= 0; *p
; p
++, len
++)
1770 else if (p
[1] == 'U')
1771 p
+= 9; /* That is a "\U????????". */
1773 gfc_internal_error ("unquote_string(): got bad string");
1776 res
= gfc_get_wide_string (len
+ 1);
1777 for (i
= 0, p
= s
; i
< len
; i
++, p
++)
1782 res
[i
] = (unsigned char) *p
;
1783 else if (p
[1] == '\\')
1785 res
[i
] = (unsigned char) '\\';
1790 /* We read the 8-digits hexadecimal constant that follows. */
1795 gcc_assert (p
[1] == 'U');
1796 for (j
= 0; j
< 8; j
++)
1799 gcc_assert (sscanf (&p
[j
+2], "%01x", &n
) == 1);
1813 /* Read or write a character pointer that points to a wide string on the
1814 heap, performing quoting/unquoting of nonprintable characters using the
1815 form \U???????? (where each ? is a hexadecimal digit).
1816 Length is the length of the string, only known and used in output mode. */
1818 static const gfc_char_t
*
1819 mio_allocated_wide_string (const gfc_char_t
*s
, const size_t length
)
1821 if (iomode
== IO_OUTPUT
)
1823 char *quoted
= quote_string (s
, length
);
1824 write_atom (ATOM_STRING
, quoted
);
1830 gfc_char_t
*unquoted
;
1832 require_atom (ATOM_STRING
);
1833 unquoted
= unquote_string (atom_string
);
1840 /* Read or write a string that is in static memory. */
1843 mio_pool_string (const char **stringp
)
1845 /* TODO: one could write the string only once, and refer to it via a
1848 /* As a special case we have to deal with a NULL string. This
1849 happens for the 'module' member of 'gfc_symbol's that are not in a
1850 module. We read / write these as the empty string. */
1851 if (iomode
== IO_OUTPUT
)
1853 const char *p
= *stringp
== NULL
? "" : *stringp
;
1854 write_atom (ATOM_STRING
, p
);
1858 require_atom (ATOM_STRING
);
1859 *stringp
= atom_string
[0] == '\0' ? NULL
: gfc_get_string (atom_string
);
1865 /* Read or write a string that is inside of some already-allocated
1869 mio_internal_string (char *string
)
1871 if (iomode
== IO_OUTPUT
)
1872 write_atom (ATOM_STRING
, string
);
1875 require_atom (ATOM_STRING
);
1876 strcpy (string
, atom_string
);
1883 { AB_ALLOCATABLE
, AB_DIMENSION
, AB_EXTERNAL
, AB_INTRINSIC
, AB_OPTIONAL
,
1884 AB_POINTER
, AB_TARGET
, AB_DUMMY
, AB_RESULT
, AB_DATA
,
1885 AB_IN_NAMELIST
, AB_IN_COMMON
, AB_FUNCTION
, AB_SUBROUTINE
, AB_SEQUENCE
,
1886 AB_ELEMENTAL
, AB_PURE
, AB_RECURSIVE
, AB_GENERIC
, AB_ALWAYS_EXPLICIT
,
1887 AB_CRAY_POINTER
, AB_CRAY_POINTEE
, AB_THREADPRIVATE
,
1888 AB_ALLOC_COMP
, AB_POINTER_COMP
, AB_PROC_POINTER_COMP
, AB_PRIVATE_COMP
,
1889 AB_VALUE
, AB_VOLATILE
, AB_PROTECTED
, AB_LOCK_COMP
,
1890 AB_IS_BIND_C
, AB_IS_C_INTEROP
, AB_IS_ISO_C
, AB_ABSTRACT
, AB_ZERO_COMP
,
1891 AB_IS_CLASS
, AB_PROCEDURE
, AB_PROC_POINTER
, AB_ASYNCHRONOUS
, AB_CODIMENSION
,
1892 AB_COARRAY_COMP
, AB_VTYPE
, AB_VTAB
, AB_CONTIGUOUS
, AB_CLASS_POINTER
,
1893 AB_IMPLICIT_PURE
, AB_ARTIFICIAL
, AB_UNLIMITED_POLY
, AB_OMP_DECLARE_TARGET
1897 static const mstring attr_bits
[] =
1899 minit ("ALLOCATABLE", AB_ALLOCATABLE
),
1900 minit ("ARTIFICIAL", AB_ARTIFICIAL
),
1901 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS
),
1902 minit ("DIMENSION", AB_DIMENSION
),
1903 minit ("CODIMENSION", AB_CODIMENSION
),
1904 minit ("CONTIGUOUS", AB_CONTIGUOUS
),
1905 minit ("EXTERNAL", AB_EXTERNAL
),
1906 minit ("INTRINSIC", AB_INTRINSIC
),
1907 minit ("OPTIONAL", AB_OPTIONAL
),
1908 minit ("POINTER", AB_POINTER
),
1909 minit ("VOLATILE", AB_VOLATILE
),
1910 minit ("TARGET", AB_TARGET
),
1911 minit ("THREADPRIVATE", AB_THREADPRIVATE
),
1912 minit ("DUMMY", AB_DUMMY
),
1913 minit ("RESULT", AB_RESULT
),
1914 minit ("DATA", AB_DATA
),
1915 minit ("IN_NAMELIST", AB_IN_NAMELIST
),
1916 minit ("IN_COMMON", AB_IN_COMMON
),
1917 minit ("FUNCTION", AB_FUNCTION
),
1918 minit ("SUBROUTINE", AB_SUBROUTINE
),
1919 minit ("SEQUENCE", AB_SEQUENCE
),
1920 minit ("ELEMENTAL", AB_ELEMENTAL
),
1921 minit ("PURE", AB_PURE
),
1922 minit ("RECURSIVE", AB_RECURSIVE
),
1923 minit ("GENERIC", AB_GENERIC
),
1924 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT
),
1925 minit ("CRAY_POINTER", AB_CRAY_POINTER
),
1926 minit ("CRAY_POINTEE", AB_CRAY_POINTEE
),
1927 minit ("IS_BIND_C", AB_IS_BIND_C
),
1928 minit ("IS_C_INTEROP", AB_IS_C_INTEROP
),
1929 minit ("IS_ISO_C", AB_IS_ISO_C
),
1930 minit ("VALUE", AB_VALUE
),
1931 minit ("ALLOC_COMP", AB_ALLOC_COMP
),
1932 minit ("COARRAY_COMP", AB_COARRAY_COMP
),
1933 minit ("LOCK_COMP", AB_LOCK_COMP
),
1934 minit ("POINTER_COMP", AB_POINTER_COMP
),
1935 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP
),
1936 minit ("PRIVATE_COMP", AB_PRIVATE_COMP
),
1937 minit ("ZERO_COMP", AB_ZERO_COMP
),
1938 minit ("PROTECTED", AB_PROTECTED
),
1939 minit ("ABSTRACT", AB_ABSTRACT
),
1940 minit ("IS_CLASS", AB_IS_CLASS
),
1941 minit ("PROCEDURE", AB_PROCEDURE
),
1942 minit ("PROC_POINTER", AB_PROC_POINTER
),
1943 minit ("VTYPE", AB_VTYPE
),
1944 minit ("VTAB", AB_VTAB
),
1945 minit ("CLASS_POINTER", AB_CLASS_POINTER
),
1946 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE
),
1947 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY
),
1948 minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET
),
1952 /* For binding attributes. */
1953 static const mstring binding_passing
[] =
1956 minit ("NOPASS", 1),
1959 static const mstring binding_overriding
[] =
1961 minit ("OVERRIDABLE", 0),
1962 minit ("NON_OVERRIDABLE", 1),
1963 minit ("DEFERRED", 2),
1966 static const mstring binding_generic
[] =
1968 minit ("SPECIFIC", 0),
1969 minit ("GENERIC", 1),
1972 static const mstring binding_ppc
[] =
1974 minit ("NO_PPC", 0),
1979 /* Specialization of mio_name. */
1980 DECL_MIO_NAME (ab_attribute
)
1981 DECL_MIO_NAME (ar_type
)
1982 DECL_MIO_NAME (array_type
)
1984 DECL_MIO_NAME (expr_t
)
1985 DECL_MIO_NAME (gfc_access
)
1986 DECL_MIO_NAME (gfc_intrinsic_op
)
1987 DECL_MIO_NAME (ifsrc
)
1988 DECL_MIO_NAME (save_state
)
1989 DECL_MIO_NAME (procedure_type
)
1990 DECL_MIO_NAME (ref_type
)
1991 DECL_MIO_NAME (sym_flavor
)
1992 DECL_MIO_NAME (sym_intent
)
1993 #undef DECL_MIO_NAME
1995 /* Symbol attributes are stored in list with the first three elements
1996 being the enumerated fields, while the remaining elements (if any)
1997 indicate the individual attribute bits. The access field is not
1998 saved-- it controls what symbols are exported when a module is
2002 mio_symbol_attribute (symbol_attribute
*attr
)
2005 unsigned ext_attr
,extension_level
;
2009 attr
->flavor
= MIO_NAME (sym_flavor
) (attr
->flavor
, flavors
);
2010 attr
->intent
= MIO_NAME (sym_intent
) (attr
->intent
, intents
);
2011 attr
->proc
= MIO_NAME (procedure_type
) (attr
->proc
, procedures
);
2012 attr
->if_source
= MIO_NAME (ifsrc
) (attr
->if_source
, ifsrc_types
);
2013 attr
->save
= MIO_NAME (save_state
) (attr
->save
, save_status
);
2015 ext_attr
= attr
->ext_attr
;
2016 mio_integer ((int *) &ext_attr
);
2017 attr
->ext_attr
= ext_attr
;
2019 extension_level
= attr
->extension
;
2020 mio_integer ((int *) &extension_level
);
2021 attr
->extension
= extension_level
;
2023 if (iomode
== IO_OUTPUT
)
2025 if (attr
->allocatable
)
2026 MIO_NAME (ab_attribute
) (AB_ALLOCATABLE
, attr_bits
);
2027 if (attr
->artificial
)
2028 MIO_NAME (ab_attribute
) (AB_ARTIFICIAL
, attr_bits
);
2029 if (attr
->asynchronous
)
2030 MIO_NAME (ab_attribute
) (AB_ASYNCHRONOUS
, attr_bits
);
2031 if (attr
->dimension
)
2032 MIO_NAME (ab_attribute
) (AB_DIMENSION
, attr_bits
);
2033 if (attr
->codimension
)
2034 MIO_NAME (ab_attribute
) (AB_CODIMENSION
, attr_bits
);
2035 if (attr
->contiguous
)
2036 MIO_NAME (ab_attribute
) (AB_CONTIGUOUS
, attr_bits
);
2038 MIO_NAME (ab_attribute
) (AB_EXTERNAL
, attr_bits
);
2039 if (attr
->intrinsic
)
2040 MIO_NAME (ab_attribute
) (AB_INTRINSIC
, attr_bits
);
2042 MIO_NAME (ab_attribute
) (AB_OPTIONAL
, attr_bits
);
2044 MIO_NAME (ab_attribute
) (AB_POINTER
, attr_bits
);
2045 if (attr
->class_pointer
)
2046 MIO_NAME (ab_attribute
) (AB_CLASS_POINTER
, attr_bits
);
2047 if (attr
->is_protected
)
2048 MIO_NAME (ab_attribute
) (AB_PROTECTED
, attr_bits
);
2050 MIO_NAME (ab_attribute
) (AB_VALUE
, attr_bits
);
2051 if (attr
->volatile_
)
2052 MIO_NAME (ab_attribute
) (AB_VOLATILE
, attr_bits
);
2054 MIO_NAME (ab_attribute
) (AB_TARGET
, attr_bits
);
2055 if (attr
->threadprivate
)
2056 MIO_NAME (ab_attribute
) (AB_THREADPRIVATE
, attr_bits
);
2058 MIO_NAME (ab_attribute
) (AB_DUMMY
, attr_bits
);
2060 MIO_NAME (ab_attribute
) (AB_RESULT
, attr_bits
);
2061 /* We deliberately don't preserve the "entry" flag. */
2064 MIO_NAME (ab_attribute
) (AB_DATA
, attr_bits
);
2065 if (attr
->in_namelist
)
2066 MIO_NAME (ab_attribute
) (AB_IN_NAMELIST
, attr_bits
);
2067 if (attr
->in_common
)
2068 MIO_NAME (ab_attribute
) (AB_IN_COMMON
, attr_bits
);
2071 MIO_NAME (ab_attribute
) (AB_FUNCTION
, attr_bits
);
2072 if (attr
->subroutine
)
2073 MIO_NAME (ab_attribute
) (AB_SUBROUTINE
, attr_bits
);
2075 MIO_NAME (ab_attribute
) (AB_GENERIC
, attr_bits
);
2077 MIO_NAME (ab_attribute
) (AB_ABSTRACT
, attr_bits
);
2080 MIO_NAME (ab_attribute
) (AB_SEQUENCE
, attr_bits
);
2081 if (attr
->elemental
)
2082 MIO_NAME (ab_attribute
) (AB_ELEMENTAL
, attr_bits
);
2084 MIO_NAME (ab_attribute
) (AB_PURE
, attr_bits
);
2085 if (attr
->implicit_pure
)
2086 MIO_NAME (ab_attribute
) (AB_IMPLICIT_PURE
, attr_bits
);
2087 if (attr
->unlimited_polymorphic
)
2088 MIO_NAME (ab_attribute
) (AB_UNLIMITED_POLY
, attr_bits
);
2089 if (attr
->recursive
)
2090 MIO_NAME (ab_attribute
) (AB_RECURSIVE
, attr_bits
);
2091 if (attr
->always_explicit
)
2092 MIO_NAME (ab_attribute
) (AB_ALWAYS_EXPLICIT
, attr_bits
);
2093 if (attr
->cray_pointer
)
2094 MIO_NAME (ab_attribute
) (AB_CRAY_POINTER
, attr_bits
);
2095 if (attr
->cray_pointee
)
2096 MIO_NAME (ab_attribute
) (AB_CRAY_POINTEE
, attr_bits
);
2097 if (attr
->is_bind_c
)
2098 MIO_NAME(ab_attribute
) (AB_IS_BIND_C
, attr_bits
);
2099 if (attr
->is_c_interop
)
2100 MIO_NAME(ab_attribute
) (AB_IS_C_INTEROP
, attr_bits
);
2102 MIO_NAME(ab_attribute
) (AB_IS_ISO_C
, attr_bits
);
2103 if (attr
->alloc_comp
)
2104 MIO_NAME (ab_attribute
) (AB_ALLOC_COMP
, attr_bits
);
2105 if (attr
->pointer_comp
)
2106 MIO_NAME (ab_attribute
) (AB_POINTER_COMP
, attr_bits
);
2107 if (attr
->proc_pointer_comp
)
2108 MIO_NAME (ab_attribute
) (AB_PROC_POINTER_COMP
, attr_bits
);
2109 if (attr
->private_comp
)
2110 MIO_NAME (ab_attribute
) (AB_PRIVATE_COMP
, attr_bits
);
2111 if (attr
->coarray_comp
)
2112 MIO_NAME (ab_attribute
) (AB_COARRAY_COMP
, attr_bits
);
2113 if (attr
->lock_comp
)
2114 MIO_NAME (ab_attribute
) (AB_LOCK_COMP
, attr_bits
);
2115 if (attr
->zero_comp
)
2116 MIO_NAME (ab_attribute
) (AB_ZERO_COMP
, attr_bits
);
2118 MIO_NAME (ab_attribute
) (AB_IS_CLASS
, attr_bits
);
2119 if (attr
->procedure
)
2120 MIO_NAME (ab_attribute
) (AB_PROCEDURE
, attr_bits
);
2121 if (attr
->proc_pointer
)
2122 MIO_NAME (ab_attribute
) (AB_PROC_POINTER
, attr_bits
);
2124 MIO_NAME (ab_attribute
) (AB_VTYPE
, attr_bits
);
2126 MIO_NAME (ab_attribute
) (AB_VTAB
, attr_bits
);
2127 if (attr
->omp_declare_target
)
2128 MIO_NAME (ab_attribute
) (AB_OMP_DECLARE_TARGET
, attr_bits
);
2138 if (t
== ATOM_RPAREN
)
2141 bad_module ("Expected attribute bit name");
2143 switch ((ab_attribute
) find_enum (attr_bits
))
2145 case AB_ALLOCATABLE
:
2146 attr
->allocatable
= 1;
2149 attr
->artificial
= 1;
2151 case AB_ASYNCHRONOUS
:
2152 attr
->asynchronous
= 1;
2155 attr
->dimension
= 1;
2157 case AB_CODIMENSION
:
2158 attr
->codimension
= 1;
2161 attr
->contiguous
= 1;
2167 attr
->intrinsic
= 1;
2175 case AB_CLASS_POINTER
:
2176 attr
->class_pointer
= 1;
2179 attr
->is_protected
= 1;
2185 attr
->volatile_
= 1;
2190 case AB_THREADPRIVATE
:
2191 attr
->threadprivate
= 1;
2202 case AB_IN_NAMELIST
:
2203 attr
->in_namelist
= 1;
2206 attr
->in_common
= 1;
2212 attr
->subroutine
= 1;
2224 attr
->elemental
= 1;
2229 case AB_IMPLICIT_PURE
:
2230 attr
->implicit_pure
= 1;
2232 case AB_UNLIMITED_POLY
:
2233 attr
->unlimited_polymorphic
= 1;
2236 attr
->recursive
= 1;
2238 case AB_ALWAYS_EXPLICIT
:
2239 attr
->always_explicit
= 1;
2241 case AB_CRAY_POINTER
:
2242 attr
->cray_pointer
= 1;
2244 case AB_CRAY_POINTEE
:
2245 attr
->cray_pointee
= 1;
2248 attr
->is_bind_c
= 1;
2250 case AB_IS_C_INTEROP
:
2251 attr
->is_c_interop
= 1;
2257 attr
->alloc_comp
= 1;
2259 case AB_COARRAY_COMP
:
2260 attr
->coarray_comp
= 1;
2263 attr
->lock_comp
= 1;
2265 case AB_POINTER_COMP
:
2266 attr
->pointer_comp
= 1;
2268 case AB_PROC_POINTER_COMP
:
2269 attr
->proc_pointer_comp
= 1;
2271 case AB_PRIVATE_COMP
:
2272 attr
->private_comp
= 1;
2275 attr
->zero_comp
= 1;
2281 attr
->procedure
= 1;
2283 case AB_PROC_POINTER
:
2284 attr
->proc_pointer
= 1;
2292 case AB_OMP_DECLARE_TARGET
:
2293 attr
->omp_declare_target
= 1;
2301 static const mstring bt_types
[] = {
2302 minit ("INTEGER", BT_INTEGER
),
2303 minit ("REAL", BT_REAL
),
2304 minit ("COMPLEX", BT_COMPLEX
),
2305 minit ("LOGICAL", BT_LOGICAL
),
2306 minit ("CHARACTER", BT_CHARACTER
),
2307 minit ("DERIVED", BT_DERIVED
),
2308 minit ("CLASS", BT_CLASS
),
2309 minit ("PROCEDURE", BT_PROCEDURE
),
2310 minit ("UNKNOWN", BT_UNKNOWN
),
2311 minit ("VOID", BT_VOID
),
2312 minit ("ASSUMED", BT_ASSUMED
),
2318 mio_charlen (gfc_charlen
**clp
)
2324 if (iomode
== IO_OUTPUT
)
2328 mio_expr (&cl
->length
);
2332 if (peek_atom () != ATOM_RPAREN
)
2334 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2335 mio_expr (&cl
->length
);
2344 /* See if a name is a generated name. */
2347 check_unique_name (const char *name
)
2349 return *name
== '@';
2354 mio_typespec (gfc_typespec
*ts
)
2358 ts
->type
= MIO_NAME (bt
) (ts
->type
, bt_types
);
2360 if (ts
->type
!= BT_DERIVED
&& ts
->type
!= BT_CLASS
)
2361 mio_integer (&ts
->kind
);
2363 mio_symbol_ref (&ts
->u
.derived
);
2365 mio_symbol_ref (&ts
->interface
);
2367 /* Add info for C interop and is_iso_c. */
2368 mio_integer (&ts
->is_c_interop
);
2369 mio_integer (&ts
->is_iso_c
);
2371 /* If the typespec is for an identifier either from iso_c_binding, or
2372 a constant that was initialized to an identifier from it, use the
2373 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2375 ts
->f90_type
= MIO_NAME (bt
) (ts
->f90_type
, bt_types
);
2377 ts
->f90_type
= MIO_NAME (bt
) (ts
->type
, bt_types
);
2379 if (ts
->type
!= BT_CHARACTER
)
2381 /* ts->u.cl is only valid for BT_CHARACTER. */
2386 mio_charlen (&ts
->u
.cl
);
2388 /* So as not to disturb the existing API, use an ATOM_NAME to
2389 transmit deferred characteristic for characters (F2003). */
2390 if (iomode
== IO_OUTPUT
)
2392 if (ts
->type
== BT_CHARACTER
&& ts
->deferred
)
2393 write_atom (ATOM_NAME
, "DEFERRED_CL");
2395 else if (peek_atom () != ATOM_RPAREN
)
2397 if (parse_atom () != ATOM_NAME
)
2398 bad_module ("Expected string");
2406 static const mstring array_spec_types
[] = {
2407 minit ("EXPLICIT", AS_EXPLICIT
),
2408 minit ("ASSUMED_RANK", AS_ASSUMED_RANK
),
2409 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE
),
2410 minit ("DEFERRED", AS_DEFERRED
),
2411 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE
),
2417 mio_array_spec (gfc_array_spec
**asp
)
2424 if (iomode
== IO_OUTPUT
)
2432 /* mio_integer expects nonnegative values. */
2433 rank
= as
->rank
> 0 ? as
->rank
: 0;
2434 mio_integer (&rank
);
2438 if (peek_atom () == ATOM_RPAREN
)
2444 *asp
= as
= gfc_get_array_spec ();
2445 mio_integer (&as
->rank
);
2448 mio_integer (&as
->corank
);
2449 as
->type
= MIO_NAME (array_type
) (as
->type
, array_spec_types
);
2451 if (iomode
== IO_INPUT
&& as
->type
== AS_ASSUMED_RANK
)
2453 if (iomode
== IO_INPUT
&& as
->corank
)
2454 as
->cotype
= (as
->type
== AS_DEFERRED
) ? AS_DEFERRED
: AS_EXPLICIT
;
2456 if (as
->rank
+ as
->corank
> 0)
2457 for (i
= 0; i
< as
->rank
+ as
->corank
; i
++)
2459 mio_expr (&as
->lower
[i
]);
2460 mio_expr (&as
->upper
[i
]);
2468 /* Given a pointer to an array reference structure (which lives in a
2469 gfc_ref structure), find the corresponding array specification
2470 structure. Storing the pointer in the ref structure doesn't quite
2471 work when loading from a module. Generating code for an array
2472 reference also needs more information than just the array spec. */
2474 static const mstring array_ref_types
[] = {
2475 minit ("FULL", AR_FULL
),
2476 minit ("ELEMENT", AR_ELEMENT
),
2477 minit ("SECTION", AR_SECTION
),
2483 mio_array_ref (gfc_array_ref
*ar
)
2488 ar
->type
= MIO_NAME (ar_type
) (ar
->type
, array_ref_types
);
2489 mio_integer (&ar
->dimen
);
2497 for (i
= 0; i
< ar
->dimen
; i
++)
2498 mio_expr (&ar
->start
[i
]);
2503 for (i
= 0; i
< ar
->dimen
; i
++)
2505 mio_expr (&ar
->start
[i
]);
2506 mio_expr (&ar
->end
[i
]);
2507 mio_expr (&ar
->stride
[i
]);
2513 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2516 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2517 we can't call mio_integer directly. Instead loop over each element
2518 and cast it to/from an integer. */
2519 if (iomode
== IO_OUTPUT
)
2521 for (i
= 0; i
< ar
->dimen
; i
++)
2523 int tmp
= (int)ar
->dimen_type
[i
];
2524 write_atom (ATOM_INTEGER
, &tmp
);
2529 for (i
= 0; i
< ar
->dimen
; i
++)
2531 require_atom (ATOM_INTEGER
);
2532 ar
->dimen_type
[i
] = (enum gfc_array_ref_dimen_type
) atom_int
;
2536 if (iomode
== IO_INPUT
)
2538 ar
->where
= gfc_current_locus
;
2540 for (i
= 0; i
< ar
->dimen
; i
++)
2541 ar
->c_where
[i
] = gfc_current_locus
;
2548 /* Saves or restores a pointer. The pointer is converted back and
2549 forth from an integer. We return the pointer_info pointer so that
2550 the caller can take additional action based on the pointer type. */
2552 static pointer_info
*
2553 mio_pointer_ref (void *gp
)
2557 if (iomode
== IO_OUTPUT
)
2559 p
= get_pointer (*((char **) gp
));
2560 write_atom (ATOM_INTEGER
, &p
->integer
);
2564 require_atom (ATOM_INTEGER
);
2565 p
= add_fixup (atom_int
, gp
);
2572 /* Save and load references to components that occur within
2573 expressions. We have to describe these references by a number and
2574 by name. The number is necessary for forward references during
2575 reading, and the name is necessary if the symbol already exists in
2576 the namespace and is not loaded again. */
2579 mio_component_ref (gfc_component
**cp
)
2583 p
= mio_pointer_ref (cp
);
2584 if (p
->type
== P_UNKNOWN
)
2585 p
->type
= P_COMPONENT
;
2589 static void mio_namespace_ref (gfc_namespace
**nsp
);
2590 static void mio_formal_arglist (gfc_formal_arglist
**formal
);
2591 static void mio_typebound_proc (gfc_typebound_proc
** proc
);
2594 mio_component (gfc_component
*c
, int vtype
)
2601 if (iomode
== IO_OUTPUT
)
2603 p
= get_pointer (c
);
2604 mio_integer (&p
->integer
);
2609 p
= get_integer (n
);
2610 associate_integer_pointer (p
, c
);
2613 if (p
->type
== P_UNKNOWN
)
2614 p
->type
= P_COMPONENT
;
2616 mio_pool_string (&c
->name
);
2617 mio_typespec (&c
->ts
);
2618 mio_array_spec (&c
->as
);
2620 mio_symbol_attribute (&c
->attr
);
2621 if (c
->ts
.type
== BT_CLASS
)
2622 c
->attr
.class_ok
= 1;
2623 c
->attr
.access
= MIO_NAME (gfc_access
) (c
->attr
.access
, access_types
);
2625 if (!vtype
|| strcmp (c
->name
, "_final") == 0
2626 || strcmp (c
->name
, "_hash") == 0)
2627 mio_expr (&c
->initializer
);
2629 if (c
->attr
.proc_pointer
)
2630 mio_typebound_proc (&c
->tb
);
2637 mio_component_list (gfc_component
**cp
, int vtype
)
2639 gfc_component
*c
, *tail
;
2643 if (iomode
== IO_OUTPUT
)
2645 for (c
= *cp
; c
; c
= c
->next
)
2646 mio_component (c
, vtype
);
2655 if (peek_atom () == ATOM_RPAREN
)
2658 c
= gfc_get_component ();
2659 mio_component (c
, vtype
);
2675 mio_actual_arg (gfc_actual_arglist
*a
)
2678 mio_pool_string (&a
->name
);
2679 mio_expr (&a
->expr
);
2685 mio_actual_arglist (gfc_actual_arglist
**ap
)
2687 gfc_actual_arglist
*a
, *tail
;
2691 if (iomode
== IO_OUTPUT
)
2693 for (a
= *ap
; a
; a
= a
->next
)
2703 if (peek_atom () != ATOM_LPAREN
)
2706 a
= gfc_get_actual_arglist ();
2722 /* Read and write formal argument lists. */
2725 mio_formal_arglist (gfc_formal_arglist
**formal
)
2727 gfc_formal_arglist
*f
, *tail
;
2731 if (iomode
== IO_OUTPUT
)
2733 for (f
= *formal
; f
; f
= f
->next
)
2734 mio_symbol_ref (&f
->sym
);
2738 *formal
= tail
= NULL
;
2740 while (peek_atom () != ATOM_RPAREN
)
2742 f
= gfc_get_formal_arglist ();
2743 mio_symbol_ref (&f
->sym
);
2745 if (*formal
== NULL
)
2758 /* Save or restore a reference to a symbol node. */
2761 mio_symbol_ref (gfc_symbol
**symp
)
2765 p
= mio_pointer_ref (symp
);
2766 if (p
->type
== P_UNKNOWN
)
2769 if (iomode
== IO_OUTPUT
)
2771 if (p
->u
.wsym
.state
== UNREFERENCED
)
2772 p
->u
.wsym
.state
= NEEDS_WRITE
;
2776 if (p
->u
.rsym
.state
== UNUSED
)
2777 p
->u
.rsym
.state
= NEEDED
;
2783 /* Save or restore a reference to a symtree node. */
2786 mio_symtree_ref (gfc_symtree
**stp
)
2791 if (iomode
== IO_OUTPUT
)
2792 mio_symbol_ref (&(*stp
)->n
.sym
);
2795 require_atom (ATOM_INTEGER
);
2796 p
= get_integer (atom_int
);
2798 /* An unused equivalence member; make a symbol and a symtree
2800 if (in_load_equiv
&& p
->u
.rsym
.symtree
== NULL
)
2802 /* Since this is not used, it must have a unique name. */
2803 p
->u
.rsym
.symtree
= gfc_get_unique_symtree (gfc_current_ns
);
2805 /* Make the symbol. */
2806 if (p
->u
.rsym
.sym
== NULL
)
2808 p
->u
.rsym
.sym
= gfc_new_symbol (p
->u
.rsym
.true_name
,
2810 p
->u
.rsym
.sym
->module
= gfc_get_string (p
->u
.rsym
.module
);
2813 p
->u
.rsym
.symtree
->n
.sym
= p
->u
.rsym
.sym
;
2814 p
->u
.rsym
.symtree
->n
.sym
->refs
++;
2815 p
->u
.rsym
.referenced
= 1;
2817 /* If the symbol is PRIVATE and in COMMON, load_commons will
2818 generate a fixup symbol, which must be associated. */
2820 resolve_fixups (p
->fixup
, p
->u
.rsym
.sym
);
2824 if (p
->type
== P_UNKNOWN
)
2827 if (p
->u
.rsym
.state
== UNUSED
)
2828 p
->u
.rsym
.state
= NEEDED
;
2830 if (p
->u
.rsym
.symtree
!= NULL
)
2832 *stp
= p
->u
.rsym
.symtree
;
2836 f
= XCNEW (fixup_t
);
2838 f
->next
= p
->u
.rsym
.stfixup
;
2839 p
->u
.rsym
.stfixup
= f
;
2841 f
->pointer
= (void **) stp
;
2848 mio_iterator (gfc_iterator
**ip
)
2854 if (iomode
== IO_OUTPUT
)
2861 if (peek_atom () == ATOM_RPAREN
)
2867 *ip
= gfc_get_iterator ();
2872 mio_expr (&iter
->var
);
2873 mio_expr (&iter
->start
);
2874 mio_expr (&iter
->end
);
2875 mio_expr (&iter
->step
);
2883 mio_constructor (gfc_constructor_base
*cp
)
2889 if (iomode
== IO_OUTPUT
)
2891 for (c
= gfc_constructor_first (*cp
); c
; c
= gfc_constructor_next (c
))
2894 mio_expr (&c
->expr
);
2895 mio_iterator (&c
->iterator
);
2901 while (peek_atom () != ATOM_RPAREN
)
2903 c
= gfc_constructor_append_expr (cp
, NULL
, NULL
);
2906 mio_expr (&c
->expr
);
2907 mio_iterator (&c
->iterator
);
2916 static const mstring ref_types
[] = {
2917 minit ("ARRAY", REF_ARRAY
),
2918 minit ("COMPONENT", REF_COMPONENT
),
2919 minit ("SUBSTRING", REF_SUBSTRING
),
2925 mio_ref (gfc_ref
**rp
)
2932 r
->type
= MIO_NAME (ref_type
) (r
->type
, ref_types
);
2937 mio_array_ref (&r
->u
.ar
);
2941 mio_symbol_ref (&r
->u
.c
.sym
);
2942 mio_component_ref (&r
->u
.c
.component
);
2946 mio_expr (&r
->u
.ss
.start
);
2947 mio_expr (&r
->u
.ss
.end
);
2948 mio_charlen (&r
->u
.ss
.length
);
2957 mio_ref_list (gfc_ref
**rp
)
2959 gfc_ref
*ref
, *head
, *tail
;
2963 if (iomode
== IO_OUTPUT
)
2965 for (ref
= *rp
; ref
; ref
= ref
->next
)
2972 while (peek_atom () != ATOM_RPAREN
)
2975 head
= tail
= gfc_get_ref ();
2978 tail
->next
= gfc_get_ref ();
2992 /* Read and write an integer value. */
2995 mio_gmp_integer (mpz_t
*integer
)
2999 if (iomode
== IO_INPUT
)
3001 if (parse_atom () != ATOM_STRING
)
3002 bad_module ("Expected integer string");
3004 mpz_init (*integer
);
3005 if (mpz_set_str (*integer
, atom_string
, 10))
3006 bad_module ("Error converting integer");
3012 p
= mpz_get_str (NULL
, 10, *integer
);
3013 write_atom (ATOM_STRING
, p
);
3020 mio_gmp_real (mpfr_t
*real
)
3025 if (iomode
== IO_INPUT
)
3027 if (parse_atom () != ATOM_STRING
)
3028 bad_module ("Expected real string");
3031 mpfr_set_str (*real
, atom_string
, 16, GFC_RND_MODE
);
3036 p
= mpfr_get_str (NULL
, &exponent
, 16, 0, *real
, GFC_RND_MODE
);
3038 if (mpfr_nan_p (*real
) || mpfr_inf_p (*real
))
3040 write_atom (ATOM_STRING
, p
);
3045 atom_string
= XCNEWVEC (char, strlen (p
) + 20);
3047 sprintf (atom_string
, "0.%s@%ld", p
, exponent
);
3049 /* Fix negative numbers. */
3050 if (atom_string
[2] == '-')
3052 atom_string
[0] = '-';
3053 atom_string
[1] = '0';
3054 atom_string
[2] = '.';
3057 write_atom (ATOM_STRING
, atom_string
);
3065 /* Save and restore the shape of an array constructor. */
3068 mio_shape (mpz_t
**pshape
, int rank
)
3074 /* A NULL shape is represented by (). */
3077 if (iomode
== IO_OUTPUT
)
3089 if (t
== ATOM_RPAREN
)
3096 shape
= gfc_get_shape (rank
);
3100 for (n
= 0; n
< rank
; n
++)
3101 mio_gmp_integer (&shape
[n
]);
3107 static const mstring expr_types
[] = {
3108 minit ("OP", EXPR_OP
),
3109 minit ("FUNCTION", EXPR_FUNCTION
),
3110 minit ("CONSTANT", EXPR_CONSTANT
),
3111 minit ("VARIABLE", EXPR_VARIABLE
),
3112 minit ("SUBSTRING", EXPR_SUBSTRING
),
3113 minit ("STRUCTURE", EXPR_STRUCTURE
),
3114 minit ("ARRAY", EXPR_ARRAY
),
3115 minit ("NULL", EXPR_NULL
),
3116 minit ("COMPCALL", EXPR_COMPCALL
),
3120 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3121 generic operators, not in expressions. INTRINSIC_USER is also
3122 replaced by the correct function name by the time we see it. */
3124 static const mstring intrinsics
[] =
3126 minit ("UPLUS", INTRINSIC_UPLUS
),
3127 minit ("UMINUS", INTRINSIC_UMINUS
),
3128 minit ("PLUS", INTRINSIC_PLUS
),
3129 minit ("MINUS", INTRINSIC_MINUS
),
3130 minit ("TIMES", INTRINSIC_TIMES
),
3131 minit ("DIVIDE", INTRINSIC_DIVIDE
),
3132 minit ("POWER", INTRINSIC_POWER
),
3133 minit ("CONCAT", INTRINSIC_CONCAT
),
3134 minit ("AND", INTRINSIC_AND
),
3135 minit ("OR", INTRINSIC_OR
),
3136 minit ("EQV", INTRINSIC_EQV
),
3137 minit ("NEQV", INTRINSIC_NEQV
),
3138 minit ("EQ_SIGN", INTRINSIC_EQ
),
3139 minit ("EQ", INTRINSIC_EQ_OS
),
3140 minit ("NE_SIGN", INTRINSIC_NE
),
3141 minit ("NE", INTRINSIC_NE_OS
),
3142 minit ("GT_SIGN", INTRINSIC_GT
),
3143 minit ("GT", INTRINSIC_GT_OS
),
3144 minit ("GE_SIGN", INTRINSIC_GE
),
3145 minit ("GE", INTRINSIC_GE_OS
),
3146 minit ("LT_SIGN", INTRINSIC_LT
),
3147 minit ("LT", INTRINSIC_LT_OS
),
3148 minit ("LE_SIGN", INTRINSIC_LE
),
3149 minit ("LE", INTRINSIC_LE_OS
),
3150 minit ("NOT", INTRINSIC_NOT
),
3151 minit ("PARENTHESES", INTRINSIC_PARENTHESES
),
3152 minit ("USER", INTRINSIC_USER
),
3157 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3160 fix_mio_expr (gfc_expr
*e
)
3162 gfc_symtree
*ns_st
= NULL
;
3165 if (iomode
!= IO_OUTPUT
)
3170 /* If this is a symtree for a symbol that came from a contained module
3171 namespace, it has a unique name and we should look in the current
3172 namespace to see if the required, non-contained symbol is available
3173 yet. If so, the latter should be written. */
3174 if (e
->symtree
->n
.sym
&& check_unique_name (e
->symtree
->name
))
3176 const char *name
= e
->symtree
->n
.sym
->name
;
3177 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_DERIVED
)
3178 name
= dt_upper_string (name
);
3179 ns_st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
3182 /* On the other hand, if the existing symbol is the module name or the
3183 new symbol is a dummy argument, do not do the promotion. */
3184 if (ns_st
&& ns_st
->n
.sym
3185 && ns_st
->n
.sym
->attr
.flavor
!= FL_MODULE
3186 && !e
->symtree
->n
.sym
->attr
.dummy
)
3189 else if (e
->expr_type
== EXPR_FUNCTION
3190 && (e
->value
.function
.name
|| e
->value
.function
.isym
))
3194 /* In some circumstances, a function used in an initialization
3195 expression, in one use associated module, can fail to be
3196 coupled to its symtree when used in a specification
3197 expression in another module. */
3198 fname
= e
->value
.function
.esym
? e
->value
.function
.esym
->name
3199 : e
->value
.function
.isym
->name
;
3200 e
->symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, fname
);
3205 /* This is probably a reference to a private procedure from another
3206 module. To prevent a segfault, make a generic with no specific
3207 instances. If this module is used, without the required
3208 specific coming from somewhere, the appropriate error message
3210 gfc_get_symbol (fname
, gfc_current_ns
, &sym
);
3211 sym
->attr
.flavor
= FL_PROCEDURE
;
3212 sym
->attr
.generic
= 1;
3213 e
->symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, fname
);
3214 gfc_commit_symbol (sym
);
3219 /* Read and write expressions. The form "()" is allowed to indicate a
3223 mio_expr (gfc_expr
**ep
)
3231 if (iomode
== IO_OUTPUT
)
3240 MIO_NAME (expr_t
) (e
->expr_type
, expr_types
);
3245 if (t
== ATOM_RPAREN
)
3252 bad_module ("Expected expression type");
3254 e
= *ep
= gfc_get_expr ();
3255 e
->where
= gfc_current_locus
;
3256 e
->expr_type
= (expr_t
) find_enum (expr_types
);
3259 mio_typespec (&e
->ts
);
3260 mio_integer (&e
->rank
);
3264 switch (e
->expr_type
)
3268 = MIO_NAME (gfc_intrinsic_op
) (e
->value
.op
.op
, intrinsics
);
3270 switch (e
->value
.op
.op
)
3272 case INTRINSIC_UPLUS
:
3273 case INTRINSIC_UMINUS
:
3275 case INTRINSIC_PARENTHESES
:
3276 mio_expr (&e
->value
.op
.op1
);
3279 case INTRINSIC_PLUS
:
3280 case INTRINSIC_MINUS
:
3281 case INTRINSIC_TIMES
:
3282 case INTRINSIC_DIVIDE
:
3283 case INTRINSIC_POWER
:
3284 case INTRINSIC_CONCAT
:
3288 case INTRINSIC_NEQV
:
3290 case INTRINSIC_EQ_OS
:
3292 case INTRINSIC_NE_OS
:
3294 case INTRINSIC_GT_OS
:
3296 case INTRINSIC_GE_OS
:
3298 case INTRINSIC_LT_OS
:
3300 case INTRINSIC_LE_OS
:
3301 mio_expr (&e
->value
.op
.op1
);
3302 mio_expr (&e
->value
.op
.op2
);
3305 case INTRINSIC_USER
:
3306 /* INTRINSIC_USER should not appear in resolved expressions,
3307 though for UDRs we need to stream unresolved ones. */
3308 if (iomode
== IO_OUTPUT
)
3309 write_atom (ATOM_STRING
, e
->value
.op
.uop
->name
);
3312 char *name
= read_string ();
3313 const char *uop_name
= find_use_name (name
, true);
3314 if (uop_name
== NULL
)
3316 size_t len
= strlen (name
);
3317 char *name2
= XCNEWVEC (char, len
+ 2);
3318 memcpy (name2
, name
, len
);
3320 name2
[len
+ 1] = '\0';
3322 uop_name
= name
= name2
;
3324 e
->value
.op
.uop
= gfc_get_uop (uop_name
);
3327 mio_expr (&e
->value
.op
.op1
);
3328 mio_expr (&e
->value
.op
.op2
);
3332 bad_module ("Bad operator");
3338 mio_symtree_ref (&e
->symtree
);
3339 mio_actual_arglist (&e
->value
.function
.actual
);
3341 if (iomode
== IO_OUTPUT
)
3343 e
->value
.function
.name
3344 = mio_allocated_string (e
->value
.function
.name
);
3345 if (e
->value
.function
.esym
)
3349 else if (e
->value
.function
.isym
== NULL
)
3353 mio_integer (&flag
);
3357 mio_symbol_ref (&e
->value
.function
.esym
);
3360 mio_ref_list (&e
->ref
);
3365 write_atom (ATOM_STRING
, e
->value
.function
.isym
->name
);
3370 require_atom (ATOM_STRING
);
3371 if (atom_string
[0] == '\0')
3372 e
->value
.function
.name
= NULL
;
3374 e
->value
.function
.name
= gfc_get_string (atom_string
);
3377 mio_integer (&flag
);
3381 mio_symbol_ref (&e
->value
.function
.esym
);
3384 mio_ref_list (&e
->ref
);
3389 require_atom (ATOM_STRING
);
3390 e
->value
.function
.isym
= gfc_find_function (atom_string
);
3398 mio_symtree_ref (&e
->symtree
);
3399 mio_ref_list (&e
->ref
);
3402 case EXPR_SUBSTRING
:
3403 e
->value
.character
.string
3404 = CONST_CAST (gfc_char_t
*,
3405 mio_allocated_wide_string (e
->value
.character
.string
,
3406 e
->value
.character
.length
));
3407 mio_ref_list (&e
->ref
);
3410 case EXPR_STRUCTURE
:
3412 mio_constructor (&e
->value
.constructor
);
3413 mio_shape (&e
->shape
, e
->rank
);
3420 mio_gmp_integer (&e
->value
.integer
);
3424 gfc_set_model_kind (e
->ts
.kind
);
3425 mio_gmp_real (&e
->value
.real
);
3429 gfc_set_model_kind (e
->ts
.kind
);
3430 mio_gmp_real (&mpc_realref (e
->value
.complex));
3431 mio_gmp_real (&mpc_imagref (e
->value
.complex));
3435 mio_integer (&e
->value
.logical
);
3439 mio_integer (&e
->value
.character
.length
);
3440 e
->value
.character
.string
3441 = CONST_CAST (gfc_char_t
*,
3442 mio_allocated_wide_string (e
->value
.character
.string
,
3443 e
->value
.character
.length
));
3447 bad_module ("Bad type in constant expression");
3465 /* Read and write namelists. */
3468 mio_namelist (gfc_symbol
*sym
)
3470 gfc_namelist
*n
, *m
;
3471 const char *check_name
;
3475 if (iomode
== IO_OUTPUT
)
3477 for (n
= sym
->namelist
; n
; n
= n
->next
)
3478 mio_symbol_ref (&n
->sym
);
3482 /* This departure from the standard is flagged as an error.
3483 It does, in fact, work correctly. TODO: Allow it
3485 if (sym
->attr
.flavor
== FL_NAMELIST
)
3487 check_name
= find_use_name (sym
->name
, false);
3488 if (check_name
&& strcmp (check_name
, sym
->name
) != 0)
3489 gfc_error ("Namelist %s cannot be renamed by USE "
3490 "association to %s", sym
->name
, check_name
);
3494 while (peek_atom () != ATOM_RPAREN
)
3496 n
= gfc_get_namelist ();
3497 mio_symbol_ref (&n
->sym
);
3499 if (sym
->namelist
== NULL
)
3506 sym
->namelist_tail
= m
;
3513 /* Save/restore lists of gfc_interface structures. When loading an
3514 interface, we are really appending to the existing list of
3515 interfaces. Checking for duplicate and ambiguous interfaces has to
3516 be done later when all symbols have been loaded. */
3519 mio_interface_rest (gfc_interface
**ip
)
3521 gfc_interface
*tail
, *p
;
3522 pointer_info
*pi
= NULL
;
3524 if (iomode
== IO_OUTPUT
)
3527 for (p
= *ip
; p
; p
= p
->next
)
3528 mio_symbol_ref (&p
->sym
);
3543 if (peek_atom () == ATOM_RPAREN
)
3546 p
= gfc_get_interface ();
3547 p
->where
= gfc_current_locus
;
3548 pi
= mio_symbol_ref (&p
->sym
);
3564 /* Save/restore a nameless operator interface. */
3567 mio_interface (gfc_interface
**ip
)
3570 mio_interface_rest (ip
);
3574 /* Save/restore a named operator interface. */
3577 mio_symbol_interface (const char **name
, const char **module
,
3581 mio_pool_string (name
);
3582 mio_pool_string (module
);
3583 mio_interface_rest (ip
);
3588 mio_namespace_ref (gfc_namespace
**nsp
)
3593 p
= mio_pointer_ref (nsp
);
3595 if (p
->type
== P_UNKNOWN
)
3596 p
->type
= P_NAMESPACE
;
3598 if (iomode
== IO_INPUT
&& p
->integer
!= 0)
3600 ns
= (gfc_namespace
*) p
->u
.pointer
;
3603 ns
= gfc_get_namespace (NULL
, 0);
3604 associate_integer_pointer (p
, ns
);
3612 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3614 static gfc_namespace
* current_f2k_derived
;
3617 mio_typebound_proc (gfc_typebound_proc
** proc
)
3620 int overriding_flag
;
3622 if (iomode
== IO_INPUT
)
3624 *proc
= gfc_get_typebound_proc (NULL
);
3625 (*proc
)->where
= gfc_current_locus
;
3631 (*proc
)->access
= MIO_NAME (gfc_access
) ((*proc
)->access
, access_types
);
3633 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3634 gcc_assert (!((*proc
)->deferred
&& (*proc
)->non_overridable
));
3635 overriding_flag
= ((*proc
)->deferred
<< 1) | (*proc
)->non_overridable
;
3636 overriding_flag
= mio_name (overriding_flag
, binding_overriding
);
3637 (*proc
)->deferred
= ((overriding_flag
& 2) != 0);
3638 (*proc
)->non_overridable
= ((overriding_flag
& 1) != 0);
3639 gcc_assert (!((*proc
)->deferred
&& (*proc
)->non_overridable
));
3641 (*proc
)->nopass
= mio_name ((*proc
)->nopass
, binding_passing
);
3642 (*proc
)->is_generic
= mio_name ((*proc
)->is_generic
, binding_generic
);
3643 (*proc
)->ppc
= mio_name((*proc
)->ppc
, binding_ppc
);
3645 mio_pool_string (&((*proc
)->pass_arg
));
3647 flag
= (int) (*proc
)->pass_arg_num
;
3648 mio_integer (&flag
);
3649 (*proc
)->pass_arg_num
= (unsigned) flag
;
3651 if ((*proc
)->is_generic
)
3658 if (iomode
== IO_OUTPUT
)
3659 for (g
= (*proc
)->u
.generic
; g
; g
= g
->next
)
3661 iop
= (int) g
->is_operator
;
3663 mio_allocated_string (g
->specific_st
->name
);
3667 (*proc
)->u
.generic
= NULL
;
3668 while (peek_atom () != ATOM_RPAREN
)
3670 gfc_symtree
** sym_root
;
3672 g
= gfc_get_tbp_generic ();
3676 g
->is_operator
= (bool) iop
;
3678 require_atom (ATOM_STRING
);
3679 sym_root
= ¤t_f2k_derived
->tb_sym_root
;
3680 g
->specific_st
= gfc_get_tbp_symtree (sym_root
, atom_string
);
3683 g
->next
= (*proc
)->u
.generic
;
3684 (*proc
)->u
.generic
= g
;
3690 else if (!(*proc
)->ppc
)
3691 mio_symtree_ref (&(*proc
)->u
.specific
);
3696 /* Walker-callback function for this purpose. */
3698 mio_typebound_symtree (gfc_symtree
* st
)
3700 if (iomode
== IO_OUTPUT
&& !st
->n
.tb
)
3703 if (iomode
== IO_OUTPUT
)
3706 mio_allocated_string (st
->name
);
3708 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3710 mio_typebound_proc (&st
->n
.tb
);
3714 /* IO a full symtree (in all depth). */
3716 mio_full_typebound_tree (gfc_symtree
** root
)
3720 if (iomode
== IO_OUTPUT
)
3721 gfc_traverse_symtree (*root
, &mio_typebound_symtree
);
3724 while (peek_atom () == ATOM_LPAREN
)
3730 require_atom (ATOM_STRING
);
3731 st
= gfc_get_tbp_symtree (root
, atom_string
);
3734 mio_typebound_symtree (st
);
3742 mio_finalizer (gfc_finalizer
**f
)
3744 if (iomode
== IO_OUTPUT
)
3747 gcc_assert ((*f
)->proc_tree
); /* Should already be resolved. */
3748 mio_symtree_ref (&(*f
)->proc_tree
);
3752 *f
= gfc_get_finalizer ();
3753 (*f
)->where
= gfc_current_locus
; /* Value should not matter. */
3756 mio_symtree_ref (&(*f
)->proc_tree
);
3757 (*f
)->proc_sym
= NULL
;
3762 mio_f2k_derived (gfc_namespace
*f2k
)
3764 current_f2k_derived
= f2k
;
3766 /* Handle the list of finalizer procedures. */
3768 if (iomode
== IO_OUTPUT
)
3771 for (f
= f2k
->finalizers
; f
; f
= f
->next
)
3776 f2k
->finalizers
= NULL
;
3777 while (peek_atom () != ATOM_RPAREN
)
3779 gfc_finalizer
*cur
= NULL
;
3780 mio_finalizer (&cur
);
3781 cur
->next
= f2k
->finalizers
;
3782 f2k
->finalizers
= cur
;
3787 /* Handle type-bound procedures. */
3788 mio_full_typebound_tree (&f2k
->tb_sym_root
);
3790 /* Type-bound user operators. */
3791 mio_full_typebound_tree (&f2k
->tb_uop_root
);
3793 /* Type-bound intrinsic operators. */
3795 if (iomode
== IO_OUTPUT
)
3798 for (op
= GFC_INTRINSIC_BEGIN
; op
!= GFC_INTRINSIC_END
; ++op
)
3800 gfc_intrinsic_op realop
;
3802 if (op
== INTRINSIC_USER
|| !f2k
->tb_op
[op
])
3806 realop
= (gfc_intrinsic_op
) op
;
3807 mio_intrinsic_op (&realop
);
3808 mio_typebound_proc (&f2k
->tb_op
[op
]);
3813 while (peek_atom () != ATOM_RPAREN
)
3815 gfc_intrinsic_op op
= GFC_INTRINSIC_BEGIN
; /* Silence GCC. */
3818 mio_intrinsic_op (&op
);
3819 mio_typebound_proc (&f2k
->tb_op
[op
]);
3826 mio_full_f2k_derived (gfc_symbol
*sym
)
3830 if (iomode
== IO_OUTPUT
)
3832 if (sym
->f2k_derived
)
3833 mio_f2k_derived (sym
->f2k_derived
);
3837 if (peek_atom () != ATOM_RPAREN
)
3839 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
3840 mio_f2k_derived (sym
->f2k_derived
);
3843 gcc_assert (!sym
->f2k_derived
);
3849 static const mstring omp_declare_simd_clauses
[] =
3851 minit ("INBRANCH", 0),
3852 minit ("NOTINBRANCH", 1),
3853 minit ("SIMDLEN", 2),
3854 minit ("UNIFORM", 3),
3855 minit ("LINEAR", 4),
3856 minit ("ALIGNED", 5),
3860 /* Handle !$omp declare simd. */
3863 mio_omp_declare_simd (gfc_namespace
*ns
, gfc_omp_declare_simd
**odsp
)
3865 if (iomode
== IO_OUTPUT
)
3870 else if (peek_atom () != ATOM_LPAREN
)
3873 gfc_omp_declare_simd
*ods
= *odsp
;
3876 if (iomode
== IO_OUTPUT
)
3878 write_atom (ATOM_NAME
, "OMP_DECLARE_SIMD");
3881 gfc_omp_namelist
*n
;
3883 if (ods
->clauses
->inbranch
)
3884 mio_name (0, omp_declare_simd_clauses
);
3885 if (ods
->clauses
->notinbranch
)
3886 mio_name (1, omp_declare_simd_clauses
);
3887 if (ods
->clauses
->simdlen_expr
)
3889 mio_name (2, omp_declare_simd_clauses
);
3890 mio_expr (&ods
->clauses
->simdlen_expr
);
3892 for (n
= ods
->clauses
->lists
[OMP_LIST_UNIFORM
]; n
; n
= n
->next
)
3894 mio_name (3, omp_declare_simd_clauses
);
3895 mio_symbol_ref (&n
->sym
);
3897 for (n
= ods
->clauses
->lists
[OMP_LIST_LINEAR
]; n
; n
= n
->next
)
3899 mio_name (4, omp_declare_simd_clauses
);
3900 mio_symbol_ref (&n
->sym
);
3901 mio_expr (&n
->expr
);
3903 for (n
= ods
->clauses
->lists
[OMP_LIST_ALIGNED
]; n
; n
= n
->next
)
3905 mio_name (5, omp_declare_simd_clauses
);
3906 mio_symbol_ref (&n
->sym
);
3907 mio_expr (&n
->expr
);
3913 gfc_omp_namelist
**ptrs
[3] = { NULL
, NULL
, NULL
};
3915 require_atom (ATOM_NAME
);
3916 *odsp
= ods
= gfc_get_omp_declare_simd ();
3917 ods
->where
= gfc_current_locus
;
3918 ods
->proc_name
= ns
->proc_name
;
3919 if (peek_atom () == ATOM_NAME
)
3921 ods
->clauses
= gfc_get_omp_clauses ();
3922 ptrs
[0] = &ods
->clauses
->lists
[OMP_LIST_UNIFORM
];
3923 ptrs
[1] = &ods
->clauses
->lists
[OMP_LIST_LINEAR
];
3924 ptrs
[2] = &ods
->clauses
->lists
[OMP_LIST_ALIGNED
];
3926 while (peek_atom () == ATOM_NAME
)
3928 gfc_omp_namelist
*n
;
3929 int t
= mio_name (0, omp_declare_simd_clauses
);
3933 case 0: ods
->clauses
->inbranch
= true; break;
3934 case 1: ods
->clauses
->notinbranch
= true; break;
3935 case 2: mio_expr (&ods
->clauses
->simdlen_expr
); break;
3939 *ptrs
[t
- 3] = n
= gfc_get_omp_namelist ();
3940 ptrs
[t
- 3] = &n
->next
;
3941 mio_symbol_ref (&n
->sym
);
3943 mio_expr (&n
->expr
);
3949 mio_omp_declare_simd (ns
, &ods
->next
);
3955 static const mstring omp_declare_reduction_stmt
[] =
3957 minit ("ASSIGN", 0),
3964 mio_omp_udr_expr (gfc_omp_udr
*udr
, gfc_symbol
**sym1
, gfc_symbol
**sym2
,
3965 gfc_namespace
*ns
, bool is_initializer
)
3967 if (iomode
== IO_OUTPUT
)
3969 if ((*sym1
)->module
== NULL
)
3971 (*sym1
)->module
= module_name
;
3972 (*sym2
)->module
= module_name
;
3974 mio_symbol_ref (sym1
);
3975 mio_symbol_ref (sym2
);
3976 if (ns
->code
->op
== EXEC_ASSIGN
)
3978 mio_name (0, omp_declare_reduction_stmt
);
3979 mio_expr (&ns
->code
->expr1
);
3980 mio_expr (&ns
->code
->expr2
);
3985 mio_name (1, omp_declare_reduction_stmt
);
3986 mio_symtree_ref (&ns
->code
->symtree
);
3987 mio_actual_arglist (&ns
->code
->ext
.actual
);
3989 flag
= ns
->code
->resolved_isym
!= NULL
;
3990 mio_integer (&flag
);
3992 write_atom (ATOM_STRING
, ns
->code
->resolved_isym
->name
);
3994 mio_symbol_ref (&ns
->code
->resolved_sym
);
3999 pointer_info
*p1
= mio_symbol_ref (sym1
);
4000 pointer_info
*p2
= mio_symbol_ref (sym2
);
4002 gcc_assert (p1
->u
.rsym
.ns
== p2
->u
.rsym
.ns
);
4003 gcc_assert (p1
->u
.rsym
.sym
== NULL
);
4004 /* Add hidden symbols to the symtree. */
4005 pointer_info
*q
= get_integer (p1
->u
.rsym
.ns
);
4006 q
->u
.pointer
= (void *) ns
;
4007 sym
= gfc_new_symbol (is_initializer
? "omp_priv" : "omp_out", ns
);
4009 sym
->module
= gfc_get_string (p1
->u
.rsym
.module
);
4010 associate_integer_pointer (p1
, sym
);
4011 sym
->attr
.omp_udr_artificial_var
= 1;
4012 gcc_assert (p2
->u
.rsym
.sym
== NULL
);
4013 sym
= gfc_new_symbol (is_initializer
? "omp_orig" : "omp_in", ns
);
4015 sym
->module
= gfc_get_string (p2
->u
.rsym
.module
);
4016 associate_integer_pointer (p2
, sym
);
4017 sym
->attr
.omp_udr_artificial_var
= 1;
4018 if (mio_name (0, omp_declare_reduction_stmt
) == 0)
4020 ns
->code
= gfc_get_code (EXEC_ASSIGN
);
4021 mio_expr (&ns
->code
->expr1
);
4022 mio_expr (&ns
->code
->expr2
);
4027 ns
->code
= gfc_get_code (EXEC_CALL
);
4028 mio_symtree_ref (&ns
->code
->symtree
);
4029 mio_actual_arglist (&ns
->code
->ext
.actual
);
4031 mio_integer (&flag
);
4034 require_atom (ATOM_STRING
);
4035 ns
->code
->resolved_isym
= gfc_find_subroutine (atom_string
);
4039 mio_symbol_ref (&ns
->code
->resolved_sym
);
4041 ns
->code
->loc
= gfc_current_locus
;
4047 /* Unlike most other routines, the address of the symbol node is already
4048 fixed on input and the name/module has already been filled in.
4049 If you update the symbol format here, don't forget to update read_module
4050 as well (look for "seek to the symbol's component list"). */
4053 mio_symbol (gfc_symbol
*sym
)
4055 int intmod
= INTMOD_NONE
;
4059 mio_symbol_attribute (&sym
->attr
);
4061 /* Note that components are always saved, even if they are supposed
4062 to be private. Component access is checked during searching. */
4063 mio_component_list (&sym
->components
, sym
->attr
.vtype
);
4064 if (sym
->components
!= NULL
)
4065 sym
->component_access
4066 = MIO_NAME (gfc_access
) (sym
->component_access
, access_types
);
4068 mio_typespec (&sym
->ts
);
4069 if (sym
->ts
.type
== BT_CLASS
)
4070 sym
->attr
.class_ok
= 1;
4072 if (iomode
== IO_OUTPUT
)
4073 mio_namespace_ref (&sym
->formal_ns
);
4076 mio_namespace_ref (&sym
->formal_ns
);
4078 sym
->formal_ns
->proc_name
= sym
;
4081 /* Save/restore common block links. */
4082 mio_symbol_ref (&sym
->common_next
);
4084 mio_formal_arglist (&sym
->formal
);
4086 if (sym
->attr
.flavor
== FL_PARAMETER
)
4087 mio_expr (&sym
->value
);
4089 mio_array_spec (&sym
->as
);
4091 mio_symbol_ref (&sym
->result
);
4093 if (sym
->attr
.cray_pointee
)
4094 mio_symbol_ref (&sym
->cp_pointer
);
4096 /* Load/save the f2k_derived namespace of a derived-type symbol. */
4097 mio_full_f2k_derived (sym
);
4101 /* Add the fields that say whether this is from an intrinsic module,
4102 and if so, what symbol it is within the module. */
4103 /* mio_integer (&(sym->from_intmod)); */
4104 if (iomode
== IO_OUTPUT
)
4106 intmod
= sym
->from_intmod
;
4107 mio_integer (&intmod
);
4111 mio_integer (&intmod
);
4113 sym
->from_intmod
= current_intmod
;
4115 sym
->from_intmod
= (intmod_id
) intmod
;
4118 mio_integer (&(sym
->intmod_sym_id
));
4120 if (sym
->attr
.flavor
== FL_DERIVED
)
4121 mio_integer (&(sym
->hash_value
));
4124 && sym
->formal_ns
->proc_name
== sym
4125 && sym
->formal_ns
->entries
== NULL
)
4126 mio_omp_declare_simd (sym
->formal_ns
, &sym
->formal_ns
->omp_declare_simd
);
4132 /************************* Top level subroutines *************************/
4134 /* Given a root symtree node and a symbol, try to find a symtree that
4135 references the symbol that is not a unique name. */
4137 static gfc_symtree
*
4138 find_symtree_for_symbol (gfc_symtree
*st
, gfc_symbol
*sym
)
4140 gfc_symtree
*s
= NULL
;
4145 s
= find_symtree_for_symbol (st
->right
, sym
);
4148 s
= find_symtree_for_symbol (st
->left
, sym
);
4152 if (st
->n
.sym
== sym
&& !check_unique_name (st
->name
))
4159 /* A recursive function to look for a specific symbol by name and by
4160 module. Whilst several symtrees might point to one symbol, its
4161 is sufficient for the purposes here than one exist. Note that
4162 generic interfaces are distinguished as are symbols that have been
4163 renamed in another module. */
4164 static gfc_symtree
*
4165 find_symbol (gfc_symtree
*st
, const char *name
,
4166 const char *module
, int generic
)
4169 gfc_symtree
*retval
, *s
;
4171 if (st
== NULL
|| st
->n
.sym
== NULL
)
4174 c
= strcmp (name
, st
->n
.sym
->name
);
4175 if (c
== 0 && st
->n
.sym
->module
4176 && strcmp (module
, st
->n
.sym
->module
) == 0
4177 && !check_unique_name (st
->name
))
4179 s
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
4181 /* Detect symbols that are renamed by use association in another
4182 module by the absence of a symtree and null attr.use_rename,
4183 since the latter is not transmitted in the module file. */
4184 if (((!generic
&& !st
->n
.sym
->attr
.generic
)
4185 || (generic
&& st
->n
.sym
->attr
.generic
))
4186 && !(s
== NULL
&& !st
->n
.sym
->attr
.use_rename
))
4190 retval
= find_symbol (st
->left
, name
, module
, generic
);
4193 retval
= find_symbol (st
->right
, name
, module
, generic
);
4199 /* Skip a list between balanced left and right parens.
4200 By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
4201 have been already parsed by hand, and the remaining of the content is to be
4202 skipped here. The default value is 0 (balanced parens). */
4205 skip_list (int nest_level
= 0)
4212 switch (parse_atom ())
4235 /* Load operator interfaces from the module. Interfaces are unusual
4236 in that they attach themselves to existing symbols. */
4239 load_operator_interfaces (void)
4242 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module
[GFC_MAX_SYMBOL_LEN
+ 1];
4244 pointer_info
*pi
= NULL
;
4249 while (peek_atom () != ATOM_RPAREN
)
4253 mio_internal_string (name
);
4254 mio_internal_string (module
);
4256 n
= number_use_names (name
, true);
4259 for (i
= 1; i
<= n
; i
++)
4261 /* Decide if we need to load this one or not. */
4262 p
= find_use_name_n (name
, &i
, true);
4266 while (parse_atom () != ATOM_RPAREN
);
4272 uop
= gfc_get_uop (p
);
4273 pi
= mio_interface_rest (&uop
->op
);
4277 if (gfc_find_uop (p
, NULL
))
4279 uop
= gfc_get_uop (p
);
4280 uop
->op
= gfc_get_interface ();
4281 uop
->op
->where
= gfc_current_locus
;
4282 add_fixup (pi
->integer
, &uop
->op
->sym
);
4291 /* Load interfaces from the module. Interfaces are unusual in that
4292 they attach themselves to existing symbols. */
4295 load_generic_interfaces (void)
4298 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module
[GFC_MAX_SYMBOL_LEN
+ 1];
4300 gfc_interface
*generic
= NULL
, *gen
= NULL
;
4302 bool ambiguous_set
= false;
4306 while (peek_atom () != ATOM_RPAREN
)
4310 mio_internal_string (name
);
4311 mio_internal_string (module
);
4313 n
= number_use_names (name
, false);
4314 renamed
= n
? 1 : 0;
4317 for (i
= 1; i
<= n
; i
++)
4320 /* Decide if we need to load this one or not. */
4321 p
= find_use_name_n (name
, &i
, false);
4323 st
= find_symbol (gfc_current_ns
->sym_root
,
4324 name
, module_name
, 1);
4326 if (!p
|| gfc_find_symbol (p
, NULL
, 0, &sym
))
4328 /* Skip the specific names for these cases. */
4329 while (i
== 1 && parse_atom () != ATOM_RPAREN
);
4334 /* If the symbol exists already and is being USEd without being
4335 in an ONLY clause, do not load a new symtree(11.3.2). */
4336 if (!only_flag
&& st
)
4344 if (strcmp (st
->name
, p
) != 0)
4346 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, p
);
4352 /* Since we haven't found a valid generic interface, we had
4356 gfc_get_symbol (p
, NULL
, &sym
);
4357 sym
->name
= gfc_get_string (name
);
4358 sym
->module
= module_name
;
4359 sym
->attr
.flavor
= FL_PROCEDURE
;
4360 sym
->attr
.generic
= 1;
4361 sym
->attr
.use_assoc
= 1;
4366 /* Unless sym is a generic interface, this reference
4369 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
4373 if (st
&& !sym
->attr
.generic
4376 && strcmp (module
, sym
->module
))
4378 ambiguous_set
= true;
4383 sym
->attr
.use_only
= only_flag
;
4384 sym
->attr
.use_rename
= renamed
;
4388 mio_interface_rest (&sym
->generic
);
4389 generic
= sym
->generic
;
4391 else if (!sym
->generic
)
4393 sym
->generic
= generic
;
4394 sym
->attr
.generic_copy
= 1;
4397 /* If a procedure that is not generic has generic interfaces
4398 that include itself, it is generic! We need to take care
4399 to retain symbols ambiguous that were already so. */
4400 if (sym
->attr
.use_assoc
4401 && !sym
->attr
.generic
4402 && sym
->attr
.flavor
== FL_PROCEDURE
)
4404 for (gen
= generic
; gen
; gen
= gen
->next
)
4406 if (gen
->sym
== sym
)
4408 sym
->attr
.generic
= 1;
4423 /* Load common blocks. */
4428 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4433 while (peek_atom () != ATOM_RPAREN
)
4438 mio_internal_string (name
);
4440 p
= gfc_get_common (name
, 1);
4442 mio_symbol_ref (&p
->head
);
4443 mio_integer (&flags
);
4447 p
->threadprivate
= 1;
4450 /* Get whether this was a bind(c) common or not. */
4451 mio_integer (&p
->is_bind_c
);
4452 /* Get the binding label. */
4453 label
= read_string ();
4455 p
->binding_label
= IDENTIFIER_POINTER (get_identifier (label
));
4465 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4466 so that unused variables are not loaded and so that the expression can
4472 gfc_equiv
*head
, *tail
, *end
, *eq
;
4476 in_load_equiv
= true;
4478 end
= gfc_current_ns
->equiv
;
4479 while (end
!= NULL
&& end
->next
!= NULL
)
4482 while (peek_atom () != ATOM_RPAREN
) {
4486 while(peek_atom () != ATOM_RPAREN
)
4489 head
= tail
= gfc_get_equiv ();
4492 tail
->eq
= gfc_get_equiv ();
4496 mio_pool_string (&tail
->module
);
4497 mio_expr (&tail
->expr
);
4500 /* Unused equivalence members have a unique name. In addition, it
4501 must be checked that the symbols are from the same module. */
4503 for (eq
= head
; eq
; eq
= eq
->eq
)
4505 if (eq
->expr
->symtree
->n
.sym
->module
4506 && head
->expr
->symtree
->n
.sym
->module
4507 && strcmp (head
->expr
->symtree
->n
.sym
->module
,
4508 eq
->expr
->symtree
->n
.sym
->module
) == 0
4509 && !check_unique_name (eq
->expr
->symtree
->name
))
4518 for (eq
= head
; eq
; eq
= head
)
4521 gfc_free_expr (eq
->expr
);
4527 gfc_current_ns
->equiv
= head
;
4538 in_load_equiv
= false;
4542 /* This function loads the sym_root of f2k_derived with the extensions to
4543 the derived type. */
4545 load_derived_extensions (void)
4548 gfc_symbol
*derived
;
4552 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4553 char module
[GFC_MAX_SYMBOL_LEN
+ 1];
4557 while (peek_atom () != ATOM_RPAREN
)
4560 mio_integer (&symbol
);
4561 info
= get_integer (symbol
);
4562 derived
= info
->u
.rsym
.sym
;
4564 /* This one is not being loaded. */
4565 if (!info
|| !derived
)
4567 while (peek_atom () != ATOM_RPAREN
)
4572 gcc_assert (derived
->attr
.flavor
== FL_DERIVED
);
4573 if (derived
->f2k_derived
== NULL
)
4574 derived
->f2k_derived
= gfc_get_namespace (NULL
, 0);
4576 while (peek_atom () != ATOM_RPAREN
)
4579 mio_internal_string (name
);
4580 mio_internal_string (module
);
4582 /* Only use one use name to find the symbol. */
4584 p
= find_use_name_n (name
, &j
, false);
4587 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
4589 st
= gfc_find_symtree (derived
->f2k_derived
->sym_root
, name
);
4592 /* Only use the real name in f2k_derived to ensure a single
4594 st
= gfc_new_symtree (&derived
->f2k_derived
->sym_root
, name
);
4607 /* This function loads OpenMP user defined reductions. */
4609 load_omp_udrs (void)
4612 while (peek_atom () != ATOM_RPAREN
)
4614 const char *name
, *newname
;
4618 gfc_omp_reduction_op rop
= OMP_REDUCTION_USER
;
4621 mio_pool_string (&name
);
4623 if (strncmp (name
, "operator ", sizeof ("operator ") - 1) == 0)
4625 const char *p
= name
+ sizeof ("operator ") - 1;
4626 if (strcmp (p
, "+") == 0)
4627 rop
= OMP_REDUCTION_PLUS
;
4628 else if (strcmp (p
, "*") == 0)
4629 rop
= OMP_REDUCTION_TIMES
;
4630 else if (strcmp (p
, "-") == 0)
4631 rop
= OMP_REDUCTION_MINUS
;
4632 else if (strcmp (p
, ".and.") == 0)
4633 rop
= OMP_REDUCTION_AND
;
4634 else if (strcmp (p
, ".or.") == 0)
4635 rop
= OMP_REDUCTION_OR
;
4636 else if (strcmp (p
, ".eqv.") == 0)
4637 rop
= OMP_REDUCTION_EQV
;
4638 else if (strcmp (p
, ".neqv.") == 0)
4639 rop
= OMP_REDUCTION_NEQV
;
4642 if (rop
== OMP_REDUCTION_USER
&& name
[0] == '.')
4644 size_t len
= strlen (name
+ 1);
4645 altname
= XALLOCAVEC (char, len
);
4646 gcc_assert (name
[len
] == '.');
4647 memcpy (altname
, name
+ 1, len
- 1);
4648 altname
[len
- 1] = '\0';
4651 if (rop
== OMP_REDUCTION_USER
)
4652 newname
= find_use_name (altname
? altname
: name
, !!altname
);
4653 else if (only_flag
&& find_use_operator ((gfc_intrinsic_op
) rop
) == NULL
)
4655 if (newname
== NULL
)
4660 if (altname
&& newname
!= altname
)
4662 size_t len
= strlen (newname
);
4663 altname
= XALLOCAVEC (char, len
+ 3);
4665 memcpy (altname
+ 1, newname
, len
);
4666 altname
[len
+ 1] = '.';
4667 altname
[len
+ 2] = '\0';
4668 name
= gfc_get_string (altname
);
4670 st
= gfc_find_symtree (gfc_current_ns
->omp_udr_root
, name
);
4671 gfc_omp_udr
*udr
= gfc_omp_udr_find (st
, &ts
);
4674 require_atom (ATOM_INTEGER
);
4675 pointer_info
*p
= get_integer (atom_int
);
4676 if (strcmp (p
->u
.rsym
.module
, udr
->omp_out
->module
))
4678 gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
4680 p
->u
.rsym
.module
, &gfc_current_locus
);
4681 gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
4683 udr
->omp_out
->module
, &udr
->where
);
4688 udr
= gfc_get_omp_udr ();
4692 udr
->where
= gfc_current_locus
;
4693 udr
->combiner_ns
= gfc_get_namespace (gfc_current_ns
, 1);
4694 udr
->combiner_ns
->proc_name
= gfc_current_ns
->proc_name
;
4695 mio_omp_udr_expr (udr
, &udr
->omp_out
, &udr
->omp_in
, udr
->combiner_ns
,
4697 if (peek_atom () != ATOM_RPAREN
)
4699 udr
->initializer_ns
= gfc_get_namespace (gfc_current_ns
, 1);
4700 udr
->initializer_ns
->proc_name
= gfc_current_ns
->proc_name
;
4701 mio_omp_udr_expr (udr
, &udr
->omp_priv
, &udr
->omp_orig
,
4702 udr
->initializer_ns
, true);
4706 udr
->next
= st
->n
.omp_udr
;
4707 st
->n
.omp_udr
= udr
;
4711 st
= gfc_new_symtree (&gfc_current_ns
->omp_udr_root
, name
);
4712 st
->n
.omp_udr
= udr
;
4720 /* Recursive function to traverse the pointer_info tree and load a
4721 needed symbol. We return nonzero if we load a symbol and stop the
4722 traversal, because the act of loading can alter the tree. */
4725 load_needed (pointer_info
*p
)
4736 rv
|= load_needed (p
->left
);
4737 rv
|= load_needed (p
->right
);
4739 if (p
->type
!= P_SYMBOL
|| p
->u
.rsym
.state
!= NEEDED
)
4742 p
->u
.rsym
.state
= USED
;
4744 set_module_locus (&p
->u
.rsym
.where
);
4746 sym
= p
->u
.rsym
.sym
;
4749 q
= get_integer (p
->u
.rsym
.ns
);
4751 ns
= (gfc_namespace
*) q
->u
.pointer
;
4754 /* Create an interface namespace if necessary. These are
4755 the namespaces that hold the formal parameters of module
4758 ns
= gfc_get_namespace (NULL
, 0);
4759 associate_integer_pointer (q
, ns
);
4762 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4763 doesn't go pear-shaped if the symbol is used. */
4765 gfc_find_symbol (p
->u
.rsym
.module
, gfc_current_ns
,
4768 sym
= gfc_new_symbol (p
->u
.rsym
.true_name
, ns
);
4769 sym
->name
= dt_lower_string (p
->u
.rsym
.true_name
);
4770 sym
->module
= gfc_get_string (p
->u
.rsym
.module
);
4771 if (p
->u
.rsym
.binding_label
)
4772 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier
4773 (p
->u
.rsym
.binding_label
));
4775 associate_integer_pointer (p
, sym
);
4779 sym
->attr
.use_assoc
= 1;
4781 /* Mark as only or rename for later diagnosis for explicitly imported
4782 but not used warnings; don't mark internal symbols such as __vtab,
4783 __def_init etc. Only mark them if they have been explicitly loaded. */
4785 if (only_flag
&& sym
->name
[0] != '_' && sym
->name
[1] != '_')
4789 /* Search the use/rename list for the variable; if the variable is
4791 for (u
= gfc_rename_list
; u
; u
= u
->next
)
4793 if (strcmp (u
->use_name
, sym
->name
) == 0)
4795 sym
->attr
.use_only
= 1;
4801 if (p
->u
.rsym
.renamed
)
4802 sym
->attr
.use_rename
= 1;
4808 /* Recursive function for cleaning up things after a module has been read. */
4811 read_cleanup (pointer_info
*p
)
4819 read_cleanup (p
->left
);
4820 read_cleanup (p
->right
);
4822 if (p
->type
== P_SYMBOL
&& p
->u
.rsym
.state
== USED
&& !p
->u
.rsym
.referenced
)
4825 /* Add hidden symbols to the symtree. */
4826 q
= get_integer (p
->u
.rsym
.ns
);
4827 ns
= (gfc_namespace
*) q
->u
.pointer
;
4829 if (!p
->u
.rsym
.sym
->attr
.vtype
4830 && !p
->u
.rsym
.sym
->attr
.vtab
)
4831 st
= gfc_get_unique_symtree (ns
);
4834 /* There is no reason to use 'unique_symtrees' for vtabs or
4835 vtypes - their name is fine for a symtree and reduces the
4836 namespace pollution. */
4837 st
= gfc_find_symtree (ns
->sym_root
, p
->u
.rsym
.sym
->name
);
4839 st
= gfc_new_symtree (&ns
->sym_root
, p
->u
.rsym
.sym
->name
);
4842 st
->n
.sym
= p
->u
.rsym
.sym
;
4845 /* Fixup any symtree references. */
4846 p
->u
.rsym
.symtree
= st
;
4847 resolve_fixups (p
->u
.rsym
.stfixup
, st
);
4848 p
->u
.rsym
.stfixup
= NULL
;
4851 /* Free unused symbols. */
4852 if (p
->type
== P_SYMBOL
&& p
->u
.rsym
.state
== UNUSED
)
4853 gfc_free_symbol (p
->u
.rsym
.sym
);
4857 /* It is not quite enough to check for ambiguity in the symbols by
4858 the loaded symbol and the new symbol not being identical. */
4860 check_for_ambiguous (gfc_symbol
*st_sym
, pointer_info
*info
)
4864 symbol_attribute attr
;
4866 if (gfc_current_ns
->proc_name
&& st_sym
->name
== gfc_current_ns
->proc_name
->name
)
4868 gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
4869 "current program unit", st_sym
->name
, module_name
);
4873 rsym
= info
->u
.rsym
.sym
;
4877 if (st_sym
->attr
.vtab
|| st_sym
->attr
.vtype
)
4880 /* If the existing symbol is generic from a different module and
4881 the new symbol is generic there can be no ambiguity. */
4882 if (st_sym
->attr
.generic
4884 && st_sym
->module
!= module_name
)
4886 /* The new symbol's attributes have not yet been read. Since
4887 we need attr.generic, read it directly. */
4888 get_module_locus (&locus
);
4889 set_module_locus (&info
->u
.rsym
.where
);
4892 mio_symbol_attribute (&attr
);
4893 set_module_locus (&locus
);
4902 /* Read a module file. */
4907 module_locus operator_interfaces
, user_operators
, extensions
, omp_udrs
;
4909 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4911 int ambiguous
, j
, nuse
, symbol
;
4912 pointer_info
*info
, *q
;
4913 gfc_use_rename
*u
= NULL
;
4917 get_module_locus (&operator_interfaces
); /* Skip these for now. */
4920 get_module_locus (&user_operators
);
4924 /* Skip commons, equivalences and derived type extensions for now. */
4928 get_module_locus (&extensions
);
4931 /* Skip OpenMP UDRs. */
4932 get_module_locus (&omp_udrs
);
4937 /* Create the fixup nodes for all the symbols. */
4939 while (peek_atom () != ATOM_RPAREN
)
4942 require_atom (ATOM_INTEGER
);
4943 info
= get_integer (atom_int
);
4945 info
->type
= P_SYMBOL
;
4946 info
->u
.rsym
.state
= UNUSED
;
4948 info
->u
.rsym
.true_name
= read_string ();
4949 info
->u
.rsym
.module
= read_string ();
4950 bind_label
= read_string ();
4951 if (strlen (bind_label
))
4952 info
->u
.rsym
.binding_label
= bind_label
;
4954 XDELETEVEC (bind_label
);
4956 require_atom (ATOM_INTEGER
);
4957 info
->u
.rsym
.ns
= atom_int
;
4959 get_module_locus (&info
->u
.rsym
.where
);
4961 /* See if the symbol has already been loaded by a previous module.
4962 If so, we reference the existing symbol and prevent it from
4963 being loaded again. This should not happen if the symbol being
4964 read is an index for an assumed shape dummy array (ns != 1). */
4966 sym
= find_true_name (info
->u
.rsym
.true_name
, info
->u
.rsym
.module
);
4969 || (sym
->attr
.flavor
== FL_VARIABLE
&& info
->u
.rsym
.ns
!=1))
4975 info
->u
.rsym
.state
= USED
;
4976 info
->u
.rsym
.sym
= sym
;
4977 /* The current symbol has already been loaded, so we can avoid loading
4978 it again. However, if it is a derived type, some of its components
4979 can be used in expressions in the module. To avoid the module loading
4980 failing, we need to associate the module's component pointer indexes
4981 with the existing symbol's component pointers. */
4982 if (sym
->attr
.flavor
== FL_DERIVED
)
4986 /* First seek to the symbol's component list. */
4987 mio_lparen (); /* symbol opening. */
4988 skip_list (); /* skip symbol attribute. */
4990 mio_lparen (); /* component list opening. */
4991 for (c
= sym
->components
; c
; c
= c
->next
)
4994 const char *comp_name
;
4997 mio_lparen (); /* component opening. */
4999 p
= get_integer (n
);
5000 if (p
->u
.pointer
== NULL
)
5001 associate_integer_pointer (p
, c
);
5002 mio_pool_string (&comp_name
);
5003 gcc_assert (comp_name
== c
->name
);
5004 skip_list (1); /* component end. */
5006 mio_rparen (); /* component list closing. */
5008 skip_list (1); /* symbol end. */
5013 /* Some symbols do not have a namespace (eg. formal arguments),
5014 so the automatic "unique symtree" mechanism must be suppressed
5015 by marking them as referenced. */
5016 q
= get_integer (info
->u
.rsym
.ns
);
5017 if (q
->u
.pointer
== NULL
)
5019 info
->u
.rsym
.referenced
= 1;
5023 /* If possible recycle the symtree that references the symbol.
5024 If a symtree is not found and the module does not import one,
5025 a unique-name symtree is found by read_cleanup. */
5026 st
= find_symtree_for_symbol (gfc_current_ns
->sym_root
, sym
);
5029 info
->u
.rsym
.symtree
= st
;
5030 info
->u
.rsym
.referenced
= 1;
5036 /* Parse the symtree lists. This lets us mark which symbols need to
5037 be loaded. Renaming is also done at this point by replacing the
5042 while (peek_atom () != ATOM_RPAREN
)
5044 mio_internal_string (name
);
5045 mio_integer (&ambiguous
);
5046 mio_integer (&symbol
);
5048 info
= get_integer (symbol
);
5050 /* See how many use names there are. If none, go through the start
5051 of the loop at least once. */
5052 nuse
= number_use_names (name
, false);
5053 info
->u
.rsym
.renamed
= nuse
? 1 : 0;
5058 for (j
= 1; j
<= nuse
; j
++)
5060 /* Get the jth local name for this symbol. */
5061 p
= find_use_name_n (name
, &j
, false);
5063 if (p
== NULL
&& strcmp (name
, module_name
) == 0)
5066 /* Exception: Always import vtabs & vtypes. */
5067 if (p
== NULL
&& name
[0] == '_'
5068 && (strncmp (name
, "__vtab_", 5) == 0
5069 || strncmp (name
, "__vtype_", 6) == 0))
5072 /* Skip symtree nodes not in an ONLY clause, unless there
5073 is an existing symtree loaded from another USE statement. */
5076 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
5078 && strcmp (st
->n
.sym
->name
, info
->u
.rsym
.true_name
) == 0
5079 && st
->n
.sym
->module
!= NULL
5080 && strcmp (st
->n
.sym
->module
, info
->u
.rsym
.module
) == 0)
5082 info
->u
.rsym
.symtree
= st
;
5083 info
->u
.rsym
.sym
= st
->n
.sym
;
5088 /* If a symbol of the same name and module exists already,
5089 this symbol, which is not in an ONLY clause, must not be
5090 added to the namespace(11.3.2). Note that find_symbol
5091 only returns the first occurrence that it finds. */
5092 if (!only_flag
&& !info
->u
.rsym
.renamed
5093 && strcmp (name
, module_name
) != 0
5094 && find_symbol (gfc_current_ns
->sym_root
, name
,
5098 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
5102 /* Check for ambiguous symbols. */
5103 if (check_for_ambiguous (st
->n
.sym
, info
))
5106 info
->u
.rsym
.symtree
= st
;
5110 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
5112 /* Create a symtree node in the current namespace for this
5114 st
= check_unique_name (p
)
5115 ? gfc_get_unique_symtree (gfc_current_ns
)
5116 : gfc_new_symtree (&gfc_current_ns
->sym_root
, p
);
5117 st
->ambiguous
= ambiguous
;
5119 sym
= info
->u
.rsym
.sym
;
5121 /* Create a symbol node if it doesn't already exist. */
5124 info
->u
.rsym
.sym
= gfc_new_symbol (info
->u
.rsym
.true_name
,
5126 info
->u
.rsym
.sym
->name
= dt_lower_string (info
->u
.rsym
.true_name
);
5127 sym
= info
->u
.rsym
.sym
;
5128 sym
->module
= gfc_get_string (info
->u
.rsym
.module
);
5130 if (info
->u
.rsym
.binding_label
)
5131 sym
->binding_label
=
5132 IDENTIFIER_POINTER (get_identifier
5133 (info
->u
.rsym
.binding_label
));
5139 if (strcmp (name
, p
) != 0)
5140 sym
->attr
.use_rename
= 1;
5143 || (strncmp (name
, "__vtab_", 5) != 0
5144 && strncmp (name
, "__vtype_", 6) != 0))
5145 sym
->attr
.use_only
= only_flag
;
5147 /* Store the symtree pointing to this symbol. */
5148 info
->u
.rsym
.symtree
= st
;
5150 if (info
->u
.rsym
.state
== UNUSED
)
5151 info
->u
.rsym
.state
= NEEDED
;
5152 info
->u
.rsym
.referenced
= 1;
5159 /* Load intrinsic operator interfaces. */
5160 set_module_locus (&operator_interfaces
);
5163 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
5165 if (i
== INTRINSIC_USER
)
5170 u
= find_use_operator ((gfc_intrinsic_op
) i
);
5181 mio_interface (&gfc_current_ns
->op
[i
]);
5182 if (u
&& !gfc_current_ns
->op
[i
])
5188 /* Load generic and user operator interfaces. These must follow the
5189 loading of symtree because otherwise symbols can be marked as
5192 set_module_locus (&user_operators
);
5194 load_operator_interfaces ();
5195 load_generic_interfaces ();
5200 /* Load OpenMP user defined reductions. */
5201 set_module_locus (&omp_udrs
);
5204 /* At this point, we read those symbols that are needed but haven't
5205 been loaded yet. If one symbol requires another, the other gets
5206 marked as NEEDED if its previous state was UNUSED. */
5208 while (load_needed (pi_root
));
5210 /* Make sure all elements of the rename-list were found in the module. */
5212 for (u
= gfc_rename_list
; u
; u
= u
->next
)
5217 if (u
->op
== INTRINSIC_NONE
)
5219 gfc_error ("Symbol %qs referenced at %L not found in module %qs",
5220 u
->use_name
, &u
->where
, module_name
);
5224 if (u
->op
== INTRINSIC_USER
)
5226 gfc_error ("User operator %qs referenced at %L not found "
5227 "in module %qs", u
->use_name
, &u
->where
, module_name
);
5231 gfc_error ("Intrinsic operator %qs referenced at %L not found "
5232 "in module %qs", gfc_op2string (u
->op
), &u
->where
,
5236 /* Now we should be in a position to fill f2k_derived with derived type
5237 extensions, since everything has been loaded. */
5238 set_module_locus (&extensions
);
5239 load_derived_extensions ();
5241 /* Clean up symbol nodes that were never loaded, create references
5242 to hidden symbols. */
5244 read_cleanup (pi_root
);
5248 /* Given an access type that is specific to an entity and the default
5249 access, return nonzero if the entity is publicly accessible. If the
5250 element is declared as PUBLIC, then it is public; if declared
5251 PRIVATE, then private, and otherwise it is public unless the default
5252 access in this context has been declared PRIVATE. */
5255 check_access (gfc_access specific_access
, gfc_access default_access
)
5257 if (specific_access
== ACCESS_PUBLIC
)
5259 if (specific_access
== ACCESS_PRIVATE
)
5262 if (flag_module_private
)
5263 return default_access
== ACCESS_PUBLIC
;
5265 return default_access
!= ACCESS_PRIVATE
;
5270 gfc_check_symbol_access (gfc_symbol
*sym
)
5272 if (sym
->attr
.vtab
|| sym
->attr
.vtype
)
5275 return check_access (sym
->attr
.access
, sym
->ns
->default_access
);
5279 /* A structure to remember which commons we've already written. */
5281 struct written_common
5283 BBT_HEADER(written_common
);
5284 const char *name
, *label
;
5287 static struct written_common
*written_commons
= NULL
;
5289 /* Comparison function used for balancing the binary tree. */
5292 compare_written_commons (void *a1
, void *b1
)
5294 const char *aname
= ((struct written_common
*) a1
)->name
;
5295 const char *alabel
= ((struct written_common
*) a1
)->label
;
5296 const char *bname
= ((struct written_common
*) b1
)->name
;
5297 const char *blabel
= ((struct written_common
*) b1
)->label
;
5298 int c
= strcmp (aname
, bname
);
5300 return (c
!= 0 ? c
: strcmp (alabel
, blabel
));
5303 /* Free a list of written commons. */
5306 free_written_common (struct written_common
*w
)
5312 free_written_common (w
->left
);
5314 free_written_common (w
->right
);
5319 /* Write a common block to the module -- recursive helper function. */
5322 write_common_0 (gfc_symtree
*st
, bool this_module
)
5328 struct written_common
*w
;
5329 bool write_me
= true;
5334 write_common_0 (st
->left
, this_module
);
5336 /* We will write out the binding label, or "" if no label given. */
5337 name
= st
->n
.common
->name
;
5339 label
= (p
->is_bind_c
&& p
->binding_label
) ? p
->binding_label
: "";
5341 /* Check if we've already output this common. */
5342 w
= written_commons
;
5345 int c
= strcmp (name
, w
->name
);
5346 c
= (c
!= 0 ? c
: strcmp (label
, w
->label
));
5350 w
= (c
< 0) ? w
->left
: w
->right
;
5353 if (this_module
&& p
->use_assoc
)
5358 /* Write the common to the module. */
5360 mio_pool_string (&name
);
5362 mio_symbol_ref (&p
->head
);
5363 flags
= p
->saved
? 1 : 0;
5364 if (p
->threadprivate
)
5366 mio_integer (&flags
);
5368 /* Write out whether the common block is bind(c) or not. */
5369 mio_integer (&(p
->is_bind_c
));
5371 mio_pool_string (&label
);
5374 /* Record that we have written this common. */
5375 w
= XCNEW (struct written_common
);
5378 gfc_insert_bbt (&written_commons
, w
, compare_written_commons
);
5381 write_common_0 (st
->right
, this_module
);
5385 /* Write a common, by initializing the list of written commons, calling
5386 the recursive function write_common_0() and cleaning up afterwards. */
5389 write_common (gfc_symtree
*st
)
5391 written_commons
= NULL
;
5392 write_common_0 (st
, true);
5393 write_common_0 (st
, false);
5394 free_written_common (written_commons
);
5395 written_commons
= NULL
;
5399 /* Write the blank common block to the module. */
5402 write_blank_common (void)
5404 const char * name
= BLANK_COMMON_NAME
;
5406 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5407 this, but it hasn't been checked. Just making it so for now. */
5410 if (gfc_current_ns
->blank_common
.head
== NULL
)
5415 mio_pool_string (&name
);
5417 mio_symbol_ref (&gfc_current_ns
->blank_common
.head
);
5418 saved
= gfc_current_ns
->blank_common
.saved
;
5419 mio_integer (&saved
);
5421 /* Write out whether the common block is bind(c) or not. */
5422 mio_integer (&is_bind_c
);
5424 /* Write out an empty binding label. */
5425 write_atom (ATOM_STRING
, "");
5431 /* Write equivalences to the module. */
5440 for (eq
= gfc_current_ns
->equiv
; eq
; eq
= eq
->next
)
5444 for (e
= eq
; e
; e
= e
->eq
)
5446 if (e
->module
== NULL
)
5447 e
->module
= gfc_get_string ("%s.eq.%d", module_name
, num
);
5448 mio_allocated_string (e
->module
);
5449 mio_expr (&e
->expr
);
5458 /* Write derived type extensions to the module. */
5461 write_dt_extensions (gfc_symtree
*st
)
5463 if (!gfc_check_symbol_access (st
->n
.sym
))
5465 if (!(st
->n
.sym
->ns
&& st
->n
.sym
->ns
->proc_name
5466 && st
->n
.sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
5470 mio_pool_string (&st
->name
);
5471 if (st
->n
.sym
->module
!= NULL
)
5472 mio_pool_string (&st
->n
.sym
->module
);
5475 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5476 if (iomode
== IO_OUTPUT
)
5477 strcpy (name
, module_name
);
5478 mio_internal_string (name
);
5479 if (iomode
== IO_INPUT
)
5480 module_name
= gfc_get_string (name
);
5486 write_derived_extensions (gfc_symtree
*st
)
5488 if (!((st
->n
.sym
->attr
.flavor
== FL_DERIVED
)
5489 && (st
->n
.sym
->f2k_derived
!= NULL
)
5490 && (st
->n
.sym
->f2k_derived
->sym_root
!= NULL
)))
5494 mio_symbol_ref (&(st
->n
.sym
));
5495 gfc_traverse_symtree (st
->n
.sym
->f2k_derived
->sym_root
,
5496 write_dt_extensions
);
5501 /* Write a symbol to the module. */
5504 write_symbol (int n
, gfc_symbol
*sym
)
5508 if (sym
->attr
.flavor
== FL_UNKNOWN
|| sym
->attr
.flavor
== FL_LABEL
)
5509 gfc_internal_error ("write_symbol(): bad module symbol %qs", sym
->name
);
5513 if (sym
->attr
.flavor
== FL_DERIVED
)
5516 name
= dt_upper_string (sym
->name
);
5517 mio_pool_string (&name
);
5520 mio_pool_string (&sym
->name
);
5522 mio_pool_string (&sym
->module
);
5523 if ((sym
->attr
.is_bind_c
|| sym
->attr
.is_iso_c
) && sym
->binding_label
)
5525 label
= sym
->binding_label
;
5526 mio_pool_string (&label
);
5529 write_atom (ATOM_STRING
, "");
5531 mio_pointer_ref (&sym
->ns
);
5538 /* Recursive traversal function to write the initial set of symbols to
5539 the module. We check to see if the symbol should be written
5540 according to the access specification. */
5543 write_symbol0 (gfc_symtree
*st
)
5547 bool dont_write
= false;
5552 write_symbol0 (st
->left
);
5555 if (sym
->module
== NULL
)
5556 sym
->module
= module_name
;
5558 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
5559 && !sym
->attr
.subroutine
&& !sym
->attr
.function
)
5562 if (!gfc_check_symbol_access (sym
))
5567 p
= get_pointer (sym
);
5568 if (p
->type
== P_UNKNOWN
)
5571 if (p
->u
.wsym
.state
!= WRITTEN
)
5573 write_symbol (p
->integer
, sym
);
5574 p
->u
.wsym
.state
= WRITTEN
;
5578 write_symbol0 (st
->right
);
5583 write_omp_udr (gfc_omp_udr
*udr
)
5587 case OMP_REDUCTION_USER
:
5588 /* Non-operators can't be used outside of the module. */
5589 if (udr
->name
[0] != '.')
5594 size_t len
= strlen (udr
->name
+ 1);
5595 char *name
= XALLOCAVEC (char, len
);
5596 memcpy (name
, udr
->name
, len
- 1);
5597 name
[len
- 1] = '\0';
5598 st
= gfc_find_symtree (gfc_current_ns
->uop_root
, name
);
5599 /* If corresponding user operator is private, don't write
5603 gfc_user_op
*uop
= st
->n
.uop
;
5604 if (!check_access (uop
->access
, uop
->ns
->default_access
))
5609 case OMP_REDUCTION_PLUS
:
5610 case OMP_REDUCTION_MINUS
:
5611 case OMP_REDUCTION_TIMES
:
5612 case OMP_REDUCTION_AND
:
5613 case OMP_REDUCTION_OR
:
5614 case OMP_REDUCTION_EQV
:
5615 case OMP_REDUCTION_NEQV
:
5616 /* If corresponding operator is private, don't write the UDR. */
5617 if (!check_access (gfc_current_ns
->operator_access
[udr
->rop
],
5618 gfc_current_ns
->default_access
))
5624 if (udr
->ts
.type
== BT_DERIVED
|| udr
->ts
.type
== BT_CLASS
)
5626 /* If derived type is private, don't write the UDR. */
5627 if (!gfc_check_symbol_access (udr
->ts
.u
.derived
))
5632 mio_pool_string (&udr
->name
);
5633 mio_typespec (&udr
->ts
);
5634 mio_omp_udr_expr (udr
, &udr
->omp_out
, &udr
->omp_in
, udr
->combiner_ns
, false);
5635 if (udr
->initializer_ns
)
5636 mio_omp_udr_expr (udr
, &udr
->omp_priv
, &udr
->omp_orig
,
5637 udr
->initializer_ns
, true);
5643 write_omp_udrs (gfc_symtree
*st
)
5648 write_omp_udrs (st
->left
);
5650 for (udr
= st
->n
.omp_udr
; udr
; udr
= udr
->next
)
5651 write_omp_udr (udr
);
5652 write_omp_udrs (st
->right
);
5656 /* Type for the temporary tree used when writing secondary symbols. */
5658 struct sorted_pointer_info
5660 BBT_HEADER (sorted_pointer_info
);
5665 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5667 /* Recursively traverse the temporary tree, free its contents. */
5670 free_sorted_pointer_info_tree (sorted_pointer_info
*p
)
5675 free_sorted_pointer_info_tree (p
->left
);
5676 free_sorted_pointer_info_tree (p
->right
);
5681 /* Comparison function for the temporary tree. */
5684 compare_sorted_pointer_info (void *_spi1
, void *_spi2
)
5686 sorted_pointer_info
*spi1
, *spi2
;
5687 spi1
= (sorted_pointer_info
*)_spi1
;
5688 spi2
= (sorted_pointer_info
*)_spi2
;
5690 if (spi1
->p
->integer
< spi2
->p
->integer
)
5692 if (spi1
->p
->integer
> spi2
->p
->integer
)
5698 /* Finds the symbols that need to be written and collects them in the
5699 sorted_pi tree so that they can be traversed in an order
5700 independent of memory addresses. */
5703 find_symbols_to_write(sorted_pointer_info
**tree
, pointer_info
*p
)
5708 if (p
->type
== P_SYMBOL
&& p
->u
.wsym
.state
== NEEDS_WRITE
)
5710 sorted_pointer_info
*sp
= gfc_get_sorted_pointer_info();
5713 gfc_insert_bbt (tree
, sp
, compare_sorted_pointer_info
);
5716 find_symbols_to_write (tree
, p
->left
);
5717 find_symbols_to_write (tree
, p
->right
);
5721 /* Recursive function that traverses the tree of symbols that need to be
5722 written and writes them in order. */
5725 write_symbol1_recursion (sorted_pointer_info
*sp
)
5730 write_symbol1_recursion (sp
->left
);
5732 pointer_info
*p1
= sp
->p
;
5733 gcc_assert (p1
->type
== P_SYMBOL
&& p1
->u
.wsym
.state
== NEEDS_WRITE
);
5735 p1
->u
.wsym
.state
= WRITTEN
;
5736 write_symbol (p1
->integer
, p1
->u
.wsym
.sym
);
5737 p1
->u
.wsym
.sym
->attr
.public_used
= 1;
5739 write_symbol1_recursion (sp
->right
);
5743 /* Write the secondary set of symbols to the module file. These are
5744 symbols that were not public yet are needed by the public symbols
5745 or another dependent symbol. The act of writing a symbol can add
5746 symbols to the pointer_info tree, so we return nonzero if a symbol
5747 was written and pass that information upwards. The caller will
5748 then call this function again until nothing was written. It uses
5749 the utility functions and a temporary tree to ensure a reproducible
5750 ordering of the symbol output and thus the module file. */
5753 write_symbol1 (pointer_info
*p
)
5758 /* Put symbols that need to be written into a tree sorted on the
5761 sorted_pointer_info
*spi_root
= NULL
;
5762 find_symbols_to_write (&spi_root
, p
);
5764 /* No symbols to write, return. */
5768 /* Otherwise, write and free the tree again. */
5769 write_symbol1_recursion (spi_root
);
5770 free_sorted_pointer_info_tree (spi_root
);
5776 /* Write operator interfaces associated with a symbol. */
5779 write_operator (gfc_user_op
*uop
)
5781 static char nullstring
[] = "";
5782 const char *p
= nullstring
;
5784 if (uop
->op
== NULL
|| !check_access (uop
->access
, uop
->ns
->default_access
))
5787 mio_symbol_interface (&uop
->name
, &p
, &uop
->op
);
5791 /* Write generic interfaces from the namespace sym_root. */
5794 write_generic (gfc_symtree
*st
)
5801 write_generic (st
->left
);
5804 if (sym
&& !check_unique_name (st
->name
)
5805 && sym
->generic
&& gfc_check_symbol_access (sym
))
5808 sym
->module
= module_name
;
5810 mio_symbol_interface (&st
->name
, &sym
->module
, &sym
->generic
);
5813 write_generic (st
->right
);
5818 write_symtree (gfc_symtree
*st
)
5825 /* A symbol in an interface body must not be visible in the
5827 if (sym
->ns
!= gfc_current_ns
5828 && sym
->ns
->proc_name
5829 && sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
5832 if (!gfc_check_symbol_access (sym
)
5833 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
5834 && !sym
->attr
.subroutine
&& !sym
->attr
.function
))
5837 if (check_unique_name (st
->name
))
5840 p
= find_pointer (sym
);
5842 gfc_internal_error ("write_symtree(): Symbol not written");
5844 mio_pool_string (&st
->name
);
5845 mio_integer (&st
->ambiguous
);
5846 mio_integer (&p
->integer
);
5855 /* Write the operator interfaces. */
5858 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
5860 if (i
== INTRINSIC_USER
)
5863 mio_interface (check_access (gfc_current_ns
->operator_access
[i
],
5864 gfc_current_ns
->default_access
)
5865 ? &gfc_current_ns
->op
[i
] : NULL
);
5873 gfc_traverse_user_op (gfc_current_ns
, write_operator
);
5879 write_generic (gfc_current_ns
->sym_root
);
5885 write_blank_common ();
5886 write_common (gfc_current_ns
->common_root
);
5898 gfc_traverse_symtree (gfc_current_ns
->sym_root
,
5899 write_derived_extensions
);
5905 write_omp_udrs (gfc_current_ns
->omp_udr_root
);
5910 /* Write symbol information. First we traverse all symbols in the
5911 primary namespace, writing those that need to be written.
5912 Sometimes writing one symbol will cause another to need to be
5913 written. A list of these symbols ends up on the write stack, and
5914 we end by popping the bottom of the stack and writing the symbol
5915 until the stack is empty. */
5919 write_symbol0 (gfc_current_ns
->sym_root
);
5920 while (write_symbol1 (pi_root
))
5929 gfc_traverse_symtree (gfc_current_ns
->sym_root
, write_symtree
);
5934 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5935 true on success, false on failure. */
5938 read_crc32_from_module_file (const char* filename
, uLong
* crc
)
5944 /* Open the file in binary mode. */
5945 if ((file
= fopen (filename
, "rb")) == NULL
)
5948 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5949 file. See RFC 1952. */
5950 if (fseek (file
, -8, SEEK_END
) != 0)
5956 /* Read the CRC32. */
5957 if (fread (buf
, 1, 4, file
) != 4)
5963 /* Close the file. */
5966 val
= (buf
[0] & 0xFF) + ((buf
[1] & 0xFF) << 8) + ((buf
[2] & 0xFF) << 16)
5967 + ((buf
[3] & 0xFF) << 24);
5970 /* For debugging, the CRC value printed in hexadecimal should match
5971 the CRC printed by "zcat -l -v filename".
5972 printf("CRC of file %s is %x\n", filename, val); */
5978 /* Given module, dump it to disk. If there was an error while
5979 processing the module, dump_flag will be set to zero and we delete
5980 the module file, even if it was already there. */
5983 gfc_dump_module (const char *name
, int dump_flag
)
5986 char *filename
, *filename_tmp
;
5989 n
= strlen (name
) + strlen (MODULE_EXTENSION
) + 1;
5990 if (gfc_option
.module_dir
!= NULL
)
5992 n
+= strlen (gfc_option
.module_dir
);
5993 filename
= (char *) alloca (n
);
5994 strcpy (filename
, gfc_option
.module_dir
);
5995 strcat (filename
, name
);
5999 filename
= (char *) alloca (n
);
6000 strcpy (filename
, name
);
6002 strcat (filename
, MODULE_EXTENSION
);
6004 /* Name of the temporary file used to write the module. */
6005 filename_tmp
= (char *) alloca (n
+ 1);
6006 strcpy (filename_tmp
, filename
);
6007 strcat (filename_tmp
, "0");
6009 /* There was an error while processing the module. We delete the
6010 module file, even if it was already there. */
6017 if (gfc_cpp_makedep ())
6018 gfc_cpp_add_target (filename
);
6020 /* Write the module to the temporary file. */
6021 module_fp
= gzopen (filename_tmp
, "w");
6022 if (module_fp
== NULL
)
6023 gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
6024 filename_tmp
, xstrerror (errno
));
6026 gzprintf (module_fp
, "GFORTRAN module version '%s' created from %s\n",
6027 MOD_VERSION
, gfc_source_file
);
6029 /* Write the module itself. */
6031 module_name
= gfc_get_string (name
);
6037 free_pi_tree (pi_root
);
6042 if (gzclose (module_fp
))
6043 gfc_fatal_error ("Error writing module file %qs for writing: %s",
6044 filename_tmp
, xstrerror (errno
));
6046 /* Read the CRC32 from the gzip trailers of the module files and
6048 if (!read_crc32_from_module_file (filename_tmp
, &crc
)
6049 || !read_crc32_from_module_file (filename
, &crc_old
)
6052 /* Module file have changed, replace the old one. */
6053 if (remove (filename
) && errno
!= ENOENT
)
6054 gfc_fatal_error ("Can't delete module file %qs: %s", filename
,
6056 if (rename (filename_tmp
, filename
))
6057 gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
6058 filename_tmp
, filename
, xstrerror (errno
));
6062 if (remove (filename_tmp
))
6063 gfc_fatal_error ("Can't delete temporary module file %qs: %s",
6064 filename_tmp
, xstrerror (errno
));
6070 create_intrinsic_function (const char *name
, int id
,
6071 const char *modname
, intmod_id module
,
6072 bool subroutine
, gfc_symbol
*result_type
)
6074 gfc_intrinsic_sym
*isym
;
6075 gfc_symtree
*tmp_symtree
;
6078 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
6081 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
6083 gfc_error ("Symbol %qs already declared", name
);
6086 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
6087 sym
= tmp_symtree
->n
.sym
;
6091 gfc_isym_id isym_id
= gfc_isym_id_by_intmod (module
, id
);
6092 isym
= gfc_intrinsic_subroutine_by_id (isym_id
);
6093 sym
->attr
.subroutine
= 1;
6097 gfc_isym_id isym_id
= gfc_isym_id_by_intmod (module
, id
);
6098 isym
= gfc_intrinsic_function_by_id (isym_id
);
6100 sym
->attr
.function
= 1;
6103 sym
->ts
.type
= BT_DERIVED
;
6104 sym
->ts
.u
.derived
= result_type
;
6105 sym
->ts
.is_c_interop
= 1;
6106 isym
->ts
.f90_type
= BT_VOID
;
6107 isym
->ts
.type
= BT_DERIVED
;
6108 isym
->ts
.f90_type
= BT_VOID
;
6109 isym
->ts
.u
.derived
= result_type
;
6110 isym
->ts
.is_c_interop
= 1;
6115 sym
->attr
.flavor
= FL_PROCEDURE
;
6116 sym
->attr
.intrinsic
= 1;
6118 sym
->module
= gfc_get_string (modname
);
6119 sym
->attr
.use_assoc
= 1;
6120 sym
->from_intmod
= module
;
6121 sym
->intmod_sym_id
= id
;
6125 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
6126 the current namespace for all named constants, pointer types, and
6127 procedures in the module unless the only clause was used or a rename
6128 list was provided. */
6131 import_iso_c_binding_module (void)
6133 gfc_symbol
*mod_sym
= NULL
, *return_type
;
6134 gfc_symtree
*mod_symtree
= NULL
, *tmp_symtree
;
6135 gfc_symtree
*c_ptr
= NULL
, *c_funptr
= NULL
;
6136 const char *iso_c_module_name
= "__iso_c_binding";
6139 bool want_c_ptr
= false, want_c_funptr
= false;
6141 /* Look only in the current namespace. */
6142 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, iso_c_module_name
);
6144 if (mod_symtree
== NULL
)
6146 /* symtree doesn't already exist in current namespace. */
6147 gfc_get_sym_tree (iso_c_module_name
, gfc_current_ns
, &mod_symtree
,
6150 if (mod_symtree
!= NULL
)
6151 mod_sym
= mod_symtree
->n
.sym
;
6153 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
6154 "create symbol for %s", iso_c_module_name
);
6156 mod_sym
->attr
.flavor
= FL_MODULE
;
6157 mod_sym
->attr
.intrinsic
= 1;
6158 mod_sym
->module
= gfc_get_string (iso_c_module_name
);
6159 mod_sym
->from_intmod
= INTMOD_ISO_C_BINDING
;
6162 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
6163 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
6165 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6167 if (strcmp (c_interop_kinds_table
[ISOCBINDING_NULL_PTR
].name
,
6170 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_LOC
].name
,
6173 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_NULL_FUNPTR
].name
,
6175 want_c_funptr
= true;
6176 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_FUNLOC
].name
,
6178 want_c_funptr
= true;
6179 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_PTR
].name
,
6182 c_ptr
= generate_isocbinding_symbol (iso_c_module_name
,
6183 (iso_c_binding_symbol
)
6185 u
->local_name
[0] ? u
->local_name
6189 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_FUNPTR
].name
,
6193 = generate_isocbinding_symbol (iso_c_module_name
,
6194 (iso_c_binding_symbol
)
6196 u
->local_name
[0] ? u
->local_name
6202 if ((want_c_ptr
|| !only_flag
) && !c_ptr
)
6203 c_ptr
= generate_isocbinding_symbol (iso_c_module_name
,
6204 (iso_c_binding_symbol
)
6206 NULL
, NULL
, only_flag
);
6207 if ((want_c_funptr
|| !only_flag
) && !c_funptr
)
6208 c_funptr
= generate_isocbinding_symbol (iso_c_module_name
,
6209 (iso_c_binding_symbol
)
6211 NULL
, NULL
, only_flag
);
6213 /* Generate the symbols for the named constants representing
6214 the kinds for intrinsic data types. */
6215 for (i
= 0; i
< ISOCBINDING_NUMBER
; i
++)
6218 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6219 if (strcmp (c_interop_kinds_table
[i
].name
, u
->use_name
) == 0)
6228 #define NAMED_FUNCTION(a,b,c,d) \
6230 not_in_std = (gfc_option.allow_std & d) == 0; \
6233 #define NAMED_SUBROUTINE(a,b,c,d) \
6235 not_in_std = (gfc_option.allow_std & d) == 0; \
6238 #define NAMED_INTCST(a,b,c,d) \
6240 not_in_std = (gfc_option.allow_std & d) == 0; \
6243 #define NAMED_REALCST(a,b,c,d) \
6245 not_in_std = (gfc_option.allow_std & d) == 0; \
6248 #define NAMED_CMPXCST(a,b,c,d) \
6250 not_in_std = (gfc_option.allow_std & d) == 0; \
6253 #include "iso-c-binding.def"
6261 gfc_error ("The symbol %qs, referenced at %L, is not "
6262 "in the selected standard", name
, &u
->where
);
6268 #define NAMED_FUNCTION(a,b,c,d) \
6270 if (a == ISOCBINDING_LOC) \
6271 return_type = c_ptr->n.sym; \
6272 else if (a == ISOCBINDING_FUNLOC) \
6273 return_type = c_funptr->n.sym; \
6275 return_type = NULL; \
6276 create_intrinsic_function (u->local_name[0] \
6277 ? u->local_name : u->use_name, \
6278 a, iso_c_module_name, \
6279 INTMOD_ISO_C_BINDING, false, \
6282 #define NAMED_SUBROUTINE(a,b,c,d) \
6284 create_intrinsic_function (u->local_name[0] ? u->local_name \
6286 a, iso_c_module_name, \
6287 INTMOD_ISO_C_BINDING, true, NULL); \
6289 #include "iso-c-binding.def"
6291 case ISOCBINDING_PTR
:
6292 case ISOCBINDING_FUNPTR
:
6293 /* Already handled above. */
6296 if (i
== ISOCBINDING_NULL_PTR
)
6297 tmp_symtree
= c_ptr
;
6298 else if (i
== ISOCBINDING_NULL_FUNPTR
)
6299 tmp_symtree
= c_funptr
;
6302 generate_isocbinding_symbol (iso_c_module_name
,
6303 (iso_c_binding_symbol
) i
,
6305 ? u
->local_name
: u
->use_name
,
6306 tmp_symtree
, false);
6310 if (!found
&& !only_flag
)
6312 /* Skip, if the symbol is not in the enabled standard. */
6315 #define NAMED_FUNCTION(a,b,c,d) \
6317 if ((gfc_option.allow_std & d) == 0) \
6320 #define NAMED_SUBROUTINE(a,b,c,d) \
6322 if ((gfc_option.allow_std & d) == 0) \
6325 #define NAMED_INTCST(a,b,c,d) \
6327 if ((gfc_option.allow_std & d) == 0) \
6330 #define NAMED_REALCST(a,b,c,d) \
6332 if ((gfc_option.allow_std & d) == 0) \
6335 #define NAMED_CMPXCST(a,b,c,d) \
6337 if ((gfc_option.allow_std & d) == 0) \
6340 #include "iso-c-binding.def"
6342 ; /* Not GFC_STD_* versioned. */
6347 #define NAMED_FUNCTION(a,b,c,d) \
6349 if (a == ISOCBINDING_LOC) \
6350 return_type = c_ptr->n.sym; \
6351 else if (a == ISOCBINDING_FUNLOC) \
6352 return_type = c_funptr->n.sym; \
6354 return_type = NULL; \
6355 create_intrinsic_function (b, a, iso_c_module_name, \
6356 INTMOD_ISO_C_BINDING, false, \
6359 #define NAMED_SUBROUTINE(a,b,c,d) \
6361 create_intrinsic_function (b, a, iso_c_module_name, \
6362 INTMOD_ISO_C_BINDING, true, NULL); \
6364 #include "iso-c-binding.def"
6366 case ISOCBINDING_PTR
:
6367 case ISOCBINDING_FUNPTR
:
6368 /* Already handled above. */
6371 if (i
== ISOCBINDING_NULL_PTR
)
6372 tmp_symtree
= c_ptr
;
6373 else if (i
== ISOCBINDING_NULL_FUNPTR
)
6374 tmp_symtree
= c_funptr
;
6377 generate_isocbinding_symbol (iso_c_module_name
,
6378 (iso_c_binding_symbol
) i
, NULL
,
6379 tmp_symtree
, false);
6384 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6389 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6390 "module ISO_C_BINDING", u
->use_name
, &u
->where
);
6395 /* Add an integer named constant from a given module. */
6398 create_int_parameter (const char *name
, int value
, const char *modname
,
6399 intmod_id module
, int id
)
6401 gfc_symtree
*tmp_symtree
;
6404 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
6405 if (tmp_symtree
!= NULL
)
6407 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
6410 gfc_error ("Symbol %qs already declared", name
);
6413 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
6414 sym
= tmp_symtree
->n
.sym
;
6416 sym
->module
= gfc_get_string (modname
);
6417 sym
->attr
.flavor
= FL_PARAMETER
;
6418 sym
->ts
.type
= BT_INTEGER
;
6419 sym
->ts
.kind
= gfc_default_integer_kind
;
6420 sym
->value
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, value
);
6421 sym
->attr
.use_assoc
= 1;
6422 sym
->from_intmod
= module
;
6423 sym
->intmod_sym_id
= id
;
6427 /* Value is already contained by the array constructor, but not
6431 create_int_parameter_array (const char *name
, int size
, gfc_expr
*value
,
6432 const char *modname
, intmod_id module
, int id
)
6434 gfc_symtree
*tmp_symtree
;
6437 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
6438 if (tmp_symtree
!= NULL
)
6440 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
6443 gfc_error ("Symbol %qs already declared", name
);
6446 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
6447 sym
= tmp_symtree
->n
.sym
;
6449 sym
->module
= gfc_get_string (modname
);
6450 sym
->attr
.flavor
= FL_PARAMETER
;
6451 sym
->ts
.type
= BT_INTEGER
;
6452 sym
->ts
.kind
= gfc_default_integer_kind
;
6453 sym
->attr
.use_assoc
= 1;
6454 sym
->from_intmod
= module
;
6455 sym
->intmod_sym_id
= id
;
6456 sym
->attr
.dimension
= 1;
6457 sym
->as
= gfc_get_array_spec ();
6459 sym
->as
->type
= AS_EXPLICIT
;
6460 sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
6461 sym
->as
->upper
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, size
);
6464 sym
->value
->shape
= gfc_get_shape (1);
6465 mpz_init_set_ui (sym
->value
->shape
[0], size
);
6469 /* Add an derived type for a given module. */
6472 create_derived_type (const char *name
, const char *modname
,
6473 intmod_id module
, int id
)
6475 gfc_symtree
*tmp_symtree
;
6476 gfc_symbol
*sym
, *dt_sym
;
6477 gfc_interface
*intr
, *head
;
6479 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
6480 if (tmp_symtree
!= NULL
)
6482 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
6485 gfc_error ("Symbol %qs already declared", name
);
6488 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
6489 sym
= tmp_symtree
->n
.sym
;
6490 sym
->module
= gfc_get_string (modname
);
6491 sym
->from_intmod
= module
;
6492 sym
->intmod_sym_id
= id
;
6493 sym
->attr
.flavor
= FL_PROCEDURE
;
6494 sym
->attr
.function
= 1;
6495 sym
->attr
.generic
= 1;
6497 gfc_get_sym_tree (dt_upper_string (sym
->name
),
6498 gfc_current_ns
, &tmp_symtree
, false);
6499 dt_sym
= tmp_symtree
->n
.sym
;
6500 dt_sym
->name
= gfc_get_string (sym
->name
);
6501 dt_sym
->attr
.flavor
= FL_DERIVED
;
6502 dt_sym
->attr
.private_comp
= 1;
6503 dt_sym
->attr
.zero_comp
= 1;
6504 dt_sym
->attr
.use_assoc
= 1;
6505 dt_sym
->module
= gfc_get_string (modname
);
6506 dt_sym
->from_intmod
= module
;
6507 dt_sym
->intmod_sym_id
= id
;
6509 head
= sym
->generic
;
6510 intr
= gfc_get_interface ();
6512 intr
->where
= gfc_current_locus
;
6514 sym
->generic
= intr
;
6515 sym
->attr
.if_source
= IFSRC_DECL
;
6519 /* Read the contents of the module file into a temporary buffer. */
6522 read_module_to_tmpbuf ()
6524 /* We don't know the uncompressed size, so enlarge the buffer as
6530 module_content
= XNEWVEC (char, cursz
);
6534 int nread
= gzread (module_fp
, module_content
+ len
, rsize
);
6539 module_content
= XRESIZEVEC (char, module_content
, cursz
);
6540 rsize
= cursz
- len
;
6543 module_content
= XRESIZEVEC (char, module_content
, len
+ 1);
6544 module_content
[len
] = '\0';
6550 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6553 use_iso_fortran_env_module (void)
6555 static char mod
[] = "iso_fortran_env";
6557 gfc_symbol
*mod_sym
;
6558 gfc_symtree
*mod_symtree
;
6562 intmod_sym symbol
[] = {
6563 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6564 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6565 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6566 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6567 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6568 #include "iso-fortran-env.def"
6569 { ISOFORTRANENV_INVALID
, NULL
, -1234, 0 } };
6572 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6573 #include "iso-fortran-env.def"
6575 /* Generate the symbol for the module itself. */
6576 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, mod
);
6577 if (mod_symtree
== NULL
)
6579 gfc_get_sym_tree (mod
, gfc_current_ns
, &mod_symtree
, false);
6580 gcc_assert (mod_symtree
);
6581 mod_sym
= mod_symtree
->n
.sym
;
6583 mod_sym
->attr
.flavor
= FL_MODULE
;
6584 mod_sym
->attr
.intrinsic
= 1;
6585 mod_sym
->module
= gfc_get_string (mod
);
6586 mod_sym
->from_intmod
= INTMOD_ISO_FORTRAN_ENV
;
6589 if (!mod_symtree
->n
.sym
->attr
.intrinsic
)
6590 gfc_error ("Use of intrinsic module %qs at %C conflicts with "
6591 "non-intrinsic module name used previously", mod
);
6593 /* Generate the symbols for the module integer named constants. */
6595 for (i
= 0; symbol
[i
].name
; i
++)
6598 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6600 if (strcmp (symbol
[i
].name
, u
->use_name
) == 0)
6605 if (!gfc_notify_std (symbol
[i
].standard
, "The symbol %qs, "
6606 "referenced at %L, is not in the selected "
6607 "standard", symbol
[i
].name
, &u
->where
))
6610 if ((flag_default_integer
|| flag_default_real
)
6611 && symbol
[i
].id
== ISOFORTRANENV_NUMERIC_STORAGE_SIZE
)
6612 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6613 "constant from intrinsic module "
6614 "ISO_FORTRAN_ENV at %L is incompatible with "
6615 "option %qs", &u
->where
,
6616 flag_default_integer
6617 ? "-fdefault-integer-8"
6618 : "-fdefault-real-8");
6619 switch (symbol
[i
].id
)
6621 #define NAMED_INTCST(a,b,c,d) \
6623 #include "iso-fortran-env.def"
6624 create_int_parameter (u
->local_name
[0] ? u
->local_name
6626 symbol
[i
].value
, mod
,
6627 INTMOD_ISO_FORTRAN_ENV
, symbol
[i
].id
);
6630 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6632 expr = gfc_get_array_expr (BT_INTEGER, \
6633 gfc_default_integer_kind,\
6635 for (j = 0; KINDS[j].kind != 0; j++) \
6636 gfc_constructor_append_expr (&expr->value.constructor, \
6637 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6638 KINDS[j].kind), NULL); \
6639 create_int_parameter_array (u->local_name[0] ? u->local_name \
6642 INTMOD_ISO_FORTRAN_ENV, \
6645 #include "iso-fortran-env.def"
6647 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6649 #include "iso-fortran-env.def"
6650 create_derived_type (u
->local_name
[0] ? u
->local_name
6652 mod
, INTMOD_ISO_FORTRAN_ENV
,
6656 #define NAMED_FUNCTION(a,b,c,d) \
6658 #include "iso-fortran-env.def"
6659 create_intrinsic_function (u
->local_name
[0] ? u
->local_name
6662 INTMOD_ISO_FORTRAN_ENV
, false,
6672 if (!found
&& !only_flag
)
6674 if ((gfc_option
.allow_std
& symbol
[i
].standard
) == 0)
6677 if ((flag_default_integer
|| flag_default_real
)
6678 && symbol
[i
].id
== ISOFORTRANENV_NUMERIC_STORAGE_SIZE
)
6679 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6680 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6681 "incompatible with option %s",
6682 flag_default_integer
6683 ? "-fdefault-integer-8" : "-fdefault-real-8");
6685 switch (symbol
[i
].id
)
6687 #define NAMED_INTCST(a,b,c,d) \
6689 #include "iso-fortran-env.def"
6690 create_int_parameter (symbol
[i
].name
, symbol
[i
].value
, mod
,
6691 INTMOD_ISO_FORTRAN_ENV
, symbol
[i
].id
);
6694 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6696 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6698 for (j = 0; KINDS[j].kind != 0; j++) \
6699 gfc_constructor_append_expr (&expr->value.constructor, \
6700 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6701 KINDS[j].kind), NULL); \
6702 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6703 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6705 #include "iso-fortran-env.def"
6707 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6709 #include "iso-fortran-env.def"
6710 create_derived_type (symbol
[i
].name
, mod
, INTMOD_ISO_FORTRAN_ENV
,
6714 #define NAMED_FUNCTION(a,b,c,d) \
6716 #include "iso-fortran-env.def"
6717 create_intrinsic_function (symbol
[i
].name
, symbol
[i
].id
, mod
,
6718 INTMOD_ISO_FORTRAN_ENV
, false,
6728 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6733 gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
6734 "module ISO_FORTRAN_ENV", u
->use_name
, &u
->where
);
6739 /* Process a USE directive. */
6742 gfc_use_module (gfc_use_list
*module
)
6747 gfc_symtree
*mod_symtree
;
6748 gfc_use_list
*use_stmt
;
6749 locus old_locus
= gfc_current_locus
;
6751 gfc_current_locus
= module
->where
;
6752 module_name
= module
->module_name
;
6753 gfc_rename_list
= module
->rename
;
6754 only_flag
= module
->only_flag
;
6755 current_intmod
= INTMOD_NONE
;
6758 gfc_warning_now (OPT_Wuse_without_only
,
6759 "USE statement at %C has no ONLY qualifier");
6761 filename
= XALLOCAVEC (char, strlen (module_name
) + strlen (MODULE_EXTENSION
)
6763 strcpy (filename
, module_name
);
6764 strcat (filename
, MODULE_EXTENSION
);
6766 /* First, try to find an non-intrinsic module, unless the USE statement
6767 specified that the module is intrinsic. */
6769 if (!module
->intrinsic
)
6770 module_fp
= gzopen_included_file (filename
, true, true);
6772 /* Then, see if it's an intrinsic one, unless the USE statement
6773 specified that the module is non-intrinsic. */
6774 if (module_fp
== NULL
&& !module
->non_intrinsic
)
6776 if (strcmp (module_name
, "iso_fortran_env") == 0
6777 && gfc_notify_std (GFC_STD_F2003
, "ISO_FORTRAN_ENV "
6778 "intrinsic module at %C"))
6780 use_iso_fortran_env_module ();
6781 free_rename (module
->rename
);
6782 module
->rename
= NULL
;
6783 gfc_current_locus
= old_locus
;
6784 module
->intrinsic
= true;
6788 if (strcmp (module_name
, "iso_c_binding") == 0
6789 && gfc_notify_std (GFC_STD_F2003
, "ISO_C_BINDING module at %C"))
6791 import_iso_c_binding_module();
6792 free_rename (module
->rename
);
6793 module
->rename
= NULL
;
6794 gfc_current_locus
= old_locus
;
6795 module
->intrinsic
= true;
6799 module_fp
= gzopen_intrinsic_module (filename
);
6801 if (module_fp
== NULL
&& module
->intrinsic
)
6802 gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
6805 /* Check for the IEEE modules, so we can mark their symbols
6806 accordingly when we read them. */
6807 if (strcmp (module_name
, "ieee_features") == 0
6808 && gfc_notify_std (GFC_STD_F2003
, "IEEE_FEATURES module at %C"))
6810 current_intmod
= INTMOD_IEEE_FEATURES
;
6812 else if (strcmp (module_name
, "ieee_exceptions") == 0
6813 && gfc_notify_std (GFC_STD_F2003
,
6814 "IEEE_EXCEPTIONS module at %C"))
6816 current_intmod
= INTMOD_IEEE_EXCEPTIONS
;
6818 else if (strcmp (module_name
, "ieee_arithmetic") == 0
6819 && gfc_notify_std (GFC_STD_F2003
,
6820 "IEEE_ARITHMETIC module at %C"))
6822 current_intmod
= INTMOD_IEEE_ARITHMETIC
;
6826 if (module_fp
== NULL
)
6827 gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
6828 filename
, xstrerror (errno
));
6830 /* Check that we haven't already USEd an intrinsic module with the
6833 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, module_name
);
6834 if (mod_symtree
&& mod_symtree
->n
.sym
->attr
.intrinsic
)
6835 gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
6836 "intrinsic module name used previously", module_name
);
6843 read_module_to_tmpbuf ();
6844 gzclose (module_fp
);
6846 /* Skip the first line of the module, after checking that this is
6847 a gfortran module file. */
6853 bad_module ("Unexpected end of module");
6856 if ((start
== 1 && strcmp (atom_name
, "GFORTRAN") != 0)
6857 || (start
== 2 && strcmp (atom_name
, " module") != 0))
6858 gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
6859 " module file", filename
);
6862 if (strcmp (atom_name
, " version") != 0
6863 || module_char () != ' '
6864 || parse_atom () != ATOM_STRING
6865 || strcmp (atom_string
, MOD_VERSION
))
6866 gfc_fatal_error ("Cannot read module file %qs opened at %C,"
6867 " because it was created by a different"
6868 " version of GNU Fortran", filename
);
6877 /* Make sure we're not reading the same module that we may be building. */
6878 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
6879 if (p
->state
== COMP_MODULE
&& strcmp (p
->sym
->name
, module_name
) == 0)
6880 gfc_fatal_error ("Can't USE the same module we're building!");
6883 init_true_name_tree ();
6887 free_true_name (true_name_root
);
6888 true_name_root
= NULL
;
6890 free_pi_tree (pi_root
);
6893 XDELETEVEC (module_content
);
6894 module_content
= NULL
;
6896 use_stmt
= gfc_get_use_list ();
6897 *use_stmt
= *module
;
6898 use_stmt
->next
= gfc_current_ns
->use_stmts
;
6899 gfc_current_ns
->use_stmts
= use_stmt
;
6901 gfc_current_locus
= old_locus
;
6905 /* Remove duplicated intrinsic operators from the rename list. */
6908 rename_list_remove_duplicate (gfc_use_rename
*list
)
6910 gfc_use_rename
*seek
, *last
;
6912 for (; list
; list
= list
->next
)
6913 if (list
->op
!= INTRINSIC_USER
&& list
->op
!= INTRINSIC_NONE
)
6916 for (seek
= list
->next
; seek
; seek
= last
->next
)
6918 if (list
->op
== seek
->op
)
6920 last
->next
= seek
->next
;
6930 /* Process all USE directives. */
6933 gfc_use_modules (void)
6935 gfc_use_list
*next
, *seek
, *last
;
6937 for (next
= module_list
; next
; next
= next
->next
)
6939 bool non_intrinsic
= next
->non_intrinsic
;
6940 bool intrinsic
= next
->intrinsic
;
6941 bool neither
= !non_intrinsic
&& !intrinsic
;
6943 for (seek
= next
->next
; seek
; seek
= seek
->next
)
6945 if (next
->module_name
!= seek
->module_name
)
6948 if (seek
->non_intrinsic
)
6949 non_intrinsic
= true;
6950 else if (seek
->intrinsic
)
6956 if (intrinsic
&& neither
&& !non_intrinsic
)
6961 filename
= XALLOCAVEC (char,
6962 strlen (next
->module_name
)
6963 + strlen (MODULE_EXTENSION
) + 1);
6964 strcpy (filename
, next
->module_name
);
6965 strcat (filename
, MODULE_EXTENSION
);
6966 fp
= gfc_open_included_file (filename
, true, true);
6969 non_intrinsic
= true;
6975 for (seek
= next
->next
; seek
; seek
= last
->next
)
6977 if (next
->module_name
!= seek
->module_name
)
6983 if ((!next
->intrinsic
&& !seek
->intrinsic
)
6984 || (next
->intrinsic
&& seek
->intrinsic
)
6987 if (!seek
->only_flag
)
6988 next
->only_flag
= false;
6991 gfc_use_rename
*r
= seek
->rename
;
6994 r
->next
= next
->rename
;
6995 next
->rename
= seek
->rename
;
6997 last
->next
= seek
->next
;
7005 for (; module_list
; module_list
= next
)
7007 next
= module_list
->next
;
7008 rename_list_remove_duplicate (module_list
->rename
);
7009 gfc_use_module (module_list
);
7012 gfc_rename_list
= NULL
;
7017 gfc_free_use_stmts (gfc_use_list
*use_stmts
)
7020 for (; use_stmts
; use_stmts
= next
)
7022 gfc_use_rename
*next_rename
;
7024 for (; use_stmts
->rename
; use_stmts
->rename
= next_rename
)
7026 next_rename
= use_stmts
->rename
->next
;
7027 free (use_stmts
->rename
);
7029 next
= use_stmts
->next
;
7036 gfc_module_init_2 (void)
7038 last_atom
= ATOM_LPAREN
;
7039 gfc_rename_list
= NULL
;
7045 gfc_module_done_2 (void)
7047 free_rename (gfc_rename_list
);
7048 gfc_rename_list
= NULL
;