1 /* Handle modules, which amounts to loading and saving symbols and
2 their attendant structures.
3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 Free Software Foundation, Inc.
6 Contributed by Andy Vaught
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
25 sequence of atoms, which can be left or right parenthesis, names,
26 integers or strings. Parenthesis are always matched which allows
27 us to skip over sections at high speed without having to know
28 anything about the internal structure of the lists. A "name" is
29 usually a fortran 95 identifier, but can also start with '@' in
30 order to reference a hidden symbol.
32 The first line of a module is an informational message about what
33 created the module, the file it came from and when it was created.
34 The second line is a warning for people not to edit the module.
35 The rest of the module looks like:
37 ( ( <Interface info for UPLUS> )
38 ( <Interface info for UMINUS> )
41 ( ( <name of operator interface> <module of op interface> <i/f1> ... )
44 ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
47 ( ( <common name> <symbol> <saved flag>)
53 ( <Symbol Number (in no particular order)>
55 <Module name of symbol>
56 ( <symbol information> )
65 In general, symbols refer to other symbols by their symbol number,
66 which are zero based. Symbols are written to the module in no
74 #include "parse.h" /* FIXME */
76 #include "constructor.h"
80 #define MODULE_EXTENSION ".mod"
82 /* Don't put any single quote (') in MOD_VERSION,
83 if yout want it to be recognized. */
84 #define MOD_VERSION "9"
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 FILE for the module we're reading or writing. */
185 static FILE *module_fp;
187 /* MD5 context structure. */
188 static struct md5_ctx ctx;
190 /* The name of the module we're reading (USE'ing) or writing. */
191 static const char *module_name;
192 static gfc_use_list *module_list;
194 static int module_line, module_column, only_flag;
195 static int prev_module_line, prev_module_column, prev_character;
198 { IO_INPUT, IO_OUTPUT }
201 static gfc_use_rename *gfc_rename_list;
202 static pointer_info *pi_root;
203 static int symbol_number; /* Counter for assigning symbol numbers */
205 /* Tells mio_expr_ref to make symbols for unused equivalence members. */
206 static bool in_load_equiv;
210 /*****************************************************************/
212 /* Pointer/integer conversion. Pointers between structures are stored
213 as integers in the module file. The next couple of subroutines
214 handle this translation for reading and writing. */
216 /* Recursively free the tree of pointer structures. */
219 free_pi_tree (pointer_info *p)
224 if (p->fixup != NULL)
225 gfc_internal_error ("free_pi_tree(): Unresolved fixup");
227 free_pi_tree (p->left);
228 free_pi_tree (p->right);
230 if (iomode == IO_INPUT)
232 XDELETEVEC (p->u.rsym.true_name);
233 XDELETEVEC (p->u.rsym.module);
234 XDELETEVEC (p->u.rsym.binding_label);
241 /* Compare pointers when searching by pointer. Used when writing a
245 compare_pointers (void *_sn1, void *_sn2)
247 pointer_info *sn1, *sn2;
249 sn1 = (pointer_info *) _sn1;
250 sn2 = (pointer_info *) _sn2;
252 if (sn1->u.pointer < sn2->u.pointer)
254 if (sn1->u.pointer > sn2->u.pointer)
261 /* Compare integers when searching by integer. Used when reading a
265 compare_integers (void *_sn1, void *_sn2)
267 pointer_info *sn1, *sn2;
269 sn1 = (pointer_info *) _sn1;
270 sn2 = (pointer_info *) _sn2;
272 if (sn1->integer < sn2->integer)
274 if (sn1->integer > sn2->integer)
281 /* Initialize the pointer_info tree. */
290 compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
292 /* Pointer 0 is the NULL pointer. */
293 p = gfc_get_pointer_info ();
298 gfc_insert_bbt (&pi_root, p, compare);
300 /* Pointer 1 is the current namespace. */
301 p = gfc_get_pointer_info ();
302 p->u.pointer = gfc_current_ns;
304 p->type = P_NAMESPACE;
306 gfc_insert_bbt (&pi_root, p, compare);
312 /* During module writing, call here with a pointer to something,
313 returning the pointer_info node. */
315 static pointer_info *
316 find_pointer (void *gp)
323 if (p->u.pointer == gp)
325 p = (gp < p->u.pointer) ? p->left : p->right;
332 /* Given a pointer while writing, returns the pointer_info tree node,
333 creating it if it doesn't exist. */
335 static pointer_info *
336 get_pointer (void *gp)
340 p = find_pointer (gp);
344 /* Pointer doesn't have an integer. Give it one. */
345 p = gfc_get_pointer_info ();
348 p->integer = symbol_number++;
350 gfc_insert_bbt (&pi_root, p, compare_pointers);
356 /* Given an integer during reading, find it in the pointer_info tree,
357 creating the node if not found. */
359 static pointer_info *
360 get_integer (int integer)
370 c = compare_integers (&t, p);
374 p = (c < 0) ? p->left : p->right;
380 p = gfc_get_pointer_info ();
381 p->integer = integer;
384 gfc_insert_bbt (&pi_root, p, compare_integers);
390 /* Resolve any fixups using a known pointer. */
393 resolve_fixups (fixup_t *f, void *gp)
406 /* Convert a string such that it starts with a lower-case character. Used
407 to convert the symtree name of a derived-type to the symbol name or to
408 the name of the associated generic function. */
411 dt_lower_string (const char *name)
413 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
414 return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
416 return gfc_get_string (name);
420 /* Convert a string such that it starts with an upper-case character. Used to
421 return the symtree-name for a derived type; the symbol name itself and the
422 symtree/symbol name of the associated generic function start with a lower-
426 dt_upper_string (const char *name)
428 if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
429 return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
431 return gfc_get_string (name);
434 /* Call here during module reading when we know what pointer to
435 associate with an integer. Any fixups that exist are resolved at
439 associate_integer_pointer (pointer_info *p, void *gp)
441 if (p->u.pointer != NULL)
442 gfc_internal_error ("associate_integer_pointer(): Already associated");
446 resolve_fixups (p->fixup, gp);
452 /* During module reading, given an integer and a pointer to a pointer,
453 either store the pointer from an already-known value or create a
454 fixup structure in order to store things later. Returns zero if
455 the reference has been actually stored, or nonzero if the reference
456 must be fixed later (i.e., associate_integer_pointer must be called
457 sometime later. Returns the pointer_info structure. */
459 static pointer_info *
460 add_fixup (int integer, void *gp)
466 p = get_integer (integer);
468 if (p->integer == 0 || p->u.pointer != NULL)
471 *cp = (char *) p->u.pointer;
480 f->pointer = (void **) gp;
487 /*****************************************************************/
489 /* Parser related subroutines */
491 /* Free the rename list left behind by a USE statement. */
494 free_rename (gfc_use_rename *list)
496 gfc_use_rename *next;
498 for (; list; list = next)
506 /* Match a USE statement. */
511 char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
512 gfc_use_rename *tail = NULL, *new_use;
513 interface_type type, type2;
516 gfc_use_list *use_list;
518 use_list = gfc_get_use_list ();
520 if (gfc_match (" , ") == MATCH_YES)
522 if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
524 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: module "
525 "nature in USE statement at %C") == FAILURE)
528 if (strcmp (module_nature, "intrinsic") == 0)
529 use_list->intrinsic = true;
532 if (strcmp (module_nature, "non_intrinsic") == 0)
533 use_list->non_intrinsic = true;
536 gfc_error ("Module nature in USE statement at %C shall "
537 "be either INTRINSIC or NON_INTRINSIC");
544 /* Help output a better error message than "Unclassifiable
546 gfc_match (" %n", module_nature);
547 if (strcmp (module_nature, "intrinsic") == 0
548 || strcmp (module_nature, "non_intrinsic") == 0)
549 gfc_error ("\"::\" was expected after module nature at %C "
550 "but was not found");
557 m = gfc_match (" ::");
558 if (m == MATCH_YES &&
559 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: "
560 "\"USE :: module\" at %C") == FAILURE)
565 m = gfc_match ("% ");
574 use_list->where = gfc_current_locus;
576 m = gfc_match_name (name);
583 use_list->module_name = gfc_get_string (name);
585 if (gfc_match_eos () == MATCH_YES)
588 if (gfc_match_char (',') != MATCH_YES)
591 if (gfc_match (" only :") == MATCH_YES)
592 use_list->only_flag = true;
594 if (gfc_match_eos () == MATCH_YES)
599 /* Get a new rename struct and add it to the rename list. */
600 new_use = gfc_get_use_rename ();
601 new_use->where = gfc_current_locus;
604 if (use_list->rename == NULL)
605 use_list->rename = new_use;
607 tail->next = new_use;
610 /* See what kind of interface we're dealing with. Assume it is
612 new_use->op = INTRINSIC_NONE;
613 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
618 case INTERFACE_NAMELESS:
619 gfc_error ("Missing generic specification in USE statement at %C");
622 case INTERFACE_USER_OP:
623 case INTERFACE_GENERIC:
624 m = gfc_match (" =>");
626 if (type == INTERFACE_USER_OP && m == MATCH_YES
627 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Renaming "
628 "operators in USE statements at %C")
632 if (type == INTERFACE_USER_OP)
633 new_use->op = INTRINSIC_USER;
635 if (use_list->only_flag)
638 strcpy (new_use->use_name, name);
641 strcpy (new_use->local_name, name);
642 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
647 if (m == MATCH_ERROR)
655 strcpy (new_use->local_name, name);
657 m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
662 if (m == MATCH_ERROR)
666 if (strcmp (new_use->use_name, use_list->module_name) == 0
667 || strcmp (new_use->local_name, use_list->module_name) == 0)
669 gfc_error ("The name '%s' at %C has already been used as "
670 "an external module name.", use_list->module_name);
675 case INTERFACE_INTRINSIC_OP:
683 if (gfc_match_eos () == MATCH_YES)
685 if (gfc_match_char (',') != MATCH_YES)
692 gfc_use_list *last = module_list;
695 last->next = use_list;
698 module_list = use_list;
703 gfc_syntax_error (ST_USE);
706 free_rename (use_list->rename);
712 /* Given a name and a number, inst, return the inst name
713 under which to load this symbol. Returns NULL if this
714 symbol shouldn't be loaded. If inst is zero, returns
715 the number of instances of this name. If interface is
716 true, a user-defined operator is sought, otherwise only
717 non-operators are sought. */
720 find_use_name_n (const char *name, int *inst, bool interface)
723 const char *low_name = NULL;
726 /* For derived types. */
727 if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
728 low_name = dt_lower_string (name);
731 for (u = gfc_rename_list; u; u = u->next)
733 if ((!low_name && strcmp (u->use_name, name) != 0)
734 || (low_name && strcmp (u->use_name, low_name) != 0)
735 || (u->op == INTRINSIC_USER && !interface)
736 || (u->op != INTRINSIC_USER && interface))
749 return only_flag ? NULL : name;
755 if (u->local_name[0] == '\0')
757 return dt_upper_string (u->local_name);
760 return (u->local_name[0] != '\0') ? u->local_name : name;
764 /* Given a name, return the name under which to load this symbol.
765 Returns NULL if this symbol shouldn't be loaded. */
768 find_use_name (const char *name, bool interface)
771 return find_use_name_n (name, &i, interface);
775 /* Given a real name, return the number of use names associated with it. */
778 number_use_names (const char *name, bool interface)
781 find_use_name_n (name, &i, interface);
786 /* Try to find the operator in the current list. */
788 static gfc_use_rename *
789 find_use_operator (gfc_intrinsic_op op)
793 for (u = gfc_rename_list; u; u = u->next)
801 /*****************************************************************/
803 /* The next couple of subroutines maintain a tree used to avoid a
804 brute-force search for a combination of true name and module name.
805 While symtree names, the name that a particular symbol is known by
806 can changed with USE statements, we still have to keep track of the
807 true names to generate the correct reference, and also avoid
808 loading the same real symbol twice in a program unit.
810 When we start reading, the true name tree is built and maintained
811 as symbols are read. The tree is searched as we load new symbols
812 to see if it already exists someplace in the namespace. */
814 typedef struct true_name
816 BBT_HEADER (true_name);
822 static true_name *true_name_root;
825 /* Compare two true_name structures. */
828 compare_true_names (void *_t1, void *_t2)
833 t1 = (true_name *) _t1;
834 t2 = (true_name *) _t2;
836 c = ((t1->sym->module > t2->sym->module)
837 - (t1->sym->module < t2->sym->module));
841 return strcmp (t1->name, t2->name);
845 /* Given a true name, search the true name tree to see if it exists
846 within the main namespace. */
849 find_true_name (const char *name, const char *module)
855 t.name = gfc_get_string (name);
857 sym.module = gfc_get_string (module);
865 c = compare_true_names ((void *) (&t), (void *) p);
869 p = (c < 0) ? p->left : p->right;
876 /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
879 add_true_name (gfc_symbol *sym)
883 t = XCNEW (true_name);
885 if (sym->attr.flavor == FL_DERIVED)
886 t->name = dt_upper_string (sym->name);
890 gfc_insert_bbt (&true_name_root, t, compare_true_names);
894 /* Recursive function to build the initial true name tree by
895 recursively traversing the current namespace. */
898 build_tnt (gfc_symtree *st)
904 build_tnt (st->left);
905 build_tnt (st->right);
907 if (st->n.sym->attr.flavor == FL_DERIVED)
908 name = dt_upper_string (st->n.sym->name);
910 name = st->n.sym->name;
912 if (find_true_name (name, st->n.sym->module) != NULL)
915 add_true_name (st->n.sym);
919 /* Initialize the true name tree with the current namespace. */
922 init_true_name_tree (void)
924 true_name_root = NULL;
925 build_tnt (gfc_current_ns->sym_root);
929 /* Recursively free a true name tree node. */
932 free_true_name (true_name *t)
936 free_true_name (t->left);
937 free_true_name (t->right);
943 /*****************************************************************/
945 /* Module reading and writing. */
949 ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
953 static atom_type last_atom;
956 /* The name buffer must be at least as long as a symbol name. Right
957 now it's not clear how we're going to store numeric constants--
958 probably as a hexadecimal string, since this will allow the exact
959 number to be preserved (this can't be done by a decimal
960 representation). Worry about that later. TODO! */
962 #define MAX_ATOM_SIZE 100
965 static char *atom_string, atom_name[MAX_ATOM_SIZE];
968 /* Report problems with a module. Error reporting is not very
969 elaborate, since this sorts of errors shouldn't really happen.
970 This subroutine never returns. */
972 static void bad_module (const char *) ATTRIBUTE_NORETURN;
975 bad_module (const char *msgid)
982 gfc_fatal_error ("Reading module %s at line %d column %d: %s",
983 module_name, module_line, module_column, msgid);
986 gfc_fatal_error ("Writing module %s at line %d column %d: %s",
987 module_name, module_line, module_column, msgid);
990 gfc_fatal_error ("Module %s at line %d column %d: %s",
991 module_name, module_line, module_column, msgid);
997 /* Set the module's input pointer. */
1000 set_module_locus (module_locus *m)
1002 module_column = m->column;
1003 module_line = m->line;
1004 fsetpos (module_fp, &m->pos);
1008 /* Get the module's input pointer so that we can restore it later. */
1011 get_module_locus (module_locus *m)
1013 m->column = module_column;
1014 m->line = module_line;
1015 fgetpos (module_fp, &m->pos);
1019 /* Get the next character in the module, updating our reckoning of
1027 c = getc (module_fp);
1030 bad_module ("Unexpected EOF");
1032 prev_module_line = module_line;
1033 prev_module_column = module_column;
1046 /* Unget a character while remembering the line and column. Works for
1047 a single character only. */
1050 module_unget_char (void)
1052 module_line = prev_module_line;
1053 module_column = prev_module_column;
1054 ungetc (prev_character, module_fp);
1057 /* Parse a string constant. The delimiter is guaranteed to be a
1067 atom_string = XNEWVEC (char, cursz);
1075 int c2 = module_char ();
1078 module_unget_char ();
1086 atom_string = XRESIZEVEC (char, atom_string, cursz);
1088 atom_string[len] = c;
1092 atom_string = XRESIZEVEC (char, atom_string, len + 1);
1093 atom_string[len] = '\0'; /* C-style string for debug purposes. */
1097 /* Parse a small integer. */
1100 parse_integer (int c)
1109 module_unget_char ();
1113 atom_int = 10 * atom_int + c - '0';
1114 if (atom_int > 99999999)
1115 bad_module ("Integer overflow");
1137 if (!ISALNUM (c) && c != '_' && c != '-')
1139 module_unget_char ();
1144 if (++len > GFC_MAX_SYMBOL_LEN)
1145 bad_module ("Name too long");
1153 /* Read the next atom in the module's input stream. */
1164 while (c == ' ' || c == '\r' || c == '\n');
1189 return ATOM_INTEGER;
1247 bad_module ("Bad name");
1254 /* Peek at the next atom on the input. */
1265 while (c == ' ' || c == '\r' || c == '\n');
1270 module_unget_char ();
1274 module_unget_char ();
1278 module_unget_char ();
1291 module_unget_char ();
1292 return ATOM_INTEGER;
1346 module_unget_char ();
1350 bad_module ("Bad name");
1355 /* Read the next atom from the input, requiring that it be a
1359 require_atom (atom_type type)
1365 column = module_column;
1374 p = _("Expected name");
1377 p = _("Expected left parenthesis");
1380 p = _("Expected right parenthesis");
1383 p = _("Expected integer");
1386 p = _("Expected string");
1389 gfc_internal_error ("require_atom(): bad atom type required");
1392 module_column = column;
1399 /* Given a pointer to an mstring array, require that the current input
1400 be one of the strings in the array. We return the enum value. */
1403 find_enum (const mstring *m)
1407 i = gfc_string2code (m, atom_name);
1411 bad_module ("find_enum(): Enum not found");
1417 /* Read a string. The caller is responsible for freeing. */
1423 require_atom (ATOM_STRING);
1430 /**************** Module output subroutines ***************************/
1432 /* Output a character to a module file. */
1435 write_char (char out)
1437 if (putc (out, module_fp) == EOF)
1438 gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
1440 /* Add this to our MD5. */
1441 md5_process_bytes (&out, sizeof (out), &ctx);
1453 /* Write an atom to a module. The line wrapping isn't perfect, but it
1454 should work most of the time. This isn't that big of a deal, since
1455 the file really isn't meant to be read by people anyway. */
1458 write_atom (atom_type atom, const void *v)
1468 p = (const char *) v;
1480 i = *((const int *) v);
1482 gfc_internal_error ("write_atom(): Writing negative integer");
1484 sprintf (buffer, "%d", i);
1489 gfc_internal_error ("write_atom(): Trying to write dab atom");
1493 if(p == NULL || *p == '\0')
1498 if (atom != ATOM_RPAREN)
1500 if (module_column + len > 72)
1505 if (last_atom != ATOM_LPAREN && module_column != 1)
1510 if (atom == ATOM_STRING)
1513 while (p != NULL && *p)
1515 if (atom == ATOM_STRING && *p == '\'')
1520 if (atom == ATOM_STRING)
1528 /***************** Mid-level I/O subroutines *****************/
1530 /* These subroutines let their caller read or write atoms without
1531 caring about which of the two is actually happening. This lets a
1532 subroutine concentrate on the actual format of the data being
1535 static void mio_expr (gfc_expr **);
1536 pointer_info *mio_symbol_ref (gfc_symbol **);
1537 pointer_info *mio_interface_rest (gfc_interface **);
1538 static void mio_symtree_ref (gfc_symtree **);
1540 /* Read or write an enumerated value. On writing, we return the input
1541 value for the convenience of callers. We avoid using an integer
1542 pointer because enums are sometimes inside bitfields. */
1545 mio_name (int t, const mstring *m)
1547 if (iomode == IO_OUTPUT)
1548 write_atom (ATOM_NAME, gfc_code2string (m, t));
1551 require_atom (ATOM_NAME);
1558 /* Specialization of mio_name. */
1560 #define DECL_MIO_NAME(TYPE) \
1561 static inline TYPE \
1562 MIO_NAME(TYPE) (TYPE t, const mstring *m) \
1564 return (TYPE) mio_name ((int) t, m); \
1566 #define MIO_NAME(TYPE) mio_name_##TYPE
1571 if (iomode == IO_OUTPUT)
1572 write_atom (ATOM_LPAREN, NULL);
1574 require_atom (ATOM_LPAREN);
1581 if (iomode == IO_OUTPUT)
1582 write_atom (ATOM_RPAREN, NULL);
1584 require_atom (ATOM_RPAREN);
1589 mio_integer (int *ip)
1591 if (iomode == IO_OUTPUT)
1592 write_atom (ATOM_INTEGER, ip);
1595 require_atom (ATOM_INTEGER);
1601 /* Read or write a gfc_intrinsic_op value. */
1604 mio_intrinsic_op (gfc_intrinsic_op* op)
1606 /* FIXME: Would be nicer to do this via the operators symbolic name. */
1607 if (iomode == IO_OUTPUT)
1609 int converted = (int) *op;
1610 write_atom (ATOM_INTEGER, &converted);
1614 require_atom (ATOM_INTEGER);
1615 *op = (gfc_intrinsic_op) atom_int;
1620 /* Read or write a character pointer that points to a string on the heap. */
1623 mio_allocated_string (const char *s)
1625 if (iomode == IO_OUTPUT)
1627 write_atom (ATOM_STRING, s);
1632 require_atom (ATOM_STRING);
1638 /* Functions for quoting and unquoting strings. */
1641 quote_string (const gfc_char_t *s, const size_t slength)
1643 const gfc_char_t *p;
1647 /* Calculate the length we'll need: a backslash takes two ("\\"),
1648 non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
1649 for (p = s, i = 0; i < slength; p++, i++)
1653 else if (!gfc_wide_is_printable (*p))
1659 q = res = XCNEWVEC (char, len + 1);
1660 for (p = s, i = 0; i < slength; p++, i++)
1663 *q++ = '\\', *q++ = '\\';
1664 else if (!gfc_wide_is_printable (*p))
1666 sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
1667 (unsigned HOST_WIDE_INT) *p);
1671 *q++ = (unsigned char) *p;
1679 unquote_string (const char *s)
1685 for (p = s, len = 0; *p; p++, len++)
1692 else if (p[1] == 'U')
1693 p += 9; /* That is a "\U????????". */
1695 gfc_internal_error ("unquote_string(): got bad string");
1698 res = gfc_get_wide_string (len + 1);
1699 for (i = 0, p = s; i < len; i++, p++)
1704 res[i] = (unsigned char) *p;
1705 else if (p[1] == '\\')
1707 res[i] = (unsigned char) '\\';
1712 /* We read the 8-digits hexadecimal constant that follows. */
1717 gcc_assert (p[1] == 'U');
1718 for (j = 0; j < 8; j++)
1721 gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
1735 /* Read or write a character pointer that points to a wide string on the
1736 heap, performing quoting/unquoting of nonprintable characters using the
1737 form \U???????? (where each ? is a hexadecimal digit).
1738 Length is the length of the string, only known and used in output mode. */
1740 static const gfc_char_t *
1741 mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
1743 if (iomode == IO_OUTPUT)
1745 char *quoted = quote_string (s, length);
1746 write_atom (ATOM_STRING, quoted);
1752 gfc_char_t *unquoted;
1754 require_atom (ATOM_STRING);
1755 unquoted = unquote_string (atom_string);
1762 /* Read or write a string that is in static memory. */
1765 mio_pool_string (const char **stringp)
1767 /* TODO: one could write the string only once, and refer to it via a
1770 /* As a special case we have to deal with a NULL string. This
1771 happens for the 'module' member of 'gfc_symbol's that are not in a
1772 module. We read / write these as the empty string. */
1773 if (iomode == IO_OUTPUT)
1775 const char *p = *stringp == NULL ? "" : *stringp;
1776 write_atom (ATOM_STRING, p);
1780 require_atom (ATOM_STRING);
1781 *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
1787 /* Read or write a string that is inside of some already-allocated
1791 mio_internal_string (char *string)
1793 if (iomode == IO_OUTPUT)
1794 write_atom (ATOM_STRING, string);
1797 require_atom (ATOM_STRING);
1798 strcpy (string, atom_string);
1805 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
1806 AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
1807 AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
1808 AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
1809 AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
1810 AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
1811 AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
1812 AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
1813 AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
1814 AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
1819 static const mstring attr_bits[] =
1821 minit ("ALLOCATABLE", AB_ALLOCATABLE),
1822 minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
1823 minit ("DIMENSION", AB_DIMENSION),
1824 minit ("CODIMENSION", AB_CODIMENSION),
1825 minit ("CONTIGUOUS", AB_CONTIGUOUS),
1826 minit ("EXTERNAL", AB_EXTERNAL),
1827 minit ("INTRINSIC", AB_INTRINSIC),
1828 minit ("OPTIONAL", AB_OPTIONAL),
1829 minit ("POINTER", AB_POINTER),
1830 minit ("VOLATILE", AB_VOLATILE),
1831 minit ("TARGET", AB_TARGET),
1832 minit ("THREADPRIVATE", AB_THREADPRIVATE),
1833 minit ("DUMMY", AB_DUMMY),
1834 minit ("RESULT", AB_RESULT),
1835 minit ("DATA", AB_DATA),
1836 minit ("IN_NAMELIST", AB_IN_NAMELIST),
1837 minit ("IN_COMMON", AB_IN_COMMON),
1838 minit ("FUNCTION", AB_FUNCTION),
1839 minit ("SUBROUTINE", AB_SUBROUTINE),
1840 minit ("SEQUENCE", AB_SEQUENCE),
1841 minit ("ELEMENTAL", AB_ELEMENTAL),
1842 minit ("PURE", AB_PURE),
1843 minit ("RECURSIVE", AB_RECURSIVE),
1844 minit ("GENERIC", AB_GENERIC),
1845 minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
1846 minit ("CRAY_POINTER", AB_CRAY_POINTER),
1847 minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
1848 minit ("IS_BIND_C", AB_IS_BIND_C),
1849 minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
1850 minit ("IS_ISO_C", AB_IS_ISO_C),
1851 minit ("VALUE", AB_VALUE),
1852 minit ("ALLOC_COMP", AB_ALLOC_COMP),
1853 minit ("COARRAY_COMP", AB_COARRAY_COMP),
1854 minit ("LOCK_COMP", AB_LOCK_COMP),
1855 minit ("POINTER_COMP", AB_POINTER_COMP),
1856 minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
1857 minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
1858 minit ("ZERO_COMP", AB_ZERO_COMP),
1859 minit ("PROTECTED", AB_PROTECTED),
1860 minit ("ABSTRACT", AB_ABSTRACT),
1861 minit ("IS_CLASS", AB_IS_CLASS),
1862 minit ("PROCEDURE", AB_PROCEDURE),
1863 minit ("PROC_POINTER", AB_PROC_POINTER),
1864 minit ("VTYPE", AB_VTYPE),
1865 minit ("VTAB", AB_VTAB),
1866 minit ("CLASS_POINTER", AB_CLASS_POINTER),
1867 minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
1871 /* For binding attributes. */
1872 static const mstring binding_passing[] =
1875 minit ("NOPASS", 1),
1878 static const mstring binding_overriding[] =
1880 minit ("OVERRIDABLE", 0),
1881 minit ("NON_OVERRIDABLE", 1),
1882 minit ("DEFERRED", 2),
1885 static const mstring binding_generic[] =
1887 minit ("SPECIFIC", 0),
1888 minit ("GENERIC", 1),
1891 static const mstring binding_ppc[] =
1893 minit ("NO_PPC", 0),
1898 /* Specialization of mio_name. */
1899 DECL_MIO_NAME (ab_attribute)
1900 DECL_MIO_NAME (ar_type)
1901 DECL_MIO_NAME (array_type)
1903 DECL_MIO_NAME (expr_t)
1904 DECL_MIO_NAME (gfc_access)
1905 DECL_MIO_NAME (gfc_intrinsic_op)
1906 DECL_MIO_NAME (ifsrc)
1907 DECL_MIO_NAME (save_state)
1908 DECL_MIO_NAME (procedure_type)
1909 DECL_MIO_NAME (ref_type)
1910 DECL_MIO_NAME (sym_flavor)
1911 DECL_MIO_NAME (sym_intent)
1912 #undef DECL_MIO_NAME
1914 /* Symbol attributes are stored in list with the first three elements
1915 being the enumerated fields, while the remaining elements (if any)
1916 indicate the individual attribute bits. The access field is not
1917 saved-- it controls what symbols are exported when a module is
1921 mio_symbol_attribute (symbol_attribute *attr)
1924 unsigned ext_attr,extension_level;
1928 attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
1929 attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
1930 attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
1931 attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
1932 attr->save = MIO_NAME (save_state) (attr->save, save_status);
1934 ext_attr = attr->ext_attr;
1935 mio_integer ((int *) &ext_attr);
1936 attr->ext_attr = ext_attr;
1938 extension_level = attr->extension;
1939 mio_integer ((int *) &extension_level);
1940 attr->extension = extension_level;
1942 if (iomode == IO_OUTPUT)
1944 if (attr->allocatable)
1945 MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
1946 if (attr->asynchronous)
1947 MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
1948 if (attr->dimension)
1949 MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
1950 if (attr->codimension)
1951 MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
1952 if (attr->contiguous)
1953 MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
1955 MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
1956 if (attr->intrinsic)
1957 MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
1959 MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
1961 MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
1962 if (attr->class_pointer)
1963 MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
1964 if (attr->is_protected)
1965 MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
1967 MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
1968 if (attr->volatile_)
1969 MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
1971 MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
1972 if (attr->threadprivate)
1973 MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
1975 MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
1977 MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
1978 /* We deliberately don't preserve the "entry" flag. */
1981 MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
1982 if (attr->in_namelist)
1983 MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
1984 if (attr->in_common)
1985 MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
1988 MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
1989 if (attr->subroutine)
1990 MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
1992 MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
1994 MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
1997 MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
1998 if (attr->elemental)
1999 MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
2001 MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
2002 if (attr->implicit_pure)
2003 MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
2004 if (attr->recursive)
2005 MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
2006 if (attr->always_explicit)
2007 MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
2008 if (attr->cray_pointer)
2009 MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
2010 if (attr->cray_pointee)
2011 MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
2012 if (attr->is_bind_c)
2013 MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
2014 if (attr->is_c_interop)
2015 MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
2017 MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
2018 if (attr->alloc_comp)
2019 MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
2020 if (attr->pointer_comp)
2021 MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
2022 if (attr->proc_pointer_comp)
2023 MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
2024 if (attr->private_comp)
2025 MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
2026 if (attr->coarray_comp)
2027 MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
2028 if (attr->lock_comp)
2029 MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
2030 if (attr->zero_comp)
2031 MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
2033 MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
2034 if (attr->procedure)
2035 MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
2036 if (attr->proc_pointer)
2037 MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
2039 MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
2041 MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
2051 if (t == ATOM_RPAREN)
2054 bad_module ("Expected attribute bit name");
2056 switch ((ab_attribute) find_enum (attr_bits))
2058 case AB_ALLOCATABLE:
2059 attr->allocatable = 1;
2061 case AB_ASYNCHRONOUS:
2062 attr->asynchronous = 1;
2065 attr->dimension = 1;
2067 case AB_CODIMENSION:
2068 attr->codimension = 1;
2071 attr->contiguous = 1;
2077 attr->intrinsic = 1;
2085 case AB_CLASS_POINTER:
2086 attr->class_pointer = 1;
2089 attr->is_protected = 1;
2095 attr->volatile_ = 1;
2100 case AB_THREADPRIVATE:
2101 attr->threadprivate = 1;
2112 case AB_IN_NAMELIST:
2113 attr->in_namelist = 1;
2116 attr->in_common = 1;
2122 attr->subroutine = 1;
2134 attr->elemental = 1;
2139 case AB_IMPLICIT_PURE:
2140 attr->implicit_pure = 1;
2143 attr->recursive = 1;
2145 case AB_ALWAYS_EXPLICIT:
2146 attr->always_explicit = 1;
2148 case AB_CRAY_POINTER:
2149 attr->cray_pointer = 1;
2151 case AB_CRAY_POINTEE:
2152 attr->cray_pointee = 1;
2155 attr->is_bind_c = 1;
2157 case AB_IS_C_INTEROP:
2158 attr->is_c_interop = 1;
2164 attr->alloc_comp = 1;
2166 case AB_COARRAY_COMP:
2167 attr->coarray_comp = 1;
2170 attr->lock_comp = 1;
2172 case AB_POINTER_COMP:
2173 attr->pointer_comp = 1;
2175 case AB_PROC_POINTER_COMP:
2176 attr->proc_pointer_comp = 1;
2178 case AB_PRIVATE_COMP:
2179 attr->private_comp = 1;
2182 attr->zero_comp = 1;
2188 attr->procedure = 1;
2190 case AB_PROC_POINTER:
2191 attr->proc_pointer = 1;
2205 static const mstring bt_types[] = {
2206 minit ("INTEGER", BT_INTEGER),
2207 minit ("REAL", BT_REAL),
2208 minit ("COMPLEX", BT_COMPLEX),
2209 minit ("LOGICAL", BT_LOGICAL),
2210 minit ("CHARACTER", BT_CHARACTER),
2211 minit ("DERIVED", BT_DERIVED),
2212 minit ("CLASS", BT_CLASS),
2213 minit ("PROCEDURE", BT_PROCEDURE),
2214 minit ("UNKNOWN", BT_UNKNOWN),
2215 minit ("VOID", BT_VOID),
2221 mio_charlen (gfc_charlen **clp)
2227 if (iomode == IO_OUTPUT)
2231 mio_expr (&cl->length);
2235 if (peek_atom () != ATOM_RPAREN)
2237 cl = gfc_new_charlen (gfc_current_ns, NULL);
2238 mio_expr (&cl->length);
2247 /* See if a name is a generated name. */
2250 check_unique_name (const char *name)
2252 return *name == '@';
2257 mio_typespec (gfc_typespec *ts)
2261 ts->type = MIO_NAME (bt) (ts->type, bt_types);
2263 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
2264 mio_integer (&ts->kind);
2266 mio_symbol_ref (&ts->u.derived);
2268 mio_symbol_ref (&ts->interface);
2270 /* Add info for C interop and is_iso_c. */
2271 mio_integer (&ts->is_c_interop);
2272 mio_integer (&ts->is_iso_c);
2274 /* If the typespec is for an identifier either from iso_c_binding, or
2275 a constant that was initialized to an identifier from it, use the
2276 f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
2278 ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
2280 ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
2282 if (ts->type != BT_CHARACTER)
2284 /* ts->u.cl is only valid for BT_CHARACTER. */
2289 mio_charlen (&ts->u.cl);
2291 /* So as not to disturb the existing API, use an ATOM_NAME to
2292 transmit deferred characteristic for characters (F2003). */
2293 if (iomode == IO_OUTPUT)
2295 if (ts->type == BT_CHARACTER && ts->deferred)
2296 write_atom (ATOM_NAME, "DEFERRED_CL");
2298 else if (peek_atom () != ATOM_RPAREN)
2300 if (parse_atom () != ATOM_NAME)
2301 bad_module ("Expected string");
2309 static const mstring array_spec_types[] = {
2310 minit ("EXPLICIT", AS_EXPLICIT),
2311 minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
2312 minit ("DEFERRED", AS_DEFERRED),
2313 minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
2319 mio_array_spec (gfc_array_spec **asp)
2326 if (iomode == IO_OUTPUT)
2334 if (peek_atom () == ATOM_RPAREN)
2340 *asp = as = gfc_get_array_spec ();
2343 mio_integer (&as->rank);
2344 mio_integer (&as->corank);
2345 as->type = MIO_NAME (array_type) (as->type, array_spec_types);
2347 if (iomode == IO_INPUT && as->corank)
2348 as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
2350 for (i = 0; i < as->rank + as->corank; i++)
2352 mio_expr (&as->lower[i]);
2353 mio_expr (&as->upper[i]);
2361 /* Given a pointer to an array reference structure (which lives in a
2362 gfc_ref structure), find the corresponding array specification
2363 structure. Storing the pointer in the ref structure doesn't quite
2364 work when loading from a module. Generating code for an array
2365 reference also needs more information than just the array spec. */
2367 static const mstring array_ref_types[] = {
2368 minit ("FULL", AR_FULL),
2369 minit ("ELEMENT", AR_ELEMENT),
2370 minit ("SECTION", AR_SECTION),
2376 mio_array_ref (gfc_array_ref *ar)
2381 ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
2382 mio_integer (&ar->dimen);
2390 for (i = 0; i < ar->dimen; i++)
2391 mio_expr (&ar->start[i]);
2396 for (i = 0; i < ar->dimen; i++)
2398 mio_expr (&ar->start[i]);
2399 mio_expr (&ar->end[i]);
2400 mio_expr (&ar->stride[i]);
2406 gfc_internal_error ("mio_array_ref(): Unknown array ref");
2409 /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
2410 we can't call mio_integer directly. Instead loop over each element
2411 and cast it to/from an integer. */
2412 if (iomode == IO_OUTPUT)
2414 for (i = 0; i < ar->dimen; i++)
2416 int tmp = (int)ar->dimen_type[i];
2417 write_atom (ATOM_INTEGER, &tmp);
2422 for (i = 0; i < ar->dimen; i++)
2424 require_atom (ATOM_INTEGER);
2425 ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
2429 if (iomode == IO_INPUT)
2431 ar->where = gfc_current_locus;
2433 for (i = 0; i < ar->dimen; i++)
2434 ar->c_where[i] = gfc_current_locus;
2441 /* Saves or restores a pointer. The pointer is converted back and
2442 forth from an integer. We return the pointer_info pointer so that
2443 the caller can take additional action based on the pointer type. */
2445 static pointer_info *
2446 mio_pointer_ref (void *gp)
2450 if (iomode == IO_OUTPUT)
2452 p = get_pointer (*((char **) gp));
2453 write_atom (ATOM_INTEGER, &p->integer);
2457 require_atom (ATOM_INTEGER);
2458 p = add_fixup (atom_int, gp);
2465 /* Save and load references to components that occur within
2466 expressions. We have to describe these references by a number and
2467 by name. The number is necessary for forward references during
2468 reading, and the name is necessary if the symbol already exists in
2469 the namespace and is not loaded again. */
2472 mio_component_ref (gfc_component **cp)
2476 p = mio_pointer_ref (cp);
2477 if (p->type == P_UNKNOWN)
2478 p->type = P_COMPONENT;
2482 static void mio_namespace_ref (gfc_namespace **nsp);
2483 static void mio_formal_arglist (gfc_formal_arglist **formal);
2484 static void mio_typebound_proc (gfc_typebound_proc** proc);
2487 mio_component (gfc_component *c, int vtype)
2491 gfc_formal_arglist *formal;
2495 if (iomode == IO_OUTPUT)
2497 p = get_pointer (c);
2498 mio_integer (&p->integer);
2503 p = get_integer (n);
2504 associate_integer_pointer (p, c);
2507 if (p->type == P_UNKNOWN)
2508 p->type = P_COMPONENT;
2510 mio_pool_string (&c->name);
2511 mio_typespec (&c->ts);
2512 mio_array_spec (&c->as);
2514 mio_symbol_attribute (&c->attr);
2515 if (c->ts.type == BT_CLASS)
2516 c->attr.class_ok = 1;
2517 c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
2520 mio_expr (&c->initializer);
2522 if (c->attr.proc_pointer)
2524 if (iomode == IO_OUTPUT)
2527 while (formal && !formal->sym)
2528 formal = formal->next;
2531 mio_namespace_ref (&formal->sym->ns);
2533 mio_namespace_ref (&c->formal_ns);
2537 mio_namespace_ref (&c->formal_ns);
2538 /* TODO: if (c->formal_ns)
2540 c->formal_ns->proc_name = c;
2545 mio_formal_arglist (&c->formal);
2547 mio_typebound_proc (&c->tb);
2555 mio_component_list (gfc_component **cp, int vtype)
2557 gfc_component *c, *tail;
2561 if (iomode == IO_OUTPUT)
2563 for (c = *cp; c; c = c->next)
2564 mio_component (c, vtype);
2573 if (peek_atom () == ATOM_RPAREN)
2576 c = gfc_get_component ();
2577 mio_component (c, vtype);
2593 mio_actual_arg (gfc_actual_arglist *a)
2596 mio_pool_string (&a->name);
2597 mio_expr (&a->expr);
2603 mio_actual_arglist (gfc_actual_arglist **ap)
2605 gfc_actual_arglist *a, *tail;
2609 if (iomode == IO_OUTPUT)
2611 for (a = *ap; a; a = a->next)
2621 if (peek_atom () != ATOM_LPAREN)
2624 a = gfc_get_actual_arglist ();
2640 /* Read and write formal argument lists. */
2643 mio_formal_arglist (gfc_formal_arglist **formal)
2645 gfc_formal_arglist *f, *tail;
2649 if (iomode == IO_OUTPUT)
2651 for (f = *formal; f; f = f->next)
2652 mio_symbol_ref (&f->sym);
2656 *formal = tail = NULL;
2658 while (peek_atom () != ATOM_RPAREN)
2660 f = gfc_get_formal_arglist ();
2661 mio_symbol_ref (&f->sym);
2663 if (*formal == NULL)
2676 /* Save or restore a reference to a symbol node. */
2679 mio_symbol_ref (gfc_symbol **symp)
2683 p = mio_pointer_ref (symp);
2684 if (p->type == P_UNKNOWN)
2687 if (iomode == IO_OUTPUT)
2689 if (p->u.wsym.state == UNREFERENCED)
2690 p->u.wsym.state = NEEDS_WRITE;
2694 if (p->u.rsym.state == UNUSED)
2695 p->u.rsym.state = NEEDED;
2701 /* Save or restore a reference to a symtree node. */
2704 mio_symtree_ref (gfc_symtree **stp)
2709 if (iomode == IO_OUTPUT)
2710 mio_symbol_ref (&(*stp)->n.sym);
2713 require_atom (ATOM_INTEGER);
2714 p = get_integer (atom_int);
2716 /* An unused equivalence member; make a symbol and a symtree
2718 if (in_load_equiv && p->u.rsym.symtree == NULL)
2720 /* Since this is not used, it must have a unique name. */
2721 p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
2723 /* Make the symbol. */
2724 if (p->u.rsym.sym == NULL)
2726 p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
2728 p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
2731 p->u.rsym.symtree->n.sym = p->u.rsym.sym;
2732 p->u.rsym.symtree->n.sym->refs++;
2733 p->u.rsym.referenced = 1;
2735 /* If the symbol is PRIVATE and in COMMON, load_commons will
2736 generate a fixup symbol, which must be associated. */
2738 resolve_fixups (p->fixup, p->u.rsym.sym);
2742 if (p->type == P_UNKNOWN)
2745 if (p->u.rsym.state == UNUSED)
2746 p->u.rsym.state = NEEDED;
2748 if (p->u.rsym.symtree != NULL)
2750 *stp = p->u.rsym.symtree;
2754 f = XCNEW (fixup_t);
2756 f->next = p->u.rsym.stfixup;
2757 p->u.rsym.stfixup = f;
2759 f->pointer = (void **) stp;
2766 mio_iterator (gfc_iterator **ip)
2772 if (iomode == IO_OUTPUT)
2779 if (peek_atom () == ATOM_RPAREN)
2785 *ip = gfc_get_iterator ();
2790 mio_expr (&iter->var);
2791 mio_expr (&iter->start);
2792 mio_expr (&iter->end);
2793 mio_expr (&iter->step);
2801 mio_constructor (gfc_constructor_base *cp)
2807 if (iomode == IO_OUTPUT)
2809 for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
2812 mio_expr (&c->expr);
2813 mio_iterator (&c->iterator);
2819 while (peek_atom () != ATOM_RPAREN)
2821 c = gfc_constructor_append_expr (cp, NULL, NULL);
2824 mio_expr (&c->expr);
2825 mio_iterator (&c->iterator);
2834 static const mstring ref_types[] = {
2835 minit ("ARRAY", REF_ARRAY),
2836 minit ("COMPONENT", REF_COMPONENT),
2837 minit ("SUBSTRING", REF_SUBSTRING),
2843 mio_ref (gfc_ref **rp)
2850 r->type = MIO_NAME (ref_type) (r->type, ref_types);
2855 mio_array_ref (&r->u.ar);
2859 mio_symbol_ref (&r->u.c.sym);
2860 mio_component_ref (&r->u.c.component);
2864 mio_expr (&r->u.ss.start);
2865 mio_expr (&r->u.ss.end);
2866 mio_charlen (&r->u.ss.length);
2875 mio_ref_list (gfc_ref **rp)
2877 gfc_ref *ref, *head, *tail;
2881 if (iomode == IO_OUTPUT)
2883 for (ref = *rp; ref; ref = ref->next)
2890 while (peek_atom () != ATOM_RPAREN)
2893 head = tail = gfc_get_ref ();
2896 tail->next = gfc_get_ref ();
2910 /* Read and write an integer value. */
2913 mio_gmp_integer (mpz_t *integer)
2917 if (iomode == IO_INPUT)
2919 if (parse_atom () != ATOM_STRING)
2920 bad_module ("Expected integer string");
2922 mpz_init (*integer);
2923 if (mpz_set_str (*integer, atom_string, 10))
2924 bad_module ("Error converting integer");
2930 p = mpz_get_str (NULL, 10, *integer);
2931 write_atom (ATOM_STRING, p);
2938 mio_gmp_real (mpfr_t *real)
2943 if (iomode == IO_INPUT)
2945 if (parse_atom () != ATOM_STRING)
2946 bad_module ("Expected real string");
2949 mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
2954 p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
2956 if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
2958 write_atom (ATOM_STRING, p);
2963 atom_string = XCNEWVEC (char, strlen (p) + 20);
2965 sprintf (atom_string, "0.%s@%ld", p, exponent);
2967 /* Fix negative numbers. */
2968 if (atom_string[2] == '-')
2970 atom_string[0] = '-';
2971 atom_string[1] = '0';
2972 atom_string[2] = '.';
2975 write_atom (ATOM_STRING, atom_string);
2983 /* Save and restore the shape of an array constructor. */
2986 mio_shape (mpz_t **pshape, int rank)
2992 /* A NULL shape is represented by (). */
2995 if (iomode == IO_OUTPUT)
3007 if (t == ATOM_RPAREN)
3014 shape = gfc_get_shape (rank);
3018 for (n = 0; n < rank; n++)
3019 mio_gmp_integer (&shape[n]);
3025 static const mstring expr_types[] = {
3026 minit ("OP", EXPR_OP),
3027 minit ("FUNCTION", EXPR_FUNCTION),
3028 minit ("CONSTANT", EXPR_CONSTANT),
3029 minit ("VARIABLE", EXPR_VARIABLE),
3030 minit ("SUBSTRING", EXPR_SUBSTRING),
3031 minit ("STRUCTURE", EXPR_STRUCTURE),
3032 minit ("ARRAY", EXPR_ARRAY),
3033 minit ("NULL", EXPR_NULL),
3034 minit ("COMPCALL", EXPR_COMPCALL),
3038 /* INTRINSIC_ASSIGN is missing because it is used as an index for
3039 generic operators, not in expressions. INTRINSIC_USER is also
3040 replaced by the correct function name by the time we see it. */
3042 static const mstring intrinsics[] =
3044 minit ("UPLUS", INTRINSIC_UPLUS),
3045 minit ("UMINUS", INTRINSIC_UMINUS),
3046 minit ("PLUS", INTRINSIC_PLUS),
3047 minit ("MINUS", INTRINSIC_MINUS),
3048 minit ("TIMES", INTRINSIC_TIMES),
3049 minit ("DIVIDE", INTRINSIC_DIVIDE),
3050 minit ("POWER", INTRINSIC_POWER),
3051 minit ("CONCAT", INTRINSIC_CONCAT),
3052 minit ("AND", INTRINSIC_AND),
3053 minit ("OR", INTRINSIC_OR),
3054 minit ("EQV", INTRINSIC_EQV),
3055 minit ("NEQV", INTRINSIC_NEQV),
3056 minit ("EQ_SIGN", INTRINSIC_EQ),
3057 minit ("EQ", INTRINSIC_EQ_OS),
3058 minit ("NE_SIGN", INTRINSIC_NE),
3059 minit ("NE", INTRINSIC_NE_OS),
3060 minit ("GT_SIGN", INTRINSIC_GT),
3061 minit ("GT", INTRINSIC_GT_OS),
3062 minit ("GE_SIGN", INTRINSIC_GE),
3063 minit ("GE", INTRINSIC_GE_OS),
3064 minit ("LT_SIGN", INTRINSIC_LT),
3065 minit ("LT", INTRINSIC_LT_OS),
3066 minit ("LE_SIGN", INTRINSIC_LE),
3067 minit ("LE", INTRINSIC_LE_OS),
3068 minit ("NOT", INTRINSIC_NOT),
3069 minit ("PARENTHESES", INTRINSIC_PARENTHESES),
3074 /* Remedy a couple of situations where the gfc_expr's can be defective. */
3077 fix_mio_expr (gfc_expr *e)
3079 gfc_symtree *ns_st = NULL;
3082 if (iomode != IO_OUTPUT)
3087 /* If this is a symtree for a symbol that came from a contained module
3088 namespace, it has a unique name and we should look in the current
3089 namespace to see if the required, non-contained symbol is available
3090 yet. If so, the latter should be written. */
3091 if (e->symtree->n.sym && check_unique_name (e->symtree->name))
3093 const char *name = e->symtree->n.sym->name;
3094 if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
3095 name = dt_upper_string (name);
3096 ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
3099 /* On the other hand, if the existing symbol is the module name or the
3100 new symbol is a dummy argument, do not do the promotion. */
3101 if (ns_st && ns_st->n.sym
3102 && ns_st->n.sym->attr.flavor != FL_MODULE
3103 && !e->symtree->n.sym->attr.dummy)
3106 else if (e->expr_type == EXPR_FUNCTION && e->value.function.name)
3110 /* In some circumstances, a function used in an initialization
3111 expression, in one use associated module, can fail to be
3112 coupled to its symtree when used in a specification
3113 expression in another module. */
3114 fname = e->value.function.esym ? e->value.function.esym->name
3115 : e->value.function.isym->name;
3116 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3121 /* This is probably a reference to a private procedure from another
3122 module. To prevent a segfault, make a generic with no specific
3123 instances. If this module is used, without the required
3124 specific coming from somewhere, the appropriate error message
3126 gfc_get_symbol (fname, gfc_current_ns, &sym);
3127 sym->attr.flavor = FL_PROCEDURE;
3128 sym->attr.generic = 1;
3129 e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
3130 gfc_commit_symbol (sym);
3135 /* Read and write expressions. The form "()" is allowed to indicate a
3139 mio_expr (gfc_expr **ep)
3147 if (iomode == IO_OUTPUT)
3156 MIO_NAME (expr_t) (e->expr_type, expr_types);
3161 if (t == ATOM_RPAREN)
3168 bad_module ("Expected expression type");
3170 e = *ep = gfc_get_expr ();
3171 e->where = gfc_current_locus;
3172 e->expr_type = (expr_t) find_enum (expr_types);
3175 mio_typespec (&e->ts);
3176 mio_integer (&e->rank);
3180 switch (e->expr_type)
3184 = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
3186 switch (e->value.op.op)
3188 case INTRINSIC_UPLUS:
3189 case INTRINSIC_UMINUS:
3191 case INTRINSIC_PARENTHESES:
3192 mio_expr (&e->value.op.op1);
3195 case INTRINSIC_PLUS:
3196 case INTRINSIC_MINUS:
3197 case INTRINSIC_TIMES:
3198 case INTRINSIC_DIVIDE:
3199 case INTRINSIC_POWER:
3200 case INTRINSIC_CONCAT:
3204 case INTRINSIC_NEQV:
3206 case INTRINSIC_EQ_OS:
3208 case INTRINSIC_NE_OS:
3210 case INTRINSIC_GT_OS:
3212 case INTRINSIC_GE_OS:
3214 case INTRINSIC_LT_OS:
3216 case INTRINSIC_LE_OS:
3217 mio_expr (&e->value.op.op1);
3218 mio_expr (&e->value.op.op2);
3222 bad_module ("Bad operator");
3228 mio_symtree_ref (&e->symtree);
3229 mio_actual_arglist (&e->value.function.actual);
3231 if (iomode == IO_OUTPUT)
3233 e->value.function.name
3234 = mio_allocated_string (e->value.function.name);
3235 flag = e->value.function.esym != NULL;
3236 mio_integer (&flag);
3238 mio_symbol_ref (&e->value.function.esym);
3240 write_atom (ATOM_STRING, e->value.function.isym->name);
3244 require_atom (ATOM_STRING);
3245 e->value.function.name = gfc_get_string (atom_string);
3248 mio_integer (&flag);
3250 mio_symbol_ref (&e->value.function.esym);
3253 require_atom (ATOM_STRING);
3254 e->value.function.isym = gfc_find_function (atom_string);
3262 mio_symtree_ref (&e->symtree);
3263 mio_ref_list (&e->ref);
3266 case EXPR_SUBSTRING:
3267 e->value.character.string
3268 = CONST_CAST (gfc_char_t *,
3269 mio_allocated_wide_string (e->value.character.string,
3270 e->value.character.length));
3271 mio_ref_list (&e->ref);
3274 case EXPR_STRUCTURE:
3276 mio_constructor (&e->value.constructor);
3277 mio_shape (&e->shape, e->rank);
3284 mio_gmp_integer (&e->value.integer);
3288 gfc_set_model_kind (e->ts.kind);
3289 mio_gmp_real (&e->value.real);
3293 gfc_set_model_kind (e->ts.kind);
3294 mio_gmp_real (&mpc_realref (e->value.complex));
3295 mio_gmp_real (&mpc_imagref (e->value.complex));
3299 mio_integer (&e->value.logical);
3303 mio_integer (&e->value.character.length);
3304 e->value.character.string
3305 = CONST_CAST (gfc_char_t *,
3306 mio_allocated_wide_string (e->value.character.string,
3307 e->value.character.length));
3311 bad_module ("Bad type in constant expression");
3329 /* Read and write namelists. */
3332 mio_namelist (gfc_symbol *sym)
3334 gfc_namelist *n, *m;
3335 const char *check_name;
3339 if (iomode == IO_OUTPUT)
3341 for (n = sym->namelist; n; n = n->next)
3342 mio_symbol_ref (&n->sym);
3346 /* This departure from the standard is flagged as an error.
3347 It does, in fact, work correctly. TODO: Allow it
3349 if (sym->attr.flavor == FL_NAMELIST)
3351 check_name = find_use_name (sym->name, false);
3352 if (check_name && strcmp (check_name, sym->name) != 0)
3353 gfc_error ("Namelist %s cannot be renamed by USE "
3354 "association to %s", sym->name, check_name);
3358 while (peek_atom () != ATOM_RPAREN)
3360 n = gfc_get_namelist ();
3361 mio_symbol_ref (&n->sym);
3363 if (sym->namelist == NULL)
3370 sym->namelist_tail = m;
3377 /* Save/restore lists of gfc_interface structures. When loading an
3378 interface, we are really appending to the existing list of
3379 interfaces. Checking for duplicate and ambiguous interfaces has to
3380 be done later when all symbols have been loaded. */
3383 mio_interface_rest (gfc_interface **ip)
3385 gfc_interface *tail, *p;
3386 pointer_info *pi = NULL;
3388 if (iomode == IO_OUTPUT)
3391 for (p = *ip; p; p = p->next)
3392 mio_symbol_ref (&p->sym);
3407 if (peek_atom () == ATOM_RPAREN)
3410 p = gfc_get_interface ();
3411 p->where = gfc_current_locus;
3412 pi = mio_symbol_ref (&p->sym);
3428 /* Save/restore a nameless operator interface. */
3431 mio_interface (gfc_interface **ip)
3434 mio_interface_rest (ip);
3438 /* Save/restore a named operator interface. */
3441 mio_symbol_interface (const char **name, const char **module,
3445 mio_pool_string (name);
3446 mio_pool_string (module);
3447 mio_interface_rest (ip);
3452 mio_namespace_ref (gfc_namespace **nsp)
3457 p = mio_pointer_ref (nsp);
3459 if (p->type == P_UNKNOWN)
3460 p->type = P_NAMESPACE;
3462 if (iomode == IO_INPUT && p->integer != 0)
3464 ns = (gfc_namespace *) p->u.pointer;
3467 ns = gfc_get_namespace (NULL, 0);
3468 associate_integer_pointer (p, ns);
3476 /* Save/restore the f2k_derived namespace of a derived-type symbol. */
3478 static gfc_namespace* current_f2k_derived;
3481 mio_typebound_proc (gfc_typebound_proc** proc)
3484 int overriding_flag;
3486 if (iomode == IO_INPUT)
3488 *proc = gfc_get_typebound_proc (NULL);
3489 (*proc)->where = gfc_current_locus;
3495 (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
3497 /* IO the NON_OVERRIDABLE/DEFERRED combination. */
3498 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3499 overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
3500 overriding_flag = mio_name (overriding_flag, binding_overriding);
3501 (*proc)->deferred = ((overriding_flag & 2) != 0);
3502 (*proc)->non_overridable = ((overriding_flag & 1) != 0);
3503 gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
3505 (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
3506 (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
3507 (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
3509 mio_pool_string (&((*proc)->pass_arg));
3511 flag = (int) (*proc)->pass_arg_num;
3512 mio_integer (&flag);
3513 (*proc)->pass_arg_num = (unsigned) flag;
3515 if ((*proc)->is_generic)
3522 if (iomode == IO_OUTPUT)
3523 for (g = (*proc)->u.generic; g; g = g->next)
3525 iop = (int) g->is_operator;
3527 mio_allocated_string (g->specific_st->name);
3531 (*proc)->u.generic = NULL;
3532 while (peek_atom () != ATOM_RPAREN)
3534 gfc_symtree** sym_root;
3536 g = gfc_get_tbp_generic ();
3540 g->is_operator = (bool) iop;
3542 require_atom (ATOM_STRING);
3543 sym_root = ¤t_f2k_derived->tb_sym_root;
3544 g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
3547 g->next = (*proc)->u.generic;
3548 (*proc)->u.generic = g;
3554 else if (!(*proc)->ppc)
3555 mio_symtree_ref (&(*proc)->u.specific);
3560 /* Walker-callback function for this purpose. */
3562 mio_typebound_symtree (gfc_symtree* st)
3564 if (iomode == IO_OUTPUT && !st->n.tb)
3567 if (iomode == IO_OUTPUT)
3570 mio_allocated_string (st->name);
3572 /* For IO_INPUT, the above is done in mio_f2k_derived. */
3574 mio_typebound_proc (&st->n.tb);
3578 /* IO a full symtree (in all depth). */
3580 mio_full_typebound_tree (gfc_symtree** root)
3584 if (iomode == IO_OUTPUT)
3585 gfc_traverse_symtree (*root, &mio_typebound_symtree);
3588 while (peek_atom () == ATOM_LPAREN)
3594 require_atom (ATOM_STRING);
3595 st = gfc_get_tbp_symtree (root, atom_string);
3598 mio_typebound_symtree (st);
3606 mio_finalizer (gfc_finalizer **f)
3608 if (iomode == IO_OUTPUT)
3611 gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
3612 mio_symtree_ref (&(*f)->proc_tree);
3616 *f = gfc_get_finalizer ();
3617 (*f)->where = gfc_current_locus; /* Value should not matter. */
3620 mio_symtree_ref (&(*f)->proc_tree);
3621 (*f)->proc_sym = NULL;
3626 mio_f2k_derived (gfc_namespace *f2k)
3628 current_f2k_derived = f2k;
3630 /* Handle the list of finalizer procedures. */
3632 if (iomode == IO_OUTPUT)
3635 for (f = f2k->finalizers; f; f = f->next)
3640 f2k->finalizers = NULL;
3641 while (peek_atom () != ATOM_RPAREN)
3643 gfc_finalizer *cur = NULL;
3644 mio_finalizer (&cur);
3645 cur->next = f2k->finalizers;
3646 f2k->finalizers = cur;
3651 /* Handle type-bound procedures. */
3652 mio_full_typebound_tree (&f2k->tb_sym_root);
3654 /* Type-bound user operators. */
3655 mio_full_typebound_tree (&f2k->tb_uop_root);
3657 /* Type-bound intrinsic operators. */
3659 if (iomode == IO_OUTPUT)
3662 for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
3664 gfc_intrinsic_op realop;
3666 if (op == INTRINSIC_USER || !f2k->tb_op[op])
3670 realop = (gfc_intrinsic_op) op;
3671 mio_intrinsic_op (&realop);
3672 mio_typebound_proc (&f2k->tb_op[op]);
3677 while (peek_atom () != ATOM_RPAREN)
3679 gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
3682 mio_intrinsic_op (&op);
3683 mio_typebound_proc (&f2k->tb_op[op]);
3690 mio_full_f2k_derived (gfc_symbol *sym)
3694 if (iomode == IO_OUTPUT)
3696 if (sym->f2k_derived)
3697 mio_f2k_derived (sym->f2k_derived);
3701 if (peek_atom () != ATOM_RPAREN)
3703 sym->f2k_derived = gfc_get_namespace (NULL, 0);
3704 mio_f2k_derived (sym->f2k_derived);
3707 gcc_assert (!sym->f2k_derived);
3714 /* Unlike most other routines, the address of the symbol node is already
3715 fixed on input and the name/module has already been filled in.
3716 If you update the symbol format here, don't forget to update read_module
3717 as well (look for "seek to the symbol's component list"). */
3720 mio_symbol (gfc_symbol *sym)
3722 int intmod = INTMOD_NONE;
3726 mio_symbol_attribute (&sym->attr);
3727 mio_typespec (&sym->ts);
3728 if (sym->ts.type == BT_CLASS)
3729 sym->attr.class_ok = 1;
3731 if (iomode == IO_OUTPUT)
3732 mio_namespace_ref (&sym->formal_ns);
3735 mio_namespace_ref (&sym->formal_ns);
3738 sym->formal_ns->proc_name = sym;
3743 /* Save/restore common block links. */
3744 mio_symbol_ref (&sym->common_next);
3746 mio_formal_arglist (&sym->formal);
3748 if (sym->attr.flavor == FL_PARAMETER)
3749 mio_expr (&sym->value);
3751 mio_array_spec (&sym->as);
3753 mio_symbol_ref (&sym->result);
3755 if (sym->attr.cray_pointee)