LCOV - code coverage report
Current view: top level - gcc/fortran - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.4 % 4041 3774
Test Date: 2026-07-11 15:47:05 Functions: 99.3 % 152 151
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.