1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
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"
80 #define MODULE_EXTENSION ".mod"
82 /* Don't put any single quote (') in MOD_VERSION, if you want it to be
84 #define MOD_VERSION "11"
87 /* Structure that describes a position within a module file. */
96 /* Structure for list of symbols of intrinsic modules. */
109 P_UNKNOWN
= 0, P_OTHER
, P_NAMESPACE
, P_COMPONENT
, P_SYMBOL
113 /* The fixup structure lists pointers to pointers that have to
114 be updated when a pointer value becomes known. */
116 typedef struct fixup_t
119 struct fixup_t
*next
;
124 /* Structure for holding extra info needed for pointers being read. */
140 typedef struct pointer_info
142 BBT_HEADER (pointer_info
);
146 /* The first component of each member of the union is the pointer
153 void *pointer
; /* Member for doing pointer searches. */
158 char *true_name
, *module
, *binding_label
;
160 gfc_symtree
*symtree
;
161 enum gfc_rsym_state state
;
162 int ns
, referenced
, renamed
;
170 enum gfc_wsym_state state
;
179 #define gfc_get_pointer_info() XCNEW (pointer_info)
182 /* Local variables */
184 /* The gzFile for the module we're reading or writing. */
185 static gzFile module_fp
;
188 /* The name of the module we're reading (USE'ing) or writing. */
189 static const char *module_name
;
190 static gfc_use_list
*module_list
;
192 /* Content of module. */
193 static char* module_content
;
195 static long module_pos
;
196 static int module_line
, module_column
, only_flag
;
197 static int prev_module_line
, prev_module_column
;
200 { IO_INPUT
, IO_OUTPUT
}
203 static gfc_use_rename
*gfc_rename_list
;
204 static pointer_info
*pi_root
;
205 static int symbol_number
; /* Counter for assigning symbol numbers */
207 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
208 static bool in_load_equiv
;
212 /*****************************************************************/
214 /* Pointer/integer conversion. Pointers between structures are stored
215 as integers in the module file. The next couple of subroutines
216 handle this translation for reading and writing. */
218 /* Recursively free the tree of pointer structures. */
221 free_pi_tree (pointer_info
*p
)
226 if (p
->fixup
!= NULL
)
227 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
229 free_pi_tree (p
->left
);
230 free_pi_tree (p
->right
);
232 if (iomode
== IO_INPUT
)
234 XDELETEVEC (p
->u
.rsym
.true_name
);
235 XDELETEVEC (p
->u
.rsym
.module
);
236 XDELETEVEC (p
->u
.rsym
.binding_label
);
243 /* Compare pointers when searching by pointer. Used when writing a
247 compare_pointers (void *_sn1
, void *_sn2
)
249 pointer_info
*sn1
, *sn2
;
251 sn1
= (pointer_info
*) _sn1
;
252 sn2
= (pointer_info
*) _sn2
;
254 if (sn1
->u
.pointer
< sn2
->u
.pointer
)
256 if (sn1
->u
.pointer
> sn2
->u
.pointer
)
263 /* Compare integers when searching by integer. Used when reading a
267 compare_integers (void *_sn1
, void *_sn2
)
269 pointer_info
*sn1
, *sn2
;
271 sn1
= (pointer_info
*) _sn1
;
272 sn2
= (pointer_info
*) _sn2
;
274 if (sn1
->integer
< sn2
->integer
)
276 if (sn1
->integer
> sn2
->integer
)
283 /* Initialize the pointer_info tree. */
292 compare
= (iomode
== IO_INPUT
) ? compare_integers
: compare_pointers
;
294 /* Pointer 0 is the NULL pointer. */
295 p
= gfc_get_pointer_info ();
300 gfc_insert_bbt (&pi_root
, p
, compare
);
302 /* Pointer 1 is the current namespace. */
303 p
= gfc_get_pointer_info ();
304 p
->u
.pointer
= gfc_current_ns
;
306 p
->type
= P_NAMESPACE
;
308 gfc_insert_bbt (&pi_root
, p
, compare
);
314 /* During module writing, call here with a pointer to something,
315 returning the pointer_info node. */
317 static pointer_info
*
318 find_pointer (void *gp
)
325 if (p
->u
.pointer
== gp
)
327 p
= (gp
< p
->u
.pointer
) ? p
->left
: p
->right
;
334 /* Given a pointer while writing, returns the pointer_info tree node,
335 creating it if it doesn't exist. */
337 static pointer_info
*
338 get_pointer (void *gp
)
342 p
= find_pointer (gp
);
346 /* Pointer doesn't have an integer. Give it one. */
347 p
= gfc_get_pointer_info ();
350 p
->integer
= symbol_number
++;
352 gfc_insert_bbt (&pi_root
, p
, compare_pointers
);
358 /* Given an integer during reading, find it in the pointer_info tree,
359 creating the node if not found. */
361 static pointer_info
*
362 get_integer (int integer
)
372 c
= compare_integers (&t
, p
);
376 p
= (c
< 0) ? p
->left
: p
->right
;
382 p
= gfc_get_pointer_info ();
383 p
->integer
= integer
;
386 gfc_insert_bbt (&pi_root
, p
, compare_integers
);
392 /* Recursive function to find a pointer within a tree by brute force. */
394 static pointer_info
*
395 fp2 (pointer_info
*p
, const void *target
)
402 if (p
->u
.pointer
== target
)
405 q
= fp2 (p
->left
, target
);
409 return fp2 (p
->right
, target
);
413 /* During reading, find a pointer_info node from the pointer value.
414 This amounts to a brute-force search. */
416 static pointer_info
*
417 find_pointer2 (void *p
)
419 return fp2 (pi_root
, p
);
423 /* Resolve any fixups using a known pointer. */
426 resolve_fixups (fixup_t
*f
, void *gp
)
439 /* Convert a string such that it starts with a lower-case character. Used
440 to convert the symtree name of a derived-type to the symbol name or to
441 the name of the associated generic function. */
444 dt_lower_string (const char *name
)
446 if (name
[0] != (char) TOLOWER ((unsigned char) name
[0]))
447 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name
[0]),
449 return gfc_get_string (name
);
453 /* Convert a string such that it starts with an upper-case character. Used to
454 return the symtree-name for a derived type; the symbol name itself and the
455 symtree/symbol name of the associated generic function start with a lower-
459 dt_upper_string (const char *name
)
461 if (name
[0] != (char) TOUPPER ((unsigned char) name
[0]))
462 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name
[0]),
464 return gfc_get_string (name
);
467 /* Call here during module reading when we know what pointer to
468 associate with an integer. Any fixups that exist are resolved at
472 associate_integer_pointer (pointer_info
*p
, void *gp
)
474 if (p
->u
.pointer
!= NULL
)
475 gfc_internal_error ("associate_integer_pointer(): Already associated");
479 resolve_fixups (p
->fixup
, gp
);
485 /* During module reading, given an integer and a pointer to a pointer,
486 either store the pointer from an already-known value or create a
487 fixup structure in order to store things later. Returns zero if
488 the reference has been actually stored, or nonzero if the reference
489 must be fixed later (i.e., associate_integer_pointer must be called
490 sometime later. Returns the pointer_info structure. */
492 static pointer_info
*
493 add_fixup (int integer
, void *gp
)
499 p
= get_integer (integer
);
501 if (p
->integer
== 0 || p
->u
.pointer
!= NULL
)
504 *cp
= (char *) p
->u
.pointer
;
513 f
->pointer
= (void **) gp
;
520 /*****************************************************************/
522 /* Parser related subroutines */
524 /* Free the rename list left behind by a USE statement. */
527 free_rename (gfc_use_rename
*list
)
529 gfc_use_rename
*next
;
531 for (; list
; list
= next
)
539 /* Match a USE statement. */
544 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module_nature
[GFC_MAX_SYMBOL_LEN
+ 1];
545 gfc_use_rename
*tail
= NULL
, *new_use
;
546 interface_type type
, type2
;
549 gfc_use_list
*use_list
;
551 use_list
= gfc_get_use_list ();
553 if (gfc_match (" , ") == MATCH_YES
)
555 if ((m
= gfc_match (" %n ::", module_nature
)) == MATCH_YES
)
557 if (!gfc_notify_std (GFC_STD_F2003
, "module "
558 "nature in USE statement at %C"))
561 if (strcmp (module_nature
, "intrinsic") == 0)
562 use_list
->intrinsic
= true;
565 if (strcmp (module_nature
, "non_intrinsic") == 0)
566 use_list
->non_intrinsic
= true;
569 gfc_error ("Module nature in USE statement at %C shall "
570 "be either INTRINSIC or NON_INTRINSIC");
577 /* Help output a better error message than "Unclassifiable
579 gfc_match (" %n", module_nature
);
580 if (strcmp (module_nature
, "intrinsic") == 0
581 || strcmp (module_nature
, "non_intrinsic") == 0)
582 gfc_error ("\"::\" was expected after module nature at %C "
583 "but was not found");
590 m
= gfc_match (" ::");
591 if (m
== MATCH_YES
&&
592 !gfc_notify_std(GFC_STD_F2003
, "\"USE :: module\" at %C"))
597 m
= gfc_match ("% ");
606 use_list
->where
= gfc_current_locus
;
608 m
= gfc_match_name (name
);
615 use_list
->module_name
= gfc_get_string (name
);
617 if (gfc_match_eos () == MATCH_YES
)
620 if (gfc_match_char (',') != MATCH_YES
)
623 if (gfc_match (" only :") == MATCH_YES
)
624 use_list
->only_flag
= true;
626 if (gfc_match_eos () == MATCH_YES
)
631 /* Get a new rename struct and add it to the rename list. */
632 new_use
= gfc_get_use_rename ();
633 new_use
->where
= gfc_current_locus
;
636 if (use_list
->rename
== NULL
)
637 use_list
->rename
= new_use
;
639 tail
->next
= new_use
;
642 /* See what kind of interface we're dealing with. Assume it is
644 new_use
->op
= INTRINSIC_NONE
;
645 if (gfc_match_generic_spec (&type
, name
, &op
) == MATCH_ERROR
)
650 case INTERFACE_NAMELESS
:
651 gfc_error ("Missing generic specification in USE statement at %C");
654 case INTERFACE_USER_OP
:
655 case INTERFACE_GENERIC
:
656 m
= gfc_match (" =>");
658 if (type
== INTERFACE_USER_OP
&& m
== MATCH_YES
659 && (!gfc_notify_std(GFC_STD_F2003
, "Renaming "
660 "operators in USE statements at %C")))
663 if (type
== INTERFACE_USER_OP
)
664 new_use
->op
= INTRINSIC_USER
;
666 if (use_list
->only_flag
)
669 strcpy (new_use
->use_name
, name
);
672 strcpy (new_use
->local_name
, name
);
673 m
= gfc_match_generic_spec (&type2
, new_use
->use_name
, &op
);
678 if (m
== MATCH_ERROR
)
686 strcpy (new_use
->local_name
, name
);
688 m
= gfc_match_generic_spec (&type2
, new_use
->use_name
, &op
);
693 if (m
== MATCH_ERROR
)
697 if (strcmp (new_use
->use_name
, use_list
->module_name
) == 0
698 || strcmp (new_use
->local_name
, use_list
->module_name
) == 0)
700 gfc_error ("The name '%s' at %C has already been used as "
701 "an external module name.", use_list
->module_name
);
706 case INTERFACE_INTRINSIC_OP
:
714 if (gfc_match_eos () == MATCH_YES
)
716 if (gfc_match_char (',') != MATCH_YES
)
723 gfc_use_list
*last
= module_list
;
726 last
->next
= use_list
;
729 module_list
= use_list
;
734 gfc_syntax_error (ST_USE
);
737 free_rename (use_list
->rename
);
743 /* Given a name and a number, inst, return the inst name
744 under which to load this symbol. Returns NULL if this
745 symbol shouldn't be loaded. If inst is zero, returns
746 the number of instances of this name. If interface is
747 true, a user-defined operator is sought, otherwise only
748 non-operators are sought. */
751 find_use_name_n (const char *name
, int *inst
, bool interface
)
754 const char *low_name
= NULL
;
757 /* For derived types. */
758 if (name
[0] != (char) TOLOWER ((unsigned char) name
[0]))
759 low_name
= dt_lower_string (name
);
762 for (u
= gfc_rename_list
; u
; u
= u
->next
)
764 if ((!low_name
&& strcmp (u
->use_name
, name
) != 0)
765 || (low_name
&& strcmp (u
->use_name
, low_name
) != 0)
766 || (u
->op
== INTRINSIC_USER
&& !interface
)
767 || (u
->op
!= INTRINSIC_USER
&& interface
))
780 return only_flag
? NULL
: name
;
786 if (u
->local_name
[0] == '\0')
788 return dt_upper_string (u
->local_name
);
791 return (u
->local_name
[0] != '\0') ? u
->local_name
: name
;
795 /* Given a name, return the name under which to load this symbol.
796 Returns NULL if this symbol shouldn't be loaded. */
799 find_use_name (const char *name
, bool interface
)
802 return find_use_name_n (name
, &i
, interface
);
806 /* Given a real name, return the number of use names associated with it. */
809 number_use_names (const char *name
, bool interface
)
812 find_use_name_n (name
, &i
, interface
);
817 /* Try to find the operator in the current list. */
819 static gfc_use_rename
*
820 find_use_operator (gfc_intrinsic_op op
)
824 for (u
= gfc_rename_list
; u
; u
= u
->next
)
832 /*****************************************************************/
834 /* The next couple of subroutines maintain a tree used to avoid a
835 brute-force search for a combination of true name and module name.
836 While symtree names, the name that a particular symbol is known by
837 can changed with USE statements, we still have to keep track of the
838 true names to generate the correct reference, and also avoid
839 loading the same real symbol twice in a program unit.
841 When we start reading, the true name tree is built and maintained
842 as symbols are read. The tree is searched as we load new symbols
843 to see if it already exists someplace in the namespace. */
845 typedef struct true_name
847 BBT_HEADER (true_name
);
853 static true_name
*true_name_root
;
856 /* Compare two true_name structures. */
859 compare_true_names (void *_t1
, void *_t2
)
864 t1
= (true_name
*) _t1
;
865 t2
= (true_name
*) _t2
;
867 c
= ((t1
->sym
->module
> t2
->sym
->module
)
868 - (t1
->sym
->module
< t2
->sym
->module
));
872 return strcmp (t1
->name
, t2
->name
);
876 /* Given a true name, search the true name tree to see if it exists
877 within the main namespace. */
880 find_true_name (const char *name
, const char *module
)
886 t
.name
= gfc_get_string (name
);
888 sym
.module
= gfc_get_string (module
);
896 c
= compare_true_names ((void *) (&t
), (void *) p
);
900 p
= (c
< 0) ? p
->left
: p
->right
;
907 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
910 add_true_name (gfc_symbol
*sym
)
914 t
= XCNEW (true_name
);
916 if (sym
->attr
.flavor
== FL_DERIVED
)
917 t
->name
= dt_upper_string (sym
->name
);
921 gfc_insert_bbt (&true_name_root
, t
, compare_true_names
);
925 /* Recursive function to build the initial true name tree by
926 recursively traversing the current namespace. */
929 build_tnt (gfc_symtree
*st
)
935 build_tnt (st
->left
);
936 build_tnt (st
->right
);
938 if (st
->n
.sym
->attr
.flavor
== FL_DERIVED
)
939 name
= dt_upper_string (st
->n
.sym
->name
);
941 name
= st
->n
.sym
->name
;
943 if (find_true_name (name
, st
->n
.sym
->module
) != NULL
)
946 add_true_name (st
->n
.sym
);
950 /* Initialize the true name tree with the current namespace. */
953 init_true_name_tree (void)
955 true_name_root
= NULL
;
956 build_tnt (gfc_current_ns
->sym_root
);
960 /* Recursively free a true name tree node. */
963 free_true_name (true_name
*t
)
967 free_true_name (t
->left
);
968 free_true_name (t
->right
);
974 /*****************************************************************/
976 /* Module reading and writing. */
978 /* The following are versions similar to the ones in scanner.c, but
979 for dealing with compressed module files. */
982 gzopen_included_file_1 (const char *name
, gfc_directorylist
*list
,
983 bool module
, bool system
)
986 gfc_directorylist
*p
;
989 for (p
= list
; p
; p
= p
->next
)
991 if (module
&& !p
->use_for_modules
)
994 fullname
= (char *) alloca(strlen (p
->path
) + strlen (name
) + 1);
995 strcpy (fullname
, p
->path
);
996 strcat (fullname
, name
);
998 f
= gzopen (fullname
, "r");
1001 if (gfc_cpp_makedep ())
1002 gfc_cpp_add_dep (fullname
, system
);
1012 gzopen_included_file (const char *name
, bool include_cwd
, bool module
)
1016 if (IS_ABSOLUTE_PATH (name
) || include_cwd
)
1018 f
= gzopen (name
, "r");
1019 if (f
&& gfc_cpp_makedep ())
1020 gfc_cpp_add_dep (name
, false);
1024 f
= gzopen_included_file_1 (name
, include_dirs
, module
, false);
1030 gzopen_intrinsic_module (const char* name
)
1034 if (IS_ABSOLUTE_PATH (name
))
1036 f
= gzopen (name
, "r");
1037 if (f
&& gfc_cpp_makedep ())
1038 gfc_cpp_add_dep (name
, true);
1042 f
= gzopen_included_file_1 (name
, intrinsic_modules_dirs
, true, true);
1050 ATOM_NAME
, ATOM_LPAREN
, ATOM_RPAREN
, ATOM_INTEGER
, ATOM_STRING
1054 static atom_type last_atom
;
1057 /* The name buffer must be at least as long as a symbol name. Right
1058 now it's not clear how we're going to store numeric constants--
1059 probably as a hexadecimal string, since this will allow the exact
1060 number to be preserved (this can't be done by a decimal
1061 representation). Worry about that later. TODO! */
1063 #define MAX_ATOM_SIZE 100
1065 static int atom_int
;
1066 static char *atom_string
, atom_name
[MAX_ATOM_SIZE
];
1069 /* Report problems with a module. Error reporting is not very
1070 elaborate, since this sorts of errors shouldn't really happen.
1071 This subroutine never returns. */
1073 static void bad_module (const char *) ATTRIBUTE_NORETURN
;
1076 bad_module (const char *msgid
)
1078 XDELETEVEC (module_content
);
1079 module_content
= NULL
;
1084 gfc_fatal_error ("Reading module %s at line %d column %d: %s",
1085 module_name
, module_line
, module_column
, msgid
);
1088 gfc_fatal_error ("Writing module %s at line %d column %d: %s",
1089 module_name
, module_line
, module_column
, msgid
);
1092 gfc_fatal_error ("Module %s at line %d column %d: %s",
1093 module_name
, module_line
, module_column
, msgid
);
1099 /* Set the module's input pointer. */
1102 set_module_locus (module_locus
*m
)
1104 module_column
= m
->column
;
1105 module_line
= m
->line
;
1106 module_pos
= m
->pos
;
1110 /* Get the module's input pointer so that we can restore it later. */
1113 get_module_locus (module_locus
*m
)
1115 m
->column
= module_column
;
1116 m
->line
= module_line
;
1117 m
->pos
= module_pos
;
1121 /* Get the next character in the module, updating our reckoning of
1127 const char c
= module_content
[module_pos
++];
1129 bad_module ("Unexpected EOF");
1131 prev_module_line
= module_line
;
1132 prev_module_column
= module_column
;
1144 /* Unget a character while remembering the line and column. Works for
1145 a single character only. */
1148 module_unget_char (void)
1150 module_line
= prev_module_line
;
1151 module_column
= prev_module_column
;
1155 /* Parse a string constant. The delimiter is guaranteed to be a
1165 atom_string
= XNEWVEC (char, cursz
);
1173 int c2
= module_char ();
1176 module_unget_char ();
1184 atom_string
= XRESIZEVEC (char, atom_string
, cursz
);
1186 atom_string
[len
] = c
;
1190 atom_string
= XRESIZEVEC (char, atom_string
, len
+ 1);
1191 atom_string
[len
] = '\0'; /* C-style string for debug purposes. */
1195 /* Parse a small integer. */
1198 parse_integer (int c
)
1207 module_unget_char ();
1211 atom_int
= 10 * atom_int
+ c
- '0';
1212 if (atom_int
> 99999999)
1213 bad_module ("Integer overflow");
1235 if (!ISALNUM (c
) && c
!= '_' && c
!= '-')
1237 module_unget_char ();
1242 if (++len
> GFC_MAX_SYMBOL_LEN
)
1243 bad_module ("Name too long");
1251 /* Read the next atom in the module's input stream. */
1262 while (c
== ' ' || c
== '\r' || c
== '\n');
1287 return ATOM_INTEGER
;
1345 bad_module ("Bad name");
1352 /* Peek at the next atom on the input. */
1363 while (c
== ' ' || c
== '\r' || c
== '\n');
1368 module_unget_char ();
1372 module_unget_char ();
1376 module_unget_char ();
1389 module_unget_char ();
1390 return ATOM_INTEGER
;
1444 module_unget_char ();
1448 bad_module ("Bad name");
1453 /* Read the next atom from the input, requiring that it be a
1457 require_atom (atom_type type
)
1463 column
= module_column
;
1472 p
= _("Expected name");
1475 p
= _("Expected left parenthesis");
1478 p
= _("Expected right parenthesis");
1481 p
= _("Expected integer");
1484 p
= _("Expected string");
1487 gfc_internal_error ("require_atom(): bad atom type required");
1490 module_column
= column
;
1497 /* Given a pointer to an mstring array, require that the current input
1498 be one of the strings in the array. We return the enum value. */
1501 find_enum (const mstring
*m
)
1505 i
= gfc_string2code (m
, atom_name
);
1509 bad_module ("find_enum(): Enum not found");
1515 /* Read a string. The caller is responsible for freeing. */
1521 require_atom (ATOM_STRING
);
1528 /**************** Module output subroutines ***************************/
1530 /* Output a character to a module file. */
1533 write_char (char out
)
1535 if (gzputc (module_fp
, out
) == EOF
)
1536 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno
));
1548 /* Write an atom to a module. The line wrapping isn't perfect, but it
1549 should work most of the time. This isn't that big of a deal, since
1550 the file really isn't meant to be read by people anyway. */
1553 write_atom (atom_type atom
, const void *v
)
1563 p
= (const char *) v
;
1575 i
= *((const int *) v
);
1577 gfc_internal_error ("write_atom(): Writing negative integer");
1579 sprintf (buffer
, "%d", i
);
1584 gfc_internal_error ("write_atom(): Trying to write dab atom");
1588 if(p
== NULL
|| *p
== '\0')
1593 if (atom
!= ATOM_RPAREN
)
1595 if (module_column
+ len
> 72)
1600 if (last_atom
!= ATOM_LPAREN
&& module_column
!= 1)
1605 if (atom
== ATOM_STRING
)
1608 while (p
!= NULL
&& *p
)
1610 if (atom
== ATOM_STRING
&& *p
== '\'')
1615 if (atom
== ATOM_STRING
)
1623 /***************** Mid-level I/O subroutines *****************/
1625 /* These subroutines let their caller read or write atoms without
1626 caring about which of the two is actually happening. This lets a
1627 subroutine concentrate on the actual format of the data being
1630 static void mio_expr (gfc_expr
**);
1631 pointer_info
*mio_symbol_ref (gfc_symbol
**);
1632 pointer_info
*mio_interface_rest (gfc_interface
**);
1633 static void mio_symtree_ref (gfc_symtree
**);
1635 /* Read or write an enumerated value. On writing, we return the input
1636 value for the convenience of callers. We avoid using an integer
1637 pointer because enums are sometimes inside bitfields. */
1640 mio_name (int t
, const mstring
*m
)
1642 if (iomode
== IO_OUTPUT
)
1643 write_atom (ATOM_NAME
, gfc_code2string (m
, t
));
1646 require_atom (ATOM_NAME
);
1653 /* Specialization of mio_name. */
1655 #define DECL_MIO_NAME(TYPE) \
1656 static inline TYPE \
1657 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1659 return (TYPE) mio_name ((int) t, m); \
1661 #define MIO_NAME(TYPE) mio_name_##TYPE
1666 if (iomode
== IO_OUTPUT
)
1667 write_atom (ATOM_LPAREN
, NULL
);
1669 require_atom (ATOM_LPAREN
);
1676 if (iomode
== IO_OUTPUT
)
1677 write_atom (ATOM_RPAREN
, NULL
);
1679 require_atom (ATOM_RPAREN
);
1684 mio_integer (int *ip
)
1686 if (iomode
== IO_OUTPUT
)
1687 write_atom (ATOM_INTEGER
, ip
);
1690 require_atom (ATOM_INTEGER
);
1696 /* Read or write a gfc_intrinsic_op value. */
1699 mio_intrinsic_op (gfc_intrinsic_op
* op
)
1701 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1702 if (iomode
== IO_OUTPUT
)
1704 int converted
= (int) *op
;
1705 write_atom (ATOM_INTEGER
, &converted
);
1709 require_atom (ATOM_INTEGER
);
1710 *op
= (gfc_intrinsic_op
) atom_int
;
1715 /* Read or write a character pointer that points to a string on the heap. */
1718 mio_allocated_string (const char *s
)
1720 if (iomode
== IO_OUTPUT
)
1722 write_atom (ATOM_STRING
, s
);
1727 require_atom (ATOM_STRING
);
1733 /* Functions for quoting and unquoting strings. */
1736 quote_string (const gfc_char_t
*s
, const size_t slength
)
1738 const gfc_char_t
*p
;
1742 /* Calculate the length we'll need: a backslash takes two ("\\"),
1743 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1744 for (p
= s
, i
= 0; i
< slength
; p
++, i
++)
1748 else if (!gfc_wide_is_printable (*p
))
1754 q
= res
= XCNEWVEC (char, len
+ 1);
1755 for (p
= s
, i
= 0; i
< slength
; p
++, i
++)
1758 *q
++ = '\\', *q
++ = '\\';
1759 else if (!gfc_wide_is_printable (*p
))
1761 sprintf (q
, "\\U%08" HOST_WIDE_INT_PRINT
"x",
1762 (unsigned HOST_WIDE_INT
) *p
);
1766 *q
++ = (unsigned char) *p
;
1774 unquote_string (const char *s
)
1780 for (p
= s
, len
= 0; *p
; p
++, len
++)
1787 else if (p
[1] == 'U')
1788 p
+= 9; /* That is a "\U????????". */
1790 gfc_internal_error ("unquote_string(): got bad string");
1793 res
= gfc_get_wide_string (len
+ 1);
1794 for (i
= 0, p
= s
; i
< len
; i
++, p
++)
1799 res
[i
] = (unsigned char) *p
;
1800 else if (p
[1] == '\\')
1802 res
[i
] = (unsigned char) '\\';
1807 /* We read the 8-digits hexadecimal constant that follows. */
1812 gcc_assert (p
[1] == 'U');
1813 for (j
= 0; j
< 8; j
++)
1816 gcc_assert (sscanf (&p
[j
+2], "%01x", &n
) == 1);
1830 /* Read or write a character pointer that points to a wide string on the
1831 heap, performing quoting/unquoting of nonprintable characters using the
1832 form \U???????? (where each ? is a hexadecimal digit).
1833 Length is the length of the string, only known and used in output mode. */
1835 static const gfc_char_t
*
1836 mio_allocated_wide_string (const gfc_char_t
*s
, const size_t length
)
1838 if (iomode
== IO_OUTPUT
)
1840 char *quoted
= quote_string (s
, length
);
1841 write_atom (ATOM_STRING
, quoted
);
1847 gfc_char_t
*unquoted
;
1849 require_atom (ATOM_STRING
);
1850 unquoted
= unquote_string (atom_string
);
1857 /* Read or write a string that is in static memory. */
1860 mio_pool_string (const char **stringp
)
1862 /* TODO: one could write the string only once, and refer to it via a
1865 /* As a special case we have to deal with a NULL string. This
1866 happens for the 'module' member of 'gfc_symbol's that are not in a
1867 module. We read / write these as the empty string. */
1868 if (iomode
== IO_OUTPUT
)
1870 const char *p
= *stringp
== NULL
? "" : *stringp
;
1871 write_atom (ATOM_STRING
, p
);
1875 require_atom (ATOM_STRING
);
1876 *stringp
= atom_string
[0] == '\0' ? NULL
: gfc_get_string (atom_string
);
1882 /* Read or write a string that is inside of some already-allocated
1886 mio_internal_string (char *string
)
1888 if (iomode
== IO_OUTPUT
)
1889 write_atom (ATOM_STRING
, string
);
1892 require_atom (ATOM_STRING
);
1893 strcpy (string
, atom_string
);
1900 { AB_ALLOCATABLE
, AB_DIMENSION
, AB_EXTERNAL
, AB_INTRINSIC
, AB_OPTIONAL
,
1901 AB_POINTER
, AB_TARGET
, AB_DUMMY
, AB_RESULT
, AB_DATA
,
1902 AB_IN_NAMELIST
, AB_IN_COMMON
, AB_FUNCTION
, AB_SUBROUTINE
, AB_SEQUENCE
,
1903 AB_ELEMENTAL
, AB_PURE
, AB_RECURSIVE
, AB_GENERIC
, AB_ALWAYS_EXPLICIT
,
1904 AB_CRAY_POINTER
, AB_CRAY_POINTEE
, AB_THREADPRIVATE
,
1905 AB_ALLOC_COMP
, AB_POINTER_COMP
, AB_PROC_POINTER_COMP
, AB_PRIVATE_COMP
,
1906 AB_VALUE
, AB_VOLATILE
, AB_PROTECTED
, AB_LOCK_COMP
,
1907 AB_IS_BIND_C
, AB_IS_C_INTEROP
, AB_IS_ISO_C
, AB_ABSTRACT
, AB_ZERO_COMP
,
1908 AB_IS_CLASS
, AB_PROCEDURE
, AB_PROC_POINTER
, AB_ASYNCHRONOUS
, AB_CODIMENSION
,
1909 AB_COARRAY_COMP
, AB_VTYPE
, AB_VTAB
, AB_CONTIGUOUS
, AB_CLASS_POINTER
,
1910 AB_IMPLICIT_PURE
, AB_ARTIFICIAL
, AB_UNLIMITED_POLY
1914 static const mstring attr_bits
[] =
1916 minit ("ALLOCATABLE", AB_ALLOCATABLE
),
1917 minit ("ARTIFICIAL", AB_ARTIFICIAL
),
1918 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS
),
1919 minit ("DIMENSION", AB_DIMENSION
),
1920 minit ("CODIMENSION", AB_CODIMENSION
),
1921 minit ("CONTIGUOUS", AB_CONTIGUOUS
),
1922 minit ("EXTERNAL", AB_EXTERNAL
),
1923 minit ("INTRINSIC", AB_INTRINSIC
),
1924 minit ("OPTIONAL", AB_OPTIONAL
),
1925 minit ("POINTER", AB_POINTER
),
1926 minit ("VOLATILE", AB_VOLATILE
),
1927 minit ("TARGET", AB_TARGET
),
1928 minit ("THREADPRIVATE", AB_THREADPRIVATE
),
1929 minit ("DUMMY", AB_DUMMY
),
1930 minit ("RESULT", AB_RESULT
),
1931 minit ("DATA", AB_DATA
),
1932 minit ("IN_NAMELIST", AB_IN_NAMELIST
),
1933 minit ("IN_COMMON", AB_IN_COMMON
),
1934 minit ("FUNCTION", AB_FUNCTION
),
1935 minit ("SUBROUTINE", AB_SUBROUTINE
),
1936 minit ("SEQUENCE", AB_SEQUENCE
),
1937 minit ("ELEMENTAL", AB_ELEMENTAL
),
1938 minit ("PURE", AB_PURE
),
1939 minit ("RECURSIVE", AB_RECURSIVE
),
1940 minit ("GENERIC", AB_GENERIC
),
1941 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT
),
1942 minit ("CRAY_POINTER", AB_CRAY_POINTER
),
1943 minit ("CRAY_POINTEE", AB_CRAY_POINTEE
),
1944 minit ("IS_BIND_C", AB_IS_BIND_C
),
1945 minit ("IS_C_INTEROP", AB_IS_C_INTEROP
),
1946 minit ("IS_ISO_C", AB_IS_ISO_C
),
1947 minit ("VALUE", AB_VALUE
),
1948 minit ("ALLOC_COMP", AB_ALLOC_COMP
),
1949 minit ("COARRAY_COMP", AB_COARRAY_COMP
),
1950 minit ("LOCK_COMP", AB_LOCK_COMP
),
1951 minit ("POINTER_COMP", AB_POINTER_COMP
),
1952 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP
),
1953 minit ("PRIVATE_COMP", AB_PRIVATE_COMP
),
1954 minit ("ZERO_COMP", AB_ZERO_COMP
),
1955 minit ("PROTECTED", AB_PROTECTED
),
1956 minit ("ABSTRACT", AB_ABSTRACT
),
1957 minit ("IS_CLASS", AB_IS_CLASS
),
1958 minit ("PROCEDURE", AB_PROCEDURE
),
1959 minit ("PROC_POINTER", AB_PROC_POINTER
),
1960 minit ("VTYPE", AB_VTYPE
),
1961 minit ("VTAB", AB_VTAB
),
1962 minit ("CLASS_POINTER", AB_CLASS_POINTER
),
1963 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE
),
1964 minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY
),
1968 /* For binding attributes. */
1969 static const mstring binding_passing
[] =
1972 minit ("NOPASS", 1),
1975 static const mstring binding_overriding
[] =
1977 minit ("OVERRIDABLE", 0),
1978 minit ("NON_OVERRIDABLE", 1),
1979 minit ("DEFERRED", 2),
1982 static const mstring binding_generic
[] =
1984 minit ("SPECIFIC", 0),
1985 minit ("GENERIC", 1),
1988 static const mstring binding_ppc
[] =
1990 minit ("NO_PPC", 0),
1995 /* Specialization of mio_name. */
1996 DECL_MIO_NAME (ab_attribute
)
1997 DECL_MIO_NAME (ar_type
)
1998 DECL_MIO_NAME (array_type
)
2000 DECL_MIO_NAME (expr_t
)
2001 DECL_MIO_NAME (gfc_access
)
2002 DECL_MIO_NAME (gfc_intrinsic_op
)
2003 DECL_MIO_NAME (ifsrc
)
2004 DECL_MIO_NAME (save_state
)
2005 DECL_MIO_NAME (procedure_type
)
2006 DECL_MIO_NAME (ref_type
)
2007 DECL_MIO_NAME (sym_flavor
)
2008 DECL_MIO_NAME (sym_intent
)
2009 #undef DECL_MIO_NAME
2011 /* Symbol attributes are stored in list with the first three elements
2012 being the enumerated fields, while the remaining elements (if any)
2013 indicate the individual attribute bits. The access field is not
2014 saved-- it controls what symbols are exported when a module is
2018 mio_symbol_attribute (symbol_attribute
*attr
)
2021 unsigned ext_attr
,extension_level
;
2025 attr
->flavor
= MIO_NAME (sym_flavor
) (attr
->flavor
, flavors
);
2026 attr
->intent
= MIO_NAME (sym_intent
) (attr
->intent
, intents
);
2027 attr
->proc
= MIO_NAME (procedure_type
) (attr
->proc
, procedures
);
2028 attr
->if_source
= MIO_NAME (ifsrc
) (attr
->if_source
, ifsrc_types
);
2029 attr
->save
= MIO_NAME (save_state
) (attr
->save
, save_status
);
2031 ext_attr
= attr
->ext_attr
;
2032 mio_integer ((int *) &ext_attr
);
2033 attr
->ext_attr
= ext_attr
;
2035 extension_level
= attr
->extension
;
2036 mio_integer ((int *) &extension_level
);
2037 attr
->extension
= extension_level
;
2039 if (iomode
== IO_OUTPUT
)
2041 if (attr
->allocatable
)
2042 MIO_NAME (ab_attribute
) (AB_ALLOCATABLE
, attr_bits
);
2043 if (attr
->artificial
)
2044 MIO_NAME (ab_attribute
) (AB_ARTIFICIAL
, attr_bits
);
2045 if (attr
->asynchronous
)
2046 MIO_NAME (ab_attribute
) (AB_ASYNCHRONOUS
, attr_bits
);
2047 if (attr
->dimension
)
2048 MIO_NAME (ab_attribute
) (AB_DIMENSION
, attr_bits
);
2049 if (attr
->codimension
)
2050 MIO_NAME (ab_attribute
) (AB_CODIMENSION
, attr_bits
);
2051 if (attr
->contiguous
)
2052 MIO_NAME (ab_attribute
) (AB_CONTIGUOUS
, attr_bits
);
2054 MIO_NAME (ab_attribute
) (AB_EXTERNAL
, attr_bits
);
2055 if (attr
->intrinsic
)
2056 MIO_NAME (ab_attribute
) (AB_INTRINSIC
, attr_bits
);
2058 MIO_NAME (ab_attribute
) (AB_OPTIONAL
, attr_bits
);
2060 MIO_NAME (ab_attribute
) (AB_POINTER
, attr_bits
);
2061 if (attr
->class_pointer
)
2062 MIO_NAME (ab_attribute
) (AB_CLASS_POINTER
, attr_bits
);
2063 if (attr
->is_protected
)
2064 MIO_NAME (ab_attribute
) (AB_PROTECTED
, attr_bits
);
2066 MIO_NAME (ab_attribute
) (AB_VALUE
, attr_bits
);
2067 if (attr
->volatile_
)
2068 MIO_NAME (ab_attribute
) (AB_VOLATILE
, attr_bits
);
2070 MIO_NAME (ab_attribute
) (AB_TARGET
, attr_bits
);
2071 if (attr
->threadprivate
)
2072 MIO_NAME (ab_attribute
) (AB_THREADPRIVATE
, attr_bits
);
2074 MIO_NAME (ab_attribute
) (AB_DUMMY
, attr_bits
);
2076 MIO_NAME (ab_attribute
) (AB_RESULT
, attr_bits
);
2077 /* We deliberately don't preserve the "entry" flag. */
2080 MIO_NAME (ab_attribute
) (AB_DATA
, attr_bits
);
2081 if (attr
->in_namelist
)
2082 MIO_NAME (ab_attribute
) (AB_IN_NAMELIST
, attr_bits
);
2083 if (attr
->in_common
)
2084 MIO_NAME (ab_attribute
) (AB_IN_COMMON
, attr_bits
);
2087 MIO_NAME (ab_attribute
) (AB_FUNCTION
, attr_bits
);
2088 if (attr
->subroutine
)
2089 MIO_NAME (ab_attribute
) (AB_SUBROUTINE
, attr_bits
);
2091 MIO_NAME (ab_attribute
) (AB_GENERIC
, attr_bits
);
2093 MIO_NAME (ab_attribute
) (AB_ABSTRACT
, attr_bits
);
2096 MIO_NAME (ab_attribute
) (AB_SEQUENCE
, attr_bits
);
2097 if (attr
->elemental
)
2098 MIO_NAME (ab_attribute
) (AB_ELEMENTAL
, attr_bits
);
2100 MIO_NAME (ab_attribute
) (AB_PURE
, attr_bits
);
2101 if (attr
->implicit_pure
)
2102 MIO_NAME (ab_attribute
) (AB_IMPLICIT_PURE
, attr_bits
);
2103 if (attr
->unlimited_polymorphic
)
2104 MIO_NAME (ab_attribute
) (AB_UNLIMITED_POLY
, attr_bits
);
2105 if (attr
->recursive
)
2106 MIO_NAME (ab_attribute
) (AB_RECURSIVE
, attr_bits
);
2107 if (attr
->always_explicit
)
2108 MIO_NAME (ab_attribute
) (AB_ALWAYS_EXPLICIT
, attr_bits
);
2109 if (attr
->cray_pointer
)
2110 MIO_NAME (ab_attribute
) (AB_CRAY_POINTER
, attr_bits
);
2111 if (attr
->cray_pointee
)
2112 MIO_NAME (ab_attribute
) (AB_CRAY_POINTEE
, attr_bits
);
2113 if (attr
->is_bind_c
)
2114 MIO_NAME(ab_attribute
) (AB_IS_BIND_C
, attr_bits
);
2115 if (attr
->is_c_interop
)
2116 MIO_NAME(ab_attribute
) (AB_IS_C_INTEROP
, attr_bits
);
2118 MIO_NAME(ab_attribute
) (AB_IS_ISO_C
, attr_bits
);
2119 if (attr
->alloc_comp
)
2120 MIO_NAME (ab_attribute
) (AB_ALLOC_COMP
, attr_bits
);
2121 if (attr
->pointer_comp
)
2122 MIO_NAME (ab_attribute
) (AB_POINTER_COMP
, attr_bits
);
2123 if (attr
->proc_pointer_comp
)
2124 MIO_NAME (ab_attribute
) (AB_PROC_POINTER_COMP
, attr_bits
);
2125 if (attr
->private_comp
)
2126 MIO_NAME (ab_attribute
) (AB_PRIVATE_COMP
, attr_bits
);
2127 if (attr
->coarray_comp
)
2128 MIO_NAME (ab_attribute
) (AB_COARRAY_COMP
, attr_bits
);
2129 if (attr
->lock_comp
)
2130 MIO_NAME (ab_attribute
) (AB_LOCK_COMP
, attr_bits
);
2131 if (attr
->zero_comp
)
2132 MIO_NAME (ab_attribute
) (AB_ZERO_COMP
, attr_bits
);
2134 MIO_NAME (ab_attribute
) (AB_IS_CLASS
, attr_bits
);
2135 if (attr
->procedure
)
2136 MIO_NAME (ab_attribute
) (AB_PROCEDURE
, attr_bits
);
2137 if (attr
->proc_pointer
)
2138 MIO_NAME (ab_attribute
) (AB_PROC_POINTER
, attr_bits
);
2140 MIO_NAME (ab_attribute
) (AB_VTYPE
, attr_bits
);
2142 MIO_NAME (ab_attribute
) (AB_VTAB
, attr_bits
);
2152 if (t
== ATOM_RPAREN
)
2155 bad_module ("Expected attribute bit name");
2157 switch ((ab_attribute
) find_enum (attr_bits
))
2159 case AB_ALLOCATABLE
:
2160 attr
->allocatable
= 1;
2163 attr
->artificial
= 1;
2165 case AB_ASYNCHRONOUS
:
2166 attr
->asynchronous
= 1;
2169 attr
->dimension
= 1;
2171 case AB_CODIMENSION
:
2172 attr
->codimension
= 1;
2175 attr
->contiguous
= 1;
2181 attr
->intrinsic
= 1;
2189 case AB_CLASS_POINTER
:
2190 attr
->class_pointer
= 1;
2193 attr
->is_protected
= 1;
2199 attr
->volatile_
= 1;
2204 case AB_THREADPRIVATE
:
2205 attr
->threadprivate
= 1;
2216 case AB_IN_NAMELIST
:
2217 attr
->in_namelist
= 1;
2220 attr
->in_common
= 1;
2226 attr
->subroutine
= 1;
2238 attr
->elemental
= 1;
2243 case AB_IMPLICIT_PURE
:
2244 attr
->implicit_pure
= 1;
2246 case AB_UNLIMITED_POLY
:
2247 attr
->unlimited_polymorphic
= 1;
2250 attr
->recursive
= 1;
2252 case AB_ALWAYS_EXPLICIT
:
2253 attr
->always_explicit
= 1;
2255 case AB_CRAY_POINTER
:
2256 attr
->cray_pointer
= 1;
2258 case AB_CRAY_POINTEE
:
2259 attr
->cray_pointee
= 1;
2262 attr
->is_bind_c
= 1;
2264 case AB_IS_C_INTEROP
:
2265 attr
->is_c_interop
= 1;
2271 attr
->alloc_comp
= 1;
2273 case AB_COARRAY_COMP
:
2274 attr
->coarray_comp
= 1;
2277 attr
->lock_comp
= 1;
2279 case AB_POINTER_COMP
:
2280 attr
->pointer_comp
= 1;
2282 case AB_PROC_POINTER_COMP
:
2283 attr
->proc_pointer_comp
= 1;
2285 case AB_PRIVATE_COMP
:
2286 attr
->private_comp
= 1;
2289 attr
->zero_comp
= 1;
2295 attr
->procedure
= 1;
2297 case AB_PROC_POINTER
:
2298 attr
->proc_pointer
= 1;
2312 static const mstring bt_types
[] = {
2313 minit ("INTEGER", BT_INTEGER
),
2314 minit ("REAL", BT_REAL
),
2315 minit ("COMPLEX", BT_COMPLEX
),
2316 minit ("LOGICAL", BT_LOGICAL
),
2317 minit ("CHARACTER", BT_CHARACTER
),
2318 minit ("DERIVED", BT_DERIVED
),
2319 minit ("CLASS", BT_CLASS
),
2320 minit ("PROCEDURE", BT_PROCEDURE
),
2321 minit ("UNKNOWN", BT_UNKNOWN
),
2322 minit ("VOID", BT_VOID
),
2323 minit ("ASSUMED", BT_ASSUMED
),
2329 mio_charlen (gfc_charlen
**clp
)
2335 if (iomode
== IO_OUTPUT
)
2339 mio_expr (&cl
->length
);
2343 if (peek_atom () != ATOM_RPAREN
)
2345 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2346 mio_expr (&cl
->length
);
2355 /* See if a name is a generated name. */
2358 check_unique_name (const char *name
)
2360 return *name
== '@';
2365 mio_typespec (gfc_typespec
*ts
)
2369 ts
->type
= MIO_NAME (bt
) (ts
->type
, bt_types
);
2371 if (ts
->type
!= BT_DERIVED
&& ts
->type
!= BT_CLASS
)
2372 mio_integer (&ts
->kind
);
2374 mio_symbol_ref (&ts
->u
.derived
);
2376 mio_symbol_ref (&ts
->interface
);
2378 /* Add info for C interop and is_iso_c. */
2379 mio_integer (&ts
->is_c_interop
);
2380 mio_integer (&ts
->is_iso_c
);
2382 /* If the typespec is for an identifier either from iso_c_binding, or
2383 a constant that was initialized to an identifier from it, use the
2384 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2386 ts
->f90_type
= MIO_NAME (bt
) (ts
->f90_type
, bt_types
);
2388 ts
->f90_type
= MIO_NAME (bt
) (ts
->type
, bt_types
);
2390 if (ts
->type
!= BT_CHARACTER
)
2392 /* ts->u.cl is only valid for BT_CHARACTER. */
2397 mio_charlen (&ts
->u
.cl
);
2399 /* So as not to disturb the existing API, use an ATOM_NAME to
2400 transmit deferred characteristic for characters (F2003). */
2401 if (iomode
== IO_OUTPUT
)
2403 if (ts
->type
== BT_CHARACTER
&& ts
->deferred
)
2404 write_atom (ATOM_NAME
, "DEFERRED_CL");
2406 else if (peek_atom () != ATOM_RPAREN
)
2408 if (parse_atom () != ATOM_NAME
)
2409 bad_module ("Expected string");
2417 static const mstring array_spec_types
[] = {
2418 minit ("EXPLICIT", AS_EXPLICIT
),
2419 minit ("ASSUMED_RANK", AS_ASSUMED_RANK
),
2420 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE
),
2421 minit ("DEFERRED", AS_DEFERRED
),
2422 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE
),
2428 mio_array_spec (gfc_array_spec
**asp
)
2435 if (iomode
== IO_OUTPUT
)
2443 /* mio_integer expects nonnegative values. */
2444 rank
= as
->rank
> 0 ? as
->rank
: 0;
2445 mio_integer (&rank
);
2449 if (peek_atom () == ATOM_RPAREN
)
2455 *asp
= as
= gfc_get_array_spec ();
2456 mio_integer (&as
->rank
);
2459 mio_integer (&as
->corank
);
2460 as
->type
= MIO_NAME (array_type
) (as
->type
, array_spec_types
);
2462 if (iomode
== IO_INPUT
&& as
->type
== AS_ASSUMED_RANK
)
2464 if (iomode
== IO_INPUT
&& as
->corank
)
2465 as
->cotype
= (as
->type
== AS_DEFERRED
) ? AS_DEFERRED
: AS_EXPLICIT
;
2467 if (as
->rank
+ as
->corank
> 0)
2468 for (i
= 0; i
< as
->rank
+ as
->corank
; i
++)
2470 mio_expr (&as
->lower
[i
]);
2471 mio_expr (&as
->upper
[i
]);
2479 /* Given a pointer to an array reference structure (which lives in a
2480 gfc_ref structure), find the corresponding array specification
2481 structure. Storing the pointer in the ref structure doesn't quite
2482 work when loading from a module. Generating code for an array
2483 reference also needs more information than just the array spec. */
2485 static const mstring array_ref_types
[] = {
2486 minit ("FULL", AR_FULL
),
2487 minit ("ELEMENT", AR_ELEMENT
),
2488 minit ("SECTION", AR_SECTION
),
2494 mio_array_ref (gfc_array_ref
*ar
)
2499 ar
->type
= MIO_NAME (ar_type
) (ar
->type
, array_ref_types
);
2500 mio_integer (&ar
->dimen
);
2508 for (i
= 0; i
< ar
->dimen
; i
++)
2509 mio_expr (&ar
->start
[i
]);
2514 for (i
= 0; i
< ar
->dimen
; i
++)
2516 mio_expr (&ar
->start
[i
]);
2517 mio_expr (&ar
->end
[i
]);
2518 mio_expr (&ar
->stride
[i
]);
2524 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2527 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2528 we can't call mio_integer directly. Instead loop over each element
2529 and cast it to/from an integer. */
2530 if (iomode
== IO_OUTPUT
)
2532 for (i
= 0; i
< ar
->dimen
; i
++)
2534 int tmp
= (int)ar
->dimen_type
[i
];
2535 write_atom (ATOM_INTEGER
, &tmp
);
2540 for (i
= 0; i
< ar
->dimen
; i
++)
2542 require_atom (ATOM_INTEGER
);
2543 ar
->dimen_type
[i
] = (enum gfc_array_ref_dimen_type
) atom_int
;
2547 if (iomode
== IO_INPUT
)
2549 ar
->where
= gfc_current_locus
;
2551 for (i
= 0; i
< ar
->dimen
; i
++)
2552 ar
->c_where
[i
] = gfc_current_locus
;
2559 /* Saves or restores a pointer. The pointer is converted back and
2560 forth from an integer. We return the pointer_info pointer so that
2561 the caller can take additional action based on the pointer type. */
2563 static pointer_info
*
2564 mio_pointer_ref (void *gp
)
2568 if (iomode
== IO_OUTPUT
)
2570 p
= get_pointer (*((char **) gp
));
2571 write_atom (ATOM_INTEGER
, &p
->integer
);
2575 require_atom (ATOM_INTEGER
);
2576 p
= add_fixup (atom_int
, gp
);
2583 /* Save and load references to components that occur within
2584 expressions. We have to describe these references by a number and
2585 by name. The number is necessary for forward references during
2586 reading, and the name is necessary if the symbol already exists in
2587 the namespace and is not loaded again. */
2590 mio_component_ref (gfc_component
**cp
, gfc_symbol
*sym
)
2592 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2596 p
= mio_pointer_ref (cp
);
2597 if (p
->type
== P_UNKNOWN
)
2598 p
->type
= P_COMPONENT
;
2600 if (iomode
== IO_OUTPUT
)
2601 mio_pool_string (&(*cp
)->name
);
2604 mio_internal_string (name
);
2606 if (sym
&& sym
->attr
.is_class
)
2607 sym
= sym
->components
->ts
.u
.derived
;
2609 /* It can happen that a component reference can be read before the
2610 associated derived type symbol has been loaded. Return now and
2611 wait for a later iteration of load_needed. */
2615 if (sym
->components
!= NULL
&& p
->u
.pointer
== NULL
)
2617 /* Symbol already loaded, so search by name. */
2618 q
= gfc_find_component (sym
, name
, true, true);
2621 associate_integer_pointer (p
, q
);
2624 /* Make sure this symbol will eventually be loaded. */
2625 p
= find_pointer2 (sym
);
2626 if (p
->u
.rsym
.state
== UNUSED
)
2627 p
->u
.rsym
.state
= NEEDED
;
2632 static void mio_namespace_ref (gfc_namespace
**nsp
);
2633 static void mio_formal_arglist (gfc_formal_arglist
**formal
);
2634 static void mio_typebound_proc (gfc_typebound_proc
** proc
);
2637 mio_component (gfc_component
*c
, int vtype
)
2644 if (iomode
== IO_OUTPUT
)
2646 p
= get_pointer (c
);
2647 mio_integer (&p
->integer
);
2652 p
= get_integer (n
);
2653 associate_integer_pointer (p
, c
);
2656 if (p
->type
== P_UNKNOWN
)
2657 p
->type
= P_COMPONENT
;
2659 mio_pool_string (&c
->name
);
2660 mio_typespec (&c
->ts
);
2661 mio_array_spec (&c
->as
);
2663 mio_symbol_attribute (&c
->attr
);
2664 if (c
->ts
.type
== BT_CLASS
)
2665 c
->attr
.class_ok
= 1;
2666 c
->attr
.access
= MIO_NAME (gfc_access
) (c
->attr
.access
, access_types
);
2668 if (!vtype
|| strcmp (c
->name
, "_final") == 0
2669 || strcmp (c
->name
, "_hash") == 0)
2670 mio_expr (&c
->initializer
);
2672 if (c
->attr
.proc_pointer
)
2673 mio_typebound_proc (&c
->tb
);
2680 mio_component_list (gfc_component
**cp
, int vtype
)
2682 gfc_component
*c
, *tail
;
2686 if (iomode
== IO_OUTPUT
)
2688 for (c
= *cp
; c
; c
= c
->next
)
2689 mio_component (c
, vtype
);
2698 if (peek_atom () == ATOM_RPAREN
)
2701 c
= gfc_get_component ();
2702 mio_component (c
, vtype
);
2718 mio_actual_arg (gfc_actual_arglist
*a
)
2721 mio_pool_string (&a
->name
);
2722 mio_expr (&a
->expr
);
2728 mio_actual_arglist (gfc_actual_arglist
**ap
)
2730 gfc_actual_arglist
*a
, *tail
;
2734 if (iomode
== IO_OUTPUT
)
2736 for (a
= *ap
; a
; a
= a
->next
)
2746 if (peek_atom () != ATOM_LPAREN
)
2749 a
= gfc_get_actual_arglist ();
2765 /* Read and write formal argument lists. */
2768 mio_formal_arglist (gfc_formal_arglist
**formal
)
2770 gfc_formal_arglist
*f
, *tail
;
2774 if (iomode
== IO_OUTPUT
)
2776 for (f
= *formal
; f
; f
= f
->next
)
2777 mio_symbol_ref (&f
->sym
);
2781 *formal
= tail
= NULL
;
2783 while (peek_atom () != ATOM_RPAREN
)
2785 f
= gfc_get_formal_arglist ();
2786 mio_symbol_ref (&f
->sym
);
2788 if (*formal
== NULL
)
2801 /* Save or restore a reference to a symbol node. */
2804 mio_symbol_ref (gfc_symbol
**symp
)
2808 p
= mio_pointer_ref (symp
);
2809 if (p
->type
== P_UNKNOWN
)
2812 if (iomode
== IO_OUTPUT
)
2814 if (p
->u
.wsym
.state
== UNREFERENCED
)
2815 p
->u
.wsym
.state
= NEEDS_WRITE
;
2819 if (p
->u
.rsym
.state
== UNUSED
)
2820 p
->u
.rsym
.state
= NEEDED
;
2826 /* Save or restore a reference to a symtree node. */
2829 mio_symtree_ref (gfc_symtree
**stp
)
2834 if (iomode
== IO_OUTPUT
)
2835 mio_symbol_ref (&(*stp
)->n
.sym
);
2838 require_atom (ATOM_INTEGER
);
2839 p
= get_integer (atom_int
);
2841 /* An unused equivalence member; make a symbol and a symtree
2843 if (in_load_equiv
&& p
->u
.rsym
.symtree
== NULL
)
2845 /* Since this is not used, it must have a unique name. */
2846 p
->u
.rsym
.symtree
= gfc_get_unique_symtree (gfc_current_ns
);
2848 /* Make the symbol. */
2849 if (p
->u
.rsym
.sym
== NULL
)
2851 p
->u
.rsym
.sym
= gfc_new_symbol (p
->u
.rsym
.true_name
,
2853 p
->u
.rsym
.sym
->module
= gfc_get_string (p
->u
.rsym
.module
);
2856 p
->u
.rsym
.symtree
->n
.sym
= p
->u
.rsym
.sym
;
2857 p
->u
.rsym
.symtree
->n
.sym
->refs
++;
2858 p
->u
.rsym
.referenced
= 1;
2860 /* If the symbol is PRIVATE and in COMMON, load_commons will
2861 generate a fixup symbol, which must be associated. */
2863 resolve_fixups (p
->fixup
, p
->u
.rsym
.sym
);
2867 if (p
->type
== P_UNKNOWN
)
2870 if (p
->u
.rsym
.state
== UNUSED
)
2871 p
->u
.rsym
.state
= NEEDED
;
2873 if (p
->u
.rsym
.symtree
!= NULL
)
2875 *stp
= p
->u
.rsym
.symtree
;
2879 f
= XCNEW (fixup_t
);
2881 f
->next
= p
->u
.rsym
.stfixup
;
2882 p
->u
.rsym
.stfixup
= f
;
2884 f
->pointer
= (void **) stp
;
2891 mio_iterator (gfc_iterator
**ip
)
2897 if (iomode
== IO_OUTPUT
)
2904 if (peek_atom () == ATOM_RPAREN
)
2910 *ip
= gfc_get_iterator ();
2915 mio_expr (&iter
->var
);
2916 mio_expr (&iter
->start
);
2917 mio_expr (&iter
->end
);
2918 mio_expr (&iter
->step
);
2926 mio_constructor (gfc_constructor_base
*cp
)
2932 if (iomode
== IO_OUTPUT
)
2934 for (c
= gfc_constructor_first (*cp
); c
; c
= gfc_constructor_next (c
))
2937 mio_expr (&c
->expr
);
2938 mio_iterator (&c
->iterator
);
2944 while (peek_atom () != ATOM_RPAREN
)
2946 c
= gfc_constructor_append_expr (cp
, NULL
, NULL
);
2949 mio_expr (&c
->expr
);
2950 mio_iterator (&c
->iterator
);
2959 static const mstring ref_types
[] = {
2960 minit ("ARRAY", REF_ARRAY
),
2961 minit ("COMPONENT", REF_COMPONENT
),
2962 minit ("SUBSTRING", REF_SUBSTRING
),
2968 mio_ref (gfc_ref
**rp
)
2975 r
->type
= MIO_NAME (ref_type
) (r
->type
, ref_types
);
2980 mio_array_ref (&r
->u
.ar
);
2984 mio_symbol_ref (&r
->u
.c
.sym
);
2985 mio_component_ref (&r
->u
.c
.component
, r
->u
.c
.sym
);
2989 mio_expr (&r
->u
.ss
.start
);
2990 mio_expr (&r
->u
.ss
.end
);
2991 mio_charlen (&r
->u
.ss
.length
);
3000 mio_ref_list (gfc_ref
**rp
)
3002 gfc_ref
*ref
, *head
, *tail
;
3006 if (iomode
== IO_OUTPUT
)
3008 for (ref
= *rp
; ref
; ref
= ref
->next
)
3015 while (peek_atom () != ATOM_RPAREN
)
3018 head
= tail
= gfc_get_ref ();
3021 tail
->next
= gfc_get_ref ();
3035 /* Read and write an integer value. */
3038 mio_gmp_integer (mpz_t
*integer
)
3042 if (iomode
== IO_INPUT
)
3044 if (parse_atom () != ATOM_STRING
)
3045 bad_module ("Expected integer string");
3047 mpz_init (*integer
);
3048 if (mpz_set_str (*integer
, atom_string
, 10))
3049 bad_module ("Error converting integer");
3055 p
= mpz_get_str (NULL
, 10, *integer
);
3056 write_atom (ATOM_STRING
, p
);
3063 mio_gmp_real (mpfr_t
*real
)
3068 if (iomode
== IO_INPUT
)
3070 if (parse_atom () != ATOM_STRING
)
3071 bad_module ("Expected real string");
3074 mpfr_set_str (*real
, atom_string
, 16, GFC_RND_MODE
);
3079 p
= mpfr_get_str (NULL
, &exponent
, 16, 0, *real
, GFC_RND_MODE
);
3081 if (mpfr_nan_p (*real
) || mpfr_inf_p (*real
))
3083 write_atom (ATOM_STRING
, p
);
3088 atom_string
= XCNEWVEC (char, strlen (p
) + 20);
3090 sprintf (atom_string
, "0.%s@%ld", p
, exponent
);
3092 /* Fix negative numbers. */
3093 if (atom_string
[2] == '-')
3095 atom_string
[0] = '-';
3096 atom_string
[1] = '0';
3097 atom_string
[2] = '.';
3100 write_atom (ATOM_STRING
, atom_string
);
3108 /* Save and restore the shape of an array constructor. */
3111 mio_shape (mpz_t
**pshape
, int rank
)
3117 /* A NULL shape is represented by (). */
3120 if (iomode
== IO_OUTPUT
)
3132 if (t
== ATOM_RPAREN
)
3139 shape
= gfc_get_shape (rank
);
3143 for (n
= 0; n
< rank
; n
++)
3144 mio_gmp_integer (&shape
[n
]);
3150 static const mstring expr_types
[] = {
3151 minit ("OP", EXPR_OP
),
3152 minit ("FUNCTION", EXPR_FUNCTION
),
3153 minit ("CONSTANT", EXPR_CONSTANT
),
3154 minit ("VARIABLE", EXPR_VARIABLE
),
3155 minit ("SUBSTRING", EXPR_SUBSTRING
),
3156 minit ("STRUCTURE", EXPR_STRUCTURE
),
3157 minit ("ARRAY", EXPR_ARRAY
),
3158 minit ("NULL", EXPR_NULL
),
3159 minit ("COMPCALL", EXPR_COMPCALL
),
3163 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3164 generic operators, not in expressions. INTRINSIC_USER is also
3165 replaced by the correct function name by the time we see it. */
3167 static const mstring intrinsics
[] =
3169 minit ("UPLUS", INTRINSIC_UPLUS
),
3170 minit ("UMINUS", INTRINSIC_UMINUS
),
3171 minit ("PLUS", INTRINSIC_PLUS
),
3172 minit ("MINUS", INTRINSIC_MINUS
),
3173 minit ("TIMES", INTRINSIC_TIMES
),
3174 minit ("DIVIDE", INTRINSIC_DIVIDE
),
3175 minit ("POWER", INTRINSIC_POWER
),
3176 minit ("CONCAT", INTRINSIC_CONCAT
),
3177 minit ("AND", INTRINSIC_AND
),
3178 minit ("OR", INTRINSIC_OR
),
3179 minit ("EQV", INTRINSIC_EQV
),
3180 minit ("NEQV", INTRINSIC_NEQV
),
3181 minit ("EQ_SIGN", INTRINSIC_EQ
),
3182 minit ("EQ", INTRINSIC_EQ_OS
),
3183 minit ("NE_SIGN", INTRINSIC_NE
),
3184 minit ("NE", INTRINSIC_NE_OS
),
3185 minit ("GT_SIGN", INTRINSIC_GT
),
3186 minit ("GT", INTRINSIC_GT_OS
),
3187 minit ("GE_SIGN", INTRINSIC_GE
),
3188 minit ("GE", INTRINSIC_GE_OS
),
3189 minit ("LT_SIGN", INTRINSIC_LT
),
3190 minit ("LT", INTRINSIC_LT_OS
),
3191 minit ("LE_SIGN", INTRINSIC_LE
),
3192 minit ("LE", INTRINSIC_LE_OS
),
3193 minit ("NOT", INTRINSIC_NOT
),
3194 minit ("PARENTHESES", INTRINSIC_PARENTHESES
),
3199 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3202 fix_mio_expr (gfc_expr
*e
)
3204 gfc_symtree
*ns_st
= NULL
;
3207 if (iomode
!= IO_OUTPUT
)
3212 /* If this is a symtree for a symbol that came from a contained module
3213 namespace, it has a unique name and we should look in the current
3214 namespace to see if the required, non-contained symbol is available
3215 yet. If so, the latter should be written. */
3216 if (e
->symtree
->n
.sym
&& check_unique_name (e
->symtree
->name
))
3218 const char *name
= e
->symtree
->n
.sym
->name
;
3219 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_DERIVED
)
3220 name
= dt_upper_string (name
);
3221 ns_st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
3224 /* On the other hand, if the existing symbol is the module name or the
3225 new symbol is a dummy argument, do not do the promotion. */
3226 if (ns_st
&& ns_st
->n
.sym
3227 && ns_st
->n
.sym
->attr
.flavor
!= FL_MODULE
3228 && !e
->symtree
->n
.sym
->attr
.dummy
)
3231 else if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.name
)
3235 /* In some circumstances, a function used in an initialization
3236 expression, in one use associated module, can fail to be
3237 coupled to its symtree when used in a specification
3238 expression in another module. */
3239 fname
= e
->value
.function
.esym
? e
->value
.function
.esym
->name
3240 : e
->value
.function
.isym
->name
;
3241 e
->symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, fname
);
3246 /* This is probably a reference to a private procedure from another
3247 module. To prevent a segfault, make a generic with no specific
3248 instances. If this module is used, without the required
3249 specific coming from somewhere, the appropriate error message
3251 gfc_get_symbol (fname
, gfc_current_ns
, &sym
);
3252 sym
->attr
.flavor
= FL_PROCEDURE
;
3253 sym
->attr
.generic
= 1;
3254 e
->symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, fname
);
3255 gfc_commit_symbol (sym
);
3260 /* Read and write expressions. The form "()" is allowed to indicate a
3264 mio_expr (gfc_expr
**ep
)
3272 if (iomode
== IO_OUTPUT
)
3281 MIO_NAME (expr_t
) (e
->expr_type
, expr_types
);
3286 if (t
== ATOM_RPAREN
)
3293 bad_module ("Expected expression type");
3295 e
= *ep
= gfc_get_expr ();
3296 e
->where
= gfc_current_locus
;
3297 e
->expr_type
= (expr_t
) find_enum (expr_types
);
3300 mio_typespec (&e
->ts
);
3301 mio_integer (&e
->rank
);
3305 switch (e
->expr_type
)
3309 = MIO_NAME (gfc_intrinsic_op
) (e
->value
.op
.op
, intrinsics
);
3311 switch (e
->value
.op
.op
)
3313 case INTRINSIC_UPLUS
:
3314 case INTRINSIC_UMINUS
:
3316 case INTRINSIC_PARENTHESES
:
3317 mio_expr (&e
->value
.op
.op1
);
3320 case INTRINSIC_PLUS
:
3321 case INTRINSIC_MINUS
:
3322 case INTRINSIC_TIMES
:
3323 case INTRINSIC_DIVIDE
:
3324 case INTRINSIC_POWER
:
3325 case INTRINSIC_CONCAT
:
3329 case INTRINSIC_NEQV
:
3331 case INTRINSIC_EQ_OS
:
3333 case INTRINSIC_NE_OS
:
3335 case INTRINSIC_GT_OS
:
3337 case INTRINSIC_GE_OS
:
3339 case INTRINSIC_LT_OS
:
3341 case INTRINSIC_LE_OS
:
3342 mio_expr (&e
->value
.op
.op1
);
3343 mio_expr (&e
->value
.op
.op2
);
3347 bad_module ("Bad operator");
3353 mio_symtree_ref (&e
->symtree
);
3354 mio_actual_arglist (&e
->value
.function
.actual
);
3356 if (iomode
== IO_OUTPUT
)
3358 e
->value
.function
.name
3359 = mio_allocated_string (e
->value
.function
.name
);
3360 flag
= e
->value
.function
.esym
!= NULL
;
3361 mio_integer (&flag
);
3363 mio_symbol_ref (&e
->value
.function
.esym
);
3365 write_atom (ATOM_STRING
, e
->value
.function
.isym
->name
);
3369 require_atom (ATOM_STRING
);
3370 e
->value
.function
.name
= gfc_get_string (atom_string
);
3373 mio_integer (&flag
);
3375 mio_symbol_ref (&e
->value
.function
.esym
);
3378 require_atom (ATOM_STRING
);
3379 e
->value
.function
.isym
= gfc_find_function (atom_string
);
3387 mio_symtree_ref (&e
->symtree
);
3388 mio_ref_list (&e
->ref
);
3391 case EXPR_SUBSTRING
:
3392 e
->value
.character
.string
3393 = CONST_CAST (gfc_char_t
*,
3394 mio_allocated_wide_string (e
->value
.character
.string
,
3395 e
->value
.character
.length
));
3396 mio_ref_list (&e
->ref
);
3399 case EXPR_STRUCTURE
:
3401 mio_constructor (&e
->value
.constructor
);
3402 mio_shape (&e
->shape
, e
->rank
);
3409 mio_gmp_integer (&e
->value
.integer
);
3413 gfc_set_model_kind (e
->ts
.kind
);
3414 mio_gmp_real (&e
->value
.real
);
3418 gfc_set_model_kind (e
->ts
.kind
);
3419 mio_gmp_real (&mpc_realref (e
->value
.complex));
3420 mio_gmp_real (&mpc_imagref (e
->value
.complex));
3424 mio_integer (&e
->value
.logical
);
3428 mio_integer (&e
->value
.character
.length
);
3429 e
->value
.character
.string
3430 = CONST_CAST (gfc_char_t
*,
3431 mio_allocated_wide_string (e
->value
.character
.string
,
3432 e
->value
.character
.length
));
3436 bad_module ("Bad type in constant expression");
3454 /* Read and write namelists. */
3457 mio_namelist (gfc_symbol
*sym
)
3459 gfc_namelist
*n
, *m
;
3460 const char *check_name
;
3464 if (iomode
== IO_OUTPUT
)
3466 for (n
= sym
->namelist
; n
; n
= n
->next
)
3467 mio_symbol_ref (&n
->sym
);
3471 /* This departure from the standard is flagged as an error.
3472 It does, in fact, work correctly. TODO: Allow it
3474 if (sym
->attr
.flavor
== FL_NAMELIST
)
3476 check_name
= find_use_name (sym
->name
, false);
3477 if (check_name
&& strcmp (check_name
, sym
->name
) != 0)
3478 gfc_error ("Namelist %s cannot be renamed by USE "
3479 "association to %s", sym
->name
, check_name
);
3483 while (peek_atom () != ATOM_RPAREN
)
3485 n
= gfc_get_namelist ();
3486 mio_symbol_ref (&n
->sym
);
3488 if (sym
->namelist
== NULL
)
3495 sym
->namelist_tail
= m
;
3502 /* Save/restore lists of gfc_interface structures. When loading an
3503 interface, we are really appending to the existing list of
3504 interfaces. Checking for duplicate and ambiguous interfaces has to
3505 be done later when all symbols have been loaded. */
3508 mio_interface_rest (gfc_interface
**ip
)
3510 gfc_interface
*tail
, *p
;
3511 pointer_info
*pi
= NULL
;
3513 if (iomode
== IO_OUTPUT
)
3516 for (p
= *ip
; p
; p
= p
->next
)
3517 mio_symbol_ref (&p
->sym
);
3532 if (peek_atom () == ATOM_RPAREN
)
3535 p
= gfc_get_interface ();
3536 p
->where
= gfc_current_locus
;
3537 pi
= mio_symbol_ref (&p
->sym
);
3553 /* Save/restore a nameless operator interface. */
3556 mio_interface (gfc_interface
**ip
)
3559 mio_interface_rest (ip
);
3563 /* Save/restore a named operator interface. */
3566 mio_symbol_interface (const char **name
, const char **module
,
3570 mio_pool_string (name
);
3571 mio_pool_string (module
);
3572 mio_interface_rest (ip
);
3577 mio_namespace_ref (gfc_namespace
**nsp
)
3582 p
= mio_pointer_ref (nsp
);
3584 if (p
->type
== P_UNKNOWN
)
3585 p
->type
= P_NAMESPACE
;
3587 if (iomode
== IO_INPUT
&& p
->integer
!= 0)
3589 ns
= (gfc_namespace
*) p
->u
.pointer
;
3592 ns
= gfc_get_namespace (NULL
, 0);
3593 associate_integer_pointer (p
, ns
);
3601 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3603 static gfc_namespace
* current_f2k_derived
;
3606 mio_typebound_proc (gfc_typebound_proc
** proc
)
3609 int overriding_flag
;
3611 if (iomode
== IO_INPUT
)
3613 *proc
= gfc_get_typebound_proc (NULL
);
3614 (*proc
)->where
= gfc_current_locus
;
3620 (*proc
)->access
= MIO_NAME (gfc_access
) ((*proc
)->access
, access_types
);
3622 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3623 gcc_assert (!((*proc
)->deferred
&& (*proc
)->non_overridable
));
3624 overriding_flag
= ((*proc
)->deferred
<< 1) | (*proc
)->non_overridable
;
3625 overriding_flag
= mio_name (overriding_flag
, binding_overriding
);
3626 (*proc
)->deferred
= ((overriding_flag
& 2) != 0);
3627 (*proc
)->non_overridable
= ((overriding_flag
& 1) != 0);
3628 gcc_assert (!((*proc
)->deferred
&& (*proc
)->non_overridable
));
3630 (*proc
)->nopass
= mio_name ((*proc
)->nopass
, binding_passing
);
3631 (*proc
)->is_generic
= mio_name ((*proc
)->is_generic
, binding_generic
);
3632 (*proc
)->ppc
= mio_name((*proc
)->ppc
, binding_ppc
);
3634 mio_pool_string (&((*proc
)->pass_arg
));
3636 flag
= (int) (*proc
)->pass_arg_num
;
3637 mio_integer (&flag
);
3638 (*proc
)->pass_arg_num
= (unsigned) flag
;
3640 if ((*proc
)->is_generic
)
3647 if (iomode
== IO_OUTPUT
)
3648 for (g
= (*proc
)->u
.generic
; g
; g
= g
->next
)
3650 iop
= (int) g
->is_operator
;
3652 mio_allocated_string (g
->specific_st
->name
);
3656 (*proc
)->u
.generic
= NULL
;
3657 while (peek_atom () != ATOM_RPAREN
)
3659 gfc_symtree
** sym_root
;
3661 g
= gfc_get_tbp_generic ();
3665 g
->is_operator
= (bool) iop
;
3667 require_atom (ATOM_STRING
);
3668 sym_root
= ¤t_f2k_derived
->tb_sym_root
;
3669 g
->specific_st
= gfc_get_tbp_symtree (sym_root
, atom_string
);
3672 g
->next
= (*proc
)->u
.generic
;
3673 (*proc
)->u
.generic
= g
;
3679 else if (!(*proc
)->ppc
)
3680 mio_symtree_ref (&(*proc
)->u
.specific
);
3685 /* Walker-callback function for this purpose. */
3687 mio_typebound_symtree (gfc_symtree
* st
)
3689 if (iomode
== IO_OUTPUT
&& !st
->n
.tb
)
3692 if (iomode
== IO_OUTPUT
)
3695 mio_allocated_string (st
->name
);
3697 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3699 mio_typebound_proc (&st
->n
.tb
);
3703 /* IO a full symtree (in all depth). */
3705 mio_full_typebound_tree (gfc_symtree
** root
)
3709 if (iomode
== IO_OUTPUT
)
3710 gfc_traverse_symtree (*root
, &mio_typebound_symtree
);
3713 while (peek_atom () == ATOM_LPAREN
)
3719 require_atom (ATOM_STRING
);
3720 st
= gfc_get_tbp_symtree (root
, atom_string
);
3723 mio_typebound_symtree (st
);
3731 mio_finalizer (gfc_finalizer
**f
)
3733 if (iomode
== IO_OUTPUT
)
3736 gcc_assert ((*f
)->proc_tree
); /* Should already be resolved. */
3737 mio_symtree_ref (&(*f
)->proc_tree
);
3741 *f
= gfc_get_finalizer ();
3742 (*f
)->where
= gfc_current_locus
; /* Value should not matter. */
3745 mio_symtree_ref (&(*f
)->proc_tree
);
3746 (*f
)->proc_sym
= NULL
;
3751 mio_f2k_derived (gfc_namespace
*f2k
)
3753 current_f2k_derived
= f2k
;
3755 /* Handle the list of finalizer procedures. */
3757 if (iomode
== IO_OUTPUT
)
3760 for (f
= f2k
->finalizers
; f
; f
= f
->next
)
3765 f2k
->finalizers
= NULL
;
3766 while (peek_atom () != ATOM_RPAREN
)
3768 gfc_finalizer
*cur
= NULL
;
3769 mio_finalizer (&cur
);
3770 cur
->next
= f2k
->finalizers
;
3771 f2k
->finalizers
= cur
;
3776 /* Handle type-bound procedures. */
3777 mio_full_typebound_tree (&f2k
->tb_sym_root
);
3779 /* Type-bound user operators. */
3780 mio_full_typebound_tree (&f2k
->tb_uop_root
);
3782 /* Type-bound intrinsic operators. */
3784 if (iomode
== IO_OUTPUT
)
3787 for (op
= GFC_INTRINSIC_BEGIN
; op
!= GFC_INTRINSIC_END
; ++op
)
3789 gfc_intrinsic_op realop
;
3791 if (op
== INTRINSIC_USER
|| !f2k
->tb_op
[op
])
3795 realop
= (gfc_intrinsic_op
) op
;
3796 mio_intrinsic_op (&realop
);
3797 mio_typebound_proc (&f2k
->tb_op
[op
]);
3802 while (peek_atom () != ATOM_RPAREN
)
3804 gfc_intrinsic_op op
= GFC_INTRINSIC_BEGIN
; /* Silence GCC. */
3807 mio_intrinsic_op (&op
);
3808 mio_typebound_proc (&f2k
->tb_op
[op
]);
3815 mio_full_f2k_derived (gfc_symbol
*sym
)
3819 if (iomode
== IO_OUTPUT
)
3821 if (sym
->f2k_derived
)
3822 mio_f2k_derived (sym
->f2k_derived
);
3826 if (peek_atom () != ATOM_RPAREN
)
3828 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
3829 mio_f2k_derived (sym
->f2k_derived
);
3832 gcc_assert (!sym
->f2k_derived
);
3839 /* Unlike most other routines, the address of the symbol node is already
3840 fixed on input and the name/module has already been filled in. */
3843 mio_symbol (gfc_symbol
*sym
)
3845 int intmod
= INTMOD_NONE
;
3849 mio_symbol_attribute (&sym
->attr
);
3850 mio_typespec (&sym
->ts
);
3851 if (sym
->ts
.type
== BT_CLASS
)
3852 sym
->attr
.class_ok
= 1;
3854 if (iomode
== IO_OUTPUT
)
3855 mio_namespace_ref (&sym
->formal_ns
);
3858 mio_namespace_ref (&sym
->formal_ns
);
3860 sym
->formal_ns
->proc_name
= sym
;
3863 /* Save/restore common block links. */
3864 mio_symbol_ref (&sym
->common_next
);
3866 mio_formal_arglist (&sym
->formal
);
3868 if (sym
->attr
.flavor
== FL_PARAMETER
)
3869 mio_expr (&sym
->value
);
3871 mio_array_spec (&sym
->as
);
3873 mio_symbol_ref (&sym
->result
);
3875 if (sym
->attr
.cray_pointee
)
3876 mio_symbol_ref (&sym
->cp_pointer
);
3878 /* Note that components are always saved, even if they are supposed
3879 to be private. Component access is checked during searching. */
3881 mio_component_list (&sym
->components
, sym
->attr
.vtype
);
3883 if (sym
->components
!= NULL
)
3884 sym
->component_access
3885 = MIO_NAME (gfc_access
) (sym
->component_access
, access_types
);
3887 /* Load/save the f2k_derived namespace of a derived-type symbol. */
3888 mio_full_f2k_derived (sym
);
3892 /* Add the fields that say whether this is from an intrinsic module,
3893 and if so, what symbol it is within the module. */
3894 /* mio_integer (&(sym->from_intmod)); */
3895 if (iomode
== IO_OUTPUT
)
3897 intmod
= sym
->from_intmod
;
3898 mio_integer (&intmod
);
3902 mio_integer (&intmod
);
3903 sym
->from_intmod
= (intmod_id
) intmod
;
3906 mio_integer (&(sym
->intmod_sym_id
));
3908 if (sym
->attr
.flavor
== FL_DERIVED
)
3909 mio_integer (&(sym
->hash_value
));
3915 /************************* Top level subroutines *************************/
3917 /* Given a root symtree node and a symbol, try to find a symtree that
3918 references the symbol that is not a unique name. */
3920 static gfc_symtree
*
3921 find_symtree_for_symbol (gfc_symtree
*st
, gfc_symbol
*sym
)
3923 gfc_symtree
*s
= NULL
;
3928 s
= find_symtree_for_symbol (st
->right
, sym
);
3931 s
= find_symtree_for_symbol (st
->left
, sym
);
3935 if (st
->n
.sym
== sym
&& !check_unique_name (st
->name
))
3942 /* A recursive function to look for a specific symbol by name and by
3943 module. Whilst several symtrees might point to one symbol, its
3944 is sufficient for the purposes here than one exist. Note that
3945 generic interfaces are distinguished as are symbols that have been
3946 renamed in another module. */
3947 static gfc_symtree
*
3948 find_symbol (gfc_symtree
*st
, const char *name
,
3949 const char *module
, int generic
)
3952 gfc_symtree
*retval
, *s
;
3954 if (st
== NULL
|| st
->n
.sym
== NULL
)
3957 c
= strcmp (name
, st
->n
.sym
->name
);
3958 if (c
== 0 && st
->n
.sym
->module
3959 && strcmp (module
, st
->n
.sym
->module
) == 0
3960 && !check_unique_name (st
->name
))
3962 s
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
3964 /* Detect symbols that are renamed by use association in another
3965 module by the absence of a symtree and null attr.use_rename,
3966 since the latter is not transmitted in the module file. */
3967 if (((!generic
&& !st
->n
.sym
->attr
.generic
)
3968 || (generic
&& st
->n
.sym
->attr
.generic
))
3969 && !(s
== NULL
&& !st
->n
.sym
->attr
.use_rename
))
3973 retval
= find_symbol (st
->left
, name
, module
, generic
);
3976 retval
= find_symbol (st
->right
, name
, module
, generic
);
3982 /* Skip a list between balanced left and right parens. */
3992 switch (parse_atom ())
4015 /* Load operator interfaces from the module. Interfaces are unusual
4016 in that they attach themselves to existing symbols. */
4019 load_operator_interfaces (void)
4022 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module
[GFC_MAX_SYMBOL_LEN
+ 1];
4024 pointer_info
*pi
= NULL
;
4029 while (peek_atom () != ATOM_RPAREN
)
4033 mio_internal_string (name
);
4034 mio_internal_string (module
);
4036 n
= number_use_names (name
, true);
4039 for (i
= 1; i
<= n
; i
++)
4041 /* Decide if we need to load this one or not. */
4042 p
= find_use_name_n (name
, &i
, true);
4046 while (parse_atom () != ATOM_RPAREN
);
4052 uop
= gfc_get_uop (p
);
4053 pi
= mio_interface_rest (&uop
->op
);
4057 if (gfc_find_uop (p
, NULL
))
4059 uop
= gfc_get_uop (p
);
4060 uop
->op
= gfc_get_interface ();
4061 uop
->op
->where
= gfc_current_locus
;
4062 add_fixup (pi
->integer
, &uop
->op
->sym
);
4071 /* Load interfaces from the module. Interfaces are unusual in that
4072 they attach themselves to existing symbols. */
4075 load_generic_interfaces (void)
4078 char name
[GFC_MAX_SYMBOL_LEN
+ 1], module
[GFC_MAX_SYMBOL_LEN
+ 1];
4080 gfc_interface
*generic
= NULL
, *gen
= NULL
;
4082 bool ambiguous_set
= false;
4086 while (peek_atom () != ATOM_RPAREN
)
4090 mio_internal_string (name
);
4091 mio_internal_string (module
);
4093 n
= number_use_names (name
, false);
4094 renamed
= n
? 1 : 0;
4097 for (i
= 1; i
<= n
; i
++)
4100 /* Decide if we need to load this one or not. */
4101 p
= find_use_name_n (name
, &i
, false);
4103 st
= find_symbol (gfc_current_ns
->sym_root
,
4104 name
, module_name
, 1);
4106 if (!p
|| gfc_find_symbol (p
, NULL
, 0, &sym
))
4108 /* Skip the specific names for these cases. */
4109 while (i
== 1 && parse_atom () != ATOM_RPAREN
);
4114 /* If the symbol exists already and is being USEd without being
4115 in an ONLY clause, do not load a new symtree(11.3.2). */
4116 if (!only_flag
&& st
)
4124 if (strcmp (st
->name
, p
) != 0)
4126 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, p
);
4132 /* Since we haven't found a valid generic interface, we had
4136 gfc_get_symbol (p
, NULL
, &sym
);
4137 sym
->name
= gfc_get_string (name
);
4138 sym
->module
= module_name
;
4139 sym
->attr
.flavor
= FL_PROCEDURE
;
4140 sym
->attr
.generic
= 1;
4141 sym
->attr
.use_assoc
= 1;
4146 /* Unless sym is a generic interface, this reference
4149 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
4153 if (st
&& !sym
->attr
.generic
4156 && strcmp (module
, sym
->module
))
4158 ambiguous_set
= true;
4163 sym
->attr
.use_only
= only_flag
;
4164 sym
->attr
.use_rename
= renamed
;
4168 mio_interface_rest (&sym
->generic
);
4169 generic
= sym
->generic
;
4171 else if (!sym
->generic
)
4173 sym
->generic
= generic
;
4174 sym
->attr
.generic_copy
= 1;
4177 /* If a procedure that is not generic has generic interfaces
4178 that include itself, it is generic! We need to take care
4179 to retain symbols ambiguous that were already so. */
4180 if (sym
->attr
.use_assoc
4181 && !sym
->attr
.generic
4182 && sym
->attr
.flavor
== FL_PROCEDURE
)
4184 for (gen
= generic
; gen
; gen
= gen
->next
)
4186 if (gen
->sym
== sym
)
4188 sym
->attr
.generic
= 1;
4203 /* Load common blocks. */
4208 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4213 while (peek_atom () != ATOM_RPAREN
)
4218 mio_internal_string (name
);
4220 p
= gfc_get_common (name
, 1);
4222 mio_symbol_ref (&p
->head
);
4223 mio_integer (&flags
);
4227 p
->threadprivate
= 1;
4230 /* Get whether this was a bind(c) common or not. */
4231 mio_integer (&p
->is_bind_c
);
4232 /* Get the binding label. */
4233 label
= read_string ();
4235 p
->binding_label
= IDENTIFIER_POINTER (get_identifier (label
));
4245 /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
4246 so that unused variables are not loaded and so that the expression can
4252 gfc_equiv
*head
, *tail
, *end
, *eq
;
4256 in_load_equiv
= true;
4258 end
= gfc_current_ns
->equiv
;
4259 while (end
!= NULL
&& end
->next
!= NULL
)
4262 while (peek_atom () != ATOM_RPAREN
) {
4266 while(peek_atom () != ATOM_RPAREN
)
4269 head
= tail
= gfc_get_equiv ();
4272 tail
->eq
= gfc_get_equiv ();
4276 mio_pool_string (&tail
->module
);
4277 mio_expr (&tail
->expr
);
4280 /* Unused equivalence members have a unique name. In addition, it
4281 must be checked that the symbols are from the same module. */
4283 for (eq
= head
; eq
; eq
= eq
->eq
)
4285 if (eq
->expr
->symtree
->n
.sym
->module
4286 && head
->expr
->symtree
->n
.sym
->module
4287 && strcmp (head
->expr
->symtree
->n
.sym
->module
,
4288 eq
->expr
->symtree
->n
.sym
->module
) == 0
4289 && !check_unique_name (eq
->expr
->symtree
->name
))
4298 for (eq
= head
; eq
; eq
= head
)
4301 gfc_free_expr (eq
->expr
);
4307 gfc_current_ns
->equiv
= head
;
4318 in_load_equiv
= false;
4322 /* This function loads the sym_root of f2k_derived with the extensions to
4323 the derived type. */
4325 load_derived_extensions (void)
4328 gfc_symbol
*derived
;
4332 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4333 char module
[GFC_MAX_SYMBOL_LEN
+ 1];
4337 while (peek_atom () != ATOM_RPAREN
)
4340 mio_integer (&symbol
);
4341 info
= get_integer (symbol
);
4342 derived
= info
->u
.rsym
.sym
;
4344 /* This one is not being loaded. */
4345 if (!info
|| !derived
)
4347 while (peek_atom () != ATOM_RPAREN
)
4352 gcc_assert (derived
->attr
.flavor
== FL_DERIVED
);
4353 if (derived
->f2k_derived
== NULL
)
4354 derived
->f2k_derived
= gfc_get_namespace (NULL
, 0);
4356 while (peek_atom () != ATOM_RPAREN
)
4359 mio_internal_string (name
);
4360 mio_internal_string (module
);
4362 /* Only use one use name to find the symbol. */
4364 p
= find_use_name_n (name
, &j
, false);
4367 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
4369 st
= gfc_find_symtree (derived
->f2k_derived
->sym_root
, name
);
4372 /* Only use the real name in f2k_derived to ensure a single
4374 st
= gfc_new_symtree (&derived
->f2k_derived
->sym_root
, name
);
4387 /* Recursive function to traverse the pointer_info tree and load a
4388 needed symbol. We return nonzero if we load a symbol and stop the
4389 traversal, because the act of loading can alter the tree. */
4392 load_needed (pointer_info
*p
)
4403 rv
|= load_needed (p
->left
);
4404 rv
|= load_needed (p
->right
);
4406 if (p
->type
!= P_SYMBOL
|| p
->u
.rsym
.state
!= NEEDED
)
4409 p
->u
.rsym
.state
= USED
;
4411 set_module_locus (&p
->u
.rsym
.where
);
4413 sym
= p
->u
.rsym
.sym
;
4416 q
= get_integer (p
->u
.rsym
.ns
);
4418 ns
= (gfc_namespace
*) q
->u
.pointer
;
4421 /* Create an interface namespace if necessary. These are
4422 the namespaces that hold the formal parameters of module
4425 ns
= gfc_get_namespace (NULL
, 0);
4426 associate_integer_pointer (q
, ns
);
4429 /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
4430 doesn't go pear-shaped if the symbol is used. */
4432 gfc_find_symbol (p
->u
.rsym
.module
, gfc_current_ns
,
4435 sym
= gfc_new_symbol (p
->u
.rsym
.true_name
, ns
);
4436 sym
->name
= dt_lower_string (p
->u
.rsym
.true_name
);
4437 sym
->module
= gfc_get_string (p
->u
.rsym
.module
);
4438 if (p
->u
.rsym
.binding_label
)
4439 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier
4440 (p
->u
.rsym
.binding_label
));
4442 associate_integer_pointer (p
, sym
);
4446 sym
->attr
.use_assoc
= 1;
4448 /* Mark as only or rename for later diagnosis for explicitly imported
4449 but not used warnings; don't mark internal symbols such as __vtab,
4450 __def_init etc. Only mark them if they have been explicitly loaded. */
4452 if (only_flag
&& sym
->name
[0] != '_' && sym
->name
[1] != '_')
4456 /* Search the use/rename list for the variable; if the variable is
4458 for (u
= gfc_rename_list
; u
; u
= u
->next
)
4460 if (strcmp (u
->use_name
, sym
->name
) == 0)
4462 sym
->attr
.use_only
= 1;
4468 if (p
->u
.rsym
.renamed
)
4469 sym
->attr
.use_rename
= 1;
4475 /* Recursive function for cleaning up things after a module has been read. */
4478 read_cleanup (pointer_info
*p
)
4486 read_cleanup (p
->left
);
4487 read_cleanup (p
->right
);
4489 if (p
->type
== P_SYMBOL
&& p
->u
.rsym
.state
== USED
&& !p
->u
.rsym
.referenced
)
4492 /* Add hidden symbols to the symtree. */
4493 q
= get_integer (p
->u
.rsym
.ns
);
4494 ns
= (gfc_namespace
*) q
->u
.pointer
;
4496 if (!p
->u
.rsym
.sym
->attr
.vtype
4497 && !p
->u
.rsym
.sym
->attr
.vtab
)
4498 st
= gfc_get_unique_symtree (ns
);
4501 /* There is no reason to use 'unique_symtrees' for vtabs or
4502 vtypes - their name is fine for a symtree and reduces the
4503 namespace pollution. */
4504 st
= gfc_find_symtree (ns
->sym_root
, p
->u
.rsym
.sym
->name
);
4506 st
= gfc_new_symtree (&ns
->sym_root
, p
->u
.rsym
.sym
->name
);
4509 st
->n
.sym
= p
->u
.rsym
.sym
;
4512 /* Fixup any symtree references. */
4513 p
->u
.rsym
.symtree
= st
;
4514 resolve_fixups (p
->u
.rsym
.stfixup
, st
);
4515 p
->u
.rsym
.stfixup
= NULL
;
4518 /* Free unused symbols. */
4519 if (p
->type
== P_SYMBOL
&& p
->u
.rsym
.state
== UNUSED
)
4520 gfc_free_symbol (p
->u
.rsym
.sym
);
4524 /* It is not quite enough to check for ambiguity in the symbols by
4525 the loaded symbol and the new symbol not being identical. */
4527 check_for_ambiguous (gfc_symbol
*st_sym
, pointer_info
*info
)
4531 symbol_attribute attr
;
4533 if (gfc_current_ns
->proc_name
&& st_sym
->name
== gfc_current_ns
->proc_name
->name
)
4535 gfc_error ("'%s' of module '%s', imported at %C, is also the name of the "
4536 "current program unit", st_sym
->name
, module_name
);
4540 rsym
= info
->u
.rsym
.sym
;
4544 if (st_sym
->attr
.vtab
|| st_sym
->attr
.vtype
)
4547 /* If the existing symbol is generic from a different module and
4548 the new symbol is generic there can be no ambiguity. */
4549 if (st_sym
->attr
.generic
4551 && st_sym
->module
!= module_name
)
4553 /* The new symbol's attributes have not yet been read. Since
4554 we need attr.generic, read it directly. */
4555 get_module_locus (&locus
);
4556 set_module_locus (&info
->u
.rsym
.where
);
4559 mio_symbol_attribute (&attr
);
4560 set_module_locus (&locus
);
4569 /* Read a module file. */
4574 module_locus operator_interfaces
, user_operators
, extensions
;
4576 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4578 int ambiguous
, j
, nuse
, symbol
;
4579 pointer_info
*info
, *q
;
4580 gfc_use_rename
*u
= NULL
;
4584 get_module_locus (&operator_interfaces
); /* Skip these for now. */
4587 get_module_locus (&user_operators
);
4591 /* Skip commons, equivalences and derived type extensions for now. */
4595 get_module_locus (&extensions
);
4600 /* Create the fixup nodes for all the symbols. */
4602 while (peek_atom () != ATOM_RPAREN
)
4605 require_atom (ATOM_INTEGER
);
4606 info
= get_integer (atom_int
);
4608 info
->type
= P_SYMBOL
;
4609 info
->u
.rsym
.state
= UNUSED
;
4611 info
->u
.rsym
.true_name
= read_string ();
4612 info
->u
.rsym
.module
= read_string ();
4613 bind_label
= read_string ();
4614 if (strlen (bind_label
))
4615 info
->u
.rsym
.binding_label
= bind_label
;
4617 XDELETEVEC (bind_label
);
4619 require_atom (ATOM_INTEGER
);
4620 info
->u
.rsym
.ns
= atom_int
;
4622 get_module_locus (&info
->u
.rsym
.where
);
4625 /* See if the symbol has already been loaded by a previous module.
4626 If so, we reference the existing symbol and prevent it from
4627 being loaded again. This should not happen if the symbol being
4628 read is an index for an assumed shape dummy array (ns != 1). */
4630 sym
= find_true_name (info
->u
.rsym
.true_name
, info
->u
.rsym
.module
);
4633 || (sym
->attr
.flavor
== FL_VARIABLE
&& info
->u
.rsym
.ns
!=1))
4636 info
->u
.rsym
.state
= USED
;
4637 info
->u
.rsym
.sym
= sym
;
4639 /* Some symbols do not have a namespace (eg. formal arguments),
4640 so the automatic "unique symtree" mechanism must be suppressed
4641 by marking them as referenced. */
4642 q
= get_integer (info
->u
.rsym
.ns
);
4643 if (q
->u
.pointer
== NULL
)
4645 info
->u
.rsym
.referenced
= 1;
4649 /* If possible recycle the symtree that references the symbol.
4650 If a symtree is not found and the module does not import one,
4651 a unique-name symtree is found by read_cleanup. */
4652 st
= find_symtree_for_symbol (gfc_current_ns
->sym_root
, sym
);
4655 info
->u
.rsym
.symtree
= st
;
4656 info
->u
.rsym
.referenced
= 1;
4662 /* Parse the symtree lists. This lets us mark which symbols need to
4663 be loaded. Renaming is also done at this point by replacing the
4668 while (peek_atom () != ATOM_RPAREN
)
4670 mio_internal_string (name
);
4671 mio_integer (&ambiguous
);
4672 mio_integer (&symbol
);
4674 info
= get_integer (symbol
);
4676 /* See how many use names there are. If none, go through the start
4677 of the loop at least once. */
4678 nuse
= number_use_names (name
, false);
4679 info
->u
.rsym
.renamed
= nuse
? 1 : 0;
4684 for (j
= 1; j
<= nuse
; j
++)
4686 /* Get the jth local name for this symbol. */
4687 p
= find_use_name_n (name
, &j
, false);
4689 if (p
== NULL
&& strcmp (name
, module_name
) == 0)
4692 /* Exception: Always import vtabs & vtypes. */
4693 if (p
== NULL
&& name
[0] == '_'
4694 && (strncmp (name
, "__vtab_", 5) == 0
4695 || strncmp (name
, "__vtype_", 6) == 0))
4698 /* Skip symtree nodes not in an ONLY clause, unless there
4699 is an existing symtree loaded from another USE statement. */
4702 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
4704 && strcmp (st
->n
.sym
->name
, info
->u
.rsym
.true_name
) == 0
4705 && st
->n
.sym
->module
!= NULL
4706 && strcmp (st
->n
.sym
->module
, info
->u
.rsym
.module
) == 0)
4708 info
->u
.rsym
.symtree
= st
;
4709 info
->u
.rsym
.sym
= st
->n
.sym
;
4714 /* If a symbol of the same name and module exists already,
4715 this symbol, which is not in an ONLY clause, must not be
4716 added to the namespace(11.3.2). Note that find_symbol
4717 only returns the first occurrence that it finds. */
4718 if (!only_flag
&& !info
->u
.rsym
.renamed
4719 && strcmp (name
, module_name
) != 0
4720 && find_symbol (gfc_current_ns
->sym_root
, name
,
4724 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, p
);
4728 /* Check for ambiguous symbols. */
4729 if (check_for_ambiguous (st
->n
.sym
, info
))
4732 info
->u
.rsym
.symtree
= st
;
4736 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
4738 /* Create a symtree node in the current namespace for this
4740 st
= check_unique_name (p
)
4741 ? gfc_get_unique_symtree (gfc_current_ns
)
4742 : gfc_new_symtree (&gfc_current_ns
->sym_root
, p
);
4743 st
->ambiguous
= ambiguous
;
4745 sym
= info
->u
.rsym
.sym
;
4747 /* Create a symbol node if it doesn't already exist. */
4750 info
->u
.rsym
.sym
= gfc_new_symbol (info
->u
.rsym
.true_name
,
4752 info
->u
.rsym
.sym
->name
= dt_lower_string (info
->u
.rsym
.true_name
);
4753 sym
= info
->u
.rsym
.sym
;
4754 sym
->module
= gfc_get_string (info
->u
.rsym
.module
);
4756 if (info
->u
.rsym
.binding_label
)
4757 sym
->binding_label
=
4758 IDENTIFIER_POINTER (get_identifier
4759 (info
->u
.rsym
.binding_label
));
4765 if (strcmp (name
, p
) != 0)
4766 sym
->attr
.use_rename
= 1;
4769 || (strncmp (name
, "__vtab_", 5) != 0
4770 && strncmp (name
, "__vtype_", 6) != 0))
4771 sym
->attr
.use_only
= only_flag
;
4773 /* Store the symtree pointing to this symbol. */
4774 info
->u
.rsym
.symtree
= st
;
4776 if (info
->u
.rsym
.state
== UNUSED
)
4777 info
->u
.rsym
.state
= NEEDED
;
4778 info
->u
.rsym
.referenced
= 1;
4785 /* Load intrinsic operator interfaces. */
4786 set_module_locus (&operator_interfaces
);
4789 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
4791 if (i
== INTRINSIC_USER
)
4796 u
= find_use_operator ((gfc_intrinsic_op
) i
);
4807 mio_interface (&gfc_current_ns
->op
[i
]);
4808 if (u
&& !gfc_current_ns
->op
[i
])
4814 /* Load generic and user operator interfaces. These must follow the
4815 loading of symtree because otherwise symbols can be marked as
4818 set_module_locus (&user_operators
);
4820 load_operator_interfaces ();
4821 load_generic_interfaces ();
4826 /* At this point, we read those symbols that are needed but haven't
4827 been loaded yet. If one symbol requires another, the other gets
4828 marked as NEEDED if its previous state was UNUSED. */
4830 while (load_needed (pi_root
));
4832 /* Make sure all elements of the rename-list were found in the module. */
4834 for (u
= gfc_rename_list
; u
; u
= u
->next
)
4839 if (u
->op
== INTRINSIC_NONE
)
4841 gfc_error ("Symbol '%s' referenced at %L not found in module '%s'",
4842 u
->use_name
, &u
->where
, module_name
);
4846 if (u
->op
== INTRINSIC_USER
)
4848 gfc_error ("User operator '%s' referenced at %L not found "
4849 "in module '%s'", u
->use_name
, &u
->where
, module_name
);
4853 gfc_error ("Intrinsic operator '%s' referenced at %L not found "
4854 "in module '%s'", gfc_op2string (u
->op
), &u
->where
,
4858 /* Now we should be in a position to fill f2k_derived with derived type
4859 extensions, since everything has been loaded. */
4860 set_module_locus (&extensions
);
4861 load_derived_extensions ();
4863 /* Clean up symbol nodes that were never loaded, create references
4864 to hidden symbols. */
4866 read_cleanup (pi_root
);
4870 /* Given an access type that is specific to an entity and the default
4871 access, return nonzero if the entity is publicly accessible. If the
4872 element is declared as PUBLIC, then it is public; if declared
4873 PRIVATE, then private, and otherwise it is public unless the default
4874 access in this context has been declared PRIVATE. */
4877 check_access (gfc_access specific_access
, gfc_access default_access
)
4879 if (specific_access
== ACCESS_PUBLIC
)
4881 if (specific_access
== ACCESS_PRIVATE
)
4884 if (gfc_option
.flag_module_private
)
4885 return default_access
== ACCESS_PUBLIC
;
4887 return default_access
!= ACCESS_PRIVATE
;
4892 gfc_check_symbol_access (gfc_symbol
*sym
)
4894 if (sym
->attr
.vtab
|| sym
->attr
.vtype
)
4897 return check_access (sym
->attr
.access
, sym
->ns
->default_access
);
4901 /* A structure to remember which commons we've already written. */
4903 struct written_common
4905 BBT_HEADER(written_common
);
4906 const char *name
, *label
;
4909 static struct written_common
*written_commons
= NULL
;
4911 /* Comparison function used for balancing the binary tree. */
4914 compare_written_commons (void *a1
, void *b1
)
4916 const char *aname
= ((struct written_common
*) a1
)->name
;
4917 const char *alabel
= ((struct written_common
*) a1
)->label
;
4918 const char *bname
= ((struct written_common
*) b1
)->name
;
4919 const char *blabel
= ((struct written_common
*) b1
)->label
;
4920 int c
= strcmp (aname
, bname
);
4922 return (c
!= 0 ? c
: strcmp (alabel
, blabel
));
4925 /* Free a list of written commons. */
4928 free_written_common (struct written_common
*w
)
4934 free_written_common (w
->left
);
4936 free_written_common (w
->right
);
4941 /* Write a common block to the module -- recursive helper function. */
4944 write_common_0 (gfc_symtree
*st
, bool this_module
)
4950 struct written_common
*w
;
4951 bool write_me
= true;
4956 write_common_0 (st
->left
, this_module
);
4958 /* We will write out the binding label, or "" if no label given. */
4959 name
= st
->n
.common
->name
;
4961 label
= (p
->is_bind_c
&& p
->binding_label
) ? p
->binding_label
: "";
4963 /* Check if we've already output this common. */
4964 w
= written_commons
;
4967 int c
= strcmp (name
, w
->name
);
4968 c
= (c
!= 0 ? c
: strcmp (label
, w
->label
));
4972 w
= (c
< 0) ? w
->left
: w
->right
;
4975 if (this_module
&& p
->use_assoc
)
4980 /* Write the common to the module. */
4982 mio_pool_string (&name
);
4984 mio_symbol_ref (&p
->head
);
4985 flags
= p
->saved
? 1 : 0;
4986 if (p
->threadprivate
)
4988 mio_integer (&flags
);
4990 /* Write out whether the common block is bind(c) or not. */
4991 mio_integer (&(p
->is_bind_c
));
4993 mio_pool_string (&label
);
4996 /* Record that we have written this common. */
4997 w
= XCNEW (struct written_common
);
5000 gfc_insert_bbt (&written_commons
, w
, compare_written_commons
);
5003 write_common_0 (st
->right
, this_module
);
5007 /* Write a common, by initializing the list of written commons, calling
5008 the recursive function write_common_0() and cleaning up afterwards. */
5011 write_common (gfc_symtree
*st
)
5013 written_commons
= NULL
;
5014 write_common_0 (st
, true);
5015 write_common_0 (st
, false);
5016 free_written_common (written_commons
);
5017 written_commons
= NULL
;
5021 /* Write the blank common block to the module. */
5024 write_blank_common (void)
5026 const char * name
= BLANK_COMMON_NAME
;
5028 /* TODO: Blank commons are not bind(c). The F2003 standard probably says
5029 this, but it hasn't been checked. Just making it so for now. */
5032 if (gfc_current_ns
->blank_common
.head
== NULL
)
5037 mio_pool_string (&name
);
5039 mio_symbol_ref (&gfc_current_ns
->blank_common
.head
);
5040 saved
= gfc_current_ns
->blank_common
.saved
;
5041 mio_integer (&saved
);
5043 /* Write out whether the common block is bind(c) or not. */
5044 mio_integer (&is_bind_c
);
5046 /* Write out an empty binding label. */
5047 write_atom (ATOM_STRING
, "");
5053 /* Write equivalences to the module. */
5062 for (eq
= gfc_current_ns
->equiv
; eq
; eq
= eq
->next
)
5066 for (e
= eq
; e
; e
= e
->eq
)
5068 if (e
->module
== NULL
)
5069 e
->module
= gfc_get_string ("%s.eq.%d", module_name
, num
);
5070 mio_allocated_string (e
->module
);
5071 mio_expr (&e
->expr
);
5080 /* Write derived type extensions to the module. */
5083 write_dt_extensions (gfc_symtree
*st
)
5085 if (!gfc_check_symbol_access (st
->n
.sym
))
5087 if (!(st
->n
.sym
->ns
&& st
->n
.sym
->ns
->proc_name
5088 && st
->n
.sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
))
5092 mio_pool_string (&st
->name
);
5093 if (st
->n
.sym
->module
!= NULL
)
5094 mio_pool_string (&st
->n
.sym
->module
);
5097 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5098 if (iomode
== IO_OUTPUT
)
5099 strcpy (name
, module_name
);
5100 mio_internal_string (name
);
5101 if (iomode
== IO_INPUT
)
5102 module_name
= gfc_get_string (name
);
5108 write_derived_extensions (gfc_symtree
*st
)
5110 if (!((st
->n
.sym
->attr
.flavor
== FL_DERIVED
)
5111 && (st
->n
.sym
->f2k_derived
!= NULL
)
5112 && (st
->n
.sym
->f2k_derived
->sym_root
!= NULL
)))
5116 mio_symbol_ref (&(st
->n
.sym
));
5117 gfc_traverse_symtree (st
->n
.sym
->f2k_derived
->sym_root
,
5118 write_dt_extensions
);
5123 /* Write a symbol to the module. */
5126 write_symbol (int n
, gfc_symbol
*sym
)
5130 if (sym
->attr
.flavor
== FL_UNKNOWN
|| sym
->attr
.flavor
== FL_LABEL
)
5131 gfc_internal_error ("write_symbol(): bad module symbol '%s'", sym
->name
);
5135 if (sym
->attr
.flavor
== FL_DERIVED
)
5138 name
= dt_upper_string (sym
->name
);
5139 mio_pool_string (&name
);
5142 mio_pool_string (&sym
->name
);
5144 mio_pool_string (&sym
->module
);
5145 if ((sym
->attr
.is_bind_c
|| sym
->attr
.is_iso_c
) && sym
->binding_label
)
5147 label
= sym
->binding_label
;
5148 mio_pool_string (&label
);
5151 write_atom (ATOM_STRING
, "");
5153 mio_pointer_ref (&sym
->ns
);
5160 /* Recursive traversal function to write the initial set of symbols to
5161 the module. We check to see if the symbol should be written
5162 according to the access specification. */
5165 write_symbol0 (gfc_symtree
*st
)
5169 bool dont_write
= false;
5174 write_symbol0 (st
->left
);
5177 if (sym
->module
== NULL
)
5178 sym
->module
= module_name
;
5180 if (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
5181 && !sym
->attr
.subroutine
&& !sym
->attr
.function
)
5184 if (!gfc_check_symbol_access (sym
))
5189 p
= get_pointer (sym
);
5190 if (p
->type
== P_UNKNOWN
)
5193 if (p
->u
.wsym
.state
!= WRITTEN
)
5195 write_symbol (p
->integer
, sym
);
5196 p
->u
.wsym
.state
= WRITTEN
;
5200 write_symbol0 (st
->right
);
5204 /* Type for the temporary tree used when writing secondary symbols. */
5206 struct sorted_pointer_info
5208 BBT_HEADER (sorted_pointer_info
);
5213 #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
5215 /* Recursively traverse the temporary tree, free its contents. */
5218 free_sorted_pointer_info_tree (sorted_pointer_info
*p
)
5223 free_sorted_pointer_info_tree (p
->left
);
5224 free_sorted_pointer_info_tree (p
->right
);
5229 /* Comparison function for the temporary tree. */
5232 compare_sorted_pointer_info (void *_spi1
, void *_spi2
)
5234 sorted_pointer_info
*spi1
, *spi2
;
5235 spi1
= (sorted_pointer_info
*)_spi1
;
5236 spi2
= (sorted_pointer_info
*)_spi2
;
5238 if (spi1
->p
->integer
< spi2
->p
->integer
)
5240 if (spi1
->p
->integer
> spi2
->p
->integer
)
5246 /* Finds the symbols that need to be written and collects them in the
5247 sorted_pi tree so that they can be traversed in an order
5248 independent of memory addresses. */
5251 find_symbols_to_write(sorted_pointer_info
**tree
, pointer_info
*p
)
5256 if (p
->type
== P_SYMBOL
&& p
->u
.wsym
.state
== NEEDS_WRITE
)
5258 sorted_pointer_info
*sp
= gfc_get_sorted_pointer_info();
5261 gfc_insert_bbt (tree
, sp
, compare_sorted_pointer_info
);
5264 find_symbols_to_write (tree
, p
->left
);
5265 find_symbols_to_write (tree
, p
->right
);
5269 /* Recursive function that traverses the tree of symbols that need to be
5270 written and writes them in order. */
5273 write_symbol1_recursion (sorted_pointer_info
*sp
)
5278 write_symbol1_recursion (sp
->left
);
5280 pointer_info
*p1
= sp
->p
;
5281 gcc_assert (p1
->type
== P_SYMBOL
&& p1
->u
.wsym
.state
== NEEDS_WRITE
);
5283 p1
->u
.wsym
.state
= WRITTEN
;
5284 write_symbol (p1
->integer
, p1
->u
.wsym
.sym
);
5285 p1
->u
.wsym
.sym
->attr
.public_used
= 1;
5287 write_symbol1_recursion (sp
->right
);
5291 /* Write the secondary set of symbols to the module file. These are
5292 symbols that were not public yet are needed by the public symbols
5293 or another dependent symbol. The act of writing a symbol can add
5294 symbols to the pointer_info tree, so we return nonzero if a symbol
5295 was written and pass that information upwards. The caller will
5296 then call this function again until nothing was written. It uses
5297 the utility functions and a temporary tree to ensure a reproducible
5298 ordering of the symbol output and thus the module file. */
5301 write_symbol1 (pointer_info
*p
)
5306 /* Put symbols that need to be written into a tree sorted on the
5309 sorted_pointer_info
*spi_root
= NULL
;
5310 find_symbols_to_write (&spi_root
, p
);
5312 /* No symbols to write, return. */
5316 /* Otherwise, write and free the tree again. */
5317 write_symbol1_recursion (spi_root
);
5318 free_sorted_pointer_info_tree (spi_root
);
5324 /* Write operator interfaces associated with a symbol. */
5327 write_operator (gfc_user_op
*uop
)
5329 static char nullstring
[] = "";
5330 const char *p
= nullstring
;
5332 if (uop
->op
== NULL
|| !check_access (uop
->access
, uop
->ns
->default_access
))
5335 mio_symbol_interface (&uop
->name
, &p
, &uop
->op
);
5339 /* Write generic interfaces from the namespace sym_root. */
5342 write_generic (gfc_symtree
*st
)
5349 write_generic (st
->left
);
5352 if (sym
&& !check_unique_name (st
->name
)
5353 && sym
->generic
&& gfc_check_symbol_access (sym
))
5356 sym
->module
= module_name
;
5358 mio_symbol_interface (&st
->name
, &sym
->module
, &sym
->generic
);
5361 write_generic (st
->right
);
5366 write_symtree (gfc_symtree
*st
)
5373 /* A symbol in an interface body must not be visible in the
5375 if (sym
->ns
!= gfc_current_ns
5376 && sym
->ns
->proc_name
5377 && sym
->ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
)
5380 if (!gfc_check_symbol_access (sym
)
5381 || (sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
5382 && !sym
->attr
.subroutine
&& !sym
->attr
.function
))
5385 if (check_unique_name (st
->name
))
5388 p
= find_pointer (sym
);
5390 gfc_internal_error ("write_symtree(): Symbol not written");
5392 mio_pool_string (&st
->name
);
5393 mio_integer (&st
->ambiguous
);
5394 mio_integer (&p
->integer
);
5403 /* Write the operator interfaces. */
5406 for (i
= GFC_INTRINSIC_BEGIN
; i
!= GFC_INTRINSIC_END
; i
++)
5408 if (i
== INTRINSIC_USER
)
5411 mio_interface (check_access (gfc_current_ns
->operator_access
[i
],
5412 gfc_current_ns
->default_access
)
5413 ? &gfc_current_ns
->op
[i
] : NULL
);
5421 gfc_traverse_user_op (gfc_current_ns
, write_operator
);
5427 write_generic (gfc_current_ns
->sym_root
);
5433 write_blank_common ();
5434 write_common (gfc_current_ns
->common_root
);
5446 gfc_traverse_symtree (gfc_current_ns
->sym_root
,
5447 write_derived_extensions
);
5452 /* Write symbol information. First we traverse all symbols in the
5453 primary namespace, writing those that need to be written.
5454 Sometimes writing one symbol will cause another to need to be
5455 written. A list of these symbols ends up on the write stack, and
5456 we end by popping the bottom of the stack and writing the symbol
5457 until the stack is empty. */
5461 write_symbol0 (gfc_current_ns
->sym_root
);
5462 while (write_symbol1 (pi_root
))
5471 gfc_traverse_symtree (gfc_current_ns
->sym_root
, write_symtree
);
5476 /* Read a CRC32 sum from the gzip trailer of a module file. Returns
5477 true on success, false on failure. */
5480 read_crc32_from_module_file (const char* filename
, uLong
* crc
)
5486 /* Open the file in binary mode. */
5487 if ((file
= fopen (filename
, "rb")) == NULL
)
5490 /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
5491 file. See RFC 1952. */
5492 if (fseek (file
, -8, SEEK_END
) != 0)
5498 /* Read the CRC32. */
5499 if (fread (buf
, 1, 4, file
) != 4)
5505 /* Close the file. */
5508 val
= (buf
[0] & 0xFF) + ((buf
[1] & 0xFF) << 8) + ((buf
[2] & 0xFF) << 16)
5509 + ((buf
[3] & 0xFF) << 24);
5512 /* For debugging, the CRC value printed in hexadecimal should match
5513 the CRC printed by "zcat -l -v filename".
5514 printf("CRC of file %s is %x\n", filename, val); */
5520 /* Given module, dump it to disk. If there was an error while
5521 processing the module, dump_flag will be set to zero and we delete
5522 the module file, even if it was already there. */
5525 gfc_dump_module (const char *name
, int dump_flag
)
5528 char *filename
, *filename_tmp
;
5531 n
= strlen (name
) + strlen (MODULE_EXTENSION
) + 1;
5532 if (gfc_option
.module_dir
!= NULL
)
5534 n
+= strlen (gfc_option
.module_dir
);
5535 filename
= (char *) alloca (n
);
5536 strcpy (filename
, gfc_option
.module_dir
);
5537 strcat (filename
, name
);
5541 filename
= (char *) alloca (n
);
5542 strcpy (filename
, name
);
5544 strcat (filename
, MODULE_EXTENSION
);
5546 /* Name of the temporary file used to write the module. */
5547 filename_tmp
= (char *) alloca (n
+ 1);
5548 strcpy (filename_tmp
, filename
);
5549 strcat (filename_tmp
, "0");
5551 /* There was an error while processing the module. We delete the
5552 module file, even if it was already there. */
5559 if (gfc_cpp_makedep ())
5560 gfc_cpp_add_target (filename
);
5562 /* Write the module to the temporary file. */
5563 module_fp
= gzopen (filename_tmp
, "w");
5564 if (module_fp
== NULL
)
5565 gfc_fatal_error ("Can't open module file '%s' for writing at %C: %s",
5566 filename_tmp
, xstrerror (errno
));
5568 gzprintf (module_fp
, "GFORTRAN module version '%s' created from %s\n",
5569 MOD_VERSION
, gfc_source_file
);
5571 /* Write the module itself. */
5573 module_name
= gfc_get_string (name
);
5579 free_pi_tree (pi_root
);
5584 if (gzclose (module_fp
))
5585 gfc_fatal_error ("Error writing module file '%s' for writing: %s",
5586 filename_tmp
, xstrerror (errno
));
5588 /* Read the CRC32 from the gzip trailers of the module files and
5590 if (!read_crc32_from_module_file (filename_tmp
, &crc
)
5591 || !read_crc32_from_module_file (filename
, &crc_old
)
5594 /* Module file have changed, replace the old one. */
5595 if (rename (filename_tmp
, filename
))
5596 gfc_fatal_error ("Can't rename module file '%s' to '%s': %s",
5597 filename_tmp
, filename
, xstrerror (errno
));
5601 if (unlink (filename_tmp
))
5602 gfc_fatal_error ("Can't delete temporary module file '%s': %s",
5603 filename_tmp
, xstrerror (errno
));
5609 create_intrinsic_function (const char *name
, int id
,
5610 const char *modname
, intmod_id module
,
5611 bool subroutine
, gfc_symbol
*result_type
)
5613 gfc_intrinsic_sym
*isym
;
5614 gfc_symtree
*tmp_symtree
;
5617 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
5620 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
5622 gfc_error ("Symbol '%s' already declared", name
);
5625 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
5626 sym
= tmp_symtree
->n
.sym
;
5630 gfc_isym_id isym_id
= gfc_isym_id_by_intmod (module
, id
);
5631 isym
= gfc_intrinsic_subroutine_by_id (isym_id
);
5632 sym
->attr
.subroutine
= 1;
5636 gfc_isym_id isym_id
= gfc_isym_id_by_intmod (module
, id
);
5637 isym
= gfc_intrinsic_function_by_id (isym_id
);
5639 sym
->attr
.function
= 1;
5642 sym
->ts
.type
= BT_DERIVED
;
5643 sym
->ts
.u
.derived
= result_type
;
5644 sym
->ts
.is_c_interop
= 1;
5645 isym
->ts
.f90_type
= BT_VOID
;
5646 isym
->ts
.type
= BT_DERIVED
;
5647 isym
->ts
.f90_type
= BT_VOID
;
5648 isym
->ts
.u
.derived
= result_type
;
5649 isym
->ts
.is_c_interop
= 1;
5654 sym
->attr
.flavor
= FL_PROCEDURE
;
5655 sym
->attr
.intrinsic
= 1;
5657 sym
->module
= gfc_get_string (modname
);
5658 sym
->attr
.use_assoc
= 1;
5659 sym
->from_intmod
= module
;
5660 sym
->intmod_sym_id
= id
;
5664 /* Import the intrinsic ISO_C_BINDING module, generating symbols in
5665 the current namespace for all named constants, pointer types, and
5666 procedures in the module unless the only clause was used or a rename
5667 list was provided. */
5670 import_iso_c_binding_module (void)
5672 gfc_symbol
*mod_sym
= NULL
, *return_type
;
5673 gfc_symtree
*mod_symtree
= NULL
, *tmp_symtree
;
5674 gfc_symtree
*c_ptr
= NULL
, *c_funptr
= NULL
;
5675 const char *iso_c_module_name
= "__iso_c_binding";
5678 bool want_c_ptr
= false, want_c_funptr
= false;
5680 /* Look only in the current namespace. */
5681 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, iso_c_module_name
);
5683 if (mod_symtree
== NULL
)
5685 /* symtree doesn't already exist in current namespace. */
5686 gfc_get_sym_tree (iso_c_module_name
, gfc_current_ns
, &mod_symtree
,
5689 if (mod_symtree
!= NULL
)
5690 mod_sym
= mod_symtree
->n
.sym
;
5692 gfc_internal_error ("import_iso_c_binding_module(): Unable to "
5693 "create symbol for %s", iso_c_module_name
);
5695 mod_sym
->attr
.flavor
= FL_MODULE
;
5696 mod_sym
->attr
.intrinsic
= 1;
5697 mod_sym
->module
= gfc_get_string (iso_c_module_name
);
5698 mod_sym
->from_intmod
= INTMOD_ISO_C_BINDING
;
5701 /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
5702 check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
5704 for (u
= gfc_rename_list
; u
; u
= u
->next
)
5706 if (strcmp (c_interop_kinds_table
[ISOCBINDING_NULL_PTR
].name
,
5709 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_LOC
].name
,
5712 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_NULL_FUNPTR
].name
,
5714 want_c_funptr
= true;
5715 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_FUNLOC
].name
,
5717 want_c_funptr
= true;
5718 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_PTR
].name
,
5721 c_ptr
= generate_isocbinding_symbol (iso_c_module_name
,
5722 (iso_c_binding_symbol
)
5724 u
->local_name
[0] ? u
->local_name
5728 else if (strcmp (c_interop_kinds_table
[ISOCBINDING_FUNPTR
].name
,
5732 = generate_isocbinding_symbol (iso_c_module_name
,
5733 (iso_c_binding_symbol
)
5735 u
->local_name
[0] ? u
->local_name
5741 if ((want_c_ptr
|| !only_flag
) && !c_ptr
)
5742 c_ptr
= generate_isocbinding_symbol (iso_c_module_name
,
5743 (iso_c_binding_symbol
)
5745 NULL
, NULL
, only_flag
);
5746 if ((want_c_funptr
|| !only_flag
) && !c_funptr
)
5747 c_funptr
= generate_isocbinding_symbol (iso_c_module_name
,
5748 (iso_c_binding_symbol
)
5750 NULL
, NULL
, only_flag
);
5752 /* Generate the symbols for the named constants representing
5753 the kinds for intrinsic data types. */
5754 for (i
= 0; i
< ISOCBINDING_NUMBER
; i
++)
5757 for (u
= gfc_rename_list
; u
; u
= u
->next
)
5758 if (strcmp (c_interop_kinds_table
[i
].name
, u
->use_name
) == 0)
5767 #define NAMED_FUNCTION(a,b,c,d) \
5769 not_in_std = (gfc_option.allow_std & d) == 0; \
5772 #define NAMED_SUBROUTINE(a,b,c,d) \
5774 not_in_std = (gfc_option.allow_std & d) == 0; \
5777 #define NAMED_INTCST(a,b,c,d) \
5779 not_in_std = (gfc_option.allow_std & d) == 0; \
5782 #define NAMED_REALCST(a,b,c,d) \
5784 not_in_std = (gfc_option.allow_std & d) == 0; \
5787 #define NAMED_CMPXCST(a,b,c,d) \
5789 not_in_std = (gfc_option.allow_std & d) == 0; \
5792 #include "iso-c-binding.def"
5800 gfc_error ("The symbol '%s', referenced at %L, is not "
5801 "in the selected standard", name
, &u
->where
);
5807 #define NAMED_FUNCTION(a,b,c,d) \
5809 if (a == ISOCBINDING_LOC) \
5810 return_type = c_ptr->n.sym; \
5811 else if (a == ISOCBINDING_FUNLOC) \
5812 return_type = c_funptr->n.sym; \
5814 return_type = NULL; \
5815 create_intrinsic_function (u->local_name[0] \
5816 ? u->local_name : u->use_name, \
5817 a, iso_c_module_name, \
5818 INTMOD_ISO_C_BINDING, false, \
5821 #define NAMED_SUBROUTINE(a,b,c,d) \
5823 create_intrinsic_function (u->local_name[0] ? u->local_name \
5825 a, iso_c_module_name, \
5826 INTMOD_ISO_C_BINDING, true, NULL); \
5828 #include "iso-c-binding.def"
5830 case ISOCBINDING_PTR
:
5831 case ISOCBINDING_FUNPTR
:
5832 /* Already handled above. */
5835 if (i
== ISOCBINDING_NULL_PTR
)
5836 tmp_symtree
= c_ptr
;
5837 else if (i
== ISOCBINDING_NULL_FUNPTR
)
5838 tmp_symtree
= c_funptr
;
5841 generate_isocbinding_symbol (iso_c_module_name
,
5842 (iso_c_binding_symbol
) i
,
5844 ? u
->local_name
: u
->use_name
,
5845 tmp_symtree
, false);
5849 if (!found
&& !only_flag
)
5851 /* Skip, if the symbol is not in the enabled standard. */
5854 #define NAMED_FUNCTION(a,b,c,d) \
5856 if ((gfc_option.allow_std & d) == 0) \
5859 #define NAMED_SUBROUTINE(a,b,c,d) \
5861 if ((gfc_option.allow_std & d) == 0) \
5864 #define NAMED_INTCST(a,b,c,d) \
5866 if ((gfc_option.allow_std & d) == 0) \
5869 #define NAMED_REALCST(a,b,c,d) \
5871 if ((gfc_option.allow_std & d) == 0) \
5874 #define NAMED_CMPXCST(a,b,c,d) \
5876 if ((gfc_option.allow_std & d) == 0) \
5879 #include "iso-c-binding.def"
5881 ; /* Not GFC_STD_* versioned. */
5886 #define NAMED_FUNCTION(a,b,c,d) \
5888 if (a == ISOCBINDING_LOC) \
5889 return_type = c_ptr->n.sym; \
5890 else if (a == ISOCBINDING_FUNLOC) \
5891 return_type = c_funptr->n.sym; \
5893 return_type = NULL; \
5894 create_intrinsic_function (b, a, iso_c_module_name, \
5895 INTMOD_ISO_C_BINDING, false, \
5898 #define NAMED_SUBROUTINE(a,b,c,d) \
5900 create_intrinsic_function (b, a, iso_c_module_name, \
5901 INTMOD_ISO_C_BINDING, true, NULL); \
5903 #include "iso-c-binding.def"
5905 case ISOCBINDING_PTR
:
5906 case ISOCBINDING_FUNPTR
:
5907 /* Already handled above. */
5910 if (i
== ISOCBINDING_NULL_PTR
)
5911 tmp_symtree
= c_ptr
;
5912 else if (i
== ISOCBINDING_NULL_FUNPTR
)
5913 tmp_symtree
= c_funptr
;
5916 generate_isocbinding_symbol (iso_c_module_name
,
5917 (iso_c_binding_symbol
) i
, NULL
,
5918 tmp_symtree
, false);
5923 for (u
= gfc_rename_list
; u
; u
= u
->next
)
5928 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
5929 "module ISO_C_BINDING", u
->use_name
, &u
->where
);
5934 /* Add an integer named constant from a given module. */
5937 create_int_parameter (const char *name
, int value
, const char *modname
,
5938 intmod_id module
, int id
)
5940 gfc_symtree
*tmp_symtree
;
5943 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
5944 if (tmp_symtree
!= NULL
)
5946 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
5949 gfc_error ("Symbol '%s' already declared", name
);
5952 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
5953 sym
= tmp_symtree
->n
.sym
;
5955 sym
->module
= gfc_get_string (modname
);
5956 sym
->attr
.flavor
= FL_PARAMETER
;
5957 sym
->ts
.type
= BT_INTEGER
;
5958 sym
->ts
.kind
= gfc_default_integer_kind
;
5959 sym
->value
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, value
);
5960 sym
->attr
.use_assoc
= 1;
5961 sym
->from_intmod
= module
;
5962 sym
->intmod_sym_id
= id
;
5966 /* Value is already contained by the array constructor, but not
5970 create_int_parameter_array (const char *name
, int size
, gfc_expr
*value
,
5971 const char *modname
, intmod_id module
, int id
)
5973 gfc_symtree
*tmp_symtree
;
5976 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
5977 if (tmp_symtree
!= NULL
)
5979 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
5982 gfc_error ("Symbol '%s' already declared", name
);
5985 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
5986 sym
= tmp_symtree
->n
.sym
;
5988 sym
->module
= gfc_get_string (modname
);
5989 sym
->attr
.flavor
= FL_PARAMETER
;
5990 sym
->ts
.type
= BT_INTEGER
;
5991 sym
->ts
.kind
= gfc_default_integer_kind
;
5992 sym
->attr
.use_assoc
= 1;
5993 sym
->from_intmod
= module
;
5994 sym
->intmod_sym_id
= id
;
5995 sym
->attr
.dimension
= 1;
5996 sym
->as
= gfc_get_array_spec ();
5998 sym
->as
->type
= AS_EXPLICIT
;
5999 sym
->as
->lower
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
6000 sym
->as
->upper
[0] = gfc_get_int_expr (gfc_default_integer_kind
, NULL
, size
);
6003 sym
->value
->shape
= gfc_get_shape (1);
6004 mpz_init_set_ui (sym
->value
->shape
[0], size
);
6008 /* Add an derived type for a given module. */
6011 create_derived_type (const char *name
, const char *modname
,
6012 intmod_id module
, int id
)
6014 gfc_symtree
*tmp_symtree
;
6015 gfc_symbol
*sym
, *dt_sym
;
6016 gfc_interface
*intr
, *head
;
6018 tmp_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, name
);
6019 if (tmp_symtree
!= NULL
)
6021 if (strcmp (modname
, tmp_symtree
->n
.sym
->module
) == 0)
6024 gfc_error ("Symbol '%s' already declared", name
);
6027 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp_symtree
, false);
6028 sym
= tmp_symtree
->n
.sym
;
6029 sym
->module
= gfc_get_string (modname
);
6030 sym
->from_intmod
= module
;
6031 sym
->intmod_sym_id
= id
;
6032 sym
->attr
.flavor
= FL_PROCEDURE
;
6033 sym
->attr
.function
= 1;
6034 sym
->attr
.generic
= 1;
6036 gfc_get_sym_tree (dt_upper_string (sym
->name
),
6037 gfc_current_ns
, &tmp_symtree
, false);
6038 dt_sym
= tmp_symtree
->n
.sym
;
6039 dt_sym
->name
= gfc_get_string (sym
->name
);
6040 dt_sym
->attr
.flavor
= FL_DERIVED
;
6041 dt_sym
->attr
.private_comp
= 1;
6042 dt_sym
->attr
.zero_comp
= 1;
6043 dt_sym
->attr
.use_assoc
= 1;
6044 dt_sym
->module
= gfc_get_string (modname
);
6045 dt_sym
->from_intmod
= module
;
6046 dt_sym
->intmod_sym_id
= id
;
6048 head
= sym
->generic
;
6049 intr
= gfc_get_interface ();
6051 intr
->where
= gfc_current_locus
;
6053 sym
->generic
= intr
;
6054 sym
->attr
.if_source
= IFSRC_DECL
;
6058 /* Read the contents of the module file into a temporary buffer. */
6061 read_module_to_tmpbuf ()
6063 /* We don't know the uncompressed size, so enlarge the buffer as
6069 module_content
= XNEWVEC (char, cursz
);
6073 int nread
= gzread (module_fp
, module_content
+ len
, rsize
);
6078 module_content
= XRESIZEVEC (char, module_content
, cursz
);
6079 rsize
= cursz
- len
;
6082 module_content
= XRESIZEVEC (char, module_content
, len
+ 1);
6083 module_content
[len
] = '\0';
6089 /* USE the ISO_FORTRAN_ENV intrinsic module. */
6092 use_iso_fortran_env_module (void)
6094 static char mod
[] = "iso_fortran_env";
6096 gfc_symbol
*mod_sym
;
6097 gfc_symtree
*mod_symtree
;
6101 intmod_sym symbol
[] = {
6102 #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
6103 #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
6104 #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
6105 #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
6106 #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
6107 #include "iso-fortran-env.def"
6108 { ISOFORTRANENV_INVALID
, NULL
, -1234, 0 } };
6111 #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
6112 #include "iso-fortran-env.def"
6114 /* Generate the symbol for the module itself. */
6115 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, mod
);
6116 if (mod_symtree
== NULL
)
6118 gfc_get_sym_tree (mod
, gfc_current_ns
, &mod_symtree
, false);
6119 gcc_assert (mod_symtree
);
6120 mod_sym
= mod_symtree
->n
.sym
;
6122 mod_sym
->attr
.flavor
= FL_MODULE
;
6123 mod_sym
->attr
.intrinsic
= 1;
6124 mod_sym
->module
= gfc_get_string (mod
);
6125 mod_sym
->from_intmod
= INTMOD_ISO_FORTRAN_ENV
;
6128 if (!mod_symtree
->n
.sym
->attr
.intrinsic
)
6129 gfc_error ("Use of intrinsic module '%s' at %C conflicts with "
6130 "non-intrinsic module name used previously", mod
);
6132 /* Generate the symbols for the module integer named constants. */
6134 for (i
= 0; symbol
[i
].name
; i
++)
6137 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6139 if (strcmp (symbol
[i
].name
, u
->use_name
) == 0)
6144 if (!gfc_notify_std (symbol
[i
].standard
, "The symbol '%s', "
6145 "referenced at %L, is not in the selected "
6146 "standard", symbol
[i
].name
, &u
->where
))
6149 if ((gfc_option
.flag_default_integer
|| gfc_option
.flag_default_real
)
6150 && symbol
[i
].id
== ISOFORTRANENV_NUMERIC_STORAGE_SIZE
)
6151 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named "
6152 "constant from intrinsic module "
6153 "ISO_FORTRAN_ENV at %L is incompatible with "
6154 "option %s", &u
->where
,
6155 gfc_option
.flag_default_integer
6156 ? "-fdefault-integer-8"
6157 : "-fdefault-real-8");
6158 switch (symbol
[i
].id
)
6160 #define NAMED_INTCST(a,b,c,d) \
6162 #include "iso-fortran-env.def"
6163 create_int_parameter (u
->local_name
[0] ? u
->local_name
6165 symbol
[i
].value
, mod
,
6166 INTMOD_ISO_FORTRAN_ENV
, symbol
[i
].id
);
6169 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6171 expr = gfc_get_array_expr (BT_INTEGER, \
6172 gfc_default_integer_kind,\
6174 for (j = 0; KINDS[j].kind != 0; j++) \
6175 gfc_constructor_append_expr (&expr->value.constructor, \
6176 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6177 KINDS[j].kind), NULL); \
6178 create_int_parameter_array (u->local_name[0] ? u->local_name \
6181 INTMOD_ISO_FORTRAN_ENV, \
6184 #include "iso-fortran-env.def"
6186 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6188 #include "iso-fortran-env.def"
6189 create_derived_type (u
->local_name
[0] ? u
->local_name
6191 mod
, INTMOD_ISO_FORTRAN_ENV
,
6195 #define NAMED_FUNCTION(a,b,c,d) \
6197 #include "iso-fortran-env.def"
6198 create_intrinsic_function (u
->local_name
[0] ? u
->local_name
6201 INTMOD_ISO_FORTRAN_ENV
, false,
6211 if (!found
&& !only_flag
)
6213 if ((gfc_option
.allow_std
& symbol
[i
].standard
) == 0)
6216 if ((gfc_option
.flag_default_integer
|| gfc_option
.flag_default_real
)
6217 && symbol
[i
].id
== ISOFORTRANENV_NUMERIC_STORAGE_SIZE
)
6218 gfc_warning_now ("Use of the NUMERIC_STORAGE_SIZE named constant "
6219 "from intrinsic module ISO_FORTRAN_ENV at %C is "
6220 "incompatible with option %s",
6221 gfc_option
.flag_default_integer
6222 ? "-fdefault-integer-8" : "-fdefault-real-8");
6224 switch (symbol
[i
].id
)
6226 #define NAMED_INTCST(a,b,c,d) \
6228 #include "iso-fortran-env.def"
6229 create_int_parameter (symbol
[i
].name
, symbol
[i
].value
, mod
,
6230 INTMOD_ISO_FORTRAN_ENV
, symbol
[i
].id
);
6233 #define NAMED_KINDARRAY(a,b,KINDS,d) \
6235 expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
6237 for (j = 0; KINDS[j].kind != 0; j++) \
6238 gfc_constructor_append_expr (&expr->value.constructor, \
6239 gfc_get_int_expr (gfc_default_integer_kind, NULL, \
6240 KINDS[j].kind), NULL); \
6241 create_int_parameter_array (symbol[i].name, j, expr, mod, \
6242 INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
6244 #include "iso-fortran-env.def"
6246 #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
6248 #include "iso-fortran-env.def"
6249 create_derived_type (symbol
[i
].name
, mod
, INTMOD_ISO_FORTRAN_ENV
,
6253 #define NAMED_FUNCTION(a,b,c,d) \
6255 #include "iso-fortran-env.def"
6256 create_intrinsic_function (symbol
[i
].name
, symbol
[i
].id
, mod
,
6257 INTMOD_ISO_FORTRAN_ENV
, false,
6267 for (u
= gfc_rename_list
; u
; u
= u
->next
)
6272 gfc_error ("Symbol '%s' referenced at %L not found in intrinsic "
6273 "module ISO_FORTRAN_ENV", u
->use_name
, &u
->where
);
6278 /* Process a USE directive. */
6281 gfc_use_module (gfc_use_list
*module
)
6286 gfc_symtree
*mod_symtree
;
6287 gfc_use_list
*use_stmt
;
6288 locus old_locus
= gfc_current_locus
;
6290 gfc_current_locus
= module
->where
;
6291 module_name
= module
->module_name
;
6292 gfc_rename_list
= module
->rename
;
6293 only_flag
= module
->only_flag
;
6295 filename
= XALLOCAVEC (char, strlen (module_name
) + strlen (MODULE_EXTENSION
)
6297 strcpy (filename
, module_name
);
6298 strcat (filename
, MODULE_EXTENSION
);
6300 /* First, try to find an non-intrinsic module, unless the USE statement
6301 specified that the module is intrinsic. */
6303 if (!module
->intrinsic
)
6304 module_fp
= gzopen_included_file (filename
, true, true);
6306 /* Then, see if it's an intrinsic one, unless the USE statement
6307 specified that the module is non-intrinsic. */
6308 if (module_fp
== NULL
&& !module
->non_intrinsic
)
6310 if (strcmp (module_name
, "iso_fortran_env") == 0
6311 && gfc_notify_std (GFC_STD_F2003
, "ISO_FORTRAN_ENV "
6312 "intrinsic module at %C"))
6314 use_iso_fortran_env_module ();
6315 free_rename (module
->rename
);
6316 module
->rename
= NULL
;
6317 gfc_current_locus
= old_locus
;
6318 module
->intrinsic
= true;
6322 if (strcmp (module_name
, "iso_c_binding") == 0
6323 && gfc_notify_std (GFC_STD_F2003
, "ISO_C_BINDING module at %C"))
6325 import_iso_c_binding_module();
6326 free_rename (module
->rename
);
6327 module
->rename
= NULL
;
6328 gfc_current_locus
= old_locus
;
6329 module
->intrinsic
= true;
6333 module_fp
= gzopen_intrinsic_module (filename
);
6335 if (module_fp
== NULL
&& module
->intrinsic
)
6336 gfc_fatal_error ("Can't find an intrinsic module named '%s' at %C",
6340 if (module_fp
== NULL
)
6341 gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
6342 filename
, xstrerror (errno
));
6344 /* Check that we haven't already USEd an intrinsic module with the
6347 mod_symtree
= gfc_find_symtree (gfc_current_ns
->sym_root
, module_name
);
6348 if (mod_symtree
&& mod_symtree
->n
.sym
->attr
.intrinsic
)
6349 gfc_error ("Use of non-intrinsic module '%s' at %C conflicts with "
6350 "intrinsic module name used previously", module_name
);
6357 read_module_to_tmpbuf ();
6358 gzclose (module_fp
);
6360 /* Skip the first line of the module, after checking that this is
6361 a gfortran module file. */
6367 bad_module ("Unexpected end of module");
6370 if ((start
== 1 && strcmp (atom_name
, "GFORTRAN") != 0)
6371 || (start
== 2 && strcmp (atom_name
, " module") != 0))
6372 gfc_fatal_error ("File '%s' opened at %C is not a GNU Fortran"
6373 " module file", filename
);
6376 if (strcmp (atom_name
, " version") != 0
6377 || module_char () != ' '
6378 || parse_atom () != ATOM_STRING
6379 || strcmp (atom_string
, MOD_VERSION
))
6380 gfc_fatal_error ("Cannot read module file '%s' opened at %C,"
6381 " because it was created by a different"
6382 " version of GNU Fortran", filename
);
6391 /* Make sure we're not reading the same module that we may be building. */
6392 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
6393 if (p
->state
== COMP_MODULE
&& strcmp (p
->sym
->name
, module_name
) == 0)
6394 gfc_fatal_error ("Can't USE the same module we're building!");
6397 init_true_name_tree ();
6401 free_true_name (true_name_root
);
6402 true_name_root
= NULL
;
6404 free_pi_tree (pi_root
);
6407 XDELETEVEC (module_content
);
6408 module_content
= NULL
;
6410 use_stmt
= gfc_get_use_list ();
6411 *use_stmt
= *module
;
6412 use_stmt
->next
= gfc_current_ns
->use_stmts
;
6413 gfc_current_ns
->use_stmts
= use_stmt
;
6415 gfc_current_locus
= old_locus
;
6419 /* Remove duplicated intrinsic operators from the rename list. */
6422 rename_list_remove_duplicate (gfc_use_rename
*list
)
6424 gfc_use_rename
*seek
, *last
;
6426 for (; list
; list
= list
->next
)
6427 if (list
->op
!= INTRINSIC_USER
&& list
->op
!= INTRINSIC_NONE
)
6430 for (seek
= list
->next
; seek
; seek
= last
->next
)
6432 if (list
->op
== seek
->op
)
6434 last
->next
= seek
->next
;
6444 /* Process all USE directives. */
6447 gfc_use_modules (void)
6449 gfc_use_list
*next
, *seek
, *last
;
6451 for (next
= module_list
; next
; next
= next
->next
)
6453 bool non_intrinsic
= next
->non_intrinsic
;
6454 bool intrinsic
= next
->intrinsic
;
6455 bool neither
= !non_intrinsic
&& !intrinsic
;
6457 for (seek
= next
->next
; seek
; seek
= seek
->next
)
6459 if (next
->module_name
!= seek
->module_name
)
6462 if (seek
->non_intrinsic
)
6463 non_intrinsic
= true;
6464 else if (seek
->intrinsic
)
6470 if (intrinsic
&& neither
&& !non_intrinsic
)
6475 filename
= XALLOCAVEC (char,
6476 strlen (next
->module_name
)
6477 + strlen (MODULE_EXTENSION
) + 1);
6478 strcpy (filename
, next
->module_name
);
6479 strcat (filename
, MODULE_EXTENSION
);
6480 fp
= gfc_open_included_file (filename
, true, true);
6483 non_intrinsic
= true;
6489 for (seek
= next
->next
; seek
; seek
= last
->next
)
6491 if (next
->module_name
!= seek
->module_name
)
6497 if ((!next
->intrinsic
&& !seek
->intrinsic
)
6498 || (next
->intrinsic
&& seek
->intrinsic
)
6501 if (!seek
->only_flag
)
6502 next
->only_flag
= false;
6505 gfc_use_rename
*r
= seek
->rename
;
6508 r
->next
= next
->rename
;
6509 next
->rename
= seek
->rename
;
6511 last
->next
= seek
->next
;
6519 for (; module_list
; module_list
= next
)
6521 next
= module_list
->next
;
6522 rename_list_remove_duplicate (module_list
->rename
);
6523 gfc_use_module (module_list
);
6526 gfc_rename_list
= NULL
;
6531 gfc_free_use_stmts (gfc_use_list
*use_stmts
)
6534 for (; use_stmts
; use_stmts
= next
)
6536 gfc_use_rename
*next_rename
;
6538 for (; use_stmts
->rename
; use_stmts
->rename
= next_rename
)
6540 next_rename
= use_stmts
->rename
->next
;
6541 free (use_stmts
->rename
);
6543 next
= use_stmts
->next
;
6550 gfc_module_init_2 (void)
6552 last_atom
= ATOM_LPAREN
;
6553 gfc_rename_list
= NULL
;
6559 gfc_module_done_2 (void)
6561 free_rename (gfc_rename_list
);
6562 gfc_rename_list
= NULL
;