LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 11128 10496
Test Date: 2026-05-30 15:37:04 Functions: 97.9 % 476 466
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* C++ modules.  Experimental!
       2              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       3              :    Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
       4              : 
       5              :    This file is part of GCC.
       6              : 
       7              :    GCC is free software; you can redistribute it and/or modify it
       8              :    under the terms of the GNU General Public License as published by
       9              :    the Free Software Foundation; either version 3, or (at your option)
      10              :    any later version.
      11              : 
      12              :    GCC is distributed in the hope that it will be useful, but
      13              :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /* Comments in this file have a non-negligible chance of being wrong
      22              :    or at least inaccurate.  Due to (a) my misunderstanding, (b)
      23              :    ambiguities that I have interpretted differently to original intent
      24              :    (c) changes in the specification, (d) my poor wording, (e) source
      25              :    changes.  */
      26              : 
      27              : /* (Incomplete) Design Notes
      28              : 
      29              :    A hash table contains all module names.  Imported modules are
      30              :    present in a modules array, which by construction places an
      31              :    import's dependencies before the import itself.  The single
      32              :    exception is the current TU, which always occupies slot zero (even
      33              :    when it is not a module).
      34              : 
      35              :    Imported decls occupy an entity_ary, an array of binding_slots, indexed
      36              :    by importing module and index within that module.  A flat index is
      37              :    used, as each module reserves a contiguous range of indices.
      38              :    Initially each slot indicates the CMI section containing the
      39              :    streamed decl.  When the decl is imported it will point to the decl
      40              :    itself.
      41              : 
      42              :    Additionally each imported decl is mapped in the entity_map via its
      43              :    DECL_UID to the flat index in the entity_ary.  Thus we can locate
      44              :    the index for any imported decl by using this map and then
      45              :    de-flattening the index via a binary seach of the module vector.
      46              :    Cross-module references are by (remapped) module number and
      47              :    module-local index.
      48              : 
      49              :    Each importable DECL contains several flags.  The simple set are
      50              :    DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
      51              :    and DECL_MODULE_IMPORT_P.  The first indicates whether it is
      52              :    exported, the second whether it is in module or header-unit
      53              :    purview.  The third indicates it is attached to the named module in
      54              :    whose purview it resides and the fourth indicates whether it was an
      55              :    import into this TU or not.  DECL_MODULE_ATTACH_P will be false for
      56              :    all decls in a header-unit, and for those in a named module inside
      57              :    a linkage declaration.
      58              : 
      59              :    The more detailed flags are DECL_MODULE_PARTITION_P,
      60              :    DECL_MODULE_ENTITY_P.  The first is set in a primary interface unit
      61              :    on decls that were read from module partitions (these will have
      62              :    DECL_MODULE_IMPORT_P set too).  Such decls will be streamed out to
      63              :    the primary's CMI.  DECL_MODULE_ENTITY_P is set when an entity is
      64              :    imported, even if it matched a non-imported entity.  Such a decl
      65              :    will not have DECL_MODULE_IMPORT_P set, even though it has an entry
      66              :    in the entity map and array.
      67              : 
      68              :    Header units are module-like.
      69              : 
      70              :    For namespace-scope lookup, the decls for a particular module are
      71              :    held located in a sparse array hanging off the binding of the name.
      72              :    This is partitioned into two: a few fixed slots at the start
      73              :    followed by the sparse slots afterwards.  By construction we only
      74              :    need to append new slots to the end -- there is never a need to
      75              :    insert in the middle.  The fixed slots are MODULE_SLOT_CURRENT for
      76              :    the current TU (regardless of whether it is a module or not),
      77              :    MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION.  These latter two
      78              :    slots are used for merging entities across the global module and
      79              :    module partitions respectively.  MODULE_SLOT_PARTITION is only
      80              :    present in a module.  Neither of those two slots is searched during
      81              :    name lookup -- they are internal use only.  This vector is created
      82              :    lazily once we require it, if there is only a declaration from the
      83              :    current TU, a regular binding is present.  It is converted on
      84              :    demand.
      85              : 
      86              :    OPTIMIZATION: Outside of the current TU, we only need ADL to work.
      87              :    We could optimize regular lookup for the current TU by glomming all
      88              :    the visible decls on its slot.  Perhaps wait until design is a
      89              :    little more settled though.
      90              : 
      91              :    There is only one instance of each extern-linkage namespace.  It
      92              :    appears in every module slot that makes it visible.  It also
      93              :    appears in MODULE_SLOT_GLOBAL.  (It is an ODR violation if they
      94              :    collide with some other global module entity.)  We also have an
      95              :    optimization that shares the slot for adjacent modules that declare
      96              :    the same such namespace.
      97              : 
      98              :    A module interface compilation produces a Compiled Module Interface
      99              :    (CMI).  The format used is Encapsulated Lazy Records Of Numbered
     100              :    Declarations, which is essentially ELF's section encapsulation. (As
     101              :    all good nerds are aware, Elrond is half Elf.)  Some sections are
     102              :    named, and contain information about the module as a whole (indices
     103              :    etc), and other sections are referenced by number.  Although I
     104              :    don't defend against actively hostile CMIs, there is some
     105              :    checksumming involved to verify data integrity.  When dumping out
     106              :    an interface, we generate a graph of all the
     107              :    independently-redeclarable DECLS that are needed, and the decls
     108              :    they reference.  From that we determine the strongly connected
     109              :    components (SCC) within this TU.  Each SCC is dumped to a separate
     110              :    numbered section of the CMI.  We generate a binding table section,
     111              :    mapping each namespace&name to a defining section.  This allows
     112              :    lazy loading.
     113              : 
     114              :    Lazy loading employs mmap to map a read-only image of the CMI.
     115              :    It thus only occupies address space and is paged in on demand,
     116              :    backed by the CMI file itself.  If mmap is unavailable, regular
     117              :    FILEIO is used.  Also, there's a bespoke ELF reader/writer here,
     118              :    which implements just the section table and sections (including
     119              :    string sections) of a 32-bit ELF in host byte-order.  You can of
     120              :    course inspect it with readelf.  I figured 32-bit is sufficient,
     121              :    for a single module.  I detect running out of section numbers, but
     122              :    do not implement the ELF overflow mechanism.  At least you'll get
     123              :    an error if that happens.
     124              : 
     125              :    We do not separate declarations and definitions.  My guess is that
     126              :    if you refer to the declaration, you'll also need the definition
     127              :    (template body, inline function, class definition etc).  But this
     128              :    does mean we can get larger SCCs than if we separated them.  It is
     129              :    unclear whether this is a win or not.
     130              : 
     131              :    Notice that we embed section indices into the contents of other
     132              :    sections.  Thus random manipulation of the CMI file by ELF tools
     133              :    may well break it.  The kosher way would probably be to introduce
     134              :    indirection via section symbols, but that would require defining a
     135              :    relocation type.
     136              : 
     137              :    Notice that lazy loading of one module's decls can cause lazy
     138              :    loading of other decls in the same or another module.  Clearly we
     139              :    want to avoid loops.  In a correct program there can be no loops in
     140              :    the module dependency graph, and the above-mentioned SCC algorithm
     141              :    places all intra-module circular dependencies in the same SCC.  It
     142              :    also orders the SCCs wrt each other, so dependent SCCs come first.
     143              :    As we load dependent modules first, we know there can be no
     144              :    reference to a higher-numbered module, and because we write out
     145              :    dependent SCCs first, likewise for SCCs within the module.  This
     146              :    allows us to immediately detect broken references.  When loading,
     147              :    we must ensure the rest of the compiler doesn't cause some
     148              :    unconnected load to occur (for instance, instantiate a template).
     149              : 
     150              : Classes used:
     151              : 
     152              :    dumper - logger
     153              : 
     154              :    data - buffer
     155              : 
     156              :    bytes_in : data - scalar reader
     157              :    bytes_out : data - scalar writer
     158              : 
     159              :    bytes_in::bits_in - bit stream reader
     160              :    bytes_out::bits_out - bit stream writer
     161              : 
     162              :    elf - ELROND format
     163              :    elf_in : elf - ELROND reader
     164              :    elf_out : elf - ELROND writer
     165              : 
     166              :    trees_in : bytes_in - tree reader
     167              :    trees_out : bytes_out - tree writer
     168              : 
     169              :    depset - dependency set
     170              :    depset::hash - hash table of depsets
     171              :    depset::tarjan - SCC determinator
     172              : 
     173              :    uidset<T> - set T's related to a UID
     174              :    uidset<T>::hash hash table of uidset<T>
     175              : 
     176              :    loc_spans - location map data
     177              : 
     178              :    module_state - module object
     179              : 
     180              :    slurping - data needed during loading
     181              : 
     182              :    macro_import - imported macro data
     183              :    macro_export - exported macro data
     184              : 
     185              :    The ELROND objects use mmap, for both reading and writing.  If mmap
     186              :    is unavailable, fileno IO is used to read and write blocks of data.
     187              : 
     188              :    The mapper object uses fileno IO to communicate with the server or
     189              :    program.   */
     190              : 
     191              : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
     192              :    in from the Makefile.  It records the modification date of the
     193              :    source directory -- that's the only way to stay sane.  In release
     194              :    sources, we (plan to) use the compiler's major.minor versioning.
     195              :    While the format might not change between at minor versions, it
     196              :    seems simplest to tie the two together.  There's no concept of
     197              :    inter-version compatibility.  */
     198              : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
     199              : #define MODULE_MAJOR(V) ((V) / 10000)
     200              : #define MODULE_MINOR(V) ((V) % 10000)
     201              : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
     202              : #ifndef MODULE_VERSION
     203              : #include "bversion.h"
     204              : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
     205              : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
     206              : #error "This is not the version I was looking for."
     207              : #endif
     208              : 
     209              : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available.  */
     210              : #include "config.h"
     211              : #define INCLUDE_STRING
     212              : #define INCLUDE_VECTOR
     213              : #include "system.h"
     214              : #include "coretypes.h"
     215              : #include "cp-tree.h"
     216              : #include "timevar.h"
     217              : #include "stringpool.h"
     218              : #include "dumpfile.h"
     219              : #include "bitmap.h"
     220              : #include "cgraph.h"
     221              : #include "varasm.h"
     222              : #include "tree-iterator.h"
     223              : #include "cpplib.h"
     224              : #include "mkdeps.h"
     225              : #include "incpath.h"
     226              : #include "libiberty.h"
     227              : #include "stor-layout.h"
     228              : #include "version.h"
     229              : #include "tree-diagnostic.h"
     230              : #include "toplev.h"
     231              : #include "opts.h"
     232              : #include "attribs.h"
     233              : #include "intl.h"
     234              : #include "langhooks.h"
     235              : #include "contracts.h"
     236              : /* This TU doesn't need or want to see the networking.  */
     237              : #define CODY_NETWORKING 0
     238              : #include "mapper-client.h"
     239              : #include <zlib.h> // for crc32, crc32_combine
     240              : 
     241              : #if 0 // 1 for testing no mmap
     242              : #define MAPPED_READING 0
     243              : #define MAPPED_WRITING 0
     244              : #else
     245              : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
     246              : /* mmap, munmap, msync.  */
     247              : #define MAPPED_READING 1
     248              : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
     249              : /* sysconf (_SC_PAGE_SIZE), ftruncate  */
     250              : /* posix_fallocate used if available.  */
     251              : #define MAPPED_WRITING 1
     252              : #else
     253              : #define MAPPED_WRITING 0
     254              : #endif
     255              : #else
     256              : #define MAPPED_READING 0
     257              : #define MAPPED_WRITING 0
     258              : #endif
     259              : #endif
     260              : 
     261              : /* Some open(2) flag differences, what a colourful world it is!  */
     262              : #if defined (O_CLOEXEC)
     263              : // OK
     264              : #elif defined (_O_NOINHERIT)
     265              : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
     266              : #define O_CLOEXEC _O_NOINHERIT
     267              : #else
     268              : #define O_CLOEXEC 0
     269              : #endif
     270              : #if defined (O_BINARY)
     271              : // Ok?
     272              : #elif defined (_O_BINARY)
     273              : /* Windows' open(2) call defaults to text!  */
     274              : #define O_BINARY _O_BINARY
     275              : #else
     276              : #define O_BINARY 0
     277              : #endif
     278              : 
     279       287352 : static inline cpp_hashnode *cpp_node (tree id)
     280              : {
     281       287352 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     282              : }
     283              : 
     284       151594 : static inline tree identifier (const cpp_hashnode *node)
     285              : {
     286              :   /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
     287              :      then subtracts a nonzero constant, deriving a pointer to
     288              :      a different member than ident.  That's strictly undefined
     289              :      and detected by -Warray-bounds.  Suppress it.  See PR 101372.  */
     290       151594 : #pragma GCC diagnostic push
     291       151594 : #pragma GCC diagnostic ignored "-Warray-bounds"
     292       151594 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     293       151594 : #pragma GCC diagnostic pop
     294              : }
     295              : 
     296              : /* Id for dumping module information.  */
     297              : int module_dump_id;
     298              : 
     299              : /* We have a special module owner.  */
     300              : #define MODULE_UNKNOWN (~0U)    /* Not yet known.  */
     301              : 
     302              : /* Prefix for section names.  */
     303              : #define MOD_SNAME_PFX ".gnu.c++"
     304              : 
     305              : /* Format a version for user consumption.  */
     306              : 
     307              : typedef char verstr_t[32];
     308              : static void
     309         4162 : version2string (unsigned version, verstr_t &out)
     310              : {
     311         4162 :   unsigned major = MODULE_MAJOR (version);
     312         4162 :   unsigned minor = MODULE_MINOR (version);
     313              : 
     314         4162 :   if (IS_EXPERIMENTAL (version))
     315         4162 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     316         4162 :              2000 + major / 10000, (major / 100) % 100, (major % 100),
     317              :              minor / 100, minor % 100,
     318              :              EXPERIMENT ("", " (experimental)"));
     319              :   else
     320            0 :     sprintf (out, "%u.%u", major, minor);
     321         4162 : }
     322              : 
     323              : /* Include files to note translation for.  */
     324              : static vec<const char *, va_heap, vl_embed> *note_includes;
     325              : 
     326              : /* Modules to note CMI pathames.  */
     327              : static vec<const char *, va_heap, vl_embed> *note_cmis;
     328              : 
     329              : /* Traits to hash an arbitrary pointer.  Entries are not deletable,
     330              :    and removal is a noop (removal needed upon destruction).  */
     331              : template <typename T>
     332              : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
     333              :   /* Nothing is deletable.  Everything is insertable.  */
     334              :   static bool is_deleted (T *) { return false; }
     335              :   static void mark_deleted (T *) { gcc_unreachable (); }
     336              : };
     337              : 
     338              : /* Map from pointer to signed integer.   */
     339              : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
     340              : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
     341              : 
     342              : /********************************************************************/
     343              : /* Basic streaming & ELF.  Serialization is usually via mmap.  For
     344              :    writing we slide a buffer over the output file, syncing it
     345              :    approproiately.  For reading we simply map the whole file (as a
     346              :    file-backed read-only map -- it's just address space, leaving the
     347              :    OS pager to deal with getting the data to us).  Some buffers need
     348              :    to be more conventional malloc'd contents.   */
     349              : 
     350              : /* Variable length buffer.  */
     351              : 
     352              : namespace {
     353              : 
     354              : constexpr line_map_uint_t loc_one = 1;
     355              : 
     356              : class data {
     357              : public:
     358         2894 :   class allocator {
     359              :   public:
     360              :     /* Tools tend to moan if the dtor's not virtual.  */
     361       101433 :     virtual ~allocator () {}
     362              : 
     363              :   public:
     364              :     void grow (data &obj, unsigned needed, bool exact);
     365              :     void shrink (data &obj);
     366              : 
     367              :   public:
     368              :     virtual char *grow (char *ptr, unsigned needed);
     369              :     virtual void shrink (char *ptr);
     370              :   };
     371              : 
     372              : public:
     373              :   char *buffer;         /* Buffer being transferred.  */
     374              :   /* Although size_t would be the usual size, we know we never get
     375              :      more than 4GB of buffer -- because that's the limit of the
     376              :      encapsulation format.  And if you need bigger imports, you're
     377              :      doing it wrong.  */
     378              :   unsigned size;        /* Allocated size of buffer.  */
     379              :   unsigned pos;         /* Position in buffer.  */
     380              : 
     381              : public:
     382       883357 :   data ()
     383       883357 :     :buffer (NULL), size (0), pos (0)
     384              :   {
     385              :   }
     386       897795 :   ~data ()
     387              :   {
     388              :     /* Make sure the derived and/or using class know what they're
     389              :        doing.  */
     390       897795 :     gcc_checking_assert (!buffer);
     391       897795 :   }
     392              : 
     393              : protected:
     394    652594857 :   char *use (unsigned count)
     395              :   {
     396    652594857 :     if (size < pos + count)
     397              :       return NULL;
     398    652594857 :     char *res = &buffer[pos];
     399    652594857 :     pos += count;
     400    365673821 :     return res;
     401              :   }
     402              : 
     403              :   unsigned calc_crc (unsigned) const;
     404              : 
     405              : public:
     406     50470619 :   void unuse (unsigned count)
     407              :   {
     408     50470619 :     pos -= count;
     409        28131 :   }
     410              : 
     411              : public:
     412              :   static allocator simple_memory;
     413              : };
     414              : } // anon namespace
     415              : 
     416              : /* The simple data allocator.  */
     417              : data::allocator data::simple_memory;
     418              : 
     419              : /* Grow buffer to at least size NEEDED.  */
     420              : 
     421              : void
     422       805253 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     423              : {
     424       805253 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     425       805253 :   if (!needed)
     426              :     /* Pick a default size.  */
     427       335506 :     needed = EXPERIMENT (100, 1000);
     428              : 
     429       805253 :   if (!exact)
     430       796913 :     needed *= 2;
     431       805253 :   obj.buffer = grow (obj.buffer, needed);
     432       805253 :   if (obj.buffer)
     433       805253 :     obj.size = needed;
     434              :   else
     435            0 :     obj.pos = obj.size = 0;
     436       805253 : }
     437              : 
     438              : /* Free a buffer.  */
     439              : 
     440              : void
     441       349988 : data::allocator::shrink (data &obj)
     442              : {
     443            0 :   shrink (obj.buffer);
     444       349988 :   obj.buffer = NULL;
     445       349988 :   obj.size = 0;
     446            0 : }
     447              : 
     448              : char *
     449        11691 : data::allocator::grow (char *ptr, unsigned needed)
     450              : {
     451        11691 :   return XRESIZEVAR (char, ptr, needed);
     452              : }
     453              : 
     454              : void
     455        14470 : data::allocator::shrink (char *ptr)
     456              : {
     457        14470 :   XDELETEVEC (ptr);
     458        14470 : }
     459              : 
     460              : /* Calculate the crc32 of the buffer.  Note the CRC is stored in the
     461              :    first 4 bytes, so don't include them.  */
     462              : 
     463              : unsigned
     464       556233 : data::calc_crc (unsigned l) const
     465              : {
     466       556233 :   return crc32 (0, (unsigned char *)buffer + 4, l - 4);
     467              : }
     468              : 
     469              : class elf_in;
     470              : 
     471              : /* Byte stream reader.  */
     472              : 
     473              : namespace {
     474              : class bytes_in : public data {
     475              :   typedef data parent;
     476              : 
     477              : protected:
     478              :   bool overrun;  /* Sticky read-too-much flag.  */
     479              : 
     480              : public:
     481       230934 :   bytes_in ()
     482       230934 :     : parent (), overrun (false)
     483              :   {
     484              :   }
     485       233790 :   ~bytes_in ()
     486              :   {
     487        15895 :   }
     488              : 
     489              : public:
     490              :   /* Begin reading a named section.  */
     491              :   bool begin (location_t loc, elf_in *src, const char *name);
     492              :   /* Begin reading a numbered section with optional name.  */
     493              :   bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
     494              :   /* Complete reading a buffer.  Propagate errors and return true on
     495              :      success.  */
     496              :   bool end (elf_in *src);
     497              :   /* Return true if there is unread data.  */
     498      1756568 :   bool more_p () const
     499              :   {
     500      1756568 :     return pos != size;
     501              :   }
     502              : 
     503              : public:
     504              :   /* Start reading at OFFSET.  */
     505          780 :   void random_access (unsigned offset)
     506              :   {
     507          780 :     if (offset > size)
     508            0 :       set_overrun ();
     509          780 :     pos = offset;
     510              :   }
     511              : 
     512              : public:
     513      1598431 :   void align (unsigned boundary)
     514              :   {
     515      1598431 :     if (unsigned pad = pos & (boundary - 1))
     516      3095636 :       read (boundary - pad);
     517              :   }
     518              : 
     519              : public:
     520    286921036 :   const char *read (unsigned count)
     521              :   {
     522      1497205 :     char *ptr = use (count);
     523    286921036 :     if (!ptr)
     524            0 :       set_overrun ();
     525    233117133 :     return ptr;
     526              :   }
     527              : 
     528              : public:
     529              :   bool check_crc () const;
     530              :   /* We store the CRC in the first 4 bytes, using host endianness.  */
     531       232068 :   unsigned get_crc () const
     532              :   {
     533       232068 :     return *(const unsigned *)&buffer[0];
     534              :   }
     535              : 
     536              : public:
     537              :   /* Manipulate the overrun flag.  */
     538    167233373 :   bool get_overrun () const
     539              :   {
     540    167233373 :     return overrun;
     541              :   }
     542           26 :   void set_overrun ()
     543              :   {
     544           26 :     overrun = true;
     545            0 :   }
     546              : 
     547              : public:
     548              :   unsigned u32 ();      /* Read uncompressed integer.  */
     549              : 
     550              : public:
     551              :   int c () ATTRIBUTE_UNUSED;            /* Read a char.  */
     552              :   int i ();             /* Read a signed int.  */
     553              :   unsigned u ();        /* Read an unsigned int.  */
     554              :   size_t z ();          /* Read a size_t.  */
     555              :   location_t loc ();    /* Read a location_t.  */
     556              :   HOST_WIDE_INT wi ();  /* Read a HOST_WIDE_INT.  */
     557              :   unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT.  */
     558              :   const char *str (size_t * = NULL); /* Read a string.  */
     559              :   const void *buf (size_t); /* Read a fixed-length buffer.  */
     560              :   cpp_hashnode *cpp_node (); /* Read a cpp node.  */
     561              : 
     562              :   struct bits_in;
     563              :   bits_in stream_bits ();
     564              : };
     565              : } // anon namespace
     566              : 
     567              : /* Verify the buffer's CRC is correct.  */
     568              : 
     569              : bool
     570       229026 : bytes_in::check_crc () const
     571              : {
     572       229026 :   if (size < 4)
     573              :     return false;
     574              : 
     575       229026 :   unsigned c_crc = calc_crc (size);
     576       229026 :   if (c_crc != get_crc ())
     577              :     return false;
     578              : 
     579              :   return true;
     580              : }
     581              : 
     582              : class elf_out;
     583              : 
     584              : /* Byte stream writer.  */
     585              : 
     586              : namespace {
     587              : class bytes_out : public data {
     588              :   typedef data parent;
     589              : 
     590              : public:
     591              :   allocator *memory;    /* Obtainer of memory.  */
     592              : 
     593              : public:
     594       646509 :   bytes_out (allocator *memory)
     595       646509 :     : parent (), memory (memory)
     596              :   {
     597              :   }
     598       646509 :   ~bytes_out ()
     599              :   {
     600       676514 :   }
     601              : 
     602              : public:
     603    926619133 :   bool streaming_p () const
     604              :   {
     605    926619133 :     return memory != NULL;
     606              :   }
     607              : 
     608              : public:
     609              :   void set_crc (unsigned *crc_ptr);
     610              : 
     611              : public:
     612              :   /* Begin writing, maybe reserve space for CRC.  */
     613              :   void begin (bool need_crc = true);
     614              :   /* Finish writing.  Spill to section by number.  */
     615              :   unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
     616              : 
     617              : public:
     618      2178098 :   void align (unsigned boundary)
     619              :   {
     620      2178098 :     if (unsigned pad = pos & (boundary - 1))
     621      2038158 :       write (boundary - pad);
     622      2178098 :   }
     623              : 
     624              : public:
     625    365673821 :   char *write (unsigned count, bool exact = false)
     626              :   {
     627    365673821 :     if (size < pos + count)
     628       455268 :       memory->grow (*this, pos + count, exact);
     629    365673821 :     return use (count);
     630              :   }
     631              : 
     632              : public:
     633              :   void u32 (unsigned);  /* Write uncompressed integer.  */
     634              : 
     635              : public:
     636              :   void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char.  */
     637              :   void i (int);         /* Write signed int.  */
     638              :   void u (unsigned);    /* Write unsigned int.  */
     639              :   void z (size_t s);    /* Write size_t.  */
     640              :   void loc (location_t); /* Write location_t.  */
     641              :   void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT.  */
     642              :   void wu (unsigned HOST_WIDE_INT);  /* Write unsigned HOST_WIDE_INT.  */
     643        19237 :   void str (const char *ptr)
     644              :   {
     645        19237 :     str (ptr, strlen (ptr));
     646        19237 :   }
     647       295989 :   void cpp_node (const cpp_hashnode *node)
     648              :   {
     649       295989 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     650        12815 :   }
     651              :   void str (const char *, size_t);  /* Write string of known length.  */
     652              :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     653              :   void *buf (size_t); /* Create a writable buffer */
     654              : 
     655              :   struct bits_out;
     656              :   bits_out stream_bits ();
     657              : 
     658              : public:
     659              :   /* Format a NUL-terminated raw string.  */
     660              :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     661              :   void print_time (const char *, const tm *, const char *);
     662              : 
     663              : public:
     664              :   /* Dump instrumentation.  */
     665              :   static void instrument ();
     666              : 
     667              : protected:
     668              :   /* Instrumentation.  */
     669              :   static unsigned spans[4];
     670              :   static unsigned lengths[4];
     671              : };
     672              : } // anon namespace
     673              : 
     674              : /* Finish bit packet.  Rewind the bytes not used.  */
     675              : 
     676              : static unsigned
     677     50414888 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     678              : {
     679     50414888 :   gcc_assert (bit_pos);
     680     50414888 :   unsigned bytes = (bit_pos + 7) / 8;
     681     50414888 :   bits.unuse (4 - bytes);
     682     50414888 :   bit_pos = 0;
     683     50414888 :   bit_val = 0;
     684     50414888 :   return bytes;
     685              : }
     686              : 
     687              : /* Bit stream reader (RAII-enabled).  Bools are packed into bytes.  You
     688              :    cannot mix bools and non-bools.  Use bflush to flush the current stream
     689              :    of bools on demand.  Upon destruction bflush is called.
     690              : 
     691              :    When reading, we don't know how many bools we'll read in.  So read
     692              :    4 bytes-worth, and then rewind when flushing if we didn't need them
     693              :    all.  You can't have a block of bools closer than 4 bytes to the
     694              :    end of the buffer.
     695              : 
     696              :    Both bits_in and bits_out maintain the necessary state for bit packing,
     697              :    and since these objects are locally constructed the compiler can more
     698              :    easily track their state across consecutive reads/writes and optimize
     699              :    away redundant buffering checks.  */
     700              : 
     701              : struct bytes_in::bits_in {
     702              :   bytes_in& in;
     703              :   uint32_t bit_val = 0;
     704              :   unsigned bit_pos = 0;
     705              : 
     706     16248105 :   bits_in (bytes_in& in)
     707     16248105 :     : in (in)
     708              :   { }
     709              : 
     710     16248105 :   ~bits_in ()
     711              :   {
     712     15254481 :     bflush ();
     713     16248105 :   }
     714              : 
     715              :   bits_in(bits_in&&) = default;
     716              :   bits_in(const bits_in&) = delete;
     717              :   bits_in& operator=(const bits_in&) = delete;
     718              : 
     719              :   /* Completed a block of bools.  */
     720     33699687 :   void bflush ()
     721              :   {
     722     33699687 :     if (bit_pos)
     723     18445206 :       bit_flush (in, bit_val, bit_pos);
     724     33699687 :   }
     725              : 
     726              :   /* Read one bit.  */
     727    557235626 :   bool b ()
     728              :   {
     729    557235626 :     if (!bit_pos)
     730     24395140 :       bit_val = in.u32 ();
     731    557235626 :     bool x = (bit_val >> bit_pos) & 1;
     732    557235626 :     bit_pos = (bit_pos + 1) % 32;
     733    557235626 :     return x;
     734              :   }
     735              : };
     736              : 
     737              : /* Factory function for bits_in.  */
     738              : 
     739              : bytes_in::bits_in
     740     16248105 : bytes_in::stream_bits ()
     741              : {
     742     16248105 :   return bits_in (*this);
     743              : }
     744              : 
     745              : /* Bit stream writer (RAII-enabled), counterpart to bits_in.  */
     746              : 
     747              : struct bytes_out::bits_out {
     748              :   bytes_out& out;
     749              :   uint32_t bit_val = 0;
     750              :   unsigned bit_pos = 0;
     751              :   char is_set = -1;
     752              : 
     753     21213314 :   bits_out (bytes_out& out)
     754     21213314 :     : out (out)
     755              :   { }
     756              : 
     757     21213314 :   ~bits_out ()
     758              :   {
     759        75754 :     bflush ();
     760              :   }
     761              : 
     762              :   bits_out(bits_out&&) = default;
     763              :   bits_out(const bits_out&) = delete;
     764              :   bits_out& operator=(const bits_out&) = delete;
     765              : 
     766              :   /* Completed a block of bools.  */
     767     43917373 :   void bflush ()
     768              :   {
     769     43917373 :     if (bit_pos)
     770              :       {
     771     24042463 :         out.u32 (bit_val);
     772     24042463 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     773              :       }
     774     43917373 :     out.spans[2]++;
     775     43917373 :     is_set = -1;
     776     43917373 :   }
     777              : 
     778              :   /* Write one bit.
     779              : 
     780              :      It may be worth optimizing for most bools being zero.  Some kind of
     781              :      run-length encoding?  */
     782    727178659 :   void b (bool x)
     783              :   {
     784    727178659 :     if (is_set != x)
     785              :       {
     786     75907559 :         is_set = x;
     787     75907559 :         out.spans[x]++;
     788              :       }
     789    727178659 :     out.lengths[x]++;
     790    727178659 :     bit_val |= unsigned (x) << bit_pos++;
     791    727178659 :     if (bit_pos == 32)
     792              :       {
     793      7927219 :         out.u32 (bit_val);
     794      7927219 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     795              :       }
     796    727178659 :   }
     797              : };
     798              : 
     799              : /* Factory function for bits_out.  */
     800              : 
     801              : bytes_out::bits_out
     802     21213314 : bytes_out::stream_bits ()
     803              : {
     804     21213314 :   return bits_out (*this);
     805              : }
     806              : 
     807              : /* Instrumentation.  */
     808              : unsigned bytes_out::spans[4];
     809              : unsigned bytes_out::lengths[4];
     810              : 
     811              : /* If CRC_PTR non-null, set the CRC of the buffer.  Mix the CRC into
     812              :    that pointed to by CRC_PTR.  */
     813              : 
     814              : void
     815       329954 : bytes_out::set_crc (unsigned *crc_ptr)
     816              : {
     817       329954 :   if (crc_ptr)
     818              :     {
     819       327207 :       gcc_checking_assert (pos >= 4);
     820              : 
     821       327207 :       unsigned crc = calc_crc (pos);
     822       327207 :       unsigned accum = *crc_ptr;
     823              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     824       327207 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     825       327207 :       *crc_ptr = accum;
     826              : 
     827              :       /* Buffer will be sufficiently aligned.  */
     828       327207 :       *(unsigned *)buffer = crc;
     829              :     }
     830       329954 : }
     831              : 
     832              : /* Exactly 4 bytes.  Used internally for bool packing and a few other
     833              :    places.  We can't simply use uint32_t because (a) alignment and
     834              :    (b) we need little-endian for the bool streaming rewinding to make
     835              :    sense.  */
     836              : 
     837              : void
     838     31978590 : bytes_out::u32 (unsigned val)
     839              : {
     840     31978590 :   if (char *ptr = write (4))
     841              :     {
     842     31978590 :       ptr[0] = val;
     843     31978590 :       ptr[1] = val >> 8;
     844     31978590 :       ptr[2] = val >> 16;
     845     31978590 :       ptr[3] = val >> 24;
     846              :     }
     847     31978590 : }
     848              : 
     849              : unsigned
     850     24404697 : bytes_in::u32 ()
     851              : {
     852     24404697 :   unsigned val = 0;
     853     24404697 :   if (const char *ptr = read (4))
     854              :     {
     855     24404697 :       val |= (unsigned char)ptr[0];
     856     24404697 :       val |= (unsigned char)ptr[1] << 8;
     857     24404697 :       val |= (unsigned char)ptr[2] << 16;
     858     24404697 :       val |= (unsigned char)ptr[3] << 24;
     859              :     }
     860              : 
     861     24404697 :   return val;
     862              : }
     863              : 
     864              : /* Chars are unsigned and written as single bytes. */
     865              : 
     866              : void
     867            0 : bytes_out::c (unsigned char v)
     868              : {
     869            0 :   if (char *ptr = write (1))
     870            0 :     *ptr = v;
     871            0 : }
     872              : 
     873              : int
     874            0 : bytes_in::c ()
     875              : {
     876            0 :   int v = 0;
     877            0 :   if (const char *ptr = read (1))
     878            0 :     v = (unsigned char)ptr[0];
     879            0 :   return v;
     880              : }
     881              : 
     882              : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
     883              :    big-endian form.  4 bits are in the first byte.  */
     884              : 
     885              : void
     886    128922162 : bytes_out::i (int v)
     887              : {
     888    128922162 :   if (char *ptr = write (1))
     889              :     {
     890    128922162 :       if (v <= 0x3f && v >= -0x40)
     891    100370699 :         *ptr = v & 0x7f;
     892              :       else
     893              :         {
     894     28551463 :           unsigned bytes = 0;
     895     28551463 :           int probe;
     896     28551463 :           if (v >= 0)
     897            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     898            0 :               bytes++;
     899              :           else
     900     43756395 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     901     15204932 :               bytes++;
     902     28551463 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     903     28551463 :           if ((ptr = write (++bytes)))
     904     72307858 :             for (; bytes--; v >>= 8)
     905     43756395 :               ptr[bytes] = v & 0xff;
     906              :         }
     907              :     }
     908    128922162 : }
     909              : 
     910              : int
     911     99815028 : bytes_in::i ()
     912              : {
     913     99815028 :   int v = 0;
     914     99815028 :   if (const char *ptr = read (1))
     915              :     {
     916     99815028 :       v = *ptr & 0xff;
     917     99815028 :       if (v & 0x80)
     918              :         {
     919     22762587 :           unsigned bytes = (v >> 4) & 0x7;
     920     22762587 :           v &= 0xf;
     921     22762587 :           if (v & 0x8)
     922     22762587 :             v |= -1 ^ 0x7;
     923              :           /* unsigned necessary due to left shifts of -ve values.  */
     924     22762587 :           unsigned uv = unsigned (v);
     925     22762587 :           if ((ptr = read (++bytes)))
     926     59018228 :             while (bytes--)
     927     36255641 :               uv = (uv << 8) | (*ptr++ & 0xff);
     928     22762587 :           v = int (uv);
     929              :         }
     930     77052441 :       else if (v & 0x40)
     931      9742105 :         v |= -1 ^ 0x3f;
     932              :     }
     933              : 
     934     99815028 :   return v;
     935              : }
     936              : 
     937              : void
     938    110143693 : bytes_out::u (unsigned v)
     939              : {
     940    110143693 :   if (char *ptr = write (1))
     941              :     {
     942    110143693 :       if (v <= 0x7f)
     943     95757141 :         *ptr = v;
     944              :       else
     945              :         {
     946     14386552 :           unsigned bytes = 0;
     947     14386552 :           unsigned probe;
     948     17142035 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     949      2755483 :             bytes++;
     950     14386552 :           *ptr = 0x80 | bytes << 4 | probe;
     951     14386552 :           if ((ptr = write (++bytes)))
     952     31528587 :             for (; bytes--; v >>= 8)
     953     17142035 :               ptr[bytes] = v & 0xff;
     954              :         }
     955              :     }
     956    110143693 : }
     957              : 
     958              : unsigned
     959     87324593 : bytes_in::u ()
     960              : {
     961     87324593 :   unsigned v = 0;
     962              : 
     963     87324593 :   if (const char *ptr = read (1))
     964              :     {
     965     87324593 :       v = *ptr & 0xff;
     966     87324593 :       if (v & 0x80)
     967              :         {
     968     11491256 :           unsigned bytes = (v >> 4) & 0x7;
     969     11491256 :           v &= 0xf;
     970     11491256 :           if ((ptr = read (++bytes)))
     971     25261767 :             while (bytes--)
     972     13770511 :               v = (v << 8) | (*ptr++ & 0xff);
     973              :         }
     974              :     }
     975              : 
     976     87324593 :   return v;
     977              : }
     978              : 
     979              : void
     980     26273048 : bytes_out::wi (HOST_WIDE_INT v)
     981              : {
     982     26273048 :   if (char *ptr = write (1))
     983              :     {
     984     26273048 :       if (v <= 0x3f && v >= -0x40)
     985      5126722 :         *ptr = v & 0x7f;
     986              :       else
     987              :         {
     988     21146326 :           unsigned bytes = 0;
     989     21146326 :           HOST_WIDE_INT probe;
     990     21146326 :           if (v >= 0)
     991     77285774 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     992     56142609 :               bytes++;
     993              :           else
     994        10053 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     995         6892 :               bytes++;
     996     21146326 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     997     21146326 :           if ((ptr = write (++bytes)))
     998     98442153 :             for (; bytes--; v >>= 8)
     999     77295827 :               ptr[bytes] = v & 0xff;
    1000              :         }
    1001              :     }
    1002     26273048 : }
    1003              : 
    1004              : HOST_WIDE_INT
    1005     19974384 : bytes_in::wi ()
    1006              : {
    1007     19974384 :   HOST_WIDE_INT v = 0;
    1008     19974384 :   if (const char *ptr = read (1))
    1009              :     {
    1010     19974384 :       v = *ptr & 0xff;
    1011     19974384 :       if (v & 0x80)
    1012              :         {
    1013     18052855 :           unsigned bytes = (v >> 4) & 0x7;
    1014     18052855 :           v &= 0xf;
    1015     18052855 :           if (v & 0x8)
    1016         2098 :             v |= -1 ^ 0x7;
    1017              :           /* unsigned necessary due to left shifts of -ve values.  */
    1018     18052855 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1019     18052855 :           if ((ptr = read (++bytes)))
    1020     83386326 :             while (bytes--)
    1021     65333471 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1022     18052855 :           v = (HOST_WIDE_INT) uv;
    1023              :         }
    1024      1921529 :       else if (v & 0x40)
    1025         8823 :         v |= -1 ^ 0x3f;
    1026              :     }
    1027              : 
    1028     19974384 :   return v;
    1029              : }
    1030              : 
    1031              : /* unsigned wide ints are just written as signed wide ints.  */
    1032              : 
    1033              : inline void
    1034     26272180 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1035              : {
    1036     26272180 :   wi ((HOST_WIDE_INT) v);
    1037              : }
    1038              : 
    1039              : inline unsigned HOST_WIDE_INT
    1040     19973810 : bytes_in::wu ()
    1041              : {
    1042     39348218 :   return (unsigned HOST_WIDE_INT) wi ();
    1043              : }
    1044              : 
    1045              : /* size_t written as unsigned or unsigned wide int.  */
    1046              : 
    1047              : inline void
    1048      2137658 : bytes_out::z (size_t s)
    1049              : {
    1050      2137658 :   if (sizeof (s) == sizeof (unsigned))
    1051              :     u (s);
    1052              :   else
    1053      4242409 :     wu (s);
    1054           12 : }
    1055              : 
    1056              : inline size_t
    1057      1581340 : bytes_in::z ()
    1058              : {
    1059      1581340 :   if (sizeof (size_t) == sizeof (unsigned))
    1060              :     return u ();
    1061              :   else
    1062      3162680 :     return wu ();
    1063              : }
    1064              : 
    1065              : /* location_t written as 32- or 64-bit as needed.  */
    1066              : 
    1067     23329539 : inline void bytes_out::loc (location_t l)
    1068              : {
    1069     23329539 :   if (sizeof (location_t) > sizeof (unsigned))
    1070     44139990 :     wu (l);
    1071              :   else
    1072              :     u (l);
    1073      2516341 : }
    1074              : 
    1075     17796067 : inline location_t bytes_in::loc ()
    1076              : {
    1077     17796067 :   if (sizeof (location_t) > sizeof (unsigned))
    1078     35589105 :     return wu ();
    1079              :   else
    1080              :     return u ();
    1081              : }
    1082              : 
    1083              : /* Buffer simply memcpied.  */
    1084              : void *
    1085      2178098 : bytes_out::buf (size_t len)
    1086              : {
    1087      2178098 :   align (sizeof (void *) * 2);
    1088      2178098 :   return write (len);
    1089              : }
    1090              : 
    1091              : void
    1092      2127793 : bytes_out::buf (const void *src, size_t len)
    1093              : {
    1094      2127793 :   if (void *ptr = buf (len))
    1095      2127793 :     memcpy (ptr, src, len);
    1096      2127793 : }
    1097              : 
    1098              : const void *
    1099      1598431 : bytes_in::buf (size_t len)
    1100              : {
    1101      1598431 :   align (sizeof (void *) * 2);
    1102      1598431 :   const char *ptr = read (len);
    1103              : 
    1104      1598431 :   return ptr;
    1105              : }
    1106              : 
    1107              : /* strings as an size_t length, followed by the buffer.  Make sure
    1108              :    there's a NUL terminator on read.  */
    1109              : 
    1110              : void
    1111      2137640 : bytes_out::str (const char *string, size_t len)
    1112              : {
    1113      2104745 :   z (len);
    1114      2104745 :   if (len)
    1115              :     {
    1116      2104745 :       gcc_checking_assert (!string[len]);
    1117      2104745 :       buf (string, len + 1);
    1118              :     }
    1119        32895 : }
    1120              : 
    1121              : const char *
    1122      1581334 : bytes_in::str (size_t *len_p)
    1123              : {
    1124      1581334 :   size_t len = z ();
    1125              : 
    1126              :   /* We're about to trust some user data.  */
    1127      1581334 :   if (overrun)
    1128            0 :     len = 0;
    1129      1581334 :   if (len_p)
    1130      1577403 :     *len_p = len;
    1131      1581334 :   const char *str = NULL;
    1132      1581334 :   if (len)
    1133              :     {
    1134      1581063 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1135      1581063 :       if (!str || str[len])
    1136              :         {
    1137            0 :           set_overrun ();
    1138            0 :           str = NULL;
    1139              :         }
    1140              :     }
    1141            0 :   return str ? str : "";
    1142              : }
    1143              : 
    1144              : cpp_hashnode *
    1145       287623 : bytes_in::cpp_node ()
    1146              : {
    1147       287623 :   size_t len;
    1148       287623 :   const char *s = str (&len);
    1149       287623 :   if (!len)
    1150              :     return NULL;
    1151       287352 :   return ::cpp_node (get_identifier_with_length (s, len));
    1152              : }
    1153              : 
    1154              : /* Format a string directly to the buffer, including a terminating
    1155              :    NUL.  Intended for human consumption.  */
    1156              : 
    1157              : void
    1158        28131 : bytes_out::printf (const char *format, ...)
    1159              : {
    1160        28131 :   va_list args;
    1161              :   /* Exercise buffer expansion.  */
    1162        28131 :   size_t len = EXPERIMENT (10, 500);
    1163              : 
    1164        55731 :   while (char *ptr = write (len))
    1165              :     {
    1166        55731 :       va_start (args, format);
    1167        55731 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1168        55731 :       va_end (args);
    1169        55731 :       if (actual <= len)
    1170              :         {
    1171        28131 :           unuse (len - actual);
    1172        28131 :           break;
    1173              :         }
    1174        27600 :       unuse (len);
    1175        27600 :       len = actual;
    1176        27600 :     }
    1177        28131 : }
    1178              : 
    1179              : void
    1180         5494 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1181              : {
    1182         5494 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1183         5494 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1184         5494 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1185         5494 : }
    1186              : 
    1187              : /* Encapsulated Lazy Records Of Named Declarations.
    1188              :    Header: Stunningly Elf32_Ehdr-like
    1189              :    Sections: Sectional data
    1190              :      [1-N) : User data sections
    1191              :      N .strtab  : strings, stunningly ELF STRTAB-like
    1192              :    Index: Section table, stunningly ELF32_Shdr-like.   */
    1193              : 
    1194              : class elf {
    1195              : protected:
    1196              :   /* Constants used within the format.  */
    1197              :   enum private_constants {
    1198              :     /* File kind. */
    1199              :     ET_NONE = 0,
    1200              :     EM_NONE = 0,
    1201              :     OSABI_NONE = 0,
    1202              : 
    1203              :     /* File format. */
    1204              :     EV_CURRENT = 1,
    1205              :     CLASS32 = 1,
    1206              :     DATA2LSB = 1,
    1207              :     DATA2MSB = 2,
    1208              : 
    1209              :     /* Section numbering.  */
    1210              :     SHN_UNDEF = 0,
    1211              :     SHN_LORESERVE = 0xff00,
    1212              :     SHN_XINDEX = 0xffff,
    1213              : 
    1214              :     /* Section types.  */
    1215              :     SHT_NONE = 0,       /* No contents.  */
    1216              :     SHT_PROGBITS = 1, /* Random bytes.  */
    1217              :     SHT_STRTAB = 3,     /* A string table.  */
    1218              : 
    1219              :     /* Section flags.  */
    1220              :     SHF_NONE = 0x00,    /* Nothing.  */
    1221              :     SHF_STRINGS = 0x20,  /* NUL-Terminated strings.  */
    1222              : 
    1223              :     /* I really hope we do not get CMI files larger than 4GB.  */
    1224              :     MY_CLASS = CLASS32,
    1225              :     /* It is host endianness that is relevant.  */
    1226              :     MY_ENDIAN = DATA2LSB
    1227              : #ifdef WORDS_BIGENDIAN
    1228              :     ^ DATA2LSB ^ DATA2MSB
    1229              : #endif
    1230              :   };
    1231              : 
    1232              : public:
    1233              :   /* Constants visible to users.  */
    1234              :   enum public_constants {
    1235              :     /* Special error codes.  Breaking layering a bit.  */
    1236              :     E_BAD_DATA = -1,  /* Random unexpected data errors.  */
    1237              :     E_BAD_LAZY = -2,  /* Badly ordered laziness.  */
    1238              :     E_BAD_IMPORT = -3 /* A nested import failed.  */
    1239              :   };
    1240              : 
    1241              : protected:
    1242              :   /* File identification.  On-disk representation.  */
    1243              :   struct ident {
    1244              :     uint8_t magic[4];   /* 0x7f, 'E', 'L', 'F' */
    1245              :     uint8_t klass;      /* 4:CLASS32 */
    1246              :     uint8_t data;       /* 5:DATA2[LM]SB */
    1247              :     uint8_t version;    /* 6:EV_CURRENT  */
    1248              :     uint8_t osabi;      /* 7:OSABI_NONE */
    1249              :     uint8_t abiver;     /* 8: 0 */
    1250              :     uint8_t pad[7];     /* 9-15 */
    1251              :   };
    1252              :   /* File header.  On-disk representation.  */
    1253              :   struct header {
    1254              :     struct ident ident;
    1255              :     uint16_t type;      /* ET_NONE */
    1256              :     uint16_t machine;   /* EM_NONE */
    1257              :     uint32_t version;   /* EV_CURRENT */
    1258              :     uint32_t entry;     /* 0 */
    1259              :     uint32_t phoff;     /* 0 */
    1260              :     uint32_t shoff;     /* Section Header Offset in file */
    1261              :     uint32_t flags;
    1262              :     uint16_t ehsize;    /* ELROND Header SIZE -- sizeof (header) */
    1263              :     uint16_t phentsize; /* 0 */
    1264              :     uint16_t phnum;     /* 0 */
    1265              :     uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
    1266              :     uint16_t shnum;     /* Section Header NUM */
    1267              :     uint16_t shstrndx;  /* Section Header STRing iNDeX */
    1268              :   };
    1269              :   /* File section.  On-disk representation.  */
    1270              :   struct section {
    1271              :     uint32_t name;      /* String table offset.  */
    1272              :     uint32_t type;      /* SHT_* */
    1273              :     uint32_t flags;     /* SHF_* */
    1274              :     uint32_t addr;      /* 0 */
    1275              :     uint32_t offset;    /* OFFSET in file */
    1276              :     uint32_t size;      /* SIZE of section */
    1277              :     uint32_t link;      /* 0 */
    1278              :     uint32_t info;      /* 0 */
    1279              :     uint32_t addralign; /* 0 */
    1280              :     uint32_t entsize;   /* ENTry SIZE, usually 0 */
    1281              :   };
    1282              : 
    1283              : protected:
    1284              :   data hdr;     /* The header.  */
    1285              :   data sectab;  /* The section table.  */
    1286              :   data strtab;  /* String table.  */
    1287              :   int fd;       /* File descriptor we're reading or writing.  */
    1288              :   int err;      /* Sticky error code.  */
    1289              : 
    1290              : public:
    1291              :   /* Construct from STREAM.  E is errno if STREAM NULL.  */
    1292         5914 :   elf (int fd, int e)
    1293        11828 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1294              :   {}
    1295         5832 :   ~elf ()
    1296              :   {
    1297         5832 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1298              :                          && !sectab.buffer && !strtab.buffer);
    1299         5832 :   }
    1300              : 
    1301              : public:
    1302              :   /* Return the error, if we have an error.  */
    1303       447585 :   int get_error () const
    1304              :   {
    1305       447585 :     return err;
    1306              :   }
    1307              :   /* Set the error, unless it's already been set.  */
    1308           53 :   void set_error (int e = E_BAD_DATA)
    1309              :   {
    1310           53 :     if (!err)
    1311           19 :       err = e;
    1312            0 :   }
    1313              :   /* Get an error string.  */
    1314              :   const char *get_error (const char *) const;
    1315              : 
    1316              : public:
    1317              :   /* Begin reading/writing file.  Return false on error.  */
    1318         5796 :   bool begin () const
    1319              :   {
    1320         5796 :     return !get_error ();
    1321              :   }
    1322              :   /* Finish reading/writing file.  Return false on error.  */
    1323              :   bool end ();
    1324              : };
    1325              : 
    1326              : /* Return error string.  */
    1327              : 
    1328              : const char *
    1329           40 : elf::get_error (const char *name) const
    1330              : {
    1331           40 :   if (!name)
    1332              :     return "Unknown CMI mapping";
    1333              : 
    1334           40 :   switch (err)
    1335              :     {
    1336            0 :     case 0:
    1337            0 :       gcc_unreachable ();
    1338              :     case E_BAD_DATA:
    1339              :       return "Bad file data";
    1340            6 :     case E_BAD_IMPORT:
    1341            6 :       return "Bad import dependency";
    1342            0 :     case E_BAD_LAZY:
    1343            0 :       return "Bad lazy ordering";
    1344           21 :     default:
    1345           21 :       return xstrerror (err);
    1346              :     }
    1347              : }
    1348              : 
    1349              : /* Finish file, return true if there's an error.  */
    1350              : 
    1351              : bool
    1352         8254 : elf::end ()
    1353              : {
    1354              :   /* Close the stream and free the section table.  */
    1355         8254 :   if (fd >= 0 && close (fd))
    1356            0 :     set_error (errno);
    1357         8254 :   fd = -1;
    1358              : 
    1359         8254 :   return !get_error ();
    1360              : }
    1361              : 
    1362              : /* ELROND reader.  */
    1363              : 
    1364              : class elf_in : public elf {
    1365              :   typedef elf parent;
    1366              : 
    1367              : private:
    1368              :   /* For freezing & defrosting.  */
    1369              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1370              :   dev_t device;
    1371              :   ino_t inode;
    1372              : #endif
    1373              : 
    1374              : public:
    1375         3020 :   elf_in (int fd, int e)
    1376         6040 :     :parent (fd, e)
    1377              :   {
    1378              :   }
    1379         2938 :   ~elf_in ()
    1380              :   {
    1381         2938 :   }
    1382              : 
    1383              : public:
    1384       210571 :   bool is_frozen () const
    1385              :   {
    1386           18 :     return fd < 0 && hdr.pos;
    1387              :   }
    1388           18 :   bool is_freezable () const
    1389              :   {
    1390            9 :     return fd >= 0 && hdr.pos;
    1391              :   }
    1392              :   void freeze ();
    1393              :   bool defrost (const char *);
    1394              : 
    1395              :   /* If BYTES is in the mmapped area, allocate a new buffer for it.  */
    1396            0 :   void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
    1397              :   {
    1398              : #if MAPPED_READING
    1399            0 :     if (hdr.buffer && bytes.buffer >= hdr.buffer
    1400            0 :         && bytes.buffer < hdr.buffer + hdr.pos)
    1401              :       {
    1402            0 :         char *buf = bytes.buffer;
    1403            0 :         bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
    1404            0 :         memcpy (bytes.buffer, buf, bytes.size);
    1405              :       }
    1406              : #endif
    1407            0 :   }
    1408              :   /* If BYTES is not in SELF's mmapped area, free it.  SELF might be
    1409              :      NULL. */
    1410         1073 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1411              :   {
    1412              : #if MAPPED_READING
    1413         1073 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1414         1073 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1415              : #endif
    1416            0 :       data::simple_memory.shrink (bytes.buffer);
    1417         1073 :     bytes.buffer = NULL;
    1418         1073 :     bytes.size = 0;
    1419         1073 :   }
    1420              : 
    1421              : public:
    1422       235018 :   static void grow (data &data, unsigned needed)
    1423              :   {
    1424       235018 :     gcc_checking_assert (!data.buffer);
    1425              : #if !MAPPED_READING
    1426              :     data.buffer = XNEWVEC (char, needed);
    1427              : #endif
    1428       235018 :     data.size = needed;
    1429       235018 :   }
    1430       241605 :   static void shrink (data &data)
    1431              :   {
    1432              : #if !MAPPED_READING
    1433              :     XDELETEVEC (data.buffer);
    1434              : #endif
    1435       241605 :     data.buffer = NULL;
    1436       241605 :     data.size = 0;
    1437            0 :   }
    1438              : 
    1439              : public:
    1440       232022 :   const section *get_section (unsigned s) const
    1441              :   {
    1442       232022 :     if (s * sizeof (section) < sectab.size)
    1443       232022 :       return reinterpret_cast<const section *>
    1444       232022 :         (&sectab.buffer[s * sizeof (section)]);
    1445              :     else
    1446              :       return NULL;
    1447              :   }
    1448              :   unsigned get_section_limit () const
    1449              :   {
    1450              :     return sectab.size / sizeof (section);
    1451              :   }
    1452              : 
    1453              : protected:
    1454              :   const char *read (data *, unsigned, unsigned);
    1455              : 
    1456              : public:
    1457              :   /* Read section by number.  */
    1458       232022 :   bool read (data *d, const section *s)
    1459              :   {
    1460       232022 :     return s && read (d, s->offset, s->size);
    1461              :   }
    1462              : 
    1463              :   /* Find section by name.  */
    1464              :   unsigned find (const char *name);
    1465              :   /* Find section by index.  */
    1466              :   const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
    1467              : 
    1468              : public:
    1469              :   /* Release the string table, when we're done with it.  */
    1470         8331 :   void release ()
    1471              :   {
    1472         8331 :     shrink (strtab);
    1473           39 :   }
    1474              : 
    1475              : public:
    1476              :   bool begin (location_t);
    1477         5360 :   bool end ()
    1478              :   {
    1479         5360 :     release ();
    1480              : #if MAPPED_READING
    1481         5360 :     if (hdr.buffer)
    1482         2938 :       munmap (hdr.buffer, hdr.pos);
    1483         5360 :     hdr.buffer = NULL;
    1484              : #endif
    1485         5360 :     shrink (sectab);
    1486              : 
    1487         5360 :     return parent::end ();
    1488              :   }
    1489              : 
    1490              : public:
    1491              :   /* Return string name at OFFSET.  Checks OFFSET range.  Always
    1492              :      returns non-NULL.  We know offset 0 is an empty string.  */
    1493       339349 :   const char *name (unsigned offset)
    1494              :   {
    1495       678698 :     return &strtab.buffer[offset < strtab.size ? offset : 0];
    1496              :   }
    1497              : };
    1498              : 
    1499              : /* ELROND writer.  */
    1500              : 
    1501              : class elf_out : public elf, public data::allocator {
    1502              :   typedef elf parent;
    1503              :   /* Desired section alignment on disk.  */
    1504              :   static const int SECTION_ALIGN = 16;
    1505              : 
    1506              : private:
    1507              :   ptr_int_hash_map identtab;    /* Map of IDENTIFIERS to strtab offsets. */
    1508              :   unsigned pos;                 /* Write position in file.  */
    1509              : #if MAPPED_WRITING
    1510              :   unsigned offset;              /* Offset of the mapping.  */
    1511              :   unsigned extent;              /* Length of mapping.  */
    1512              :   unsigned page_size;           /* System page size.  */
    1513              : #endif
    1514              : 
    1515              : public:
    1516         2894 :   elf_out (int fd, int e)
    1517         5676 :     :parent (fd, e), identtab (500), pos (0)
    1518              :   {
    1519              : #if MAPPED_WRITING
    1520         2894 :     offset = extent = 0;
    1521         2894 :     page_size = sysconf (_SC_PAGE_SIZE);
    1522         2894 :     if (page_size < SECTION_ALIGN)
    1523              :       /* Something really strange.  */
    1524            0 :       set_error (EINVAL);
    1525              : #endif
    1526         2894 :   }
    1527         2894 :   ~elf_out ()
    1528         2894 :   {
    1529         2894 :     data::simple_memory.shrink (hdr);
    1530         2894 :     data::simple_memory.shrink (sectab);
    1531         2894 :     data::simple_memory.shrink (strtab);
    1532         2894 :   }
    1533              : 
    1534              : #if MAPPED_WRITING
    1535              : private:
    1536              :   void create_mapping (unsigned ext, bool extending = true);
    1537              :   void remove_mapping ();
    1538              : #endif
    1539              : 
    1540              : protected:
    1541              :   using allocator::grow;
    1542              :   char *grow (char *, unsigned needed) final override;
    1543              : #if MAPPED_WRITING
    1544              :   using allocator::shrink;
    1545              :   void shrink (char *) final override;
    1546              : #endif
    1547              : 
    1548              : public:
    1549         5794 :   unsigned get_section_limit () const
    1550              :   {
    1551         5794 :     return sectab.pos / sizeof (section);
    1552              :   }
    1553              : 
    1554              : protected:
    1555              :   unsigned add (unsigned type, unsigned name = 0,
    1556              :                 unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
    1557              :   unsigned write (const data &);
    1558              : #if MAPPED_WRITING
    1559              :   unsigned write (const bytes_out &);
    1560              : #endif
    1561              : 
    1562              : public:
    1563              :   /* IDENTIFIER to strtab offset.  */
    1564              :   unsigned name (tree ident);
    1565              :   /* String literal to strtab offset.  */
    1566              :   unsigned name (const char *n);
    1567              :   /* Qualified name of DECL to strtab offset.  */
    1568              :   unsigned qualified_name (tree decl, bool is_defn);
    1569              : 
    1570              : private:
    1571              :   unsigned strtab_write (const char *s, unsigned l);
    1572              :   void strtab_write (tree decl, int);
    1573              : 
    1574              : public:
    1575              :   /* Add a section with contents or strings.  */
    1576              :   unsigned add (const bytes_out &, bool string_p, unsigned name);
    1577              : 
    1578              : public:
    1579              :   /* Begin and end writing.  */
    1580              :   bool begin ();
    1581              :   bool end ();
    1582              : };
    1583              : 
    1584              : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
    1585              :    Data always checked for CRC.  */
    1586              : 
    1587              : bool
    1588        21424 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1589              : {
    1590        21424 :   unsigned snum = source->find (name);
    1591              : 
    1592        21424 :   return begin (loc, source, snum, name);
    1593              : }
    1594              : 
    1595              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1596              : 
    1597              : bool
    1598       229026 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1599              : {
    1600       229026 :   if (!source->read (this, source->find (snum))
    1601       229026 :       || !size || !check_crc ())
    1602              :     {
    1603            0 :       source->set_error (elf::E_BAD_DATA);
    1604            0 :       source->shrink (*this);
    1605            0 :       if (name)
    1606            0 :         error_at (loc, "section %qs is missing or corrupted", name);
    1607              :       else
    1608            0 :         error_at (loc, "section #%u is missing or corrupted", snum);
    1609            0 :       return false;
    1610              :     }
    1611       229026 :   pos = 4;
    1612       229026 :   return true;
    1613              : }
    1614              : 
    1615              : /* Finish reading a section.  */
    1616              : 
    1617              : bool
    1618       227914 : bytes_in::end (elf_in *src)
    1619              : {
    1620       227914 :   if (more_p ())
    1621           13 :     set_overrun ();
    1622       227914 :   if (overrun)
    1623           13 :     src->set_error ();
    1624              : 
    1625       227914 :   src->shrink (*this);
    1626              : 
    1627       227914 :   return !overrun;
    1628              : }
    1629              : 
    1630              : /* Begin writing buffer.  */
    1631              : 
    1632              : void
    1633       329954 : bytes_out::begin (bool need_crc)
    1634              : {
    1635            0 :   if (need_crc)
    1636            0 :     pos = 4;
    1637            0 :   memory->grow (*this, 0, false);
    1638       308260 : }
    1639              : 
    1640              : /* Finish writing buffer.  Stream out to SINK as named section NAME.
    1641              :    Return section number or 0 on failure.  If CRC_PTR is true, crc
    1642              :    the data.  Otherwise it is a string section.  */
    1643              : 
    1644              : unsigned
    1645       329954 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1646              : {
    1647       329954 :   lengths[3] += pos;
    1648       329954 :   spans[3]++;
    1649              : 
    1650       329954 :   set_crc (crc_ptr);
    1651       329954 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1652       329954 :   memory->shrink (*this);
    1653              : 
    1654       329954 :   return sec_num;
    1655              : }
    1656              : 
    1657              : /* Close and open the file, without destroying it.  */
    1658              : 
    1659              : void
    1660            9 : elf_in::freeze ()
    1661              : {
    1662            9 :   gcc_checking_assert (!is_frozen ());
    1663              : #if MAPPED_READING
    1664            9 :   if (munmap (hdr.buffer, hdr.pos) < 0)
    1665            0 :     set_error (errno);
    1666              : #endif
    1667            9 :   if (close (fd) < 0)
    1668            0 :     set_error (errno);
    1669            9 :   fd = -1;
    1670            9 : }
    1671              : 
    1672              : bool
    1673            9 : elf_in::defrost (const char *name)
    1674              : {
    1675            9 :   gcc_checking_assert (is_frozen ());
    1676            9 :   struct stat stat;
    1677              : 
    1678            9 :   fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
    1679            9 :   if (fd < 0 || fstat (fd, &stat) < 0)
    1680            0 :     set_error (errno);
    1681              :   else
    1682              :     {
    1683            9 :       bool ok = hdr.pos == unsigned (stat.st_size);
    1684              : #ifndef HOST_LACKS_INODE_NUMBERS
    1685            9 :       if (device != stat.st_dev
    1686            9 :           || inode != stat.st_ino)
    1687              :         ok = false;
    1688              : #endif
    1689            9 :       if (!ok)
    1690            0 :         set_error (EMFILE);
    1691              : #if MAPPED_READING
    1692            0 :       if (ok)
    1693              :         {
    1694            9 :           char *mapping = reinterpret_cast<char *>
    1695            9 :             (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
    1696            9 :           if (mapping == MAP_FAILED)
    1697            0 :           fail:
    1698            0 :               set_error (errno);
    1699              :           else
    1700              :             {
    1701            9 :               if (madvise (mapping, hdr.pos, MADV_RANDOM))
    1702            0 :                 goto fail;
    1703              : 
    1704              :               /* These buffers are never NULL in this case.  */
    1705            9 :               strtab.buffer = mapping + strtab.pos;
    1706            9 :               sectab.buffer = mapping + sectab.pos;
    1707            9 :               hdr.buffer = mapping;
    1708              :             }
    1709              :         }
    1710              : #endif
    1711              :     }
    1712              : 
    1713            9 :   return !get_error ();
    1714              : }
    1715              : 
    1716              : /* Read at current position into BUFFER.  Return true on success.  */
    1717              : 
    1718              : const char *
    1719       235018 : elf_in::read (data *data, unsigned pos, unsigned length)
    1720              : {
    1721              : #if MAPPED_READING
    1722       235018 :   if (pos + length > hdr.pos)
    1723              :     {
    1724            0 :       set_error (EINVAL);
    1725            0 :       return NULL;
    1726              :     }
    1727              : #else
    1728              :   if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
    1729              :     {
    1730              :       set_error (errno);
    1731              :       return NULL;
    1732              :     }
    1733              : #endif
    1734       235018 :   grow (*data, length);
    1735              : #if MAPPED_READING
    1736       235018 :   data->buffer = hdr.buffer + pos;
    1737              : #else
    1738              :   if (::read (fd, data->buffer, data->size) != ssize_t (length))
    1739              :     {
    1740              :       set_error (errno);
    1741              :       shrink (*data);
    1742              :       return NULL;
    1743              :     }
    1744              : #endif
    1745              : 
    1746       235018 :   return data->buffer;
    1747              : }
    1748              : 
    1749              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1750              : 
    1751              : const elf::section *
    1752       232022 : elf_in::find (unsigned snum, unsigned type)
    1753              : {
    1754       232022 :   const section *sec = get_section (snum);
    1755       232022 :   if (!snum || !sec || sec->type != type)
    1756            0 :     return NULL;
    1757              :   return sec;
    1758              : }
    1759              : 
    1760              : /* Find a section NAME and TYPE.  Return section number, or zero on
    1761              :    failure.  */
    1762              : 
    1763              : unsigned
    1764        21469 : elf_in::find (const char *sname)
    1765              : {
    1766       137440 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1767              :     {
    1768       137440 :       const section *sec
    1769       137440 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1770              : 
    1771       274880 :       if (0 == strcmp (sname, name (sec->name)))
    1772        21469 :         return pos / sizeof (section);
    1773              :     }
    1774              : 
    1775              :   return 0;
    1776              : }
    1777              : 
    1778              : /* Begin reading file.  Verify header.  Pull in section and string
    1779              :    tables.  Return true on success.  */
    1780              : 
    1781              : bool
    1782         3020 : elf_in::begin (location_t loc)
    1783              : {
    1784         3020 :   if (!parent::begin ())
    1785              :     return false;
    1786              : 
    1787         2996 :   struct stat stat;
    1788         2996 :   unsigned size = 0;
    1789         2996 :   if (!fstat (fd, &stat))
    1790              :     {
    1791              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1792         2996 :       device = stat.st_dev;
    1793         2996 :       inode = stat.st_ino;
    1794              : #endif
    1795              :       /* Never generate files > 4GB, check we've not been given one.  */
    1796         2996 :       if (stat.st_size == unsigned (stat.st_size))
    1797         2996 :         size = unsigned (stat.st_size);
    1798              :     }
    1799              : 
    1800              : #if MAPPED_READING
    1801              :   /* MAP_SHARED so that the file is backing store.  If someone else
    1802              :      concurrently writes it, they're wrong.  */
    1803         2996 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1804         2996 :   if (mapping == MAP_FAILED)
    1805              :     {
    1806            0 :     fail:
    1807            0 :       set_error (errno);
    1808            0 :       return false;
    1809              :     }
    1810              :   /* We'll be hopping over this randomly.  Some systems declare the
    1811              :      first parm as char *, and other declare it as void *.  */
    1812         2996 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1813            0 :     goto fail;
    1814              : 
    1815         2996 :   hdr.buffer = (char *)mapping;
    1816              : #else
    1817              :   read (&hdr, 0, sizeof (header));
    1818              : #endif
    1819         2996 :   hdr.pos = size; /* Record size of the file.  */
    1820              : 
    1821         2996 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1822         2996 :   if (!h)
    1823              :     return false;
    1824              : 
    1825         2996 :   if (h->ident.magic[0] != 0x7f
    1826         2996 :       || h->ident.magic[1] != 'E'
    1827         2996 :       || h->ident.magic[2] != 'L'
    1828         2996 :       || h->ident.magic[3] != 'F')
    1829              :     {
    1830            0 :       error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
    1831            0 :     failed:
    1832            0 :       shrink (hdr);
    1833            0 :       return false;
    1834              :     }
    1835              : 
    1836              :   /* We expect a particular format -- the ELF is not intended to be
    1837              :      distributable.  */
    1838         2996 :   if (h->ident.klass != MY_CLASS
    1839         2996 :       || h->ident.data != MY_ENDIAN
    1840         2996 :       || h->ident.version != EV_CURRENT
    1841         2996 :       || h->type != ET_NONE
    1842         2996 :       || h->machine != EM_NONE
    1843         2996 :       || h->ident.osabi != OSABI_NONE)
    1844              :     {
    1845            0 :       error_at (loc, "unexpected encapsulation format or type");
    1846            0 :       goto failed;
    1847              :     }
    1848              : 
    1849         2996 :   int e = -1;
    1850         2996 :   if (!h->shoff || h->shentsize != sizeof (section))
    1851              :     {
    1852            0 :     malformed:
    1853            0 :       set_error (e);
    1854            0 :       error_at (loc, "encapsulation is malformed");
    1855            0 :       goto failed;
    1856              :     }
    1857              : 
    1858         2996 :   unsigned strndx = h->shstrndx;
    1859         2996 :   unsigned shnum = h->shnum;
    1860         2996 :   if (shnum == SHN_XINDEX)
    1861              :     {
    1862            0 :       if (!read (&sectab, h->shoff, sizeof (section)))
    1863              :         {
    1864            0 :         section_table_fail:
    1865            0 :           e = errno;
    1866            0 :           goto malformed;
    1867              :         }
    1868            0 :       shnum = get_section (0)->size;
    1869              :       /* Freeing does mean we'll re-read it in the case we're not
    1870              :          mapping, but this is going to be rare.  */
    1871            0 :       shrink (sectab);
    1872              :     }
    1873              : 
    1874         2996 :   if (!shnum)
    1875            0 :     goto malformed;
    1876              : 
    1877         2996 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1878            0 :     goto section_table_fail;
    1879              : 
    1880         2996 :   if (strndx == SHN_XINDEX)
    1881            0 :     strndx = get_section (0)->link;
    1882              : 
    1883         2996 :   if (!read (&strtab, find (strndx, SHT_STRTAB)))
    1884            0 :     goto malformed;
    1885              : 
    1886              :   /* The string table should be at least one byte, with NUL chars
    1887              :      at either end.  */
    1888         2996 :   if (!(strtab.size && !strtab.buffer[0]
    1889         2996 :         && !strtab.buffer[strtab.size - 1]))
    1890            0 :     goto malformed;
    1891              : 
    1892              : #if MAPPED_READING
    1893              :   /* Record the offsets of the section and string tables.  */
    1894         2996 :   sectab.pos = h->shoff;
    1895         2996 :   strtab.pos = shnum * sizeof (section);
    1896              : #else
    1897              :   shrink (hdr);
    1898              : #endif
    1899              : 
    1900         2996 :   return true;
    1901              : }
    1902              : 
    1903              : /* Create a new mapping.  */
    1904              : 
    1905              : #if MAPPED_WRITING
    1906              : void
    1907         3628 : elf_out::create_mapping (unsigned ext, bool extending)
    1908              : {
    1909              :   /* A wrapper around posix_fallocate, falling back to ftruncate
    1910              :      if the underlying filesystem does not support the operation.  */
    1911         7105 :   auto allocate = [](int fd, off_t offset, off_t length)
    1912              :     {
    1913              : #ifdef HAVE_POSIX_FALLOCATE
    1914         3477 :       int result = posix_fallocate (fd, offset, length);
    1915         3477 :       if (result != EINVAL && result != ENOTSUP)
    1916         3477 :         return result == 0;
    1917              :       /* Not supported by the underlying filesystem, fallback to ftruncate.  */
    1918              : #endif
    1919            0 :       return ftruncate (fd, offset + length) == 0;
    1920              :     };
    1921              : 
    1922         3628 :   void *mapping = MAP_FAILED;
    1923         3628 :   if (extending && ext < 1024 * 1024)
    1924              :     {
    1925         3312 :       if (allocate (fd, offset, ext * 2))
    1926         3312 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1927         3312 :                         MAP_SHARED, fd, offset);
    1928         3312 :       if (mapping != MAP_FAILED)
    1929              :         ext *= 2;
    1930              :     }
    1931              :   if (mapping == MAP_FAILED)
    1932              :     {
    1933          316 :       if (!extending || allocate (fd, offset, ext))
    1934          316 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1935          316 :                         MAP_SHARED, fd, offset);
    1936          316 :       if (mapping == MAP_FAILED)
    1937              :         {
    1938            0 :           set_error (errno);
    1939              :           mapping = NULL;
    1940              :           ext = 0;
    1941              :         }
    1942              :     }
    1943         3628 :   hdr.buffer = (char *)mapping;
    1944         3628 :   extent = ext;
    1945         3628 : }
    1946              : #endif
    1947              : 
    1948              : /* Flush out the current mapping.  */
    1949              : 
    1950              : #if MAPPED_WRITING
    1951              : void
    1952         3634 : elf_out::remove_mapping ()
    1953              : {
    1954         3634 :   if (hdr.buffer)
    1955              :     {
    1956              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1957              :          subsequent overlapping remap.  */
    1958         3628 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1959         3628 :           || munmap (hdr.buffer, extent))
    1960              :         /* We're somewhat screwed at this point.  */
    1961            0 :         set_error (errno);
    1962              :     }
    1963              : 
    1964         3634 :   hdr.buffer = NULL;
    1965         3634 : }
    1966              : #endif
    1967              : 
    1968              : /* Grow a mapping of PTR to be NEEDED bytes long.  This gets
    1969              :    interesting if the new size grows the EXTENT.  */
    1970              : 
    1971              : char *
    1972       793562 : elf_out::grow (char *data, unsigned needed)
    1973              : {
    1974       793562 :   if (!data)
    1975              :     {
    1976              :       /* First allocation, check we're aligned.  */
    1977       335518 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1978              : #if MAPPED_WRITING
    1979       335518 :       data = hdr.buffer + (pos - offset);
    1980              : #endif
    1981              :     }
    1982              : 
    1983              : #if MAPPED_WRITING
    1984       793562 :   unsigned off = data - hdr.buffer;
    1985       793562 :   if (off + needed > extent)
    1986              :     {
    1987              :       /* We need to grow the mapping.  */
    1988          701 :       unsigned lwm = off & ~(page_size - 1);
    1989          701 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1990              : 
    1991          701 :       gcc_checking_assert (hwm > extent);
    1992              : 
    1993          701 :       remove_mapping ();
    1994              : 
    1995          701 :       offset += lwm;
    1996          701 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    1997              : 
    1998          701 :       data = hdr.buffer + (off - lwm);
    1999              :     }
    2000              : #else
    2001              :   data = allocator::grow (data, needed);
    2002              : #endif
    2003              : 
    2004       793562 :   return data;
    2005              : }
    2006              : 
    2007              : #if MAPPED_WRITING
    2008              : /* Shrinking is a NOP.  */
    2009              : void
    2010       335518 : elf_out::shrink (char *)
    2011              : {
    2012       335518 : }
    2013              : #endif
    2014              : 
    2015              : /* Write S of length L to the strtab buffer.  L must include the ending
    2016              :    NUL, if that's what you want.  */
    2017              : 
    2018              : unsigned
    2019      1483700 : elf_out::strtab_write (const char *s, unsigned l)
    2020              : {
    2021      1483700 :   if (strtab.pos + l > strtab.size)
    2022         1304 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2023      1483700 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2024      1483700 :   unsigned res = strtab.pos;
    2025      1483700 :   strtab.pos += l;
    2026      1483700 :   return res;
    2027              : }
    2028              : 
    2029              : /* Write qualified name of decl.  INNER >0 if this is a definition, <0
    2030              :    if this is a qualifier of an outer name.  */
    2031              : 
    2032              : void
    2033       591907 : elf_out::strtab_write (tree decl, int inner)
    2034              : {
    2035       591907 :   tree ctx = CP_DECL_CONTEXT (decl);
    2036       591907 :   if (TYPE_P (ctx))
    2037         6331 :     ctx = TYPE_NAME (ctx);
    2038       591907 :   if (ctx != global_namespace)
    2039       301714 :     strtab_write (ctx, -1);
    2040              : 
    2041       591907 :   tree name = DECL_NAME (decl);
    2042       591907 :   if (!name)
    2043          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2044       591907 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2045              : 
    2046       591907 :   if (inner)
    2047       426100 :     strtab_write (&"::{}"[inner+1], 2);
    2048       591907 : }
    2049              : 
    2050              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2051              :    already there.  */
    2052              : 
    2053              : unsigned
    2054       157330 : elf_out::name (tree ident)
    2055              : {
    2056       157330 :   unsigned res = 0;
    2057       157330 :   if (ident)
    2058              :     {
    2059       157278 :       bool existed;
    2060       157278 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2061       157278 :       if (!existed)
    2062       279924 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2063       139962 :                               IDENTIFIER_LENGTH (ident) + 1);
    2064       157278 :       res = *slot;
    2065              :     }
    2066       157330 :   return res;
    2067              : }
    2068              : 
    2069              : /* Map LITERAL to strtab offset.  Does not detect duplicates and
    2070              :    expects LITERAL to remain live until strtab is written out.  */
    2071              : 
    2072              : unsigned
    2073        35538 : elf_out::name (const char *literal)
    2074              : {
    2075        35538 :   return strtab_write (literal, strlen (literal) + 1);
    2076              : }
    2077              : 
    2078              : /* Map a DECL's qualified name to strtab offset.  Does not detect
    2079              :    duplicates.  */
    2080              : 
    2081              : unsigned
    2082       290193 : elf_out::qualified_name (tree decl, bool is_defn)
    2083              : {
    2084       290193 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2085       290193 :   unsigned result = strtab.pos;
    2086              : 
    2087       290193 :   strtab_write (decl, is_defn);
    2088       290193 :   strtab_write ("", 1);
    2089              : 
    2090       290193 :   return result;
    2091              : }
    2092              : 
    2093              : /* Add section to file.  Return section number.  TYPE & NAME identify
    2094              :    the section.  OFF and SIZE identify the file location of its
    2095              :    data.  FLAGS contains additional info.  */
    2096              : 
    2097              : unsigned
    2098       335512 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2099              :               unsigned flags)
    2100              : {
    2101       335512 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2102       335512 :   if (sectab.pos + sizeof (section) > sectab.size)
    2103         4835 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2104       335512 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2105       335512 :   memset (sec, 0, sizeof (section));
    2106       335512 :   sec->type = type;
    2107       335512 :   sec->flags = flags;
    2108       335512 :   sec->name = name;
    2109       335512 :   sec->offset = off;
    2110       335512 :   sec->size = size;
    2111       335512 :   if (flags & SHF_STRINGS)
    2112         5529 :     sec->entsize = 1;
    2113              : 
    2114       335512 :   unsigned res = sectab.pos;
    2115       335512 :   sectab.pos += sizeof (section);
    2116       335512 :   return res / sizeof (section);
    2117              : }
    2118              : 
    2119              : /* Pad to the next alignment boundary, then write BUFFER to disk.
    2120              :    Return the position of the start of the write, or zero on failure.   */
    2121              : 
    2122              : unsigned
    2123        11122 : elf_out::write (const data &buffer)
    2124              : {
    2125              : #if MAPPED_WRITING
    2126              :   /* HDR is always mapped.  */
    2127        11122 :   if (&buffer != &hdr)
    2128              :     {
    2129         5564 :       bytes_out out (this);
    2130         5564 :       grow (out, buffer.pos, true);
    2131         5564 :       if (out.buffer)
    2132         5564 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2133         5564 :       shrink (out);
    2134         5564 :     }
    2135              :   else
    2136              :     /* We should have been aligned during the first allocation.  */
    2137         5558 :     gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    2138              : #else
    2139              :   if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
    2140              :     {
    2141              :       set_error (errno);
    2142              :       return 0;
    2143              :     }
    2144              : #endif
    2145        11122 :   unsigned res = pos;
    2146        11122 :   pos += buffer.pos;
    2147              : 
    2148        11122 :   if (unsigned padding = -pos & (SECTION_ALIGN - 1))
    2149              :     {
    2150              : #if !MAPPED_WRITING
    2151              :       /* Align the section on disk, should help the necessary copies.
    2152              :          fseeking to extend is non-portable.  */
    2153              :       static char zero[SECTION_ALIGN];
    2154              :       if (::write (fd, &zero, padding) != ssize_t (padding))
    2155              :         set_error (errno);
    2156              : #endif
    2157         9466 :       pos += padding;
    2158              :     }
    2159        11122 :   return res;
    2160              : }
    2161              : 
    2162              : /* Write a streaming buffer.  It must be using us as an allocator.  */
    2163              : 
    2164              : #if MAPPED_WRITING
    2165              : unsigned
    2166       329954 : elf_out::write (const bytes_out &buf)
    2167              : {
    2168       329954 :   gcc_checking_assert (buf.memory == this);
    2169              :   /* A directly mapped buffer.  */
    2170       329954 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2171              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2172       329954 :   unsigned res = pos;
    2173       329954 :   pos += buf.pos;
    2174              : 
    2175              :   /* Align up.  We're not going to advance into the next page. */
    2176       329954 :   pos += -pos & (SECTION_ALIGN - 1);
    2177              : 
    2178       329954 :   return res;
    2179              : }
    2180              : #endif
    2181              : 
    2182              : /* Write data and add section.  STRING_P is true for a string
    2183              :    section, false for PROGBITS.  NAME identifies the section (0 is the
    2184              :    empty name).  DATA is the contents.  Return section number or 0 on
    2185              :    failure (0 is the undef section).  */
    2186              : 
    2187              : unsigned
    2188       329954 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2189              : {
    2190       329954 :   unsigned off = write (data);
    2191              : 
    2192       659908 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2193       329954 :               off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
    2194              : }
    2195              : 
    2196              : /* Begin writing the file.  Initialize the section table and write an
    2197              :    empty header.  Return false on failure.  */
    2198              : 
    2199              : bool
    2200         2776 : elf_out::begin ()
    2201              : {
    2202         2776 :   if (!parent::begin ())
    2203              :     return false;
    2204              : 
    2205              :   /* Let the allocators pick a default.  */
    2206         2776 :   data::simple_memory.grow (strtab, 0, false);
    2207         2776 :   data::simple_memory.grow (sectab, 0, false);
    2208              : 
    2209              :   /* The string table starts with an empty string.  */
    2210         2776 :   name ("");
    2211              : 
    2212              :   /* Create the UNDEF section.  */
    2213         2776 :   add (SHT_NONE);
    2214              : 
    2215              : #if MAPPED_WRITING
    2216              :   /* Start a mapping.  */
    2217         2776 :   create_mapping (EXPERIMENT (page_size,
    2218              :                               (32767 + page_size) & ~(page_size - 1)));
    2219         2776 :   if (!hdr.buffer)
    2220              :     return false;
    2221              : #endif
    2222              : 
    2223              :   /* Write an empty header.  */
    2224         2776 :   grow (hdr, sizeof (header), true);
    2225         2776 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2226         2776 :   memset (h, 0, sizeof (header));
    2227         2776 :   hdr.pos = hdr.size;
    2228         2776 :   write (hdr);
    2229         2776 :   return !get_error ();
    2230              : }
    2231              : 
    2232              : /* Finish writing the file.  Write out the string & section tables.
    2233              :    Fill in the header.  Return true on error.  */
    2234              : 
    2235              : bool
    2236         2894 : elf_out::end ()
    2237              : {
    2238         2894 :   if (fd >= 0)
    2239              :     {
    2240              :       /* Write the string table.  */
    2241         2782 :       unsigned strnam = name (".strtab");
    2242         2782 :       unsigned stroff = write (strtab);
    2243         2782 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2244              :                              SHF_STRINGS);
    2245              : 
    2246              :       /* Store escape values in section[0].  */
    2247         2782 :       if (strndx >= SHN_LORESERVE)
    2248              :         {
    2249            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2250            0 :           strndx = SHN_XINDEX;
    2251              :         }
    2252         2782 :       unsigned shnum = sectab.pos / sizeof (section);
    2253         2782 :       if (shnum >= SHN_LORESERVE)
    2254              :         {
    2255            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2256            0 :           shnum = SHN_XINDEX;
    2257              :         }
    2258              : 
    2259         2782 :       unsigned shoff = write (sectab);
    2260              : 
    2261              : #if MAPPED_WRITING
    2262         2782 :       if (offset)
    2263              :         {
    2264          151 :           remove_mapping ();
    2265          151 :           offset = 0;
    2266          151 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2267              :                           false);
    2268              :         }
    2269         2782 :       unsigned length = pos;
    2270              : #else
    2271              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2272              :         set_error (errno);
    2273              : #endif
    2274              :       /* Write header.  */
    2275         2782 :       if (!get_error ())
    2276              :         {
    2277              :           /* Write the correct header now.  */
    2278         2782 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2279         2782 :           h->ident.magic[0] = 0x7f;
    2280         2782 :           h->ident.magic[1] = 'E';   /* Elrond */
    2281         2782 :           h->ident.magic[2] = 'L';   /* is an */
    2282         2782 :           h->ident.magic[3] = 'F';   /* elf.  */
    2283         2782 :           h->ident.klass = MY_CLASS;
    2284         2782 :           h->ident.data =  MY_ENDIAN;
    2285         2782 :           h->ident.version = EV_CURRENT;
    2286         2782 :           h->ident.osabi = OSABI_NONE;
    2287         2782 :           h->type = ET_NONE;
    2288         2782 :           h->machine = EM_NONE;
    2289         2782 :           h->version = EV_CURRENT;
    2290         2782 :           h->shoff = shoff;
    2291         2782 :           h->ehsize = sizeof (header);
    2292         2782 :           h->shentsize = sizeof (section);
    2293         2782 :           h->shnum = shnum;
    2294         2782 :           h->shstrndx = strndx;
    2295              : 
    2296         2782 :           pos = 0;
    2297         2782 :           write (hdr);
    2298              :         }
    2299              : 
    2300              : #if MAPPED_WRITING
    2301         2782 :       remove_mapping ();
    2302         2782 :       if (ftruncate (fd, length))
    2303            0 :         set_error (errno);
    2304              : #endif
    2305              :     }
    2306              : 
    2307         2894 :   data::simple_memory.shrink (sectab);
    2308         2894 :   data::simple_memory.shrink (strtab);
    2309              : 
    2310         2894 :   return parent::end ();
    2311              : }
    2312              : 
    2313              : /********************************************************************/
    2314              : 
    2315              : /* A dependency set.  This is used during stream out to determine the
    2316              :    connectivity of the graph.  Every namespace-scope declaration that
    2317              :    needs writing has a depset.  The depset is filled with the (depsets
    2318              :    of) declarations within this module that it references.  For a
    2319              :    declaration that'll generally be named types.  For definitions
    2320              :    it'll also be declarations in the body.
    2321              : 
    2322              :    From that we can convert the graph to a DAG, via determining the
    2323              :    Strongly Connected Clusters.  Each cluster is streamed
    2324              :    independently, and thus we achieve lazy loading.
    2325              : 
    2326              :    Other decls that get a depset are namespaces themselves and
    2327              :    unnameable declarations.   */
    2328              : 
    2329              : class depset {
    2330              : private:
    2331              :   tree entity;  /* Entity, or containing namespace.  */
    2332              :   uintptr_t discriminator;  /* Flags or identifier.  */
    2333              : 
    2334              : public:
    2335              :   /* The kinds of entity the depset could describe.  The ordering is
    2336              :      significant, see entity_kind_name.  */
    2337              :   enum entity_kind
    2338              :   {
    2339              :     EK_DECL,            /* A decl.  */
    2340              :     EK_SPECIALIZATION,  /* A specialization.  */
    2341              :     EK_PARTIAL,         /* A partial specialization.  */
    2342              :     EK_USING,           /* A using declaration (at namespace scope).  */
    2343              :     EK_NAMESPACE,       /* A namespace.  */
    2344              :     EK_TU_LOCAL,        /* A TU-local decl for ADL.  */
    2345              :     EK_REDIRECT,        /* Redirect to a template_decl.  */
    2346              :     EK_EXPLICIT_HWM,
    2347              :     EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded.  */
    2348              :     EK_FOR_BINDING,     /* A decl being inserted for a binding.  */
    2349              :     EK_INNER_DECL,      /* A decl defined outside of its imported
    2350              :                            context.  */
    2351              :     EK_DIRECT_HWM = EK_PARTIAL + 1,
    2352              : 
    2353              :     EK_BITS = 3         /* Only need to encode below EK_EXPLICIT_HWM.  */
    2354              :   };
    2355              :   static_assert (EK_EXPLICIT_HWM < (1u << EK_BITS),
    2356              :                  "not enough bits reserved for entity_kind");
    2357              : 
    2358              : private:
    2359              :   /* Placement of bit fields in discriminator.  */
    2360              :   enum disc_bits
    2361              :   {
    2362              :     DB_ZERO_BIT, /* Set to disambiguate identifier from flags  */
    2363              :     DB_SPECIAL_BIT, /* First dep slot is special.  */
    2364              :     DB_KIND_BIT, /* Kind of the entity.  */
    2365              :     DB_KIND_BITS = EK_BITS,
    2366              :     DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
    2367              :     DB_IS_PENDING_BIT,          /* Is a maybe-pending entity.  */
    2368              :     DB_TU_LOCAL_BIT,            /* Is a TU-local entity.  */
    2369              :     DB_REF_GLOBAL_BIT,          /* Refers to a GMF TU-local entity.  */
    2370              :     DB_REF_PURVIEW_BIT,         /* Refers to a purview TU-local entity.  */
    2371              :     DB_EXPOSE_GLOBAL_BIT,       /* Exposes a GMF TU-local entity.  */
    2372              :     DB_EXPOSE_PURVIEW_BIT,      /* Exposes a purview TU-local entity.  */
    2373              :     DB_IGNORED_EXPOSURE_BIT,    /* Only seen where exposures are ignored.  */
    2374              :     DB_IMPORTED_BIT,            /* An imported entity.  */
    2375              :     DB_UNREACHED_BIT,           /* A yet-to-be reached entity.  */
    2376              :     DB_MAYBE_RECURSIVE_BIT,     /* An entity maybe in a recursive cluster.  */
    2377              :     DB_ENTRY_BIT,               /* The first reached recursive dep.  */
    2378              :     DB_HIDDEN_BIT,              /* A hidden binding.  */
    2379              :     /* The following bits are not independent, but enumerating them is
    2380              :        awkward.  */
    2381              :     DB_TYPE_SPEC_BIT,           /* Specialization in the type table.  */
    2382              :     DB_FRIEND_SPEC_BIT,         /* An instantiated template friend.  */
    2383              :     DB_HWM,
    2384              :   };
    2385              :   static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
    2386              :                  "not enough bits in discriminator");
    2387              : 
    2388              : public:
    2389              :   /* The first slot is special for EK_SPECIALIZATIONS it is a
    2390              :      spec_entry pointer.  It is not relevant for the SCC
    2391              :      determination.  */
    2392              :   vec<depset *> deps;  /* Depsets we reference.  */
    2393              : 
    2394              : public:
    2395              :   unsigned cluster; /* Strongly connected cluster, later entity number  */
    2396              :   unsigned section; /* Section written to.  */
    2397              :   /* During SCC construction, section is lowlink, until the depset is
    2398              :      removed from the stack.  See Tarjan algorithm for details.  */
    2399              : 
    2400              : private:
    2401              :   /* Construction via factories.  Destruction via hash traits.  */
    2402              :   depset (tree entity);
    2403              :   ~depset ();
    2404              : 
    2405              : public:
    2406              :   static depset *make_binding (tree, tree);
    2407              :   static depset *make_entity (tree, entity_kind, bool = false);
    2408              :   /* Late setting a binding name -- /then/ insert into hash!  */
    2409              :   inline void set_binding_name (tree name)
    2410              :   {
    2411              :     gcc_checking_assert (!get_name ());
    2412              :     discriminator = reinterpret_cast<uintptr_t> (name);
    2413              :   }
    2414              : 
    2415              : private:
    2416      5868290 :   template<unsigned I> void set_flag_bit ()
    2417              :   {
    2418            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2419      5868290 :     discriminator |= 1u << I;
    2420      3709748 :   }
    2421      6202861 :   template<unsigned I> void clear_flag_bit ()
    2422              :   {
    2423            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2424      6202861 :     discriminator &= ~(1u << I);
    2425      6202861 :   }
    2426    594394066 :   template<unsigned I> bool get_flag_bit () const
    2427              :   {
    2428            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2429    722643916 :     return bool ((discriminator >> I) & 1);
    2430              :   }
    2431              : 
    2432              : public:
    2433    582983851 :   bool is_binding () const
    2434              :   {
    2435    138162459 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2436              :   }
    2437    303663886 :   entity_kind get_entity_kind () const
    2438              :   {
    2439        39132 :     if (is_binding ())
    2440              :       return EK_BINDING;
    2441    229308409 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2442              :   }
    2443              :   const char *entity_kind_name () const;
    2444              : 
    2445              : public:
    2446      9647708 :   bool has_defn () const
    2447              :   {
    2448              :     /* Never consider TU-local entities as having definitions, since
    2449              :        we will never be accessing them from importers anyway.  */
    2450      9647708 :     return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
    2451              :   }
    2452              : 
    2453              : public:
    2454              :   /* This entity might be found other than by namespace-scope lookup;
    2455              :      see module_state::write_pendings for more details.  */
    2456      2397642 :   bool is_pending_entity () const
    2457              :   {
    2458      3810726 :     return (get_entity_kind () == EK_SPECIALIZATION
    2459      1413084 :             || get_entity_kind () == EK_PARTIAL
    2460      3772630 :             || (get_entity_kind () == EK_DECL
    2461      1330843 :                 && get_flag_bit<DB_IS_PENDING_BIT> ()));
    2462              :   }
    2463              : 
    2464              : public:
    2465              :   /* Only consider global module entities as being TU-local
    2466              :      when STRICT is set; otherwise, as an extension we support
    2467              :      emitting declarations referencing TU-local GMF entities
    2468              :      (and only check purview entities), to assist in migration.  */
    2469     51460724 :   bool is_tu_local (bool strict = false) const
    2470              :   {
    2471              :     /* Non-strict is only intended for migration purposes, so
    2472              :        for simplicity's sake we only care about whether this is
    2473              :        a non-purview variable or function at namespace scope;
    2474              :        these are the most common cases (coming from C), and
    2475              :        that way we don't have to care about diagnostics for
    2476              :        nested types and so forth.  */
    2477     22707211 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2478     51460724 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2479     51460724 :             && (strict
    2480         3304 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2481         2174 :                 || !NAMESPACE_SCOPE_P (inner)
    2482         2147 :                 || (DECL_LANG_SPECIFIC (inner)
    2483         2108 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2484              :   }
    2485      1450378 :   bool refs_tu_local (bool strict = false) const
    2486              :   {
    2487      1450378 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2488      1450378 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2489              :   }
    2490      3418476 :   bool is_exposure (bool strict = false) const
    2491              :   {
    2492      3418476 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2493      3418476 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2494              :   }
    2495      2353497 :   bool is_ignored_exposure_context () const
    2496              :   {
    2497      2353497 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2498              :   }
    2499              : 
    2500              : public:
    2501     31403340 :   bool is_import () const
    2502              :   {
    2503      9641336 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2504              :   }
    2505     19023795 :   bool is_unreached () const
    2506              :   {
    2507      1215524 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2508              :   }
    2509      2011727 :   bool is_hidden () const
    2510              :   {
    2511      2011727 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2512              :   }
    2513      1214618 :   bool is_maybe_recursive () const
    2514              :   {
    2515      1214618 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2516              :   }
    2517         1126 :   bool is_entry () const
    2518              :   {
    2519         1126 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2520              :   }
    2521      1476759 :   bool is_type_spec () const
    2522              :   {
    2523      1476759 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2524              :   }
    2525      1476759 :   bool is_friend_spec () const
    2526              :   {
    2527      1476759 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2528              :   }
    2529              : 
    2530              : public:
    2531              :   /* We set these bit outside of depset.  */
    2532           88 :   void set_hidden_binding ()
    2533              :   {
    2534           88 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2535           88 :   }
    2536           36 :   void clear_hidden_binding ()
    2537              :   {
    2538           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2539           36 :   }
    2540              : 
    2541              : public:
    2542     11410215 :   bool is_special () const
    2543              :   {
    2544     11410215 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2545              :   }
    2546      2158542 :   void set_special ()
    2547              :   {
    2548      2158542 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2549            0 :   }
    2550              : 
    2551              : public:
    2552    195538130 :   tree get_entity () const
    2553              :   {
    2554     51460724 :     return entity;
    2555              :   }
    2556     20517826 :   tree get_name () const
    2557              :   {
    2558     20517826 :     gcc_checking_assert (is_binding ());
    2559     20517826 :     return reinterpret_cast <tree> (discriminator);
    2560              :   }
    2561              : 
    2562              : public:
    2563              :   /* Traits for a hash table of pointers to bindings.  */
    2564              :   struct traits {
    2565              :     /* Each entry is a pointer to a depset. */
    2566              :     typedef depset *value_type;
    2567              :     /* We lookup by container:maybe-identifier pair.  */
    2568              :     typedef std::pair<tree,tree> compare_type;
    2569              : 
    2570              :     static const bool empty_zero_p = true;
    2571              : 
    2572              :     /* hash and equality for compare_type.  */
    2573     19372252 :     inline static hashval_t hash (const compare_type &p)
    2574              :     {
    2575     19372252 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2576     19372252 :       if (p.second)
    2577              :         {
    2578       221783 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2579       221783 :           h = iterative_hash_hashval_t (h, nh);
    2580              :         }
    2581     19372252 :       return h;
    2582              :     }
    2583    111776731 :     inline static bool equal (const value_type b, const compare_type &p)
    2584              :     {
    2585    111776731 :       if (b->entity != p.first)
    2586              :         return false;
    2587              : 
    2588     13907747 :       if (p.second)
    2589        41769 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2590              :       else
    2591     13865978 :         return !b->is_binding ();
    2592              :     }
    2593              : 
    2594              :     /* (re)hasher for a binding itself.  */
    2595     87328217 :     inline static hashval_t hash (const value_type b)
    2596              :     {
    2597     87328217 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2598     87328217 :       if (b->is_binding ())
    2599              :         {
    2600      4967626 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2601      4967626 :           h = iterative_hash_hashval_t (h, nh);
    2602              :         }
    2603     87328217 :       return h;
    2604              :     }
    2605              : 
    2606              :     /* Empty via NULL.  */
    2607            0 :     static inline void mark_empty (value_type &p) {p = NULL;}
    2608              :     static inline bool is_empty (value_type p) {return !p;}
    2609              : 
    2610              :     /* Nothing is deletable.  Everything is insertable.  */
    2611              :     static bool is_deleted (value_type) { return false; }
    2612              :     static void mark_deleted (value_type) { gcc_unreachable (); }
    2613              : 
    2614              :     /* We own the entities in the hash table.  */
    2615      3080105 :     static void remove (value_type p)
    2616              :     {
    2617      3080105 :       delete (p);
    2618      3080105 :     }
    2619              :   };
    2620              : 
    2621              : public:
    2622              :   class hash : public hash_table<traits> {
    2623              :     typedef traits::compare_type key_t;
    2624              :     typedef hash_table<traits> parent;
    2625              : 
    2626              :   public:
    2627              :     vec<depset *> worklist;  /* Worklist of decls to walk.  */
    2628              :     hash *chain;             /* Original table.  */
    2629              :     depset *current;         /* Current depset being depended.  */
    2630              :     unsigned section;        /* When writing out, the section.  */
    2631              :     bool reached_unreached;  /* We reached an unreached entity.  */
    2632              :     bool writing_merge_key;  /* We're writing merge key information.  */
    2633              : 
    2634              :   private:
    2635              :     bool ignore_exposure;    /* In a context where referencing a TU-local
    2636              :                                 entity is not an exposure.  */
    2637              : 
    2638              :   private:
    2639              :     /* Information needed to do dependent ADL for discovering
    2640              :        more decl-reachable entities.  Cached during walking to
    2641              :        prevent tree marking from interfering with lookup.  */
    2642      8595802 :     struct dep_adl_info {
    2643              :       /* The name of the call or operator.  */
    2644              :       tree name = NULL_TREE;
    2645              :       /* If not ERROR_MARK, a rewrite candidate for this operator.  */
    2646              :       tree_code rewrite = ERROR_MARK;
    2647              :       /* Argument list for the call.  */
    2648              :       vec<tree, va_gc>* args = make_tree_vector ();
    2649              :     };
    2650              :     vec<dep_adl_info> dep_adl_entity_list;
    2651              : 
    2652              :   public:
    2653       308244 :     hash (size_t size, hash *c = NULL)
    2654       616488 :       : parent (size), chain (c), current (NULL), section (0),
    2655       308244 :         reached_unreached (false), writing_merge_key (false),
    2656       308244 :         ignore_exposure (false)
    2657              :     {
    2658       308244 :       worklist.create (size);
    2659       308244 :       dep_adl_entity_list.create (16);
    2660       308244 :     }
    2661       308244 :     ~hash ()
    2662              :     {
    2663       308244 :       worklist.release ();
    2664       308244 :       dep_adl_entity_list.release ();
    2665       308244 :     }
    2666              : 
    2667              :   public:
    2668    141224914 :     bool is_key_order () const
    2669              :     {
    2670    141224914 :       return chain != NULL;
    2671              :     }
    2672              : 
    2673              :   public:
    2674              :     /* Returns a temporary override that will additionally consider this
    2675              :        to be a context where exposures of TU-local entities are ignored
    2676              :        if COND is true.  */
    2677       904053 :     temp_override<bool> ignore_exposure_if (bool cond)
    2678              :     {
    2679       709557 :       return make_temp_override (ignore_exposure, ignore_exposure || cond);
    2680              :     }
    2681              : 
    2682              :   private:
    2683              :     depset **entity_slot (tree entity, bool = true);
    2684              :     depset **binding_slot (tree ctx, tree name, bool = true);
    2685              :     depset *maybe_add_declaration (tree decl);
    2686              : 
    2687              :   public:
    2688              :     depset *find_dependency (tree entity);
    2689              :     depset *find_binding (tree ctx, tree name);
    2690              :     depset *make_dependency (tree decl, entity_kind);
    2691              :     void add_dependency (depset *);
    2692              : 
    2693              :   public:
    2694              :     void add_mergeable (depset *);
    2695              :     depset *add_dependency (tree decl, entity_kind);
    2696              :     void add_namespace_context (depset *, tree ns);
    2697              : 
    2698              :   private:
    2699              :     static bool add_binding_entity (tree, WMB_Flags, void *);
    2700              : 
    2701              :   public:
    2702              :     bool add_namespace_entities (tree ns, bitmap partitions);
    2703              :     void add_specializations (bool decl_p);
    2704              :     void add_partial_entities (vec<tree, va_gc> *);
    2705              :     void add_class_entities (vec<tree, va_gc> *);
    2706              :     void add_dependent_adl_entities (tree expr);
    2707              : 
    2708              :   private:
    2709              :     void add_deduction_guides (tree decl);
    2710              : 
    2711              :   public:
    2712              :     void find_dependencies (module_state *);
    2713              :     bool finalize_dependencies ();
    2714              :     vec<depset *> connect ();
    2715              : 
    2716              :   private:
    2717              :     bool diagnose_bad_internal_ref (depset *dep, bool strict = false);
    2718              :     bool diagnose_template_names_tu_local (depset *dep, bool strict = false);
    2719              :   };
    2720              : 
    2721              : public:
    2722              :   struct tarjan {
    2723              :     vec<depset *> result;
    2724              :     vec<depset *> stack;
    2725              :     unsigned index;
    2726              : 
    2727       308215 :     tarjan (unsigned size)
    2728       308215 :       : index (0)
    2729              :     {
    2730       308215 :       result.create (size);
    2731       308215 :       stack.create (50);
    2732       308215 :     }
    2733       308215 :     ~tarjan ()
    2734              :     {
    2735       308215 :       gcc_assert (!stack.length ());
    2736       308215 :       stack.release ();
    2737       308215 :     }
    2738              : 
    2739              :   public:
    2740              :     void connect (depset *);
    2741              :   };
    2742              : };
    2743              : 
    2744              : inline
    2745      3080105 : depset::depset (tree entity)
    2746      3080105 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2747              : {
    2748      3080105 :   deps.create (0);
    2749              : }
    2750              : 
    2751              : inline
    2752      3080105 : depset::~depset ()
    2753              : {
    2754      3080105 :   deps.release ();
    2755      3080105 : }
    2756              : 
    2757              : const char *
    2758        44362 : depset::entity_kind_name () const
    2759              : {
    2760              :   /* Same order as entity_kind.  */
    2761        44362 :   static const char *const names[] =
    2762              :     {"decl", "specialization", "partial", "using",
    2763              :      "namespace", "tu-local", "redirect", "binding"};
    2764        44362 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2765              :                  "names must have an entry for every explicit entity_kind");
    2766        44362 :   entity_kind kind = get_entity_kind ();
    2767        44362 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2768        44362 :   return names[kind];
    2769              : }
    2770              : 
    2771              : /* Create a depset for a namespace binding NS::NAME.  */
    2772              : 
    2773       155019 : depset *depset::make_binding (tree ns, tree name)
    2774              : {
    2775       155019 :   depset *binding = new depset (ns);
    2776              : 
    2777       155019 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2778              : 
    2779       155019 :   return binding;
    2780              : }
    2781              : 
    2782      2925086 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2783              : {
    2784      2925086 :   depset *r = new depset (entity);
    2785              : 
    2786      2925086 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2787      2925086 :                       | (ek << DB_KIND_BIT)
    2788      2925086 :                       | is_defn << DB_DEFN_BIT);
    2789              : 
    2790      2925086 :   return r;
    2791              : }
    2792              : 
    2793              : class pending_key
    2794              : {
    2795              : public:
    2796              :   tree ns;
    2797              :   tree id;
    2798              : };
    2799              : 
    2800              : template<>
    2801              : struct default_hash_traits<pending_key>
    2802              : {
    2803              :   using value_type = pending_key;
    2804              : 
    2805              :   static const bool empty_zero_p = false;
    2806     48445462 :   static hashval_t hash (const value_type &k)
    2807              :   {
    2808     48445462 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2809     48445462 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2810              : 
    2811     48445462 :     return h;
    2812              :   }
    2813     14824447 :   static bool equal (const value_type &k, const value_type &l)
    2814              :   {
    2815     14824447 :     return k.ns == l.ns && k.id == l.id;
    2816              :   }
    2817       221419 :   static void mark_empty (value_type &k)
    2818              :   {
    2819       221419 :     k.ns = k.id = NULL_TREE;
    2820              :   }
    2821         6077 :   static void mark_deleted (value_type &k)
    2822              :   {
    2823         6077 :     k.ns = NULL_TREE;
    2824         6077 :     gcc_checking_assert (k.id);
    2825         6077 :   }
    2826    359583611 :   static bool is_empty (const value_type &k)
    2827              :   {
    2828    359532770 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2829              :   }
    2830     19795252 :   static bool is_deleted (const value_type &k)
    2831              :   {
    2832     19795252 :     return k.ns == NULL_TREE && k.id != NULL_TREE;
    2833              :   }
    2834              :   static void remove (value_type &)
    2835              :   {
    2836              :   }
    2837              : };
    2838              : 
    2839              : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
    2840              : 
    2841              : /* Not-loaded entities that are keyed to a namespace-scope
    2842              :    identifier.  See module_state::write_pendings for details.  */
    2843              : pending_map_t *pending_table;
    2844              : 
    2845              : /* Decls that need some post processing once a batch of lazy loads has
    2846              :    completed.  */
    2847              : vec<tree, va_heap, vl_embed> *post_load_decls;
    2848              : 
    2849              : /* Some entities are keyed to another entitity for ODR purposes.
    2850              :    For example, at namespace scope, 'inline auto var = []{};', that
    2851              :    lambda is keyed to 'var', and follows its ODRness.  */
    2852              : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
    2853              : static keyed_map_t *keyed_table;
    2854              : 
    2855              : static tree get_keyed_decl_scope (tree);
    2856              : 
    2857              : /* Instantiations of temploid friends imported from another module
    2858              :    need to be attached to the same module as the temploid.  This maps
    2859              :    these decls to the temploid they are instantiated from, as there is
    2860              :    no other easy way to get this information.  */
    2861              : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
    2862              : 
    2863              : /********************************************************************/
    2864              : /* Tree streaming.   The tree streaming is very specific to the tree
    2865              :    structures themselves.  A tag indicates the kind of tree being
    2866              :    streamed.  -ve tags indicate backreferences to already-streamed
    2867              :    trees.  Backreferences are auto-numbered.  */
    2868              : 
    2869              : /* Tree tags.  */
    2870              : enum tree_tag {
    2871              :   tt_null,              /* NULL_TREE.  */
    2872              :   tt_tu_local,          /* A TU-local entity.  */
    2873              :   tt_fixed,             /* Fixed vector index.  */
    2874              : 
    2875              :   tt_node,              /* By-value node.  */
    2876              :   tt_decl,              /* By-value mergeable decl.  */
    2877              :   tt_tpl_parm,          /* Template parm.  */
    2878              : 
    2879              :   /* The ordering of the following 5 is relied upon in
    2880              :      trees_out::tree_node.  */
    2881              :   tt_id,                /* Identifier node.  */
    2882              :   tt_conv_id,           /* Conversion operator name.  */
    2883              :   tt_anon_id,           /* Anonymous name.  */
    2884              :   tt_lambda_id,         /* Lambda name.  */
    2885              :   tt_internal_id,       /* Internal name.  */
    2886              : 
    2887              :   tt_typedef_type,      /* A (possibly implicit) typedefed type.  */
    2888              :   tt_derived_type,      /* A type derived from another type.  */
    2889              :   tt_variant_type,      /* A variant of another type.  */
    2890              : 
    2891              :   tt_tinfo_var,         /* Typeinfo object. */
    2892              :   tt_tinfo_typedef,     /* Typeinfo typedef.  */
    2893              :   tt_ptrmem_type,       /* Pointer to member type.  */
    2894              :   tt_nttp_var,          /* NTTP_OBJECT VAR_DECL.  */
    2895              : 
    2896              :   tt_parm,              /* Function parameter or result.  */
    2897              :   tt_enum_value,        /* An enum value.  */
    2898              :   tt_enum_decl,         /* An enum decl.  */
    2899              :   tt_data_member,       /* Data member/using-decl.  */
    2900              : 
    2901              :   tt_binfo,             /* A BINFO.  */
    2902              :   tt_vtable,            /* A vtable.  */
    2903              :   tt_thunk,             /* A thunk.  */
    2904              :   tt_clone_ref,
    2905              : 
    2906              :   tt_entity,            /* A extra-cluster entity.  */
    2907              : 
    2908              :   tt_template,          /* The TEMPLATE_RESULT of a template.  */
    2909              : };
    2910              : 
    2911              : enum walk_kind {
    2912              :   WK_none,      /* No walk to do (a back- or fixed-ref happened).  */
    2913              :   WK_normal,    /* Normal walk (by-name if possible).  */
    2914              : 
    2915              :   WK_value,     /* By-value walk.  */
    2916              : };
    2917              : 
    2918              : enum merge_kind
    2919              : {
    2920              :   MK_unique,    /* Known unique.  */
    2921              :   MK_named,     /* Found by CTX, NAME + maybe_arg types etc.  */
    2922              :   MK_field,     /* Found by CTX and index on TYPE_FIELDS  */
    2923              :   MK_vtable,    /* Found by CTX and index on TYPE_VTABLES  */
    2924              :   MK_as_base,   /* Found by CTX.  */
    2925              : 
    2926              :   MK_partial,
    2927              : 
    2928              :   MK_enum,      /* Found by CTX, & 1stMemberNAME.  */
    2929              :   MK_keyed,     /* Found by key & index.  */
    2930              :   MK_local_type, /* Found by CTX, index.  */
    2931              : 
    2932              :   MK_friend_spec,  /* Like named, but has a tmpl & args too.  */
    2933              :   MK_local_friend, /* Found by CTX, index.  */
    2934              : 
    2935              :   MK_indirect_lwm = MK_enum,
    2936              : 
    2937              :   /* Template specialization kinds below. These are all found via
    2938              :      primary template and specialization args.  */
    2939              :   MK_template_mask = 0x10,  /* A template specialization.  */
    2940              : 
    2941              :   MK_tmpl_decl_mask = 0x4, /* In decl table.  */
    2942              : 
    2943              :   MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL.  */
    2944              : 
    2945              :   MK_type_spec = MK_template_mask,
    2946              :   MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
    2947              : 
    2948              :   MK_hwm = 0x20
    2949              : };
    2950              : /* This is more than a debugging array.  NULLs are used to determine
    2951              :    an invalid merge_kind number.  */
    2952              : static char const *const merge_kind_name[MK_hwm] =
    2953              :   {
    2954              :     "unique", "named", "field", "vtable",       /* 0...3  */
    2955              :     "asbase", "partial", "enum", "attached",    /* 4...7  */
    2956              : 
    2957              :     "local type", "friend spec", "local friend", NULL,  /* 8...11 */
    2958              :     NULL, NULL, NULL, NULL,
    2959              : 
    2960              :     "type spec", "type tmpl spec",  /* 16,17 type (template).  */
    2961              :     NULL, NULL,
    2962              : 
    2963              :     "decl spec", "decl tmpl spec",  /* 20,21 decl (template).  */
    2964              :     NULL, NULL,
    2965              :     NULL, NULL, NULL, NULL,
    2966              :     NULL, NULL, NULL, NULL,
    2967              :   };
    2968              : 
    2969              : /* Mergeable entity location data.  */
    2970              : struct merge_key {
    2971              :   cp_ref_qualifier ref_q : 2;
    2972              :   unsigned coro_disc : 2;  /* Discriminator for coroutine transforms.  */
    2973              :   unsigned iobj_p : 1;
    2974              :   unsigned xobj_p : 1;
    2975              :   unsigned index;
    2976              : 
    2977              :   tree ret;  /* Return type, if appropriate.  */
    2978              :   tree args; /* Arg types, if appropriate.  */
    2979              : 
    2980              :   tree constraints;  /* Constraints.  */
    2981              : 
    2982      2998431 :   merge_key ()
    2983      2998431 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2984      2998431 :      ret (NULL_TREE), args (NULL_TREE),
    2985      2998431 :      constraints (NULL_TREE)
    2986              :   {
    2987              :   }
    2988              : };
    2989              : 
    2990              : /* Hashmap of merged duplicates.  Usually decls, but can contain
    2991              :    BINFOs.  */
    2992              : typedef hash_map<tree,uintptr_t,
    2993              :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    2994              : duplicate_hash_map;
    2995              : 
    2996              : /* Data needed for post-processing.  */
    2997              : struct post_process_data {
    2998              :   tree decl;
    2999              :   location_t start_locus;
    3000              :   location_t end_locus;
    3001              :   bool returns_value;
    3002              :   bool returns_null;
    3003              :   bool returns_abnormally;
    3004              :   bool infinite_loop;
    3005              : };
    3006              : 
    3007              : /* Tree stream reader.  Note that reading a stream doesn't mark the
    3008              :    read trees with TREE_VISITED.  Thus it's quite safe to have
    3009              :    multiple concurrent readers.  Which is good, because lazy
    3010              :    loading.
    3011              : 
    3012              :    It's important that trees_in/out have internal linkage so that the
    3013              :    compiler knows core_bools, lang_type_bools and lang_decl_bools have
    3014              :    only a single caller (tree_node_bools) and inlines them appropriately.  */
    3015              : namespace {
    3016              : class trees_in : public bytes_in {
    3017              :   typedef bytes_in parent;
    3018              : 
    3019              : private:
    3020              :   module_state *state;          /* Module being imported.  */
    3021              :   vec<tree> back_refs;            /* Back references.  */
    3022              :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    3023              :   vec<post_process_data> post_decls;      /* Decls to post process.  */
    3024              :   vec<tree> post_types;           /* Types to post process.  */
    3025              :   unsigned unused;              /* Inhibit any interior TREE_USED
    3026              :                                    marking.  */
    3027              : 
    3028              : public:
    3029              :   trees_in (module_state *);
    3030              :   ~trees_in ();
    3031              : 
    3032              : public:
    3033              :   int insert (tree);
    3034              :   tree back_ref (int);
    3035              : 
    3036              : private:
    3037              :   tree start (unsigned = 0);
    3038              : 
    3039              : public:
    3040              :   /* Needed for binfo writing  */
    3041              :   bool core_bools (tree, bits_in&);
    3042              : 
    3043              : private:
    3044              :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    3045              :      bits.  */
    3046              :   bool core_vals (tree);
    3047              :   bool lang_type_bools (tree, bits_in&);
    3048              :   bool lang_type_vals (tree);
    3049              :   bool lang_decl_bools (tree, bits_in&);
    3050              :   bool lang_decl_vals (tree);
    3051              :   bool lang_vals (tree);
    3052              :   bool tree_node_bools (tree);
    3053              :   bool tree_node_vals (tree);
    3054              :   tree tree_value ();
    3055              :   tree decl_value ();
    3056              :   tree tpl_parm_value ();
    3057              : 
    3058              : private:
    3059              :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    3060              :   vec<tree, va_heap> *vec_chained_decls ();
    3061              :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    3062              :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    3063              :   tree tree_list (bool has_purpose);
    3064              : 
    3065              : public:
    3066              :   /* Read a tree node.  */
    3067              :   tree tree_node (bool is_use = false);
    3068              : 
    3069              : private:
    3070              :   bool install_entity (tree decl);
    3071              :   tree tpl_parms (unsigned &tpl_levels);
    3072              :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    3073              :   bool tpl_header (tree decl, unsigned *tpl_levels);
    3074              :   int fn_parms_init (tree);
    3075              :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    3076              :   unsigned add_indirect_tpl_parms (tree);
    3077              : public:
    3078              :   bool add_indirects (tree);
    3079              : 
    3080              : public:
    3081              :   /* Serialize various definitions. */
    3082              :   bool read_definition (tree decl);
    3083              : 
    3084              : private:
    3085              :   void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
    3086              :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    3087              :   static bool install_implicit_member (tree decl);
    3088              :   bool read_function_def (tree decl, tree maybe_template);
    3089              :   bool read_var_def (tree decl, tree maybe_template);
    3090              :   bool read_class_def (tree decl, tree maybe_template);
    3091              :   bool read_enum_def (tree decl, tree maybe_template);
    3092              : 
    3093              : public:
    3094              :   tree decl_container ();
    3095              :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    3096              :                       tree container, bool is_attached,
    3097              :                       bool is_imported_temploid_friend);
    3098              :   unsigned binfo_mergeable (tree *);
    3099              : 
    3100              : private:
    3101              :   tree key_local_type (const merge_key&, tree, tree);
    3102              :   uintptr_t *find_duplicate (tree existing);
    3103              :   void register_duplicate (tree decl, tree existing);
    3104              :   /* Mark as an already diagnosed bad duplicate.  */
    3105           54 :   void unmatched_duplicate (tree existing)
    3106              :   {
    3107          108 :     *find_duplicate (existing) |= 1;
    3108           54 :   }
    3109              : 
    3110              : public:
    3111       134694 :   bool is_duplicate (tree decl)
    3112              :   {
    3113       269388 :     return find_duplicate (decl) != NULL;
    3114              :   }
    3115       163152 :   tree maybe_duplicate (tree decl)
    3116              :   {
    3117       163152 :     if (uintptr_t *dup = find_duplicate (decl))
    3118         8907 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    3119              :     return decl;
    3120              :   }
    3121              :   tree odr_duplicate (tree decl, bool has_defn);
    3122              : 
    3123              : public:
    3124              :   /* Return the decls to postprocess.  */
    3125              :   const vec<post_process_data>& post_process ()
    3126              :   {
    3127              :     return post_decls;
    3128              :   }
    3129              :   /* Return the types to postprocess.  */
    3130              :   const vec<tree>& post_process_type ()
    3131              :   {
    3132              :     return post_types;
    3133              :   }
    3134              : private:
    3135              :   /* Register DATA for postprocessing.  */
    3136       139671 :   void post_process (post_process_data data)
    3137              :   {
    3138       139671 :     post_decls.safe_push (data);
    3139              :   }
    3140              :   /* Register TYPE for postprocessing.  */
    3141           36 :   void post_process_type (tree type)
    3142              :   {
    3143           36 :     gcc_checking_assert (TYPE_P (type));
    3144           36 :     post_types.safe_push (type);
    3145           36 :   }
    3146              : 
    3147              : private:
    3148              :   void assert_definition (tree, bool installing);
    3149              : };
    3150              : } // anon namespace
    3151              : 
    3152       211871 : trees_in::trees_in (module_state *state)
    3153       211871 :   :parent (), state (state), unused (0)
    3154              : {
    3155       211871 :   duplicates = NULL;
    3156       211871 :   back_refs.create (500);
    3157       211871 :   post_decls.create (0);
    3158       211871 :   post_types.create (0);
    3159       211871 : }
    3160              : 
    3161       211871 : trees_in::~trees_in ()
    3162              : {
    3163       314833 :   delete (duplicates);
    3164       211871 :   back_refs.release ();
    3165       211871 :   post_decls.release ();
    3166       211871 :   post_types.release ();
    3167       211871 : }
    3168              : 
    3169              : /* Tree stream writer.  */
    3170              : namespace {
    3171              : class trees_out : public bytes_out {
    3172              :   typedef bytes_out parent;
    3173              : 
    3174              : private:
    3175              :   module_state *state;          /* The module we are writing.  */
    3176              :   ptr_int_hash_map tree_map;    /* Trees to references */
    3177              :   depset::hash *dep_hash;       /* Dependency table.  */
    3178              :   int ref_num;                  /* Back reference number.  */
    3179              :   unsigned section;
    3180              :   bool writing_local_entities;  /* Whether we might walk into a TU-local
    3181              :                                    entity we need to emit placeholders for.  */
    3182              :   bool walking_bit_field_unit;  /* Whether we're walking the underlying
    3183              :                                    storage for a bit field.  There's no other
    3184              :                                    great way to detect this.  */
    3185              : #if CHECKING_P
    3186              :   int importedness;             /* Checker that imports not occurring
    3187              :                                    inappropriately.  +ve imports ok,
    3188              :                                    -ve imports not ok.  */
    3189              : #endif
    3190              : 
    3191              : public:
    3192              :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    3193              :   ~trees_out ();
    3194              : 
    3195              : private:
    3196              :   void mark_trees ();
    3197              :   void unmark_trees ();
    3198              : 
    3199              : public:
    3200              :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3201              :   void begin ();
    3202              :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3203              :   void end ();
    3204              : 
    3205              : public:
    3206              :   enum tags
    3207              :   {
    3208              :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3209              :     tag_value = 0,      /* Write by value.  */
    3210              :     tag_fixed           /* Lower bound on the fixed trees.  */
    3211              :   };
    3212              : 
    3213              : public:
    3214              :   /* The walk is used for three similar purposes:
    3215              : 
    3216              :       1. The initial scan for dependencies.
    3217              :       2. Once dependencies have been found, ordering them.
    3218              :       3. Writing dependencies to file (streaming_p).
    3219              : 
    3220              :      For cases where it matters, these accessers can be used to determine
    3221              :      which state we're in.  */
    3222    115440457 :   bool is_initial_scan () const
    3223              :   {
    3224    191934897 :     return !streaming_p () && !is_key_order ();
    3225              :   }
    3226     94420825 :   bool is_key_order () const
    3227              :   {
    3228     76494440 :     return dep_hash->is_key_order ();
    3229              :   }
    3230              : 
    3231              : public:
    3232              :   int insert (tree, walk_kind = WK_normal);
    3233              : 
    3234              : private:
    3235              :   void start (tree, bool = false);
    3236              : 
    3237              : private:
    3238              :   walk_kind ref_node (tree);
    3239              : public:
    3240              :   int get_tag (tree);
    3241       610936 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3242              :   {
    3243              : #if CHECKING_P
    3244       610936 :     importedness = i;
    3245              : #endif
    3246              :   }
    3247              : 
    3248              : private:
    3249              :   void core_bools (tree, bits_out&);
    3250              :   void core_vals (tree);
    3251              :   void lang_type_bools (tree, bits_out&);
    3252              :   void lang_type_vals (tree);
    3253              :   void lang_decl_bools (tree, bits_out&);
    3254              :   void lang_decl_vals (tree);
    3255              :   void lang_vals (tree);
    3256              :   void tree_node_bools (tree);
    3257              :   void tree_node_vals (tree);
    3258              : 
    3259              : private:
    3260              :   void chained_decls (tree);
    3261              :   void vec_chained_decls (tree);
    3262              :   void tree_vec (vec<tree, va_gc> *);
    3263              :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3264              :   void tree_list (tree, bool has_purpose);
    3265              : 
    3266              : private:
    3267              :   bool has_tu_local_dep (tree) const;
    3268              :   tree find_tu_local_decl (tree);
    3269              : 
    3270              : public:
    3271              :   /* Mark a node for by-value walking.  */
    3272              :   void mark_by_value (tree);
    3273              : 
    3274              : public:
    3275              :   void tree_node (tree);
    3276              : 
    3277              : private:
    3278              :   void install_entity (tree decl, depset *);
    3279              :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3280              :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3281              :   void fn_parms_fini (tree) {}
    3282              :   unsigned add_indirect_tpl_parms (tree);
    3283              : public:
    3284              :   void add_indirects (tree);
    3285              :   void fn_parms_init (tree);
    3286              :   void tpl_header (tree decl, unsigned *tpl_levels);
    3287              : 
    3288              : public:
    3289              :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3290              :   tree decl_container (tree decl);
    3291              :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3292              :                       tree container, depset *maybe_dep);
    3293              :   void binfo_mergeable (tree binfo);
    3294              : 
    3295              : private:
    3296              :   void key_local_type (merge_key&, tree, tree);
    3297              :   bool decl_node (tree, walk_kind ref);
    3298              :   void type_node (tree);
    3299              :   void tree_value (tree);
    3300              :   void tpl_parm_value (tree);
    3301              : 
    3302              : public:
    3303              :   void decl_value (tree, depset *);
    3304              : 
    3305              : public:
    3306              :   /* Serialize various definitions. */
    3307              :   void write_definition (tree decl, bool refs_tu_local = false);
    3308              :   void mark_declaration (tree decl, bool do_defn);
    3309              : 
    3310              : private:
    3311              :   void mark_function_def (tree decl);
    3312              :   void mark_var_def (tree decl);
    3313              :   void mark_class_def (tree decl);
    3314              :   void mark_enum_def (tree decl);
    3315              :   void mark_class_member (tree decl, bool do_defn = true);
    3316              :   void mark_binfos (tree type);
    3317              : 
    3318              : private:
    3319              :   void write_var_def (tree decl);
    3320              :   void write_function_def (tree decl);
    3321              :   void write_class_def (tree decl);
    3322              :   void write_enum_def (tree decl);
    3323              : 
    3324              : private:
    3325              :   static void assert_definition (tree);
    3326              : 
    3327              : public:
    3328              :   static void instrument ();
    3329              : 
    3330              : private:
    3331              :   /* Tree instrumentation. */
    3332              :   static unsigned tree_val_count;
    3333              :   static unsigned decl_val_count;
    3334              :   static unsigned back_ref_count;
    3335              :   static unsigned tu_local_count;
    3336              :   static unsigned null_count;
    3337              : };
    3338              : } // anon namespace
    3339              : 
    3340              : /* Instrumentation counters.  */
    3341              : unsigned trees_out::tree_val_count;
    3342              : unsigned trees_out::decl_val_count;
    3343              : unsigned trees_out::back_ref_count;
    3344              : unsigned trees_out::tu_local_count;
    3345              : unsigned trees_out::null_count;
    3346              : 
    3347       616504 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3348       616504 :                       unsigned section)
    3349      1233008 :   :parent (mem), state (state), tree_map (500),
    3350       616504 :    dep_hash (&deps), ref_num (0), section (section),
    3351       616504 :    writing_local_entities (false), walking_bit_field_unit (false)
    3352              : {
    3353              : #if CHECKING_P
    3354       616504 :   importedness = 0;
    3355              : #endif
    3356       616504 : }
    3357              : 
    3358       616504 : trees_out::~trees_out ()
    3359              : {
    3360       616504 : }
    3361              : 
    3362              : /********************************************************************/
    3363              : /* Location.  We're aware of the line-map concept and reproduce it
    3364              :    here.  Each imported module allocates a contiguous span of ordinary
    3365              :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3366              :    not pre-allocated.   The scattered linemaps of a module are
    3367              :    coalesced when writing.  */
    3368              : 
    3369              : 
    3370              : /* I use half-open [first,second) ranges.  */
    3371              : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
    3372              : 
    3373              : /* A range of locations.  */
    3374              : typedef std::pair<location_t,location_t> loc_range_t;
    3375              : 
    3376              : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3377              :    within imports.  Only extended when in an interface unit.
    3378              :    Interval zero corresponds to the forced header linemap(s).  This
    3379              :    is a singleton object.  */
    3380              : 
    3381              : class loc_spans {
    3382              : public:
    3383              :   /* An interval of line maps.  The line maps here represent a contiguous
    3384              :      non-imported range.  */
    3385         5986 :   struct span {
    3386              :     loc_range_t ordinary;       /* Ordinary map location range. */
    3387              :     loc_range_t macro;          /* Macro map location range.  */
    3388              :     /* Add to locs to get serialized loc.  */
    3389              :     location_diff_t ordinary_delta;
    3390              :     location_diff_t macro_delta;
    3391              :   };
    3392              : 
    3393              : private:
    3394              :   vec<span> *spans;
    3395              :   bool locs_exhausted_p;
    3396              : 
    3397              : public:
    3398              :   loc_spans ()
    3399              :     /* Do not preallocate spans, as that causes
    3400              :        --enable-detailed-mem-stats problems.  */
    3401              :     : spans (nullptr), locs_exhausted_p (false)
    3402              :   {
    3403              :   }
    3404        98539 :   ~loc_spans ()
    3405              :   {
    3406        98539 :     delete spans;
    3407        98539 :   }
    3408              : 
    3409              : public:
    3410          300 :   span &operator[] (unsigned ix)
    3411              :   {
    3412          600 :     return (*spans)[ix];
    3413              :   }
    3414              :   unsigned length () const
    3415              :   {
    3416              :     return spans->length ();
    3417              :   }
    3418              : 
    3419              : public:
    3420        15658 :   bool init_p () const
    3421              :   {
    3422        15658 :     return spans != nullptr;
    3423              :   }
    3424              :   /* Initializer.  */
    3425              :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3426              : 
    3427              :   /* Slightly skewed preprocessed files can cause us to miss an
    3428              :      initialization in some places.  Fallback initializer.  */
    3429         5811 :   void maybe_init ()
    3430              :   {
    3431         5811 :     if (!init_p ())
    3432            6 :       init (line_table, nullptr);
    3433         5811 :   }
    3434              : 
    3435              : public:
    3436              :   enum {
    3437              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3438              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3439              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3440              :   };
    3441              : 
    3442              : public:
    3443       462124 :   location_t main_start () const
    3444              :   {
    3445       462124 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3446              :   }
    3447              : 
    3448              : public:
    3449              :   void open (location_t);
    3450              :   void close ();
    3451              : 
    3452              : public:
    3453              :   /* Propagate imported linemaps to us, if needed.  */
    3454              :   bool maybe_propagate (module_state *import, location_t loc);
    3455              : 
    3456              : public:
    3457              :   /* Whether we can no longer represent new imported locations.  */
    3458            0 :   bool locations_exhausted_p () const
    3459              :   {
    3460            0 :     return locs_exhausted_p;
    3461              :   }
    3462            0 :   void report_location_exhaustion (location_t loc)
    3463              :   {
    3464            0 :     if (!locs_exhausted_p)
    3465              :       {
    3466              :         /* Just give the notice once.  */
    3467            0 :         locs_exhausted_p = true;
    3468            0 :         inform (loc, "unable to represent further imported source locations");
    3469              :       }
    3470              :   }
    3471              : 
    3472              : public:
    3473              :   const span *ordinary (location_t);
    3474              :   const span *macro (location_t);
    3475              : };
    3476              : 
    3477              : static loc_spans spans;
    3478              : 
    3479              : /* Information about ordinary locations we stream out.  */
    3480              : struct ord_loc_info
    3481              : {
    3482              :   const line_map_ordinary *src; // line map we're based on
    3483              :   line_map_uint_t offset;       // offset to this line
    3484              :   line_map_uint_t span;         // number of locs we span
    3485              :   line_map_uint_t remap;        // serialization
    3486              : 
    3487    304447299 :   static int compare (const void *a_, const void *b_)
    3488              :   {
    3489    304447299 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3490    304447299 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3491              : 
    3492    304447299 :     if (a->src != b->src)
    3493     66298559 :       return a->src < b->src ? -1 : +1;
    3494              : 
    3495              :     // Ensure no overlap
    3496    259613098 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3497              :                          || b->offset + b->span <= a->offset);
    3498              : 
    3499    259613098 :     gcc_checking_assert (a->offset != b->offset);
    3500    259613098 :     return a->offset < b->offset ? -1 : +1;
    3501              :   }
    3502              : };
    3503              : struct ord_loc_traits
    3504              : {
    3505              :   typedef ord_loc_info value_type;
    3506              :   typedef value_type compare_type;
    3507              : 
    3508              :   static const bool empty_zero_p = false;
    3509              : 
    3510    120144270 :   static hashval_t hash (const value_type &v)
    3511              :   {
    3512    100583404 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3513    120144270 :     return iterative_hash_hashval_t (v.offset, h);
    3514              :   }
    3515    133495864 :   static bool equal (const value_type &v, const compare_type p)
    3516              :   {
    3517    133495864 :     return v.src == p.src && v.offset == p.offset;
    3518              :   }
    3519              : 
    3520      9281351 :   static void mark_empty (value_type &v)
    3521              :   {
    3522      9281351 :     v.src = nullptr;
    3523              :   }
    3524    406277602 :   static bool is_empty (value_type &v)
    3525              :   {
    3526    406277602 :     return !v.src;
    3527              :   }
    3528              : 
    3529              :   static bool is_deleted (value_type &) { return false; }
    3530              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3531              : 
    3532      2257318 :   static void remove (value_type &) {}
    3533              : };
    3534              : /* Table keyed by ord_loc_info, used for noting.  */
    3535              : static  hash_table<ord_loc_traits> *ord_loc_table;
    3536              : /* Sorted vector, used for writing.  */
    3537              : static vec<ord_loc_info> *ord_loc_remap;
    3538              : 
    3539              : /* Information about macro locations we stream out.  */
    3540              : struct macro_loc_info
    3541              : {
    3542              :   const line_map_macro *src;    // original expansion
    3543              :   line_map_uint_t remap;        // serialization
    3544              : 
    3545      9329910 :   static int compare (const void *a_, const void *b_)
    3546              :   {
    3547      9329910 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3548      9329910 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3549              : 
    3550      9329910 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3551              :                          != MAP_START_LOCATION (b->src));
    3552      9329910 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3553              :       return -1;
    3554              :     else
    3555      4497970 :       return +1;
    3556              :   }
    3557              : };
    3558              : struct macro_loc_traits
    3559              : {
    3560              :   typedef macro_loc_info value_type;
    3561              :   typedef const line_map_macro *compare_type;
    3562              : 
    3563              :   static const bool empty_zero_p = false;
    3564              : 
    3565     10385539 :   static hashval_t hash (compare_type p)
    3566              :   {
    3567     10385539 :     return pointer_hash<const line_map_macro>::hash (p);
    3568              :   }
    3569      8873888 :   static hashval_t hash (const value_type &v)
    3570              :   {
    3571      8873888 :     return hash (v.src);
    3572              :   }
    3573              :   static bool equal (const value_type &v, const compare_type p)
    3574              :   {
    3575              :     return v.src == p;
    3576              :   }
    3577              : 
    3578       700888 :   static void mark_empty (value_type &v)
    3579              :   {
    3580       700888 :     v.src = nullptr;
    3581              :   }
    3582     32148808 :   static bool is_empty (value_type &v)
    3583              :   {
    3584     32148808 :     return !v.src;
    3585              :   }
    3586              : 
    3587              :   static bool is_deleted (value_type &) { return false; }
    3588              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3589              : 
    3590       159954 :   static void remove (value_type &) {}
    3591              : };
    3592              : /* Table keyed by line_map_macro, used for noting.  */
    3593              : static  hash_table<macro_loc_traits> *macro_loc_table;
    3594              : /* Sorted vector, used for writing.  */
    3595              : static vec<macro_loc_info> *macro_loc_remap;
    3596              : 
    3597              : /* Indirection to allow bsearching imports by ordinary location.  */
    3598              : static vec<module_state *> *ool;
    3599              : 
    3600              : /********************************************************************/
    3601              : /* Data needed by a module during the process of loading.  */
    3602              : struct GTY(()) slurping {
    3603              : 
    3604              :   /* Remap import's module numbering to our numbering.  Values are
    3605              :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3606              :   vec<unsigned, va_heap, vl_embed> *
    3607              :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3608              : 
    3609              :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3610              : 
    3611              :   /* This map is only for header imports themselves -- the global
    3612              :      headers bitmap hold it for the current TU.  */
    3613              :   bitmap headers;       /* Transitive set of direct imports, including
    3614              :                            self.  Used for macro visibility and
    3615              :                            priority.  */
    3616              : 
    3617              :   /* These objects point into the mmapped area, unless we're not doing
    3618              :      that, or we got frozen or closed.  In those cases they point to
    3619              :      buffers we own.  */
    3620              :   bytes_in macro_defs;  /* Macro definitions.  */
    3621              :   bytes_in macro_tbl;   /* Macro table.  */
    3622              : 
    3623              :   /* Location remapping.  first->ordinary, second->macro.  */
    3624              :   range_t GTY((skip)) loc_deltas;
    3625              : 
    3626              :   unsigned current;     /* Section currently being loaded.  */
    3627              :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3628              :   unsigned lru;         /* An LRU counter.  */
    3629              : 
    3630              :  public:
    3631              :   slurping (elf_in *);
    3632              :   ~slurping ();
    3633              : 
    3634              :  public:
    3635              :   /* Close the ELF file, if it's open.  */
    3636         5360 :   void close ()
    3637              :   {
    3638         5360 :     if (from)
    3639              :       {
    3640         2938 :         from->end ();
    3641         5876 :         delete from;
    3642         2938 :         from = NULL;
    3643              :       }
    3644         5360 :   }
    3645              : 
    3646              :  public:
    3647              :   void release_macros ();
    3648              : 
    3649              :  public:
    3650         2996 :   void alloc_remap (unsigned size)
    3651              :   {
    3652         2996 :     gcc_assert (!remap);
    3653         2996 :     vec_safe_reserve (remap, size);
    3654         6384 :     for (unsigned ix = size; ix--;)
    3655         3388 :       remap->quick_push (0);
    3656         2996 :   }
    3657      1213200 :   unsigned remap_module (unsigned owner)
    3658              :   {
    3659      1213200 :     if (owner < remap->length ())
    3660      1213200 :       return (*remap)[owner] >> 1;
    3661              :     return 0;
    3662              :   }
    3663              : 
    3664              :  public:
    3665              :   /* GC allocation.  But we must explicitly delete it.   */
    3666         3020 :   static void *operator new (size_t x)
    3667              :   {
    3668         6040 :     return ggc_alloc_atomic (x);
    3669              :   }
    3670         2938 :   static void operator delete (void *p)
    3671              :   {
    3672         2938 :     ggc_free (p);
    3673         2938 :   }
    3674              : };
    3675              : 
    3676         3020 : slurping::slurping (elf_in *from)
    3677         3020 :   : remap (NULL), from (from),
    3678         3020 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3679         3020 :     loc_deltas (0, 0),
    3680         3020 :     current (~0u), remaining (0), lru (0)
    3681              : {
    3682         3020 : }
    3683              : 
    3684         2938 : slurping::~slurping ()
    3685              : {
    3686         2938 :   vec_free (remap);
    3687         2938 :   remap = NULL;
    3688         2938 :   release_macros ();
    3689         2938 :   close ();
    3690         2938 : }
    3691              : 
    3692         5360 : void slurping::release_macros ()
    3693              : {
    3694         5360 :   if (macro_defs.size)
    3695          892 :     elf_in::release (from, macro_defs);
    3696         5360 :   if (macro_tbl.size)
    3697            0 :     elf_in::release (from, macro_tbl);
    3698         5360 : }
    3699              : 
    3700              : /* Flags for extensions that end up being streamed.  */
    3701              : 
    3702              : enum streamed_extensions {
    3703              :   SE_OPENMP_SIMD = 1 << 0,
    3704              :   SE_OPENMP = 1 << 1,
    3705              :   SE_OPENACC = 1 << 2,
    3706              :   SE_BITS = 3
    3707              : };
    3708              : 
    3709              : /* Counter indices.  */
    3710              : enum module_state_counts
    3711              : {
    3712              :   MSC_sec_lwm,
    3713              :   MSC_sec_hwm,
    3714              :   MSC_pendings,
    3715              :   MSC_entities,
    3716              :   MSC_namespaces,
    3717              :   MSC_using_directives,
    3718              :   MSC_bindings,
    3719              :   MSC_macros,
    3720              :   MSC_inits,
    3721              :   MSC_HWM
    3722              : };
    3723              : 
    3724              : /********************************************************************/
    3725              : struct module_state_config;
    3726              : 
    3727              : /* Increasing levels of loadedness.  */
    3728              : enum module_loadedness {
    3729              :   ML_NONE,              /* Not loaded.  */
    3730              :   ML_CONFIG,            /* Config loaed.  */
    3731              :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3732              :   ML_LANGUAGE,          /* Language loaded.  */
    3733              : };
    3734              : 
    3735              : /* Increasing levels of directness (toplevel) of import.  */
    3736              : enum module_directness {
    3737              :   MD_NONE,              /* Not direct.  */
    3738              :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3739              :   MD_DIRECT,            /* Direct import.  */
    3740              :   MD_PURVIEW_DIRECT,    /* Direct import in purview.  */
    3741              : };
    3742              : 
    3743              : /* State of a particular module. */
    3744              : 
    3745              : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3746              :  public:
    3747              :   /* We always import & export ourselves.  */
    3748              :   bitmap imports;       /* Transitive modules we're importing.  */
    3749              :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3750              : 
    3751              :   /* For a named module interface A.B, parent is A and name is B.
    3752              :      For a partition M:P, parent is M and name is P.
    3753              :      For an implementation unit I, parent is I's interface and name is NULL.
    3754              :      Otherwise parent is NULL and name will be the flatname.  */
    3755              :   module_state *parent;
    3756              :   tree name;
    3757              : 
    3758              :   slurping *slurp;      /* Data for loading.  */
    3759              : 
    3760              :   const char *flatname; /* Flatname of module.  */
    3761              :   char *filename;       /* CMI Filename */
    3762              : 
    3763              :   /* Indices into the entity_ary.  */
    3764              :   unsigned entity_lwm;
    3765              :   unsigned entity_num;
    3766              : 
    3767              :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3768              :      don't have a range.  */
    3769              :   loc_range_t GTY((skip)) ordinary_locs;
    3770              :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3771              : 
    3772              :   /* LOC is first set too the importing location.  When initially
    3773              :      loaded it refers to a module loc whose parent is the importing
    3774              :      location.  */
    3775              :   location_t loc;       /* Location referring to module itself.  */
    3776              :   unsigned crc;         /* CRC we saw reading it in. */
    3777              : 
    3778              :   unsigned mod;         /* Module owner number.  */
    3779              :   unsigned remap;       /* Remapping during writing.  */
    3780              : 
    3781              :   unsigned short subst; /* Mangle subst if !0.  */
    3782              : 
    3783              :   /* How loaded this module is.  */
    3784              :   enum module_loadedness loadedness : 2;
    3785              : 
    3786              :   bool module_p : 1;    /* /The/ module of this TU.  */
    3787              :   bool header_p : 1;    /* Is a header unit.  */
    3788              :   bool interface_p : 1; /* An interface.  */
    3789              :   bool partition_p : 1; /* A partition.  */
    3790              : 
    3791              :   /* How directly this module is imported.  */
    3792              :   enum module_directness directness : 2;
    3793              : 
    3794              :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3795              :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3796              :                            do it again  */
    3797              :   bool active_init_p : 1; /* This module's global initializer needs
    3798              :                            calling.  */
    3799              :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3800              :   bool visited_p : 1;    /* A walk-once flag. */
    3801              :   /* Record extensions emitted or permitted.  */
    3802              :   unsigned extensions : SE_BITS;
    3803              :   /* 14 bits used, 2 bits remain  */
    3804              : 
    3805              :  public:
    3806              :   module_state (tree name, module_state *, bool);
    3807              :   ~module_state ();
    3808              : 
    3809              :  public:
    3810         2998 :   void release ()
    3811              :   {
    3812         2998 :     imports = exports = NULL;
    3813         2998 :     slurped ();
    3814         2938 :   }
    3815         5420 :   void slurped ()
    3816              :   {
    3817         5420 :     delete slurp;
    3818         5420 :     slurp = NULL;
    3819         5420 :   }
    3820      1300011 :   elf_in *from () const
    3821              :   {
    3822      1300011 :     return slurp->from;
    3823              :   }
    3824              : 
    3825              :  public:
    3826              :   /* Kind of this module.  */
    3827       143013 :   bool is_module () const
    3828              :   {
    3829       143013 :     return module_p;
    3830              :   }
    3831      2480672 :   bool is_header () const
    3832              :   {
    3833      2480672 :     return header_p;
    3834              :   }
    3835          615 :   bool is_interface () const
    3836              :   {
    3837          615 :     return interface_p;
    3838              :   }
    3839       333601 :   bool is_partition () const
    3840              :   {
    3841       333601 :     return partition_p;
    3842              :   }
    3843              : 
    3844              :   /* How this module is used in the current TU.  */
    3845         3225 :   bool is_exported () const
    3846              :   {
    3847         3225 :     return exported_p;
    3848              :   }
    3849        20918 :   bool is_direct () const
    3850              :   {
    3851        20918 :     return directness >= MD_DIRECT;
    3852              :   }
    3853          280 :   bool is_purview_direct () const
    3854              :   {
    3855          280 :     return directness == MD_PURVIEW_DIRECT;
    3856              :   }
    3857          469 :   bool is_partition_direct () const
    3858              :   {
    3859          469 :     return directness == MD_PARTITION_DIRECT;
    3860              :   }
    3861              : 
    3862              :  public:
    3863              :   /* Is this a real module?  */
    3864        16151 :   bool has_location () const
    3865              :   {
    3866        16151 :     return loc != UNKNOWN_LOCATION;
    3867              :   }
    3868              : 
    3869              :  public:
    3870              :   bool check_circular_import (location_t loc);
    3871              : 
    3872              :  public:
    3873              :   void mangle (bool include_partition);
    3874              : 
    3875              :  public:
    3876              :   void set_import (module_state const *, bool is_export);
    3877              :   void announce (const char *) const;
    3878              : 
    3879              :  public:
    3880              :   /* Read and write module.  */
    3881              :   bool write_begin (elf_out *to, cpp_reader *,
    3882              :                     module_state_config &, unsigned &crc);
    3883              :   void write_end (elf_out *to, cpp_reader *,
    3884              :                   module_state_config &, unsigned &crc);
    3885              :   bool read_initial (cpp_reader *);
    3886              :   bool read_preprocessor (bool);
    3887              :   bool read_language (bool);
    3888              : 
    3889              :  public:
    3890              :   /* Read a section.  */
    3891              :   bool load_section (unsigned snum, binding_slot *mslot);
    3892              :   /* Lazily read a section.  */
    3893              :   bool lazy_load (unsigned index, binding_slot *mslot);
    3894              : 
    3895              :  public:
    3896              :   /* Juggle a limited number of file numbers.  */
    3897              :   static void freeze_an_elf ();
    3898              :   bool maybe_defrost ();
    3899              : 
    3900              :  public:
    3901              :   void maybe_completed_reading ();
    3902              :   bool check_read (bool outermost, bool ok);
    3903              : 
    3904              :  private:
    3905              :   /* The README, for human consumption.  */
    3906              :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3907              :   void write_env (elf_out *to);
    3908              : 
    3909              :  private:
    3910              :   /* Import tables. */
    3911              :   void write_imports (bytes_out &cfg, bool direct);
    3912              :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3913              : 
    3914              :  private:
    3915              :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3916              :   bool read_imports (cpp_reader *, line_maps *);
    3917              : 
    3918              :  private:
    3919              :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3920              :   bool read_partitions (unsigned);
    3921              : 
    3922              :  private:
    3923              :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3924              :   bool read_config (struct module_state_config &, bool = true);
    3925              :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3926              :   bool read_counts (unsigned *);
    3927              : 
    3928              :  public:
    3929              :   void note_cmi_name ();
    3930              : 
    3931              :  private:
    3932              :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3933              :                                   unsigned *crc_ptr);
    3934              :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3935              : 
    3936              :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3937              :   tree read_namespace (bytes_in &sec);
    3938              : 
    3939              :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3940              :                          unsigned, unsigned *crc_ptr);
    3941              :   bool read_namespaces (unsigned);
    3942              : 
    3943              :   unsigned write_using_directives (elf_out *to, depset::hash &,
    3944              :                                    vec<depset *> spaces, unsigned *crc_ptr);
    3945              :   bool read_using_directives (unsigned);
    3946              : 
    3947              :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3948              :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3949              :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3950              :   bool read_cluster (unsigned snum);
    3951              :   bool open_slurp (cpp_reader *);
    3952              : 
    3953              :  private:
    3954              :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3955              :   bool read_inits (unsigned count);
    3956              : 
    3957              :  private:
    3958              :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3959              :                            depset::hash &, unsigned *crc_ptr);
    3960              :   bool read_pendings (unsigned count);
    3961              : 
    3962              :  private:
    3963              :   void write_entities (elf_out *to, vec<depset *> depsets,
    3964              :                        unsigned count, unsigned *crc_ptr);
    3965              :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3966              : 
    3967              :  private:
    3968              :   void write_init_maps ();
    3969              :   range_t write_prepare_maps (module_state_config *, bool);
    3970              :   bool read_prepare_maps (const module_state_config *);
    3971              : 
    3972              :   void write_ordinary_maps (elf_out *to, range_t &,
    3973              :                             bool, unsigned *crc_ptr);
    3974              :   bool read_ordinary_maps (line_map_uint_t, unsigned);
    3975              :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3976              :   bool read_macro_maps (line_map_uint_t);
    3977              : 
    3978              :   void write_diagnostic_classification (elf_out *, diagnostics::context *,
    3979              :                                         unsigned *);
    3980              :   bool read_diagnostic_classification (diagnostics::context *);
    3981              : 
    3982              :  private:
    3983              :   void write_define (bytes_out &, const cpp_macro *);
    3984              :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3985              :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3986              :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3987              :   bool read_macros ();
    3988              :   void install_macros ();
    3989              : 
    3990              :  public:
    3991              :   void import_macros ();
    3992              : 
    3993              :  public:
    3994              :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    3995              :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    3996              : 
    3997              :  public:
    3998              :   static bool note_location (location_t);
    3999              :   static void write_location (bytes_out &, location_t);
    4000              :   location_t read_location (bytes_in &) const;
    4001              : 
    4002              :  public:
    4003              :   void set_flatname ();
    4004        52858 :   const char *get_flatname () const
    4005              :   {
    4006        52858 :     return flatname;
    4007              :   }
    4008              :   location_t imported_from () const;
    4009              : 
    4010              :  public:
    4011              :   void set_filename (const Cody::Packet &);
    4012              :   bool do_import (cpp_reader *, bool outermost);
    4013              :   bool check_importable (cpp_reader *);
    4014              : };
    4015              : 
    4016              : /* Hash module state by name.  This cannot be a member of
    4017              :    module_state, because of GTY restrictions.  We never delete from
    4018              :    the hash table, but ggc_ptr_hash doesn't support that
    4019              :    simplification.  */
    4020              : 
    4021              : struct module_state_hash : ggc_ptr_hash<module_state> {
    4022              :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    4023              : 
    4024              :   static inline hashval_t hash (const value_type m);
    4025              :   static inline hashval_t hash (const compare_type &n);
    4026              :   static inline bool equal (const value_type existing,
    4027              :                             const compare_type &candidate);
    4028              : };
    4029              : 
    4030        11579 : module_state::module_state (tree name, module_state *parent, bool partition)
    4031        11579 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4032        11579 :     parent (parent), name (name), slurp (NULL),
    4033        11579 :     flatname (NULL), filename (NULL),
    4034        11579 :     entity_lwm (~0u >> 1), entity_num (0),
    4035        11579 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4036        11579 :     loc (UNKNOWN_LOCATION),
    4037        11579 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4038              : {
    4039        11579 :   loadedness = ML_NONE;
    4040              : 
    4041        11579 :   module_p = header_p = interface_p = partition_p = false;
    4042              : 
    4043        11579 :   directness = MD_NONE;
    4044        11579 :   exported_p = false;
    4045              : 
    4046        11579 :   cmi_noted_p = false;
    4047        11579 :   active_init_p = false;
    4048              : 
    4049        11579 :   partition_p = partition;
    4050              : 
    4051        11579 :   inform_cmi_p = false;
    4052        11579 :   visited_p = false;
    4053              : 
    4054        11579 :   extensions = 0;
    4055        11579 :   if (name && TREE_CODE (name) == STRING_CST)
    4056              :     {
    4057         1913 :       header_p = true;
    4058              : 
    4059         1913 :       const char *string = TREE_STRING_POINTER (name);
    4060         1913 :       gcc_checking_assert (string[0] == '.'
    4061              :                            ? IS_DIR_SEPARATOR (string[1])
    4062              :                            : IS_ABSOLUTE_PATH (string));
    4063              :     }
    4064              : 
    4065        11579 :   gcc_checking_assert (!(parent && header_p));
    4066        11579 : }
    4067              : 
    4068           60 : module_state::~module_state ()
    4069              : {
    4070           60 :   release ();
    4071           60 : }
    4072              : 
    4073              : /* Hash module state.  */
    4074              : static hashval_t
    4075        17087 : module_name_hash (const_tree name)
    4076              : {
    4077        17087 :   if (TREE_CODE (name) == STRING_CST)
    4078         3535 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4079              :   else
    4080        13552 :     return IDENTIFIER_HASH_VALUE (name);
    4081              : }
    4082              : 
    4083              : hashval_t
    4084         4779 : module_state_hash::hash (const value_type m)
    4085              : {
    4086         4779 :   hashval_t ph = pointer_hash<void>::hash
    4087         4779 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4088         4779 :                                | m->is_partition ()));
    4089         4779 :   hashval_t nh = module_name_hash (m->name);
    4090         4779 :   return iterative_hash_hashval_t (ph, nh);
    4091              : }
    4092              : 
    4093              : /* Hash a name.  */
    4094              : hashval_t
    4095        12308 : module_state_hash::hash (const compare_type &c)
    4096              : {
    4097        12308 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4098        12308 :   hashval_t nh = module_name_hash (c.first);
    4099              : 
    4100        12308 :   return iterative_hash_hashval_t (ph, nh);
    4101              : }
    4102              : 
    4103              : bool
    4104         8435 : module_state_hash::equal (const value_type existing,
    4105              :                           const compare_type &candidate)
    4106              : {
    4107         8435 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4108         8435 :                   | existing->is_partition ());
    4109         8435 :   if (ep != candidate.second)
    4110              :     return false;
    4111              : 
    4112              :   /* Identifier comparison is by pointer.  If the string_csts happen
    4113              :      to be the same object, then they're equal too.  */
    4114         6989 :   if (existing->name == candidate.first)
    4115              :     return true;
    4116              : 
    4117              :   /* If neither are string csts, they can't be equal.  */
    4118         1486 :   if (TREE_CODE (candidate.first) != STRING_CST
    4119          495 :       || TREE_CODE (existing->name) != STRING_CST)
    4120              :     return false;
    4121              : 
    4122              :   /* String equality.  */
    4123          425 :   if (TREE_STRING_LENGTH (existing->name)
    4124          425 :       == TREE_STRING_LENGTH (candidate.first)
    4125          425 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4126          422 :                   TREE_STRING_POINTER (candidate.first),
    4127          422 :                   TREE_STRING_LENGTH (existing->name)))
    4128          139 :     return true;
    4129              : 
    4130              :   return false;
    4131              : }
    4132              : 
    4133              : /********************************************************************/
    4134              : /* Global state */
    4135              : 
    4136              : /* Mapper name.  */
    4137              : static const char *module_mapper_name;
    4138              : 
    4139              : /* Deferred import queue (FIFO).  */
    4140              : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    4141              : 
    4142              : /* CMI repository path and workspace.  */
    4143              : static char *cmi_repo;
    4144              : static size_t cmi_repo_length;
    4145              : static char *cmi_path;
    4146              : static size_t cmi_path_alloc;
    4147              : 
    4148              : /* Count of available and loaded clusters.  */
    4149              : static unsigned available_clusters;
    4150              : static unsigned loaded_clusters;
    4151              : 
    4152              : /* What the current TU is.  */
    4153              : unsigned module_kind;
    4154              : 
    4155              : /* Global trees.  */
    4156              : static const std::pair<tree *, unsigned> global_tree_arys[] =
    4157              :   {
    4158              :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    4159              :     std::pair<tree *, unsigned> (integer_types, itk_none),
    4160              :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    4161              :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    4162              :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    4163              :     std::pair<tree *, unsigned> (NULL, 0)
    4164              :   };
    4165              : static GTY(()) vec<tree, va_gc> *fixed_trees;
    4166              : static unsigned global_crc;
    4167              : 
    4168              : /* Lazy loading can open many files concurrently, there are
    4169              :    per-process limits on that.  We pay attention to the process limit,
    4170              :    and attempt to increase it when we run out.  Otherwise we use an
    4171              :    LRU scheme to figure out who to flush.  Note that if the import
    4172              :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    4173              : static unsigned lazy_lru;  /* LRU counter.  */
    4174              : static unsigned lazy_open; /* Number of open modules */
    4175              : static unsigned lazy_limit; /* Current limit of open modules.  */
    4176              : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    4177              : /* Account for source, assembler and dump files & directory searches.
    4178              :    We don't keep the source file's open, so we don't have to account
    4179              :    for #include depth.  I think dump files are opened and closed per
    4180              :    pass, but ICBW.  */
    4181              : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    4182              : 
    4183              : /* Vector of module state.  Indexed by OWNER.  Index 0 is reserved for the
    4184              :    current TU; imports start at 1.  */
    4185              : static GTY(()) vec<module_state *, va_gc> *modules;
    4186              : 
    4187              : /* Get the module state for the current TU's module.  */
    4188              : 
    4189              : static module_state *
    4190       299254 : this_module() {
    4191       299254 :   return (*modules)[0];
    4192              : }
    4193              : 
    4194              : /* Hash of module state, findable by {name, parent}. */
    4195              : static GTY(()) hash_table<module_state_hash> *modules_hash;
    4196              : 
    4197              : /* Map of imported entities.  We map DECL_UID to index of entity
    4198              :    vector.  */
    4199              : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    4200              :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    4201              :                  > entity_map_t;
    4202              : static entity_map_t *entity_map;
    4203              : /* Doesn't need GTYing, because any tree referenced here is also
    4204              :    findable by, symbol table, specialization table, return type of
    4205              :    reachable function.  */
    4206              : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    4207              : 
    4208              : /* Members entities of imported classes that are defined in this TU.
    4209              :    These are where the entity's context is not from the current TU.
    4210              :    We need to emit the definition (but not the enclosing class).
    4211              : 
    4212              :    We could find these by walking ALL the imported classes that we
    4213              :    could provide a member definition.  But that's expensive,
    4214              :    especially when you consider lazy implicit member declarations,
    4215              :    which could be ANY imported class.  */
    4216              : static GTY(()) vec<tree, va_gc> *class_members;
    4217              : 
    4218              : /* The same problem exists for class template partial
    4219              :    specializations.  Now that we have constraints, the invariant of
    4220              :    expecting them in the instantiation table no longer holds.  One of
    4221              :    the constrained partial specializations will be there, but the
    4222              :    others not so much.  It's not even an unconstrained partial
    4223              :    spacialization in the table :(  so any partial template declaration
    4224              :    is added to this list too.  */
    4225              : static GTY(()) vec<tree, va_gc> *partial_specializations;
    4226              : 
    4227              : /********************************************************************/
    4228              : 
    4229              : /* Our module mapper (created lazily).  */
    4230              : module_client *mapper;
    4231              : 
    4232              : static module_client *make_mapper (location_t loc, class mkdeps *deps);
    4233        32483 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4234              : {
    4235        32483 :   auto *res = mapper;
    4236          312 :   if (!res)
    4237         4913 :     res = make_mapper (loc, deps);
    4238        32483 :   return res;
    4239              : }
    4240              : 
    4241              : /********************************************************************/
    4242              : static tree
    4243       441632 : get_clone_target (tree decl)
    4244              : {
    4245       441632 :   tree target;
    4246              : 
    4247       441632 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4248              :     {
    4249        56616 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4250              : 
    4251        56616 :       target = DECL_TI_TEMPLATE (res_orig);
    4252              :     }
    4253              :   else
    4254       385016 :     target = DECL_CLONED_FUNCTION (decl);
    4255              : 
    4256       441632 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4257              : 
    4258       441632 :   return target;
    4259              : }
    4260              : 
    4261              : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4262              : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4263              :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4264              :   else                                                  \
    4265              :     for (CLONE = DECL_CHAIN (FN);                       \
    4266              :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4267              :          CLONE = DECL_CHAIN (CLONE))
    4268              : 
    4269              : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4270              :    (a) it'd solve the enum case dealt with below,
    4271              :    (b) both class templates and decl templates would store this in the
    4272              :    same place
    4273              :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4274              : 
    4275              : static tree
    4276    135878936 : node_template_info (tree decl, int &use)
    4277              : {
    4278    135878936 :   tree ti = NULL_TREE;
    4279    135878936 :   int use_tpl = -1;
    4280    135878936 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4281              :     {
    4282     24403828 :       tree type = TREE_TYPE (decl);
    4283              : 
    4284     24403828 :       ti = TYPE_TEMPLATE_INFO (type);
    4285     24403828 :       if (ti)
    4286              :         {
    4287      5592571 :           if (TYPE_LANG_SPECIFIC (type))
    4288      5581610 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4289              :           else
    4290              :             {
    4291              :               /* An enum, where we don't explicitly encode use_tpl.
    4292              :                  If the containing context (a type or a function), is
    4293              :                  an ({im,ex}plicit) instantiation, then this is too.
    4294              :                  If it's a partial or explicit specialization, then
    4295              :                  this is not!.  */
    4296        10961 :               tree ctx = CP_DECL_CONTEXT (decl);
    4297        10961 :               if (TYPE_P (ctx))
    4298        10744 :                 ctx = TYPE_NAME (ctx);
    4299        10961 :               node_template_info (ctx, use);
    4300        10961 :               use_tpl = use != 2 ? use : 0;
    4301              :             }
    4302              :         }
    4303              :     }
    4304    111475108 :   else if (DECL_LANG_SPECIFIC (decl)
    4305    111475108 :            && (VAR_P (decl)
    4306              :                || TREE_CODE (decl) == TYPE_DECL
    4307              :                || TREE_CODE (decl) == FUNCTION_DECL
    4308              :                || TREE_CODE (decl) == FIELD_DECL
    4309              :                || TREE_CODE (decl) == CONCEPT_DECL
    4310              :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4311              :     {
    4312     95590932 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4313     95590932 :       ti = DECL_TEMPLATE_INFO (decl);
    4314              :     }
    4315              : 
    4316    135878936 :   use = use_tpl;
    4317    135878936 :   return ti;
    4318              : }
    4319              : 
    4320              : /* Find the index in entity_ary for an imported DECL.  It should
    4321              :    always be there, but bugs can cause it to be missing, and that can
    4322              :    crash the crash reporting -- let's not do that!  When streaming
    4323              :    out we place entities from this module there too -- with negated
    4324              :    indices.  */
    4325              : 
    4326              : static unsigned
    4327      1717958 : import_entity_index (tree decl, bool null_ok = false)
    4328              : {
    4329      1717958 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4330      1717913 :     return *slot;
    4331              : 
    4332           45 :   gcc_checking_assert (null_ok);
    4333              :   return ~(~0u >> 1);
    4334              : }
    4335              : 
    4336              : /* Find the module for an imported entity at INDEX in the entity ary.
    4337              :    There must be one.  */
    4338              : 
    4339              : static module_state *
    4340       134078 : import_entity_module (unsigned index)
    4341              : {
    4342       134078 :   if (index > ~(~0u >> 1))
    4343              :     /* This is an index for an exported entity.  */
    4344           60 :     return this_module ();
    4345              : 
    4346              :   /* Do not include the current TU (not an off-by-one error).  */
    4347       134018 :   unsigned pos = 1;
    4348       134018 :   unsigned len = modules->length () - pos;
    4349       306670 :   while (len)
    4350              :     {
    4351       172652 :       unsigned half = len / 2;
    4352       172652 :       module_state *probe = (*modules)[pos + half];
    4353       172652 :       if (index < probe->entity_lwm)
    4354              :         len = half;
    4355       134484 :       else if (index < probe->entity_lwm + probe->entity_num)
    4356              :         return probe;
    4357              :       else
    4358              :         {
    4359          466 :           pos += half + 1;
    4360          466 :           len = len - (half + 1);
    4361              :         }
    4362              :     }
    4363            0 :   gcc_unreachable ();
    4364              : }
    4365              : 
    4366              : 
    4367              : /********************************************************************/
    4368              : /* A dumping machinery.  */
    4369              : 
    4370              : class dumper {
    4371              : public:
    4372              :   enum {
    4373              :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4374              :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4375              :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4376              :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4377              :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4378              :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4379              :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4380              :   };
    4381              : 
    4382              : private:
    4383              :   struct impl {
    4384              :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4385              : 
    4386              :     FILE *stream;       /* Dump stream.  */
    4387              :     unsigned indent;    /* Local indentation.  */
    4388              :     bool bol;           /* Beginning of line.  */
    4389              :     stack_t stack;      /* Trailing array of module_state.  */
    4390              : 
    4391              :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4392              :   };
    4393              : 
    4394              : public:
    4395              :   /* The dumper.  */
    4396              :   impl *dumps;
    4397              :   dump_flags_t flags;
    4398              : 
    4399              : public:
    4400              :   /* Push/pop module state dumping.  */
    4401              :   unsigned push (module_state *);
    4402              :   void pop (unsigned);
    4403              : 
    4404              : public:
    4405              :   /* Change local indentation.  */
    4406    426496357 :   void indent ()
    4407              :   {
    4408          309 :     if (dumps)
    4409       725915 :       dumps->indent++;
    4410              :   }
    4411    426496357 :   void outdent ()
    4412              :   {
    4413    426496357 :     if (dumps)
    4414              :       {
    4415       725915 :         gcc_checking_assert (dumps->indent);
    4416       725915 :         dumps->indent--;
    4417              :       }
    4418    426496357 :   }
    4419              : 
    4420              : public:
    4421              :   /* Is dump enabled?.  */
    4422    259233357 :   bool operator () (int mask = 0)
    4423              :   {
    4424      5117545 :     if (!dumps || !dumps->stream)
    4425              :       return false;
    4426       505830 :     if (mask && !(mask & flags))
    4427         5388 :       return false;
    4428              :     return true;
    4429              :   }
    4430              :   /* Dump some information.  */
    4431              :   bool operator () (const char *, ...);
    4432              : };
    4433              : 
    4434              : /* The dumper.  */
    4435              : static dumper dump = {0, dump_flags_t (0)};
    4436              : 
    4437              : /* Push to dumping M.  Return previous indentation level.  */
    4438              : 
    4439              : unsigned
    4440       130139 : dumper::push (module_state *m)
    4441              : {
    4442       130139 :   FILE *stream = NULL;
    4443       130139 :   if (!dumps || !dumps->stack.length ())
    4444              :     {
    4445       128866 :       stream = dump_begin (module_dump_id, &flags);
    4446       128866 :       if (!stream)
    4447              :         return 0;
    4448              :     }
    4449              : 
    4450         6953 :   if (!dumps || !dumps->stack.space (1))
    4451              :     {
    4452              :       /* Create or extend the dump implementor.  */
    4453         1239 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4454          656 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4455         1239 :       size_t alloc = (offsetof (impl, stack)
    4456         1239 :                       + impl::stack_t::embedded_size (count));
    4457         1239 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4458         1239 :       dumps->stack.embedded_init (count, current);
    4459              :     }
    4460         6953 :   if (stream)
    4461         5680 :     dumps->stream = stream;
    4462              : 
    4463         6953 :   unsigned n = dumps->indent;
    4464         6953 :   dumps->indent = 0;
    4465         6953 :   dumps->bol = true;
    4466         6953 :   dumps->stack.quick_push (m);
    4467         6953 :   if (m)
    4468              :     {
    4469         2039 :       module_state *from = NULL;
    4470              : 
    4471         2039 :       if (dumps->stack.length () > 1)
    4472          654 :         from = dumps->stack[dumps->stack.length () - 2];
    4473              :       else
    4474         1385 :         dump ("");
    4475         3683 :       dump (from ? "Starting module %M (from %M)"
    4476              :             : "Starting module %M", m, from);
    4477              :     }
    4478              : 
    4479              :   return n;
    4480              : }
    4481              : 
    4482              : /* Pop from dumping.  Restore indentation to N.  */
    4483              : 
    4484       130096 : void dumper::pop (unsigned n)
    4485              : {
    4486       130096 :   if (!dumps)
    4487              :     return;
    4488              : 
    4489        13906 :   gcc_checking_assert (dump () && !dumps->indent);
    4490         6953 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4491              :     {
    4492         2039 :       module_state *from = (dumps->stack.length () > 1
    4493         2039 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4494         2298 :       dump (from ? "Finishing module %M (returning to %M)"
    4495              :             : "Finishing module %M", m, from);
    4496              :     }
    4497         6953 :   dumps->stack.pop ();
    4498         6953 :   dumps->indent = n;
    4499         6953 :   if (!dumps->stack.length ())
    4500              :     {
    4501         5680 :       dump_end (module_dump_id, dumps->stream);
    4502         5680 :       dumps->stream = NULL;
    4503              :     }
    4504              : }
    4505              : 
    4506              : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4507              :    name.  */
    4508              : 
    4509              : bool
    4510       523956 : dumper::impl::nested_name (tree t)
    4511              : {
    4512       523956 :   tree ti = NULL_TREE;
    4513       523956 :   int origin = -1;
    4514       523956 :   tree name = NULL_TREE;
    4515              : 
    4516       523956 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4517            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4518              : 
    4519       523926 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4520          384 :     t = BINFO_TYPE (t);
    4521              : 
    4522       523956 :   if (t && TYPE_P (t))
    4523       255366 :     t = TYPE_NAME (t);
    4524              : 
    4525       523914 :   if (t && DECL_P (t))
    4526              :     {
    4527       440527 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4528              :         ;
    4529       408629 :       else if (tree ctx = DECL_CONTEXT (t))
    4530       318468 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4531       318468 :             || nested_name (ctx))
    4532       318468 :           fputs ("::", stream);
    4533              : 
    4534       440527 :       int use_tpl;
    4535       440527 :       ti = node_template_info (t, use_tpl);
    4536       138636 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4537       579118 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4538              :         t = TI_TEMPLATE (ti);
    4539       440527 :       tree not_tmpl = t;
    4540       440527 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4541              :         {
    4542        23726 :           fputs ("template ", stream);
    4543        23726 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4544              :         }
    4545              : 
    4546        23726 :       if (not_tmpl
    4547       440523 :           && DECL_P (not_tmpl)
    4548       440523 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4549       264817 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4550              :         {
    4551              :           /* We need to be careful here, so as to not explode on
    4552              :              inconsistent data -- we're probably debugging, because
    4553              :              Something Is Wrong.  */
    4554        24183 :           unsigned index = import_entity_index (t, true);
    4555        24183 :           if (!(index & ~(~0u >> 1)))
    4556        23583 :             origin = import_entity_module (index)->mod;
    4557          600 :           else if (index > ~(~0u >> 1))
    4558              :             /* An imported partition member that we're emitting.  */
    4559              :             origin = 0;
    4560              :           else
    4561           45 :             origin = -2;
    4562              :         }
    4563              : 
    4564       443757 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4565         4309 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4566              :         : NULL_TREE;
    4567              :     }
    4568              :   else
    4569              :     name = t;
    4570              : 
    4571       440527 :   if (name)
    4572       488831 :     switch (TREE_CODE (name))
    4573              :       {
    4574        13638 :       default:
    4575        13638 :         fputs ("#unnamed#", stream);
    4576        13638 :         break;
    4577              : 
    4578       450785 :       case IDENTIFIER_NODE:
    4579       450785 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4580       450785 :         break;
    4581              : 
    4582        24316 :       case INTEGER_CST:
    4583        24316 :         print_hex (wi::to_wide (name), stream);
    4584        24316 :         break;
    4585              : 
    4586           92 :       case STRING_CST:
    4587              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4588          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4589           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4590              :                 stream);
    4591           92 :         break;
    4592              :       }
    4593              :   else
    4594        35125 :     fputs ("#null#", stream);
    4595              : 
    4596       523956 :   if (t && TREE_CODE (t) == FUNCTION_DECL && DECL_COROUTINE_P (t))
    4597           48 :     if (tree ramp = DECL_RAMP_FN (t))
    4598              :       {
    4599           27 :         if (DECL_ACTOR_FN (ramp) == t)
    4600           12 :           fputs (".actor", stream);
    4601           15 :         else if (DECL_DESTROY_FN (ramp) == t)
    4602           15 :           fputs (".destroy", stream);
    4603              :         else
    4604            0 :           gcc_unreachable ();
    4605              :       }
    4606              : 
    4607       523956 :   if (origin >= 0)
    4608              :     {
    4609        24138 :       const module_state *module = (*modules)[origin];
    4610        48276 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4611        24138 :                : module->get_flatname (), origin);
    4612              :     }
    4613       499818 :   else if (origin == -2)
    4614           45 :     fprintf (stream, "@???");
    4615              : 
    4616       523956 :   if (ti)
    4617              :     {
    4618       138636 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4619       138636 :       fputs ("<", stream);
    4620       138636 :       if (args)
    4621       349412 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4622              :           {
    4623       210776 :             if (ix)
    4624        72140 :               fputs (",", stream);
    4625       210776 :             nested_name (TREE_VEC_ELT (args, ix));
    4626              :           }
    4627       138636 :       fputs (">", stream);
    4628              :     }
    4629              : 
    4630       523956 :   return true;
    4631              : }
    4632              : 
    4633              : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4634              :    new line.  (Normally it is appended.)
    4635              :    Escapes:
    4636              :       %C - tree_code
    4637              :       %I - identifier
    4638              :       %K - location_t or line_map_uint_t
    4639              :       %M - module_state
    4640              :       %N - name -- DECL_NAME
    4641              :       %P - context:name pair
    4642              :       %R - unsigned:unsigned ratio
    4643              :       %S - symbol -- DECL_ASSEMBLER_NAME
    4644              :       %U - long unsigned
    4645              :       %V - version
    4646              :       --- the following are printf-like, but without its flexibility
    4647              :       %d - decimal int
    4648              :       %p - pointer
    4649              :       %s - string
    4650              :       %u - unsigned int
    4651              :       %x - hex int
    4652              : 
    4653              :   We do not implement the printf modifiers.  */
    4654              : 
    4655              : bool
    4656       446084 : dumper::operator () (const char *format, ...)
    4657              : {
    4658       446084 :   if (!(*this) ())
    4659              :     return false;
    4660              : 
    4661       382579 :   bool no_nl = format[0] == '+';
    4662       382579 :   format += no_nl;
    4663              : 
    4664       382579 :   if (dumps->bol)
    4665              :     {
    4666              :       /* Module import indent.  */
    4667       197185 :       if (unsigned depth = dumps->stack.length () - 1)
    4668              :         {
    4669        22534 :           const char *prefix = ">>>>";
    4670        45050 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4671        22516 :                                    ? &prefix[strlen (prefix) - depth]
    4672              :                                    : ">.%d.>"), depth);
    4673              :         }
    4674              : 
    4675              :       /* Local indent.  */
    4676       197185 :       if (unsigned indent = dumps->indent)
    4677              :         {
    4678       106178 :           const char *prefix = "      ";
    4679       207340 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4680       101162 :                                    ? &prefix[strlen (prefix) - indent]
    4681              :                                    : "  .%d.  "), indent);
    4682              :         }
    4683       197185 :       dumps->bol = false;
    4684              :     }
    4685              : 
    4686       382579 :   va_list args;
    4687       382579 :   va_start (args, format);
    4688      1119164 :   while (const char *esc = strchr (format, '%'))
    4689              :     {
    4690       736585 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4691       736585 :       format = ++esc;
    4692       736585 :       switch (*format++)
    4693              :         {
    4694            0 :         default:
    4695            0 :           gcc_unreachable ();
    4696              : 
    4697          583 :         case '%':
    4698          583 :           fputc ('%', dumps->stream);
    4699          583 :           break;
    4700              : 
    4701       112297 :         case 'C': /* Code */
    4702       112297 :           {
    4703       112297 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4704       112297 :             fputs (get_tree_code_name (code), dumps->stream);
    4705              :           }
    4706       112297 :           break;
    4707              : 
    4708           81 :         case 'I': /* Identifier.  */
    4709           81 :           {
    4710           81 :             tree t = va_arg (args, tree);
    4711           81 :             dumps->nested_name (t);
    4712              :           }
    4713           81 :           break;
    4714              : 
    4715         4647 :         case 'K': /* location_t, either 32- or 64-bit.  */
    4716         4647 :           {
    4717         4647 :             unsigned long long u = va_arg (args, location_t);
    4718         4647 :             fprintf (dumps->stream, "%llu", u);
    4719              :           }
    4720         4647 :           break;
    4721              : 
    4722         7734 :         case 'M': /* Module. */
    4723         7734 :           {
    4724         7734 :             const char *str = "(none)";
    4725         7734 :             if (module_state *m = va_arg (args, module_state *))
    4726              :               {
    4727         7734 :                 if (!m->has_location ())
    4728              :                   str = "(detached)";
    4729              :                 else
    4730         7734 :                   str = m->get_flatname ();
    4731              :               }
    4732         7734 :             fputs (str, dumps->stream);
    4733              :           }
    4734         7734 :           break;
    4735              : 
    4736       125556 :         case 'N': /* Name.  */
    4737       125556 :           {
    4738       125556 :             tree t = va_arg (args, tree);
    4739       251601 :             while (t && TREE_CODE (t) == OVERLOAD)
    4740          489 :               t = OVL_FUNCTION (t);
    4741       125556 :             fputc ('\'', dumps->stream);
    4742       125556 :             dumps->nested_name (t);
    4743       125556 :             fputc ('\'', dumps->stream);
    4744              :           }
    4745       125556 :           break;
    4746              : 
    4747         7355 :         case 'P': /* Pair.  */
    4748         7355 :           {
    4749         7355 :             tree ctx = va_arg (args, tree);
    4750         7355 :             tree name = va_arg (args, tree);
    4751         7355 :             fputc ('\'', dumps->stream);
    4752         7355 :             dumps->nested_name (ctx);
    4753         7355 :             if (ctx && ctx != global_namespace)
    4754          998 :               fputs ("::", dumps->stream);
    4755         7355 :             dumps->nested_name (name);
    4756         7355 :             fputc ('\'', dumps->stream);
    4757              :           }
    4758         7355 :           break;
    4759              : 
    4760          900 :         case 'R': /* Ratio */
    4761          900 :           {
    4762          900 :             unsigned a = va_arg (args, unsigned);
    4763          900 :             unsigned b = va_arg (args, unsigned);
    4764          900 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4765              :           }
    4766          900 :           break;
    4767              : 
    4768        34704 :         case 'S': /* Symbol name */
    4769        34704 :           {
    4770        34704 :             tree t = va_arg (args, tree);
    4771        34704 :             if (t && TYPE_P (t))
    4772        12631 :               t = TYPE_NAME (t);
    4773        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4774        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4775              :               {
    4776          172 :                 fputc ('(', dumps->stream);
    4777          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4778          172 :                        dumps->stream);
    4779          172 :                 fputc (')', dumps->stream);
    4780              :               }
    4781              :           }
    4782              :           break;
    4783              : 
    4784            0 :         case 'U': /* long unsigned.  */
    4785            0 :           {
    4786            0 :             unsigned long u = va_arg (args, unsigned long);
    4787            0 :             fprintf (dumps->stream, "%lu", u);
    4788              :           }
    4789            0 :           break;
    4790              : 
    4791          832 :         case 'V': /* Version.  */
    4792          832 :           {
    4793          832 :             unsigned v = va_arg (args, unsigned);
    4794          832 :             verstr_t string;
    4795              : 
    4796          832 :             version2string (v, string);
    4797          832 :             fputs (string, dumps->stream);
    4798              :           }
    4799          832 :           break;
    4800              : 
    4801            0 :         case 'c': /* Character.  */
    4802            0 :           {
    4803            0 :             int c = va_arg (args, int);
    4804            0 :             fputc (c, dumps->stream);
    4805              :           }
    4806            0 :           break;
    4807              : 
    4808        63158 :         case 'd': /* Decimal Int.  */
    4809        63158 :           {
    4810        63158 :             int d = va_arg (args, int);
    4811        63158 :             fprintf (dumps->stream, "%d", d);
    4812              :           }
    4813        63158 :           break;
    4814              : 
    4815            0 :         case 'p': /* Pointer. */
    4816            0 :           {
    4817            0 :             void *p = va_arg (args, void *);
    4818            0 :             fprintf (dumps->stream, "%p", p);
    4819              :           }
    4820            0 :           break;
    4821              : 
    4822       126795 :         case 's': /* String. */
    4823       126795 :           {
    4824       126795 :             const char *s = va_arg (args, char *);
    4825       126795 :             gcc_checking_assert (s);
    4826       126795 :             fputs (s, dumps->stream);
    4827              :           }
    4828       126795 :           break;
    4829              : 
    4830       249267 :         case 'u': /* Unsigned.  */
    4831       249267 :           {
    4832       249267 :             unsigned u = va_arg (args, unsigned);
    4833       249267 :             fprintf (dumps->stream, "%u", u);
    4834              :           }
    4835       249267 :           break;
    4836              : 
    4837         2676 :         case 'x': /* Hex. */
    4838         2676 :           {
    4839         2676 :             unsigned x = va_arg (args, unsigned);
    4840         2676 :             fprintf (dumps->stream, "%x", x);
    4841              :           }
    4842         2676 :           break;
    4843              :         }
    4844              :     }
    4845       382579 :   fputs (format, dumps->stream);
    4846       382579 :   va_end (args);
    4847       382579 :   if (!no_nl)
    4848              :     {
    4849       197185 :       dumps->bol = true;
    4850       197185 :       fputc ('\n', dumps->stream);
    4851              :     }
    4852              :   return true;
    4853              : }
    4854              : 
    4855              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4856              : {
    4857       318747 :   static int keep_cache_entry (tree t)
    4858              :   {
    4859       318747 :     if (!CHECKING_P)
    4860              :       /* GTY is unfortunately not clever enough to conditionalize
    4861              :          this.  */
    4862              :       gcc_unreachable ();
    4863              : 
    4864       318747 :     if (ggc_marked_p (t))
    4865              :       return -1;
    4866              : 
    4867            0 :     unsigned n = dump.push (NULL);
    4868              :     /* This might or might not be an error.  We should note its
    4869              :        dropping whichever.  */
    4870            0 :     dump () && dump ("Dropping %N from note_defs table", t);
    4871            0 :     dump.pop (n);
    4872              : 
    4873            0 :     return 0;
    4874              :   }
    4875              : };
    4876              : 
    4877              : /* We should stream each definition at most once.
    4878              :    This needs to be a cache because there are cases where a definition
    4879              :    ends up being not retained, and we need to drop those so we don't
    4880              :    get confused if memory is reallocated.  */
    4881              : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4882              : static GTY((cache)) note_defs_table_t *note_defs;
    4883              : 
    4884              : void
    4885       340883 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4886              :                              bool installing ATTRIBUTE_UNUSED)
    4887              : {
    4888              : #if CHECKING_P
    4889       340883 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4890       340883 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4891       340883 :   if (installing)
    4892              :     {
    4893              :       /* We must be inserting for the first time.  */
    4894       206189 :       gcc_assert (!*slot);
    4895       206189 :       *slot = decl;
    4896              :     }
    4897              :   else
    4898              :     /* If this is not the mergeable entity, it should not be in the
    4899              :        table.  If it is a non-global-module mergeable entity, it
    4900              :        should be in the table.  Global module entities could have been
    4901              :        defined textually in the current TU and so might or might not
    4902              :        be present.  */
    4903       134694 :     gcc_assert (!is_duplicate (decl)
    4904              :                 ? !slot
    4905              :                 : (slot
    4906              :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4907              :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4908              :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4909              :                        && header_module_p ())));
    4910              : 
    4911       340883 :   if (not_tmpl != decl)
    4912       201426 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4913              : #endif
    4914       340883 : }
    4915              : 
    4916              : void
    4917       458285 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4918              : {
    4919              : #if CHECKING_P
    4920       458285 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4921       458285 :   gcc_assert (!*slot);
    4922       458285 :   *slot = decl;
    4923       458285 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4924       258124 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4925              : #endif
    4926       458285 : }
    4927              : 
    4928              : /********************************************************************/
    4929              : static bool
    4930        12647 : noisy_p ()
    4931              : {
    4932            0 :   if (quiet_flag)
    4933              :     return false;
    4934              : 
    4935            0 :   pp_needs_newline (global_dc->get_reference_printer ()) = true;
    4936            0 :   diagnostic_set_last_function (global_dc,
    4937              :                                 (diagnostics::diagnostic_info *) nullptr);
    4938              : 
    4939            0 :   return true;
    4940              : }
    4941              : 
    4942              : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4943              : 
    4944              : static void
    4945       101735 : set_cmi_repo (const char *r)
    4946              : {
    4947       101735 :   XDELETEVEC (cmi_repo);
    4948       101735 :   XDELETEVEC (cmi_path);
    4949       101735 :   cmi_path_alloc = 0;
    4950              : 
    4951       101735 :   cmi_repo = NULL;
    4952       101735 :   cmi_repo_length = 0;
    4953              : 
    4954       101735 :   if (!r || !r[0])
    4955              :     return;
    4956              : 
    4957         4910 :   size_t len = strlen (r);
    4958         4910 :   cmi_repo = XNEWVEC (char, len + 1);
    4959         4910 :   memcpy (cmi_repo, r, len + 1);
    4960              : 
    4961         4910 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4962         4910 :     len--;
    4963         4910 :   if (len == 1 && cmi_repo[0] == '.')
    4964           27 :     len--;
    4965         4910 :   cmi_repo[len] = 0;
    4966         4910 :   cmi_repo_length = len;
    4967              : }
    4968              : 
    4969              : /* TO is a repo-relative name.  Provide one that we may use from where
    4970              :    we are.  */
    4971              : 
    4972              : static const char *
    4973         5999 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4974              : {
    4975         5999 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4976              : 
    4977         5999 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4978              :     {
    4979         5972 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4980              :         {
    4981         4788 :           XDELETEVEC (cmi_path);
    4982         4788 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4983         4788 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4984              : 
    4985         4788 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4986         4788 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4987              :         }
    4988              : 
    4989         5972 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4990         5972 :       len += cmi_repo_length + 1;
    4991         5972 :       to = cmi_path;
    4992              :     }
    4993              : 
    4994         5999 :   if (len_p)
    4995         2894 :     *len_p = len;
    4996              : 
    4997         5999 :   return to;
    4998              : }
    4999              : 
    5000              : /* Try and create the directories of PATH.  */
    5001              : 
    5002              : static void
    5003           43 : create_dirs (char *path)
    5004              : {
    5005           43 :   char *base = path;
    5006              :   /* Skip past initial slashes of absolute path.  */
    5007           43 :   while (IS_DIR_SEPARATOR (*base))
    5008            0 :     base++;
    5009              : 
    5010              :   /* Try and create the missing directories.  */
    5011         2693 :   for (; *base; base++)
    5012         2650 :     if (IS_DIR_SEPARATOR (*base))
    5013              :       {
    5014          261 :         char sep = *base;
    5015          261 :         *base = 0;
    5016          261 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5017          265 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5018          261 :         *base = sep;
    5019          261 :         if (failed
    5020              :             /* Maybe racing with another creator (of a *different*
    5021              :                module).  */
    5022           49 :             && errno != EEXIST)
    5023              :           break;
    5024              :       }
    5025           43 : }
    5026              : 
    5027              : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    5028              :    if that's what this is.  */
    5029              : 
    5030              : static tree
    5031        97898 : friend_from_decl_list (tree frnd)
    5032              : {
    5033        97898 :   tree res = frnd;
    5034              : 
    5035        97898 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5036              :     {
    5037        59896 :       tree tmpl = NULL_TREE;
    5038        59896 :       if (TYPE_P (frnd))
    5039              :         {
    5040         9831 :           res = TYPE_NAME (frnd);
    5041         9672 :           if (CLASS_TYPE_P (frnd)
    5042        19503 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5043         9663 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5044              :         }
    5045        50065 :       else if (DECL_TEMPLATE_INFO (frnd))
    5046              :         {
    5047        50065 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5048        50065 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5049              :             tmpl = NULL_TREE;
    5050              :         }
    5051              : 
    5052        68630 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5053              :         res = tmpl;
    5054              :     }
    5055              : 
    5056        97898 :   return res;
    5057              : }
    5058              : 
    5059              : static tree
    5060        29390 : find_enum_member (tree ctx, tree name)
    5061              : {
    5062        29390 :   for (tree values = TYPE_VALUES (ctx);
    5063       485785 :        values; values = TREE_CHAIN (values))
    5064       477267 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5065              :       return TREE_VALUE (values);
    5066              : 
    5067              :   return NULL_TREE;
    5068              : }
    5069              : 
    5070              : /********************************************************************/
    5071              : /* Instrumentation gathered writing bytes.  */
    5072              : 
    5073              : void
    5074          300 : bytes_out::instrument ()
    5075              : {
    5076          300 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5077          300 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5078          900 :   for (unsigned ix = 0; ix < 2; ix++)
    5079          900 :     dump ("  %u %s spans of %R bits", spans[ix],
    5080              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5081          300 :   dump ("  %u blocks with %R bits padding", spans[2],
    5082          300 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5083          300 : }
    5084              : 
    5085              : /* Instrumentation gathered writing trees.  */
    5086              : void
    5087         2747 : trees_out::instrument ()
    5088              : {
    5089         2747 :   if (dump (""))
    5090              :     {
    5091          300 :       bytes_out::instrument ();
    5092          300 :       dump ("Wrote:");
    5093          300 :       dump ("  %u decl trees", decl_val_count);
    5094          300 :       dump ("  %u other trees", tree_val_count);
    5095          300 :       dump ("  %u back references", back_ref_count);
    5096          300 :       dump ("  %u TU-local entities", tu_local_count);
    5097          300 :       dump ("  %u null trees", null_count);
    5098              :     }
    5099         2747 : }
    5100              : 
    5101              : /* Setup and teardown for a tree walk.  */
    5102              : 
    5103              : void
    5104      2706048 : trees_out::begin ()
    5105              : {
    5106      2706048 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5107              : 
    5108      2706048 :   mark_trees ();
    5109      2706048 :   if (streaming_p ())
    5110       308260 :     parent::begin ();
    5111      2706048 : }
    5112              : 
    5113              : unsigned
    5114       308260 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5115              : {
    5116       308260 :   gcc_checking_assert (streaming_p ());
    5117              : 
    5118       308260 :   unmark_trees ();
    5119       308260 :   return parent::end (sink, name, crc_ptr);
    5120              : }
    5121              : 
    5122              : void
    5123      2397788 : trees_out::end ()
    5124              : {
    5125      2397788 :   gcc_assert (!streaming_p ());
    5126              : 
    5127      2397788 :   unmark_trees ();
    5128              :   /* Do not parent::end -- we weren't streaming.  */
    5129      2397788 : }
    5130              : 
    5131              : void
    5132      2706048 : trees_out::mark_trees ()
    5133              : {
    5134      2706048 :   if (size_t size = tree_map.elements ())
    5135              :     {
    5136              :       /* This isn't our first rodeo, destroy and recreate the
    5137              :          tree_map.  I'm a bad bad man.  Use the previous size as a
    5138              :          guess for the next one (so not all bad).  */
    5139      2105073 :       tree_map.~ptr_int_hash_map ();
    5140      2105073 :       new (&tree_map) ptr_int_hash_map (size);
    5141              :     }
    5142              : 
    5143              :   /* Install the fixed trees, with +ve references.  */
    5144      2706048 :   unsigned limit = fixed_trees->length ();
    5145    521365605 :   for (unsigned ix = 0; ix != limit; ix++)
    5146              :     {
    5147    518659557 :       tree val = (*fixed_trees)[ix];
    5148    518659557 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5149    518659557 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5150    518659557 :       TREE_VISITED (val) = true;
    5151              :     }
    5152              : 
    5153      2706048 :   ref_num = 0;
    5154      2706048 : }
    5155              : 
    5156              : /* Unmark the trees we encountered  */
    5157              : 
    5158              : void
    5159      2706048 : trees_out::unmark_trees ()
    5160              : {
    5161      2706048 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5162    619656032 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5163              :     {
    5164    616949984 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5165    616949984 :       int ref = (*iter).second;
    5166              :       /* We should have visited the node, and converted its mergeable
    5167              :          reference to a regular reference.  */
    5168    616949984 :       gcc_checking_assert (TREE_VISITED (node)
    5169              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5170    616949984 :       TREE_VISITED (node) = false;
    5171              :     }
    5172      2706048 : }
    5173              : 
    5174              : /* Mark DECL for by-value walking.  We do this by inserting it into
    5175              :    the tree map with a reference of zero.  May be called multiple
    5176              :    times on the same node.  */
    5177              : 
    5178              : void
    5179      4195670 : trees_out::mark_by_value (tree decl)
    5180              : {
    5181      4195670 :   gcc_checking_assert (DECL_P (decl)
    5182              :                        /* Enum consts are INTEGER_CSTS.  */
    5183              :                        || TREE_CODE (decl) == INTEGER_CST
    5184              :                        || TREE_CODE (decl) == TREE_BINFO);
    5185              : 
    5186      4195670 :   if (TREE_VISITED (decl))
    5187              :     /* Must already be forced or fixed.  */
    5188         4014 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5189              :   else
    5190              :     {
    5191      4191656 :       bool existed = tree_map.put (decl, tag_value);
    5192      4191656 :       gcc_checking_assert (!existed);
    5193      4191656 :       TREE_VISITED (decl) = true;
    5194              :     }
    5195      4195670 : }
    5196              : 
    5197              : int
    5198    117647824 : trees_out::get_tag (tree t)
    5199              : {
    5200    117647824 :   gcc_checking_assert (TREE_VISITED (t));
    5201    117647824 :   return *tree_map.get (t);
    5202              : }
    5203              : 
    5204              : /* Insert T into the map, return its tag number.    */
    5205              : 
    5206              : int
    5207     98290427 : trees_out::insert (tree t, walk_kind walk)
    5208              : {
    5209     98290427 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5210     98290427 :   int tag = --ref_num;
    5211     98290427 :   bool existed;
    5212     98290427 :   int &slot = tree_map.get_or_insert (t, &existed);
    5213     98290427 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5214              :                        && (!existed
    5215              :                            || (walk == WK_value && slot == tag_value)));
    5216     98290427 :   TREE_VISITED (t) = true;
    5217     98290427 :   slot = tag;
    5218              : 
    5219     98290427 :   return tag;
    5220              : }
    5221              : 
    5222              : /* Insert T into the backreference array.  Return its back reference
    5223              :    number.  */
    5224              : 
    5225              : int
    5226     21178875 : trees_in::insert (tree t)
    5227              : {
    5228     21178875 :   gcc_checking_assert (t || get_overrun ());
    5229     21178875 :   back_refs.safe_push (t);
    5230     21178875 :   return -(int)back_refs.length ();
    5231              : }
    5232              : 
    5233              : /* A chained set of decls.  */
    5234              : 
    5235              : void
    5236       152954 : trees_out::chained_decls (tree decls)
    5237              : {
    5238       350805 :   for (; decls; decls = DECL_CHAIN (decls))
    5239       197851 :     tree_node (decls);
    5240       152954 :   tree_node (NULL_TREE);
    5241       152954 : }
    5242              : 
    5243              : tree
    5244        57983 : trees_in::chained_decls ()
    5245              : {
    5246        57983 :   tree decls = NULL_TREE;
    5247        57983 :   for (tree *chain = &decls;;)
    5248       138616 :     if (tree decl = tree_node ())
    5249              :       {
    5250        80633 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5251              :           {
    5252            0 :             set_overrun ();
    5253            0 :             break;
    5254              :           }
    5255        80633 :         *chain = decl;
    5256        80633 :         chain = &DECL_CHAIN (decl);
    5257              :       }
    5258              :     else
    5259        80633 :       break;
    5260              : 
    5261        57983 :   return decls;
    5262              : }
    5263              : 
    5264              : /* A vector of decls following DECL_CHAIN.  */
    5265              : 
    5266              : void
    5267       392480 : trees_out::vec_chained_decls (tree decls)
    5268              : {
    5269       392480 :   if (streaming_p ())
    5270              :     {
    5271              :       unsigned len = 0;
    5272              : 
    5273      1240108 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5274      1043914 :         len++;
    5275       196194 :       u (len);
    5276              :     }
    5277              : 
    5278      2480718 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5279              :     {
    5280       419927 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5281      2104251 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5282              :         /* An anonynmous struct with a typedef name.  An odd thing to
    5283              :            write.  */
    5284            8 :         tree_node (NULL_TREE);
    5285              :       else
    5286      2088230 :         tree_node (decl);
    5287              :     }
    5288       392480 : }
    5289              : 
    5290              : vec<tree, va_heap> *
    5291       133169 : trees_in::vec_chained_decls ()
    5292              : {
    5293       133169 :   vec<tree, va_heap> *v = NULL;
    5294              : 
    5295       133169 :   if (unsigned len = u ())
    5296              :     {
    5297        69898 :       vec_alloc (v, len);
    5298              : 
    5299       849771 :       for (unsigned ix = 0; ix < len; ix++)
    5300              :         {
    5301       779873 :           tree decl = tree_node ();
    5302       779873 :           if (decl && !DECL_P (decl))
    5303              :             {
    5304            0 :               set_overrun ();
    5305            0 :               break;
    5306              :             }
    5307       779873 :           v->quick_push (decl);
    5308              :         }
    5309              : 
    5310        69898 :       if (get_overrun ())
    5311              :         {
    5312            0 :           vec_free (v);
    5313            0 :           v = NULL;
    5314              :         }
    5315              :     }
    5316              : 
    5317       133169 :   return v;
    5318              : }
    5319              : 
    5320              : /* A vector of trees.  */
    5321              : 
    5322              : void
    5323       276022 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5324              : {
    5325       276022 :   unsigned len = vec_safe_length (v);
    5326       276022 :   if (streaming_p ())
    5327       137988 :     u (len);
    5328       353716 :   for (unsigned ix = 0; ix != len; ix++)
    5329        77694 :     tree_node ((*v)[ix]);
    5330       276022 : }
    5331              : 
    5332              : vec<tree, va_gc> *
    5333        93260 : trees_in::tree_vec ()
    5334              : {
    5335        93260 :   vec<tree, va_gc> *v = NULL;
    5336        93260 :   if (unsigned len = u ())
    5337              :     {
    5338        23542 :       vec_alloc (v, len);
    5339        49685 :       for (unsigned ix = 0; ix != len; ix++)
    5340        26143 :         v->quick_push (tree_node ());
    5341              :     }
    5342        93260 :   return v;
    5343              : }
    5344              : 
    5345              : /* A vector of tree pairs.  */
    5346              : 
    5347              : void
    5348         7084 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5349              : {
    5350         7084 :   unsigned len = vec_safe_length (v);
    5351         7084 :   if (streaming_p ())
    5352         3542 :     u (len);
    5353         7084 :   if (len)
    5354        38384 :     for (unsigned ix = 0; ix != len; ix++)
    5355              :       {
    5356        31430 :         tree_pair_s const &s = (*v)[ix];
    5357        31430 :         tree_node (s.purpose);
    5358        31430 :         tree_node (s.value);
    5359              :       }
    5360         7084 : }
    5361              : 
    5362              : vec<tree_pair_s, va_gc> *
    5363         2671 : trees_in::tree_pair_vec ()
    5364              : {
    5365         2671 :   vec<tree_pair_s, va_gc> *v = NULL;
    5366         2671 :   if (unsigned len = u ())
    5367              :     {
    5368         2617 :       vec_alloc (v, len);
    5369        14575 :       for (unsigned ix = 0; ix != len; ix++)
    5370              :         {
    5371        11958 :           tree_pair_s s;
    5372        11958 :           s.purpose = tree_node ();
    5373        11958 :           s.value = tree_node ();
    5374        11958 :           v->quick_push (s);
    5375              :       }
    5376              :     }
    5377         2671 :   return v;
    5378              : }
    5379              : 
    5380              : void
    5381       417376 : trees_out::tree_list (tree list, bool has_purpose)
    5382              : {
    5383      1868719 :   for (; list; list = TREE_CHAIN (list))
    5384              :     {
    5385      1451343 :       gcc_checking_assert (TREE_VALUE (list));
    5386      1451343 :       tree_node (TREE_VALUE (list));
    5387      1451343 :       if (has_purpose)
    5388      1402751 :         tree_node (TREE_PURPOSE (list));
    5389              :     }
    5390       417376 :   tree_node (NULL_TREE);
    5391       417376 : }
    5392              : 
    5393              : tree
    5394       143910 : trees_in::tree_list (bool has_purpose)
    5395              : {
    5396       143910 :   tree res = NULL_TREE;
    5397              : 
    5398       691222 :   for (tree *chain = &res; tree value = tree_node ();
    5399      1094624 :        chain = &TREE_CHAIN (*chain))
    5400              :     {
    5401       547312 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5402       547312 :       *chain = build_tree_list (purpose, value);
    5403       547312 :     }
    5404              : 
    5405       143910 :   return res;
    5406              : }
    5407              : 
    5408              : #define CASE_OMP_SIMD_CODE \
    5409              :     case OMP_SIMD:                      \
    5410              :     case OMP_STRUCTURED_BLOCK:          \
    5411              :     case OMP_LOOP:                      \
    5412              :     case OMP_ORDERED:                   \
    5413              :     case OMP_TILE:                      \
    5414              :     case OMP_UNROLL
    5415              : #define CASE_OMP_CODE \
    5416              :     case OMP_PARALLEL:                  \
    5417              :     case OMP_TASK:                      \
    5418              :     case OMP_FOR:                       \
    5419              :     case OMP_DISTRIBUTE:                \
    5420              :     case OMP_TASKLOOP:                  \
    5421              :     case OMP_TEAMS:                     \
    5422              :     case OMP_TARGET_DATA:               \
    5423              :     case OMP_TARGET:                    \
    5424              :     case OMP_SECTIONS:                  \
    5425              :     case OMP_CRITICAL:                  \
    5426              :     case OMP_SINGLE:                    \
    5427              :     case OMP_SCOPE:                     \
    5428              :     case OMP_TASKGROUP:                 \
    5429              :     case OMP_MASKED:                    \
    5430              :     case OMP_DISPATCH:                  \
    5431              :     case OMP_INTEROP:                   \
    5432              :     case OMP_MASTER:                    \
    5433              :     case OMP_TARGET_UPDATE:             \
    5434              :     case OMP_TARGET_ENTER_DATA:         \
    5435              :     case OMP_TARGET_EXIT_DATA:          \
    5436              :     case OMP_METADIRECTIVE:             \
    5437              :     case OMP_ATOMIC:                    \
    5438              :     case OMP_ATOMIC_READ:               \
    5439              :     case OMP_ATOMIC_CAPTURE_OLD:        \
    5440              :     case OMP_ATOMIC_CAPTURE_NEW
    5441              : #define CASE_OACC_CODE \
    5442              :     case OACC_PARALLEL:                 \
    5443              :     case OACC_KERNELS:                  \
    5444              :     case OACC_SERIAL:                   \
    5445              :     case OACC_DATA:                     \
    5446              :     case OACC_HOST_DATA:                \
    5447              :     case OACC_LOOP:                     \
    5448              :     case OACC_CACHE:                    \
    5449              :     case OACC_DECLARE:                  \
    5450              :     case OACC_ENTER_DATA:               \
    5451              :     case OACC_EXIT_DATA:                \
    5452              :     case OACC_UPDATE
    5453              : 
    5454              : /* Start tree write.  Write information to allocate the receiving
    5455              :    node.  */
    5456              : 
    5457              : void
    5458     19764563 : trees_out::start (tree t, bool code_streamed)
    5459              : {
    5460     19764563 :   if (TYPE_P (t))
    5461              :     {
    5462       757564 :       enum tree_code code = TREE_CODE (t);
    5463       757564 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5464              :       /* All these types are TYPE_NON_COMMON.  */
    5465       757564 :       gcc_checking_assert (code == RECORD_TYPE
    5466              :                            || code == UNION_TYPE
    5467              :                            || code == ENUMERAL_TYPE
    5468              :                            || code == TEMPLATE_TYPE_PARM
    5469              :                            || code == TEMPLATE_TEMPLATE_PARM
    5470              :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5471              :     }
    5472              : 
    5473     19764563 :   if (!code_streamed)
    5474     19098204 :     u (TREE_CODE (t));
    5475              : 
    5476     19764563 :   switch (TREE_CODE (t))
    5477              :     {
    5478     17678221 :     default:
    5479     17678221 :       if (VL_EXP_CLASS_P (t))
    5480       758934 :         u (VL_EXP_OPERAND_LENGTH (t));
    5481              :       break;
    5482              : 
    5483       802155 :     case INTEGER_CST:
    5484       802155 :       u (TREE_INT_CST_NUNITS (t));
    5485       802155 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5486       802155 :       break;
    5487              : 
    5488           18 :     case OMP_CLAUSE:
    5489           18 :       u (OMP_CLAUSE_CODE (t));
    5490           18 :       break;
    5491              : 
    5492            6 :     CASE_OMP_SIMD_CODE:
    5493            6 :       state->extensions |= SE_OPENMP_SIMD;
    5494            6 :       break;
    5495              : 
    5496            9 :     CASE_OMP_CODE:
    5497            9 :       state->extensions |= SE_OPENMP;
    5498            9 :       break;
    5499              : 
    5500            6 :     CASE_OACC_CODE:
    5501            6 :       state->extensions |= SE_OPENACC;
    5502            6 :       break;
    5503              : 
    5504        58834 :     case STRING_CST:
    5505        58834 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5506        58834 :       break;
    5507              : 
    5508           18 :     case RAW_DATA_CST:
    5509           18 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    5510              :         {
    5511              :           /* Stream RAW_DATA_CST with no owner (i.e. data pointing
    5512              :              into libcpp buffers) as something we can stream in as
    5513              :              STRING_CST which owns the data.  */
    5514            6 :           u (0);
    5515              :           /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5516              :              here as there isn't a null termination after it.  */
    5517            6 :           z (RAW_DATA_LENGTH (t));
    5518            6 :           if (RAW_DATA_LENGTH (t))
    5519            6 :             if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
    5520              :               {
    5521            6 :                 memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5522            6 :                 ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
    5523              :               }
    5524              :         }
    5525              :       else
    5526              :         {
    5527           12 :           gcc_assert (RAW_DATA_LENGTH (t));
    5528           12 :           u (RAW_DATA_LENGTH (t));
    5529              :         }
    5530              :       break;
    5531              : 
    5532           18 :     case VECTOR_CST:
    5533           18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5534           18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5535           18 :       break;
    5536              : 
    5537       134446 :     case TREE_BINFO:
    5538       134446 :       u (BINFO_N_BASE_BINFOS (t));
    5539       134446 :       break;
    5540              : 
    5541      1090832 :     case TREE_VEC:
    5542      1090832 :       u (TREE_VEC_LENGTH (t));
    5543      1090832 :       break;
    5544              : 
    5545            0 :     case FIXED_CST:
    5546            0 :       gcc_unreachable (); /* Not supported in C++.  */
    5547            0 :       break;
    5548              : 
    5549            0 :     case IDENTIFIER_NODE:
    5550            0 :     case SSA_NAME:
    5551            0 :     case TARGET_MEM_REF:
    5552            0 :     case TRANSLATION_UNIT_DECL:
    5553              :       /* We shouldn't meet these.  */
    5554            0 :       gcc_unreachable ();
    5555     19764563 :       break;
    5556              :     }
    5557     19764563 : }
    5558              : 
    5559              : /* Start tree read.  Allocate the receiving node.  */
    5560              : 
    5561              : tree
    5562     15232223 : trees_in::start (unsigned code)
    5563              : {
    5564     15232223 :   tree t = NULL_TREE;
    5565              : 
    5566     15232223 :   if (!code)
    5567     13800691 :     code = u ();
    5568              : 
    5569     15232223 :   switch (code)
    5570              :     {
    5571     13656529 :     default:
    5572     13656529 :       if (code >= MAX_TREE_CODES)
    5573              :         {
    5574            0 :         fail:
    5575            0 :           set_overrun ();
    5576            0 :           return NULL_TREE;
    5577              :         }
    5578     13656529 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5579              :         {
    5580       592262 :           unsigned ops = u ();
    5581       592262 :           t = build_vl_exp (tree_code (code), ops);
    5582              :         }
    5583              :       else
    5584     13064267 :         t = make_node (tree_code (code));
    5585              :       break;
    5586              : 
    5587       594259 :     case INTEGER_CST:
    5588       594259 :       {
    5589       594259 :         unsigned n = u ();
    5590       594259 :         unsigned e = u ();
    5591       594259 :         t = make_int_cst (n, e);
    5592              :       }
    5593       594259 :       break;
    5594              : 
    5595           18 :     case OMP_CLAUSE:
    5596           18 :       t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
    5597           18 :       break;
    5598              : 
    5599            9 :     CASE_OMP_SIMD_CODE:
    5600            9 :       if (!(state->extensions & SE_OPENMP_SIMD))
    5601            0 :         goto fail;
    5602            9 :       t = make_node (tree_code (code));
    5603            9 :       break;
    5604              : 
    5605            9 :     CASE_OMP_CODE:
    5606            9 :       if (!(state->extensions & SE_OPENMP))
    5607            0 :         goto fail;
    5608            9 :       t = make_node (tree_code (code));
    5609            9 :       break;
    5610              : 
    5611            6 :     CASE_OACC_CODE:
    5612            6 :       if (!(state->extensions & SE_OPENACC))
    5613            0 :         goto fail;
    5614            6 :       t = make_node (tree_code (code));
    5615            6 :       break;
    5616              : 
    5617        50128 :     case STRING_CST:
    5618        50128 :       {
    5619        50128 :         size_t l;
    5620        50128 :         const char *chars = str (&l);
    5621        50128 :         t = build_string (l, chars);
    5622              :       }
    5623        50128 :       break;
    5624              : 
    5625            9 :     case RAW_DATA_CST:
    5626            9 :       {
    5627            9 :         size_t l = u ();
    5628            9 :         if (l == 0)
    5629              :           {
    5630              :             /* Stream in RAW_DATA_CST with no owner as STRING_CST
    5631              :                which owns the data.  */
    5632            3 :             const char *chars = str (&l);
    5633            3 :             t = build_string (l, chars);
    5634              :           }
    5635              :         else
    5636              :           {
    5637            6 :             t = make_node (RAW_DATA_CST);
    5638            6 :             RAW_DATA_LENGTH (t) = l;
    5639              :           }
    5640              :       }
    5641            9 :       break;
    5642              : 
    5643           24 :     case VECTOR_CST:
    5644           24 :       {
    5645           24 :         unsigned log2_npats = u ();
    5646           24 :         unsigned elts_per = u ();
    5647           24 :         t = make_vector (log2_npats, elts_per);
    5648              :       }
    5649           24 :       break;
    5650              : 
    5651        90589 :     case TREE_BINFO:
    5652        90589 :       t = make_tree_binfo (u ());
    5653        90589 :       break;
    5654              : 
    5655       840643 :     case TREE_VEC:
    5656       840643 :       t = make_tree_vec (u ());
    5657       840643 :       break;
    5658              : 
    5659            0 :     case FIXED_CST:
    5660            0 :     case IDENTIFIER_NODE:
    5661            0 :     case SSA_NAME:
    5662            0 :     case TARGET_MEM_REF:
    5663            0 :     case TRANSLATION_UNIT_DECL:
    5664            0 :       goto fail;
    5665              :     }
    5666              : 
    5667              :   return t;
    5668              : }
    5669              : 
    5670              : /* The kinds of interface an importer could have for a decl.  */
    5671              : 
    5672              : enum class importer_interface {
    5673              :   unknown,        /* The definition may or may not need to be emitted.  */
    5674              :   external,       /* The definition can always be found in another TU.  */
    5675              :   internal,       /* The definition should be emitted in the importer's TU.  */
    5676              :   always_emit,    /* The definition must be emitted in the importer's TU,
    5677              :                      regardless of if it's used or not. */
    5678              : };
    5679              : 
    5680              : /* Returns what kind of interface an importer will have of DECL.  */
    5681              : 
    5682              : static importer_interface
    5683       747035 : get_importer_interface (tree decl)
    5684              : {
    5685              :   /* Internal linkage entities must be emitted in each importer if
    5686              :      there is a definition available.  */
    5687       747035 :   if (!TREE_PUBLIC (decl))
    5688              :     return importer_interface::internal;
    5689              : 
    5690              :   /* Other entities that aren't vague linkage are either not definitions
    5691              :      or will be publicly emitted in this TU, so importers can just refer
    5692              :      to an external definition.  */
    5693       356547 :   if (!vague_linkage_p (decl))
    5694              :     return importer_interface::external;
    5695              : 
    5696              :   /* For explicit instantiations, importers can always rely on there
    5697              :      being a definition in another TU, unless this is a definition
    5698              :      in a header module: in which case the importer will always need
    5699              :      to emit it.  */
    5700       350193 :   if (DECL_LANG_SPECIFIC (decl)
    5701       350193 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5702        26435 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5703        26435 :             ? importer_interface::always_emit
    5704              :             : importer_interface::external);
    5705              : 
    5706              :   /* A gnu_inline function is never emitted in any TU.  */
    5707       323758 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5708       238199 :       && DECL_DECLARED_INLINE_P (decl)
    5709       553830 :       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
    5710              :     return importer_interface::external;
    5711              : 
    5712              :   /* Everything else has vague linkage.  */
    5713              :   return importer_interface::unknown;
    5714              : }
    5715              : 
    5716              : /* The structure streamers access the raw fields, because the
    5717              :    alternative, of using the accessor macros can require using
    5718              :    different accessors for the same underlying field, depending on the
    5719              :    tree code.  That's both confusing and annoying.  */
    5720              : 
    5721              : /* Read & write the core boolean flags.  */
    5722              : 
    5723              : void
    5724     19799156 : trees_out::core_bools (tree t, bits_out& bits)
    5725              : {
    5726              : #define WB(X) (bits.b (X))
    5727              : /* Stream X if COND holds, and if !COND stream a dummy value so that the
    5728              :    overall number of bits streamed is independent of the runtime value
    5729              :    of COND, which allows the compiler to better optimize this function.  */
    5730              : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
    5731     19799156 :   tree_code code = TREE_CODE (t);
    5732              : 
    5733     19799156 :   WB (t->base.side_effects_flag);
    5734     19799156 :   WB (t->base.constant_flag);
    5735     19799156 :   WB (t->base.addressable_flag);
    5736     19799156 :   WB (t->base.volatile_flag);
    5737     19799156 :   WB (t->base.readonly_flag);
    5738              :   /* base.asm_written_flag is a property of the current TU's use of
    5739              :      this decl.  */
    5740     19799156 :   WB (t->base.nowarning_flag);
    5741              :   /* base.visited read as zero (it's set for writer, because that's
    5742              :      how we mark nodes).  */
    5743              :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5744              :      decls they use.  */
    5745     19799156 :   WB (t->base.nothrow_flag);
    5746     19799156 :   WB (t->base.static_flag);
    5747              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5748     19799156 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5749     19799156 :   WB (t->base.private_flag);
    5750     19799156 :   WB (t->base.protected_flag);
    5751     19799156 :   WB (t->base.deprecated_flag);
    5752     19799156 :   WB (t->base.default_def_flag);
    5753              : 
    5754     19799156 :   switch (code)
    5755              :     {
    5756              :     case CALL_EXPR:
    5757              :     case INTEGER_CST:
    5758              :     case SSA_NAME:
    5759              :     case TARGET_MEM_REF:
    5760              :     case TREE_VEC:
    5761              :       /* These use different base.u fields.  */
    5762              :       return;
    5763              : 
    5764     17164527 :     default:
    5765     17164527 :       WB (t->base.u.bits.lang_flag_0);
    5766     17164527 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5767     17164527 :       if (!flag_1)
    5768              :         ;
    5769       561050 :       else if (code == TEMPLATE_INFO)
    5770              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5771              :         flag_1 = false;
    5772       554998 :       else if (code == VAR_DECL)
    5773              :         {
    5774              :           /* This is DECL_INITIALIZED_P.  */
    5775       101327 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5776              :             /* We'll set this when reading the definition.  */
    5777     17164527 :             flag_1 = false;
    5778              :         }
    5779     17164527 :       WB (flag_1);
    5780     17164527 :       WB (t->base.u.bits.lang_flag_2);
    5781     17164527 :       WB (t->base.u.bits.lang_flag_3);
    5782     17164527 :       WB (t->base.u.bits.lang_flag_4);
    5783     17164527 :       WB (t->base.u.bits.lang_flag_5);
    5784     17164527 :       WB (t->base.u.bits.lang_flag_6);
    5785     17164527 :       WB (t->base.u.bits.saturating_flag);
    5786     17164527 :       WB (t->base.u.bits.unsigned_flag);
    5787     17164527 :       WB (t->base.u.bits.packed_flag);
    5788     17164527 :       WB (t->base.u.bits.user_align);
    5789     17164527 :       WB (t->base.u.bits.nameless_flag);
    5790     17164527 :       WB (t->base.u.bits.atomic_flag);
    5791     17164527 :       WB (t->base.u.bits.unavailable_flag);
    5792     17164527 :       break;
    5793              :     }
    5794              : 
    5795     17164527 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5796              :     {
    5797       792157 :       WB (t->type_common.no_force_blk_flag);
    5798       792157 :       WB (t->type_common.needs_constructing_flag);
    5799       792157 :       WB (t->type_common.transparent_aggr_flag);
    5800       792157 :       WB (t->type_common.restrict_flag);
    5801       792157 :       WB (t->type_common.string_flag);
    5802       792157 :       WB (t->type_common.lang_flag_0);
    5803       792157 :       WB (t->type_common.lang_flag_1);
    5804       792157 :       WB (t->type_common.lang_flag_2);
    5805       792157 :       WB (t->type_common.lang_flag_3);
    5806       792157 :       WB (t->type_common.lang_flag_4);
    5807       792157 :       WB (t->type_common.lang_flag_5);
    5808       792157 :       WB (t->type_common.lang_flag_6);
    5809       792157 :       WB (t->type_common.typeless_storage);
    5810              :     }
    5811              : 
    5812     17164527 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5813              :     return;
    5814              : 
    5815      4304538 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5816              :     {
    5817      4304538 :       WB (t->decl_common.nonlocal_flag);
    5818      4304538 :       WB (t->decl_common.virtual_flag);
    5819      4304538 :       WB (t->decl_common.ignored_flag);
    5820      4304538 :       WB (t->decl_common.abstract_flag);
    5821      4304538 :       WB (t->decl_common.artificial_flag);
    5822      4304538 :       WB (t->decl_common.preserve_flag);
    5823      4304538 :       WB (t->decl_common.debug_expr_is_from);
    5824      4304538 :       WB (t->decl_common.lang_flag_0);
    5825      4304538 :       WB (t->decl_common.lang_flag_1);
    5826      4304538 :       WB (t->decl_common.lang_flag_2);
    5827      4304538 :       WB (t->decl_common.lang_flag_3);
    5828      4304538 :       WB (t->decl_common.lang_flag_4);
    5829              : 
    5830      4304538 :       {
    5831              :         /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
    5832              :            we need to import or export any vague-linkage entities on
    5833              :            stream-in.  */
    5834      4304538 :         bool interface_known = t->decl_common.lang_flag_5;
    5835      4304538 :         if (interface_known
    5836      4304538 :             && get_importer_interface (t) == importer_interface::unknown)
    5837              :           interface_known = false;
    5838      4304538 :         WB (interface_known);
    5839              :       }
    5840              : 
    5841      4304538 :       WB (t->decl_common.lang_flag_6);
    5842      4304538 :       WB (t->decl_common.lang_flag_7);
    5843      4304538 :       WB (t->decl_common.lang_flag_8);
    5844      4304538 :       WB (t->decl_common.decl_flag_0);
    5845              : 
    5846      4304538 :       {
    5847              :         /* DECL_EXTERNAL -> decl_flag_1
    5848              :              == it is defined elsewhere
    5849              :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5850              :              == that was a lie, it is here  */
    5851              : 
    5852      4304538 :         bool is_external = t->decl_common.decl_flag_1;
    5853              :         /* maybe_emit_vtables relies on vtables being marked as
    5854              :            DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing.  */
    5855      4304538 :         if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
    5856              :           is_external = true;
    5857              :         /* Things we emit here might well be external from the POV of an
    5858              :            importer.  */
    5859      4304135 :         if (!is_external
    5860      3615317 :             && VAR_OR_FUNCTION_DECL_P (t)
    5861      4644673 :             && get_importer_interface (t) == importer_interface::external)
    5862              :           is_external = true;
    5863      4304538 :         WB (is_external);
    5864              :       }
    5865              : 
    5866      4304538 :       WB (t->decl_common.decl_flag_2);
    5867      4304538 :       WB (t->decl_common.decl_flag_3);
    5868      4304538 :       WB (t->decl_common.not_gimple_reg_flag);
    5869      4304538 :       WB (t->decl_common.decl_by_reference_flag);
    5870      4304538 :       WB (t->decl_common.decl_read_flag);
    5871      4304538 :       WB (t->decl_common.decl_nonshareable_flag);
    5872      4304538 :       WB (t->decl_common.decl_not_flexarray);
    5873              :     }
    5874              :   else
    5875              :     return;
    5876              : 
    5877      4304538 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5878              :     {
    5879      2025712 :       WB (t->decl_with_vis.defer_output);
    5880      2025712 :       WB (t->decl_with_vis.hard_register);
    5881      2025712 :       WB (t->decl_with_vis.common_flag);
    5882      2025712 :       WB (t->decl_with_vis.in_text_section);
    5883      2025712 :       WB (t->decl_with_vis.in_constant_pool);
    5884      2025712 :       WB (t->decl_with_vis.dllimport_flag);
    5885      2025712 :       WB (t->decl_with_vis.weak_flag);
    5886      2025712 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5887      2025712 :       WB (t->decl_with_vis.comdat_flag);
    5888      2025712 :       WB (t->decl_with_vis.visibility_specified);
    5889      2025712 :       WB (t->decl_with_vis.init_priority_p);
    5890      2025712 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5891      2025712 :       WB (t->decl_with_vis.cxx_constructor);
    5892      2025712 :       WB (t->decl_with_vis.cxx_destructor);
    5893      2025712 :       WB (t->decl_with_vis.final);
    5894      2025712 :       WB (t->decl_with_vis.regdecl_flag);
    5895              :     }
    5896              :   else
    5897              :     return;
    5898              : 
    5899      2025712 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5900              :     {
    5901       598014 :       WB (t->function_decl.static_ctor_flag);
    5902       598014 :       WB (t->function_decl.static_dtor_flag);
    5903       598014 :       WB (t->function_decl.uninlinable);
    5904       598014 :       WB (t->function_decl.possibly_inlined);
    5905       598014 :       WB (t->function_decl.novops_flag);
    5906       598014 :       WB (t->function_decl.returns_twice_flag);
    5907       598014 :       WB (t->function_decl.malloc_flag);
    5908       598014 :       WB (t->function_decl.declared_inline_flag);
    5909       598014 :       WB (t->function_decl.no_inline_warning_flag);
    5910       598014 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5911       598014 :       WB (t->function_decl.no_limit_stack);
    5912       598014 :       WB (t->function_decl.disregard_inline_limits);
    5913       598014 :       WB (t->function_decl.pure_flag);
    5914       598014 :       WB (t->function_decl.looping_const_or_pure_flag);
    5915              : 
    5916       598014 :       WB (t->function_decl.has_debug_args_flag);
    5917       598014 :       WB (t->function_decl.versioned_function);
    5918       598014 :       WB (t->function_decl.replaceable_operator);
    5919              : 
    5920              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5921       598014 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5922       598014 :       WB ((kind >> 0) & 1);
    5923       598014 :       WB ((kind >> 1) & 1);
    5924              :     }
    5925              : #undef WB_IF
    5926              : #undef WB
    5927              : }
    5928              : 
    5929              : bool
    5930     15253707 : trees_in::core_bools (tree t, bits_in& bits)
    5931              : {
    5932              : #define RB(X) ((X) = bits.b ())
    5933              : /* See the comment for WB_IF in trees_out::core_bools.  */
    5934              : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
    5935              : 
    5936     15253707 :   tree_code code = TREE_CODE (t);
    5937              : 
    5938     15253707 :   RB (t->base.side_effects_flag);
    5939     15253707 :   RB (t->base.constant_flag);
    5940     15253707 :   RB (t->base.addressable_flag);
    5941     15253707 :   RB (t->base.volatile_flag);
    5942     15253707 :   RB (t->base.readonly_flag);
    5943              :   /* base.asm_written_flag is not streamed.  */
    5944     15253707 :   RB (t->base.nowarning_flag);
    5945              :   /* base.visited is not streamed.  */
    5946              :   /* base.used_flag is not streamed.  */
    5947     15253707 :   RB (t->base.nothrow_flag);
    5948     15253707 :   RB (t->base.static_flag);
    5949     15253707 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5950     15253707 :   RB (t->base.private_flag);
    5951     15253707 :   RB (t->base.protected_flag);
    5952     15253707 :   RB (t->base.deprecated_flag);
    5953     15253707 :   RB (t->base.default_def_flag);
    5954              : 
    5955     15253707 :   switch (code)
    5956              :     {
    5957      2014348 :     case CALL_EXPR:
    5958      2014348 :     case INTEGER_CST:
    5959      2014348 :     case SSA_NAME:
    5960      2014348 :     case TARGET_MEM_REF:
    5961      2014348 :     case TREE_VEC:
    5962              :       /* These use different base.u fields.  */
    5963      2014348 :       goto done;
    5964              : 
    5965     13239359 :     default:
    5966     13239359 :       RB (t->base.u.bits.lang_flag_0);
    5967     13239359 :       RB (t->base.u.bits.lang_flag_1);
    5968     13239359 :       RB (t->base.u.bits.lang_flag_2);
    5969     13239359 :       RB (t->base.u.bits.lang_flag_3);
    5970     13239359 :       RB (t->base.u.bits.lang_flag_4);
    5971     13239359 :       RB (t->base.u.bits.lang_flag_5);
    5972     13239359 :       RB (t->base.u.bits.lang_flag_6);
    5973     13239359 :       RB (t->base.u.bits.saturating_flag);
    5974     13239359 :       RB (t->base.u.bits.unsigned_flag);
    5975     13239359 :       RB (t->base.u.bits.packed_flag);
    5976     13239359 :       RB (t->base.u.bits.user_align);
    5977     13239359 :       RB (t->base.u.bits.nameless_flag);
    5978     13239359 :       RB (t->base.u.bits.atomic_flag);
    5979     13239359 :       RB (t->base.u.bits.unavailable_flag);
    5980     13239359 :       break;
    5981              :     }
    5982              : 
    5983     13239359 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5984              :     {
    5985       574770 :       RB (t->type_common.no_force_blk_flag);
    5986       574770 :       RB (t->type_common.needs_constructing_flag);
    5987       574770 :       RB (t->type_common.transparent_aggr_flag);
    5988       574770 :       RB (t->type_common.restrict_flag);
    5989       574770 :       RB (t->type_common.string_flag);
    5990       574770 :       RB (t->type_common.lang_flag_0);
    5991       574770 :       RB (t->type_common.lang_flag_1);
    5992       574770 :       RB (t->type_common.lang_flag_2);
    5993       574770 :       RB (t->type_common.lang_flag_3);
    5994       574770 :       RB (t->type_common.lang_flag_4);
    5995       574770 :       RB (t->type_common.lang_flag_5);
    5996       574770 :       RB (t->type_common.lang_flag_6);
    5997       574770 :       RB (t->type_common.typeless_storage);
    5998              :     }
    5999              : 
    6000     13239359 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6001      9978429 :     goto done;
    6002              : 
    6003      3260930 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6004              :     {
    6005      3260930 :       RB (t->decl_common.nonlocal_flag);
    6006      3260930 :       RB (t->decl_common.virtual_flag);
    6007      3260930 :       RB (t->decl_common.ignored_flag);
    6008      3260930 :       RB (t->decl_common.abstract_flag);
    6009      3260930 :       RB (t->decl_common.artificial_flag);
    6010      3260930 :       RB (t->decl_common.preserve_flag);
    6011      3260930 :       RB (t->decl_common.debug_expr_is_from);
    6012      3260930 :       RB (t->decl_common.lang_flag_0);
    6013      3260930 :       RB (t->decl_common.lang_flag_1);
    6014      3260930 :       RB (t->decl_common.lang_flag_2);
    6015      3260930 :       RB (t->decl_common.lang_flag_3);
    6016      3260930 :       RB (t->decl_common.lang_flag_4);
    6017      3260930 :       RB (t->decl_common.lang_flag_5);
    6018      3260930 :       RB (t->decl_common.lang_flag_6);
    6019      3260930 :       RB (t->decl_common.lang_flag_7);
    6020      3260930 :       RB (t->decl_common.lang_flag_8);
    6021      3260930 :       RB (t->decl_common.decl_flag_0);
    6022      3260930 :       RB (t->decl_common.decl_flag_1);
    6023      3260930 :       RB (t->decl_common.decl_flag_2);
    6024      3260930 :       RB (t->decl_common.decl_flag_3);
    6025      3260930 :       RB (t->decl_common.not_gimple_reg_flag);
    6026      3260930 :       RB (t->decl_common.decl_by_reference_flag);
    6027      3260930 :       RB (t->decl_common.decl_read_flag);
    6028      3260930 :       RB (t->decl_common.decl_nonshareable_flag);
    6029      3260930 :       RB (t->decl_common.decl_not_flexarray);
    6030              :     }
    6031              :   else
    6032            0 :     goto done;
    6033              : 
    6034      3260930 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6035              :     {
    6036      1500753 :       RB (t->decl_with_vis.defer_output);
    6037      1500753 :       RB (t->decl_with_vis.hard_register);
    6038      1500753 :       RB (t->decl_with_vis.common_flag);
    6039      1500753 :       RB (t->decl_with_vis.in_text_section);
    6040      1500753 :       RB (t->decl_with_vis.in_constant_pool);
    6041      1500753 :       RB (t->decl_with_vis.dllimport_flag);
    6042      1500753 :       RB (t->decl_with_vis.weak_flag);
    6043      1500753 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6044      1500753 :       RB (t->decl_with_vis.comdat_flag);
    6045      1500753 :       RB (t->decl_with_vis.visibility_specified);
    6046      1500753 :       RB (t->decl_with_vis.init_priority_p);
    6047      1500753 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6048      1500753 :       RB (t->decl_with_vis.cxx_constructor);
    6049      1500753 :       RB (t->decl_with_vis.cxx_destructor);
    6050      1500753 :       RB (t->decl_with_vis.final);
    6051      1500753 :       RB (t->decl_with_vis.regdecl_flag);
    6052              :     }
    6053              :   else
    6054      1760177 :     goto done;
    6055              : 
    6056      1500753 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6057              :     {
    6058       462193 :       RB (t->function_decl.static_ctor_flag);
    6059       462193 :       RB (t->function_decl.static_dtor_flag);
    6060       462193 :       RB (t->function_decl.uninlinable);
    6061       462193 :       RB (t->function_decl.possibly_inlined);
    6062       462193 :       RB (t->function_decl.novops_flag);
    6063       462193 :       RB (t->function_decl.returns_twice_flag);
    6064       462193 :       RB (t->function_decl.malloc_flag);
    6065       462193 :       RB (t->function_decl.declared_inline_flag);
    6066       462193 :       RB (t->function_decl.no_inline_warning_flag);
    6067       462193 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6068       462193 :       RB (t->function_decl.no_limit_stack);
    6069       462193 :       RB (t->function_decl.disregard_inline_limits);
    6070       462193 :       RB (t->function_decl.pure_flag);
    6071       462193 :       RB (t->function_decl.looping_const_or_pure_flag);
    6072              : 
    6073       462193 :       RB (t->function_decl.has_debug_args_flag);
    6074       462193 :       RB (t->function_decl.versioned_function);
    6075       462193 :       RB (t->function_decl.replaceable_operator);
    6076              : 
    6077              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6078       462193 :       unsigned kind = 0;
    6079       462193 :       kind |= unsigned (bits.b ()) << 0;
    6080       462193 :       kind |= unsigned (bits.b ()) << 1;
    6081       462193 :       t->function_decl.decl_type = function_decl_type (kind);
    6082              :     }
    6083              : #undef RB_IF
    6084              : #undef RB
    6085      1038560 : done:
    6086     15253707 :   return !get_overrun ();
    6087              : }
    6088              : 
    6089              : void
    6090      2622351 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6091              : {
    6092              : #define WB(X) (bits.b (X))
    6093      2622351 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6094              : 
    6095      2622351 :   bits.bflush ();
    6096      2622351 :   WB (lang->u.base.language == lang_cplusplus);
    6097      2622351 :   WB ((lang->u.base.use_template >> 0) & 1);
    6098      2622351 :   WB ((lang->u.base.use_template >> 1) & 1);
    6099              :   /* Do not write lang->u.base.not_really_extern, importer will set
    6100              :      when reading the definition (if any).  */
    6101      2622351 :   WB (lang->u.base.initialized_in_class);
    6102              : 
    6103      2622351 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6104      2622351 :   WB (lang->u.base.anticipated_p);
    6105      2622351 :   WB (lang->u.base.friend_or_tls);
    6106      2622351 :   WB (lang->u.base.unknown_bound_p);
    6107              :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    6108              :      they do ODR use this decl.  */
    6109      2622351 :   WB (lang->u.base.concept_p);
    6110      2622351 :   WB (lang->u.base.var_declared_inline_p);
    6111      2622351 :   WB (lang->u.base.dependent_init_p);
    6112              : 
    6113              :   /* When building a header unit, everthing is marked as purview, (so
    6114              :      we know which decls to write).  But when we import them we do not
    6115              :      want to mark them as in module purview.  */
    6116      5158561 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6117      2622351 :   WB (lang->u.base.module_attach_p);
    6118              :   /* Importer will set module_import_p and module_entity_p themselves
    6119              :      as appropriate.  */
    6120      2622351 :   WB (lang->u.base.module_keyed_decls_p);
    6121              : 
    6122      2622351 :   WB (lang->u.base.omp_declare_mapper_p);
    6123              : 
    6124      2622351 :   switch (lang->u.base.selector)
    6125              :     {
    6126            0 :     default:
    6127            0 :       gcc_unreachable ();
    6128              : 
    6129       598014 :     case lds_fn:  /* lang_decl_fn.  */
    6130       598014 :       WB (lang->u.fn.global_ctor_p);
    6131       598014 :       WB (lang->u.fn.global_dtor_p);
    6132              : 
    6133       598014 :       WB (lang->u.fn.static_function);
    6134       598014 :       WB (lang->u.fn.pure_virtual);
    6135       598014 :       WB (lang->u.fn.defaulted_p);
    6136       598014 :       WB (lang->u.fn.has_in_charge_parm_p);
    6137       598014 :       WB (lang->u.fn.has_vtt_parm_p);
    6138              :       /* There shouldn't be a pending inline at this point.  */
    6139       598014 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6140       598014 :       WB (lang->u.fn.nonconverting);
    6141       598014 :       WB (lang->u.fn.thunk_p);
    6142              : 
    6143       598014 :       WB (lang->u.fn.this_thunk_p);
    6144       598014 :       WB (lang->u.fn.omp_declare_reduction_p);
    6145       598014 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6146       598014 :       WB (lang->u.fn.immediate_fn_p);
    6147       598014 :       WB (lang->u.fn.maybe_deleted);
    6148       598014 :       WB (lang->u.fn.coroutine_p);
    6149       598014 :       WB (lang->u.fn.implicit_constexpr);
    6150       598014 :       WB (lang->u.fn.escalated_p);
    6151       598014 :       WB (lang->u.fn.xobj_func);
    6152       598014 :       goto lds_min;
    6153              : 
    6154         3133 :     case lds_decomp:  /* lang_decl_decomp.  */
    6155              :       /* No bools.  */
    6156         3133 :       goto lds_min;
    6157              : 
    6158              :     case lds_min:  /* lang_decl_min.  */
    6159      2622351 :     lds_min:
    6160              :       /* No bools.  */
    6161              :       break;
    6162              : 
    6163              :     case lds_ns:  /* lang_decl_ns.  */
    6164              :       /* No bools.  */
    6165              :       break;
    6166              : 
    6167              :     case lds_parm:  /* lang_decl_parm.  */
    6168              :       /* No bools.  */
    6169              :       break;
    6170              :     }
    6171              : #undef WB
    6172      2622351 : }
    6173              : 
    6174              : bool
    6175      2045813 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6176              : {
    6177              : #define RB(X) ((X) = bits.b ())
    6178      2045813 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6179              : 
    6180      2045813 :   bits.bflush ();
    6181      2045813 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6182      2045813 :   unsigned v;
    6183      2045813 :   v = bits.b () << 0;
    6184      2045813 :   v |= bits.b () << 1;
    6185      2045813 :   lang->u.base.use_template = v;
    6186              :   /* lang->u.base.not_really_extern is not streamed.  */
    6187      2045813 :   RB (lang->u.base.initialized_in_class);
    6188              : 
    6189      2045813 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6190      2045813 :   RB (lang->u.base.anticipated_p);
    6191      2045813 :   RB (lang->u.base.friend_or_tls);
    6192      2045813 :   RB (lang->u.base.unknown_bound_p);
    6193              :   /* lang->u.base.odr_used is not streamed.  */
    6194      2045813 :   RB (lang->u.base.concept_p);
    6195      2045813 :   RB (lang->u.base.var_declared_inline_p);
    6196      2045813 :   RB (lang->u.base.dependent_init_p);
    6197              : 
    6198      2045813 :   RB (lang->u.base.module_purview_p);
    6199      2045813 :   RB (lang->u.base.module_attach_p);
    6200              :   /* module_import_p and module_entity_p are not streamed.  */
    6201      2045813 :   RB (lang->u.base.module_keyed_decls_p);
    6202              : 
    6203      2045813 :   RB (lang->u.base.omp_declare_mapper_p);
    6204              : 
    6205      2045813 :   switch (lang->u.base.selector)
    6206              :     {
    6207            0 :     default:
    6208            0 :       gcc_unreachable ();
    6209              : 
    6210       462193 :     case lds_fn:  /* lang_decl_fn.  */
    6211       462193 :       RB (lang->u.fn.global_ctor_p);
    6212       462193 :       RB (lang->u.fn.global_dtor_p);
    6213              : 
    6214       462193 :       RB (lang->u.fn.static_function);
    6215       462193 :       RB (lang->u.fn.pure_virtual);
    6216       462193 :       RB (lang->u.fn.defaulted_p);
    6217       462193 :       RB (lang->u.fn.has_in_charge_parm_p);
    6218       462193 :       RB (lang->u.fn.has_vtt_parm_p);
    6219              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6220       462193 :       RB (lang->u.fn.nonconverting);
    6221       462193 :       RB (lang->u.fn.thunk_p);
    6222              : 
    6223       462193 :       RB (lang->u.fn.this_thunk_p);
    6224       462193 :       RB (lang->u.fn.omp_declare_reduction_p);
    6225       462193 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6226       462193 :       RB (lang->u.fn.immediate_fn_p);
    6227       462193 :       RB (lang->u.fn.maybe_deleted);
    6228       462193 :       RB (lang->u.fn.coroutine_p);
    6229       462193 :       RB (lang->u.fn.implicit_constexpr);
    6230       462193 :       RB (lang->u.fn.escalated_p);
    6231       462193 :       RB (lang->u.fn.xobj_func);
    6232       462193 :       goto lds_min;
    6233              : 
    6234         2926 :     case lds_decomp:  /* lang_decl_decomp.  */
    6235              :       /* No bools.  */
    6236         2926 :       goto lds_min;
    6237              : 
    6238              :     case lds_min:  /* lang_decl_min.  */
    6239      2045813 :     lds_min:
    6240              :       /* No bools.  */
    6241              :       break;
    6242              : 
    6243              :     case lds_ns:  /* lang_decl_ns.  */
    6244              :       /* No bools.  */
    6245              :       break;
    6246              : 
    6247              :     case lds_parm:  /* lang_decl_parm.  */
    6248              :       /* No bools.  */
    6249              :       break;
    6250              :     }
    6251              : #undef RB
    6252      2045813 :   return !get_overrun ();
    6253              : }
    6254              : 
    6255              : void
    6256       206798 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6257              : {
    6258              : #define WB(X) (bits.b (X))
    6259       206798 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6260              : 
    6261       206798 :   bits.bflush ();
    6262       206798 :   WB (lang->has_type_conversion);
    6263       206798 :   WB (lang->has_copy_ctor);
    6264       206798 :   WB (lang->has_default_ctor);
    6265       206798 :   WB (lang->const_needs_init);
    6266       206798 :   WB (lang->ref_needs_init);
    6267       206798 :   WB (lang->has_const_copy_assign);
    6268       206798 :   WB ((lang->use_template >> 0) & 1);
    6269       206798 :   WB ((lang->use_template >> 1) & 1);
    6270              : 
    6271       206798 :   WB (lang->has_mutable);
    6272       206798 :   WB (lang->com_interface);
    6273       206798 :   WB (lang->non_pod_class);
    6274       206798 :   WB (lang->nearly_empty_p);
    6275       206798 :   WB (lang->user_align);
    6276       206798 :   WB (lang->has_copy_assign);
    6277       206798 :   WB (lang->has_new);
    6278       206798 :   WB (lang->has_array_new);
    6279              : 
    6280       206798 :   WB ((lang->gets_delete >> 0) & 1);
    6281       206798 :   WB ((lang->gets_delete >> 1) & 1);
    6282       206798 :   WB (lang->interface_only);
    6283       206798 :   WB (lang->interface_unknown);
    6284       206798 :   WB (lang->contains_empty_class_p);
    6285       206798 :   WB (lang->anon_aggr);
    6286       206798 :   WB (lang->non_zero_init);
    6287       206798 :   WB (lang->empty_p);
    6288              : 
    6289       206798 :   WB (lang->vec_new_uses_cookie);
    6290       206798 :   WB (lang->declared_class);
    6291       206798 :   WB (lang->diamond_shaped);
    6292       206798 :   WB (lang->repeated_base);
    6293       206798 :   gcc_checking_assert (!lang->being_defined);
    6294              :   // lang->debug_requested
    6295       206798 :   WB (lang->fields_readonly);
    6296       206798 :   WB (lang->ptrmemfunc_flag);
    6297              : 
    6298       206798 :   WB (lang->lazy_default_ctor);
    6299       206798 :   WB (lang->lazy_copy_ctor);
    6300       206798 :   WB (lang->lazy_copy_assign);
    6301       206798 :   WB (lang->lazy_destructor);
    6302       206798 :   WB (lang->has_const_copy_ctor);
    6303       206798 :   WB (lang->has_complex_copy_ctor);
    6304       206798 :   WB (lang->has_complex_copy_assign);
    6305       206798 :   WB (lang->non_aggregate);
    6306              : 
    6307       206798 :   WB (lang->has_complex_dflt);
    6308       206798 :   WB (lang->has_list_ctor);
    6309       206798 :   WB (lang->non_std_layout);
    6310       206798 :   WB (lang->is_literal);
    6311       206798 :   WB (lang->lazy_move_ctor);
    6312       206798 :   WB (lang->lazy_move_assign);
    6313       206798 :   WB (lang->has_complex_move_ctor);
    6314       206798 :   WB (lang->has_complex_move_assign);
    6315              : 
    6316       206798 :   WB (lang->has_constexpr_ctor);
    6317       206798 :   WB (lang->unique_obj_representations);
    6318       206798 :   WB (lang->unique_obj_representations_set);
    6319       206798 :   gcc_checking_assert (!lang->erroneous);
    6320       206798 :   WB (lang->non_pod_aggregate);
    6321       206798 :   WB (lang->non_aggregate_pod);
    6322              : #undef WB
    6323       206798 : }
    6324              : 
    6325              : bool
    6326       151288 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6327              : {
    6328              : #define RB(X) ((X) = bits.b ())
    6329       151288 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6330              : 
    6331       151288 :   bits.bflush ();
    6332       151288 :   RB (lang->has_type_conversion);
    6333       151288 :   RB (lang->has_copy_ctor);
    6334       151288 :   RB (lang->has_default_ctor);
    6335       151288 :   RB (lang->const_needs_init);
    6336       151288 :   RB (lang->ref_needs_init);
    6337       151288 :   RB (lang->has_const_copy_assign);
    6338       151288 :   unsigned v;
    6339       151288 :   v = bits.b () << 0;
    6340       151288 :   v |= bits.b () << 1;
    6341       151288 :   lang->use_template = v;
    6342              : 
    6343       151288 :   RB (lang->has_mutable);
    6344       151288 :   RB (lang->com_interface);
    6345       151288 :   RB (lang->non_pod_class);
    6346       151288 :   RB (lang->nearly_empty_p);
    6347       151288 :   RB (lang->user_align);
    6348       151288 :   RB (lang->has_copy_assign);
    6349       151288 :   RB (lang->has_new);
    6350       151288 :   RB (lang->has_array_new);
    6351              : 
    6352       151288 :   v = bits.b () << 0;
    6353       151288 :   v |= bits.b () << 1;
    6354       151288 :   lang->gets_delete = v;
    6355       151288 :   RB (lang->interface_only);
    6356       151288 :   RB (lang->interface_unknown);
    6357       151288 :   RB (lang->contains_empty_class_p);
    6358       151288 :   RB (lang->anon_aggr);
    6359       151288 :   RB (lang->non_zero_init);
    6360       151288 :   RB (lang->empty_p);
    6361              : 
    6362       151288 :   RB (lang->vec_new_uses_cookie);
    6363       151288 :   RB (lang->declared_class);
    6364       151288 :   RB (lang->diamond_shaped);
    6365       151288 :   RB (lang->repeated_base);
    6366       151288 :   gcc_checking_assert (!lang->being_defined);
    6367       151288 :   gcc_checking_assert (!lang->debug_requested);
    6368       151288 :   RB (lang->fields_readonly);
    6369       151288 :   RB (lang->ptrmemfunc_flag);
    6370              : 
    6371       151288 :   RB (lang->lazy_default_ctor);
    6372       151288 :   RB (lang->lazy_copy_ctor);
    6373       151288 :   RB (lang->lazy_copy_assign);
    6374       151288 :   RB (lang->lazy_destructor);
    6375       151288 :   RB (lang->has_const_copy_ctor);
    6376       151288 :   RB (lang->has_complex_copy_ctor);
    6377       151288 :   RB (lang->has_complex_copy_assign);
    6378       151288 :   RB (lang->non_aggregate);
    6379              : 
    6380       151288 :   RB (lang->has_complex_dflt);
    6381       151288 :   RB (lang->has_list_ctor);
    6382       151288 :   RB (lang->non_std_layout);
    6383       151288 :   RB (lang->is_literal);
    6384       151288 :   RB (lang->lazy_move_ctor);
    6385       151288 :   RB (lang->lazy_move_assign);
    6386       151288 :   RB (lang->has_complex_move_ctor);
    6387       151288 :   RB (lang->has_complex_move_assign);
    6388              : 
    6389       151288 :   RB (lang->has_constexpr_ctor);
    6390       151288 :   RB (lang->unique_obj_representations);
    6391       151288 :   RB (lang->unique_obj_representations_set);
    6392       151288 :   gcc_checking_assert (!lang->erroneous);
    6393       151288 :   RB (lang->non_pod_aggregate);
    6394       151288 :   RB (lang->non_aggregate_pod);
    6395              : #undef RB
    6396       151288 :   return !get_overrun ();
    6397              : }
    6398              : 
    6399              : /* Read & write the core values and pointers.  */
    6400              : 
    6401              : void
    6402     51417255 : trees_out::core_vals (tree t)
    6403              : {
    6404              : #define WU(X) (u (X))
    6405              : #define WT(X) (tree_node (X))
    6406     51417255 :   tree_code code = TREE_CODE (t);
    6407              : 
    6408              :   /* First by shape of the tree.  */
    6409              : 
    6410     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6411              :     {
    6412              :       /* Write this early, for better log information.  */
    6413     11299492 :       WT (t->decl_minimal.name);
    6414     11299492 :       if (!DECL_TEMPLATE_PARM_P (t))
    6415      8730899 :         WT (t->decl_minimal.context);
    6416              : 
    6417     11299492 :       if (state)
    6418      9294117 :         state->write_location (*this, t->decl_minimal.locus);
    6419              : 
    6420     11299492 :       if (streaming_p ())
    6421      4304538 :         if (has_warning_spec (t))
    6422          891 :           u (get_warning_spec (t));
    6423              :     }
    6424              : 
    6425     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6426              :     {
    6427              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6428      2786764 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6429              : 
    6430              :       /* We only stream the main variant.  */
    6431      2786764 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6432              : 
    6433              :       /* Stream the name & context first, for better log information  */
    6434      2786764 :       WT (t->type_common.name);
    6435      2786764 :       WT (t->type_common.context);
    6436              : 
    6437              :       /* By construction we want to make sure we have the canonical
    6438              :          and main variants already in the type table, so emit them
    6439              :          now.  */
    6440      2786764 :       WT (t->type_common.main_variant);
    6441              : 
    6442      2786764 :       tree canonical = t->type_common.canonical;
    6443      2786764 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    6444              :         /* We do not want to wander into different templates.
    6445              :            Reconstructed on stream in.  */
    6446              :         canonical = t;
    6447      2786764 :       WT (canonical);
    6448              : 
    6449              :       /* type_common.next_variant is internally manipulated.  */
    6450              :       /* type_common.pointer_to, type_common.reference_to.  */
    6451              : 
    6452      2786764 :       if (streaming_p ())
    6453              :         {
    6454       757564 :           WU (t->type_common.precision);
    6455       757564 :           WU (t->type_common.contains_placeholder_bits);
    6456       757564 :           WU (t->type_common.mode);
    6457       757564 :           WU (t->type_common.align);
    6458              :         }
    6459              : 
    6460      2786764 :       if (!RECORD_OR_UNION_CODE_P (code))
    6461              :         {
    6462      2313896 :           WT (t->type_common.size);
    6463      2313896 :           WT (t->type_common.size_unit);
    6464              :         }
    6465      2786764 :       WT (t->type_common.attributes);
    6466              : 
    6467      2786764 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6468              :     }
    6469              : 
    6470     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6471              :     {
    6472     11299492 :       if (streaming_p ())
    6473              :         {
    6474      4304538 :           WU (t->decl_common.mode);
    6475      4304538 :           WU (t->decl_common.off_align);
    6476      4304538 :           WU (t->decl_common.align);
    6477              :         }
    6478              : 
    6479              :       /* For templates these hold instantiation (partial and/or
    6480              :          specialization) information.  */
    6481     11299492 :       if (code != TEMPLATE_DECL)
    6482              :         {
    6483     10443840 :           WT (t->decl_common.size);
    6484     10443840 :           WT (t->decl_common.size_unit);
    6485              :         }
    6486              : 
    6487     11299492 :       WT (t->decl_common.attributes);
    6488              :       // FIXME: Does this introduce cross-decl links?  For instance
    6489              :       // from instantiation to the template.  If so, we'll need more
    6490              :       // deduplication logic.  I think we'll need to walk the blocks
    6491              :       // of the owning function_decl's abstract origin in tandem, to
    6492              :       // generate the locating data needed?
    6493     11299492 :       WT (t->decl_common.abstract_origin);
    6494              :     }
    6495              : 
    6496     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6497              :     {
    6498      5342976 :       WT (t->decl_with_vis.assembler_name);
    6499      5342976 :       if (streaming_p ())
    6500      2025712 :         WU (t->decl_with_vis.visibility);
    6501              :     }
    6502              : 
    6503     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6504              :     {
    6505      2786764 :       if (code == ENUMERAL_TYPE)
    6506              :         {
    6507              :           /* These fields get set even for opaque enums that lack a
    6508              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6509              :              We stream TYPE_VALUES as part of the definition.  */
    6510         9426 :           WT (t->type_non_common.maxval);
    6511         9426 :           WT (t->type_non_common.minval);
    6512              :         }
    6513              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6514              :          things.  */
    6515      2777338 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6516              :         {
    6517              :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6518              :           // Perhaps it should just be doing them directly?
    6519      2304470 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6520              :                                || code == TEMPLATE_TEMPLATE_PARM
    6521              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6522      2304470 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6523      2304470 :           WT (t->type_non_common.values);
    6524      2304470 :           WT (t->type_non_common.maxval);
    6525      2304470 :           WT (t->type_non_common.minval);
    6526              :         }
    6527              : 
    6528      2786764 :       WT (t->type_non_common.lang_1);
    6529              :     }
    6530              : 
    6531     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6532              :     {
    6533     15623789 :       if (state)
    6534     15310495 :         state->write_location (*this, t->exp.locus);
    6535              : 
    6536     15623789 :       if (streaming_p ())
    6537      7582505 :         if (has_warning_spec (t))
    6538       602158 :           u (get_warning_spec (t));
    6539              : 
    6540     15623789 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6541     15623789 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6542     15623789 :                         : TREE_OPERAND_LENGTH (t));
    6543     15623789 :       unsigned ix = unsigned (vl);
    6544     15623789 :       if (code == REQUIRES_EXPR)
    6545              :         {
    6546              :           /* The first operand of a REQUIRES_EXPR is a tree chain
    6547              :              of PARM_DECLs.  We need to stream this separately as
    6548              :              otherwise we would only stream the first one.  */
    6549        22985 :           chained_decls (REQUIRES_EXPR_PARMS (t));
    6550        22985 :           ++ix;
    6551              :         }
    6552     43042465 :       for (; ix != limit; ix++)
    6553     27418676 :         WT (TREE_OPERAND (t, ix));
    6554              :     }
    6555              :   else
    6556              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6557     35793466 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6558              :                          && TREE_CODE_CLASS (code) != tcc_binary
    6559              :                          && TREE_CODE_CLASS (code) != tcc_unary
    6560              :                          && TREE_CODE_CLASS (code) != tcc_reference
    6561              :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6562              :                          && TREE_CODE_CLASS (code) != tcc_statement
    6563              :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6564              : 
    6565              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6566              :      correspondance. */
    6567     51417255 :   switch (code)
    6568              :     {
    6569              :     default:
    6570              :       break;
    6571              : 
    6572            0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6573            0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6574              :                                    outermost class.  */
    6575            0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6576            0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6577              :                                    table.  */
    6578            0 :     case SSA_NAME:
    6579            0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6580              :                                    global_tree.  */
    6581            0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6582            0 :       gcc_unreachable (); /* Should never meet.  */
    6583              : 
    6584              :       /* Constants.  */
    6585           18 :     case COMPLEX_CST:
    6586           18 :       WT (TREE_REALPART (t));
    6587           18 :       WT (TREE_IMAGPART (t));
    6588           18 :       break;
    6589              : 
    6590            0 :     case FIXED_CST:
    6591            0 :       gcc_unreachable (); /* Not supported in C++.  */
    6592              : 
    6593      4733109 :     case INTEGER_CST:
    6594      4733109 :       if (streaming_p ())
    6595              :         {
    6596       802155 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6597      1607117 :           for (unsigned ix = 0; ix != num; ix++)
    6598       804962 :             wu (TREE_INT_CST_ELT (t, ix));
    6599              :         }
    6600              :       break;
    6601              : 
    6602            0 :     case POLY_INT_CST:
    6603            0 :       if (streaming_p ())
    6604            0 :         for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    6605            0 :           WT (POLY_INT_CST_COEFF (t, ix));
    6606              :       break;
    6607              : 
    6608        46188 :     case REAL_CST:
    6609        46188 :       if (streaming_p ())
    6610        23048 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6611              :       break;
    6612              : 
    6613              :     case STRING_CST:
    6614              :       /* Streamed during start.  */
    6615              :       break;
    6616              : 
    6617           36 :     case RAW_DATA_CST:
    6618           36 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    6619              :         break; /* Streamed as STRING_CST during start.  */
    6620           24 :       WT (RAW_DATA_OWNER (t));
    6621           24 :       if (streaming_p ())
    6622              :         {
    6623           12 :           if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
    6624            6 :             z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
    6625            6 :           else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
    6626            6 :             z (RAW_DATA_POINTER (t)
    6627            6 :                - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
    6628              :           else
    6629            0 :             gcc_unreachable ();
    6630              :         }
    6631              :       break;
    6632              : 
    6633           36 :     case VECTOR_CST:
    6634          102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6635           66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6636              :       break;
    6637              : 
    6638              :       /* Decls.  */
    6639       622739 :     case VAR_DECL:
    6640       622739 :       if (DECL_CONTEXT (t)
    6641       622739 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6642              :         {
    6643       131439 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6644           18 :             WT (DECL_VALUE_EXPR (t));
    6645              :           break;
    6646              :         }
    6647              :       /* FALLTHROUGH  */
    6648              : 
    6649      5151075 :     case RESULT_DECL:
    6650      5151075 :     case PARM_DECL:
    6651      5151075 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6652        49284 :         WT (DECL_VALUE_EXPR (t));
    6653              :       /* FALLTHROUGH  */
    6654              : 
    6655      5368486 :     case CONST_DECL:
    6656      5368486 :     case IMPORTED_DECL:
    6657      5368486 :       WT (t->decl_common.initial);
    6658      5368486 :       break;
    6659              : 
    6660       158409 :     case FIELD_DECL:
    6661       158409 :       WT (t->field_decl.offset);
    6662       158409 :       WT (t->field_decl.bit_field_type);
    6663       158409 :       {
    6664       158409 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6665       158409 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6666       158409 :       }
    6667       158409 :       WT (t->field_decl.bit_offset);
    6668       158409 :       WT (t->field_decl.fcontext);
    6669       158409 :       WT (t->decl_common.initial);
    6670       158409 :       break;
    6671              : 
    6672        50137 :     case LABEL_DECL:
    6673        50137 :       if (streaming_p ())
    6674              :         {
    6675        25067 :           WU (t->label_decl.label_decl_uid);
    6676        25067 :           WU (t->label_decl.eh_landing_pad_nr);
    6677              :         }
    6678              :       break;
    6679              : 
    6680      1196242 :     case FUNCTION_DECL:
    6681      1196242 :       if (streaming_p ())
    6682              :         {
    6683              :           /* Builtins can be streamed by value when a header declares
    6684              :              them.  */
    6685       598014 :           WU (DECL_BUILT_IN_CLASS (t));
    6686       598014 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6687        11656 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6688              :         }
    6689              : 
    6690      1196242 :       WT (t->function_decl.personality);
    6691              :       /* Rather than streaming target/optimize nodes, we should reconstruct
    6692              :          them on stream-in from any attributes applied to the function.  */
    6693      1196242 :       if (streaming_p () && t->function_decl.function_specific_target)
    6694            0 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6695              :                     "%<target%> attribute currently unsupported in modules");
    6696      1196242 :       if (streaming_p () && t->function_decl.function_specific_optimization)
    6697            3 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6698              :                     "%<optimize%> attribute currently unsupported in modules");
    6699      1196242 :       WT (t->function_decl.vindex);
    6700              : 
    6701      1196242 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6702         8070 :         WT (lookup_explicit_specifier (t));
    6703              :       break;
    6704              : 
    6705       142251 :     case USING_DECL:
    6706              :       /* USING_DECL_DECLS  */
    6707       142251 :       WT (t->decl_common.initial);
    6708              :       /* FALLTHROUGH  */
    6709              : 
    6710      3523508 :     case TYPE_DECL:
    6711              :       /* USING_DECL: USING_DECL_SCOPE  */
    6712              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6713      3523508 :       WT (t->decl_non_common.result);
    6714      3523508 :       break;
    6715              : 
    6716              :       /* Miscellaneous common nodes.  */
    6717       783152 :     case BLOCK:
    6718       783152 :       if (state)
    6719              :         {
    6720       783152 :           state->write_location (*this, t->block.locus);
    6721       783152 :           state->write_location (*this, t->block.end_locus);
    6722              :         }
    6723              : 
    6724              :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6725              :          streamed by value.  */
    6726      1196076 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6727              :         {
    6728       412924 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6729       412924 :               && DECL_LOCAL_DECL_P (decls))
    6730              :             {
    6731              :               /* Make sure this is the first encounter, and mark for
    6732              :                  walk-by-value.  */
    6733          308 :               gcc_checking_assert (!TREE_VISITED (decls)
    6734              :                                    && !DECL_TEMPLATE_INFO (decls));
    6735          308 :               mark_by_value (decls);
    6736              :             }
    6737       412924 :           tree_node (decls);
    6738              :         }
    6739       783152 :       tree_node (NULL_TREE);
    6740              : 
    6741              :       /* nonlocalized_vars is a middle-end thing.  */
    6742       783152 :       WT (t->block.subblocks);
    6743       783152 :       WT (t->block.supercontext);
    6744              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6745       783152 :       WT (t->block.abstract_origin);
    6746              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6747       783152 :       WT (t->block.chain);
    6748              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6749              :          things.  */
    6750       783152 :       break;
    6751              : 
    6752      1550324 :     case CALL_EXPR:
    6753      1550324 :       if (streaming_p ())
    6754       741642 :         WU (t->base.u.ifn);
    6755              :       break;
    6756              : 
    6757              :     case CONSTRUCTOR:
    6758              :       // This must be streamed /after/ we've streamed the type,
    6759              :       // because it can directly refer to elements of the type. Eg,
    6760              :       // FIELD_DECLs of a RECORD_TYPE.
    6761              :       break;
    6762              : 
    6763           36 :     case OMP_CLAUSE:
    6764           36 :       {
    6765              :         /* The ompcode is serialized in start.  */
    6766           36 :         if (streaming_p ())
    6767           18 :           WU (t->omp_clause.subcode.map_kind);
    6768           36 :         if (state)
    6769           36 :           state->write_location (*this, t->omp_clause.locus);
    6770              : 
    6771           36 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6772          120 :         for (unsigned ix = 0; ix != len; ix++)
    6773           84 :           WT (t->omp_clause.ops[ix]);
    6774              :       }
    6775              :       break;
    6776              : 
    6777       648195 :     case STATEMENT_LIST:
    6778      2699100 :       for (tree stmt : tsi_range (t))
    6779      2050905 :         if (stmt)
    6780      2050905 :           WT (stmt);
    6781       648195 :       WT (NULL_TREE);
    6782       648195 :       break;
    6783              : 
    6784            0 :     case OPTIMIZATION_NODE:
    6785            0 :     case TARGET_OPTION_NODE:
    6786              :       // FIXME: Our representation for these two nodes is a cache of
    6787              :       // the resulting set of options.  Not a record of the options
    6788              :       // that got changed by a particular attribute or pragma.  Instead
    6789              :       // of recording that, we probably should just rebuild the options
    6790              :       // on stream-in from the function attributes.  This could introduce
    6791              :       // strangeness if the importer has some incompatible set of flags
    6792              :       // but we currently assume users "know what they're doing" in such
    6793              :       // a case anyway.
    6794            0 :       gcc_unreachable ();
    6795       268938 :       break;
    6796              : 
    6797       268938 :     case TREE_BINFO:
    6798       268938 :       {
    6799       268938 :         WT (t->binfo.common.chain);
    6800       268938 :         WT (t->binfo.offset);
    6801       268938 :         WT (t->binfo.inheritance);
    6802       268938 :         WT (t->binfo.vptr_field);
    6803              : 
    6804       268938 :         WT (t->binfo.vtable);
    6805       268938 :         WT (t->binfo.virtuals);
    6806       268938 :         WT (t->binfo.vtt_subvtt);
    6807       268938 :         WT (t->binfo.vtt_vptr);
    6808              : 
    6809       268938 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6810       268938 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6811       343588 :         for (unsigned ix = 0; ix != num; ix++)
    6812        74650 :           WT (BINFO_BASE_BINFO (t, ix));
    6813              :       }
    6814              :       break;
    6815              : 
    6816      3907330 :     case TREE_LIST:
    6817      3907330 :       WT (t->list.purpose);
    6818      3907330 :       WT (t->list.value);
    6819      3907330 :       WT (t->list.common.chain);
    6820      3907330 :       break;
    6821              : 
    6822      3333736 :     case TREE_VEC:
    6823      9257240 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6824      5923504 :         WT (TREE_VEC_ELT (t, ix));
    6825              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6826      3333736 :       gcc_checking_assert (!t->type_common.common.chain
    6827              :                            || (TREE_CODE (t->type_common.common.chain)
    6828              :                                == INTEGER_CST));
    6829      3333736 :       WT (t->type_common.common.chain);
    6830      3333736 :       break;
    6831              : 
    6832              :       /* C++-specific nodes ...  */
    6833       274345 :     case BASELINK:
    6834       274345 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6835       274345 :       WT (((lang_tree_node *)t)->baselink.functions);
    6836       274345 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6837       274345 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6838       274345 :       break;
    6839              : 
    6840       121381 :     case CONSTRAINT_INFO:
    6841       121381 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6842       121381 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6843       121381 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6844       121381 :       break;
    6845              : 
    6846        19332 :     case DEFERRED_NOEXCEPT:
    6847        19332 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6848        19332 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6849        19332 :       break;
    6850              : 
    6851        18896 :     case LAMBDA_EXPR:
    6852        18896 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6853        18896 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6854        18896 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6855        18896 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6856        18896 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6857              :       /* pending_proxies is a parse-time thing.  */
    6858        18896 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6859        18896 :       if (state)
    6860        18893 :         state->write_location
    6861        18893 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6862        18896 :       if (streaming_p ())
    6863              :         {
    6864         6342 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6865         6342 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6866         6342 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6867              :         }
    6868              :       break;
    6869              : 
    6870      2829101 :     case OVERLOAD:
    6871      2829101 :       WT (((lang_tree_node *)t)->overload.function);
    6872      2829101 :       WT (t->common.chain);
    6873      2829101 :       break;
    6874              : 
    6875           33 :     case PTRMEM_CST:
    6876           33 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6877           33 :       if (state)
    6878           24 :         state->write_location (*this, ((lang_tree_node *)t)->ptrmem.locus);
    6879              :       break;
    6880              : 
    6881        21159 :     case STATIC_ASSERT:
    6882        21159 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6883        21159 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6884        21159 :       if (state)
    6885        21159 :         state->write_location
    6886        21159 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6887              :       break;
    6888              : 
    6889       855652 :     case TEMPLATE_DECL:
    6890              :       /* Streamed with the template_decl node itself.  */
    6891       855652 :       gcc_checking_assert
    6892              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6893       855652 :       gcc_checking_assert
    6894              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6895       855652 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6896        16392 :         WT (DECL_CHAIN (t));
    6897              :       break;
    6898              : 
    6899      1938560 :     case TEMPLATE_INFO:
    6900      1938560 :       {
    6901      1938560 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6902      1938560 :         WT (((lang_tree_node *)t)->template_info.args);
    6903      1938560 :         WT (((lang_tree_node *)t)->template_info.partial);
    6904              : 
    6905      1938560 :         const auto *ac = (((lang_tree_node *)t)
    6906              :                           ->template_info.deferred_access_checks);
    6907      1938560 :         unsigned len = vec_safe_length (ac);
    6908      1938560 :         if (streaming_p ())
    6909       967209 :           u (len);
    6910      1938560 :         if (len)
    6911              :           {
    6912            0 :             for (unsigned ix = 0; ix != len; ix++)
    6913              :               {
    6914            0 :                 const auto &m = (*ac)[ix];
    6915            0 :                 WT (m.binfo);
    6916            0 :                 WT (m.decl);
    6917            0 :                 WT (m.diag_decl);
    6918            0 :                 if (state)
    6919            0 :                   state->write_location (*this, m.loc);
    6920              :               }
    6921              :           }
    6922              :       }
    6923              :       break;
    6924              : 
    6925      2475061 :     case TEMPLATE_PARM_INDEX:
    6926      2475061 :       if (streaming_p ())
    6927              :         {
    6928       548301 :           WU (((lang_tree_node *)t)->tpi.index);
    6929       548301 :           WU (((lang_tree_node *)t)->tpi.level);
    6930       548301 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6931              :         }
    6932      2475061 :       WT (((lang_tree_node *)t)->tpi.decl);
    6933              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6934              :          cache, do not stream.  */
    6935      2475061 :       break;
    6936              : 
    6937        29252 :     case TRAIT_EXPR:
    6938        29252 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6939        29252 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6940        29252 :       if (state)
    6941        22867 :         state->write_location
    6942        22867 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6943        29252 :       if (streaming_p ())
    6944        11267 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6945              :       break;
    6946              : 
    6947            4 :     case TU_LOCAL_ENTITY:
    6948            4 :       WT (((lang_tree_node *)t)->tu_local_entity.name);
    6949            4 :       if (state)
    6950            4 :         state->write_location
    6951            4 :           (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
    6952              :       break;
    6953              :     }
    6954              : 
    6955     51417255 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6956              :     {
    6957              :       /* We want to stream the type of a expression-like nodes /after/
    6958              :          we've streamed the operands.  The type often contains (bits
    6959              :          of the) types of the operands, and with things like decltype
    6960              :          and noexcept in play, we really want to stream the decls
    6961              :          defining the type before we try and stream the type on its
    6962              :          own.  Otherwise we can find ourselves trying to read in a
    6963              :          decl, when we're already partially reading in a component of
    6964              :          its type.  And that's bad.  */
    6965     48533667 :       tree type = t->typed.type;
    6966     48533667 :       unsigned prec = 0;
    6967              : 
    6968     48533667 :       switch (code)
    6969              :         {
    6970              :         default:
    6971              :           break;
    6972              : 
    6973              :         case TEMPLATE_DECL:
    6974              :           /* We fill in the template's type separately.  */
    6975     48533667 :           type = NULL_TREE;
    6976              :           break;
    6977              : 
    6978      3381257 :         case TYPE_DECL:
    6979      3381257 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6980              :             /* This is a typedef.  We set its type separately.  */
    6981              :             type = NULL_TREE;
    6982              :           break;
    6983              : 
    6984         9426 :         case ENUMERAL_TYPE:
    6985         9426 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6986              :             {
    6987              :               /* Type is a restricted range integer type derived from the
    6988              :                  integer_types.  Find the right one.  */
    6989         5570 :               prec = TYPE_PRECISION (type);
    6990         5570 :               tree name = DECL_NAME (TYPE_NAME (type));
    6991              : 
    6992        72782 :               for (unsigned itk = itk_none; itk--;)
    6993        72782 :                 if (integer_types[itk]
    6994        72782 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    6995              :                   {
    6996              :                     type = integer_types[itk];
    6997              :                     break;
    6998              :                   }
    6999         5570 :               gcc_assert (type != t->typed.type);
    7000              :             }
    7001              :           break;
    7002              :         }
    7003              : 
    7004     48533667 :       WT (type);
    7005     48533667 :       if (prec && streaming_p ())
    7006         2783 :         WU (prec);
    7007              :     }
    7008              : 
    7009     51417255 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7010              :     {
    7011       132459 :       unsigned len = vec_safe_length (t->constructor.elts);
    7012       132459 :       if (streaming_p ())
    7013        65234 :         WU (len);
    7014       132459 :       if (len)
    7015       485648 :         for (unsigned ix = 0; ix != len; ix++)
    7016              :           {
    7017       415470 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7018              : 
    7019       415470 :             WT (elt.index);
    7020       415470 :             WT (elt.value);
    7021              :           }
    7022              :     }
    7023              : 
    7024              : #undef WT
    7025              : #undef WU
    7026     51417255 : }
    7027              : 
    7028              : // Streaming in a reference to a decl can cause that decl to be
    7029              : // TREE_USED, which is the mark_used behaviour we need most of the
    7030              : // time.  The trees_in::unused can be incremented to inhibit this,
    7031              : // which is at least needed for vtables.
    7032              : 
    7033              : bool
    7034     15232223 : trees_in::core_vals (tree t)
    7035              : {
    7036              : #define RU(X) ((X) = u ())
    7037              : #define RUC(T,X) ((X) = T (u ()))
    7038              : #define RT(X) ((X) = tree_node ())
    7039              : #define RTU(X) ((X) = tree_node (true))
    7040     15232223 :   tree_code code = TREE_CODE (t);
    7041              : 
    7042              :   /* First by tree shape.  */
    7043     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7044              :     {
    7045      3260930 :       RT (t->decl_minimal.name);
    7046      3260930 :       if (!DECL_TEMPLATE_PARM_P (t))
    7047      2839552 :         RT (t->decl_minimal.context);
    7048              : 
    7049              :       /* Don't zap the locus just yet, we don't record it correctly
    7050              :          and thus lose all location information.  */
    7051      3260930 :       t->decl_minimal.locus = state->read_location (*this);
    7052      3260930 :       if (has_warning_spec (t))
    7053          643 :         put_warning_spec (t, u ());
    7054              :     }
    7055              : 
    7056     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7057              :     {
    7058       553286 :       RT (t->type_common.name);
    7059       553286 :       RT (t->type_common.context);
    7060              : 
    7061       553286 :       RT (t->type_common.main_variant);
    7062       553286 :       RT (t->type_common.canonical);
    7063              : 
    7064              :       /* type_common.next_variant is internally manipulated.  */
    7065              :       /* type_common.pointer_to, type_common.reference_to.  */
    7066              : 
    7067       553286 :       RU (t->type_common.precision);
    7068       553286 :       RU (t->type_common.contains_placeholder_bits);
    7069       553286 :       RUC (machine_mode, t->type_common.mode);
    7070       553286 :       RU (t->type_common.align);
    7071              : 
    7072       553286 :       if (!RECORD_OR_UNION_CODE_P (code))
    7073              :         {
    7074       382129 :           RT (t->type_common.size);
    7075       382129 :           RT (t->type_common.size_unit);
    7076              :         }
    7077       553286 :       RT (t->type_common.attributes);
    7078              : 
    7079       553286 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7080              :     }
    7081              : 
    7082     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7083              :     {
    7084      3260930 :       RUC (machine_mode, t->decl_common.mode);
    7085      3260930 :       RU (t->decl_common.off_align);
    7086      3260930 :       RU (t->decl_common.align);
    7087              : 
    7088      3260930 :       if (code != TEMPLATE_DECL)
    7089              :         {
    7090      2934271 :           RT (t->decl_common.size);
    7091      2934271 :           RT (t->decl_common.size_unit);
    7092              :         }
    7093              : 
    7094      3260930 :       RT (t->decl_common.attributes);
    7095      3260930 :       RT (t->decl_common.abstract_origin);
    7096              :     }
    7097              : 
    7098     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7099              :     {
    7100      1500753 :       RT (t->decl_with_vis.assembler_name);
    7101      1500753 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7102              :     }
    7103              : 
    7104     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7105              :     {
    7106       553286 :       if (code == ENUMERAL_TYPE)
    7107              :         {
    7108              :           /* These fields get set even for opaque enums that lack a
    7109              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    7110              :              We stream TYPE_VALUES as part of the definition.  */
    7111         3168 :           RT (t->type_non_common.maxval);
    7112         3168 :           RT (t->type_non_common.minval);
    7113              :         }
    7114              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7115              :          things.  */
    7116       550118 :       else if (!RECORD_OR_UNION_CODE_P (code))
    7117              :         {
    7118              :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    7119              :              is a type that doesn't have any.  */
    7120       378961 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7121       378961 :           RT (t->type_non_common.values);
    7122       378961 :           RT (t->type_non_common.maxval);
    7123       378961 :           RT (t->type_non_common.minval);
    7124              :         }
    7125              : 
    7126       553286 :       RT (t->type_non_common.lang_1);
    7127              :     }
    7128              : 
    7129     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7130              :     {
    7131      5996594 :       t->exp.locus = state->read_location (*this);
    7132      5996594 :       if (has_warning_spec (t))
    7133       473751 :         put_warning_spec (t, u ());
    7134              : 
    7135      5996594 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7136      5996594 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7137      5996594 :                         : TREE_OPERAND_LENGTH (t));
    7138      5996594 :       unsigned ix = unsigned (vl);
    7139      5996594 :       if (code == REQUIRES_EXPR)
    7140              :         {
    7141         7468 :           REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7142         7468 :           ++ix;
    7143              :         }
    7144     16404921 :       for (; ix != limit; ix++)
    7145     10408327 :         RTU (TREE_OPERAND (t, ix));
    7146              :     }
    7147              : 
    7148              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7149              :      correspondance. */
    7150     15232223 :   switch (code)
    7151              :     {
    7152              :     default:
    7153              :       break;
    7154              : 
    7155              :     case ARGUMENT_PACK_SELECT:
    7156              :     case DEFERRED_PARSE:
    7157              :     case IDENTIFIER_NODE:
    7158              :     case BINDING_VECTOR:
    7159              :     case SSA_NAME:
    7160              :     case TRANSLATION_UNIT_DECL:
    7161              :     case USERDEF_LITERAL:
    7162              :       return false; /* Should never meet.  */
    7163              : 
    7164              :       /* Constants.  */
    7165            9 :     case COMPLEX_CST:
    7166            9 :       RT (TREE_REALPART (t));
    7167            9 :       RT (TREE_IMAGPART (t));
    7168            9 :       break;
    7169              : 
    7170              :     case FIXED_CST:
    7171              :       /* Not suported in C++.  */
    7172              :       return false;
    7173              : 
    7174       594259 :     case INTEGER_CST:
    7175       594259 :       {
    7176       594259 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7177      1190632 :         for (unsigned ix = 0; ix != num; ix++)
    7178       596373 :           TREE_INT_CST_ELT (t, ix) = wu ();
    7179              :       }
    7180              :       break;
    7181              : 
    7182              :     case POLY_INT_CST:
    7183            0 :       for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    7184            0 :         RT (POLY_INT_CST_COEFF (t, ix));
    7185              :       break;
    7186              : 
    7187        16774 :     case REAL_CST:
    7188        16774 :       if (const void *bytes = buf (sizeof (real_value)))
    7189        16774 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    7190              :       break;
    7191              : 
    7192              :     case STRING_CST:
    7193              :       /* Streamed during start.  */
    7194              :       break;
    7195              : 
    7196            6 :     case RAW_DATA_CST:
    7197            6 :       RT (RAW_DATA_OWNER (t));
    7198            6 :       gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
    7199              :                   && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
    7200            6 :       RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
    7201            6 :       break;
    7202              : 
    7203           24 :     case VECTOR_CST:
    7204           63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    7205           39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    7206              :       break;
    7207              : 
    7208              :       /* Decls.  */
    7209       220649 :     case VAR_DECL:
    7210       220649 :       if (DECL_CONTEXT (t)
    7211       220649 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7212              :         {
    7213        42007 :           if (DECL_HAS_VALUE_EXPR_P (t))
    7214              :             {
    7215            9 :               tree val = tree_node ();
    7216            9 :               SET_DECL_VALUE_EXPR (t, val);
    7217              :             }
    7218              :           break;
    7219              :         }
    7220              :       /* FALLTHROUGH  */
    7221              : 
    7222      1487465 :     case RESULT_DECL:
    7223      1487465 :     case PARM_DECL:
    7224      1487465 :       if (DECL_HAS_VALUE_EXPR_P (t))
    7225              :         {
    7226              :           /* The DECL_VALUE hash table is a cache, thus if we're
    7227              :              reading a duplicate (which we end up discarding), the
    7228              :              value expr will also be cleaned up at the next gc.  */
    7229        13748 :           tree val = tree_node ();
    7230        13748 :           SET_DECL_VALUE_EXPR (t, val);
    7231              :         }
    7232              :       /* FALLTHROUGH  */
    7233              : 
    7234      1532514 :     case CONST_DECL:
    7235      1532514 :     case IMPORTED_DECL:
    7236      1532514 :       RT (t->decl_common.initial);
    7237      1532514 :       break;
    7238              : 
    7239        56230 :     case FIELD_DECL:
    7240        56230 :       RT (t->field_decl.offset);
    7241        56230 :       RT (t->field_decl.bit_field_type);
    7242        56230 :       RT (t->field_decl.qualifier);
    7243        56230 :       RT (t->field_decl.bit_offset);
    7244        56230 :       RT (t->field_decl.fcontext);
    7245        56230 :       RT (t->decl_common.initial);
    7246        56230 :       break;
    7247              : 
    7248        17288 :     case LABEL_DECL:
    7249        17288 :       RU (t->label_decl.label_decl_uid);
    7250        17288 :       RU (t->label_decl.eh_landing_pad_nr);
    7251        17288 :       break;
    7252              : 
    7253       462193 :     case FUNCTION_DECL:
    7254       462193 :       {
    7255       462193 :         unsigned bltin = u ();
    7256       462193 :         t->function_decl.built_in_class = built_in_class (bltin);
    7257       462193 :         if (bltin != NOT_BUILT_IN)
    7258              :           {
    7259         9540 :             bltin = u ();
    7260         9540 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7261              :           }
    7262              : 
    7263       462193 :         RT (t->function_decl.personality);
    7264              :         /* These properties are not streamed, and should be reconstructed
    7265              :            from any function attributes.  */
    7266              :         // t->function_decl.function_specific_target);
    7267              :         // t->function_decl.function_specific_optimization);
    7268       462193 :         RT (t->function_decl.vindex);
    7269              : 
    7270       462193 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7271              :           {
    7272         3722 :             tree spec;
    7273         3722 :             RT (spec);
    7274         3722 :             store_explicit_specifier (t, spec);
    7275              :           }
    7276              :       }
    7277              :       break;
    7278              : 
    7279        51294 :     case USING_DECL:
    7280              :       /* USING_DECL_DECLS  */
    7281        51294 :       RT (t->decl_common.initial);
    7282              :       /* FALLTHROUGH  */
    7283              : 
    7284       817720 :     case TYPE_DECL:
    7285              :       /* USING_DECL: USING_DECL_SCOPE  */
    7286              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7287       817720 :       RT (t->decl_non_common.result);
    7288       817720 :       break;
    7289              : 
    7290              :       /* Miscellaneous common nodes.  */
    7291       307792 :     case BLOCK:
    7292       307792 :       t->block.locus = state->read_location (*this);
    7293       307792 :       t->block.end_locus = state->read_location (*this);
    7294              : 
    7295       307792 :       for (tree *chain = &t->block.vars;;)
    7296       463976 :         if (tree decl = tree_node ())
    7297              :           {
    7298              :             /* For a deduplicated local type or enumerator, chain the
    7299              :                duplicate decl instead of the canonical in-TU decl.  Seeing
    7300              :                a duplicate here means the containing function whose body
    7301              :                we're streaming in is a duplicate too, so we'll end up
    7302              :                discarding this BLOCK (and the rest of the duplicate function
    7303              :                body) anyway.  */
    7304       156184 :             decl = maybe_duplicate (decl);
    7305              : 
    7306       156184 :             if (!DECL_P (decl))
    7307              :               {
    7308            0 :                 set_overrun ();
    7309            0 :                 break;
    7310              :               }
    7311              : 
    7312              :             /* If DECL_CHAIN is already set then this was a backreference to a
    7313              :                local type or enumerator from a previous read (PR c++/114630).
    7314              :                Let's copy the node so we can keep building the chain for ODR
    7315              :                checking later.  */
    7316       156184 :             if (DECL_CHAIN (decl))
    7317              :               {
    7318           12 :                 gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    7319              :                                      && find_duplicate (DECL_CONTEXT (decl)));
    7320            6 :                 decl = copy_decl (decl);
    7321              :               }
    7322              : 
    7323       156184 :             *chain = decl;
    7324       156184 :             chain = &DECL_CHAIN (decl);
    7325              :           }
    7326              :         else
    7327       156184 :           break;
    7328              : 
    7329              :       /* nonlocalized_vars is middle-end.  */
    7330       307792 :       RT (t->block.subblocks);
    7331       307792 :       RT (t->block.supercontext);
    7332       307792 :       RT (t->block.abstract_origin);
    7333              :       /* fragment_origin, fragment_chain are middle-end.  */
    7334       307792 :       RT (t->block.chain);
    7335              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7336              :          things.  */
    7337       307792 :       break;
    7338              : 
    7339       579446 :     case CALL_EXPR:
    7340       579446 :       RUC (internal_fn, t->base.u.ifn);
    7341       579446 :       break;
    7342              : 
    7343              :     case CONSTRUCTOR:
    7344              :       // Streamed after the node's type.
    7345              :       break;
    7346              : 
    7347           18 :     case OMP_CLAUSE:
    7348           18 :       {
    7349           18 :         RU (t->omp_clause.subcode.map_kind);
    7350           18 :         t->omp_clause.locus = state->read_location (*this);
    7351              : 
    7352           18 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    7353           66 :         for (unsigned ix = 0; ix != len; ix++)
    7354           48 :           RT (t->omp_clause.ops[ix]);
    7355              :       }
    7356              :       break;
    7357              : 
    7358       276036 :     case STATEMENT_LIST:
    7359       276036 :       {
    7360       276036 :         tree_stmt_iterator iter = tsi_start (t);
    7361      1146169 :         for (tree stmt; RT (stmt);)
    7362              :           {
    7363       870133 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7364       168242 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7365            0 :               continue;
    7366       870133 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7367              :           }
    7368              :       }
    7369       276036 :       break;
    7370              : 
    7371            0 :     case OPTIMIZATION_NODE:
    7372            0 :     case TARGET_OPTION_NODE:
    7373              :       /* Not implemented, see trees_out::core_vals.  */
    7374            0 :       gcc_unreachable ();
    7375        90589 :       break;
    7376              : 
    7377        90589 :     case TREE_BINFO:
    7378        90589 :       RT (t->binfo.common.chain);
    7379        90589 :       RT (t->binfo.offset);
    7380        90589 :       RT (t->binfo.inheritance);
    7381        90589 :       RT (t->binfo.vptr_field);
    7382              : 
    7383              :       /* Do not mark the vtables as USED in the address expressions
    7384              :          here.  */
    7385        90589 :       unused++;
    7386        90589 :       RT (t->binfo.vtable);
    7387        90589 :       RT (t->binfo.virtuals);
    7388        90589 :       RT (t->binfo.vtt_subvtt);
    7389        90589 :       RT (t->binfo.vtt_vptr);
    7390        90589 :       unused--;
    7391              : 
    7392        90589 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7393        90589 :       if (!get_overrun ())
    7394              :         {
    7395        90589 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7396       115346 :           for (unsigned ix = 0; ix != num; ix++)
    7397        24757 :             BINFO_BASE_APPEND (t, tree_node ());
    7398              :         }
    7399              :       break;
    7400              : 
    7401      1313807 :     case TREE_LIST:
    7402      1313807 :       RT (t->list.purpose);
    7403      1313807 :       RT (t->list.value);
    7404      1313807 :       RT (t->list.common.chain);
    7405      1313807 :       break;
    7406              : 
    7407       840643 :     case TREE_VEC:
    7408      2263638 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7409      1422995 :         RT (TREE_VEC_ELT (t, ix));
    7410       840643 :       RT (t->type_common.common.chain);
    7411       840643 :       break;
    7412              : 
    7413              :       /* C++-specific nodes ...  */
    7414       106730 :     case BASELINK:
    7415       106730 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7416       106730 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7417       106730 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7418       106730 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7419       106730 :       break;
    7420              : 
    7421        43900 :     case CONSTRAINT_INFO:
    7422        43900 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7423        43900 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7424        43900 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7425        43900 :       break;
    7426              : 
    7427         7446 :     case DEFERRED_NOEXCEPT:
    7428         7446 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7429         7446 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7430         7446 :       break;
    7431              : 
    7432         4074 :     case LAMBDA_EXPR:
    7433         4074 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7434         4074 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7435         4074 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7436         4074 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7437         4074 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7438              :       /* lambda_expression.pending_proxies is NULL  */
    7439         4074 :       ((lang_tree_node *)t)->lambda_expression.locus
    7440         4074 :         = state->read_location (*this);
    7441         4074 :       RUC (cp_lambda_default_capture_mode_type,
    7442              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7443         4074 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7444         4074 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7445         4074 :       break;
    7446              : 
    7447       568765 :     case OVERLOAD:
    7448       568765 :       RT (((lang_tree_node *)t)->overload.function);
    7449       568765 :       RT (t->common.chain);
    7450       568765 :       break;
    7451              : 
    7452           12 :     case PTRMEM_CST:
    7453           12 :       RTU (((lang_tree_node *)t)->ptrmem.member);
    7454           12 :       ((lang_tree_node *)t)->ptrmem.locus = state->read_location (*this);
    7455           12 :       break;
    7456              : 
    7457         8011 :     case STATIC_ASSERT:
    7458         8011 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7459         8011 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7460         8011 :       ((lang_tree_node *)t)->static_assertion.location
    7461         8011 :         = state->read_location (*this);
    7462         8011 :       break;
    7463              : 
    7464       326659 :     case TEMPLATE_DECL:
    7465              :       /* Streamed when reading the raw template decl itself.  */
    7466       326659 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7467       326659 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7468       326659 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7469         7078 :         RT (DECL_CHAIN (t));
    7470              :       break;
    7471              : 
    7472       727465 :     case TEMPLATE_INFO:
    7473       727465 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7474       727465 :       RT (((lang_tree_node *)t)->template_info.args);
    7475       727465 :       RT (((lang_tree_node *)t)->template_info.partial);
    7476       727465 :       if (unsigned len = u ())
    7477              :         {
    7478            0 :           auto &ac = (((lang_tree_node *)t)
    7479              :                       ->template_info.deferred_access_checks);
    7480            0 :           vec_alloc (ac, len);
    7481            0 :           for (unsigned ix = 0; ix != len; ix++)
    7482              :             {
    7483            0 :               deferred_access_check m;
    7484              : 
    7485            0 :               RT (m.binfo);
    7486            0 :               RT (m.decl);
    7487            0 :               RT (m.diag_decl);
    7488            0 :               m.loc = state->read_location (*this);
    7489            0 :               ac->quick_push (m);
    7490              :             }
    7491              :         }
    7492              :       break;
    7493              : 
    7494       403736 :     case TEMPLATE_PARM_INDEX:
    7495       403736 :       RU (((lang_tree_node *)t)->tpi.index);
    7496       403736 :       RU (((lang_tree_node *)t)->tpi.level);
    7497       403736 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7498       403736 :       RT (((lang_tree_node *)t)->tpi.decl);
    7499       403736 :       break;
    7500              : 
    7501         8179 :     case TRAIT_EXPR:
    7502         8179 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7503         8179 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7504         8179 :       ((lang_tree_node *)t)->trait_expression.locus
    7505         8179 :         = state->read_location (*this);
    7506         8179 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7507         8179 :       break;
    7508              : 
    7509            2 :     case TU_LOCAL_ENTITY:
    7510            2 :       RT (((lang_tree_node *)t)->tu_local_entity.name);
    7511            2 :       ((lang_tree_node *)t)->tu_local_entity.loc
    7512            2 :         = state->read_location (*this);
    7513              :     }
    7514              : 
    7515     15232223 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7516              :     {
    7517     14137607 :       tree type = tree_node ();
    7518              : 
    7519     14137607 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7520              :         {
    7521         1743 :           unsigned precision = u ();
    7522              : 
    7523         1743 :           type = build_distinct_type_copy (type);
    7524         1743 :           TYPE_PRECISION (type) = precision;
    7525         3486 :           set_min_and_max_values_for_integral_type (type, precision,
    7526         1743 :                                                     TYPE_SIGN (type));
    7527              :         }
    7528              : 
    7529     14137607 :       if (code != TEMPLATE_DECL)
    7530     13810948 :         t->typed.type = type;
    7531              :     }
    7532              : 
    7533     15232223 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7534        53005 :     if (unsigned len = u ())
    7535              :       {
    7536        32036 :         vec_alloc (t->constructor.elts, len);
    7537       211427 :         for (unsigned ix = 0; ix != len; ix++)
    7538              :           {
    7539       179391 :             constructor_elt elt;
    7540              : 
    7541       179391 :             RT (elt.index);
    7542       179391 :             RTU (elt.value);
    7543       179391 :             t->constructor.elts->quick_push (elt);
    7544              :           }
    7545              :       }
    7546              : 
    7547              : #undef RT
    7548              : #undef RM
    7549              : #undef RU
    7550     15232223 :   return !get_overrun ();
    7551              : }
    7552              : 
    7553              : void
    7554      6092786 : trees_out::lang_decl_vals (tree t)
    7555              : {
    7556      6092786 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7557              : #define WU(X) (u (X))
    7558              : #define WT(X) (tree_node (X))
    7559              :   /* Module index already written.  */
    7560      6092786 :   switch (lang->u.base.selector)
    7561              :     {
    7562            0 :     default:
    7563            0 :       gcc_unreachable ();
    7564              : 
    7565      1196242 :     case lds_fn:  /* lang_decl_fn.  */
    7566      1196242 :       if (streaming_p ())
    7567              :         {
    7568       598014 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7569       102681 :             WU (lang->u.fn.ovl_op_code);
    7570              :         }
    7571              : 
    7572      1489536 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7573       933788 :         WT (lang->u.fn.context);
    7574              : 
    7575      1196242 :       if (lang->u.fn.thunk_p)
    7576              :         {
    7577              :           /* The thunked-to function.  */
    7578         1736 :           WT (lang->u.fn.befriending_classes);
    7579         1736 :           if (streaming_p ())
    7580          868 :             wi (lang->u.fn.u5.fixed_offset);
    7581              :         }
    7582      1194506 :       else if (decl_tls_wrapper_p (t))
    7583              :         /* The wrapped variable.  */
    7584           18 :         WT (lang->u.fn.befriending_classes);
    7585              :       else
    7586      1194488 :         WT (lang->u.fn.u5.cloned_function);
    7587              : 
    7588      1196242 :       if (FNDECL_USED_AUTO (t))
    7589         4899 :         WT (lang->u.fn.u.saved_auto_return_type);
    7590              : 
    7591      1196242 :       goto lds_min;
    7592              : 
    7593         6288 :     case lds_decomp:  /* lang_decl_decomp.  */
    7594         6288 :       WT (lang->u.decomp.base);
    7595         6288 :       goto lds_min;
    7596              : 
    7597      3505155 :     case lds_min:  /* lang_decl_min.  */
    7598      3505155 :     lds_min:
    7599      3505155 :       WT (lang->u.min.template_info);
    7600      3505155 :       {
    7601      3505155 :         tree access = lang->u.min.access;
    7602              : 
    7603              :         /* DECL_ACCESS needs to be maintained by the definition of the
    7604              :            (derived) class that changes the access.  The other users
    7605              :            of DECL_ACCESS need to write it here.  */
    7606      1196242 :         if (!DECL_THUNK_P (t)
    7607      4699661 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7608              :           access = NULL_TREE;
    7609              : 
    7610      3505155 :         WT (access);
    7611              :       }
    7612              :       /* A friend template specialisation stashes its owning class on its
    7613              :          DECL_CHAIN; we need to reconstruct this, but it needs to happen
    7614              :          after we stream the template_info so readers can know this is such
    7615              :          an entity.  */
    7616      3505155 :       if (decl_specialization_friend_p (t))
    7617          120 :         WT (t->common.chain);
    7618              :       break;
    7619              : 
    7620              :     case lds_ns:  /* lang_decl_ns.  */
    7621              :       break;
    7622              : 
    7623      2587308 :     case lds_parm:  /* lang_decl_parm.  */
    7624      2587308 :       if (streaming_p ())
    7625              :         {
    7626       879425 :           WU (lang->u.parm.level);
    7627       879425 :           WU (lang->u.parm.index);
    7628              :         }
    7629              :       break;
    7630              :     }
    7631              : #undef WU
    7632              : #undef WT
    7633      6092786 : }
    7634              : 
    7635              : bool
    7636      2045813 : trees_in::lang_decl_vals (tree t)
    7637              : {
    7638      2045813 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7639              : #define RU(X) ((X) = u ())
    7640              : #define RT(X) ((X) = tree_node ())
    7641              : 
    7642              :   /* Module index already read.  */
    7643      2045813 :   switch (lang->u.base.selector)
    7644              :     {
    7645            0 :     default:
    7646            0 :       gcc_unreachable ();
    7647              : 
    7648       462193 :     case lds_fn:  /* lang_decl_fn.  */
    7649       462193 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7650              :         {
    7651        79643 :           unsigned code = u ();
    7652              : 
    7653              :           /* Check consistency.  */
    7654        79643 :           if (code >= OVL_OP_MAX
    7655        79643 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7656        79643 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7657            0 :             set_overrun ();
    7658              :           else
    7659        79643 :             lang->u.fn.ovl_op_code = code;
    7660              :         }
    7661              : 
    7662       572891 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7663       364702 :         RT (lang->u.fn.context);
    7664              : 
    7665       462193 :       if (lang->u.fn.thunk_p)
    7666              :         {
    7667          574 :           RT (lang->u.fn.befriending_classes);
    7668          574 :           lang->u.fn.u5.fixed_offset = wi ();
    7669              :         }
    7670       461619 :       else if (decl_tls_wrapper_p (t))
    7671           15 :         RT (lang->u.fn.befriending_classes);
    7672              :       else
    7673       461604 :         RT (lang->u.fn.u5.cloned_function);
    7674              : 
    7675       462193 :       if (FNDECL_USED_AUTO (t))
    7676         1446 :         RT (lang->u.fn.u.saved_auto_return_type);
    7677       462193 :       goto lds_min;
    7678              : 
    7679         2926 :     case lds_decomp:  /* lang_decl_decomp.  */
    7680         2926 :       RT (lang->u.decomp.base);
    7681         2926 :       goto lds_min;
    7682              : 
    7683      1369198 :     case lds_min:  /* lang_decl_min.  */
    7684      1369198 :     lds_min:
    7685      1369198 :       RT (lang->u.min.template_info);
    7686      1369198 :       RT (lang->u.min.access);
    7687      1369198 :       if (decl_specialization_friend_p (t))
    7688           51 :         RT (t->common.chain);
    7689              :       break;
    7690              : 
    7691              :     case lds_ns:  /* lang_decl_ns.  */
    7692              :       break;
    7693              : 
    7694       676460 :     case lds_parm:  /* lang_decl_parm.  */
    7695       676460 :       RU (lang->u.parm.level);
    7696       676460 :       RU (lang->u.parm.index);
    7697       676460 :       break;
    7698              :     }
    7699              : #undef RU
    7700              : #undef RT
    7701      2045813 :   return !get_overrun ();
    7702              : }
    7703              : 
    7704              : /* Most of the value contents of lang_type is streamed in
    7705              :    define_class.  */
    7706              : 
    7707              : void
    7708       413642 : trees_out::lang_type_vals (tree t)
    7709              : {
    7710       413642 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7711              : #define WU(X) (u (X))
    7712              : #define WT(X) (tree_node (X))
    7713       413642 :   if (streaming_p ())
    7714       206798 :     WU (lang->align);
    7715              : #undef WU
    7716              : #undef WT
    7717       413642 : }
    7718              : 
    7719              : bool
    7720       151288 : trees_in::lang_type_vals (tree t)
    7721              : {
    7722       151288 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7723              : #define RU(X) ((X) = u ())
    7724              : #define RT(X) ((X) = tree_node ())
    7725       151288 :   RU (lang->align);
    7726              : #undef RU
    7727              : #undef RT
    7728       151288 :   return !get_overrun ();
    7729              : }
    7730              : 
    7731              : /* Write out the bools of T, including information about any
    7732              :    LANG_SPECIFIC information.  Including allocation of any lang
    7733              :    specific object.  */
    7734              : 
    7735              : void
    7736     19799156 : trees_out::tree_node_bools (tree t)
    7737              : {
    7738     19799156 :   gcc_checking_assert (streaming_p ());
    7739              : 
    7740              :   /* We should never stream a namespace.  */
    7741     19799156 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7742              :                        || DECL_NAMESPACE_ALIAS (t));
    7743              : 
    7744     19799156 :   bits_out bits = stream_bits ();
    7745     19799156 :   core_bools (t, bits);
    7746              : 
    7747     19799156 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7748              :     {
    7749      4304538 :     case tcc_declaration:
    7750      4304538 :       {
    7751      4304538 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7752      4304538 :         bits.b (specific);
    7753      4304538 :         if (specific && VAR_P (t))
    7754       357137 :           bits.b (DECL_DECOMPOSITION_P (t));
    7755      2622351 :         if (specific)
    7756      2622351 :           lang_decl_bools (t, bits);
    7757              :       }
    7758              :       break;
    7759              : 
    7760       792157 :     case tcc_type:
    7761       792157 :       {
    7762       792157 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7763       792157 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7764       792157 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7765              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7766              : 
    7767       792157 :         bits.b (specific);
    7768       792157 :         if (specific)
    7769       206798 :           lang_type_bools (t, bits);
    7770              :       }
    7771              :       break;
    7772              : 
    7773              :     default:
    7774              :       break;
    7775              :     }
    7776              : 
    7777     19799156 :   bits.bflush ();
    7778     19799156 : }
    7779              : 
    7780              : bool
    7781     15253707 : trees_in::tree_node_bools (tree t)
    7782              : {
    7783     15253707 :   bits_in bits = stream_bits ();
    7784     15253707 :   bool ok = core_bools (t, bits);
    7785              : 
    7786     15253707 :   if (ok)
    7787     15253707 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7788              :       {
    7789      3260930 :       case tcc_declaration:
    7790      3260930 :         if (bits.b ())
    7791              :           {
    7792      2045813 :             bool decomp = VAR_P (t) && bits.b ();
    7793              : 
    7794      2045813 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7795      2045813 :             if (ok)
    7796      2045813 :               ok = lang_decl_bools (t, bits);
    7797              :           }
    7798              :         break;
    7799              : 
    7800       574770 :       case tcc_type:
    7801       574770 :         if (bits.b ())
    7802              :           {
    7803       151288 :             ok = maybe_add_lang_type_raw (t);
    7804       151288 :             if (ok)
    7805       151288 :               ok = lang_type_bools (t, bits);
    7806              :           }
    7807              :         break;
    7808              : 
    7809              :       default:
    7810              :         break;
    7811              :       }
    7812              : 
    7813     15253707 :   bits.bflush ();
    7814     15253707 :   if (!ok || get_overrun ())
    7815            0 :     return false;
    7816              : 
    7817              :   return true;
    7818     15253707 : }
    7819              : 
    7820              : 
    7821              : /* Write out the lang-specific vals of node T.  */
    7822              : 
    7823              : void
    7824     51417255 : trees_out::lang_vals (tree t)
    7825              : {
    7826     51417255 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7827              :     {
    7828     11299492 :     case tcc_declaration:
    7829     11299492 :       if (DECL_LANG_SPECIFIC (t))
    7830      6092786 :         lang_decl_vals (t);
    7831              :       break;
    7832              : 
    7833      2786764 :     case tcc_type:
    7834      2786764 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7835       413642 :         lang_type_vals (t);
    7836              :       break;
    7837              : 
    7838              :     default:
    7839              :       break;
    7840              :     }
    7841     51417255 : }
    7842              : 
    7843              : bool
    7844     15232223 : trees_in::lang_vals (tree t)
    7845              : {
    7846     15232223 :   bool ok = true;
    7847              : 
    7848     15232223 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7849              :     {
    7850      3260930 :     case tcc_declaration:
    7851      3260930 :       if (DECL_LANG_SPECIFIC (t))
    7852      2045813 :         ok = lang_decl_vals (t);
    7853              :       break;
    7854              : 
    7855       553286 :     case tcc_type:
    7856       553286 :       if (TYPE_LANG_SPECIFIC (t))
    7857       151288 :         ok = lang_type_vals (t);
    7858              :       else
    7859       401998 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7860              :       break;
    7861              : 
    7862              :     default:
    7863              :       break;
    7864              :     }
    7865              : 
    7866     15232223 :   return ok;
    7867              : }
    7868              : 
    7869              : /* Write out the value fields of node T.  */
    7870              : 
    7871              : void
    7872     51417255 : trees_out::tree_node_vals (tree t)
    7873              : {
    7874     51417255 :   core_vals (t);
    7875     51417255 :   lang_vals (t);
    7876     51417255 : }
    7877              : 
    7878              : bool
    7879     15232223 : trees_in::tree_node_vals (tree t)
    7880              : {
    7881     15232223 :   bool ok = core_vals (t);
    7882     15232223 :   if (ok)
    7883     15232223 :     ok = lang_vals (t);
    7884              : 
    7885     15232223 :   return ok;
    7886              : }
    7887              : 
    7888              : 
    7889              : /* If T is a back reference, fixed reference or NULL, write out its
    7890              :    code and return WK_none.  Otherwise return WK_value if we must write
    7891              :    by value, or WK_normal otherwise.  */
    7892              : 
    7893              : walk_kind
    7894    336963649 : trees_out::ref_node (tree t)
    7895              : {
    7896    336963649 :   if (!t)
    7897              :     {
    7898    135383689 :       if (streaming_p ())
    7899              :         {
    7900              :           /* NULL_TREE -> tt_null.  */
    7901     51447344 :           null_count++;
    7902     51447344 :           i (tt_null);
    7903              :         }
    7904    135383689 :       return WK_none;
    7905              :     }
    7906              : 
    7907    201579960 :   if (!TREE_VISITED (t))
    7908              :     return WK_normal;
    7909              : 
    7910              :   /* An already-visited tree.  It must be in the map.  */
    7911    117647824 :   int val = get_tag (t);
    7912              : 
    7913    117647824 :   if (val == tag_value)
    7914              :     /* An entry we should walk into.  */
    7915              :     return WK_value;
    7916              : 
    7917    115839982 :   const char *kind;
    7918              : 
    7919    115839982 :   if (val <= tag_backref)
    7920              :     {
    7921              :       /* Back reference -> -ve number  */
    7922     88327251 :       if (streaming_p ())
    7923     41801710 :         i (val);
    7924              :       kind = "backref";
    7925              :     }
    7926     27512731 :   else if (val >= tag_fixed)
    7927              :     {
    7928              :       /* Fixed reference -> tt_fixed */
    7929     27512731 :       val -= tag_fixed;
    7930     27512731 :       if (streaming_p ())
    7931      9658397 :         i (tt_fixed), u (val);
    7932              :       kind = "fixed";
    7933              :     }
    7934              : 
    7935    115839982 :   if (streaming_p ())
    7936              :     {
    7937     51460107 :       back_ref_count++;
    7938     51460107 :       dump (dumper::TREE)
    7939        14184 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7940              :     }
    7941              :   return WK_none;
    7942              : }
    7943              : 
    7944              : tree
    7945     31973428 : trees_in::back_ref (int tag)
    7946              : {
    7947     31973428 :   tree res = NULL_TREE;
    7948              : 
    7949     31973428 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7950     31973428 :     res = back_refs[~tag];
    7951              : 
    7952     31973428 :   if (!res
    7953              :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7954              :          wild pointer.  Checking the code provides evidence we've not
    7955              :          corrupted something.  */
    7956     31973428 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7957            0 :     set_overrun ();
    7958              :   else
    7959     31987968 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7960              :                                  TREE_CODE (res), res, res);
    7961     31973428 :   return res;
    7962              : }
    7963              : 
    7964              : unsigned
    7965      4778124 : trees_out::add_indirect_tpl_parms (tree parms)
    7966              : {
    7967      4778124 :   unsigned len = 0;
    7968      8990388 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7969              :     {
    7970      5055253 :       if (TREE_VISITED (parms))
    7971              :         break;
    7972              : 
    7973      4212264 :       int tag = insert (parms);
    7974      4212264 :       if (streaming_p ())
    7975      4212591 :         dump (dumper::TREE)
    7976          153 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7977          153 :                    tag, len, TREE_CODE (parms), parms);
    7978              :     }
    7979              : 
    7980      4778124 :   if (streaming_p ())
    7981       797041 :     u (len);
    7982              : 
    7983      4778124 :   return len;
    7984              : }
    7985              : 
    7986              : unsigned
    7987       541699 : trees_in::add_indirect_tpl_parms (tree parms)
    7988              : {
    7989       541699 :   unsigned len = u ();
    7990       969022 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    7991              :     {
    7992       427323 :       int tag = insert (parms);
    7993       427788 :       dump (dumper::TREE)
    7994          159 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    7995          159 :                  tag, ix, TREE_CODE (parms), parms);
    7996              :     }
    7997              : 
    7998       541699 :   return len;
    7999              : }
    8000              : 
    8001              : /* We've just found DECL by name.  Insert nodes that come with it, but
    8002              :    cannot be found by name, so we'll not accidentally walk into them.  */
    8003              : 
    8004              : void
    8005     10560132 : trees_out::add_indirects (tree decl)
    8006              : {
    8007     10560132 :   unsigned count = 0;
    8008              : 
    8009              :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    8010              :   // templates and perhaps default template parms too.  The former can
    8011              :   // be referenced from instantiations (as they are lazily
    8012              :   // instantiated).  Also (deferred?) exception specifications of
    8013              :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    8014     10560132 :   tree inner = decl;
    8015     10560132 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8016              :     {
    8017      4778124 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8018              : 
    8019      4778124 :       inner = DECL_TEMPLATE_RESULT (decl);
    8020      4778124 :       int tag = insert (inner);
    8021      4778124 :       if (streaming_p ())
    8022       797041 :         dump (dumper::TREE)
    8023          219 :           && dump ("Indirect:%d template's result %C:%N",
    8024          219 :                    tag, TREE_CODE (inner), inner);
    8025      4778124 :       count++;
    8026              :     }
    8027              : 
    8028     10560132 :   if (TREE_CODE (inner) == TYPE_DECL)
    8029              :     {
    8030              :       /* Make sure the type is in the map too.  Otherwise we get
    8031              :          different RECORD_TYPEs for the same type, and things go
    8032              :          south.  */
    8033      5568220 :       tree type = TREE_TYPE (inner);
    8034      5568220 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8035              :                            || TYPE_NAME (type) == inner);
    8036      5568220 :       int tag = insert (type);
    8037      5568220 :       if (streaming_p ())
    8038       611941 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8039          362 :                                      TREE_CODE (type), type);
    8040      5568220 :       count++;
    8041              :     }
    8042              : 
    8043     10560132 :   if (streaming_p ())
    8044              :     {
    8045      1600877 :       u (count);
    8046      1601406 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8047              :     }
    8048     10560132 : }
    8049              : 
    8050              : bool
    8051      1082954 : trees_in::add_indirects (tree decl)
    8052              : {
    8053      1082954 :   unsigned count = 0;
    8054              : 
    8055      1082954 :   tree inner = decl;
    8056      1082954 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8057              :     {
    8058       541699 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8059              : 
    8060       541699 :       inner = DECL_TEMPLATE_RESULT (decl);
    8061       541699 :       int tag = insert (inner);
    8062       541699 :       dump (dumper::TREE)
    8063          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8064          228 :                  TREE_CODE (inner), inner);
    8065       541699 :       count++;
    8066              :     }
    8067              : 
    8068      1082954 :   if (TREE_CODE (inner) == TYPE_DECL)
    8069              :     {
    8070       411927 :       tree type = TREE_TYPE (inner);
    8071       411927 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8072              :                            || TYPE_NAME (type) == inner);
    8073       411927 :       int tag = insert (type);
    8074       411927 :       dump (dumper::TREE)
    8075          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8076       411927 :       count++;
    8077              :     }
    8078              : 
    8079      1083559 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8080      1082954 :   return count == u ();
    8081              : }
    8082              : 
    8083              : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    8084              :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    8085              :         TEMPLATE_TYPE_PARM_INDEX TPI
    8086              :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    8087              :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    8088              :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    8089              :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    8090              :        TEMPLATE_TYPE_PARM_INDEX->TPI
    8091              :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    8092              : 
    8093              :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    8094              : */
    8095              : 
    8096              : void
    8097      2568593 : trees_out::tpl_parm_value (tree parm)
    8098              : {
    8099      2568593 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8100              : 
    8101      2568593 :   int parm_tag = insert (parm);
    8102      2568593 :   if (streaming_p ())
    8103              :     {
    8104       571462 :       i (tt_tpl_parm);
    8105       571462 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8106          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8107       571462 :       start (parm);
    8108       571462 :       tree_node_bools (parm);
    8109              :     }
    8110              : 
    8111      2568593 :   tree inner = parm;
    8112      2568593 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8113              :     {
    8114         6652 :       inner = DECL_TEMPLATE_RESULT (inner);
    8115         6652 :       int inner_tag = insert (inner);
    8116         6652 :       if (streaming_p ())
    8117              :         {
    8118         1752 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8119            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8120         1752 :           start (inner);
    8121         1752 :           tree_node_bools (inner);
    8122              :         }
    8123              :     }
    8124              : 
    8125      2568593 :   tree type = NULL_TREE;
    8126      2568593 :   if (TREE_CODE (inner) == TYPE_DECL)
    8127              :     {
    8128      2304470 :       type = TREE_TYPE (inner);
    8129      2304470 :       int type_tag = insert (type);
    8130      2304470 :       if (streaming_p ())
    8131              :         {
    8132       516454 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8133          108 :                                        type_tag, TREE_CODE (type), type);
    8134       516454 :           start (type);
    8135       516454 :           tree_node_bools (type);
    8136              :         }
    8137              :     }
    8138              : 
    8139      2568593 :   if (inner != parm)
    8140              :     {
    8141              :       /* This is a template-template parameter.  */
    8142         6652 :       unsigned tpl_levels = 0;
    8143         6652 :       tpl_header (parm, &tpl_levels);
    8144         6652 :       tpl_parms_fini (parm, tpl_levels);
    8145              :     }
    8146              : 
    8147      2568593 :   tree_node_vals (parm);
    8148      2568593 :   if (inner != parm)
    8149         6652 :     tree_node_vals (inner);
    8150      2568593 :   if (type)
    8151              :     {
    8152      2304470 :       tree_node_vals (type);
    8153      2304470 :       if (DECL_NAME (inner) == auto_identifier
    8154      2304470 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8155              :         {
    8156              :           /* Placeholder auto.  */
    8157       108474 :           tree_node (DECL_INITIAL (inner));
    8158       108474 :           tree_node (DECL_SIZE_UNIT (inner));
    8159              :         }
    8160              :     }
    8161              : 
    8162      2568593 :   if (streaming_p ())
    8163       571462 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8164          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8165      2568593 : }
    8166              : 
    8167              : tree
    8168       421378 : trees_in::tpl_parm_value ()
    8169              : {
    8170       421378 :   tree parm = start ();
    8171       421378 :   if (!parm || !tree_node_bools (parm))
    8172            0 :     return NULL_TREE;
    8173              : 
    8174       421378 :   int parm_tag = insert (parm);
    8175       421378 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8176          198 :                                parm_tag, TREE_CODE (parm), parm);
    8177              : 
    8178       421378 :   tree inner = parm;
    8179       421378 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8180              :     {
    8181         1219 :       inner = start ();
    8182         1219 :       if (!inner || !tree_node_bools (inner))
    8183            0 :         return NULL_TREE;
    8184         1219 :       int inner_tag = insert (inner);
    8185         1219 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8186            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8187         1219 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8188              :     }
    8189              : 
    8190       421378 :   tree type = NULL_TREE;
    8191       421378 :   if (TREE_CODE (inner) == TYPE_DECL)
    8192              :     {
    8193       378961 :       type = start ();
    8194       378961 :       if (!type || !tree_node_bools (type))
    8195            0 :         return NULL_TREE;
    8196       378961 :       int type_tag = insert (type);
    8197       378961 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8198          120 :                                    type_tag, TREE_CODE (type), type);
    8199              : 
    8200       378961 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8201       378961 :       TYPE_NAME (type) = parm;
    8202              :     }
    8203              : 
    8204       421378 :   if (inner != parm)
    8205              :     {
    8206              :       /* A template template parameter.  */
    8207         1219 :       unsigned tpl_levels = 0;
    8208         1219 :       tpl_header (parm, &tpl_levels);
    8209         1219 :       tpl_parms_fini (parm, tpl_levels);
    8210              :     }
    8211              : 
    8212       421378 :   tree_node_vals (parm);
    8213       421378 :   if (inner != parm)
    8214         1219 :     tree_node_vals (inner);
    8215       421378 :   if (type)
    8216              :     {
    8217       378961 :       tree_node_vals (type);
    8218       378961 :       if (DECL_NAME (inner) == auto_identifier
    8219       378961 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8220              :         {
    8221              :           /* Placeholder auto.  */
    8222        32821 :           DECL_INITIAL (inner) = tree_node ();
    8223        32821 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8224              :         }
    8225       378961 :       if (TYPE_CANONICAL (type))
    8226              :         {
    8227       378961 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8228       378961 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8229              :         }
    8230              :     }
    8231              : 
    8232       421378 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    8233          198 :                                parm_tag, TREE_CODE (parm), parm);
    8234              : 
    8235              :   return parm;
    8236              : }
    8237              : 
    8238              : void
    8239      1618159 : trees_out::install_entity (tree decl, depset *dep)
    8240              : {
    8241      1618159 :   gcc_checking_assert (streaming_p ());
    8242              : 
    8243              :   /* Write the entity index, so we can insert it as soon as we
    8244              :      know this is new.  */
    8245      1618159 :   u (dep ? dep->cluster + 1 : 0);
    8246      1618159 :   if (CHECKING_P && dep)
    8247              :     {
    8248              :       /* Add it to the entity map, such that we can tell it is
    8249              :          part of us.  */
    8250      1176440 :       bool existed;
    8251      1176440 :       unsigned *slot = &entity_map->get_or_insert
    8252      1176440 :         (DECL_UID (decl), &existed);
    8253      1176440 :       if (existed)
    8254              :         /* If it existed, it should match.  */
    8255         1630 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8256      1176440 :       *slot = ~dep->cluster;
    8257              :     }
    8258      1618159 : }
    8259              : 
    8260              : bool
    8261      1203585 : trees_in::install_entity (tree decl)
    8262              : {
    8263      1203585 :   unsigned entity_index = u ();
    8264      1203585 :   if (!entity_index)
    8265              :     return false;
    8266              : 
    8267       874009 :   if (entity_index > state->entity_num)
    8268              :     {
    8269            0 :       set_overrun ();
    8270            0 :       return false;
    8271              :     }
    8272              : 
    8273              :   /* Insert the real decl into the entity ary.  */
    8274       874009 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8275       874009 :   (*entity_ary)[ident] = decl;
    8276              : 
    8277              :   /* And into the entity map, if it's not already there.  */
    8278       874009 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8279       874009 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8280      1652228 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    8281              :     {
    8282              :       /* We don't want to use retrofit_lang_decl directly so that we aren't
    8283              :          affected by the language state when we load in.  */
    8284       872872 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8285              :         {
    8286        95790 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8287        95790 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8288              :         }
    8289       872872 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8290              : 
    8291              :       /* Insert into the entity hash (it cannot already be there).  */
    8292       872872 :       bool existed;
    8293       872872 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8294       872872 :       gcc_checking_assert (!existed);
    8295       872872 :       slot = ident;
    8296              :     }
    8297              :   else
    8298              :     {
    8299         1137 :       unsigned *slot = entity_map->get (DECL_UID (decl));
    8300              : 
    8301              :       /* The entity must be in the entity map already.  However, DECL may
    8302              :          be the DECL_TEMPLATE_RESULT of an existing partial specialisation
    8303              :          if we matched it while streaming another instantiation; in this
    8304              :          case we already registered that TEMPLATE_DECL.  */
    8305         1137 :       if (!slot)
    8306              :         {
    8307            9 :           tree type = TREE_TYPE (decl);
    8308            9 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    8309              :                                && CLASS_TYPE_P (type)
    8310              :                                && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
    8311            9 :           slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
    8312              :         }
    8313            9 :       gcc_checking_assert (slot);
    8314              : 
    8315         1137 :       if (state->is_partition ())
    8316              :         {
    8317              :           /* The decl is already in the entity map, but we see it again now
    8318              :              from a partition: we want to overwrite if the original decl
    8319              :              wasn't also from a (possibly different) partition.  Otherwise,
    8320              :              for things like template instantiations, make_dependency might
    8321              :              not realise that this is also provided from a partition and
    8322              :              should be considered part of this module (and thus always
    8323              :              emitted into the primary interface's CMI).  */
    8324          432 :           module_state *imp = import_entity_module (*slot);
    8325          432 :           if (!imp->is_partition ())
    8326          297 :             *slot = ident;
    8327              :         }
    8328              :     }
    8329              : 
    8330              :   return true;
    8331              : }
    8332              : 
    8333              : static bool has_definition (tree decl);
    8334              : 
    8335              : /* DECL is a decl node that must be written by value.  DEP is the
    8336              :    decl's depset.  */
    8337              : 
    8338              : void
    8339      4435408 : trees_out::decl_value (tree decl, depset *dep)
    8340              : {
    8341              :   /* We should not be writing clones or template parms.  */
    8342      4435408 :   gcc_checking_assert (DECL_P (decl)
    8343              :                        && !DECL_CLONED_FUNCTION_P (decl)
    8344              :                        && !DECL_TEMPLATE_PARM_P (decl));
    8345              : 
    8346              :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    8347      4435408 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    8348              :                        || DECL_ORIGINAL_TYPE (decl)
    8349              :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    8350              : 
    8351              :   /* There's no need to walk any of the contents of a known TU-local entity,
    8352              :      since importers should never see any of it regardless.  But make sure we
    8353              :      at least note its location so importers can use it for diagnostics.  */
    8354      4435408 :   if (dep && dep->is_tu_local ())
    8355              :     {
    8356          416 :       gcc_checking_assert (is_initial_scan ());
    8357          416 :       insert (decl, WK_value);
    8358          416 :       state->note_location (DECL_SOURCE_LOCATION (decl));
    8359          416 :       return;
    8360              :     }
    8361              : 
    8362      4434992 :   merge_kind mk = get_merge_kind (decl, dep);
    8363              : 
    8364      4434992 :   if (CHECKING_P)
    8365              :     {
    8366              :       /* Never start in the middle of a template.  */
    8367      4434992 :       int use_tpl = -1;
    8368      4434992 :       if (tree ti = node_template_info (decl, use_tpl))
    8369      1544694 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8370              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8371              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8372              :                                  != decl));
    8373              :     }
    8374              : 
    8375      4434992 :   if (streaming_p ())
    8376              :     {
    8377              :       /* A new node -> tt_decl.  */
    8378      1618159 :       decl_val_count++;
    8379      1618159 :       i (tt_decl);
    8380      1618159 :       u (mk);
    8381      1618159 :       start (decl);
    8382              : 
    8383      1618159 :       if (mk != MK_unique)
    8384              :         {
    8385      1338404 :           bits_out bits = stream_bits ();
    8386      1338404 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8387              :             {
    8388              :               /* Tell the importer whether this is a global module entity,
    8389              :                  or a module entity.  */
    8390       312393 :               tree o = get_originating_module_decl (decl);
    8391       312393 :               bool is_attached = false;
    8392              : 
    8393       312393 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8394       312393 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8395       498819 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8396              :                 is_attached = true;
    8397              : 
    8398       312393 :               bits.b (is_attached);
    8399              :             }
    8400      1338404 :           bits.b (dep && dep->has_defn ());
    8401      1338404 :         }
    8402      1618159 :       tree_node_bools (decl);
    8403              :     }
    8404              : 
    8405      4434992 :   int tag = insert (decl, WK_value);
    8406      4434992 :   if (streaming_p ())
    8407      1618159 :     dump (dumper::TREE)
    8408          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8409          683 :                TREE_CODE (decl), decl, decl);
    8410              : 
    8411      4434992 :   tree inner = decl;
    8412      4434992 :   int inner_tag = 0;
    8413      4434992 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8414              :     {
    8415      1273477 :       inner = DECL_TEMPLATE_RESULT (decl);
    8416      1273477 :       inner_tag = insert (inner, WK_value);
    8417              : 
    8418              :       /* On stream-in we assume that a template and its result will
    8419              :          have the same type.  */
    8420      1273477 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8421              : 
    8422      1273477 :       if (streaming_p ())
    8423              :         {
    8424       424477 :           int code = TREE_CODE (inner);
    8425       424477 :           u (code);
    8426       424477 :           start (inner, true);
    8427       424477 :           tree_node_bools (inner);
    8428       424477 :           dump (dumper::TREE)
    8429          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8430          132 :                      TREE_CODE (inner), inner, inner);
    8431              :         }
    8432              :     }
    8433              : 
    8434      4434992 :   tree type = NULL_TREE;
    8435      4434992 :   int type_tag = 0;
    8436      4434992 :   tree stub_decl = NULL_TREE;
    8437      4434992 :   int stub_tag = 0;
    8438      4434992 :   if (TREE_CODE (inner) == TYPE_DECL)
    8439              :     {
    8440      1586600 :       type = TREE_TYPE (inner);
    8441      1586600 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8442      1586600 :                        && TYPE_NAME (type) == inner);
    8443              : 
    8444      1586600 :       if (streaming_p ())
    8445       535938 :         u (has_type ? TREE_CODE (type) : 0);
    8446              : 
    8447      1586600 :       if (has_type)
    8448              :         {
    8449       723362 :           type_tag = insert (type, WK_value);
    8450       723362 :           if (streaming_p ())
    8451              :             {
    8452       241096 :               start (type, true);
    8453       241096 :               tree_node_bools (type);
    8454       241096 :               dump (dumper::TREE)
    8455          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8456          155 :                          TREE_CODE (type), type);
    8457              :             }
    8458              : 
    8459       723362 :           stub_decl = TYPE_STUB_DECL (type);
    8460       723362 :           bool has_stub = inner != stub_decl;
    8461       723362 :           if (streaming_p ())
    8462       241096 :             u (has_stub ? TREE_CODE (stub_decl) : 0);
    8463       723362 :           if (has_stub)
    8464              :             {
    8465         2361 :               stub_tag = insert (stub_decl);
    8466         2361 :               if (streaming_p ())
    8467              :                 {
    8468          786 :                   start (stub_decl, true);
    8469          786 :                   tree_node_bools (stub_decl);
    8470          786 :                   dump (dumper::TREE)
    8471            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8472            0 :                              TREE_CODE (stub_decl), stub_decl);
    8473              :                 }
    8474              :             }
    8475              :           else
    8476              :             stub_decl = NULL_TREE;
    8477              :         }
    8478              :       else
    8479              :         /* Regular typedef.  */
    8480              :         type = NULL_TREE;
    8481              :     }
    8482              : 
    8483              :   /* Stream the container, we want it correctly canonicalized before
    8484              :      we start emitting keys for this decl.  */
    8485      4434992 :   tree container = decl_container (decl);
    8486      4434992 :   unsigned tpl_levels = 0;
    8487              : 
    8488              :   /* Also tell the importer whether this is a temploid friend attached
    8489              :      to a different module (which has implications for merging), so that
    8490              :      importers can reconstruct this information on stream-in.  */
    8491      4434992 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8492              :     {
    8493      3379843 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8494      3379843 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8495      3379843 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8496              :     }
    8497              : 
    8498      4434992 :   {
    8499      4434992 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8500      4434992 :     if (decl != inner)
    8501      1273477 :       tpl_header (decl, &tpl_levels);
    8502      4434992 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8503      1793243 :       fn_parms_init (inner);
    8504              : 
    8505              :     /* Now write out the merging information, and then really
    8506              :        install the tag values.  */
    8507      4434992 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8508              : 
    8509      4434992 :     if (streaming_p ())
    8510      4438891 :       dump (dumper::MERGE)
    8511         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8512         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8513      4434992 :   }
    8514              : 
    8515      4434992 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8516      4434992 :     fn_parms_fini (inner);
    8517              : 
    8518      4434992 :   if (!is_key_order ())
    8519      3256326 :     tree_node_vals (decl);
    8520              : 
    8521      4434992 :   if (inner_tag)
    8522              :     {
    8523      1273477 :       if (!is_key_order ())
    8524       849000 :         tree_node_vals (inner);
    8525      1273477 :       tpl_parms_fini (decl, tpl_levels);
    8526              :     }
    8527              : 
    8528      4434992 :   if (type && !is_key_order ())
    8529              :     {
    8530       482266 :       tree_node_vals (type);
    8531       482266 :       if (stub_decl)
    8532         1575 :         tree_node_vals (stub_decl);
    8533              :     }
    8534              : 
    8535      4434992 :   if (!is_key_order ())
    8536              :     {
    8537      3256326 :       if (mk & MK_template_mask
    8538      2271820 :           || mk == MK_partial
    8539      2271820 :           || mk == MK_friend_spec)
    8540              :         {
    8541        38096 :           if (mk != MK_partial)
    8542              :             {
    8543              :               // FIXME: We should make use of the merge-key by
    8544              :               // exposing it outside of key_mergeable.  But this gets
    8545              :               // the job done.
    8546       984506 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8547              : 
    8548       984506 :               if (streaming_p ())
    8549       492253 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8550              :                                                        entry->tmpl, decl));
    8551       984506 :               tree_node (entry->tmpl);
    8552       984506 :               tree_node (entry->args);
    8553              :             }
    8554              :           else
    8555              :             {
    8556        38096 :               tree ti = get_template_info (inner);
    8557        38096 :               tree_node (TI_TEMPLATE (ti));
    8558        38096 :               tree_node (TI_ARGS (ti));
    8559              :             }
    8560              :         }
    8561      3256326 :       tree_node (get_constraints (decl));
    8562              :     }
    8563              : 
    8564      4434992 :   if (streaming_p ())
    8565              :     {
    8566              :       /* Do not stray outside this section.  */
    8567      1618159 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8568              : 
    8569              :       /* Write the entity index, so we can insert it as soon as we
    8570              :          know this is new.  */
    8571      1618159 :       install_entity (decl, dep);
    8572              :     }
    8573              : 
    8574      4434992 :   if (DECL_LANG_SPECIFIC (inner)
    8575      3748863 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8576      4436189 :       && streaming_p ())
    8577              :     {
    8578              :       /* Stream the keyed entities.  There may be keyed entities that we
    8579              :          choose not to stream, such as a lambda in a non-inline variable's
    8580              :          initializer, so don't build dependencies for them here; any deps
    8581              :          we need should be acquired during write_definition (possibly
    8582              :          indirectly).  */
    8583          392 :       auto *attach_vec = keyed_table->get (inner);
    8584          392 :       unsigned num = attach_vec->length ();
    8585          392 :       u (num);
    8586          820 :       for (unsigned ix = 0; ix != num; ix++)
    8587              :         {
    8588          428 :           tree attached = (*attach_vec)[ix];
    8589          428 :           if (attached)
    8590              :             {
    8591          428 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8592          428 :               if (!dep_hash->find_dependency (attached)
    8593          428 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8594              :                 attached = NULL_TREE;
    8595              :             }
    8596              : 
    8597          428 :           tree_node (attached);
    8598          470 :           dump (dumper::MERGE)
    8599           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8600              :         }
    8601              :     }
    8602              : 
    8603      4434992 :   bool is_typedef = false;
    8604      4434992 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8605              :     {
    8606       863238 :       tree t = TREE_TYPE (inner);
    8607       863238 :       unsigned tdef_flags = 0;
    8608       863238 :       if (DECL_ORIGINAL_TYPE (inner)
    8609       863238 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8610              :         {
    8611       863238 :           tdef_flags |= 1;
    8612       863238 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8613       189173 :               && TYPE_DEPENDENT_P_VALID (t)
    8614      1043762 :               && TYPE_DEPENDENT_P (t))
    8615              :             tdef_flags |= 2;
    8616              :         }
    8617       863238 :       if (streaming_p ())
    8618       294842 :         u (tdef_flags);
    8619              : 
    8620       863238 :       if (tdef_flags & 1)
    8621              :         {
    8622              :           /* A typedef type.  */
    8623       863238 :           int type_tag = insert (t);
    8624       863238 :           if (streaming_p ())
    8625       294842 :             dump (dumper::TREE)
    8626          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8627              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8628          206 :                        TREE_CODE (t), t);
    8629              : 
    8630              :           is_typedef = true;
    8631              :         }
    8632              :     }
    8633              : 
    8634      4434992 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8635              :     {
    8636       135237 :       bool cloned_p
    8637       135237 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8638        96279 :       bool needs_vtt_parm_p
    8639        96279 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8640        96279 :       bool omit_inherited_parms_p
    8641        96279 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8642        77015 :            && base_ctor_omit_inherited_parms (decl));
    8643       135237 :       unsigned flags = (int (cloned_p) << 0
    8644       135237 :                         | int (needs_vtt_parm_p) << 1
    8645       135237 :                         | int (omit_inherited_parms_p) << 2);
    8646       135237 :       u (flags);
    8647       135316 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8648              :                                    decl, cloned_p ? "" : "not ");
    8649              :     }
    8650              : 
    8651      4434992 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8652          172 :     u (decl_tls_model (decl));
    8653              : 
    8654      4434992 :   if (streaming_p ())
    8655      1618159 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8656          683 :                                  TREE_CODE (decl), decl);
    8657              : 
    8658      4434992 :   if (NAMESPACE_SCOPE_P (inner))
    8659      2492090 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8660              :                                   && DECL_LOCAL_DECL_P (inner)));
    8661      3188796 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8662       858494 :             && !is_typedef
    8663       164057 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8664      3883233 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8665              :     {
    8666      1517546 :       bool write_defn = !dep && has_definition (decl);
    8667      1517546 :       if (streaming_p ())
    8668       506079 :         u (write_defn);
    8669      1517546 :       if (write_defn)
    8670            6 :         write_definition (decl);
    8671              :     }
    8672              : }
    8673              : 
    8674              : tree
    8675      1203585 : trees_in::decl_value ()
    8676              : {
    8677      1203585 :   int tag = 0;
    8678      1203585 :   bool is_attached = false;
    8679      1203585 :   bool has_defn = false;
    8680      1203585 :   unsigned mk_u = u ();
    8681      1203585 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8682              :     {
    8683            0 :       set_overrun ();
    8684            0 :       return NULL_TREE;
    8685              :     }
    8686              : 
    8687      1203585 :   unsigned saved_unused = unused;
    8688      1203585 :   unused = 0;
    8689              : 
    8690      1203585 :   merge_kind mk = merge_kind (mk_u);
    8691              : 
    8692      1203585 :   tree decl = start ();
    8693      1203585 :   if (decl)
    8694              :     {
    8695      1203585 :       if (mk != MK_unique)
    8696              :         {
    8697       993624 :           bits_in bits = stream_bits ();
    8698       993624 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8699        87029 :             is_attached = bits.b ();
    8700              : 
    8701       993624 :           has_defn = bits.b ();
    8702       993624 :         }
    8703              : 
    8704      1203585 :       if (!tree_node_bools (decl))
    8705            0 :         decl = NULL_TREE;
    8706              :     }
    8707              : 
    8708              :   /* Insert into map.  */
    8709      1203585 :   tag = insert (decl);
    8710      1203585 :   if (decl)
    8711      1203585 :     dump (dumper::TREE)
    8712          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8713              : 
    8714      1203585 :   tree inner = decl;
    8715      1203585 :   int inner_tag = 0;
    8716      1203585 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8717              :     {
    8718       325440 :       int code = u ();
    8719       325440 :       inner = start (code);
    8720       325440 :       if (inner && tree_node_bools (inner))
    8721       325440 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8722              :       else
    8723            0 :         decl = NULL_TREE;
    8724              : 
    8725       325440 :       inner_tag = insert (inner);
    8726       325440 :       if (decl)
    8727       325440 :         dump (dumper::TREE)
    8728          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8729              :     }
    8730              : 
    8731      1203585 :   tree type = NULL_TREE;
    8732      1203585 :   int type_tag = 0;
    8733      1203585 :   tree stub_decl = NULL_TREE;
    8734      1203585 :   int stub_tag = 0;
    8735      1203585 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8736              :     {
    8737       387010 :       if (unsigned type_code = u ())
    8738              :         {
    8739       174311 :           type = start (type_code);
    8740       174311 :           if (type && tree_node_bools (type))
    8741              :             {
    8742       174311 :               TREE_TYPE (inner) = type;
    8743       174311 :               TYPE_NAME (type) = inner;
    8744              :             }
    8745              :           else
    8746            0 :             decl = NULL_TREE;
    8747              : 
    8748       174311 :           type_tag = insert (type);
    8749       174311 :           if (decl)
    8750       174311 :             dump (dumper::TREE)
    8751          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8752              : 
    8753       174311 :           if (unsigned stub_code = u ())
    8754              :             {
    8755          441 :               stub_decl = start (stub_code);
    8756          441 :               if (stub_decl && tree_node_bools (stub_decl))
    8757              :                 {
    8758          441 :                   TREE_TYPE (stub_decl) = type;
    8759          441 :                   TYPE_STUB_DECL (type) = stub_decl;
    8760              :                 }
    8761              :               else
    8762            0 :                 decl = NULL_TREE;
    8763              : 
    8764          441 :               stub_tag = insert (stub_decl);
    8765          441 :               if (decl)
    8766          441 :                 dump (dumper::TREE)
    8767            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8768            0 :                            TREE_CODE (stub_decl));
    8769              :             }
    8770              :         }
    8771              :     }
    8772              : 
    8773      1203585 :   if (!decl)
    8774              :     {
    8775            0 :     bail:
    8776            0 :       if (inner_tag != 0)
    8777            0 :         back_refs[~inner_tag] = NULL_TREE;
    8778            0 :       if (type_tag != 0)
    8779            0 :         back_refs[~type_tag] = NULL_TREE;
    8780            0 :       if (stub_tag != 0)
    8781            0 :         back_refs[~stub_tag] = NULL_TREE;
    8782            0 :       if (tag != 0)
    8783            0 :         back_refs[~tag] = NULL_TREE;
    8784            0 :       set_overrun ();
    8785              :       /* Bail.  */
    8786            0 :       unused = saved_unused;
    8787            0 :       return NULL_TREE;
    8788              :     }
    8789              : 
    8790              :   /* Read the container, to ensure it's already been streamed in.  */
    8791      1203585 :   tree container = decl_container ();
    8792      1203585 :   unsigned tpl_levels = 0;
    8793              : 
    8794              :   /* If this is an imported temploid friend, get the owning decl its
    8795              :      attachment is determined by (or NULL_TREE otherwise).  */
    8796      1203585 :   tree temploid_friend = NULL_TREE;
    8797      1203585 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8798       849203 :     temploid_friend = tree_node ();
    8799              : 
    8800              :   /* Figure out if this decl is already known about.  */
    8801      1203585 :   int parm_tag = 0;
    8802              : 
    8803      1203585 :   if (decl != inner)
    8804       325440 :     if (!tpl_header (decl, &tpl_levels))
    8805            0 :       goto bail;
    8806      1203585 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8807       462193 :     parm_tag = fn_parms_init (inner);
    8808              : 
    8809      1203585 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8810              :                                  is_attached, temploid_friend);
    8811      1203585 :   tree existing_inner = existing;
    8812      1203585 :   if (existing)
    8813              :     {
    8814       436559 :       if (existing == error_mark_node)
    8815            0 :         goto bail;
    8816              : 
    8817       436559 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8818              :         {
    8819       161743 :           tree etype = TREE_TYPE (existing);
    8820       161743 :           if (TYPE_LANG_SPECIFIC (etype)
    8821       116690 :               && COMPLETE_TYPE_P (etype)
    8822       229849 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8823              :             /* Give it a member vec, we're likely gonna be looking
    8824              :                inside it.  */
    8825        14189 :             set_class_bindings (etype, -1);
    8826              :         }
    8827              : 
    8828              :       /* Install the existing decl into the back ref array.  */
    8829       436559 :       register_duplicate (decl, existing);
    8830       436559 :       back_refs[~tag] = existing;
    8831       436559 :       if (inner_tag != 0)
    8832              :         {
    8833       145632 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8834       145632 :           back_refs[~inner_tag] = existing_inner;
    8835              :         }
    8836              : 
    8837       436559 :       if (type_tag != 0)
    8838              :         {
    8839        77351 :           tree existing_type = TREE_TYPE (existing);
    8840        77351 :           back_refs[~type_tag] = existing_type;
    8841        77351 :           if (stub_tag != 0)
    8842          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8843              :         }
    8844              :     }
    8845              : 
    8846      1203585 :   if (parm_tag)
    8847       462193 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8848              : 
    8849      1203585 :   if (!tree_node_vals (decl))
    8850            0 :     goto bail;
    8851              : 
    8852      1203585 :   if (inner_tag)
    8853              :     {
    8854       325440 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8855              : 
    8856       325440 :       if (!tree_node_vals (inner))
    8857            0 :         goto bail;
    8858              : 
    8859       325440 :       if (!tpl_parms_fini (decl, tpl_levels))
    8860            0 :         goto bail;
    8861              :     }
    8862              : 
    8863      1203585 :   if (type && (!tree_node_vals (type)
    8864       174311 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8865            0 :     goto bail;
    8866              : 
    8867      1203585 :   spec_entry spec;
    8868      1203585 :   unsigned spec_flags = 0;
    8869      1203585 :   if (mk & MK_template_mask
    8870       831587 :       || mk == MK_partial
    8871       831587 :       || mk == MK_friend_spec)
    8872              :     {
    8873        10763 :       if (mk == MK_partial)
    8874              :         spec_flags = 2;
    8875              :       else
    8876       371998 :         spec_flags = u ();
    8877              : 
    8878       382761 :       spec.tmpl = tree_node ();
    8879       382761 :       spec.args = tree_node ();
    8880              :     }
    8881              :   /* Hold constraints on the spec field, for a short while.  */
    8882      1203585 :   spec.spec = tree_node ();
    8883              : 
    8884      1204549 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8885              : 
    8886      1203585 :   existing = back_refs[~tag];
    8887      1203585 :   bool installed = install_entity (existing);
    8888      1203585 :   bool is_new = existing == decl;
    8889              : 
    8890      1203585 :   if (DECL_LANG_SPECIFIC (inner)
    8891      2259714 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8892              :     {
    8893              :       /* Read and maybe install the attached entities.  */
    8894          375 :       bool existed;
    8895          375 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8896              :                                               &existed);
    8897          375 :       unsigned num = u ();
    8898          375 :       if (is_new == existed)
    8899            0 :         set_overrun ();
    8900          375 :       if (is_new)
    8901          242 :         set.reserve (num);
    8902          799 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8903              :         {
    8904          424 :           tree attached = tree_node ();
    8905          424 :           dump (dumper::MERGE)
    8906          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8907              :                      is_new ? "new" : "matched", attached);
    8908          424 :           if (is_new)
    8909          272 :             set.quick_push (attached);
    8910          152 :           else if (set[ix] != attached)
    8911              :             {
    8912            3 :               if (!set[ix] || !attached)
    8913              :                 /* One import left a hole for a lambda dep we chose not
    8914              :                    to stream, but another import chose to stream that lambda.
    8915              :                    Let's not error here: hopefully we'll complain later in
    8916              :                    is_matching_decl about whatever caused us to make a
    8917              :                    different decision.  */
    8918              :                 ;
    8919              :               else
    8920            0 :                 set_overrun ();
    8921              :             }
    8922              :         }
    8923              :     }
    8924              : 
    8925              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8926      1203585 :   unsigned tdef_flags = 0;
    8927      1203585 :   bool is_typedef = false;
    8928      1203585 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8929              :     {
    8930       212699 :       tdef_flags = u ();
    8931       212699 :       if (tdef_flags & 1)
    8932       212699 :         is_typedef = true;
    8933              :     }
    8934              : 
    8935      1203585 :   if (is_new)
    8936              :     {
    8937              :       /* A newly discovered node.  */
    8938       767026 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8939              :         /* Mark this identifier as naming a virtual function --
    8940              :            lookup_overrides relies on this optimization.  */
    8941         6570 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8942              : 
    8943       767026 :       if (installed)
    8944              :         {
    8945              :           /* Mark the entity as imported.  */
    8946       490223 :           retrofit_lang_decl (inner);
    8947       490223 :           DECL_MODULE_IMPORT_P (inner) = true;
    8948              :         }
    8949              : 
    8950       767026 :       if (temploid_friend)
    8951           38 :         imported_temploid_friends->put (decl, temploid_friend);
    8952              : 
    8953       767026 :       if (spec.spec)
    8954        32491 :         set_constraints (decl, spec.spec);
    8955              : 
    8956       767026 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8957              :         {
    8958            0 :           decl = cache_integer_cst (decl, true);
    8959            0 :           back_refs[~tag] = decl;
    8960              :         }
    8961              : 
    8962       767026 :       if (is_typedef)
    8963              :         {
    8964              :           /* Frob it to be ready for cloning.  */
    8965       128307 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8966       128307 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8967       128307 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8968              :             {
    8969       128304 :               set_underlying_type (inner);
    8970       128304 :               if (tdef_flags & 2)
    8971              :                 {
    8972              :                   /* Match instantiate_alias_template's handling.  */
    8973        32585 :                   tree type = TREE_TYPE (inner);
    8974        32585 :                   TYPE_DEPENDENT_P (type) = true;
    8975        32585 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8976        32585 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8977              :                 }
    8978              :             }
    8979              :         }
    8980              : 
    8981       767026 :       if (inner_tag)
    8982              :         /* Set the TEMPLATE_DECL's type.  */
    8983       179808 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8984              : 
    8985              :       /* Redetermine whether we need to import or export this declaration
    8986              :          for this TU.  But for extern templates we know we must import:
    8987              :          they'll be defined in a different TU.
    8988              :          FIXME: How do dllexport and dllimport interact across a module?
    8989              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    8990              :          May have to revisit?  */
    8991       767026 :       if (type
    8992        96960 :           && CLASS_TYPE_P (type)
    8993        83515 :           && TYPE_LANG_SPECIFIC (type)
    8994       850541 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    8995          766 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    8996          766 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    8997              :         {
    8998        82801 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    8999        82801 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9000              :         }
    9001              : 
    9002              :       /* Add to specialization tables now that constraints etc are
    9003              :          added.  */
    9004       767026 :       if (mk == MK_partial)
    9005              :         {
    9006         4603 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9007         4603 :           spec.spec = is_type ? type : inner;
    9008         4603 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9009              :         }
    9010       762423 :       else if (mk & MK_template_mask)
    9011              :         {
    9012       221453 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9013       221453 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9014       221453 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9015              :         }
    9016              : 
    9017       767026 :       if (NAMESPACE_SCOPE_P (decl)
    9018       160776 :           && (mk == MK_named || mk == MK_unique
    9019       160776 :               || mk == MK_enum || mk == MK_friend_spec)
    9020       839350 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9021        72225 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9022              : 
    9023       767026 :       if (DECL_ARTIFICIAL (decl)
    9024       208397 :           && TREE_CODE (decl) == FUNCTION_DECL
    9025        22707 :           && !DECL_TEMPLATE_INFO (decl)
    9026        22395 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9027        22223 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9028       768561 :           && !DECL_THUNK_P (decl))
    9029              :         /* A new implicit member function, when the class is
    9030              :            complete.  This means the importee declared it, and
    9031              :            we must now add it to the class.  Note that implicit
    9032              :            member fns of template instantiations do not themselves
    9033              :            look like templates.  */
    9034          961 :         if (!install_implicit_member (inner))
    9035            0 :           set_overrun ();
    9036              : 
    9037              :       /* When importing a TLS wrapper from a header unit, we haven't
    9038              :          actually emitted its definition yet. Remember it so we can
    9039              :          do this later.  */
    9040       767026 :       if (state->is_header ()
    9041       767026 :           && decl_tls_wrapper_p (decl))
    9042            6 :         note_vague_linkage_fn (decl);
    9043              : 
    9044              :       /* Apply relevant attributes.
    9045              :          FIXME should probably use cplus_decl_attributes for this,
    9046              :          but it's not yet ready for modules.  */
    9047              : 
    9048       767026 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9049       462018 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9050              :           {
    9051            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9052            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9053              :           }
    9054              : 
    9055              :       /* Setup aliases for the declaration.  */
    9056       767026 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9057              :         {
    9058            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9059            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9060            3 :           assemble_alias (decl, alias);
    9061              :         }
    9062              :     }
    9063              :   else
    9064              :     {
    9065              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9066              :          be to the EXISTING's structure.  Frob it to point to its
    9067              :          own other structures, so loading its definition will alter
    9068              :          it, and not the existing decl.  */
    9069       438321 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9070              : 
    9071       436559 :       if (inner_tag)
    9072       145632 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9073              : 
    9074       436559 :       if (type)
    9075              :         {
    9076              :           /* Point at the to-be-discarded type & decl.  */
    9077        77351 :           TYPE_NAME (type) = inner;
    9078        77351 :           TREE_TYPE (inner) = type;
    9079              : 
    9080       154457 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9081        77351 :           if (stub_decl)
    9082          245 :             TREE_TYPE (stub_decl) = type;
    9083              : 
    9084        77351 :           tree etype = TREE_TYPE (existing);
    9085              : 
    9086              :           /* Handle separate declarations with different attributes.  */
    9087        77351 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9088        77351 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9089        77351 :           check_abi_tags (existing, decl, eattr, dattr);
    9090              :           // TODO: handle other conflicting type attributes
    9091        77351 :           eattr = merge_attributes (eattr, dattr);
    9092              : 
    9093              :           /* When merging a partial specialisation, the existing decl may have
    9094              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9095              :              equality to ensure is_matching_decl doesn't get confused.  */
    9096        77351 :           if ((spec_flags & 2)
    9097        77351 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9098            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9099              :         }
    9100              : 
    9101       436559 :       if (inner_tag)
    9102              :         /* Set the TEMPLATE_DECL's type.  */
    9103       145632 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9104              : 
    9105       436559 :       if (!is_matching_decl (existing, decl, is_typedef))
    9106           42 :         unmatched_duplicate (existing);
    9107              : 
    9108       436559 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9109              :         {
    9110       200028 :           tree e_inner = STRIP_TEMPLATE (existing);
    9111       200028 :           for (auto parm = DECL_ARGUMENTS (inner);
    9112       598309 :                parm; parm = DECL_CHAIN (parm))
    9113       398281 :             DECL_CONTEXT (parm) = e_inner;
    9114              :         }
    9115              : 
    9116              :       /* And our result is the existing node.  */
    9117       436559 :       decl = existing;
    9118              :     }
    9119              : 
    9120      1203585 :   if (mk == MK_friend_spec)
    9121              :     {
    9122            0 :       tree e = match_mergeable_specialization (true, &spec);
    9123            0 :       if (!e)
    9124              :         {
    9125            0 :           spec.spec = inner;
    9126            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9127              :         }
    9128            0 :       else if (e != existing)
    9129            0 :         set_overrun ();
    9130              :     }
    9131              : 
    9132      1203585 :   if (is_typedef)
    9133              :     {
    9134              :       /* Insert the type into the array now.  */
    9135       212699 :       tag = insert (TREE_TYPE (decl));
    9136       212699 :       dump (dumper::TREE)
    9137          247 :         && dump ("Cloned:%d typedef %C:%N",
    9138          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9139              :     }
    9140              : 
    9141      1203585 :   unused = saved_unused;
    9142              : 
    9143      1203585 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9144              :     {
    9145       104029 :       unsigned flags = u ();
    9146              : 
    9147       104029 :       if (is_new)
    9148              :         {
    9149        63749 :           bool cloned_p = flags & 1;
    9150        63849 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9151              :                                        decl, cloned_p ? "" : "not ");
    9152        63749 :           if (cloned_p)
    9153              :             {
    9154              :               /* Update the member vec, if there is one (we're in a different
    9155              :                  cluster to the class defn) and this isn't a primary template
    9156              :                  specialization (as in tsubst_function_decl).  */
    9157        45270 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9158        45270 :                          && !primary_template_specialization_p (decl));
    9159        45270 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9160              :             }
    9161              :         }
    9162              :     }
    9163              : 
    9164      1203585 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9165              :     {
    9166          160 :       enum tls_model model = tls_model (u ());
    9167          160 :       if (is_new)
    9168          140 :         set_decl_tls_model (decl, model);
    9169              :     }
    9170              : 
    9171      1203585 :   if (!NAMESPACE_SCOPE_P (inner)
    9172       897110 :       && ((TREE_CODE (inner) == TYPE_DECL
    9173       209111 :            && !is_typedef
    9174        37387 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9175       859723 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9176      1592470 :       && u ())
    9177            3 :     read_definition (decl);
    9178              : 
    9179              :   return decl;
    9180              : }
    9181              : 
    9182              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9183              :    index.  */
    9184              : 
    9185              : static unsigned
    9186         1457 : get_field_ident (tree ctx, tree decl)
    9187              : {
    9188         1457 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9189              :                        || !DECL_NAME (decl)
    9190              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9191              : 
    9192         1457 :   unsigned ix = 0;
    9193         1457 :   for (tree fields = TYPE_FIELDS (ctx);
    9194        10669 :        fields; fields = DECL_CHAIN (fields))
    9195              :     {
    9196        10669 :       if (fields == decl)
    9197         1457 :         return ix;
    9198              : 
    9199         9212 :       if (DECL_CONTEXT (fields) == ctx
    9200         9212 :           && (TREE_CODE (fields) == USING_DECL
    9201         9191 :               || (TREE_CODE (fields) == FIELD_DECL
    9202           91 :                   && (!DECL_NAME (fields)
    9203           48 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9204              :         /* Count this field.  */
    9205           46 :         ix++;
    9206              :     }
    9207            0 :   gcc_unreachable ();
    9208              : }
    9209              : 
    9210              : static tree
    9211         1023 : lookup_field_ident (tree ctx, unsigned ix)
    9212              : {
    9213         1023 :   for (tree fields = TYPE_FIELDS (ctx);
    9214         7769 :        fields; fields = DECL_CHAIN (fields))
    9215         7769 :     if (DECL_CONTEXT (fields) == ctx
    9216         7769 :         && (TREE_CODE (fields) == USING_DECL
    9217         7757 :             || (TREE_CODE (fields) == FIELD_DECL
    9218         1086 :                 && (!DECL_NAME (fields)
    9219           25 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9220         1067 :       if (!ix--)
    9221              :         return fields;
    9222              : 
    9223              :   return NULL_TREE;
    9224              : }
    9225              : 
    9226              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9227              :    Return true if we should write this decl by value.  */
    9228              : 
    9229              : bool
    9230     15492188 : trees_out::decl_node (tree decl, walk_kind ref)
    9231              : {
    9232     15492188 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9233              :                        && DECL_CONTEXT (decl));
    9234              : 
    9235     15492188 :   if (ref == WK_value)
    9236              :     {
    9237      1503360 :       depset *dep = dep_hash->find_dependency (decl);
    9238      1503360 :       decl_value (decl, dep);
    9239      1503360 :       return false;
    9240              :     }
    9241              : 
    9242     13988828 :   switch (TREE_CODE (decl))
    9243              :     {
    9244              :     default:
    9245              :       break;
    9246              : 
    9247      1483019 :     case FUNCTION_DECL:
    9248      1483019 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9249              :       break;
    9250              : 
    9251              :     case RESULT_DECL:
    9252              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9253              :          referenced when we're inside the function itself.  */
    9254              :       return true;
    9255              : 
    9256       223835 :     case PARM_DECL:
    9257       223835 :       {
    9258       223835 :         if (streaming_p ())
    9259        96669 :           i (tt_parm);
    9260       223835 :         tree_node (DECL_CONTEXT (decl));
    9261              : 
    9262              :         /* That must have put this in the map.  */
    9263       223835 :         walk_kind ref = ref_node (decl);
    9264       223835 :         if (ref != WK_none)
    9265              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9266              :           // template this was instantiated from, for instance
    9267              :           // deferred noexcept and default parms, or references
    9268              :           // to parms from earlier forward-decls (PR c++/119608).
    9269              :           //
    9270              :           // Currently we'll end up cloning those bits of tree.
    9271              :           // It would be nice to reference those specific nodes.
    9272              :           // I think putting those things in the map when we
    9273              :           // reference their template by name.
    9274              :           //
    9275              :           // See the note in add_indirects.
    9276              :           return true;
    9277              : 
    9278            0 :         if (streaming_p ())
    9279            0 :           dump (dumper::TREE)
    9280            0 :             && dump ("Wrote %s reference %N",
    9281            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9282              :                      decl);
    9283              :       }
    9284              :       return false;
    9285              : 
    9286              :     case IMPORTED_DECL:
    9287              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9288              :          originates from the fortran FE, and has nothing to do with
    9289              :          C++ modules.  */
    9290              :       return true;
    9291              : 
    9292              :     case LABEL_DECL:
    9293              :       return true;
    9294              : 
    9295        83904 :     case CONST_DECL:
    9296        83904 :       {
    9297              :         /* If I end up cloning enum decls, implementing C++20 using
    9298              :            E::v, this will need tweaking.   */
    9299        83904 :         if (streaming_p ())
    9300        19693 :           i (tt_enum_decl);
    9301        83904 :         tree ctx = DECL_CONTEXT (decl);
    9302        83904 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9303        83904 :         tree_node (ctx);
    9304        83904 :         tree_node (DECL_NAME (decl));
    9305              : 
    9306        83904 :         int tag = insert (decl);
    9307        83904 :         if (streaming_p ())
    9308        19693 :           dump (dumper::TREE)
    9309           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9310              :         return false;
    9311              :       }
    9312        32403 :       break;
    9313              : 
    9314        32403 :     case USING_DECL:
    9315        32403 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9316              :         break;
    9317              :       /* FALLTHROUGH  */
    9318              : 
    9319       210257 :     case FIELD_DECL:
    9320       210257 :       {
    9321       210257 :         if (streaming_p ())
    9322        11799 :           i (tt_data_member);
    9323              : 
    9324       210257 :         tree ctx = DECL_CONTEXT (decl);
    9325       210257 :         tree_node (ctx);
    9326              : 
    9327       210257 :         tree name = NULL_TREE;
    9328              : 
    9329       210257 :         if (TREE_CODE (decl) == USING_DECL)
    9330              :           ;
    9331              :         else
    9332              :           {
    9333       208720 :             name = DECL_NAME (decl);
    9334       405103 :             if (name && IDENTIFIER_ANON_P (name))
    9335              :               name = NULL_TREE;
    9336              :           }
    9337              : 
    9338       210257 :         tree_node (name);
    9339       210257 :         if (!name && streaming_p ())
    9340              :           {
    9341         1457 :             unsigned ix = get_field_ident (ctx, decl);
    9342         1457 :             u (ix);
    9343              :           }
    9344              : 
    9345       210257 :         int tag = insert (decl);
    9346       210257 :         if (streaming_p ())
    9347        11799 :           dump (dumper::TREE)
    9348           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9349              :         return false;
    9350              :       }
    9351       633206 :       break;
    9352              : 
    9353       633206 :     case VAR_DECL:
    9354       633206 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9355       633206 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9356              :         {
    9357              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9358         4080 :           tree ctx = CP_DECL_CONTEXT (decl);
    9359         4080 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9360         4185 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9361         4185 :             if (vtable == decl)
    9362              :               {
    9363         4080 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9364         4080 :                 if (streaming_p ())
    9365              :                   {
    9366           43 :                     u (tt_vtable);
    9367           43 :                     u (ix);
    9368           43 :                     dump (dumper::TREE)
    9369            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9370              :                   }
    9371         4080 :                 tree_node (ctx);
    9372         4080 :                 return false;
    9373              :               }
    9374              :           gcc_unreachable ();
    9375              :         }
    9376              : 
    9377       629126 :       if (DECL_TINFO_P (decl))
    9378              :         {
    9379         8423 :         tinfo:
    9380              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9381        15499 :           bool is_var = VAR_P (decl);
    9382        15499 :           tree type = TREE_TYPE (decl);
    9383        15499 :           unsigned ix = get_pseudo_tinfo_index (type);
    9384        15499 :           if (streaming_p ())
    9385              :             {
    9386         9778 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9387         6932 :               u (ix);
    9388              :             }
    9389              : 
    9390        15499 :           if (is_var)
    9391              :             {
    9392              :               /* We also need the type it is for and mangled name, so
    9393              :                  the reader doesn't need to complete the type (which
    9394              :                  would break section ordering).  The type it is for is
    9395              :                  stashed on the name's TREE_TYPE.  */
    9396         8423 :               tree name = DECL_NAME (decl);
    9397         8423 :               tree_node (name);
    9398         8423 :               type = TREE_TYPE (name);
    9399         8423 :               tree_node (type);
    9400              :             }
    9401              : 
    9402        15499 :           int tag = insert (decl);
    9403        15499 :           if (streaming_p ())
    9404         6932 :             dump (dumper::TREE)
    9405           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9406              :                        tag, ix, type);
    9407              : 
    9408        15499 :           if (!is_var)
    9409              :             {
    9410         7076 :               tag = insert (type);
    9411         7076 :               if (streaming_p ())
    9412         2846 :                 dump (dumper::TREE)
    9413            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9414              :             }
    9415        15499 :           return false;
    9416              :         }
    9417              : 
    9418       620703 :       if (DECL_NTTP_OBJECT_P (decl))
    9419              :         {
    9420              :           /* A NTTP parm object.  */
    9421           48 :           if (streaming_p ())
    9422           12 :             i (tt_nttp_var);
    9423           48 :           tree_node (tparm_object_argument (decl));
    9424           48 :           tree_node (DECL_NAME (decl));
    9425           48 :           int tag = insert (decl);
    9426           48 :           if (streaming_p ())
    9427           12 :             dump (dumper::TREE)
    9428            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9429           48 :           return false;
    9430              :         }
    9431              : 
    9432              :       break;
    9433              : 
    9434      4800042 :     case TYPE_DECL:
    9435      4800042 :       if (DECL_TINFO_P (decl))
    9436         7076 :         goto tinfo;
    9437              :       break;
    9438              :     }
    9439              : 
    9440     12713388 :   if (DECL_THUNK_P (decl))
    9441              :     {
    9442              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9443              :          then thunk-specific key info.  */
    9444            0 :       if (streaming_p ())
    9445              :         {
    9446            0 :           i (tt_thunk);
    9447            0 :           i (THUNK_FIXED_OFFSET (decl));
    9448              :         }
    9449              : 
    9450              :       tree target = decl;
    9451            0 :       while (DECL_THUNK_P (target))
    9452            0 :         target = THUNK_TARGET (target);
    9453            0 :       tree_node (target);
    9454            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9455            0 :       int tag = insert (decl);
    9456            0 :       if (streaming_p ())
    9457            0 :         dump (dumper::TREE)
    9458            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9459            0 :       return false;
    9460              :     }
    9461              : 
    9462     12713388 :   if (DECL_CLONED_FUNCTION_P (decl))
    9463              :     {
    9464       441632 :       tree target = get_clone_target (decl);
    9465       441632 :       if (streaming_p ())
    9466       209430 :         i (tt_clone_ref);
    9467              : 
    9468       441632 :       tree_node (target);
    9469       441632 :       tree_node (DECL_NAME (decl));
    9470       441632 :       if (DECL_VIRTUAL_P (decl))
    9471        30257 :         tree_node (DECL_VINDEX (decl));
    9472       441632 :       int tag = insert (decl);
    9473       441632 :       if (streaming_p ())
    9474       209430 :         dump (dumper::TREE)
    9475          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9476       441632 :       return false;
    9477              :     }
    9478              : 
    9479              :   /* Everything left should be a thing that is in the entity table.
    9480              :      Mostly things that can be defined outside of their (original
    9481              :      declaration) context.  */
    9482     12271756 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9483              :                        || VAR_P (decl)
    9484              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9485              :                        || TREE_CODE (decl) == TYPE_DECL
    9486              :                        || TREE_CODE (decl) == USING_DECL
    9487              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9488              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9489              : 
    9490     12271756 :   int use_tpl = -1;
    9491     12271756 :   tree ti = node_template_info (decl, use_tpl);
    9492     12271756 :   tree tpl = NULL_TREE;
    9493              : 
    9494              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9495              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9496              :      (some) friends, so we need to check that.  */
    9497              :   // FIXME: Should local friend template specializations be by value?
    9498              :   // They don't get idents so we'll never know they're imported, but I
    9499              :   // think we can only reach them from the TU that defines the
    9500              :   // befriending class?
    9501      4481324 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9502     16753014 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9503              :     {
    9504              :       tpl = TI_TEMPLATE (ti);
    9505      1133073 :     partial_template:
    9506      1133073 :       if (streaming_p ())
    9507              :         {
    9508         3737 :           i (tt_template);
    9509         3737 :           dump (dumper::TREE)
    9510            9 :             && dump ("Writing implicit template %C:%N%S",
    9511            9 :                      TREE_CODE (tpl), tpl, tpl);
    9512              :         }
    9513      1133073 :       tree_node (tpl);
    9514              : 
    9515              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9516              :          if it wasn't TU-local.  */
    9517      1133073 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9518              :         {
    9519      1133046 :           gcc_checking_assert (TREE_VISITED (decl));
    9520      1133046 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9521       585537 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9522              :         }
    9523              :       return false;
    9524              :     }
    9525              : 
    9526     11254495 :   tree ctx = CP_DECL_CONTEXT (decl);
    9527     11254495 :   depset *dep = NULL;
    9528     11254495 :   if (streaming_p ())
    9529      1879149 :     dep = dep_hash->find_dependency (decl);
    9530      9375346 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9531       372302 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9532       336522 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9533      9675365 :            || (DECL_LANG_SPECIFIC (decl)
    9534       159915 :                && DECL_MODULE_IMPORT_P (decl)))
    9535              :     {
    9536      9075327 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9537       701314 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9538      9075327 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9539      9075327 :       dep = dep_hash->add_dependency (decl, kind);
    9540              :     }
    9541              : 
    9542     10954476 :   if (!dep || dep->is_tu_local ())
    9543              :     {
    9544              :       /* Some internal entity of context.  Do by value.  */
    9545       578551 :       decl_value (decl, dep);
    9546       578551 :       return false;
    9547              :     }
    9548              : 
    9549     10675944 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9550              :     {
    9551              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9552              :          Write the partial specialization's template.  */
    9553       115812 :       depset *redirect = dep->deps[0];
    9554       115812 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9555       115812 :       tpl = redirect->get_entity ();
    9556       115812 :       goto partial_template;
    9557              :     }
    9558              : 
    9559     10560132 :   if (streaming_p ())
    9560              :     {
    9561              :       /* Locate the entity.  */
    9562      1600877 :       unsigned index = dep->cluster;
    9563      1600877 :       unsigned import = 0;
    9564              : 
    9565      1600877 :       if (dep->is_import ())
    9566        10355 :         import = dep->section;
    9567      1590522 :       else if (CHECKING_P)
    9568              :         /* It should be what we put there.  */
    9569      1590522 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9570              : 
    9571              : #if CHECKING_P
    9572        10355 :       gcc_assert (!import || importedness >= 0);
    9573              : #endif
    9574      1600877 :       i (tt_entity);
    9575      1600877 :       u (import);
    9576      1600877 :       u (index);
    9577              :     }
    9578              : 
    9579     10560132 :   int tag = insert (decl);
    9580     10560132 :   if (streaming_p () && dump (dumper::TREE))
    9581              :     {
    9582          529 :       char const *kind = "import";
    9583          529 :       module_state *from = this_module ();
    9584          529 :       if (dep->is_import ())
    9585              :         /* Rediscover the unremapped index.  */
    9586           78 :         from = import_entity_module (import_entity_index (decl));
    9587              :       else
    9588              :         {
    9589          451 :           tree o = get_originating_module_decl (decl);
    9590          451 :           o = STRIP_TEMPLATE (o);
    9591          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9592          451 :                   ? "purview" : "GMF");
    9593              :         }
    9594          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9595          529 :             tag, TREE_CODE (decl), decl, from);
    9596              :     }
    9597              : 
    9598     10560132 :   add_indirects (decl);
    9599              : 
    9600     10560132 :   return false;
    9601              : }
    9602              : 
    9603              : void
    9604     12595711 : trees_out::type_node (tree type)
    9605              : {
    9606     12595711 :   gcc_assert (TYPE_P (type));
    9607              : 
    9608     12595711 :   tree root = (TYPE_NAME (type)
    9609     12595711 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9610     12595711 :   gcc_checking_assert (root);
    9611              : 
    9612     12595711 :   if (type != root)
    9613              :     {
    9614      2823094 :       if (streaming_p ())
    9615       582400 :         i (tt_variant_type);
    9616      2823094 :       tree_node (root);
    9617              : 
    9618      2823094 :       int flags = -1;
    9619              : 
    9620      2823094 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9621      2823094 :           || TREE_CODE (type) == METHOD_TYPE)
    9622              :         {
    9623       661834 :           int quals = type_memfn_quals (type);
    9624       661834 :           int rquals = type_memfn_rqual (type);
    9625       661834 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9626       661834 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9627              : 
    9628       661834 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9629        21221 :               || rquals != type_memfn_rqual (root)
    9630        15355 :               || quals != type_memfn_quals (root)
    9631       677171 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9632       661834 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9633              :         }
    9634              :       else
    9635              :         {
    9636      2161260 :           if (TYPE_USER_ALIGN (type))
    9637        24066 :             flags = TYPE_ALIGN_RAW (type);
    9638              :         }
    9639              : 
    9640      2823094 :       if (streaming_p ())
    9641       582400 :         i (flags);
    9642              : 
    9643      2823094 :       if (flags < 0)
    9644              :         ;
    9645       685900 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9646       685900 :                || TREE_CODE (type) == METHOD_TYPE)
    9647              :         {
    9648       661834 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9649       661834 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9650        21221 :             raises = error_mark_node;
    9651       661834 :           tree_node (raises);
    9652              :         }
    9653              : 
    9654              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9655              :          variants should all have the same set of attributes.  */
    9656      2823094 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9657              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9658              : 
    9659      2823094 :       if (streaming_p ())
    9660              :         {
    9661              :           /* Qualifiers.  */
    9662       582400 :           int rquals = cp_type_quals (root);
    9663       582400 :           int quals = cp_type_quals (type);
    9664       582400 :           if (quals == rquals)
    9665       269644 :             quals = -1;
    9666       582400 :           i (quals);
    9667              :         }
    9668              : 
    9669      2823094 :       if (ref_node (type) != WK_none)
    9670              :         {
    9671      2823094 :           int tag = insert (type);
    9672      2823094 :           if (streaming_p ())
    9673              :             {
    9674       582400 :               i (0);
    9675       582400 :               dump (dumper::TREE)
    9676          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9677              :             }
    9678              :         }
    9679      2823094 :       return;
    9680              :     }
    9681              : 
    9682      9772617 :   if (tree name = TYPE_NAME (type))
    9683      3870549 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9684      3041022 :         || DECL_TEMPLATE_PARM_P (name)
    9685      2071253 :         || TREE_CODE (type) == RECORD_TYPE
    9686       360550 :         || TREE_CODE (type) == UNION_TYPE
    9687      4223090 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9688              :       {
    9689      3620094 :         gcc_checking_assert (DECL_P (name));
    9690              : 
    9691              :         /* We can meet template parms that we didn't meet in the
    9692              :            tpl_parms walk, because we're referring to a derived type
    9693              :            that was previously constructed from equivalent template
    9694              :            parms. */
    9695      3620094 :         if (streaming_p ())
    9696              :           {
    9697       247455 :             i (tt_typedef_type);
    9698       247455 :             dump (dumper::TREE)
    9699           59 :               && dump ("Writing %stypedef %C:%N",
    9700           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9701           59 :                        TREE_CODE (name), name);
    9702              :           }
    9703      3620094 :         tree_node (name);
    9704      3620094 :         if (streaming_p ())
    9705       247455 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9706           59 :                                        TREE_CODE (name), name, name);
    9707              : 
    9708              :         /* We'll have either visited this type or have newly discovered
    9709              :            that it's TU-local; either way we won't need to visit it again.  */
    9710      3620094 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9711      3620094 :         return;
    9712              :       }
    9713              : 
    9714      6152523 :   if (TYPE_PTRMEMFUNC_P (type))
    9715              :     {
    9716              :       /* This is a distinct type node, masquerading as a structure. */
    9717         5449 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9718         5449 :       if (streaming_p ())
    9719         1535 :         i (tt_ptrmem_type);
    9720         5449 :       tree_node (fn_type);
    9721         5449 :       int tag = insert (type);
    9722         5449 :       if (streaming_p ())
    9723         1538 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9724         5449 :       return;
    9725              :     }
    9726              : 
    9727      6147074 :   if (streaming_p ())
    9728              :     {
    9729      1947609 :       u (tt_derived_type);
    9730      1947609 :       u (TREE_CODE (type));
    9731              :     }
    9732              : 
    9733      6147074 :   tree_node (TREE_TYPE (type));
    9734      6147074 :   switch (TREE_CODE (type))
    9735              :     {
    9736            0 :     default:
    9737              :       /* We should never meet a type here that is indescribable in
    9738              :          terms of other types.  */
    9739            0 :       gcc_unreachable ();
    9740              : 
    9741        93282 :     case ARRAY_TYPE:
    9742        93282 :       tree_node (TYPE_DOMAIN (type));
    9743        93282 :       if (streaming_p ())
    9744              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9745              :            already set.  */
    9746        29804 :         u (TYPE_DEPENDENT_P (type));
    9747              :       break;
    9748              : 
    9749              :     case COMPLEX_TYPE:
    9750              :       /* No additional data.  */
    9751              :       break;
    9752              : 
    9753           12 :     case BOOLEAN_TYPE:
    9754              :       /* A non-standard boolean type.  */
    9755           12 :       if (streaming_p ())
    9756            6 :         u (TYPE_PRECISION (type));
    9757              :       break;
    9758              : 
    9759        86496 :     case INTEGER_TYPE:
    9760        86496 :       if (TREE_TYPE (type))
    9761              :         {
    9762              :           /* A range type (representing an array domain).  */
    9763        80944 :           tree_node (TYPE_MIN_VALUE (type));
    9764        80944 :           tree_node (TYPE_MAX_VALUE (type));
    9765              :         }
    9766              :       else
    9767              :         {
    9768              :           /* A new integral type (representing a bitfield).  */
    9769         5552 :           if (streaming_p ())
    9770              :             {
    9771         1249 :               unsigned prec = TYPE_PRECISION (type);
    9772         1249 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9773              : 
    9774         1249 :               u ((prec << 1) | unsigned_p);
    9775              :             }
    9776              :         }
    9777              :       break;
    9778              : 
    9779      1344089 :     case METHOD_TYPE:
    9780      1344089 :     case FUNCTION_TYPE:
    9781      1344089 :       {
    9782      1344089 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9783              : 
    9784      1344089 :         tree arg_types = TYPE_ARG_TYPES (type);
    9785      1344089 :         if (TREE_CODE (type) == METHOD_TYPE)
    9786              :           {
    9787       867265 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9788       867265 :             arg_types = TREE_CHAIN (arg_types);
    9789              :           }
    9790      1344089 :         tree_node (arg_types);
    9791              :       }
    9792      1344089 :       break;
    9793              : 
    9794         1587 :     case OFFSET_TYPE:
    9795         1587 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9796         1587 :       break;
    9797              : 
    9798              :     case POINTER_TYPE:
    9799              :       /* No additional data.  */
    9800              :       break;
    9801              : 
    9802      1055193 :     case REFERENCE_TYPE:
    9803      1055193 :       if (streaming_p ())
    9804       232051 :         u (TYPE_REF_IS_RVALUE (type));
    9805              :       break;
    9806              : 
    9807      1243325 :     case DECLTYPE_TYPE:
    9808      1243325 :     case TYPEOF_TYPE:
    9809      1243325 :     case DEPENDENT_OPERATOR_TYPE:
    9810      1243325 :       tree_node (TYPE_VALUES_RAW (type));
    9811      1243325 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9812              :         /* We stash a whole bunch of things into decltype's
    9813              :            flags.  */
    9814       101919 :         if (streaming_p ())
    9815        34593 :           tree_node_bools (type);
    9816              :       break;
    9817              : 
    9818         8469 :     case TRAIT_TYPE:
    9819         8469 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9820         8469 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9821         8469 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9822         8469 :       break;
    9823              : 
    9824              :     case TYPE_ARGUMENT_PACK:
    9825              :       /* No additional data.  */
    9826              :       break;
    9827              : 
    9828       208030 :     case TYPE_PACK_EXPANSION:
    9829       208030 :       if (streaming_p ())
    9830        84017 :         u (PACK_EXPANSION_LOCAL_P (type));
    9831       416060 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9832       208030 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9833       208030 :       break;
    9834              : 
    9835           40 :     case PACK_INDEX_TYPE:
    9836           40 :       tree_node (PACK_INDEX_PACK (type));
    9837           40 :       tree_node (PACK_INDEX_INDEX (type));
    9838           40 :       break;
    9839              : 
    9840       252105 :     case TYPENAME_TYPE:
    9841       252105 :       {
    9842       252105 :         tree_node (TYPE_CONTEXT (type));
    9843       252105 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9844       252105 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9845       252105 :         if (streaming_p ())
    9846        87137 :           u (get_typename_tag (type));
    9847              :         }
    9848              :       break;
    9849              : 
    9850          258 :     case UNBOUND_CLASS_TEMPLATE:
    9851          258 :       {
    9852          258 :         tree decl = TYPE_NAME (type);
    9853          258 :         tree_node (DECL_CONTEXT (decl));
    9854          258 :         tree_node (DECL_NAME (decl));
    9855          258 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9856              :       }
    9857          258 :       break;
    9858              : 
    9859           42 :     case VECTOR_TYPE:
    9860           42 :       if (streaming_p ())
    9861              :         {
    9862           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9863           42 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9864           21 :             wu (nunits.coeffs[ix]);
    9865              :         }
    9866              :       break;
    9867              : 
    9868              :     case META_TYPE:
    9869              :       /* No additional data.  */
    9870              :       break;
    9871              : 
    9872            4 :     case SPLICE_SCOPE:
    9873            4 :       if (streaming_p ())
    9874            2 :         u (SPLICE_SCOPE_TYPE_P (type));
    9875            4 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9876            4 :       break;
    9877              :     }
    9878              : 
    9879      6147074 :   tree_node (TYPE_ATTRIBUTES (type));
    9880              : 
    9881              :   /* We may have met the type during emitting the above.  */
    9882      6147074 :   if (ref_node (type) != WK_none)
    9883              :     {
    9884      5576371 :       int tag = insert (type);
    9885      5576371 :       if (streaming_p ())
    9886              :         {
    9887      1680414 :           i (0);
    9888      1680414 :           dump (dumper::TREE)
    9889          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9890              :         }
    9891              :     }
    9892              : 
    9893              :   return;
    9894              : }
    9895              : 
    9896              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9897              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9898              : 
    9899              : void
    9900     38322968 : trees_out::tree_value (tree t)
    9901              : {
    9902              :   /* We should never be writing a type by value.  tree_type should
    9903              :      have streamed it, or we're going via its TYPE_DECL.  */
    9904     38322968 :   gcc_checking_assert (!TYPE_P (t));
    9905              : 
    9906     38322968 :   if (DECL_P (t))
    9907              :     /* No template, type, var or function, except anonymous
    9908              :        non-context vars and types.  */
    9909       991969 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9910              :                           && (TREE_CODE (t) != TYPE_DECL
    9911              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9912              :                           && (TREE_CODE (t) != VAR_DECL
    9913              :                               || ((!DECL_NAME (t)
    9914              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9915              :                                   && !DECL_CONTEXT (t)))
    9916              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9917              : 
    9918     57889486 :   if (is_initial_scan () && EXPR_P (t))
    9919      7727990 :     dep_hash->add_dependent_adl_entities (t);
    9920              : 
    9921     38322968 :   if (streaming_p ())
    9922              :     {
    9923              :       /* A new node -> tt_node.  */
    9924     15181941 :       tree_val_count++;
    9925     15181941 :       i (tt_node);
    9926     15181941 :       start (t);
    9927     15181941 :       tree_node_bools (t);
    9928              :     }
    9929              : 
    9930     38322968 :   if (TREE_CODE (t) == TREE_BINFO)
    9931              :     /* Binfos are decl-like and need merging information.  */
    9932       268938 :     binfo_mergeable (t);
    9933              : 
    9934     38322968 :   int tag = insert (t, WK_value);
    9935     38322968 :   if (streaming_p ())
    9936     15181941 :     dump (dumper::TREE)
    9937         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9938              : 
    9939     38322968 :   int type_tag = 0;
    9940     38322968 :   tree type = NULL_TREE;
    9941     38322968 :   if (TREE_CODE (t) == TYPE_DECL)
    9942              :     {
    9943           28 :       type = TREE_TYPE (t);
    9944              : 
    9945              :       /* We only support a limited set of features for uncontexted types;
    9946              :          these are typically types created in the language-independent
    9947              :          parts of the frontend (such as ubsan).  */
    9948           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9949              :                            && TYPE_MAIN_VARIANT (type) == type
    9950              :                            && TYPE_NAME (type) == t
    9951              :                            && TYPE_STUB_DECL (type) == t
    9952              :                            && !TYPE_VFIELD (type)
    9953              :                            && !TYPE_BINFO (type)
    9954              :                            && !CLASS_TYPE_P (type));
    9955              : 
    9956           28 :       if (streaming_p ())
    9957              :         {
    9958           14 :           start (type);
    9959           14 :           tree_node_bools (type);
    9960              :         }
    9961              : 
    9962           28 :       type_tag = insert (type, WK_value);
    9963           28 :       if (streaming_p ())
    9964           14 :         dump (dumper::TREE)
    9965            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9966            0 :                    TREE_CODE (type), type);
    9967              :     }
    9968              : 
    9969     38322968 :   tree_node_vals (t);
    9970              : 
    9971     38322968 :   if (type)
    9972              :     {
    9973           28 :       tree_node_vals (type);
    9974           28 :       tree_node (TYPE_SIZE (type));
    9975           28 :       tree_node (TYPE_SIZE_UNIT (type));
    9976           28 :       chained_decls (TYPE_FIELDS (type));
    9977           28 :       if (streaming_p ())
    9978           14 :         dump (dumper::TREE)
    9979            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
    9980              :     }
    9981              : 
    9982              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
    9983              :      importers can recreate their value.  */
    9984     38322968 :   if (TREE_CODE (t) == VAR_DECL)
    9985              :     {
    9986          774 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
    9987          774 :       tree_node (DECL_INITIAL (t));
    9988              :     }
    9989              : 
    9990     38322968 :   if (streaming_p ())
    9991     15184764 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
    9992     38322968 : }
    9993              : 
    9994              : tree
    9995     11795534 : trees_in::tree_value ()
    9996              : {
    9997     11795534 :   tree t = start ();
    9998     11795534 :   if (!t || !tree_node_bools (t))
    9999            0 :     return NULL_TREE;
   10000              : 
   10001     11795534 :   tree existing = t;
   10002     11795534 :   if (TREE_CODE (t) == TREE_BINFO)
   10003              :     {
   10004        90589 :       tree type;
   10005        90589 :       unsigned ix = binfo_mergeable (&type);
   10006        90589 :       if (TYPE_BINFO (type))
   10007              :         {
   10008              :           /* We already have a definition, this must be a duplicate.  */
   10009        41431 :           dump (dumper::MERGE)
   10010          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10011        41431 :           existing = TYPE_BINFO (type);
   10012        56730 :           while (existing && ix--)
   10013        15299 :             existing = TREE_CHAIN (existing);
   10014        41431 :           if (existing)
   10015        41431 :             register_duplicate (t, existing);
   10016              :           else
   10017              :             /* Error, mismatch -- diagnose in read_class_def's
   10018              :                checking.  */
   10019              :             existing = t;
   10020              :         }
   10021              :     }
   10022              : 
   10023              :   /* Insert into map.  */
   10024     11795534 :   int tag = insert (existing);
   10025     11795534 :   dump (dumper::TREE)
   10026         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10027              : 
   10028     11795534 :   int type_tag = 0;
   10029     11795534 :   tree type = NULL_TREE;
   10030     11795534 :   if (TREE_CODE (t) == TYPE_DECL)
   10031              :     {
   10032           14 :       type = start ();
   10033           14 :       if (!type || !tree_node_bools (type))
   10034              :         t = NULL_TREE;
   10035              : 
   10036           14 :       type_tag = insert (type);
   10037           14 :       if (t)
   10038           14 :         dump (dumper::TREE)
   10039            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10040              :     }
   10041              : 
   10042              :   if (!t)
   10043              :     {
   10044            0 : bail:
   10045            0 :       back_refs[~tag] = NULL_TREE;
   10046            0 :       if (type_tag)
   10047            0 :         back_refs[~type_tag] = NULL_TREE;
   10048            0 :       set_overrun ();
   10049            0 :       return NULL_TREE;
   10050              :     }
   10051              : 
   10052     11795534 :   if (!tree_node_vals (t))
   10053            0 :     goto bail;
   10054              : 
   10055     11795534 :   if (type)
   10056              :     {
   10057           14 :       if (!tree_node_vals (type))
   10058            0 :         goto bail;
   10059              : 
   10060           14 :       TYPE_SIZE (type) = tree_node ();
   10061           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10062           14 :       TYPE_FIELDS (type) = chained_decls ();
   10063           14 :       if (get_overrun ())
   10064            0 :         goto bail;
   10065              : 
   10066           14 :       dump (dumper::TREE)
   10067            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10068              :     }
   10069              : 
   10070     11795534 :   if (TREE_CODE (t) == VAR_DECL)
   10071              :     {
   10072          348 :       DECL_INITIAL (t) = tree_node ();
   10073          348 :       if (TREE_STATIC (t))
   10074            8 :         varpool_node::finalize_decl (t);
   10075              :     }
   10076              : 
   10077     11795534 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10078     11795534 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10079              :     {
   10080         1930 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10081         1930 :       back_refs[~tag] = existing;
   10082              :     }
   10083              : 
   10084     11799211 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10085              : 
   10086     11795534 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10087              :     {
   10088       594259 :       existing = cache_integer_cst (t, true);
   10089       594259 :       back_refs[~tag] = existing;
   10090              :     }
   10091              : 
   10092              :   return existing;
   10093              : }
   10094              : 
   10095              : /* Whether DECL has a TU-local dependency in the hash.  */
   10096              : 
   10097              : bool
   10098      1133434 : trees_out::has_tu_local_dep (tree decl) const
   10099              : {
   10100              :   /* Only the contexts of fields or enums remember that they're
   10101              :      TU-local.  */
   10102      1133434 :   if (DECL_CONTEXT (decl)
   10103      1133434 :       && (TREE_CODE (decl) == FIELD_DECL
   10104      1133431 :           || TREE_CODE (decl) == CONST_DECL))
   10105            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10106              : 
   10107      1133434 :   depset *dep = dep_hash->find_dependency (decl);
   10108      1133434 :   if (!dep)
   10109              :     {
   10110              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10111              :          which we found was TU-local and gave up early.  */
   10112        14210 :       int use_tpl = -1;
   10113        14210 :       if (tree ti = node_template_info (decl, use_tpl))
   10114         2327 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10115              :     }
   10116              : 
   10117      1133434 :   return dep && dep->is_tu_local ();
   10118              : }
   10119              : 
   10120              : /* If T depends on a TU-local entity, return that decl.  */
   10121              : 
   10122              : tree
   10123          395 : trees_out::find_tu_local_decl (tree t)
   10124              : {
   10125              :   /* We need to have walked all deps first before we can check.  */
   10126          395 :   gcc_checking_assert (!is_initial_scan ());
   10127              : 
   10128          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10129              :     {
   10130          556 :       auto self = (trees_out *)data;
   10131              : 
   10132          556 :       tree decl = NULL_TREE;
   10133          556 :       if (TYPE_P (*tp))
   10134              :         {
   10135              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10136              :              return whether the function type is TU-local.  */
   10137          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10138              :             {
   10139            3 :               *walk_subtrees = 0;
   10140            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10141              :             }
   10142              :           else
   10143          367 :             decl = TYPE_MAIN_DECL (*tp);
   10144              :         }
   10145          186 :       else if (DECL_P (*tp))
   10146              :         decl = *tp;
   10147              : 
   10148          373 :       if (decl)
   10149              :         {
   10150              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10151           59 :           *walk_subtrees = 0;
   10152           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10153              :         }
   10154              :       return NULL_TREE;
   10155              :     };
   10156              : 
   10157              :   /* We need to walk without duplicates so that we step into the pointed-to
   10158              :      types of array types.  */
   10159          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10160              : }
   10161              : 
   10162              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10163              : 
   10164              : static tree
   10165          206 : name_for_tu_local_decl (tree t)
   10166              : {
   10167          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10168          206 :   const char *str = decl_as_string (t, flags);
   10169          206 :   return get_identifier (str);
   10170              : }
   10171              : 
   10172              : /* Stream out tree node T.  We automatically create local back
   10173              :    references, which is essentially a single pass lisp
   10174              :    self-referential structure pretty-printer.  */
   10175              : 
   10176              : void
   10177    327176205 : trees_out::tree_node (tree t)
   10178              : {
   10179    327176205 :   dump.indent ();
   10180    327176205 :   walk_kind ref = ref_node (t);
   10181    327176205 :   if (ref == WK_none)
   10182    250059527 :     goto done;
   10183              : 
   10184              :   /* Find TU-local entities and intercept streaming to instead write a
   10185              :      placeholder value; this way we don't need to emit such decls.
   10186              :      We only need to do this when writing a definition of an entity
   10187              :      that we know names a TU-local entity.  */
   10188     90252159 :   if (!is_initial_scan () && writing_local_entities)
   10189              :     {
   10190          952 :       tree local_decl = NULL_TREE;
   10191          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10192              :         local_decl = t;
   10193              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10194              :          no matter how deep.
   10195              : 
   10196              :          This worsens diagnostics slightly, as we often no longer point
   10197              :          directly to the at-fault entity when instantiating.  However, this
   10198              :          reduces the module size slightly and means that much less of pt.cc
   10199              :          needs to know about us.  */
   10200          848 :       else if (TYPE_P (t))
   10201          142 :         local_decl = find_tu_local_decl (t);
   10202          706 :       else if (EXPR_P (t))
   10203          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10204              : 
   10205          496 :       if (local_decl)
   10206              :         {
   10207          158 :           int tag = insert (t, WK_value);
   10208          158 :           if (streaming_p ())
   10209              :             {
   10210          158 :               tu_local_count++;
   10211          158 :               i (tt_tu_local);
   10212          158 :               dump (dumper::TREE)
   10213            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10214            0 :                          tag, TREE_CODE (t), t);
   10215              :             }
   10216          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10217          158 :           if (state)
   10218          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10219          158 :           goto done;
   10220              :         }
   10221              :     }
   10222              : 
   10223     77116520 :   if (ref != WK_normal)
   10224      1807842 :     goto skip_normal;
   10225              : 
   10226     75308678 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10227              :     {
   10228              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10229              :          tt_internal_id.  */
   10230      8977617 :       int code = tt_id;
   10231      8977617 :       if (IDENTIFIER_ANON_P (t))
   10232        35062 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10233      8942555 :       else if (IDENTIFIER_INTERNAL_P (t))
   10234              :         code = tt_internal_id;
   10235      8942541 :       else if (IDENTIFIER_CONV_OP_P (t))
   10236        13445 :         code = tt_conv_id;
   10237              : 
   10238      8977617 :       if (streaming_p ())
   10239      1745420 :         i (code);
   10240              : 
   10241      8977617 :       if (code == tt_conv_id)
   10242              :         {
   10243        13445 :           tree type = TREE_TYPE (t);
   10244        13445 :           gcc_checking_assert (type || t == conv_op_identifier);
   10245        13445 :           tree_node (type);
   10246              :         }
   10247      8964172 :       else if (code == tt_id && streaming_p ())
   10248      1730685 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10249      7233487 :       else if (code == tt_internal_id && streaming_p ())
   10250            7 :         str (prefix_for_internal_label (t));
   10251              : 
   10252      8977617 :       int tag = insert (t);
   10253      8977617 :       if (streaming_p ())
   10254              :         {
   10255              :           /* We know the ordering of the 5 id tags.  */
   10256      1745420 :           static const char *const kinds[] =
   10257              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10258      1745420 :           dump (dumper::TREE)
   10259         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10260         1071 :                      kinds[code - tt_id],
   10261            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10262              :         }
   10263      8977617 :       goto done;
   10264              :     }
   10265              : 
   10266     66331061 :   if (TREE_CODE (t) == TREE_BINFO)
   10267              :     {
   10268              :       /* A BINFO -> tt_binfo.
   10269              :          We must do this by reference.  We stream the binfo tree
   10270              :          itself when streaming its owning RECORD_TYPE.  That we got
   10271              :          here means the dominating type is not in this SCC.  */
   10272        77759 :       if (streaming_p ())
   10273         2529 :         i (tt_binfo);
   10274        77759 :       binfo_mergeable (t);
   10275        77759 :       gcc_checking_assert (!TREE_VISITED (t));
   10276        77759 :       int tag = insert (t);
   10277        77759 :       if (streaming_p ())
   10278         2529 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10279        77759 :       goto done;
   10280              :     }
   10281              : 
   10282     66253302 :   if (TREE_CODE (t) == INTEGER_CST
   10283      4740901 :       && !TREE_OVERFLOW (t)
   10284     70994203 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10285              :     {
   10286              :       /* An integral constant of enumeral type.  See if it matches one
   10287              :          of the enumeration values.  */
   10288        45630 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10289       948840 :            values; values = TREE_CHAIN (values))
   10290              :         {
   10291       946546 :           tree decl = TREE_VALUE (values);
   10292       946546 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10293              :             {
   10294        43336 :               if (streaming_p ())
   10295        12490 :                 u (tt_enum_value);
   10296        43336 :               tree_node (decl);
   10297        43378 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10298        43336 :               goto done;
   10299              :             }
   10300              :         }
   10301              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10302              :          node.  */
   10303              :     }
   10304              : 
   10305     66209966 :   if (TYPE_P (t))
   10306              :     {
   10307     12595711 :       type_node (t);
   10308     12595711 :       goto done;
   10309              :     }
   10310              : 
   10311     53614255 :   if (DECL_P (t))
   10312              :     {
   10313     16587738 :       if (DECL_TEMPLATE_PARM_P (t))
   10314              :         {
   10315      2568593 :           tpl_parm_value (t);
   10316      2568593 :           goto done;
   10317              :         }
   10318              : 
   10319     14019145 :       if (!DECL_CONTEXT (t))
   10320              :         {
   10321              :           /* There are a few cases of decls with no context.  We'll write
   10322              :              these by value, but first assert they are cases we expect.  */
   10323        30317 :           gcc_checking_assert (ref == WK_normal);
   10324        30317 :           switch (TREE_CODE (t))
   10325              :             {
   10326            0 :             default: gcc_unreachable ();
   10327              : 
   10328        11688 :             case LABEL_DECL:
   10329              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10330        11688 :               gcc_checking_assert (!DECL_NAME (t));
   10331              :               break;
   10332              : 
   10333          774 :             case VAR_DECL:
   10334              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10335              :                  and internal vars are created by sanitizers and
   10336              :                  __builtin_source_location.  */
   10337          774 :               gcc_checking_assert ((!DECL_NAME (t)
   10338              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10339              :                                    && DECL_ARTIFICIAL (t));
   10340              :               break;
   10341              : 
   10342        17827 :             case PARM_DECL:
   10343              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10344              :                  and an implicit this parm in an NSDMI has no context.  */
   10345        17827 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10346              :                                    || DECL_NAME (t) == this_identifier);
   10347              :               break;
   10348              : 
   10349           28 :             case TYPE_DECL:
   10350              :               /* Some parts of the compiler need internal struct types;
   10351              :                  these types may not have an appropriate context to use.
   10352              :                  Walk the whole type (including its definition) by value.  */
   10353           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10354              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10355              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10356              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10357              :               break;
   10358              :             }
   10359        30317 :           mark_declaration (t, has_definition (t));
   10360        30317 :           goto by_value;
   10361              :         }
   10362              :     }
   10363              : 
   10364     37026517 :  skip_normal:
   10365     52823187 :   if (DECL_P (t) && !decl_node (t, ref))
   10366     14530536 :     goto done;
   10367              : 
   10368              :   /* Otherwise by value */
   10369     38322968 :  by_value:
   10370     38322968 :   tree_value (t);
   10371              : 
   10372    327176205 :  done:
   10373              :   /* And, breath out.  */
   10374    327176205 :   dump.outdent ();
   10375    327176205 : }
   10376              : 
   10377              : /* Stream in a tree node.  */
   10378              : 
   10379              : tree
   10380     96059002 : trees_in::tree_node (bool is_use)
   10381              : {
   10382     96059002 :   if (get_overrun ())
   10383              :     return NULL_TREE;
   10384              : 
   10385     96059002 :   dump.indent ();
   10386     96059002 :   int tag = i ();
   10387     96059002 :   tree res = NULL_TREE;
   10388     96059002 :   switch (tag)
   10389              :     {
   10390     31563485 :     default:
   10391              :       /* backref, pull it out of the map.  */
   10392     31563485 :       res = back_ref (tag);
   10393     31563485 :       break;
   10394              : 
   10395              :     case tt_null:
   10396              :       /* NULL_TREE.  */
   10397              :       break;
   10398              : 
   10399          158 :     case tt_tu_local:
   10400          158 :       {
   10401              :         /* A translation-unit-local entity.  */
   10402          158 :         res = make_node (TU_LOCAL_ENTITY);
   10403          158 :         int tag = insert (res);
   10404              : 
   10405          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10406          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10407          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10408              :       }
   10409              :       break;
   10410              : 
   10411      7403310 :     case tt_fixed:
   10412              :       /* A fixed ref, find it in the fixed_ref array.   */
   10413      7403310 :       {
   10414      7403310 :         unsigned fix = u ();
   10415      7403310 :         if (fix < (*fixed_trees).length ())
   10416              :           {
   10417      7403310 :             res = (*fixed_trees)[fix];
   10418      7403310 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10419         5046 :                                          TREE_CODE (res), res, res);
   10420              :           }
   10421              : 
   10422      7403310 :         if (!res)
   10423            0 :           set_overrun ();
   10424              :       }
   10425              :       break;
   10426              : 
   10427        78629 :     case tt_parm:
   10428        78629 :       {
   10429        78629 :         tree fn = tree_node ();
   10430        78629 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10431        78629 :           res = tree_node ();
   10432        78629 :         if (res)
   10433        78629 :           dump (dumper::TREE)
   10434           21 :             && dump ("Read %s reference %N",
   10435           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10436              :                      res);
   10437              :       }
   10438              :       break;
   10439              : 
   10440     11795534 :     case tt_node:
   10441              :       /* A new node.  Stream it in.  */
   10442     11795534 :       res = tree_value ();
   10443     11795534 :       break;
   10444              : 
   10445      1203585 :     case tt_decl:
   10446              :       /* A new decl.  Stream it in.  */
   10447      1203585 :       res = decl_value ();
   10448      1203585 :       break;
   10449              : 
   10450       421378 :     case tt_tpl_parm:
   10451              :       /* A template parameter.  Stream it in.  */
   10452       421378 :       res = tpl_parm_value ();
   10453       421378 :       break;
   10454              : 
   10455      1222860 :     case tt_id:
   10456              :       /* An identifier node.  */
   10457      1222860 :       {
   10458      1222860 :         size_t l;
   10459      1222860 :         const char *chars = str (&l);
   10460      1222860 :         res = get_identifier_with_length (chars, l);
   10461      1222860 :         int tag = insert (res);
   10462      1222860 :         dump (dumper::TREE)
   10463         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10464              :       }
   10465      1222860 :       break;
   10466              : 
   10467         3220 :     case tt_conv_id:
   10468              :       /* A conversion operator.  Get the type and recreate the
   10469              :          identifier.  */
   10470         3220 :       {
   10471         3220 :         tree type = tree_node ();
   10472         3220 :         if (!get_overrun ())
   10473              :           {
   10474         3220 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10475         3220 :             int tag = insert (res);
   10476         3220 :             dump (dumper::TREE)
   10477           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10478              :           }
   10479              :       }
   10480              :       break;
   10481              : 
   10482         6576 :     case tt_anon_id:
   10483         6576 :     case tt_lambda_id:
   10484              :       /* An anonymous or lambda id.  */
   10485         6576 :       {
   10486         6576 :         res = make_anon_name ();
   10487         6576 :         if (tag == tt_lambda_id)
   10488         3899 :           IDENTIFIER_LAMBDA_P (res) = true;
   10489         6576 :         int tag = insert (res);
   10490         6576 :         dump (dumper::TREE)
   10491            3 :           && dump ("Read %s identifier:%d %N",
   10492            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10493              :       }
   10494              :       break;
   10495              : 
   10496            8 :     case tt_internal_id:
   10497              :       /* An internal label.  */
   10498            8 :       {
   10499            8 :         const char *prefix = str ();
   10500            8 :         res = generate_internal_label (prefix);
   10501            8 :         int tag = insert (res);
   10502            8 :         dump (dumper::TREE)
   10503            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10504              :       }
   10505              :       break;
   10506              : 
   10507       190891 :     case tt_typedef_type:
   10508       190891 :       res = tree_node ();
   10509       190891 :       if (res)
   10510              :         {
   10511       190891 :           dump (dumper::TREE)
   10512           74 :             && dump ("Read %stypedef %C:%N",
   10513           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10514           74 :                      TREE_CODE (res), res);
   10515       190891 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10516       190890 :             res = TREE_TYPE (res);
   10517              :         }
   10518              :       break;
   10519              : 
   10520      1473505 :     case tt_derived_type:
   10521              :       /* A type derived from some other type.  */
   10522      1473505 :       {
   10523      1473505 :         enum tree_code code = tree_code (u ());
   10524      1473505 :         res = tree_node ();
   10525              : 
   10526      1473505 :         switch (code)
   10527              :           {
   10528            0 :           default:
   10529            0 :             set_overrun ();
   10530            0 :             break;
   10531              : 
   10532        20898 :           case ARRAY_TYPE:
   10533        20898 :             {
   10534        20898 :               tree elt_type = res;
   10535        20898 :               tree domain = tree_node ();
   10536        20898 :               int dep = u ();
   10537        20898 :               if (!get_overrun ())
   10538              :                 {
   10539        20898 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10540              :                   /* If we're an array of an incomplete imported type,
   10541              :                      save it for post-processing so that we can attempt
   10542              :                      to complete the type later if it will get a
   10543              :                      definition later in the cluster.  */
   10544        20898 :                   if (!dep
   10545        18044 :                       && !COMPLETE_TYPE_P (elt_type)
   10546           36 :                       && CLASS_TYPE_P (elt_type)
   10547           36 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10548        20934 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10549           36 :                     post_process_type (res);
   10550              :                 }
   10551              :             }
   10552              :             break;
   10553              : 
   10554          258 :           case COMPLEX_TYPE:
   10555          258 :             if (!get_overrun ())
   10556          258 :               res = build_complex_type (res);
   10557              :             break;
   10558              : 
   10559            9 :           case BOOLEAN_TYPE:
   10560            9 :             {
   10561            9 :               unsigned precision = u ();
   10562            9 :               if (!get_overrun ())
   10563            9 :                 res = build_nonstandard_boolean_type (precision);
   10564              :             }
   10565              :             break;
   10566              : 
   10567        19143 :           case INTEGER_TYPE:
   10568        19143 :             if (res)
   10569              :               {
   10570              :                 /* A range type (representing an array domain).  */
   10571        18121 :                 tree min = tree_node ();
   10572        18121 :                 tree max = tree_node ();
   10573              : 
   10574        18121 :                 if (!get_overrun ())
   10575        18121 :                   res = build_range_type (res, min, max);
   10576              :               }
   10577              :             else
   10578              :               {
   10579              :                 /* A new integral type (representing a bitfield).  */
   10580         1022 :                 unsigned enc = u ();
   10581         1022 :                 if (!get_overrun ())
   10582         1022 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10583              :               }
   10584              :             break;
   10585              : 
   10586       418677 :           case FUNCTION_TYPE:
   10587       418677 :           case METHOD_TYPE:
   10588       418677 :             {
   10589       418677 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10590       418677 :               tree args = tree_node ();
   10591       418677 :               if (!get_overrun ())
   10592              :                 {
   10593       418677 :                   if (klass)
   10594       265955 :                     res = build_method_type_directly (klass, res, args);
   10595              :                   else
   10596       152722 :                     res = cp_build_function_type (res, args);
   10597              :                 }
   10598              :             }
   10599              :             break;
   10600              : 
   10601          264 :           case OFFSET_TYPE:
   10602          264 :             {
   10603          264 :               tree base = tree_node ();
   10604          264 :               if (!get_overrun ())
   10605          264 :                 res = build_offset_type (base, res);
   10606              :             }
   10607              :             break;
   10608              : 
   10609       200962 :           case POINTER_TYPE:
   10610       200962 :             if (!get_overrun ())
   10611       200962 :               res = build_pointer_type (res);
   10612              :             break;
   10613              : 
   10614       172876 :           case REFERENCE_TYPE:
   10615       172876 :             {
   10616       172876 :               bool rval = bool (u ());
   10617       172876 :               if (!get_overrun ())
   10618       172876 :                 res = cp_build_reference_type (res, rval);
   10619              :             }
   10620              :             break;
   10621              : 
   10622       446581 :           case DECLTYPE_TYPE:
   10623       446581 :           case TYPEOF_TYPE:
   10624       446581 :           case DEPENDENT_OPERATOR_TYPE:
   10625       446581 :             {
   10626       446581 :               tree expr = tree_node ();
   10627       446581 :               if (!get_overrun ())
   10628              :                 {
   10629       446581 :                   res = cxx_make_type (code);
   10630       446581 :                   TYPE_VALUES_RAW (res) = expr;
   10631       446581 :                   if (code == DECLTYPE_TYPE)
   10632        21484 :                     tree_node_bools (res);
   10633       446581 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10634              :                 }
   10635              :             }
   10636              :             break;
   10637              : 
   10638         2202 :           case TRAIT_TYPE:
   10639         2202 :             {
   10640         2202 :               tree kind = tree_node ();
   10641         2202 :               tree type1 = tree_node ();
   10642         2202 :               tree type2 = tree_node ();
   10643         2202 :               if (!get_overrun ())
   10644              :                 {
   10645         2202 :                   res = cxx_make_type (TRAIT_TYPE);
   10646         2202 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10647         2202 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10648         2202 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10649         2202 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10650              :                 }
   10651              :             }
   10652              :             break;
   10653              : 
   10654        60896 :           case TYPE_ARGUMENT_PACK:
   10655        60896 :             if (!get_overrun ())
   10656              :               {
   10657        60896 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10658        60896 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10659        60896 :                 res = pack;
   10660              :               }
   10661              :             break;
   10662              : 
   10663        64250 :           case TYPE_PACK_EXPANSION:
   10664        64250 :             {
   10665        64250 :               bool local = u ();
   10666        64250 :               tree param_packs = tree_node ();
   10667        64250 :               tree extra_args = tree_node ();
   10668        64250 :               if (!get_overrun ())
   10669              :                 {
   10670        64250 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10671        64250 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10672        64250 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10673       128500 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10674        64250 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10675        64250 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10676        64250 :                   res = expn;
   10677              :                 }
   10678              :             }
   10679              :             break;
   10680              : 
   10681           25 :           case PACK_INDEX_TYPE:
   10682           25 :             {
   10683           25 :               tree pack = tree_node ();
   10684           25 :               tree index = tree_node ();
   10685           25 :               if (!get_overrun ())
   10686           25 :                 res = make_pack_index (pack, index);
   10687              :             }
   10688              :             break;
   10689              : 
   10690        66335 :           case TYPENAME_TYPE:
   10691        66335 :             {
   10692        66335 :               tree ctx = tree_node ();
   10693        66335 :               tree name = tree_node ();
   10694        66335 :               tree fullname = tree_node ();
   10695        66335 :               enum tag_types tag_type = tag_types (u ());
   10696              : 
   10697        66335 :               if (!get_overrun ())
   10698        66335 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10699              :             }
   10700              :             break;
   10701              : 
   10702           52 :           case UNBOUND_CLASS_TEMPLATE:
   10703           52 :             {
   10704           52 :               tree ctx = tree_node ();
   10705           52 :               tree name = tree_node ();
   10706           52 :               tree parms = tree_node ();
   10707              : 
   10708           52 :               if (!get_overrun ())
   10709           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10710              :             }
   10711              :             break;
   10712              : 
   10713              :           case VECTOR_TYPE:
   10714              :             {
   10715              :               poly_uint64 nunits;
   10716           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10717           30 :                 nunits.coeffs[ix] = wu ();
   10718           30 :               if (!get_overrun ())
   10719           30 :                 res = build_vector_type (res, nunits);
   10720              :             }
   10721              :             break;
   10722              : 
   10723           45 :           case META_TYPE:
   10724           45 :             if (!get_overrun ())
   10725           45 :               res = meta_info_type_node;
   10726              :             break;
   10727              : 
   10728            2 :           case SPLICE_SCOPE:
   10729            2 :             {
   10730            2 :               bool type = u ();
   10731            2 :               tree expr = tree_node ();
   10732              : 
   10733            2 :               if (!get_overrun ())
   10734            2 :                 res = make_splice_scope (expr, type);
   10735              :             }
   10736              :             break;
   10737              :           }
   10738              : 
   10739              :         /* In the exporting TU, a derived type with attributes was built by
   10740              :            build_type_attribute_variant as a distinct copy, with itself as
   10741              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10742              :            without attributes as TYPE_CANONICAL.  */
   10743      1473505 :         if (tree attribs = tree_node ())
   10744        17079 :           res = cp_build_type_attribute_variant (res, attribs);
   10745              : 
   10746      1473505 :         int tag = i ();
   10747      1473505 :         if (!tag)
   10748              :           {
   10749      1260958 :             tag = insert (res);
   10750      1260958 :             if (res)
   10751      1260958 :               dump (dumper::TREE)
   10752          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10753              :           }
   10754              :         else
   10755       212547 :           res = back_ref (tag);
   10756              :       }
   10757              :       break;
   10758              : 
   10759       424013 :     case tt_variant_type:
   10760              :       /* Variant of some type.  */
   10761       424013 :       {
   10762       424013 :         res = tree_node ();
   10763       424013 :         int flags = i ();
   10764       424013 :         if (get_overrun ())
   10765              :           ;
   10766       424013 :         else if (flags < 0)
   10767              :           /* No change.  */;
   10768       204263 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10769       204263 :                  || TREE_CODE (res) == METHOD_TYPE)
   10770              :           {
   10771       202690 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10772       202690 :             bool late = (flags >> 2) & 1;
   10773       202690 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10774              : 
   10775       202690 :             tree raises = tree_node ();
   10776       202690 :             if (raises == error_mark_node)
   10777         7034 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10778              : 
   10779       202690 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10780       202690 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10781        71814 :               res = apply_memfn_quals (res, quals, rqual);
   10782              :           }
   10783              :         else
   10784              :           {
   10785         1573 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10786         1573 :             TYPE_USER_ALIGN (res) = true;
   10787              :           }
   10788              : 
   10789       424013 :         int quals = i ();
   10790       424013 :         if (quals >= 0 && !get_overrun ())
   10791       220900 :           res = cp_build_qualified_type (res, quals);
   10792              : 
   10793       424013 :         int tag = i ();
   10794       424013 :         if (!tag)
   10795              :           {
   10796       424013 :             tag = insert (res);
   10797       424013 :             if (res)
   10798       424013 :               dump (dumper::TREE)
   10799          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10800              :           }
   10801              :         else
   10802            0 :           res = back_ref (tag);
   10803              :       }
   10804              :       break;
   10805              : 
   10806         5026 :     case tt_tinfo_var:
   10807         5026 :     case tt_tinfo_typedef:
   10808              :       /* A tinfo var or typedef.  */
   10809         5026 :       {
   10810         5026 :         bool is_var = tag == tt_tinfo_var;
   10811         5026 :         unsigned ix = u ();
   10812         5026 :         tree type = NULL_TREE;
   10813              : 
   10814         5026 :         if (is_var)
   10815              :           {
   10816         3062 :             tree name = tree_node ();
   10817         3062 :             type = tree_node ();
   10818              : 
   10819         3062 :             if (!get_overrun ())
   10820         3062 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10821              :           }
   10822              :         else
   10823              :           {
   10824         1964 :             if (!get_overrun ())
   10825              :               {
   10826         1964 :                 type = get_pseudo_tinfo_type (ix);
   10827         1964 :                 res = TYPE_NAME (type);
   10828              :               }
   10829              :           }
   10830         5026 :         if (res)
   10831              :           {
   10832         5026 :             int tag = insert (res);
   10833         5026 :             dump (dumper::TREE)
   10834           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10835              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10836         5026 :             if (!is_var)
   10837              :               {
   10838         1964 :                 tag = insert (type);
   10839         1964 :                 dump (dumper::TREE)
   10840           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10841              :               }
   10842              :           }
   10843              :       }
   10844              :       break;
   10845              : 
   10846         1093 :     case tt_ptrmem_type:
   10847              :       /* A pointer to member function.  */
   10848         1093 :       {
   10849         1093 :         tree type = tree_node ();
   10850         1093 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10851         2186 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10852              :           {
   10853         1093 :             res = build_ptrmemfunc_type (type);
   10854         1093 :             int tag = insert (res);
   10855         1096 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10856              :           }
   10857              :         else
   10858            0 :           set_overrun ();
   10859              :       }
   10860              :       break;
   10861              : 
   10862           10 :     case tt_nttp_var:
   10863              :       /* An NTTP object. */
   10864           10 :       {
   10865           10 :         tree init = tree_node ();
   10866           10 :         tree name = tree_node ();
   10867           10 :         if (!get_overrun ())
   10868              :           {
   10869              :             /* We don't want to check the initializer as that may require
   10870              :                name lookup, which could recursively start lazy loading.
   10871              :                Instead we know that INIT is already valid so we can just
   10872              :                apply that directly.  */
   10873           10 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10874           10 :             int tag = insert (res);
   10875           10 :             dump (dumper::TREE)
   10876            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10877           10 :             vec_safe_push (post_load_decls, res);
   10878              :           }
   10879              :       }
   10880              :       break;
   10881              : 
   10882         7584 :     case tt_enum_value:
   10883              :       /* An enum const value.  */
   10884         7584 :       {
   10885         7584 :         if (tree decl = tree_node ())
   10886              :           {
   10887         7602 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10888         7584 :             res = DECL_INITIAL (decl);
   10889              :           }
   10890              : 
   10891         7584 :         if (!res)
   10892            0 :           set_overrun ();
   10893              :       }
   10894              :       break;
   10895              : 
   10896        13916 :     case tt_enum_decl:
   10897              :       /* An enum decl.  */
   10898        13916 :       {
   10899        13916 :         tree ctx = tree_node ();
   10900        13916 :         tree name = tree_node ();
   10901              : 
   10902        13916 :         if (!get_overrun ()
   10903        13916 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10904        13916 :           res = find_enum_member (ctx, name);
   10905              : 
   10906        13916 :         if (!res)
   10907            0 :           set_overrun ();
   10908              :         else
   10909              :           {
   10910        13916 :             int tag = insert (res);
   10911        13916 :             dump (dumper::TREE)
   10912           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10913              :           }
   10914              :       }
   10915              :       break;
   10916              : 
   10917         8637 :     case tt_data_member:
   10918              :       /* A data member.  */
   10919         8637 :       {
   10920         8637 :         tree ctx = tree_node ();
   10921         8637 :         tree name = tree_node ();
   10922              : 
   10923         8637 :         if (!get_overrun ()
   10924         8637 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10925              :           {
   10926         8637 :             if (name)
   10927         7614 :               res = lookup_class_binding (ctx, name);
   10928              :             else
   10929         1023 :               res = lookup_field_ident (ctx, u ());
   10930              : 
   10931         8637 :             if (!res
   10932         8637 :                 || (TREE_CODE (res) != FIELD_DECL
   10933         8637 :                     && TREE_CODE (res) != USING_DECL)
   10934        17274 :                 || DECL_CONTEXT (res) != ctx)
   10935            0 :               res = NULL_TREE;
   10936              :           }
   10937              : 
   10938         8637 :         if (!res)
   10939            0 :           set_overrun ();
   10940              :         else
   10941              :           {
   10942         8637 :             int tag = insert (res);
   10943         8637 :             dump (dumper::TREE)
   10944           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10945              :           }
   10946              :       }
   10947              :       break;
   10948              : 
   10949         1694 :     case tt_binfo:
   10950              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10951         1694 :       {
   10952         1694 :         tree type;
   10953         1694 :         unsigned ix = binfo_mergeable (&type);
   10954         1694 :         if (type)
   10955              :           {
   10956         1694 :             res = TYPE_BINFO (type);
   10957         1780 :             for (; ix && res; res = TREE_CHAIN (res))
   10958           86 :               ix--;
   10959         1694 :             if (!res)
   10960            0 :               set_overrun ();
   10961              :           }
   10962              : 
   10963         1694 :         if (get_overrun ())
   10964              :           break;
   10965              : 
   10966              :         /* Insert binfo into backreferences.  */
   10967         1694 :         tag = insert (res);
   10968         1694 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10969              :       }
   10970         1694 :       break;
   10971              : 
   10972           73 :     case tt_vtable:
   10973           73 :       {
   10974           73 :         unsigned ix = u ();
   10975           73 :         tree ctx = tree_node ();
   10976           73 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   10977           73 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   10978           85 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   10979           85 :             if (!ix--)
   10980              :               break;
   10981           73 :         if (!res)
   10982            0 :           set_overrun ();
   10983              :       }
   10984              :       break;
   10985              : 
   10986            0 :     case tt_thunk:
   10987            0 :       {
   10988            0 :         int fixed = i ();
   10989            0 :         tree target = tree_node ();
   10990            0 :         tree virt = tree_node ();
   10991              : 
   10992            0 :         for (tree thunk = DECL_THUNKS (target);
   10993            0 :              thunk; thunk = DECL_CHAIN (thunk))
   10994            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   10995            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   10996            0 :               && (!virt
   10997            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   10998              :             {
   10999            0 :               res = thunk;
   11000            0 :               break;
   11001              :             }
   11002              : 
   11003            0 :         int tag = insert (res);
   11004            0 :         if (res)
   11005            0 :           dump (dumper::TREE)
   11006            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11007              :         else
   11008            0 :           set_overrun ();
   11009              :       }
   11010              :       break;
   11011              : 
   11012       154480 :     case tt_clone_ref:
   11013       154480 :       {
   11014       154480 :         tree target = tree_node ();
   11015       154480 :         tree name = tree_node ();
   11016              : 
   11017       154480 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11018              :           {
   11019       154480 :             tree clone;
   11020       239859 :             FOR_EVERY_CLONE (clone, target)
   11021       239859 :               if (DECL_NAME (clone) == name)
   11022              :                 {
   11023       154480 :                   res = clone;
   11024       154480 :                   break;
   11025              :                 }
   11026              :           }
   11027              : 
   11028              :         /* A clone might have a different vtable entry.  */
   11029       154480 :         if (res && DECL_VIRTUAL_P (res))
   11030         8543 :           DECL_VINDEX (res) = tree_node ();
   11031              : 
   11032       154480 :         if (!res)
   11033            0 :           set_overrun ();
   11034       154480 :         int tag = insert (res);
   11035       154480 :         if (res)
   11036       154480 :           dump (dumper::TREE)
   11037          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11038              :         else
   11039            0 :           set_overrun ();
   11040              :        }
   11041              :       break;
   11042              : 
   11043      1082954 :     case tt_entity:
   11044              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11045      1082954 :       {
   11046      1082954 :         unsigned origin = state->slurp->remap_module (u ());
   11047      1082954 :         unsigned ident = u ();
   11048      1082954 :         module_state *from = (*modules)[origin];
   11049              : 
   11050      1082954 :         if (!origin || ident >= from->entity_num)
   11051            0 :           set_overrun ();
   11052      1082954 :         if (!get_overrun ())
   11053              :           {
   11054      1082954 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11055      1082954 :             if (slot->is_lazy ())
   11056        52243 :               if (!from->lazy_load (ident, slot))
   11057            0 :                 set_overrun ();
   11058      1082954 :             res = *slot;
   11059              :           }
   11060              : 
   11061      1082954 :         if (res)
   11062              :           {
   11063      1082954 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11064      1082954 :             int tag = insert (res);
   11065      1082954 :             dump (dumper::TREE)
   11066          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11067          605 :                        res, (*modules)[origin]);
   11068              : 
   11069      1082954 :             if (!add_indirects (res))
   11070              :               {
   11071            0 :                 set_overrun ();
   11072            0 :                 res = NULL_TREE;
   11073              :               }
   11074              :           }
   11075              :       }
   11076              :       break;
   11077              : 
   11078         3051 :     case tt_template:
   11079              :       /* A template.  */
   11080         3051 :       if (tree tpl = tree_node ())
   11081              :         {
   11082         3051 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11083         3051 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11084         3051 :           dump (dumper::TREE)
   11085            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11086              :         }
   11087              :       break;
   11088              :     }
   11089              : 
   11090     96059002 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11091              :     {
   11092              :       /* Mark decl used as mark_used does -- we cannot call
   11093              :          mark_used in the middle of streaming, we only need a subset
   11094              :          of its functionality.   */
   11095       742645 :       TREE_USED (res) = true;
   11096              : 
   11097              :       /* And for structured bindings also the underlying decl.  */
   11098       742645 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11099         1932 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11100              : 
   11101       742645 :       if (DECL_CLONED_FUNCTION_P (res))
   11102         7214 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11103              :     }
   11104              : 
   11105     96059002 :   dump.outdent ();
   11106     96059002 :   return res;
   11107              : }
   11108              : 
   11109              : void
   11110      2107018 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11111              : {
   11112      2107018 :   if (!parms)
   11113              :     return;
   11114              : 
   11115      1420330 :   if (TREE_VISITED (parms))
   11116              :     {
   11117       593441 :       ref_node (parms);
   11118       593441 :       return;
   11119              :     }
   11120              : 
   11121       826889 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11122              : 
   11123       826889 :   tree vec = TREE_VALUE (parms);
   11124       826889 :   unsigned len = TREE_VEC_LENGTH (vec);
   11125              :   /* Depth.  */
   11126       826889 :   int tag = insert (parms);
   11127       826889 :   if (streaming_p ())
   11128              :     {
   11129       220345 :       i (len + 1);
   11130       220411 :       dump (dumper::TREE)
   11131           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11132           66 :                  tag, TREE_PURPOSE (parms), len);
   11133              :     }
   11134       826889 :   tree_node (TREE_PURPOSE (parms));
   11135              : 
   11136      2290291 :   for (unsigned ix = 0; ix != len; ix++)
   11137              :     {
   11138      1463402 :       tree parm = TREE_VEC_ELT (vec, ix);
   11139      1463402 :       tree decl = TREE_VALUE (parm);
   11140              : 
   11141      1463402 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11142      1463402 :       if (CHECKING_P)
   11143      1463402 :         switch (TREE_CODE (decl))
   11144              :           {
   11145            0 :           default: gcc_unreachable ();
   11146              : 
   11147         3804 :           case TEMPLATE_DECL:
   11148         3804 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11149              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11150              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11151              :             break;
   11152              : 
   11153      1365952 :           case TYPE_DECL:
   11154      1365952 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11155              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11156              :             break;
   11157              : 
   11158        93646 :           case PARM_DECL:
   11159        93646 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11160              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11161              :                             == CONST_DECL)
   11162              :                         && (DECL_TEMPLATE_PARM_P
   11163              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11164              :             break;
   11165              :           }
   11166              : 
   11167      1463402 :       tree_node (decl);
   11168      1463402 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11169              :     }
   11170              : 
   11171       826889 :   tpl_levels++;
   11172              : }
   11173              : 
   11174              : tree
   11175       326659 : trees_in::tpl_parms (unsigned &tpl_levels)
   11176              : {
   11177       326659 :   tree parms = NULL_TREE;
   11178              : 
   11179       689492 :   while (int len = i ())
   11180              :     {
   11181       362833 :       if (len < 0)
   11182              :         {
   11183       197396 :           parms = back_ref (len);
   11184       197396 :           continue;
   11185              :         }
   11186              : 
   11187       165437 :       len -= 1;
   11188       165437 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11189       165437 :       int tag = insert (parms);
   11190       165437 :       TREE_PURPOSE (parms) = tree_node ();
   11191              : 
   11192       165437 :       dump (dumper::TREE)
   11193          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11194          105 :                  tag, TREE_PURPOSE (parms), len);
   11195              : 
   11196       165437 :       tree vec = make_tree_vec (len);
   11197       436153 :       for (int ix = 0; ix != len; ix++)
   11198              :         {
   11199       270716 :           tree decl = tree_node ();
   11200       270716 :           if (!decl)
   11201              :             return NULL_TREE;
   11202              : 
   11203       270716 :           tree parm = build_tree_list (NULL, decl);
   11204       270716 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11205              : 
   11206       270716 :           TREE_VEC_ELT (vec, ix) = parm;
   11207              :         }
   11208              : 
   11209       165437 :       TREE_VALUE (parms) = vec;
   11210       165437 :       tpl_levels++;
   11211              :     }
   11212              : 
   11213              :   return parms;
   11214              : }
   11215              : 
   11216              : void
   11217      1280129 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11218              : {
   11219      1280129 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11220      2107018 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11221              :     {
   11222       826889 :       tree vec = TREE_VALUE (parms);
   11223              : 
   11224       826889 :       tree_node (TREE_TYPE (vec));
   11225      2290291 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11226              :         {
   11227      1463402 :           tree parm = TREE_VEC_ELT (vec, ix);
   11228      1463402 :           tree dflt = TREE_PURPOSE (parm);
   11229      1463402 :           tree_node (dflt);
   11230              : 
   11231              :           /* Template template parameters need a context of their owning
   11232              :              template. This is quite tricky to infer correctly on stream-in
   11233              :              (see PR c++/98881) so we'll just provide it directly.  */
   11234      1463402 :           tree decl = TREE_VALUE (parm);
   11235      1463402 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11236         3804 :             tree_node (DECL_CONTEXT (decl));
   11237              :         }
   11238              :     }
   11239      1280129 : }
   11240              : 
   11241              : bool
   11242       326659 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11243              : {
   11244       326659 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11245       492096 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11246              :     {
   11247       165437 :       tree vec = TREE_VALUE (parms);
   11248              : 
   11249       165437 :       TREE_TYPE (vec) = tree_node ();
   11250       436153 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11251              :         {
   11252       270716 :           tree parm = TREE_VEC_ELT (vec, ix);
   11253       270716 :           tree dflt = tree_node ();
   11254       270716 :           TREE_PURPOSE (parm) = dflt;
   11255              : 
   11256       270716 :           tree decl = TREE_VALUE (parm);
   11257       270716 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11258          843 :             DECL_CONTEXT (decl) = tree_node ();
   11259              : 
   11260       270716 :           if (get_overrun ())
   11261              :             return false;
   11262              :         }
   11263              :     }
   11264              :   return true;
   11265              : }
   11266              : 
   11267              : /* PARMS is a LIST, one node per level.
   11268              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11269              :    each ELT is a TREE_LIST
   11270              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11271              :    TREE_PURPOSE is the default value.  */
   11272              : 
   11273              : void
   11274      1280129 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11275              : {
   11276      1280129 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11277      1280129 :   tpl_parms (parms, *tpl_levels);
   11278              : 
   11279              :   /* Mark end.  */
   11280      1280129 :   if (streaming_p ())
   11281       426229 :     u (0);
   11282              : 
   11283      1280129 :   if (*tpl_levels)
   11284       777307 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11285      1280129 : }
   11286              : 
   11287              : bool
   11288       326659 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11289              : {
   11290       326659 :   tree parms = tpl_parms (*tpl_levels);
   11291       326659 :   if (!parms)
   11292              :     return false;
   11293              : 
   11294       326659 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11295              : 
   11296       326659 :   if (*tpl_levels)
   11297       163343 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11298              : 
   11299              :   return true;
   11300              : }
   11301              : 
   11302              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11303              :    All the parms will have consecutive tags.  */
   11304              : 
   11305              : void
   11306      1793243 : trees_out::fn_parms_init (tree fn)
   11307              : {
   11308              :   /* First init them.  */
   11309      1793243 :   int base_tag = ref_num - 1;
   11310      1793243 :   int ix = 0;
   11311      1793243 :   for (tree parm = DECL_ARGUMENTS (fn);
   11312      5418620 :        parm; parm = DECL_CHAIN (parm), ix++)
   11313              :     {
   11314      3625377 :       if (streaming_p ())
   11315              :         {
   11316      1208422 :           start (parm);
   11317      1208422 :           tree_node_bools (parm);
   11318              :         }
   11319      3625377 :       int tag = insert (parm);
   11320      3625377 :       gcc_checking_assert (base_tag - ix == tag);
   11321              :     }
   11322              :   /* Mark the end.  */
   11323      1793243 :   if (streaming_p ())
   11324       598014 :     u (0);
   11325              : 
   11326              :   /* Now stream their contents.  */
   11327      1793243 :   ix = 0;
   11328      1793243 :   for (tree parm = DECL_ARGUMENTS (fn);
   11329      5418620 :        parm; parm = DECL_CHAIN (parm), ix++)
   11330              :     {
   11331      3625377 :       if (streaming_p ())
   11332      1208422 :         dump (dumper::TREE)
   11333          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11334              :                    base_tag - ix, ix, parm, fn);
   11335      3625377 :       tree_node_vals (parm);
   11336              :     }
   11337              : 
   11338      1793243 :   if (!streaming_p ())
   11339              :     {
   11340              :       /* We must walk contract specifiers so the dependency graph is
   11341              :          complete.  */
   11342      1195229 :       tree contract = get_fn_contract_specifiers (fn);
   11343      2390458 :       for (; contract; contract = TREE_CHAIN (contract))
   11344            0 :         tree_node (contract);
   11345              :     }
   11346              : 
   11347              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11348              :      regenerating them in importers.  */
   11349      1793243 :   tree_node (DECL_PRE_FN (fn));
   11350      1793243 :   tree_node (DECL_POST_FN (fn));
   11351      1793243 : }
   11352              : 
   11353              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11354              : 
   11355              : int
   11356       462193 : trees_in::fn_parms_init (tree fn)
   11357              : {
   11358       462193 :   int base_tag = ~(int)back_refs.length ();
   11359              : 
   11360       462193 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11361       462193 :   int ix = 0;
   11362      1393533 :   for (; int code = u (); ix++)
   11363              :     {
   11364       931340 :       tree parm = start (code);
   11365       931340 :       if (!tree_node_bools (parm))
   11366              :         return 0;
   11367              : 
   11368       931340 :       int tag = insert (parm);
   11369       931340 :       gcc_checking_assert (base_tag - ix == tag);
   11370       931340 :       *parm_ptr = parm;
   11371       931340 :       parm_ptr = &DECL_CHAIN (parm);
   11372       931340 :     }
   11373              : 
   11374       462193 :   ix = 0;
   11375       462193 :   for (tree parm = DECL_ARGUMENTS (fn);
   11376      1393533 :        parm; parm = DECL_CHAIN (parm), ix++)
   11377              :     {
   11378       931340 :       dump (dumper::TREE)
   11379          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11380              :                  base_tag - ix, ix, parm, fn);
   11381       931340 :       if (!tree_node_vals (parm))
   11382              :         return 0;
   11383              : 
   11384              :       /* Apply relevant attributes.
   11385              :          FIXME should probably use cplus_decl_attributes for this,
   11386              :          but it's not yet ready for modules.  */
   11387              : 
   11388              :       /* TREE_USED is deliberately not streamed for most declarations,
   11389              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11390       931340 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11391       931340 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11392              :         {
   11393         2096 :           TREE_USED (parm) = true;
   11394         2096 :           DECL_READ_P (parm) = true;
   11395              :         }
   11396              :     }
   11397              : 
   11398              :   /* Reload references to contract functions, if any.  */
   11399       462193 :   tree pre_fn = tree_node ();
   11400       462193 :   tree post_fn = tree_node ();
   11401       462193 :   set_contract_functions (fn, pre_fn, post_fn);
   11402              : 
   11403       462193 :   return base_tag;
   11404              : }
   11405              : 
   11406              : /* Read the remaining parm node data.  Replace with existing (if
   11407              :    non-null) in the map.  */
   11408              : 
   11409              : void
   11410       462193 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11411              : {
   11412       662221 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11413       462193 :   tree parms = DECL_ARGUMENTS (fn);
   11414      1393533 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11415              :     {
   11416       931340 :       if (existing_parm)
   11417              :         {
   11418       583138 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11419              :             {
   11420              :               /* If we're about to become the definition, set the
   11421              :                  names of the parms from us.  */
   11422        15282 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11423        15282 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11424              : 
   11425              :               /* And some other flags important for codegen are only set
   11426              :                  by the definition.  */
   11427        15282 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11428        15282 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11429        15282 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11430        15282 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11431              : 
   11432              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11433        15282 :               if (DECL_BY_REFERENCE (parm))
   11434              :                 {
   11435            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11436            6 :                   relayout_decl (existing_parm);
   11437              :                 }
   11438              :             }
   11439              : 
   11440       395447 :           back_refs[~tag] = existing_parm;
   11441       395447 :           existing_parm = DECL_CHAIN (existing_parm);
   11442              :         }
   11443       931340 :       tag--;
   11444              :     }
   11445       462193 : }
   11446              : 
   11447              : /* Encode into KEY the position of the local type (class or enum)
   11448              :    declaration DECL within FN.  The position is encoded as the
   11449              :    index of the innermost BLOCK (numbered in BFS order) along with
   11450              :    the index within its BLOCK_VARS list.  */
   11451              : 
   11452              : void
   11453        19671 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11454              : {
   11455        19671 :   auto_vec<tree, 4> blocks;
   11456        19671 :   blocks.quick_push (DECL_INITIAL (fn));
   11457        19671 :   unsigned block_ix = 0;
   11458        94947 :   while (block_ix != blocks.length ())
   11459              :     {
   11460        37638 :       tree block = blocks[block_ix];
   11461        37638 :       unsigned decl_ix = 0;
   11462       114453 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11463              :         {
   11464        96486 :           if (TREE_CODE (var) != TYPE_DECL)
   11465        60054 :             continue;
   11466        36432 :           if (var == decl)
   11467              :             {
   11468        19671 :               key.index = (block_ix << 10) | decl_ix;
   11469        19671 :               return;
   11470              :             }
   11471        16761 :           ++decl_ix;
   11472              :         }
   11473        37554 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11474        19587 :         blocks.safe_push (sub);
   11475        17967 :       ++block_ix;
   11476              :     }
   11477              : 
   11478              :   /* Not-found value.  */
   11479            0 :   key.index = 1023;
   11480        19671 : }
   11481              : 
   11482              : /* Look up the local type corresponding at the position encoded by
   11483              :    KEY within FN and named NAME.  */
   11484              : 
   11485              : tree
   11486         4217 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11487              : {
   11488         4217 :   if (!DECL_INITIAL (fn))
   11489              :     return NULL_TREE;
   11490              : 
   11491         1902 :   const unsigned block_pos = key.index >> 10;
   11492         1902 :   const unsigned decl_pos = key.index & 1023;
   11493              : 
   11494         1902 :   if (decl_pos == 1023)
   11495              :     return NULL_TREE;
   11496              : 
   11497         1902 :   auto_vec<tree, 4> blocks;
   11498         1902 :   blocks.quick_push (DECL_INITIAL (fn));
   11499         1902 :   unsigned block_ix = 0;
   11500         8688 :   while (block_ix != blocks.length ())
   11501              :     {
   11502         3393 :       tree block = blocks[block_ix];
   11503         3393 :       if (block_ix == block_pos)
   11504              :         {
   11505         1902 :           unsigned decl_ix = 0;
   11506         5158 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11507              :             {
   11508         5158 :               if (TREE_CODE (var) != TYPE_DECL)
   11509         2278 :                 continue;
   11510              :               /* Prefer using the identifier as the key for more robustness
   11511              :                  to ODR violations, except for anonymous types since their
   11512              :                  compiler-generated identifiers aren't stable.  */
   11513         5760 :               if (IDENTIFIER_ANON_P (name)
   11514         2880 :                   ? decl_ix == decl_pos
   11515          323 :                   : DECL_NAME (var) == name)
   11516              :                 return var;
   11517          978 :               ++decl_ix;
   11518              :             }
   11519              :           return NULL_TREE;
   11520              :         }
   11521         3091 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11522         1600 :         blocks.safe_push (sub);
   11523         1491 :       ++block_ix;
   11524              :     }
   11525              : 
   11526              :   return NULL_TREE;
   11527         1902 : }
   11528              : 
   11529              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11530              :    the merging behaviour.  */
   11531              : 
   11532              : merge_kind
   11533      4434992 : trees_out::get_merge_kind (tree decl, depset *dep)
   11534              : {
   11535      4434992 :   if (!dep)
   11536              :     {
   11537       905211 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11538              :         {
   11539              :           /* Any var or function with template info should have DEP.  */
   11540       502132 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11541              :                                || !DECL_TEMPLATE_INFO (decl));
   11542       502132 :           if (DECL_LOCAL_DECL_P (decl))
   11543              :             return MK_unique;
   11544              :         }
   11545              : 
   11546              :       /* Either unique, or some member of a class that cannot have an
   11547              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11548       904903 :       tree ctx = CP_DECL_CONTEXT (decl);
   11549       904903 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11550              :         {
   11551              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11552              :              this isn't permitting them to have one.   */
   11553       578291 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11554              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11555              :                                || !DECL_LANG_SPECIFIC (decl)
   11556              :                                || !DECL_TEMPLATE_INFO (decl));
   11557              : 
   11558              :           return MK_unique;
   11559              :         }
   11560              : 
   11561       326612 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11562       326612 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11563              :         return MK_local_friend;
   11564              : 
   11565       326612 :       gcc_checking_assert (TYPE_P (ctx));
   11566              : 
   11567              :       /* Internal-only types will not need to dedup their members.  */
   11568       326612 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11569              :         return MK_unique;
   11570              : 
   11571       326556 :       if (TREE_CODE (decl) == USING_DECL)
   11572              :         return MK_field;
   11573              : 
   11574       215171 :       if (TREE_CODE (decl) == FIELD_DECL)
   11575              :         {
   11576       158353 :           if (DECL_NAME (decl))
   11577              :             {
   11578              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11579       128054 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11580              :               return MK_named;
   11581              :             }
   11582              : 
   11583        30299 :           if (walking_bit_field_unit)
   11584              :             {
   11585              :               /* The underlying storage unit for a bitfield.  We do not
   11586              :                  need to dedup it, because it's only reachable through
   11587              :                  the bitfields it represents.  And those are deduped.  */
   11588              :               // FIXME: Is that assertion correct -- do we ever fish it
   11589              :               // out and put it in an expr?
   11590          524 :               gcc_checking_assert (!DECL_NAME (decl)
   11591              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11592              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11593          524 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11594              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11595              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11596              :                                    == INTEGER_TYPE);
   11597              :               return MK_unique;
   11598              :             }
   11599              : 
   11600              :           return MK_field;
   11601              :         }
   11602              : 
   11603        56818 :       if (TREE_CODE (decl) == CONST_DECL)
   11604              :         return MK_named;
   11605              : 
   11606        10162 :       if (TREE_CODE (decl) == VAR_DECL
   11607        10162 :           && DECL_VTABLE_OR_VTT_P (decl))
   11608              :         return MK_vtable;
   11609              : 
   11610         1736 :       if (DECL_THUNK_P (decl))
   11611              :         /* Thunks are unique-enough, because they're only referenced
   11612              :            from the vtable.  And that's either new (so we want the
   11613              :            thunks), or it's a duplicate (so it will be dropped).  */
   11614              :         return MK_unique;
   11615              : 
   11616              :       /* There should be no other cases.  */
   11617            0 :       gcc_unreachable ();
   11618              :     }
   11619              : 
   11620      3529781 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11621              :                        && TREE_CODE (decl) != USING_DECL
   11622              :                        && TREE_CODE (decl) != CONST_DECL);
   11623              : 
   11624      3529781 :   if (is_key_order ())
   11625              :     {
   11626              :       /* When doing the mergeablilty graph, there's an indirection to
   11627              :          the actual depset.  */
   11628      1176440 :       gcc_assert (dep->is_special ());
   11629      1176440 :       dep = dep->deps[0];
   11630              :     }
   11631              : 
   11632      3529781 :   gcc_checking_assert (decl == dep->get_entity ());
   11633              : 
   11634      3529781 :   merge_kind mk = MK_named;
   11635      3529781 :   switch (dep->get_entity_kind ())
   11636              :     {
   11637            0 :     default:
   11638            0 :       gcc_unreachable ();
   11639              : 
   11640              :     case depset::EK_PARTIAL:
   11641              :       mk = MK_partial;
   11642              :       break;
   11643              : 
   11644      1995878 :     case depset::EK_DECL:
   11645      1995878 :       {
   11646      1995878 :         tree ctx = CP_DECL_CONTEXT (decl);
   11647              : 
   11648      1995878 :         switch (TREE_CODE (ctx))
   11649              :           {
   11650            0 :           default:
   11651            0 :             gcc_unreachable ();
   11652              : 
   11653        19935 :           case FUNCTION_DECL:
   11654        19935 :             gcc_checking_assert
   11655              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11656              : 
   11657        19935 :             if (has_definition (ctx))
   11658              :               mk = MK_local_type;
   11659              :             else
   11660              :               /* We're not providing a definition of the context to key
   11661              :                  the local type into; use the keyed map instead.  */
   11662         1210 :               mk = MK_keyed;
   11663              :             break;
   11664              : 
   11665      1975943 :           case RECORD_TYPE:
   11666      1975943 :           case UNION_TYPE:
   11667      1975943 :           case NAMESPACE_DECL:
   11668      1975943 :             if (DECL_NAME (decl) == as_base_identifier)
   11669              :               {
   11670              :                 mk = MK_as_base;
   11671              :                 break;
   11672              :               }
   11673              : 
   11674              :             /* A lambda may have a class as its context, even though it
   11675              :                isn't a member in the traditional sense; see the test
   11676              :                g++.dg/modules/lambda-6_a.C.  */
   11677      2462930 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11678      2147672 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11679              :               {
   11680          982 :                 if (get_keyed_decl_scope (decl))
   11681              :                   mk = MK_keyed;
   11682              :                 else
   11683              :                   /* Lambdas not attached to any mangling scope are TU-local
   11684              :                      and so cannot be deduplicated.  */
   11685       581428 :                   mk = MK_unique;
   11686              :                 break;
   11687              :               }
   11688              : 
   11689      1886170 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11690      1886170 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11691       809955 :                 : decl_specialization_friend_p (decl))
   11692              :               {
   11693              :                 mk = MK_local_friend;
   11694              :                 break;
   11695              :               }
   11696              : 
   11697      1861402 :             if (DECL_DECOMPOSITION_P (decl))
   11698              :               {
   11699              :                 mk = MK_unique;
   11700              :                 break;
   11701              :               }
   11702              : 
   11703      1860925 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11704              :               {
   11705        32495 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11706              :                   mk = MK_field;
   11707         1171 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11708         1171 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11709         2342 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11710              :                   /* Keyed by first enum value, and underlying type.  */
   11711              :                   mk = MK_enum;
   11712              :                 else
   11713              :                   /* No way to merge it, it is an ODR land-mine.  */
   11714              :                   mk = MK_unique;
   11715              :               }
   11716              :           }
   11717              :       }
   11718              :       break;
   11719              : 
   11720      1476759 :     case depset::EK_SPECIALIZATION:
   11721      1476759 :       {
   11722      1476759 :         gcc_checking_assert (dep->is_special ());
   11723              : 
   11724      1476759 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11725              :           /* An block-scope classes of templates are themselves
   11726              :              templates.  */
   11727         5472 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11728              : 
   11729      1476759 :         if (dep->is_friend_spec ())
   11730              :           mk = MK_friend_spec;
   11731      1476759 :         else if (dep->is_type_spec ())
   11732              :           mk = MK_type_spec;
   11733              :         else
   11734      1050468 :           mk = MK_decl_spec;
   11735              : 
   11736      1476759 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11737              :           {
   11738       122553 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11739       122553 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11740        12654 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11741              :           }
   11742              :       }
   11743              :       break;
   11744              :     }
   11745              : 
   11746              :   return mk;
   11747              : }
   11748              : 
   11749              : 
   11750              : /* The container of DECL -- not necessarily its context!  */
   11751              : 
   11752              : tree
   11753      4434992 : trees_out::decl_container (tree decl)
   11754              : {
   11755      4434992 :   int use_tpl;
   11756      4434992 :   tree tpl = NULL_TREE;
   11757      4434992 :   if (tree template_info = node_template_info (decl, use_tpl))
   11758      1544694 :     tpl = TI_TEMPLATE (template_info);
   11759      4434992 :   if (tpl == decl)
   11760            0 :     tpl = nullptr;
   11761              : 
   11762              :   /* Stream the template we're instantiated from.  */
   11763      4434992 :   tree_node (tpl);
   11764              : 
   11765      4434992 :   tree container = NULL_TREE;
   11766      4434992 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11767      4434992 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11768      3161515 :       : decl_specialization_friend_p (decl))
   11769        24768 :     container = DECL_CHAIN (decl);
   11770              :   else
   11771      4410224 :     container = CP_DECL_CONTEXT (decl);
   11772              : 
   11773      4434992 :   if (TYPE_P (container))
   11774      2609737 :     container = TYPE_NAME (container);
   11775              : 
   11776      4434992 :   tree_node (container);
   11777              : 
   11778      4434992 :   return container;
   11779              : }
   11780              : 
   11781              : tree
   11782      1203585 : trees_in::decl_container ()
   11783              : {
   11784              :   /* The maybe-template.  */
   11785      1203585 :   (void)tree_node ();
   11786              : 
   11787      1203585 :   tree container = tree_node ();
   11788              : 
   11789      1203585 :   return container;
   11790              : }
   11791              : 
   11792              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11793              :    functions from a normal function.  */
   11794              : 
   11795              : static int
   11796      1246584 : get_coroutine_discriminator (tree inner)
   11797              : {
   11798      1246584 :   if (DECL_COROUTINE_P (inner))
   11799           72 :     if (tree ramp = DECL_RAMP_FN (inner))
   11800              :       {
   11801           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11802              :           return 1;
   11803            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11804              :           return 2;
   11805              :         else
   11806            0 :           gcc_unreachable ();
   11807              :       }
   11808              :   return 0;
   11809              : }
   11810              : 
   11811              : /* Write out key information about a mergeable DEP.  Does not write
   11812              :    the contents of DEP itself.  The context has already been
   11813              :    written.  The container has already been streamed.  */
   11814              : 
   11815              : void
   11816      4434992 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11817              :                           tree container, depset *dep)
   11818              : {
   11819      4434992 :   if (dep && is_key_order ())
   11820              :     {
   11821      1176440 :       gcc_checking_assert (dep->is_special ());
   11822      1176440 :       dep = dep->deps[0];
   11823              :     }
   11824              : 
   11825      4434992 :   if (streaming_p ())
   11826      1618159 :     dump (dumper::MERGE)
   11827         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11828          993 :                dep ? dep->entity_kind_name () : "contained",
   11829         1101 :                TREE_CODE (decl), decl);
   11830              : 
   11831              :   /* Now write the locating information. */
   11832      4434992 :   if (mk & MK_template_mask)
   11833              :     {
   11834              :       /* Specializations are located via their originating template,
   11835              :          and the set of template args they specialize.  */
   11836      1476759 :       gcc_checking_assert (dep && dep->is_special ());
   11837      1476759 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11838              : 
   11839      1476759 :       tree_node (entry->tmpl);
   11840      1476759 :       tree_node (entry->args);
   11841      1476759 :       if (mk & MK_tmpl_decl_mask)
   11842      1050468 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11843              :           {
   11844              :             /* Variable template partial specializations might need
   11845              :                constraints (see spec_hasher::equal).  It's simpler to
   11846              :                write NULL when we don't need them.  */
   11847        22518 :             tree constraints = NULL_TREE;
   11848              : 
   11849        22518 :             if (uses_template_parms (entry->args))
   11850          705 :               constraints = get_constraints (inner);
   11851        22518 :             tree_node (constraints);
   11852              :           }
   11853              : 
   11854      1476759 :       if (CHECKING_P)
   11855              :         {
   11856              :           /* Make sure we can locate the decl.  */
   11857      1476759 :           tree existing = match_mergeable_specialization
   11858      1476759 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11859              : 
   11860      1476759 :           gcc_assert (existing);
   11861      1476759 :           if (mk & MK_tmpl_decl_mask)
   11862              :             {
   11863      1050468 :               if (mk & MK_tmpl_tmpl_mask)
   11864         9969 :                 existing = DECL_TI_TEMPLATE (existing);
   11865              :             }
   11866              :           else
   11867              :             {
   11868       426291 :               if (mk & MK_tmpl_tmpl_mask)
   11869         2685 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11870              :               else
   11871       423606 :                 existing = TYPE_NAME (existing);
   11872              :             }
   11873              : 
   11874              :           /* The walkabout should have found ourselves.  */
   11875      1476759 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11876              :                                ? same_type_p (TREE_TYPE (decl),
   11877              :                                               TREE_TYPE (existing))
   11878              :                                : existing == decl);
   11879              :         }
   11880              :     }
   11881      2958233 :   else if (mk != MK_unique)
   11882              :     {
   11883      2376805 :       merge_key key;
   11884      2376805 :       tree name = DECL_NAME (decl);
   11885              : 
   11886      2376805 :       switch (mk)
   11887              :         {
   11888            0 :         default:
   11889            0 :           gcc_unreachable ();
   11890              : 
   11891      2003140 :         case MK_named:
   11892      2003140 :         case MK_friend_spec:
   11893      2003140 :           if (IDENTIFIER_CONV_OP_P (name))
   11894         7021 :             name = conv_op_identifier;
   11895              : 
   11896      2003140 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11897              :             {
   11898              :               /* Functions are distinguished by parameter types.  */
   11899      1115047 :               tree fn_type = TREE_TYPE (inner);
   11900              : 
   11901      1115047 :               key.ref_q = type_memfn_rqual (fn_type);
   11902      1115047 :               key.coro_disc = get_coroutine_discriminator (inner);
   11903      1115047 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11904      1115047 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11905      1115047 :               key.args = TYPE_ARG_TYPES (fn_type);
   11906              : 
   11907      1115047 :               if (tree reqs = get_constraints (inner))
   11908              :                 {
   11909        79803 :                   if (cxx_dialect < cxx20)
   11910           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11911              :                   else
   11912       159558 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11913        79803 :                   key.constraints = reqs;
   11914              :                 }
   11915              : 
   11916      1115047 :               if (IDENTIFIER_CONV_OP_P (name)
   11917      1115047 :                   || (decl != inner
   11918       610086 :                       && !(name == fun_identifier
   11919              :                            /* In case the user names something _FUN  */
   11920          144 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11921              :                 /* And a function template, or conversion operator needs
   11922              :                    the return type.  Except for the _FUN thunk of a
   11923              :                    generic lambda, which has a recursive decl_type'd
   11924              :                    return type.  */
   11925              :                 // FIXME: What if the return type is a voldemort?
   11926       617035 :                 key.ret = fndecl_declared_return_type (inner);
   11927              :             }
   11928              :           break;
   11929              : 
   11930       172484 :         case MK_field:
   11931       172484 :           {
   11932       172484 :             unsigned ix = 0;
   11933       172484 :             if (TREE_CODE (inner) != FIELD_DECL)
   11934              :               name = NULL_TREE;
   11935              :             else
   11936        29775 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11937              : 
   11938       172484 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11939      3502533 :                  ; field = DECL_CHAIN (field))
   11940              :               {
   11941      3675017 :                 tree finner = STRIP_TEMPLATE (field);
   11942      3675017 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11943              :                   {
   11944      1263712 :                     if (finner == inner)
   11945              :                       break;
   11946      1091228 :                     ix++;
   11947              :                   }
   11948      3502533 :               }
   11949       172484 :             key.index = ix;
   11950              :           }
   11951       172484 :           break;
   11952              : 
   11953         8426 :         case MK_vtable:
   11954         8426 :           {
   11955         8426 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11956        11042 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11957        11042 :               if (vtable == decl)
   11958              :                 {
   11959         8426 :                   key.index = ix;
   11960         8426 :                   break;
   11961              :                 }
   11962         8426 :             name = NULL_TREE;
   11963              :           }
   11964         8426 :           break;
   11965              : 
   11966        88791 :         case MK_as_base:
   11967        88791 :           gcc_checking_assert
   11968              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11969              :           break;
   11970              : 
   11971        24768 :         case MK_local_friend:
   11972        24768 :           {
   11973              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   11974              :                point to the class in push_template_decl or grokfndecl.  */
   11975        24768 :             unsigned ix = 0;
   11976        24768 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   11977       690084 :                  decls; decls = TREE_CHAIN (decls))
   11978       690084 :               if (!TREE_PURPOSE (decls))
   11979              :                 {
   11980        94869 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   11981        94869 :                   if (frnd == decl)
   11982              :                     break;
   11983        70101 :                   ix++;
   11984              :                 }
   11985        24768 :             key.index = ix;
   11986        24768 :             name = NULL_TREE;
   11987              :           }
   11988        24768 :           break;
   11989              : 
   11990        19671 :         case MK_local_type:
   11991        19671 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   11992        19671 :           break;
   11993              : 
   11994         1171 :         case MK_enum:
   11995         1171 :           {
   11996              :             /* Anonymous enums are located by their first identifier,
   11997              :                and underlying type.  */
   11998         1171 :             tree type = TREE_TYPE (decl);
   11999              : 
   12000         1171 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12001              :             /* Using the type name drops the bit precision we might
   12002              :                have been using on the enum.  */
   12003         1171 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12004         1171 :             if (tree values = TYPE_VALUES (type))
   12005         1171 :               name = DECL_NAME (TREE_VALUE (values));
   12006              :           }
   12007              :           break;
   12008              : 
   12009         1210 :         case MK_keyed:
   12010         1210 :           {
   12011         1210 :             tree scope = get_keyed_decl_scope (inner);
   12012         1210 :             gcc_checking_assert (scope);
   12013              : 
   12014         1210 :             auto *root = keyed_table->get (scope);
   12015         1210 :             unsigned ix = root->length ();
   12016              :             /* If we don't find it, we'll write a really big number
   12017              :                that the reader will ignore.  */
   12018         1328 :             while (ix--)
   12019         1328 :               if ((*root)[ix] == inner)
   12020              :                 break;
   12021              : 
   12022              :             /* Use the keyed-to decl as the 'name'.  */
   12023         1210 :             name = scope;
   12024         1210 :             key.index = ix;
   12025              :           }
   12026         1210 :           break;
   12027              : 
   12028        57144 :         case MK_partial:
   12029        57144 :           {
   12030        57144 :             tree ti = get_template_info (inner);
   12031        57144 :             key.constraints = get_constraints (inner);
   12032        57144 :             key.ret = TI_TEMPLATE (ti);
   12033        57144 :             key.args = TI_ARGS (ti);
   12034              :           }
   12035        57144 :           break;
   12036              :         }
   12037              : 
   12038      2376805 :       tree_node (name);
   12039      2376805 :       if (streaming_p ())
   12040              :         {
   12041              :           /* Check we have enough bits for the index.  */
   12042       846151 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12043              : 
   12044       846151 :           unsigned code = ((key.ref_q << 0)
   12045       846151 :                            | (key.coro_disc << 2)
   12046       846151 :                            | (key.iobj_p << 4)
   12047       846151 :                            | (key.xobj_p << 5)
   12048       846151 :                            | (key.index << 6));
   12049       846151 :           u (code);
   12050              :         }
   12051              : 
   12052      2376805 :       if (mk == MK_enum)
   12053         1171 :         tree_node (key.ret);
   12054      2375634 :       else if (mk == MK_partial
   12055      2318490 :                || (mk == MK_named && inner
   12056      2003140 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12057              :         {
   12058      1172191 :           tree_node (key.ret);
   12059      1172191 :           tree arg = key.args;
   12060      1172191 :           if (mk == MK_named)
   12061      3253053 :             while (arg && arg != void_list_node)
   12062              :               {
   12063      2138006 :                 tree_node (TREE_VALUE (arg));
   12064      2138006 :                 arg = TREE_CHAIN (arg);
   12065              :               }
   12066      1172191 :           tree_node (arg);
   12067      1172191 :           tree_node (key.constraints);
   12068              :         }
   12069              :     }
   12070      4434992 : }
   12071              : 
   12072              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12073              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12074              :    has been found by a proxy.  It will be an enum type located by its
   12075              :    first member.
   12076              : 
   12077              :    We're conservative with matches, so ambiguous decls will be
   12078              :    registered as different, then lead to a lookup error if the two
   12079              :    modules are both visible.  Perhaps we want to do something similar
   12080              :    to duplicate decls to get ODR errors on loading?  We already have
   12081              :    some special casing for namespaces.  */
   12082              : 
   12083              : static tree
   12084       302214 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12085              : {
   12086       302214 :   tree found = NULL_TREE;
   12087      1442900 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12088              :     {
   12089       692448 :       tree match = *iter;
   12090              : 
   12091       692448 :       tree d_inner = decl;
   12092       692448 :       tree m_inner = match;
   12093              : 
   12094       800087 :     again:
   12095       800087 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12096              :         {
   12097       123160 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12098       123160 :               && !DECL_NAMESPACE_ALIAS (match))
   12099              :             /* Namespaces are never overloaded.  */
   12100              :             found = match;
   12101              : 
   12102       123160 :           continue;
   12103              :         }
   12104              : 
   12105       676927 :       switch (TREE_CODE (d_inner))
   12106              :         {
   12107       257342 :         case TEMPLATE_DECL:
   12108       257342 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12109              :             {
   12110       107639 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12111       107639 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12112       107639 :               if (d_inner == error_mark_node
   12113       107639 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12114              :                 {
   12115              :                   found = match;
   12116              :                   break;
   12117              :                 }
   12118       107639 :               goto again;
   12119              :             }
   12120              :           break;
   12121              : 
   12122       319153 :         case FUNCTION_DECL:
   12123       319153 :           if (tree m_type = TREE_TYPE (m_inner))
   12124       319153 :             if ((!key.ret
   12125       158513 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12126       292527 :                 && type_memfn_rqual (m_type) == key.ref_q
   12127       292261 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12128       584280 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12129       292240 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12130       131537 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12131              :                 /* Reject if old is a "C" builtin and new is not "C".
   12132              :                    Matches decls_match behaviour.  */
   12133       131534 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12134         8160 :                     || !DECL_EXTERN_C_P (m_inner)
   12135         7945 :                     || DECL_EXTERN_C_P (d_inner))
   12136              :                 /* Reject if one is a different member of a
   12137              :                    guarded/pre/post fn set.  */
   12138       131509 :                 && (!flag_contracts
   12139       241320 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12140       120660 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12141       450662 :                 && (!flag_contracts
   12142       241320 :                     || (DECL_IS_POST_FN_P (d_inner)
   12143       120660 :                         == DECL_IS_POST_FN_P (m_inner))))
   12144              :               {
   12145       131509 :                 tree m_reqs = get_constraints (m_inner);
   12146       131509 :                 if (m_reqs)
   12147              :                   {
   12148         9641 :                     if (cxx_dialect < cxx20)
   12149            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12150              :                     else
   12151        19274 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12152              :                   }
   12153              : 
   12154       131509 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12155       170920 :                   found = match;
   12156              :               }
   12157              :           break;
   12158              : 
   12159        60946 :         case TYPE_DECL:
   12160       121892 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12161        60946 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12162              :             {
   12163        60915 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12164        60786 :                 return match;
   12165          129 :               else if (mk == MK_enum
   12166          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12167          129 :                            == key.ret))
   12168              :                 found = match;
   12169              :             }
   12170              :           /* With -freflection, typedef struct { } A is now represented the same
   12171              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12172              :              to name lookup, so we won't be able to find and match it directly.
   12173              :              But we will find the in-TU A (m_inner), through which we can obtain
   12174              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12175           31 :           else if (flag_reflection
   12176            5 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12177           34 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12178              :             {
   12179            3 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12180            3 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12181            6 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12182              :                 found = orig;
   12183              :             }
   12184              :           break;
   12185              : 
   12186              :         default:
   12187              :           found = match;
   12188              :           break;
   12189              :         }
   12190              :     }
   12191              : 
   12192       241428 :   return found;
   12193              : }
   12194              : 
   12195              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12196              :    the bools have been filled in.  Read its merging key and merge it.
   12197              :    Returns the existing decl if there is one.  */
   12198              : 
   12199              : tree
   12200      1203585 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12201              :                          tree type, tree container, bool is_attached,
   12202              :                          bool is_imported_temploid_friend)
   12203              : {
   12204      1203585 :   const char *kind = "new";
   12205      1203585 :   tree existing = NULL_TREE;
   12206              : 
   12207      1203585 :   if (mk & MK_template_mask)
   12208              :     {
   12209              :       // FIXME: We could stream the specialization hash?
   12210       371998 :       spec_entry spec;
   12211       371998 :       spec.tmpl = tree_node ();
   12212       371998 :       spec.args = tree_node ();
   12213              : 
   12214       371998 :       if (get_overrun ())
   12215            0 :         return error_mark_node;
   12216              : 
   12217       371998 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12218       371998 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12219       371998 :       DECL_NAME (inner) = DECL_NAME (decl);
   12220       371998 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12221              : 
   12222       371998 :       tree constr = NULL_TREE;
   12223       371998 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12224       371998 :       if (is_decl)
   12225              :         {
   12226       265823 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12227              :             {
   12228         5273 :               constr = tree_node ();
   12229         5273 :               if (constr)
   12230            0 :                 set_constraints (inner, constr);
   12231              :             }
   12232       529022 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12233              :         }
   12234              :       else
   12235       106175 :         spec.spec = type;
   12236       371998 :       existing = match_mergeable_specialization (is_decl, &spec);
   12237       371998 :       if (constr)
   12238              :         /* We'll add these back later, if this is the new decl.  */
   12239            0 :         remove_constraints (inner);
   12240              : 
   12241       371998 :       if (!existing)
   12242              :         ; /* We'll add to the table once read.  */
   12243       150545 :       else if (mk & MK_tmpl_decl_mask)
   12244              :         {
   12245              :           /* A declaration specialization.  */
   12246       104395 :           if (mk & MK_tmpl_tmpl_mask)
   12247         1053 :             existing = DECL_TI_TEMPLATE (existing);
   12248              :         }
   12249              :       else
   12250              :         {
   12251              :           /* A type specialization.  */
   12252        46150 :           if (mk & MK_tmpl_tmpl_mask)
   12253          241 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12254              :           else
   12255        45909 :             existing = TYPE_NAME (existing);
   12256              :         }
   12257              :     }
   12258       831587 :   else if (mk == MK_unique)
   12259              :     kind = "unique";
   12260              :   else
   12261              :     {
   12262       621626 :       tree name = tree_node ();
   12263              : 
   12264       621626 :       merge_key key;
   12265       621626 :       unsigned code = u ();
   12266       621626 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12267       621626 :       key.coro_disc = (code >> 2) & 3;
   12268       621626 :       key.iobj_p = (code >> 4) & 1;
   12269       621626 :       key.xobj_p = (code >> 5) & 1;
   12270       621626 :       key.index = code >> 6;
   12271              : 
   12272       621626 :       if (mk == MK_enum)
   12273          236 :         key.ret = tree_node ();
   12274       621390 :       else if (mk == MK_partial
   12275       610627 :                || ((mk == MK_named || mk == MK_friend_spec)
   12276       518924 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12277              :         {
   12278       298912 :           key.ret = tree_node ();
   12279       298912 :           tree arg, *arg_ptr = &key.args;
   12280       298912 :           while ((arg = tree_node ())
   12281       845372 :                  && arg != void_list_node
   12282      1405563 :                  && mk != MK_partial)
   12283              :             {
   12284       547944 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12285       547944 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12286              :             }
   12287       298912 :           *arg_ptr = arg;
   12288       298912 :           key.constraints = tree_node ();
   12289              :         }
   12290              : 
   12291       621626 :       if (get_overrun ())
   12292            0 :         return error_mark_node;
   12293              : 
   12294       621626 :       if (mk < MK_indirect_lwm)
   12295              :         {
   12296       609647 :           DECL_NAME (decl) = name;
   12297       609647 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12298              :         }
   12299       621626 :       DECL_NAME (inner) = DECL_NAME (decl);
   12300       621626 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12301              : 
   12302       621626 :       if (mk == MK_partial)
   12303              :         {
   12304        10763 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12305        55314 :                spec; spec = TREE_CHAIN (spec))
   12306              :             {
   12307        50711 :               tree tmpl = TREE_VALUE (spec);
   12308        50711 :               tree ti = get_template_info (tmpl);
   12309        50711 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12310        57664 :                   && cp_tree_equal (key.constraints,
   12311              :                                     get_constraints
   12312         6953 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12313              :                 {
   12314              :                   existing = tmpl;
   12315              :                   break;
   12316              :                 }
   12317              :             }
   12318              :         }
   12319       610863 :       else if (mk == MK_keyed
   12320          397 :                && DECL_LANG_SPECIFIC (name)
   12321       611260 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12322              :         {
   12323          397 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12324              :                                || TREE_CODE (container) == TYPE_DECL
   12325              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12326          397 :           if (auto *set = keyed_table->get (name))
   12327       621775 :             if (key.index < set->length ())
   12328              :               {
   12329          149 :                 existing = (*set)[key.index];
   12330          149 :                 if (existing)
   12331              :                   {
   12332          149 :                     gcc_checking_assert
   12333              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12334          149 :                     if (inner != decl)
   12335           91 :                       existing
   12336           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12337              :                   }
   12338              :               }
   12339              :         }
   12340              :       else
   12341       610466 :         switch (TREE_CODE (container))
   12342              :           {
   12343            0 :           default:
   12344            0 :             gcc_unreachable ();
   12345              : 
   12346       149242 :           case NAMESPACE_DECL:
   12347       149242 :             if (is_attached
   12348       149242 :                 && !is_imported_temploid_friend
   12349       149242 :                 && !(state->is_module () || state->is_partition ()))
   12350              :               kind = "unique";
   12351              :             else
   12352              :               {
   12353       147173 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12354       147173 :                 tree mvec;
   12355       147173 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12356              :                                                          is_attached, &mvec);
   12357       147173 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12358       147173 :                 if (!existing)
   12359        69985 :                   add_mergeable_namespace_entity (vslot, decl);
   12360              :                 else
   12361              :                   {
   12362              :                     /* Note that we now have duplicates to deal with in
   12363              :                        name lookup.  */
   12364        77188 :                     if (is_attached)
   12365           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12366              :                     else
   12367        77122 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12368              :                   }
   12369              :               }
   12370       153459 :             break;
   12371              : 
   12372         4217 :           case FUNCTION_DECL:
   12373         4217 :             gcc_checking_assert (mk == MK_local_type);
   12374         4217 :             existing = key_local_type (key, container, name);
   12375         4217 :             if (existing && inner != decl)
   12376         3426 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12377              :             break;
   12378              : 
   12379       457007 :           case TYPE_DECL:
   12380       457007 :             gcc_checking_assert (!is_imported_temploid_friend);
   12381       457007 :             int use_tmpl = 0;
   12382         5931 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12383              :                 /* Implicit or in-class defaulted member functions
   12384              :                    can come from anywhere.  */
   12385         4261 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12386         1259 :                      && !DECL_THUNK_P (decl)
   12387         1259 :                      && DECL_DEFAULTED_FN (decl)
   12388          808 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12389              :                 /* As can members of template specialisations.  */
   12390       460460 :                 && !(node_template_info (container, use_tmpl)
   12391         1295 :                      && use_tmpl != 0))
   12392              :               kind = "unique";
   12393              :             else
   12394              :               {
   12395       453802 :                 tree ctx = TREE_TYPE (container);
   12396              : 
   12397              :                 /* For some reason templated enumeral types are not marked
   12398              :                    as COMPLETE_TYPE_P, even though they have members.
   12399              :                    This may well be a bug elsewhere.  */
   12400       453802 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12401        15474 :                   existing = find_enum_member (ctx, name);
   12402       438328 :                 else if (COMPLETE_TYPE_P (ctx))
   12403              :                   {
   12404       194873 :                     switch (mk)
   12405              :                       {
   12406            0 :                       default:
   12407            0 :                         gcc_unreachable ();
   12408              : 
   12409       155482 :                       case MK_named:
   12410       155482 :                         existing = lookup_class_binding (ctx, name);
   12411       155482 :                         if (existing)
   12412              :                           {
   12413       155041 :                             tree inner = decl;
   12414       155041 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12415       155041 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12416        69016 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12417              : 
   12418       155041 :                             existing = check_mergeable_decl
   12419       155041 :                               (mk, inner, existing, key);
   12420              : 
   12421       155041 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12422              :                               {} // FIXME: Insert into specialization
   12423              :                             // tables, we'll need the arguments for that!
   12424              :                           }
   12425              :                         break;
   12426              : 
   12427        26877 :                       case MK_field:
   12428        26877 :                         {
   12429        26877 :                           unsigned ix = key.index;
   12430        26877 :                           for (tree field = TYPE_FIELDS (ctx);
   12431       892902 :                                field; field = DECL_CHAIN (field))
   12432              :                             {
   12433       892902 :                               tree finner = STRIP_TEMPLATE (field);
   12434       892902 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12435       299729 :                                 if (!ix--)
   12436              :                                   {
   12437              :                                     existing = field;
   12438              :                                     break;
   12439              :                                   }
   12440              :                             }
   12441              :                         }
   12442              :                         break;
   12443              : 
   12444         1484 :                       case MK_vtable:
   12445         1484 :                         {
   12446         1484 :                           unsigned ix = key.index;
   12447         1484 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12448         1823 :                                vtable; vtable = DECL_CHAIN (vtable))
   12449         1570 :                             if (!ix--)
   12450              :                               {
   12451              :                                 existing = vtable;
   12452              :                                 break;
   12453              :                               }
   12454              :                         }
   12455              :                         break;
   12456              : 
   12457         8001 :                       case MK_as_base:
   12458         8001 :                         {
   12459         8001 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12460         8001 :                           if (as_base && as_base != ctx)
   12461         8001 :                             existing = TYPE_NAME (as_base);
   12462              :                         }
   12463              :                         break;
   12464              : 
   12465         3029 :                       case MK_local_friend:
   12466         3029 :                         {
   12467         3029 :                           unsigned ix = key.index;
   12468         3029 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12469        85052 :                                decls; decls = TREE_CHAIN (decls))
   12470        85052 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12471              :                               {
   12472         3029 :                                 existing
   12473         3029 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12474         3029 :                                 break;
   12475              :                               }
   12476              :                         }
   12477              :                         break;
   12478              :                       }
   12479              : 
   12480       194873 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12481       190630 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12482       282842 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12483              :                       {
   12484        71011 :                         tree ti;
   12485        71011 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12486          909 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12487              :                         else
   12488        70102 :                           ti = DECL_TEMPLATE_INFO (existing);
   12489        71011 :                         existing = TI_TEMPLATE (ti);
   12490              :                       }
   12491              :                   }
   12492              :               }
   12493              :           }
   12494              :     }
   12495              : 
   12496      1203585 :   dump (dumper::MERGE)
   12497         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12498         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12499              : 
   12500              :   return existing;
   12501              : }
   12502              : 
   12503              : void
   12504       346697 : trees_out::binfo_mergeable (tree binfo)
   12505              : {
   12506       346697 :   tree dom = binfo;
   12507       438587 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12508              :     dom = parent;
   12509       346697 :   tree type = BINFO_TYPE (dom);
   12510       346697 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12511       346697 :   tree_node (type);
   12512       346697 :   if (streaming_p ())
   12513              :     {
   12514              :       unsigned ix = 0;
   12515       185331 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12516        48356 :         ix++;
   12517       136975 :       u (ix);
   12518              :     }
   12519       346697 : }
   12520              : 
   12521              : unsigned
   12522        92283 : trees_in::binfo_mergeable (tree *type)
   12523              : {
   12524        92283 :   *type = tree_node ();
   12525        92283 :   return u ();
   12526              : }
   12527              : 
   12528              : /* DECL is a just streamed declaration with attributes DATTR that should
   12529              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12530              :    ABI tags match, and report an error if not.  */
   12531              : 
   12532              : void
   12533       297812 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12534              : {
   12535       297812 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12536       297812 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12537       297812 :   if ((etags == nullptr) != (dtags == nullptr)
   12538       297812 :       || (etags && !attribute_value_equal (etags, dtags)))
   12539              :     {
   12540           33 :       if (etags)
   12541           24 :         etags = TREE_VALUE (etags);
   12542           33 :       if (dtags)
   12543           24 :         dtags = TREE_VALUE (dtags);
   12544              : 
   12545              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12546              :          Since tags might have been inherited during mangling, ignore
   12547              :          inherited tags if there's a mangled-ness mismatch.  */
   12548           33 :       bool ignore_inherited_p
   12549           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12550           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12551           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12552              :         {
   12553           21 :           auto_diagnostic_group d;
   12554           21 :           if (dtags)
   12555           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12556              :                       "mismatching abi tags for %qD with tags %qE",
   12557              :                       decl, dtags);
   12558              :           else
   12559            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12560              :                       "mismatching abi tags for %qD with no tags", decl);
   12561           21 :           if (etags)
   12562           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12563              :                     "existing declaration here with tags %qE", etags);
   12564              :           else
   12565            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12566              :                     "existing declaration here with no tags");
   12567           21 :         }
   12568              : 
   12569              :       /* Always use the existing abi_tags as the canonical set so that
   12570              :          later processing doesn't get confused.  */
   12571           33 :       if (dtags)
   12572           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12573           33 :       if (etags)
   12574           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12575              :     }
   12576       297812 : }
   12577              : 
   12578              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12579              :    it does and issue an appropriate diagnostic if not.  Merge any
   12580              :    bits from DECL to EXISTING.  This is stricter matching than
   12581              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12582              :    decls_match because it can cause instantiations of constraints.  */
   12583              : 
   12584              : bool
   12585       436559 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12586              : {
   12587              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12588              :   // (beware, default parms should be the same?)  Can we just call
   12589              :   // duplicate_decls and teach it how to handle the module-specific
   12590              :   // permitted/required duplications?
   12591              : 
   12592              :   // We know at this point that the decls have matched by key, so we
   12593              :   // can elide some of the checking
   12594       436559 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12595              : 
   12596       436559 :   tree d_inner = decl;
   12597       436559 :   tree e_inner = existing;
   12598       436559 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12599              :     {
   12600       145632 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12601       145632 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12602       145632 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12603              :     }
   12604              : 
   12605              :   // FIXME: do more precise errors at point of mismatch
   12606       436559 :   const char *mismatch_msg = nullptr;
   12607              : 
   12608       436559 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12609       436559 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12610              :     {
   12611            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12612              :                         "declaration %#qD");
   12613            6 :       goto mismatch;
   12614              :     }
   12615       436553 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12616              :     {
   12617       200028 :       tree e_ret = fndecl_declared_return_type (existing);
   12618       200028 :       tree d_ret = fndecl_declared_return_type (decl);
   12619              : 
   12620        85458 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12621       200052 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12622              :         /* This has a recursive type that will compare different.  */;
   12623       200016 :       else if (!same_type_p (d_ret, e_ret))
   12624              :         {
   12625           21 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12626           21 :           goto mismatch;
   12627              :         }
   12628              : 
   12629       200007 :       tree& e_type = TREE_TYPE (e_inner);
   12630       200007 :       tree d_type = TREE_TYPE (d_inner);
   12631              : 
   12632       200007 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12633       200007 :              d_args = TYPE_ARG_TYPES (d_type);
   12634       343511 :            e_args != d_args && (e_args || d_args);
   12635       143504 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12636              :         {
   12637       143504 :           if (!(e_args && d_args))
   12638              :             {
   12639            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12640              :                                 "declaration %#qD");
   12641            0 :               goto mismatch;
   12642              :             }
   12643              : 
   12644       143504 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12645              :             {
   12646            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12647              :                                 "declaration %#qD");
   12648            0 :               goto mismatch;
   12649              :             }
   12650              :         }
   12651              : 
   12652              :       /* If EXISTING has an undeduced or uninstantiated exception
   12653              :          specification, but DECL does not, propagate the exception
   12654              :          specification.  Otherwise we end up asserting or trying to
   12655              :          instantiate it in the middle of loading.   */
   12656       200007 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12657       200007 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12658       292717 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12659              :         {
   12660        31536 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12661        15710 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12662        47015 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12663        25658 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12664              :             {
   12665          147 :               dump (dumper::MERGE)
   12666            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12667          147 :               gcc_checking_assert (existing == e_inner);
   12668          147 :               e_type = build_exception_variant (e_type, d_spec);
   12669              : 
   12670              :               /* Propagate to existing clones.  */
   12671          147 :               tree clone;
   12672          405 :               FOR_EACH_CLONE (clone, existing)
   12673          258 :                 TREE_TYPE (clone)
   12674          516 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12675              :             }
   12676              :         }
   12677       184181 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12678       262851 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12679       368361 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12680              :         {
   12681         1737 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12682              :                             "imported declaration %#qD");
   12683         1737 :           goto mismatch;
   12684              :         }
   12685              : 
   12686              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12687              :          is already deduced.  */
   12688       198270 :       bool e_undeduced = undeduced_auto_decl (existing);
   12689       198270 :       bool d_undeduced = undeduced_auto_decl (decl);
   12690       198270 :       if (e_undeduced && !d_undeduced)
   12691              :         {
   12692           16 :           dump (dumper::MERGE)
   12693            0 :             && dump ("Propagating deduced return type to %N", existing);
   12694           16 :           gcc_checking_assert (existing == e_inner);
   12695           16 :           FNDECL_USED_AUTO (existing) = true;
   12696           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12697           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12698              :         }
   12699       198254 :       else if (d_undeduced && !e_undeduced)
   12700              :         /* EXISTING was deduced, leave it alone.  */;
   12701       198251 :       else if (type_uses_auto (d_ret)
   12702       198251 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12703              :         {
   12704            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12705              :                             "imported declaration %#qD");
   12706            9 :           goto mismatch;
   12707              :         }
   12708              : 
   12709              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12710              :          is already deduced.  */
   12711       198261 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12712       198261 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12713              :         /* Already matches.  */;
   12714            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12715            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12716            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12717              :         /* DECL was deduced, copy to EXISTING.  */
   12718              :         {
   12719            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12720            1 :           if (decl_implicit_constexpr_p (d_inner))
   12721            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12722              :         }
   12723            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12724            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12725            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12726              :         /* EXISTING was deduced, leave it alone.  */;
   12727              :       else
   12728              :         {
   12729            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12730              :                             "declaration %#qD");
   12731            0 :           goto mismatch;
   12732              :         }
   12733              : 
   12734              :       /* Don't synthesize a defaulted function if we're importing one
   12735              :          we've already determined.  */
   12736       198261 :       if (!DECL_MAYBE_DELETED (d_inner))
   12737       198144 :         DECL_MAYBE_DELETED (e_inner) = false;
   12738              :     }
   12739       236525 :   else if (is_typedef)
   12740              :     {
   12741        84392 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12742        84392 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12743              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12744              :         {
   12745            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12746            3 :           goto mismatch;
   12747              :         }
   12748              :     }
   12749              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12750              :      here. I suspect the entities that directly do that are things
   12751              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12752       152133 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12753              :     {
   12754              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12755         2498 :     mismatch:
   12756         2498 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12757              :         /* Just like duplicate_decls, presum the user knows what
   12758              :            they're doing in overriding a builtin.   */
   12759         1755 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12760          743 :       else if (decl_function_context (decl))
   12761              :         /* The type of a mergeable local entity (such as a function scope
   12762              :            capturing lambda's closure type fields) can depend on an
   12763              :            unmergeable local entity (such as a local variable), so type
   12764              :            equality isn't feasible in general for local entities.  */;
   12765              :       else
   12766              :         {
   12767           21 :           gcc_checking_assert (mismatch_msg);
   12768           21 :           auto_diagnostic_group d;
   12769           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12770           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12771              :                   "existing declaration %#qD", existing);
   12772           21 :           return false;
   12773           21 :         }
   12774              :     }
   12775              : 
   12776       436538 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12777       436538 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12778              :     {
   12779              :       /* We're matching a builtin that the user has yet to declare.
   12780              :          We are the one!  This is very much duplicate-decl
   12781              :          shenanigans. */
   12782         2028 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12783         2028 :       if (TREE_CODE (decl) != TYPE_DECL)
   12784              :         {
   12785              :           /* Propagate exceptions etc.  */
   12786         2007 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12787         2007 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12788              :         }
   12789              :       /* This is actually an import! */
   12790         2028 :       DECL_MODULE_IMPORT_P (existing) = true;
   12791              : 
   12792              :       /* Yay, sliced!  */
   12793         2028 :       existing->base = decl->base;
   12794              : 
   12795         2028 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12796              :         {
   12797              :           /* Ew :(  */
   12798         2007 :           memcpy (&existing->decl_common.size,
   12799              :                   &decl->decl_common.size,
   12800              :                   (offsetof (tree_decl_common, pt_uid)
   12801              :                    - offsetof (tree_decl_common, size)));
   12802         2007 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12803         2007 :           existing->function_decl.built_in_class = bltin_class;
   12804         2007 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12805         2007 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12806         2007 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12807              :             {
   12808         1809 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12809         1809 :                 switch (fncode)
   12810              :                   {
   12811            0 :                   case BUILT_IN_STPCPY:
   12812            0 :                     set_builtin_decl_implicit_p
   12813            0 :                       (built_in_function (fncode), true);
   12814            0 :                     break;
   12815         1809 :                   default:
   12816         1809 :                     set_builtin_decl_declared_p
   12817         1809 :                       (built_in_function (fncode), true);
   12818         1809 :                     break;
   12819              :                   }
   12820         1809 :               copy_attributes_to_builtin (decl);
   12821              :             }
   12822              :         }
   12823              :     }
   12824              : 
   12825       436538 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12826       436538 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12827              :     /* Don't instantiate again!  */
   12828         9079 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12829              : 
   12830       436538 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12831       436538 :       && DECL_DECLARED_INLINE_P (d_inner))
   12832              :     {
   12833       161697 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12834       161697 :       if (!DECL_SAVED_TREE (e_inner)
   12835        80162 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12836       161714 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12837              :         {
   12838           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12839           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12840           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12841           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12842              :           // TODO: we will eventually want to merge all decl attributes
   12843           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12844           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12845              :         }
   12846              :     }
   12847       436538 :   if (!DECL_EXTERNAL (d_inner))
   12848       204839 :     DECL_EXTERNAL (e_inner) = false;
   12849              : 
   12850       436538 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12851       440922 :     check_abi_tags (existing, decl,
   12852       220461 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12853              : 
   12854       436538 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12855              :     {
   12856              :       /* Merge default template arguments.  */
   12857       145630 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12858       145630 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12859       145630 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12860              :                            == TREE_VEC_LENGTH (e_parms));
   12861       417424 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12862              :         {
   12863       271803 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12864       271803 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12865       271803 :           if (e_default == NULL_TREE)
   12866       230078 :             e_default = d_default;
   12867        41725 :           else if (d_default != NULL_TREE
   12868        41725 :                    && !cp_tree_equal (d_default, e_default))
   12869              :             {
   12870            9 :               auto_diagnostic_group d;
   12871            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12872            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12873            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12874              :                         "conflicting default argument for %#qD", d_parm);
   12875            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12876              :                       "existing default declared here");
   12877            9 :               return false;
   12878            9 :             }
   12879              :         }
   12880              :     }
   12881              : 
   12882       436529 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12883              :     {
   12884              :       /* Merge default function arguments.  */
   12885       200019 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12886       200019 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12887       200019 :       int i = 0;
   12888       476957 :       for (; d_parm && d_parm != void_list_node;
   12889       276938 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12890              :         {
   12891       276950 :           tree d_default = TREE_PURPOSE (d_parm);
   12892       276950 :           tree& e_default = TREE_PURPOSE (e_parm);
   12893       276950 :           if (e_default == NULL_TREE)
   12894       260374 :             e_default = d_default;
   12895        16576 :           else if (d_default != NULL_TREE
   12896        16576 :                    && !cp_tree_equal (d_default, e_default))
   12897              :             {
   12898           12 :               auto_diagnostic_group d;
   12899           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12900              :                         "conflicting default argument for parameter %P of %#qD",
   12901              :                         i, decl);
   12902           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12903              :                       "existing default declared here");
   12904           12 :               return false;
   12905           12 :             }
   12906              :         }
   12907              :     }
   12908              : 
   12909              :   return true;
   12910              : }
   12911              : 
   12912              : /* FN is an implicit member function that we've discovered is new to
   12913              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12914              :    Reset the appropriate classtype lazy flag.   */
   12915              : 
   12916              : bool
   12917          961 : trees_in::install_implicit_member (tree fn)
   12918              : {
   12919          961 :   tree ctx = DECL_CONTEXT (fn);
   12920          961 :   tree name = DECL_NAME (fn);
   12921              :   /* We know these are synthesized, so the set of expected prototypes
   12922              :      is quite restricted.  We're not validating correctness, just
   12923              :      distinguishing beteeen the small set of possibilities.  */
   12924          961 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12925          961 :   if (IDENTIFIER_CTOR_P (name))
   12926              :     {
   12927          659 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12928          659 :           && VOID_TYPE_P (parm_type))
   12929          167 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12930          492 :       else if (!TYPE_REF_P (parm_type))
   12931              :         return false;
   12932          492 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12933          492 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12934          249 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12935          243 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12936          243 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12937              :       else
   12938              :         return false;
   12939              :     }
   12940          302 :   else if (IDENTIFIER_DTOR_P (name))
   12941              :     {
   12942          246 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12943          246 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12944              :       else
   12945              :         return false;
   12946          246 :       if (DECL_VIRTUAL_P (fn))
   12947              :         /* A virtual dtor should have been created when the class
   12948              :            became complete.  */
   12949              :         return false;
   12950              :     }
   12951           56 :   else if (name == assign_op_identifier)
   12952              :     {
   12953           56 :       if (!TYPE_REF_P (parm_type))
   12954              :         return false;
   12955           56 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12956           56 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12957           28 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12958           28 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12959           28 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12960              :       else
   12961              :         return false;
   12962              :     }
   12963              :   else
   12964              :     return false;
   12965              : 
   12966         1006 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12967              : 
   12968          961 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12969          961 :   TYPE_FIELDS (ctx) = fn;
   12970              : 
   12971          961 :   add_method (ctx, fn, false);
   12972              : 
   12973              :     /* Propagate TYPE_FIELDS.  */
   12974          961 :   fixup_type_variants (ctx);
   12975              : 
   12976          961 :   return true;
   12977              : }
   12978              : 
   12979              : /* Return non-zero if DECL has a definition that would be interesting to
   12980              :    write out.  */
   12981              : 
   12982              : static bool
   12983      2025894 : has_definition (tree decl)
   12984              : {
   12985      2025900 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   12986      2025900 :   if (is_tmpl)
   12987       449909 :     decl = DECL_TEMPLATE_RESULT (decl);
   12988              : 
   12989      2025900 :   switch (TREE_CODE (decl))
   12990              :     {
   12991              :     default:
   12992              :       break;
   12993              : 
   12994       763637 :     case FUNCTION_DECL:
   12995       763637 :       if (!DECL_SAVED_TREE (decl))
   12996              :         /* Not defined.  */
   12997              :         break;
   12998              : 
   12999       346627 :       if (DECL_DECLARED_INLINE_P (decl))
   13000              :         return true;
   13001              : 
   13002        29561 :       if (header_module_p ())
   13003              :         /* We always need to write definitions in header modules,
   13004              :            since there's no TU to emit them in otherwise.  */
   13005              :         return true;
   13006              : 
   13007        17484 :       if (DECL_TEMPLATE_INFO (decl))
   13008              :         {
   13009        16195 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13010              : 
   13011              :           // FIXME: Partial specializations have definitions too.
   13012        16195 :           if (use_tpl < 2)
   13013              :             return true;
   13014              :         }
   13015              : 
   13016              :       /* Coroutine transform functions always need to be emitted
   13017              :          into the importing TU if the ramp function will be.  */
   13018         1349 :       if (DECL_COROUTINE_P (decl))
   13019           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13020              :           return has_definition (ramp);
   13021              :       break;
   13022              : 
   13023       835816 :     case TYPE_DECL:
   13024       835816 :       {
   13025       835816 :         tree type = TREE_TYPE (decl);
   13026       835816 :         if (type == TYPE_MAIN_VARIANT (type)
   13027       383213 :             && decl == TYPE_NAME (type)
   13028      1219029 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13029       383213 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13030              :           return true;
   13031              :       }
   13032              :       break;
   13033              : 
   13034       107043 :     case VAR_DECL:
   13035              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13036       107043 :       if (DECL_LANG_SPECIFIC (decl)
   13037       106209 :           && DECL_TEMPLATE_INFO (decl)
   13038       174442 :           && DECL_INITIAL (decl))
   13039              :         return true;
   13040              :       else
   13041              :         {
   13042        44594 :           if (!DECL_INITIALIZED_P (decl))
   13043              :             /* Not defined.  */
   13044              :             return false;
   13045              : 
   13046        37403 :           if (header_module_p ())
   13047              :             /* We always need to write definitions in header modules,
   13048              :                since there's no TU to emit them in otherwise.  */
   13049              :             return true;
   13050              : 
   13051        12723 :           if (decl_maybe_constant_var_p (decl))
   13052              :             /* We might need its constant value.  */
   13053              :             return true;
   13054              : 
   13055          476 :           if (vague_linkage_p (decl))
   13056              :             /* These are emitted as needed.  */
   13057              :             return true;
   13058              : 
   13059              :           return false;
   13060              :         }
   13061         7725 :       break;
   13062              : 
   13063         7725 :     case CONCEPT_DECL:
   13064         7725 :       if (DECL_INITIAL (decl))
   13065              :         return true;
   13066              : 
   13067              :       break;
   13068              :     }
   13069              : 
   13070              :   return false;
   13071              : }
   13072              : 
   13073              : uintptr_t *
   13074       638789 : trees_in::find_duplicate (tree existing)
   13075              : {
   13076       297906 :   if (!duplicates)
   13077              :     return NULL;
   13078              : 
   13079       411301 :   return duplicates->get (existing);
   13080              : }
   13081              : 
   13082              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13083              :    known node.  */
   13084              : 
   13085              : void
   13086       623622 : trees_in::register_duplicate (tree decl, tree existing)
   13087              : {
   13088       623622 :   if (!duplicates)
   13089       102962 :     duplicates = new duplicate_hash_map (40);
   13090              : 
   13091       623622 :   bool existed;
   13092       623622 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13093       623622 :   gcc_checking_assert (!existed);
   13094       623622 :   slot = reinterpret_cast<uintptr_t> (decl);
   13095              : 
   13096       623622 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13097              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13098              :        that passing decl's _RESULT to maybe_duplicate naturally
   13099              :        gives us existing's _RESULT back.  */
   13100       291264 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13101       145632 :                         DECL_TEMPLATE_RESULT (existing));
   13102       623622 : }
   13103              : 
   13104              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13105              :    return MAYBE_EXISTING (into which the definition should be
   13106              :    installed).  Otherwise return NULL if already known bad, or the
   13107              :    duplicate we read (for ODR checking, or extracting additional merge
   13108              :    information).  */
   13109              : 
   13110              : tree
   13111       340883 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13112              : {
   13113       340883 :   tree res = NULL_TREE;
   13114              : 
   13115       527133 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13116              :     {
   13117       147588 :       if (!(*dup & 1))
   13118       147585 :         res = reinterpret_cast<tree> (*dup);
   13119              :     }
   13120              :   else
   13121              :     res = maybe_existing;
   13122              : 
   13123       340883 :   assert_definition (maybe_existing, res && !has_defn);
   13124              : 
   13125              :   // FIXME: We probably need to return the template, so that the
   13126              :   // template header can be checked?
   13127       340883 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13128              : }
   13129              : 
   13130              : /* The following writer functions rely on the current behaviour of
   13131              :    depset::hash::add_dependency making the decl and defn depset nodes
   13132              :    depend on eachother.  That way we don't have to worry about seeding
   13133              :    the tree map with named decls that cannot be looked up by name (I.e
   13134              :    template and function parms).  We know the decl and definition will
   13135              :    be in the same cluster, which is what we want.  */
   13136              : 
   13137              : void
   13138       572839 : trees_out::write_function_def (tree decl)
   13139              : {
   13140       572839 :   tree_node (DECL_RESULT (decl));
   13141              : 
   13142       572839 :   {
   13143              :     /* The function body for a non-inline function or function template
   13144              :        is ignored for determining exposures.  This should only matter
   13145              :        for templates (we don't emit the bodies of non-inline functions
   13146              :        to begin with).  */
   13147       572839 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13148       572839 :     tree_node (DECL_INITIAL (decl));
   13149       572839 :     tree_node (DECL_SAVED_TREE (decl));
   13150       572839 :   }
   13151              : 
   13152      1222896 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13153              : 
   13154       572839 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13155              : 
   13156       572839 :   if (streaming_p ())
   13157       286378 :     u (cexpr != nullptr);
   13158       572839 :   if (cexpr)
   13159              :     {
   13160       128633 :       chained_decls (cexpr->parms);
   13161       128633 :       tree_node (cexpr->result);
   13162       128633 :       tree_node (cexpr->body);
   13163              :     }
   13164              : 
   13165       572839 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13166              : 
   13167       572839 :   if (streaming_p ())
   13168              :     {
   13169       286378 :       unsigned flags = 0;
   13170              : 
   13171              :       /* Whether the importer should emit this definition, if used.  */
   13172       286378 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13173       286378 :                     && (get_importer_interface (decl)
   13174              :                         != importer_interface::external));
   13175              : 
   13176              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13177              :          on non-templates or we'll crash later in import_export_decl.  */
   13178       191046 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13179              :                            || (DECL_LANG_SPECIFIC (decl)
   13180              :                                && DECL_LOCAL_DECL_P (decl)
   13181              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13182              :                            || (DECL_LANG_SPECIFIC (decl)
   13183              :                                && DECL_TEMPLATE_INFO (decl)
   13184              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13185              : 
   13186       286378 :       if (f)
   13187              :         {
   13188       285280 :           flags |= 2;
   13189              :           /* These flags are needed in tsubst_lambda_expr.  */
   13190       285280 :           flags |= 4 * f->language->returns_value;
   13191       285280 :           flags |= 8 * f->language->returns_null;
   13192       285280 :           flags |= 16 * f->language->returns_abnormally;
   13193       285280 :           flags |= 32 * f->language->infinite_loop;
   13194              :         }
   13195              : 
   13196       286378 :       u (flags);
   13197              :     }
   13198              : 
   13199       572839 :   if (state && f)
   13200              :     {
   13201       570643 :       state->write_location (*this, f->function_start_locus);
   13202       570643 :       state->write_location (*this, f->function_end_locus);
   13203              :     }
   13204              : 
   13205       572839 :   if (DECL_COROUTINE_P (decl))
   13206              :     {
   13207           40 :       tree ramp = DECL_RAMP_FN (decl);
   13208           40 :       tree_node (ramp);
   13209           40 :       if (!ramp)
   13210              :         {
   13211           28 :           tree_node (DECL_ACTOR_FN (decl));
   13212           28 :           tree_node (DECL_DESTROY_FN (decl));
   13213              :         }
   13214              :     }
   13215       572839 : }
   13216              : 
   13217              : void
   13218            0 : trees_out::mark_function_def (tree)
   13219              : {
   13220            0 : }
   13221              : 
   13222              : bool
   13223       225992 : trees_in::read_function_def (tree decl, tree maybe_template)
   13224              : {
   13225       226529 :   dump () && dump ("Reading function definition %N", decl);
   13226       225992 :   tree result = tree_node ();
   13227       225992 :   tree initial = tree_node ();
   13228       225992 :   tree saved = tree_node ();
   13229       225992 :   tree context = tree_node ();
   13230       225992 :   post_process_data pdata {};
   13231       225992 :   pdata.decl = maybe_template;
   13232              : 
   13233       225992 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13234       451981 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13235              : 
   13236       225992 :   constexpr_fundef cexpr;
   13237       225992 :   if (u ())
   13238              :     {
   13239        50063 :       cexpr.parms = chained_decls ();
   13240        50063 :       cexpr.result = tree_node ();
   13241        50063 :       cexpr.body = tree_node ();
   13242        50063 :       cexpr.decl = decl;
   13243              :     }
   13244              :   else
   13245       175929 :     cexpr.decl = NULL_TREE;
   13246              : 
   13247       225992 :   unsigned flags = u ();
   13248       225992 :   if (flags & 2)
   13249              :     {
   13250       225122 :       pdata.start_locus = state->read_location (*this);
   13251       225122 :       pdata.end_locus = state->read_location (*this);
   13252       225122 :       pdata.returns_value = flags & 4;
   13253       225122 :       pdata.returns_null = flags & 8;
   13254       225122 :       pdata.returns_abnormally = flags & 16;
   13255       225122 :       pdata.infinite_loop = flags & 32;
   13256              :     }
   13257              : 
   13258       225992 :   tree coro_actor = NULL_TREE;
   13259       225992 :   tree coro_destroy = NULL_TREE;
   13260       225992 :   tree coro_ramp = NULL_TREE;
   13261       225992 :   if (DECL_COROUTINE_P (decl))
   13262              :     {
   13263           18 :       coro_ramp = tree_node ();
   13264           18 :       if (!coro_ramp)
   13265              :         {
   13266           12 :           coro_actor = tree_node ();
   13267           12 :           coro_destroy = tree_node ();
   13268           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13269            0 :             set_overrun ();
   13270              :         }
   13271              :     }
   13272              : 
   13273       225992 :   if (get_overrun ())
   13274              :     return NULL_TREE;
   13275              : 
   13276       225992 :   if (installing)
   13277              :     {
   13278       139674 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13279       139674 :       DECL_RESULT (decl) = result;
   13280       139674 :       DECL_INITIAL (decl) = initial;
   13281       139674 :       DECL_SAVED_TREE (decl) = saved;
   13282              : 
   13283              :       /* Some entities (like anticipated builtins) were declared without
   13284              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13285              :          already an argument list, because we've already built the
   13286              :          definition referencing those merged PARM_DECLs.  */
   13287       139674 :       if (!DECL_ARGUMENTS (decl))
   13288         6669 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13289              : 
   13290       139674 :       if (context)
   13291         5949 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13292       139674 :       if (cexpr.decl)
   13293        35976 :         register_constexpr_fundef (cexpr);
   13294              : 
   13295       139674 :       if (coro_ramp)
   13296            6 :         coro_set_ramp_function (decl, coro_ramp);
   13297       139668 :       else if (coro_actor && coro_destroy)
   13298            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13299              : 
   13300       139674 :       if (DECL_LOCAL_DECL_P (decl))
   13301              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13302              :            function structure to be allocated or to be expanded.  */
   13303            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13304              :       else
   13305       139671 :         post_process (pdata);
   13306              :     }
   13307              :   else if (maybe_dup)
   13308              :     {
   13309              :       // FIXME:QOI Check matching defn
   13310              :     }
   13311              : 
   13312              :   return true;
   13313              : }
   13314              : 
   13315              : /* Also for CONCEPT_DECLs.  */
   13316              : 
   13317              : void
   13318       136718 : trees_out::write_var_def (tree decl)
   13319              : {
   13320              :   /* The initializer of a non-inline variable or variable template is
   13321              :      ignored for determining exposures.  */
   13322       136718 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13323       148185 :                                            && !DECL_INLINE_VAR_P (decl));
   13324              : 
   13325       136718 :   tree init = DECL_INITIAL (decl);
   13326       136718 :   tree_node (init);
   13327       136718 :   if (!init)
   13328              :     {
   13329         1730 :       tree dyn_init = NULL_TREE;
   13330              : 
   13331              :       /* We only need to write initializers in header modules.  */
   13332         2912 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13333              :         {
   13334          450 :           dyn_init = value_member (decl,
   13335          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13336              :                                    ? tls_aggregates : static_aggregates);
   13337          450 :           gcc_checking_assert (dyn_init);
   13338              :           /* Mark it so write_inits knows this is needed.  */
   13339          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13340          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13341              :         }
   13342         1730 :       tree_node (dyn_init);
   13343              :     }
   13344       136718 : }
   13345              : 
   13346              : void
   13347            0 : trees_out::mark_var_def (tree)
   13348              : {
   13349            0 : }
   13350              : 
   13351              : bool
   13352        44536 : trees_in::read_var_def (tree decl, tree maybe_template)
   13353              : {
   13354              :   /* Do not mark the virtual table entries as used.  */
   13355        44536 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13356        44536 :   unused += vtable;
   13357        44536 :   tree init = tree_node ();
   13358        44536 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13359        44536 :   unused -= vtable;
   13360              : 
   13361        44536 :   if (get_overrun ())
   13362              :     return false;
   13363              : 
   13364        44536 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13365        44536 :                       : bool (DECL_INITIAL (decl)));
   13366        44536 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13367        44536 :   bool installing = maybe_dup && !initialized;
   13368        44536 :   if (installing)
   13369              :     {
   13370        27637 :       DECL_INITIAL (decl) = init;
   13371        27637 :       if (DECL_EXTERNAL (decl))
   13372         3524 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13373        27637 :       if (VAR_P (decl))
   13374              :         {
   13375        24266 :           DECL_INITIALIZED_P (decl) = true;
   13376        24266 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13377        23858 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13378        24266 :           tentative_decl_linkage (decl);
   13379        24266 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13380        24266 :               && !DECL_EXTERNAL (decl))
   13381            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13382              :           /* Class non-template static members are handled in read_class_def.
   13383              :              But still handle specialisations of member templates.  */
   13384        48532 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13385        15792 :                || primary_template_specialization_p (decl))
   13386        32859 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13387         8510 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13388           21 :                       && !DECL_EXTERNAL (decl))))
   13389           92 :             note_vague_linkage_variable (decl);
   13390              :         }
   13391        27637 :       if (!dyn_init)
   13392              :         ;
   13393          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13394           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13395              :       else
   13396          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13397              :     }
   13398              :   else if (maybe_dup)
   13399              :     {
   13400              :       // FIXME:QOI Check matching defn
   13401              :     }
   13402              : 
   13403              :   return true;
   13404              : }
   13405              : 
   13406              : /* If MEMBER doesn't have an independent life outside the class,
   13407              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13408              : 
   13409              : static tree
   13410       277022 : member_owned_by_class (tree member)
   13411              : {
   13412       277022 :   gcc_assert (DECL_P (member));
   13413              : 
   13414              :   /* Clones are owned by their origin.  */
   13415       277022 :   if (DECL_CLONED_FUNCTION_P (member))
   13416              :     return NULL;
   13417              : 
   13418       277022 :   if (TREE_CODE (member) == FIELD_DECL)
   13419              :     /* FIELD_DECLS can have template info in some cases.  We always
   13420              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13421              :        wrapping them.  */
   13422              :     return member;
   13423              : 
   13424       119137 :   int use_tpl = -1;
   13425       119137 :   if (tree ti = node_template_info (member, use_tpl))
   13426              :     {
   13427              :       // FIXME: Don't bail on things that CANNOT have their own
   13428              :       // template header.  No, make sure they're in the same cluster.
   13429            0 :       if (use_tpl > 0)
   13430              :         return NULL_TREE;
   13431              : 
   13432            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13433       277022 :         member = TI_TEMPLATE (ti);
   13434              :     }
   13435              :   return member;
   13436              : }
   13437              : 
   13438              : void
   13439       197984 : trees_out::write_class_def (tree defn)
   13440              : {
   13441       197984 :   gcc_assert (DECL_P (defn));
   13442       197984 :   if (streaming_p ())
   13443        99389 :     dump () && dump ("Writing class definition %N", defn);
   13444              : 
   13445       197984 :   tree type = TREE_TYPE (defn);
   13446       197984 :   tree_node (TYPE_SIZE (type));
   13447       197984 :   tree_node (TYPE_SIZE_UNIT (type));
   13448       197984 :   tree_node (TYPE_VFIELD (type));
   13449       197984 :   tree_node (TYPE_BINFO (type));
   13450              : 
   13451       197984 :   vec_chained_decls (TYPE_FIELDS (type));
   13452              : 
   13453              :   /* Every class but __as_base has a type-specific.  */
   13454       392480 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13455              : 
   13456       197984 :   if (TYPE_LANG_SPECIFIC (type))
   13457              :     {
   13458       194496 :       {
   13459       194496 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13460       194496 :         if (!v)
   13461              :           {
   13462        46322 :             gcc_checking_assert (!streaming_p ());
   13463              :             /* Force a class vector.  */
   13464        46322 :             v = set_class_bindings (type, -1);
   13465        46322 :             gcc_checking_assert (v);
   13466              :           }
   13467              : 
   13468       194496 :         unsigned len = v->length ();
   13469       194496 :         if (streaming_p ())
   13470        97225 :           u (len);
   13471      1642370 :         for (unsigned ix = 0; ix != len; ix++)
   13472              :           {
   13473      1447874 :             tree m = (*v)[ix];
   13474      1447874 :             if (TREE_CODE (m) == TYPE_DECL
   13475       420639 :                 && DECL_ARTIFICIAL (m)
   13476      1659083 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13477              :               /* This is a using-decl for a type, or an anonymous
   13478              :                  struct (maybe with a typedef name).  Write the type.  */
   13479        15492 :               m = TREE_TYPE (m);
   13480      1447874 :             tree_node (m);
   13481              :           }
   13482              :       }
   13483       194496 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13484              : 
   13485              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13486              :          reader won't know at this point.  */
   13487       194496 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13488              : 
   13489       194496 :       if (streaming_p ())
   13490              :         {
   13491        97225 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13492        97225 :           u (nvbases);
   13493        97225 :           i (has_vptr);
   13494              :         }
   13495              : 
   13496       194496 :       if (has_vptr)
   13497              :         {
   13498         7084 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13499         7084 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13500         7084 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13501              :         }
   13502              :     }
   13503              : 
   13504       197984 :   if (TYPE_LANG_SPECIFIC (type))
   13505              :     {
   13506       194496 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13507              : 
   13508       194496 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13509       194496 :       if (as_base)
   13510       101251 :         as_base = TYPE_NAME (as_base);
   13511       194496 :       tree_node (as_base);
   13512              : 
   13513              :       /* Write the vtables.  */
   13514       194496 :       tree vtables = CLASSTYPE_VTABLES (type);
   13515       194496 :       vec_chained_decls (vtables);
   13516       397418 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13517         8426 :         write_definition (vtables);
   13518              : 
   13519       194496 :       {
   13520              :         /* Friend declarations in class definitions are ignored when
   13521              :            determining exposures.  */
   13522       194496 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13523              : 
   13524              :         /* Write the friend classes.  */
   13525       194496 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13526              : 
   13527              :         /* Write the friend functions.  */
   13528       194496 :         for (tree friends = DECL_FRIENDLIST (defn);
   13529       222880 :              friends; friends = TREE_CHAIN (friends))
   13530              :           {
   13531        28384 :             tree_node (FRIEND_NAME (friends));
   13532        28384 :             tree_list (FRIEND_DECLS (friends), false);
   13533              :           }
   13534              :         /* End of friend fns.  */
   13535       194496 :         tree_node (NULL_TREE);
   13536       194496 :       }
   13537              : 
   13538              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13539              :          decls here as any such decls should already have been added and
   13540              :          ignored above.  */
   13541       194496 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13542              : 
   13543       194496 :       if (TYPE_CONTAINS_VPTR_P (type))
   13544              :         {
   13545              :           /* Write the thunks.  */
   13546         7084 :           for (tree decls = TYPE_FIELDS (type);
   13547       201146 :                decls; decls = DECL_CHAIN (decls))
   13548       194062 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13549       140082 :                 && DECL_VIRTUAL_P (decls)
   13550       230764 :                 && DECL_THUNKS (decls))
   13551              :               {
   13552         1308 :                 tree_node (decls);
   13553              :                 /* Thunks are always unique, so chaining is ok.  */
   13554         1308 :                 chained_decls (DECL_THUNKS (decls));
   13555              :               }
   13556         7084 :           tree_node (NULL_TREE);
   13557              :         }
   13558              :     }
   13559       197984 : }
   13560              : 
   13561              : void
   13562       277022 : trees_out::mark_class_member (tree member, bool do_defn)
   13563              : {
   13564       277022 :   gcc_assert (DECL_P (member));
   13565              : 
   13566       277022 :   member = member_owned_by_class (member);
   13567       277022 :   if (member)
   13568       554044 :     mark_declaration (member, do_defn && has_definition (member));
   13569       277022 : }
   13570              : 
   13571              : void
   13572       198012 : trees_out::mark_class_def (tree defn)
   13573              : {
   13574       198012 :   gcc_assert (DECL_P (defn));
   13575       198012 :   tree type = TREE_TYPE (defn);
   13576              :   /* Mark the class members that are not type-decls and cannot have
   13577              :      independent definitions.  */
   13578      2277880 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13579      2079868 :     if (TREE_CODE (member) == FIELD_DECL
   13580      2079868 :         || TREE_CODE (member) == USING_DECL
   13581              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13582      2079868 :         || (TREE_CODE (member) == CONST_DECL
   13583        16386 :             && DECL_CONTEXT (member) == type))
   13584              :       {
   13585       277022 :         mark_class_member (member);
   13586       277022 :         if (TREE_CODE (member) == FIELD_DECL)
   13587       157885 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13588              :             /* If we're marking a class template definition, then
   13589              :                this'll contain the width (as set by grokbitfield)
   13590              :                instead of a decl.  */
   13591         2986 :             if (DECL_P (repr))
   13592         2330 :               mark_declaration (repr, false);
   13593              :       }
   13594              : 
   13595              :   /* Mark the binfo hierarchy.  */
   13596       466950 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13597       268938 :     mark_by_value (child);
   13598              : 
   13599       198012 :   if (TYPE_LANG_SPECIFIC (type))
   13600              :     {
   13601       194500 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13602       202926 :            vtable; vtable = TREE_CHAIN (vtable))
   13603         8426 :         mark_declaration (vtable, true);
   13604              : 
   13605       194500 :       if (TYPE_CONTAINS_VPTR_P (type))
   13606              :         /* Mark the thunks, they belong to the class definition,
   13607              :            /not/ the thunked-to function.  */
   13608         7084 :         for (tree decls = TYPE_FIELDS (type);
   13609       201146 :              decls; decls = DECL_CHAIN (decls))
   13610       194062 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13611       140082 :             for (tree thunks = DECL_THUNKS (decls);
   13612       141818 :                  thunks; thunks = DECL_CHAIN (thunks))
   13613         1736 :               mark_declaration (thunks, false);
   13614              :     }
   13615       198012 : }
   13616              : 
   13617              : /* Nop sorting, needed for resorting the member vec.  */
   13618              : 
   13619              : static void
   13620     11203912 : nop (void *, void *, void *)
   13621              : {
   13622     11203912 : }
   13623              : 
   13624              : bool
   13625        67269 : trees_in::read_class_def (tree defn, tree maybe_template)
   13626              : {
   13627        67269 :   gcc_assert (DECL_P (defn));
   13628        67902 :   dump () && dump ("Reading class definition %N", defn);
   13629        67269 :   tree type = TREE_TYPE (defn);
   13630        67269 :   tree size = tree_node ();
   13631        67269 :   tree size_unit = tree_node ();
   13632        67269 :   tree vfield = tree_node ();
   13633        67269 :   tree binfo = tree_node ();
   13634        67269 :   vec<tree, va_gc> *vbase_vec = NULL;
   13635        67269 :   vec<tree, va_gc> *member_vec = NULL;
   13636        67269 :   vec<tree, va_gc> *pure_virts = NULL;
   13637        67269 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13638        67269 :   tree key_method = NULL_TREE;
   13639        67269 :   tree lambda = NULL_TREE;
   13640              : 
   13641              :   /* Read the fields.  */
   13642        67269 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13643              : 
   13644        67269 :   if (TYPE_LANG_SPECIFIC (type))
   13645              :     {
   13646        65900 :       if (unsigned len = u ())
   13647              :         {
   13648        65900 :           vec_alloc (member_vec, len);
   13649       596654 :           for (unsigned ix = 0; ix != len; ix++)
   13650              :             {
   13651       530754 :               tree m = tree_node ();
   13652       530754 :               if (get_overrun ())
   13653              :                 break;
   13654       530754 :               if (TYPE_P (m))
   13655         5581 :                 m = TYPE_STUB_DECL (m);
   13656       530754 :               member_vec->quick_push (m);
   13657              :             }
   13658              :         }
   13659        65900 :       lambda = tree_node ();
   13660              : 
   13661        65900 :       if (!get_overrun ())
   13662              :         {
   13663        65900 :           unsigned nvbases = u ();
   13664        65900 :           if (nvbases)
   13665              :             {
   13666          285 :               vec_alloc (vbase_vec, nvbases);
   13667         1300 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13668         1015 :                 if (BINFO_VIRTUAL_P (child))
   13669          285 :                   vbase_vec->quick_push (child);
   13670              :             }
   13671              :         }
   13672              : 
   13673        65900 :       if (!get_overrun ())
   13674              :         {
   13675        65900 :           int has_vptr = i ();
   13676        65900 :           if (has_vptr)
   13677              :             {
   13678         2671 :               pure_virts = tree_vec ();
   13679         2671 :               vcall_indices = tree_pair_vec ();
   13680         2671 :               key_method = tree_node ();
   13681              :             }
   13682              :         }
   13683              :     }
   13684              : 
   13685        67269 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13686        67269 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13687        37311 :   if (installing)
   13688              :     {
   13689        37311 :       if (maybe_dup != defn)
   13690              :         {
   13691              :           // FIXME: This is needed on other defns too, almost
   13692              :           // duplicate-decl like?  See is_matching_decl too.
   13693              :           /* Copy flags from the duplicate.  */
   13694          353 :           tree type_dup = TREE_TYPE (maybe_dup);
   13695              : 
   13696              :           /* Core pieces.  */
   13697          353 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13698          353 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13699          706 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13700          353 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13701          353 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13702              : 
   13703          353 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13704          353 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13705          353 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13706          353 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13707          706 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13708          353 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13709          353 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13710              : 
   13711          353 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13712          353 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13713          353 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13714          353 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13715          706 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13716          353 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13717              : 
   13718          353 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13719          353 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13720              : 
   13721              :           /* C++ pieces.  */
   13722          353 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13723          353 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13724              : 
   13725          706 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13726          353 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13727          706 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13728          353 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13729          706 :           TYPE_NEEDS_CONSTRUCTING (type)
   13730          353 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13731              : 
   13732          353 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13733              :             {
   13734          353 :               if (TYPE_LANG_SPECIFIC (type))
   13735              :                 {
   13736         1059 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13737          353 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13738          706 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13739              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13740              :                 }
   13741         1535 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13742         1182 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13743              :             }
   13744              :         }
   13745              : 
   13746        37311 :       TYPE_SIZE (type) = size;
   13747        37311 :       TYPE_SIZE_UNIT (type) = size_unit;
   13748              : 
   13749        37311 :       if (fields)
   13750              :         {
   13751        37311 :           tree *chain = &TYPE_FIELDS (type);
   13752        37311 :           unsigned len = fields->length ();
   13753       488338 :           for (unsigned ix = 0; ix != len; ix++)
   13754              :             {
   13755       451027 :               tree decl = (*fields)[ix];
   13756              : 
   13757       451027 :               if (!decl)
   13758              :                 {
   13759              :                   /* An anonymous struct with typedef name.  */
   13760            3 :                   tree tdef = (*fields)[ix+1];
   13761            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13762            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13763              :                                        && decl != tdef);
   13764              :                 }
   13765              : 
   13766       814051 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13767       451027 :               *chain = decl;
   13768       451027 :               chain = &DECL_CHAIN (decl);
   13769              : 
   13770       451027 :               if (TREE_CODE (decl) == FIELD_DECL
   13771       451027 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13772              :                 {
   13773          287 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13774          287 :                   if (DECL_NAME (defn) == as_base_identifier)
   13775              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13776              :                        original FIELD_DECL; don't overwrite it to point
   13777              :                        to the as-base FIELD_DECL copy.  */
   13778           26 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13779              :                   else
   13780          274 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13781              :                 }
   13782              : 
   13783       451027 :               if (TREE_CODE (decl) == USING_DECL
   13784       451027 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13785              :                 {
   13786              :                   /* Reconstruct DECL_ACCESS.  */
   13787        18420 :                   tree decls = USING_DECL_DECLS (decl);
   13788        18420 :                   tree access = declared_access (decl);
   13789              : 
   13790        21748 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13791              :                     {
   13792         2168 :                       tree d = *iter;
   13793              : 
   13794         2168 :                       retrofit_lang_decl (d);
   13795         2168 :                       tree list = DECL_ACCESS (d);
   13796              : 
   13797         2168 :                       if (!purpose_member (type, list))
   13798         2607 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13799              :                     }
   13800              :                 }
   13801              : 
   13802       451027 :               if (TREE_CODE (decl) == VAR_DECL
   13803        13217 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13804        11378 :                 note_vague_linkage_variable (decl);
   13805              :             }
   13806              :         }
   13807              : 
   13808        37311 :       TYPE_VFIELD (type) = vfield;
   13809        37311 :       TYPE_BINFO (type) = binfo;
   13810              : 
   13811        37311 :       if (TYPE_LANG_SPECIFIC (type))
   13812              :         {
   13813        36456 :           if (!TYPE_POLYMORPHIC_P (type))
   13814        34887 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13815              :           else
   13816         1569 :             gcc_checking_assert (lambda == NULL_TREE);
   13817              : 
   13818        36456 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13819        36456 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13820        36456 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13821              : 
   13822        36456 :           if (TYPE_POLYMORPHIC_P (type))
   13823         1569 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13824              :           else
   13825        34887 :             gcc_checking_assert (key_method == NULL_TREE);
   13826              : 
   13827        36456 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13828              : 
   13829              :           /* Resort the member vector.  */
   13830        36456 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13831              :         }
   13832              :     }
   13833              :   else if (maybe_dup)
   13834              :     {
   13835              :       // FIXME:QOI Check matching defn
   13836              :     }
   13837              : 
   13838        67269 :   if (TYPE_LANG_SPECIFIC (type))
   13839              :     {
   13840        65900 :       tree primary = tree_node ();
   13841        65900 :       tree as_base = tree_node ();
   13842              : 
   13843        65900 :       if (as_base)
   13844        34004 :         as_base = TREE_TYPE (as_base);
   13845              : 
   13846              :       /* Read the vtables.  */
   13847        65900 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13848        65900 :       if (vtables)
   13849              :         {
   13850         2629 :           unsigned len = vtables->length ();
   13851         5739 :           for (unsigned ix = 0; ix != len; ix++)
   13852              :             {
   13853         3110 :               tree vtable = (*vtables)[ix];
   13854         3110 :               read_var_def (vtable, vtable);
   13855              :             }
   13856              :         }
   13857              : 
   13858        65900 :       tree friend_classes = tree_list (false);
   13859        65900 :       tree friend_functions = NULL_TREE;
   13860        65900 :       for (tree *chain = &friend_functions;
   13861        78010 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13862              :         {
   13863        12110 :           tree val = tree_list (false);
   13864        12110 :           *chain = build_tree_list (name, val);
   13865        12110 :         }
   13866        65900 :       tree decl_list = tree_list (true);
   13867              : 
   13868        65900 :       if (installing)
   13869              :         {
   13870        36456 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13871        36456 :           CLASSTYPE_AS_BASE (type) = as_base;
   13872              : 
   13873        36456 :           if (vtables)
   13874              :             {
   13875         1581 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13876              :                    /* Sneaky user may have defined it inline
   13877              :                       out-of-class.  */
   13878         1116 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13879              :                   /* An imported non-template class attached to a module
   13880              :                      doesn't need to have its vtables emitted here.  */
   13881         1725 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13882          279 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13883         1119 :                 vec_safe_push (keyed_classes, type);
   13884         1581 :               unsigned len = vtables->length ();
   13885         1581 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13886         3460 :               for (unsigned ix = 0; ix != len; ix++)
   13887              :                 {
   13888         1879 :                   tree vtable = (*vtables)[ix];
   13889         1879 :                   gcc_checking_assert (!*chain);
   13890         1879 :                   *chain = vtable;
   13891         1879 :                   chain = &DECL_CHAIN (vtable);
   13892              :                 }
   13893              :             }
   13894        36456 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13895        36456 :           DECL_FRIENDLIST (defn) = friend_functions;
   13896        36456 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13897              : 
   13898        39505 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13899              :             {
   13900         3049 :               tree f = TREE_VALUE (friend_classes);
   13901         3049 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13902         1251 :                 f = TREE_TYPE (f);
   13903              : 
   13904         3049 :               if (CLASS_TYPE_P (f))
   13905              :                 {
   13906         3011 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13907         6022 :                     = tree_cons (NULL_TREE, type,
   13908         3011 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13909         3055 :                   dump () && dump ("Class %N befriending %C:%N",
   13910            6 :                                    type, TREE_CODE (f), f);
   13911              :                 }
   13912              :             }
   13913              : 
   13914        43722 :           for (; friend_functions;
   13915         7266 :                friend_functions = TREE_CHAIN (friend_functions))
   13916         7266 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13917        16520 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13918              :               {
   13919         9254 :                 tree f = TREE_VALUE (friend_decls);
   13920         9254 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13921           36 :                   continue;
   13922              : 
   13923         9218 :                 DECL_BEFRIENDING_CLASSES (f)
   13924         9218 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13925         9284 :                 dump () && dump ("Class %N befriending %C:%N",
   13926           30 :                                  type, TREE_CODE (f), f);
   13927              :               }
   13928              :         }
   13929              : 
   13930        65900 :       if (TYPE_CONTAINS_VPTR_P (type))
   13931              :         /* Read and install the thunks.  */
   13932         3109 :         while (tree vfunc = tree_node ())
   13933              :           {
   13934          438 :             tree thunks = chained_decls ();
   13935          438 :             if (installing)
   13936          270 :               SET_DECL_THUNKS (vfunc, thunks);
   13937              :           }
   13938              : 
   13939        65900 :       vec_free (vtables);
   13940              :     }
   13941              : 
   13942              :   /* Propagate to all variants.  */
   13943        67269 :   if (installing)
   13944        37311 :     fixup_type_variants (type);
   13945              : 
   13946              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13947              :      the fake base, we've not hooked it into the containing class's
   13948              :      data structure yet.  Fortunately it has a unique name.  */
   13949        37311 :   if (installing
   13950        37311 :       && DECL_NAME (defn) != as_base_identifier
   13951        36456 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13952        30766 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13953              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13954              :        already emitted this.  */
   13955        20358 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13956              : 
   13957        67269 :   vec_free (fields);
   13958              : 
   13959        67269 :   return !get_overrun ();
   13960              : }
   13961              : 
   13962              : void
   13963         9210 : trees_out::write_enum_def (tree decl)
   13964              : {
   13965         9210 :   tree type = TREE_TYPE (decl);
   13966              : 
   13967         9210 :   tree_node (TYPE_VALUES (type));
   13968              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13969              :      ENUMERAL_TYPE.  */
   13970         9210 : }
   13971              : 
   13972              : void
   13973         9210 : trees_out::mark_enum_def (tree decl)
   13974              : {
   13975         9210 :   tree type = TREE_TYPE (decl);
   13976              : 
   13977        48114 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   13978              :     {
   13979        38904 :       tree cst = TREE_VALUE (values);
   13980        38904 :       mark_by_value (cst);
   13981              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   13982        38904 :       if (tree init = DECL_INITIAL (cst))
   13983        38468 :         if (TREE_CODE (init) == INTEGER_CST)
   13984        37752 :           mark_by_value (init);
   13985              :     }
   13986         9210 : }
   13987              : 
   13988              : bool
   13989         3086 : trees_in::read_enum_def (tree defn, tree maybe_template)
   13990              : {
   13991         3086 :   tree type = TREE_TYPE (defn);
   13992         3086 :   tree values = tree_node ();
   13993              : 
   13994         3086 :   if (get_overrun ())
   13995              :     return false;
   13996              : 
   13997         3086 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   13998         6172 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   13999              : 
   14000         3086 :   if (installing)
   14001              :     {
   14002         1567 :       TYPE_VALUES (type) = values;
   14003              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14004              :          ENUMERAL_TYPE.  */
   14005              : 
   14006         2534 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14007              :     }
   14008         1519 :   else if (maybe_dup)
   14009              :     {
   14010         1519 :       tree known = TYPE_VALUES (type);
   14011         8472 :       for (; known && values;
   14012         6953 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14013              :         {
   14014         6962 :           tree known_decl = TREE_VALUE (known);
   14015         6962 :           tree new_decl = TREE_VALUE (values);
   14016              : 
   14017         6962 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14018              :             break;
   14019              : 
   14020         6956 :           new_decl = maybe_duplicate (new_decl);
   14021              : 
   14022         6956 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14023         6956 :                               DECL_INITIAL (new_decl)))
   14024              :             break;
   14025              :         }
   14026              : 
   14027         1519 :       if (known || values)
   14028              :         {
   14029           12 :           auto_diagnostic_group d;
   14030           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14031              :                     "definition of %qD does not match", maybe_dup);
   14032           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14033              :                   "existing definition %qD", defn);
   14034              : 
   14035           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14036              : 
   14037           12 :           if (known)
   14038            9 :             known_decl = TREE_VALUE (known);
   14039           12 :           if (values)
   14040           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14041              : 
   14042           12 :           if (known_decl && new_decl)
   14043              :             {
   14044            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14045              :                       "enumerator %qD does not match ...", new_decl);
   14046            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14047              :                       "... this enumerator %qD", known_decl);
   14048              :             }
   14049            3 :           else if (known_decl || new_decl)
   14050              :             {
   14051            3 :               tree extra = known_decl ? known_decl : new_decl;
   14052            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14053              :                       "additional enumerators beginning with %qD", extra);
   14054              :             }
   14055              :           else
   14056            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14057              :                     "enumeration range differs");
   14058              : 
   14059              :           /* Mark it bad.  */
   14060           12 :           unmatched_duplicate (maybe_template);
   14061           12 :         }
   14062              :     }
   14063              : 
   14064              :   return true;
   14065              : }
   14066              : 
   14067              : /* Write out the body of DECL.  See above circularity note.  */
   14068              : 
   14069              : void
   14070       916751 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14071              : {
   14072       916751 :   auto ovr = make_temp_override (writing_local_entities,
   14073       916751 :                                  writing_local_entities || refs_tu_local);
   14074              : 
   14075       916751 :   if (streaming_p ())
   14076              :     {
   14077       458285 :       assert_definition (decl);
   14078       458285 :       dump ()
   14079          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14080              :     }
   14081              :   else
   14082       458466 :     dump (dumper::DEPEND)
   14083           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14084              : 
   14085      1433038 :  again:
   14086      1433038 :   switch (TREE_CODE (decl))
   14087              :     {
   14088            0 :     default:
   14089            0 :       gcc_unreachable ();
   14090              : 
   14091       516287 :     case TEMPLATE_DECL:
   14092       516287 :       decl = DECL_TEMPLATE_RESULT (decl);
   14093       516287 :       goto again;
   14094              : 
   14095       572839 :     case FUNCTION_DECL:
   14096       572839 :       write_function_def (decl);
   14097       572839 :       break;
   14098              : 
   14099       207194 :     case TYPE_DECL:
   14100       207194 :       {
   14101       207194 :         tree type = TREE_TYPE (decl);
   14102       207194 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14103              :                     && TYPE_NAME (type) == decl);
   14104       207194 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14105         9210 :           write_enum_def (decl);
   14106              :         else
   14107       197984 :           write_class_def (decl);
   14108              :       }
   14109              :       break;
   14110              : 
   14111       136718 :     case VAR_DECL:
   14112       136718 :     case CONCEPT_DECL:
   14113       136718 :       write_var_def (decl);
   14114       136718 :       break;
   14115              :     }
   14116       916751 : }
   14117              : 
   14118              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14119              :    its body too.  */
   14120              : 
   14121              : void
   14122      3849768 : trees_out::mark_declaration (tree decl, bool do_defn)
   14123              : {
   14124      3849768 :   mark_by_value (decl);
   14125              : 
   14126      3849768 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14127      1273500 :     decl = DECL_TEMPLATE_RESULT (decl);
   14128              : 
   14129      3849768 :   if (!do_defn)
   14130              :     return;
   14131              : 
   14132       916773 :   switch (TREE_CODE (decl))
   14133              :     {
   14134            0 :     default:
   14135            0 :       gcc_unreachable ();
   14136              : 
   14137              :     case FUNCTION_DECL:
   14138              :       mark_function_def (decl);
   14139              :       break;
   14140              : 
   14141       207222 :     case TYPE_DECL:
   14142       207222 :       {
   14143       207222 :         tree type = TREE_TYPE (decl);
   14144       207222 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14145              :                     && TYPE_NAME (type) == decl);
   14146       207222 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14147         9210 :           mark_enum_def (decl);
   14148              :         else
   14149       198012 :           mark_class_def (decl);
   14150              :       }
   14151              :       break;
   14152              : 
   14153              :     case VAR_DECL:
   14154              :     case CONCEPT_DECL:
   14155              :       mark_var_def (decl);
   14156              :       break;
   14157              :     }
   14158              : }
   14159              : 
   14160              : /* Read in the body of DECL.  See above circularity note.  */
   14161              : 
   14162              : bool
   14163       337773 : trees_in::read_definition (tree decl)
   14164              : {
   14165       339078 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14166              : 
   14167              :   tree maybe_template = decl;
   14168              : 
   14169       337773 :  again:
   14170       539199 :   switch (TREE_CODE (decl))
   14171              :     {
   14172              :     default:
   14173              :       break;
   14174              : 
   14175       201426 :     case TEMPLATE_DECL:
   14176       201426 :       decl = DECL_TEMPLATE_RESULT (decl);
   14177       201426 :       goto again;
   14178              : 
   14179       225992 :     case FUNCTION_DECL:
   14180       225992 :       return read_function_def (decl, maybe_template);
   14181              : 
   14182        70355 :     case TYPE_DECL:
   14183        70355 :       {
   14184        70355 :         tree type = TREE_TYPE (decl);
   14185        70355 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14186              :                     && TYPE_NAME (type) == decl);
   14187        70355 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14188         3086 :           return read_enum_def (decl, maybe_template);
   14189              :         else
   14190        67269 :           return read_class_def (decl, maybe_template);
   14191              :       }
   14192        41426 :       break;
   14193              : 
   14194        41426 :     case VAR_DECL:
   14195        41426 :     case CONCEPT_DECL:
   14196        41426 :       return read_var_def (decl, maybe_template);
   14197              :     }
   14198              : 
   14199              :   return false;
   14200              : }
   14201              : 
   14202              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14203              : 
   14204              : depset **
   14205     19150469 : depset::hash::entity_slot (tree entity, bool insert)
   14206              : {
   14207     19150469 :   traits::compare_type key (entity, NULL);
   14208     28881566 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14209              :                                        insert ? INSERT : NO_INSERT);
   14210              : 
   14211     19150469 :   return slot;
   14212              : }
   14213              : 
   14214              : depset **
   14215       221783 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14216              : {
   14217       221783 :   traits::compare_type key (ctx, name);
   14218       288547 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14219              :                                        insert ? INSERT : NO_INSERT);
   14220              : 
   14221       221783 :   return slot;
   14222              : }
   14223              : 
   14224              : depset *
   14225      9281188 : depset::hash::find_dependency (tree decl)
   14226              : {
   14227      9281188 :   depset **slot = entity_slot (decl, false);
   14228              : 
   14229      9281188 :   return slot ? *slot : NULL;
   14230              : }
   14231              : 
   14232              : depset *
   14233        66764 : depset::hash::find_binding (tree ctx, tree name)
   14234              : {
   14235        66764 :   depset **slot = binding_slot (ctx, name, false);
   14236              : 
   14237        66764 :   return slot ? *slot : NULL;
   14238              : }
   14239              : 
   14240              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14241              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14242              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14243              : 
   14244              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14245              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14246              : 
   14247              : static bool
   14248      4572929 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14249              : {
   14250      4572929 :   gcc_checking_assert (DECL_P (decl));
   14251      4572929 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14252      4572929 :   tree type = TREE_TYPE (decl);
   14253              : 
   14254              :   /* Only types, functions, variables, and template (specialisations)
   14255              :      can be TU-local.  */
   14256      4572929 :   if (TREE_CODE (decl) != TYPE_DECL
   14257              :       && TREE_CODE (decl) != FUNCTION_DECL
   14258              :       && TREE_CODE (decl) != VAR_DECL
   14259              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14260              :     return false;
   14261              : 
   14262              :   /* An explicit type alias is not an entity; we don't want to stream
   14263              :      such aliases if they refer to TU-local entities, so propagate this
   14264              :      from the original type. The built-in declarations of 'int' and such
   14265              :      are never TU-local.  */
   14266      4569240 :   if (TREE_CODE (decl) == TYPE_DECL
   14267      1539445 :       && !DECL_SELF_REFERENCE_P (decl)
   14268      6046026 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14269              :     {
   14270       789062 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14271       789062 :       if (orig && TYPE_NAME (orig))
   14272              :         {
   14273       164676 :           if (explain)
   14274           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14275       164676 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14276              :         }
   14277              :       else
   14278              :         return false;
   14279              :     }
   14280              : 
   14281              :   /* Check specializations first for slightly better explanations.  */
   14282      3780178 :   int use_tpl = -1;
   14283      3780178 :   tree ti = node_template_info (decl, use_tpl);
   14284      4629350 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14285              :     {
   14286              :       /* A specialization of a TU-local template.  */
   14287       849013 :       tree tmpl = TI_TEMPLATE (ti);
   14288       849013 :       if (is_tu_local_entity (tmpl))
   14289              :         {
   14290           72 :           if (explain)
   14291              :             {
   14292           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14293              :                       decl, tmpl);
   14294           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14295              :             }
   14296           72 :           return true;
   14297              :         }
   14298              : 
   14299              :       /* A specialization of a template with any TU-local template argument.  */
   14300       848941 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14301              :         return true;
   14302              : 
   14303              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14304              :          declaration is an exposure.  This should always be covered by the
   14305              :          above cases??  */
   14306              :     }
   14307              : 
   14308              :   /* A type, function, variable, or template with internal linkage.  */
   14309      3780074 :   linkage_kind kind = decl_linkage (decl);
   14310      3780074 :   if (kind == lk_internal
   14311              :       /* But although weakrefs are marked static, don't consider them
   14312              :          to be TU-local.  */
   14313      3780074 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14314              :     {
   14315          846 :       if (explain)
   14316          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14317          846 :       return true;
   14318              :     }
   14319              : 
   14320              :   /* Does not have a name with linkage and is declared, or introduced by a
   14321              :      lambda-expression, within the definition of a TU-local entity.  */
   14322      3779228 :   if (kind == lk_none)
   14323              :     {
   14324       387876 :       tree ctx = CP_DECL_CONTEXT (decl);
   14325       494390 :       if (LAMBDA_TYPE_P (type))
   14326        77912 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14327       387876 :           ctx = extra;
   14328              : 
   14329       387876 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14330              :         {
   14331           31 :           if (!TREE_PUBLIC (ctx))
   14332              :             {
   14333            0 :               if (explain)
   14334            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14335              :                         "anonymous namespace", decl);
   14336            0 :               return true;
   14337              :             }
   14338              :         }
   14339       387845 :       else if (TYPE_P (ctx))
   14340              :         {
   14341        44962 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14342        44962 :           if (is_tu_local_entity (ctx_decl))
   14343              :             {
   14344            6 :               if (explain)
   14345              :                 {
   14346            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14347              :                           "TU-local entity %qT", decl, ctx);
   14348            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14349              :                 }
   14350            6 :               return true;
   14351              :             }
   14352              :         }
   14353       342883 :       else if (is_tu_local_entity (ctx))
   14354              :         {
   14355           33 :           if (explain)
   14356              :             {
   14357            6 :               inform (loc, "%qD has no linkage and is declared within "
   14358              :                       "TU-local entity %qD", decl, ctx);
   14359            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14360              :             }
   14361           33 :           return true;
   14362              :         }
   14363              :     }
   14364              : 
   14365              :   /* A type with no name that is defined outside a class-specifier, function
   14366              :      body, or initializer; or is introduced by a defining-type-specifier that
   14367              :      is used to declare only TU-local entities.
   14368              : 
   14369              :      We consider types with names for linkage purposes as having names, since
   14370              :      these aren't really TU-local.  */
   14371      3779189 :   tree inner = STRIP_TEMPLATE (decl);
   14372      1590217 :   if (inner
   14373      3779189 :       && TREE_CODE (inner) == TYPE_DECL
   14374      2851503 :       && TYPE_ANON_P (type)
   14375        42212 :       && !DECL_SELF_REFERENCE_P (inner)
   14376              :       /* An enum with an enumerator name for linkage.  */
   14377      1628507 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14378              :     {
   14379        36356 :       tree main_decl = TYPE_MAIN_DECL (type);
   14380        72236 :       if (LAMBDA_TYPE_P (type))
   14381              :         {
   14382              :           /* A lambda expression is, in practice, TU-local iff it has no
   14383              :              mangling scope.  This currently doesn't line up exactly with
   14384              :              the standard's definition due to some ABI issues, but it's
   14385              :              pretty close, and avoids other issues down the line.  */
   14386        71634 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14387              :             {
   14388            4 :               if (explain)
   14389            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14390              :                         "from similar lambdas in other TUs", type);
   14391            4 :               return true;
   14392              :             }
   14393              :         }
   14394         1078 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14395          569 :                && !decl_function_context (main_decl))
   14396              :         {
   14397           30 :           if (explain)
   14398           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14399              :                     "function, or initializer", type);
   14400           30 :           return true;
   14401              :         }
   14402              : 
   14403              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14404              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14405              :       // think of any cases where this isn't covered by earlier cases.  */
   14406              :     }
   14407              : 
   14408              :   return false;
   14409              : }
   14410              : 
   14411              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14412              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14413              : 
   14414              : static bool
   14415       966449 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14416              : {
   14417       966449 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14418              :     return false;
   14419              : 
   14420      2617913 :   for (tree a : tree_vec_range (args))
   14421              :     {
   14422      1651496 :       if (TREE_CODE (a) == TREE_VEC)
   14423              :         {
   14424       117508 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14425           32 :             return true;
   14426              :         }
   14427              :       else if (!WILDCARD_TYPE_P (a))
   14428              :         {
   14429      1396035 :           if (DECL_P (a) && is_tu_local_entity (a))
   14430              :             {
   14431            0 :               if (explain)
   14432              :                 {
   14433            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14434              :                           "%qD has TU-local template argument %qD",
   14435              :                           decl, a);
   14436            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14437              :                 }
   14438            0 :               return true;
   14439              :             }
   14440              : 
   14441      1396035 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14442              :             {
   14443           17 :               if (explain)
   14444              :                 {
   14445            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14446              :                           "%qD has TU-local template argument %qT",
   14447              :                           decl, a);
   14448            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14449              :                 }
   14450           17 :               return true;
   14451              :             }
   14452              : 
   14453      1396018 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14454              :             return true;
   14455              :         }
   14456              :     }
   14457              : 
   14458       966417 :   return false;
   14459              : }
   14460              : 
   14461              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14462              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14463              : 
   14464              : static bool
   14465       103391 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14466              : {
   14467       103391 :   if (!expr)
   14468              :     return false;
   14469              : 
   14470       101610 :   tree e = expr;
   14471       101610 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14472       101610 :   STRIP_NOPS (e);
   14473       101610 :   if (TREE_CODE (e) == TARGET_EXPR)
   14474            0 :     e = TARGET_EXPR_INITIAL (e);
   14475            0 :   if (!e)
   14476              :     return false;
   14477              : 
   14478              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14479              :      with a TU-local variable.  */
   14480       101610 :   tree object = NULL_TREE;
   14481       101610 :   if (TREE_CODE (e) == ADDR_EXPR)
   14482         2791 :     object = TREE_OPERAND (e, 0);
   14483        98819 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14484            0 :     object = PTRMEM_CST_MEMBER (e);
   14485        98819 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14486              :     object = e;
   14487              : 
   14488         2791 :   if (object
   14489         3476 :       && VAR_OR_FUNCTION_DECL_P (object)
   14490         3563 :       && is_tu_local_entity (object))
   14491              :     {
   14492           54 :       if (explain)
   14493              :         {
   14494              :           /* We've lost a lot of location information by the time we get here,
   14495              :              so let's just do our best effort.  */
   14496           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14497           18 :           if (VAR_P (object))
   14498            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14499              :           else
   14500            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14501           18 :           is_tu_local_entity (object, true);
   14502              :         }
   14503           54 :       return true;
   14504              :     }
   14505              : 
   14506              :   /* It is an object of class or array type and any of its subobjects or
   14507              :      any of the objects or functions to which its non-static data members
   14508              :      of reference type refer is TU-local and is usable in constant
   14509              :      expressions.  */
   14510       101556 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14511        52965 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14512        42315 :       if (is_tu_local_value (decl, f.value, explain))
   14513              :         return true;
   14514              : 
   14515              :   return false;
   14516              : }
   14517              : 
   14518              : /* Complains if DECL is a TU-local entity imported from a named module.
   14519              :    Returns TRUE if instantiation should fail.  */
   14520              : 
   14521              : bool
   14522   8564392394 : instantiating_tu_local_entity (tree decl)
   14523              : {
   14524   8564392394 :   if (!modules_p ())
   14525              :     return false;
   14526              : 
   14527     32432919 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14528              :     {
   14529           92 :       auto_diagnostic_group d;
   14530           92 :       error ("instantiation exposes TU-local entity %qD",
   14531           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14532           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14533           92 :       return true;
   14534           92 :     }
   14535              : 
   14536              :   /* Currently, only TU-local variables and functions, or possibly
   14537              :      templates thereof, will be emitted from named modules.  */
   14538     32432827 :   tree inner = STRIP_TEMPLATE (decl);
   14539     32432827 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14540              :     return false;
   14541              : 
   14542              :   /* From this point we will only be emitting warnings; if we're not
   14543              :      warning about this case then there's no need to check further.  */
   14544      1353313 :   if (!warn_expose_global_module_tu_local
   14545      2706626 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14546      1353313 :                               OPT_Wexpose_global_module_tu_local))
   14547        10784 :     return false;
   14548              : 
   14549      1342529 :   if (!is_tu_local_entity (decl))
   14550              :     return false;
   14551              : 
   14552           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14553          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14554              :     return false;
   14555              : 
   14556              :   /* Referencing TU-local entities from a header is generally OK.
   14557              :      We don't have an easy way to detect if this declaration came
   14558              :      from a header via a separate named module, but we can just
   14559              :      ignore that case for warning purposes.  */
   14560            9 :   unsigned index = import_entity_index (decl);
   14561            9 :   module_state *mod = import_entity_module (index);
   14562            9 :   if (mod->is_header ())
   14563              :     return false;
   14564              : 
   14565            9 :   auto_diagnostic_group d;
   14566            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14567              :            "instantiation exposes TU-local entity %qD", decl);
   14568            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14569              : 
   14570              :   /* We treat TU-local entities from the GMF as not actually being
   14571              :      TU-local as an extension, so allow instantation to proceed.  */
   14572            9 :   return false;
   14573            9 : }
   14574              : 
   14575              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14576              :    doesn't already exist.  Add it to the worklist if so.
   14577              : 
   14578              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14579              :    a using decl.
   14580              : 
   14581              :    We do not have to worry about adding the same dependency more than
   14582              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14583              :    prevents us wanting to do it anyway.  */
   14584              : 
   14585              : depset *
   14586      8242932 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14587              : {
   14588              :   /* Make sure we're being told consistent information.  */
   14589     15425764 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14590              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14591              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14592      8242932 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14593      8242932 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14594              :                        && (TREE_CODE (decl) != USING_DECL
   14595              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14596      8242932 :   gcc_checking_assert (!is_key_order ());
   14597      8242932 :   if (ek == EK_USING)
   14598        39087 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14599      8242932 :   if (ek == EK_TU_LOCAL)
   14600           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14601              : 
   14602      8242932 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14603              :     /* The template should have copied these from its result decl.  */
   14604      3185906 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14605              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14606              : 
   14607      8242932 :   depset **slot = entity_slot (decl, true);
   14608      8242932 :   depset *dep = *slot;
   14609      8242932 :   bool for_binding = ek == EK_FOR_BINDING;
   14610              : 
   14611      8242932 :   if (!dep)
   14612              :     {
   14613       695639 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14614              :            /* ... not an enum, for instance.  */
   14615       344789 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14616       339951 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14617       310317 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14618      2407084 :           || (VAR_P (decl)
   14619        93728 :               && DECL_LANG_SPECIFIC (decl)
   14620        93668 :               && DECL_USE_TEMPLATE (decl) == 2))
   14621              :         {
   14622              :           /* A partial or explicit specialization. Partial
   14623              :              specializations might not be in the hash table, because
   14624              :              there can be multiple differently-constrained variants.
   14625              : 
   14626              :              template<typename T> class silly;
   14627              :              template<typename T> requires true class silly {};
   14628              : 
   14629              :              We need to find them, insert their TEMPLATE_DECL in the
   14630              :              dep_hash, and then convert the dep we just found into a
   14631              :              redirect.  */
   14632              : 
   14633        44461 :           tree ti = get_template_info (decl);
   14634        44461 :           tree tmpl = TI_TEMPLATE (ti);
   14635        44461 :           tree partial = NULL_TREE;
   14636        44461 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14637       163529 :                spec; spec = TREE_CHAIN (spec))
   14638       143130 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14639              :               {
   14640              :                 partial = TREE_VALUE (spec);
   14641              :                 break;
   14642              :               }
   14643              : 
   14644        44461 :           if (partial)
   14645              :             {
   14646              :               /* Eagerly create an empty redirect.  The following
   14647              :                  make_dependency call could cause hash reallocation,
   14648              :                  and invalidate slot's value.  */
   14649        24062 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14650              : 
   14651              :               /* Redirects are never reached -- always snap to their target.  */
   14652        24062 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14653              : 
   14654        24062 :               *slot = redirect;
   14655              : 
   14656        24062 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14657        24062 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14658              : 
   14659        24062 :               redirect->deps.safe_push (tmpl_dep);
   14660              : 
   14661        24062 :               return redirect;
   14662              :             }
   14663              :         }
   14664              : 
   14665      1724584 :       bool has_def = ek != EK_USING && has_definition (decl);
   14666      1685497 :       if (ek > EK_BINDING)
   14667       151108 :         ek = EK_DECL;
   14668              : 
   14669              :       /* The only OVERLOADS we should see are USING decls from
   14670              :          bindings.  */
   14671      1724584 :       *slot = dep = make_entity (decl, ek, has_def);
   14672              : 
   14673      1724584 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14674              :         /* The template_result should otherwise not be in the
   14675              :            table, or be an empty redirect (created above).  */
   14676       449909 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14677        24062 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14678              :                                && !(*eslot)->deps.length ());
   14679              : 
   14680      1724584 :       if (ignore_exposure)
   14681        45600 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14682              : 
   14683      1724584 :       if (ek != EK_USING)
   14684              :         {
   14685      1685497 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14686      1685497 :           bool imported_from_module_p = false;
   14687              : 
   14688      1685497 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14689      3225547 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14690              :             {
   14691              :               /* Store the module number and index in cluster/section,
   14692              :                  so we don't have to look them up again.  */
   14693        86210 :               unsigned index = import_entity_index (decl);
   14694        86210 :               module_state *from = import_entity_module (index);
   14695              :               /* Remap will be zero for imports from partitions, which
   14696              :                  we want to treat as-if declared in this TU.  */
   14697        86210 :               if (from->remap)
   14698              :                 {
   14699        85456 :                   dep->cluster = index - from->entity_lwm;
   14700        85456 :                   dep->section = from->remap;
   14701        85456 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14702              : 
   14703        85456 :                   if (!from->is_header ())
   14704      1685497 :                     imported_from_module_p = true;
   14705              :                 }
   14706              :             }
   14707              : 
   14708              :           /* Check for TU-local entities.  This is unnecessary in header
   14709              :              units because we can export internal-linkage decls, and
   14710              :              no declarations are exposures.  Similarly, if the decl was
   14711              :              imported from a non-header module we know it cannot have
   14712              :              been TU-local.  */
   14713      1685497 :           if (!header_module_p () && !imported_from_module_p)
   14714              :             {
   14715       783747 :               if (is_tu_local_entity (decl))
   14716          305 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14717              : 
   14718       783747 :               if (VAR_P (decl)
   14719        41634 :                   && decl_maybe_constant_var_p (decl)
   14720       824137 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14721              :                 {
   14722              :                   /* A potentially-constant variable initialized to a TU-local
   14723              :                      value is not usable in constant expressions within other
   14724              :                      translation units.  We can achieve this by simply not
   14725              :                      streaming the definition in such cases.  */
   14726           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14727              : 
   14728           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14729           39 :                       || DECL_INLINE_VAR_P (decl))
   14730              :                     /* A constexpr variable initialized to a TU-local value,
   14731              :                        or an inline value (PR c++/119996), is an exposure.
   14732              : 
   14733              :                        For simplicity, we don't support "non-strict" TU-local
   14734              :                        values: even if the TU-local entity we refer to in the
   14735              :                        initialiser is in the GMF, we still won't consider this
   14736              :                        valid in constant expressions in other TUs, and so
   14737              :                        complain accordingly.  */
   14738           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14739              :                 }
   14740              :             }
   14741              : 
   14742              :           /* A namespace-scope type may be declared in one module unit
   14743              :              and defined in another; make sure that we're found when
   14744              :              completing the class.  */
   14745      1685497 :           if (ek == EK_DECL
   14746       674129 :               && !dep->is_import ()
   14747       665704 :               && dep->has_defn ()
   14748       351977 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14749       127919 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14750              :               /* Anonymous types can't be forward-declared.  */
   14751      1719322 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14752        33323 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14753              : 
   14754              :           /* Namespace-scope functions can be found by ADL by template
   14755              :              instantiations in this module.  We need to create bindings
   14756              :              for them so that name lookup recognises they exist, if they
   14757              :              won't be discarded.  add_binding_entity is too early to do
   14758              :              this for GM functions, because if nobody ends up using them
   14759              :              we'll have leftover bindings laying around, and it's tricky
   14760              :              to delete them and any namespaces they've implicitly created
   14761              :              deps on.  The downside is this means we don't pick up on
   14762              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14763              :              to.  */
   14764      1685497 :           if (ek == EK_DECL
   14765      1685497 :               && !for_binding
   14766       523033 :               && !dep->is_import ()
   14767       514614 :               && !dep->is_tu_local ()
   14768       514510 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14769        60438 :               && DECL_DECLARES_FUNCTION_P (decl)
   14770              :               /* Compiler-generated functions won't participate in ADL.  */
   14771        46799 :               && !DECL_ARTIFICIAL (decl)
   14772              :               /* A hidden friend doesn't need a binding.  */
   14773      1724887 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14774        39390 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14775              :             {
   14776              :               /* This will only affect GM functions.  */
   14777        48140 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14778              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14779              :               /* We shouldn't see any instantiations or specialisations.  */
   14780        24070 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14781              :                                    || !DECL_USE_TEMPLATE (decl));
   14782              : 
   14783        24070 :               tree ns = CP_DECL_CONTEXT (decl);
   14784        24070 :               tree name = DECL_NAME (decl);
   14785        24070 :               depset *binding = find_binding (ns, name);
   14786        24070 :               if (!binding)
   14787              :                 {
   14788         7616 :                   binding = make_binding (ns, name);
   14789         7616 :                   add_namespace_context (binding, ns);
   14790              : 
   14791         7616 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14792         7616 :                   *slot = binding;
   14793              :                 }
   14794              : 
   14795        24070 :               binding->deps.safe_push (dep);
   14796        24070 :               dep->deps.safe_push (binding);
   14797        24105 :               dump (dumper::DEPEND)
   14798            9 :                 && dump ("Built ADL binding for %C:%N",
   14799            9 :                          TREE_CODE (decl), decl);
   14800              :             }
   14801              :         }
   14802              : 
   14803      1724584 :       if (!dep->is_import ())
   14804      1639128 :         worklist.safe_push (dep);
   14805              :     }
   14806      6494286 :   else if (!ignore_exposure)
   14807      5736158 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14808              : 
   14809      8218870 :   dump (dumper::DEPEND)
   14810        36583 :     && dump ("%s on %s %C:%N found",
   14811              :              ek == EK_REDIRECT ? "Redirect"
   14812        36583 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14813              :              : "Dependency",
   14814        36583 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14815              : 
   14816      8218870 :   return dep;
   14817              : }
   14818              : 
   14819              : /* Whether REF is an exposure of a member type of SOURCE.
   14820              : 
   14821              :    This comes up with exposures of class-scope lambdas, that we currently
   14822              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14823              :    lambda will be exposed in two places, first by the class type it is in
   14824              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14825              :    lambda.  We only want the second case to warn.  */
   14826              : 
   14827              : static bool
   14828          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14829              : {
   14830          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14831              :                        && ref->is_tu_local (/*strict=*/true));
   14832          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14833          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14834              : 
   14835          253 :   if (!source->is_tu_local (/*strict=*/true)
   14836          241 :       && source_entity
   14837          241 :       && ref_entity
   14838          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14839            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14840            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14841          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14842              :     {
   14843            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14844              :       return true;
   14845              :     }
   14846              :   else
   14847              :     return false;
   14848              : }
   14849              : 
   14850              : /* DEP is a newly discovered dependency.  Append it to current's
   14851              :    depset.  */
   14852              : 
   14853              : void
   14854      6237373 : depset::hash::add_dependency (depset *dep)
   14855              : {
   14856      6237373 :   gcc_checking_assert (current && !is_key_order ());
   14857      6237373 :   current->deps.safe_push (dep);
   14858              : 
   14859      6237373 :   if (dep->is_tu_local (/*strict=*/true))
   14860              :     {
   14861          325 :       if (dep->is_tu_local ())
   14862          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14863              :       else
   14864           65 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14865              : 
   14866          325 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14867              :         {
   14868          133 :           if (dep->is_tu_local ())
   14869           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14870              :           else
   14871           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14872              :         }
   14873              :     }
   14874              : 
   14875      6237373 :   if (current->get_entity_kind () == EK_USING
   14876        39087 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14877      6242684 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14878              :     {
   14879              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14880              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14881         4894 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14882              : 
   14883         4894 :       if (TREE_CODE (c_decl) == CONST_DECL
   14884         9744 :           && (current->deps[0]->get_entity ()
   14885         4850 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14886              :         /* Make DECL depend on CURRENT.  */
   14887         4796 :         dep->deps.safe_push (current);
   14888              :     }
   14889              : 
   14890              :   /* If two dependencies recursively depend on each other existing within
   14891              :      their own merge keys, we must ensure that the first dep we saw while
   14892              :      walking is written first in this cluster.  See sort_cluster for more
   14893              :      details.  */
   14894      6237373 :   if (writing_merge_key)
   14895              :     {
   14896      1106409 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14897        58894 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14898      1106409 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14899      1106409 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14900              :     }
   14901              : 
   14902      6237373 :   if (dep->is_unreached ())
   14903              :     {
   14904              :       /* The dependency is reachable now.  */
   14905       449663 :       reached_unreached = true;
   14906       449663 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14907       449663 :       dump (dumper::DEPEND)
   14908           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14909           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14910              :     }
   14911      6237373 : }
   14912              : 
   14913              : depset *
   14914      9075327 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14915              : {
   14916      9075327 :   depset *dep;
   14917              : 
   14918      9075327 :   if (is_key_order ())
   14919              :     {
   14920      2871047 :       dep = find_dependency (decl);
   14921      2871047 :       if (dep)
   14922              :         {
   14923      1307379 :           current->deps.safe_push (dep);
   14924      1307379 :           dump (dumper::MERGE)
   14925          723 :             && dump ("Key dependency on %s %C:%N found",
   14926          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14927              :         }
   14928              :       else
   14929              :         {
   14930              :           /* It's not a mergeable decl, look for it in the original
   14931              :              table.  */
   14932      1563668 :           dep = chain->find_dependency (decl);
   14933      1563668 :           gcc_checking_assert (dep);
   14934              :         }
   14935              :     }
   14936              :   else
   14937              :     {
   14938      6204280 :       dep = make_dependency (decl, ek);
   14939      6204280 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14940      6144872 :         add_dependency (dep);
   14941              :     }
   14942              : 
   14943      9075327 :   return dep;
   14944              : }
   14945              : 
   14946              : void
   14947       702296 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14948              : {
   14949       702296 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14950       702296 :   dep->deps.safe_push (ns_dep);
   14951              : 
   14952              :   /* Mark it as special if imported so we don't walk connect when
   14953              :      SCCing.  */
   14954       702296 :   if (!dep->is_binding () && ns_dep->is_import ())
   14955            0 :     dep->set_special ();
   14956       702296 : }
   14957              : 
   14958              : struct add_binding_data
   14959              : {
   14960              :   tree ns;
   14961              :   bitmap partitions;
   14962              :   depset *binding;
   14963              :   depset::hash *hash;
   14964              :   bool met_namespace;
   14965              : };
   14966              : 
   14967              : /* Return true if we are, or contain something that is exported.  */
   14968              : 
   14969              : bool
   14970      5812327 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   14971              : {
   14972      5812327 :   auto data = static_cast <add_binding_data *> (data_);
   14973      5812327 :   decl = strip_using_decl (decl);
   14974              : 
   14975      5812327 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   14976              :     {
   14977      5803946 :       tree inner = decl;
   14978              : 
   14979      5803946 :       if (TREE_CODE (inner) == CONST_DECL
   14980         9385 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   14981              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   14982              :              enumeration.  */
   14983      5813331 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   14984         9344 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   14985      5794602 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   14986       134859 :         inner = DECL_TEMPLATE_RESULT (inner);
   14987              : 
   14988     11427100 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   14989     11255755 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   14990              :         /* Ignore entities not within the module purview.  We'll need to
   14991              :            create bindings for any non-discarded function calls for ADL,
   14992              :            but it's simpler to handle that at the point of use rather
   14993              :            than trying to clear out bindings after the fact.  */
   14994              :         return false;
   14995              : 
   14996       194599 :       if ((flags & WMB_Hidden)
   14997         5371 :           && DECL_LANG_SPECIFIC (inner)
   14998       199970 :           && DECL_UNIQUE_FRIEND_P (inner))
   14999              :         /* Hidden friends will be found via ADL on the class type,
   15000              :            and so do not need to have bindings.  Anticipated builtin
   15001              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15002              :            also don't need exporting, but we should create a binding
   15003              :            anyway so that we can have a common decl to match against.  */
   15004              :         return false;
   15005              : 
   15006       189316 :       bool internal_decl = false;
   15007       189316 :       if (!header_module_p () && is_tu_local_entity (decl)
   15008       189577 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15009              :         {
   15010              :           /* A TU-local entity.  For ADL we still need to create bindings
   15011              :              for internal-linkage functions attached to a named module.  */
   15012          150 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15013          105 :               && DECL_LANG_SPECIFIC (inner)
   15014          360 :               && DECL_MODULE_ATTACH_P (inner))
   15015              :             {
   15016           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15017              :               internal_decl = true;
   15018              :             }
   15019              :           else
   15020              :             return false;
   15021              :         }
   15022              : 
   15023       189154 :       if ((TREE_CODE (decl) == VAR_DECL
   15024       189154 :            || TREE_CODE (decl) == TYPE_DECL)
   15025       189154 :           && DECL_TINFO_P (decl))
   15026              :         /* Ignore TINFO things.  */
   15027              :         return false;
   15028              : 
   15029       189154 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15030              :         /* Ignore NTTP objects.  */
   15031              :         return false;
   15032              : 
   15033       189154 :       if (deduction_guide_p (decl))
   15034              :         {
   15035              :           /* Ignore deduction guides, bindings for them will be created within
   15036              :              find_dependencies for their class template.  But still build a dep
   15037              :              for them so that we don't discard them.  */
   15038         1610 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15039         1610 :           return false;
   15040              :         }
   15041              : 
   15042       187544 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15043              :         {
   15044              :           /* An unscoped enum constant implicitly brought into the containing
   15045              :              namespace.  We treat this like a using-decl.  */
   15046         4064 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15047              : 
   15048         4064 :           flags = WMB_Flags (flags | WMB_Using);
   15049         4064 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15050              :               /* A using-decl can make an enum constant exported for a
   15051              :                  non-exported enumeration.  */
   15052         4064 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15053         3833 :             flags = WMB_Flags (flags | WMB_Export);
   15054              :         }
   15055              : 
   15056       187544 :       if (!data->binding)
   15057              :         /* No binding to check.  */;
   15058        41131 :       else if (flags & WMB_Using)
   15059              :         {
   15060              :           /* Look in the binding to see if we already have this
   15061              :              using.  */
   15062       160161 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15063              :             {
   15064       129422 :               depset *d = data->binding->deps[ix];
   15065       258844 :               if (d->get_entity_kind () == EK_USING
   15066       129422 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15067              :                 {
   15068            3 :                   if (!(flags & WMB_Hidden))
   15069            3 :                     d->clear_hidden_binding ();
   15070            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15071            3 :                   if (flags & WMB_Export)
   15072            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15073            3 :                   return bool (flags & WMB_Export);
   15074              :                 }
   15075              :             }
   15076              :         }
   15077        25760 :       else if (flags & WMB_Dups)
   15078              :         {
   15079              :           /* Look in the binding to see if we already have this decl.  */
   15080           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15081              :             {
   15082           39 :               depset *d = data->binding->deps[ix];
   15083           39 :               if (d->get_entity () == decl)
   15084              :                 {
   15085           33 :                   if (!(flags & WMB_Hidden))
   15086           33 :                     d->clear_hidden_binding ();
   15087           33 :                   return false;
   15088              :                 }
   15089              :             }
   15090              :         }
   15091              : 
   15092              :       /* We're adding something.  */
   15093       187508 :       if (!data->binding)
   15094              :         {
   15095       146413 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15096       146413 :           data->hash->add_namespace_context (data->binding, data->ns);
   15097              : 
   15098       146413 :           depset **slot = data->hash->binding_slot (data->ns,
   15099       146413 :                                                     DECL_NAME (decl), true);
   15100       146413 :           gcc_checking_assert (!*slot);
   15101       146413 :           *slot = data->binding;
   15102              :         }
   15103              : 
   15104              :       /* Make sure nobody left a tree visited lying about.  */
   15105       187508 :       gcc_checking_assert (!TREE_VISITED (decl));
   15106              : 
   15107       187508 :       if (flags & WMB_Using)
   15108              :         {
   15109        39087 :           decl = ovl_make (decl, NULL_TREE);
   15110        39087 :           OVL_USING_P (decl) = true;
   15111        39087 :           OVL_PURVIEW_P (decl) = true;
   15112        39087 :           if (flags & WMB_Export)
   15113        37997 :             OVL_EXPORT_P (decl) = true;
   15114              :         }
   15115              : 
   15116       187508 :       entity_kind ek = EK_FOR_BINDING;
   15117       187508 :       if (internal_decl)
   15118              :         ek = EK_TU_LOCAL;
   15119       187415 :       else if (flags & WMB_Using)
   15120        39087 :         ek = EK_USING;
   15121              : 
   15122       187508 :       depset *dep = data->hash->make_dependency (decl, ek);
   15123       187508 :       if (flags & WMB_Hidden)
   15124           88 :         dep->set_hidden_binding ();
   15125       187508 :       data->binding->deps.safe_push (dep);
   15126              :       /* Binding and contents are mutually dependent.  */
   15127       187508 :       dep->deps.safe_push (data->binding);
   15128              : 
   15129       187508 :       return (flags & WMB_Using
   15130       187508 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15131              :     }
   15132         8381 :   else if (!data->met_namespace)
   15133              :     {
   15134              :       /* Namespace, walk exactly once.  */
   15135         8372 :       data->met_namespace = true;
   15136         8372 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15137              :         {
   15138              :           /* It contains an exported thing, so it is exported.  */
   15139         1674 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15140         1674 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15141         1674 :           DECL_MODULE_EXPORT_P (decl) = true;
   15142              :         }
   15143              : 
   15144         8372 :       if (DECL_MODULE_PURVIEW_P (decl))
   15145              :         {
   15146         2037 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15147              : 
   15148         2037 :           return DECL_MODULE_EXPORT_P (decl);
   15149              :         }
   15150              :     }
   15151              : 
   15152              :   return false;
   15153              : }
   15154              : 
   15155              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15156              :    for every binding that contains an export or module-linkage entity.
   15157              :    Add a defining depset for every such decl that we need to write a
   15158              :    definition.  Such defining depsets depend on the binding depset.
   15159              :    Returns true if we contain something exported.  */
   15160              : 
   15161              : bool
   15162        11148 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15163              : {
   15164        12360 :   dump () && dump ("Looking for writables in %N", ns);
   15165        11148 :   dump.indent ();
   15166              : 
   15167        11148 :   unsigned count = 0;
   15168        11148 :   add_binding_data data;
   15169        11148 :   data.ns = ns;
   15170        11148 :   data.partitions = partitions;
   15171        11148 :   data.hash = this;
   15172              : 
   15173     15136896 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15174              :     {
   15175      7562874 :       data.binding = nullptr;
   15176      7562874 :       data.met_namespace = false;
   15177      7562874 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15178       138614 :         count++;
   15179              :     }
   15180              : 
   15181              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15182        11739 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15183          203 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15184              :       {
   15185          171 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15186          171 :         if (DECL_MODULE_EXPORT_P (udir))
   15187          100 :           count++;
   15188              :       }
   15189              : 
   15190        11148 :   if (count)
   15191         3983 :     dump () && dump ("Found %u entries", count);
   15192        11148 :   dump.outdent ();
   15193              : 
   15194        11148 :   return count != 0;
   15195              : }
   15196              : 
   15197              : void
   15198          210 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15199              : {
   15200        20593 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15201              :     {
   15202        20383 :       tree inner = (*partial_classes)[ix];
   15203              : 
   15204        20383 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15205              : 
   15206        20383 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15207              :         {
   15208        20383 :           dep = dep->deps[0];
   15209              :           /* We should have recorded the template as a partial
   15210              :              specialization.  */
   15211        20383 :           gcc_checking_assert (dep->get_entity_kind ()
   15212              :                                == depset::EK_PARTIAL);
   15213              : 
   15214              :           /* Only emit GM entities if reached.  */
   15215        20383 :           if (!DECL_LANG_SPECIFIC (inner)
   15216        32647 :               || !DECL_MODULE_PURVIEW_P (inner))
   15217         8963 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15218              :         }
   15219              :       else
   15220              :         {
   15221              :           /* It was an explicit specialization, not a partial one.
   15222              :              We should have already added this.  */
   15223            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15224              :                                == depset::EK_SPECIALIZATION);
   15225            0 :           gcc_checking_assert (dep->is_special ());
   15226              :         }
   15227              :     }
   15228          210 : }
   15229              : 
   15230              : /* Add the members of imported classes that we defined in this TU.
   15231              :    This will also include lazily created implicit member function
   15232              :    declarations.  (All others will be definitions.)  */
   15233              : 
   15234              : void
   15235           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15236              : {
   15237           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15238              :     {
   15239           12 :       tree defn = (*class_members)[ix];
   15240           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15241              : 
   15242           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15243            0 :         dep = dep->deps[0];
   15244              : 
   15245              :       /* Only non-instantiations need marking as pendings.  */
   15246           24 :       if (dep->get_entity_kind () == EK_DECL)
   15247           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15248              :     }
   15249           12 : }
   15250              : 
   15251              : /* Add any entities found via dependent ADL.  */
   15252              : 
   15253              : void
   15254      7727990 : depset::hash::add_dependent_adl_entities (tree expr)
   15255              : {
   15256      7727990 :   gcc_checking_assert (!is_key_order ());
   15257      7727990 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15258      7517909 :     return;
   15259              : 
   15260      4297901 :   dep_adl_info info;
   15261      4297901 :   switch (TREE_CODE (expr))
   15262              :     {
   15263       465501 :     case CALL_EXPR:
   15264       465501 :       if (!KOENIG_LOOKUP_P (expr))
   15265              :         return;
   15266        27257 :       info.name = CALL_EXPR_FN (expr);
   15267        27257 :       if (!info.name)
   15268              :         return;
   15269        27132 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15270         3119 :         info.name = TREE_OPERAND (info.name, 0);
   15271        27132 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15272              :         return;
   15273        40169 :       if (!identifier_p (info.name))
   15274        26810 :         info.name = OVL_NAME (info.name);
   15275        80553 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15276        53422 :         vec_safe_push (info.args, CALL_EXPR_ARG (expr, ix));
   15277              :       break;
   15278              : 
   15279        36592 :     case LE_EXPR:
   15280        36592 :     case GE_EXPR:
   15281        36592 :     case LT_EXPR:
   15282        36592 :     case GT_EXPR:
   15283        36592 :       info.rewrite = SPACESHIP_EXPR;
   15284        36592 :       goto overloadable_expr;
   15285              : 
   15286        18355 :     case NE_EXPR:
   15287        18355 :       info.rewrite = EQ_EXPR;
   15288        18355 :       goto overloadable_expr;
   15289              : 
   15290        37682 :     case EQ_EXPR:
   15291              :       /* Not strictly a rewrite candidate, but we need to ensure
   15292              :          that lookup of a matching NE_EXPR can succeed if that
   15293              :          would inhibit a rewrite with reversed parameters.  */
   15294        37682 :       info.rewrite = NE_EXPR;
   15295        37682 :       goto overloadable_expr;
   15296              : 
   15297       285138 :     case COMPOUND_EXPR:
   15298       285138 :     case MEMBER_REF:
   15299       285138 :     case MULT_EXPR:
   15300       285138 :     case TRUNC_DIV_EXPR:
   15301       285138 :     case TRUNC_MOD_EXPR:
   15302       285138 :     case PLUS_EXPR:
   15303       285138 :     case MINUS_EXPR:
   15304       285138 :     case LSHIFT_EXPR:
   15305       285138 :     case RSHIFT_EXPR:
   15306       285138 :     case SPACESHIP_EXPR:
   15307       285138 :     case BIT_AND_EXPR:
   15308       285138 :     case BIT_XOR_EXPR:
   15309       285138 :     case BIT_IOR_EXPR:
   15310       285138 :     case TRUTH_ANDIF_EXPR:
   15311       285138 :     case TRUTH_ORIF_EXPR:
   15312       285138 :     overloadable_expr:
   15313       285138 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15314       285138 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15315       285138 :       vec_safe_push (info.args, TREE_OPERAND (expr, 0));
   15316       285138 :       vec_safe_push (info.args, TREE_OPERAND (expr, 1));
   15317       285138 :       break;
   15318              : 
   15319              :     default:
   15320              :       return;
   15321              :     }
   15322              : 
   15323              :   /* If all arguments are type-dependent we don't need to do
   15324              :      anything further, we won't find new entities.  */
   15325       522350 :   processing_template_decl_sentinel ptds;
   15326       312269 :   ++processing_template_decl;
   15327       312269 :   if (!any_type_dependent_arguments_p (info.args))
   15328       102188 :     return;
   15329              : 
   15330              :   /* We need to defer name lookup until after walking, otherwise
   15331              :      we get confused by stray TREE_VISITEDs.  */
   15332       210081 :   dep_adl_entity_list.safe_push (info);
   15333              : }
   15334              : 
   15335              : /* We add the partial & explicit specializations, and the explicit
   15336              :    instantiations.  */
   15337              : 
   15338              : static void
   15339      1010257 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15340              : {
   15341      1010257 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15342              : 
   15343      1010257 :   if (!decl_p)
   15344              :     {
   15345              :       /* We exclusively use decls to locate things.  Make sure there's
   15346              :          no mismatch between the two specialization tables we keep.
   15347              :          pt.cc optimizes instantiation lookup using a complicated
   15348              :          heuristic.  We don't attempt to replicate that algorithm, but
   15349              :          observe its behaviour and reproduce it upon read back.  */
   15350              : 
   15351       298388 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15352              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15353              : 
   15354       298388 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15355              :     }
   15356       711869 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15357       350889 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15358              : 
   15359      1010257 :   data->safe_push (entry);
   15360      1010257 : }
   15361              : 
   15362              : /* Arbitrary stable comparison.  */
   15363              : 
   15364              : static int
   15365     61385230 : specialization_cmp (const void *a_, const void *b_)
   15366              : {
   15367     61385230 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15368     61385230 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15369              : 
   15370     61385230 :   if (ea == eb)
   15371              :     return 0;
   15372              : 
   15373     61385230 :   tree a = ea->spec;
   15374     61385230 :   tree b = eb->spec;
   15375     61385230 :   if (TYPE_P (a))
   15376              :     {
   15377     17199743 :       a = TYPE_NAME (a);
   15378     17199743 :       b = TYPE_NAME (b);
   15379              :     }
   15380              : 
   15381     61385230 :   if (a == b)
   15382              :     /* This can happen with friend specializations.  Just order by
   15383              :        entry address.  See note in depset_cmp.  */
   15384            0 :     return ea < eb ? -1 : +1;
   15385              : 
   15386     61385230 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15387              : }
   15388              : 
   15389              : /* We add all kinds of specialializations.  Implicit specializations
   15390              :    should only streamed and walked if they are reachable from
   15391              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15392              :    assumption that it is cheaper to reinstantiate them on demand
   15393              :    elsewhere, rather than stream them in when we instantiate their
   15394              :    general template.  Also, if we do stream them, we can only do that
   15395              :    if they are not internal (which they can become if they themselves
   15396              :    touch an internal entity?).  */
   15397              : 
   15398              : void
   15399         5552 : depset::hash::add_specializations (bool decl_p)
   15400              : {
   15401         5552 :   vec<spec_entry *> data;
   15402         5552 :   data.create (100);
   15403         5552 :   walk_specializations (decl_p, specialization_add, &data);
   15404         5552 :   data.qsort (specialization_cmp);
   15405      1015809 :   while (data.length ())
   15406              :     {
   15407      1010257 :       spec_entry *entry = data.pop ();
   15408      1010257 :       tree spec = entry->spec;
   15409      1010257 :       int use_tpl = 0;
   15410      1010257 :       bool is_friend = false;
   15411              : 
   15412      1010257 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15413              :         /* A friend of a template.  This is keyed to the
   15414              :            instantiation.  */
   15415              :         is_friend = true;
   15416              : 
   15417      1010257 :       if (decl_p)
   15418              :         {
   15419       711869 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15420              :             {
   15421       711869 :               tree tmpl = TI_TEMPLATE (ti);
   15422              : 
   15423       711869 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15424       711869 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15425              :                 {
   15426         5309 :                   spec = tmpl;
   15427         5309 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15428              :                 }
   15429       706560 :               else if (is_friend)
   15430              :                 {
   15431         4953 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15432         4953 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15433         4953 :                     goto template_friend;
   15434              :                 }
   15435              :             }
   15436              :           else
   15437              :             {
   15438            0 :             template_friend:;
   15439         4953 :               gcc_checking_assert (is_friend);
   15440              :               /* This is a friend of a template class, but not the one
   15441              :                  that generated entry->spec itself (i.e. it's an
   15442              :                  equivalent clone).  We do not need to record
   15443              :                  this.  */
   15444         4953 :               continue;
   15445              :             }
   15446              :         }
   15447              :       else
   15448              :         {
   15449       298388 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15450              :             {
   15451         1391 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15452              : 
   15453         1391 :               if (TYPE_P (ctx))
   15454         1385 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15455              :               else
   15456            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15457              :             }
   15458              :           else
   15459       296997 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15460              : 
   15461       298388 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15462       298388 :           tree tmpl = TI_TEMPLATE (ti);
   15463              : 
   15464       298388 :           spec = TYPE_NAME (spec);
   15465       298388 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15466              :             {
   15467         1562 :               spec = tmpl;
   15468         1562 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15469              :             }
   15470              :         }
   15471              : 
   15472      1005304 :       bool needs_reaching = false;
   15473      1005304 :       if (use_tpl == 1)
   15474              :         /* Implicit instantiations only walked if we reach them.  */
   15475              :         needs_reaching = true;
   15476        80909 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15477       146479 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15478              :         /* Likewise, GMF explicit or partial specializations.  */
   15479              :         needs_reaching = true;
   15480              : 
   15481              : #if false && CHECKING_P
   15482              :       /* The instantiation isn't always on
   15483              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15484              :       // FIXME: we probably need to remember this information?
   15485              :       /* Verify the specialization is on the
   15486              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15487              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15488              :            cons; cons = TREE_CHAIN (cons))
   15489              :         if (TREE_VALUE (cons) == entry->spec)
   15490              :           {
   15491              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15492              :             goto have_spec;
   15493              :           }
   15494              :       gcc_unreachable ();
   15495              :     have_spec:;
   15496              : #endif
   15497              : 
   15498              :       /* Make sure nobody left a tree visited lying about.  */
   15499      1005304 :       gcc_checking_assert (!TREE_VISITED (spec));
   15500      1005304 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15501      1005304 :       if (dep->is_special ())
   15502            0 :         gcc_unreachable ();
   15503              :       else
   15504              :         {
   15505      1005304 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15506        23202 :             dep = dep->deps[0];
   15507       982102 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15508              :             {
   15509       982102 :               dep->set_special ();
   15510       982102 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15511       982102 :               if (!decl_p)
   15512       278865 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15513              :             }
   15514              : 
   15515      1005304 :           if (needs_reaching)
   15516       960889 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15517      1005304 :           if (is_friend)
   15518            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15519              :         }
   15520              :     }
   15521         5552 :   data.release ();
   15522         5552 : }
   15523              : 
   15524              : /* Add a depset into the mergeable hash.  */
   15525              : 
   15526              : void
   15527      1176440 : depset::hash::add_mergeable (depset *mergeable)
   15528              : {
   15529      1176440 :   gcc_checking_assert (is_key_order ());
   15530      1176440 :   entity_kind ek = mergeable->get_entity_kind ();
   15531      1176440 :   tree decl = mergeable->get_entity ();
   15532      1176440 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15533              : 
   15534      1176440 :   depset **slot = entity_slot (decl, true);
   15535      1176440 :   gcc_checking_assert (!*slot);
   15536      1176440 :   depset *dep = make_entity (decl, ek);
   15537      1176440 :   *slot = dep;
   15538              : 
   15539      1176440 :   worklist.safe_push (dep);
   15540              : 
   15541              :   /* So we can locate the mergeable depset this depset refers to,
   15542              :      mark the first dep.  */
   15543      1176440 :   dep->set_special ();
   15544      1176440 :   dep->deps.safe_push (mergeable);
   15545      1176440 : }
   15546              : 
   15547              : /* Find the innermost-namespace scope of DECL, and that
   15548              :    namespace-scope decl.  */
   15549              : 
   15550              : tree
   15551     37129609 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15552              : {
   15553     37129609 :   tree ns = decl;
   15554     44885516 :   do
   15555              :     {
   15556     44885516 :       decl = ns;
   15557     44885516 :       ns = CP_DECL_CONTEXT (ns);
   15558     44885516 :       if (TYPE_P (ns))
   15559      4806243 :         ns = TYPE_NAME (ns);
   15560              :     }
   15561     44885516 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15562              : 
   15563     37129609 :   if (decl_p)
   15564     36584921 :     *decl_p = decl;
   15565              : 
   15566     37129609 :   return ns;
   15567              : }
   15568              : 
   15569              : /* Creates bindings and dependencies for all deduction guides of
   15570              :    the given class template DECL as needed.  */
   15571              : 
   15572              : void
   15573        55221 : depset::hash::add_deduction_guides (tree decl)
   15574              : {
   15575              :   /* Alias templates never have deduction guides.  */
   15576        55221 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15577        54225 :     return;
   15578              : 
   15579              :   /* We don't need to do anything for class-scope deduction guides,
   15580              :      as they will be added as members anyway.  */
   15581        55221 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15582              :     return;
   15583              : 
   15584        42694 :   tree ns = CP_DECL_CONTEXT (decl);
   15585        42694 :   tree name = dguide_name (decl);
   15586              : 
   15587              :   /* We always add all deduction guides with a given name at once,
   15588              :      so if there's already a binding there's nothing to do.  */
   15589        42694 :   if (find_binding (ns, name))
   15590              :     return;
   15591              : 
   15592        39677 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15593              :                                        /*complain=*/false);
   15594        39677 :   if (guides == error_mark_node)
   15595              :     return;
   15596              : 
   15597          996 :   depset *binding = nullptr;
   15598         4760 :   for (tree t : lkp_range (guides))
   15599              :     {
   15600         2768 :       gcc_checking_assert (!TREE_VISITED (t));
   15601         2768 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15602              : 
   15603              :       /* We don't want to create bindings for imported deduction guides, as
   15604              :          this would potentially cause name lookup to return duplicates.  */
   15605         2768 :       if (dep->is_import ())
   15606            6 :         continue;
   15607              : 
   15608         2762 :       if (!binding)
   15609              :         {
   15610              :           /* We have bindings to add.  */
   15611          990 :           binding = make_binding (ns, name);
   15612          990 :           add_namespace_context (binding, ns);
   15613              : 
   15614          990 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15615          990 :           *slot = binding;
   15616              :         }
   15617              : 
   15618         2762 :       binding->deps.safe_push (dep);
   15619         2762 :       dep->deps.safe_push (binding);
   15620         2762 :       dump (dumper::DEPEND)
   15621            0 :         && dump ("Built binding for deduction guide %C:%N",
   15622            0 :                  TREE_CODE (decl), decl);
   15623              :     }
   15624              : }
   15625              : 
   15626              : /* Iteratively find dependencies.  During the walk we may find more
   15627              :    entries on the same binding that need walking.  */
   15628              : 
   15629              : void
   15630       308244 : depset::hash::find_dependencies (module_state *module)
   15631              : {
   15632       308244 :   trees_out walker (NULL, module, *this);
   15633       308244 :   vec<depset *> unreached;
   15634       616488 :   unreached.create (worklist.length ());
   15635              : 
   15636         1121 :   for (;;)
   15637              :     {
   15638       309365 :       reached_unreached = false;
   15639      5324842 :       while (worklist.length ())
   15640              :         {
   15641      5015477 :           depset *item = worklist.pop ();
   15642              : 
   15643      5015477 :           gcc_checking_assert (!item->is_binding ());
   15644      5015477 :           if (item->is_unreached ())
   15645      2617689 :             unreached.quick_push (item);
   15646              :           else
   15647              :             {
   15648      2397788 :               current = item;
   15649      2397788 :               tree decl = current->get_entity ();
   15650      2397788 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15651      2399111 :                 && dump ("Dependencies of %s %C:%N",
   15652         1323 :                          is_key_order () ? "key-order"
   15653         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15654      2397788 :               dump.indent ();
   15655      2397788 :               walker.begin ();
   15656      2397788 :               if (current->get_entity_kind () == EK_USING)
   15657        39087 :                 walker.tree_node (OVL_FUNCTION (decl));
   15658      2358701 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15659              :                 /* We only stream its name and location.  */
   15660           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15661      2358608 :               else if (TREE_VISITED (decl))
   15662              :                 /* A global tree.  */;
   15663      2356086 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15664              :                 {
   15665         2589 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15666         2589 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15667              :                 }
   15668              :               else
   15669              :                 {
   15670      2353497 :                   walker.mark_declaration (decl, current->has_defn ());
   15671              : 
   15672      2353497 :                   if (!is_key_order ()
   15673      2353497 :                       && item->is_pending_entity ())
   15674              :                     {
   15675       544688 :                       tree ns = find_pending_key (decl, nullptr);
   15676       544688 :                       add_namespace_context (item, ns);
   15677              :                     }
   15678              : 
   15679      2353497 :                   auto ovr = make_temp_override
   15680      2353497 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15681      2353497 :                   walker.decl_value (decl, current);
   15682      2353497 :                   if (current->has_defn ())
   15683       454250 :                     walker.write_definition (decl, current->refs_tu_local ());
   15684      2353497 :                 }
   15685      2397788 :               walker.end ();
   15686              : 
   15687              :               /* If we see either a class template or a deduction guide, make
   15688              :                  sure to add all visible deduction guides.  We need to check
   15689              :                  both in case they have been added in separate modules, or
   15690              :                  one is in the GMF and would have otherwise been discarded.  */
   15691      2397788 :               if (!is_key_order ()
   15692      2397788 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15693        52453 :                 add_deduction_guides (decl);
   15694      2397788 :               if (!is_key_order ()
   15695      2397788 :                   && deduction_guide_p (decl))
   15696         2768 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15697              : 
   15698              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15699      2397788 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15700              :                 {
   15701        76501 :                   processing_template_decl_sentinel ptds;
   15702        76501 :                   ++processing_template_decl;
   15703       286582 :                   for (auto &info : dep_adl_entity_list)
   15704              :                     {
   15705       210081 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15706              :                                                           info.args, true);
   15707       493093 :                       for (tree fn : lkp_range (lookup))
   15708        72931 :                         add_dependency (make_dependency (fn, EK_DECL));
   15709              : 
   15710       210081 :                       if (info.rewrite)
   15711              :                         {
   15712        64152 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15713        64152 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15714              :                                                          info.args, true);
   15715       147874 :                           for (tree fn : lkp_range (lookup))
   15716        19570 :                             add_dependency (make_dependency (fn, EK_DECL));
   15717              :                         }
   15718       210081 :                       release_tree_vector (info.args);
   15719              :                     }
   15720        76501 :                   dep_adl_entity_list.truncate (0);
   15721        76501 :                 }
   15722              : 
   15723      2397788 :               if (!is_key_order ()
   15724      1221348 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15725      2822346 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15726              :                 {
   15727              :                   /* Mark all the explicit & partial specializations as
   15728              :                      reachable.  We search both specialization lists as some
   15729              :                      constrained partial specializations for class types are
   15730              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15731      1211618 :                   auto mark_reached = [this](tree spec)
   15732              :                     {
   15733       795274 :                       if (TYPE_P (spec))
   15734       229440 :                         spec = TYPE_NAME (spec);
   15735       795274 :                       int use_tpl;
   15736       795274 :                       node_template_info (spec, use_tpl);
   15737       795274 :                       if (use_tpl & 2)
   15738              :                         {
   15739        81732 :                           depset *spec_dep = find_dependency (spec);
   15740        81732 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15741        18345 :                             spec_dep = spec_dep->deps[0];
   15742        81732 :                           if (spec_dep->is_unreached ())
   15743              :                             {
   15744        16980 :                               reached_unreached = true;
   15745        16980 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15746        16980 :                               dump (dumper::DEPEND)
   15747            0 :                                 && dump ("Reaching unreached specialization"
   15748            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15749              :                             }
   15750              :                         }
   15751      1211618 :                     };
   15752              : 
   15753       416344 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15754      1192565 :                        cons; cons = TREE_CHAIN (cons))
   15755       776221 :                     mark_reached (TREE_VALUE (cons));
   15756       416344 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15757       435397 :                        cons; cons = TREE_CHAIN (cons))
   15758        19053 :                     mark_reached (TREE_VALUE (cons));
   15759              :                 }
   15760              : 
   15761      2397788 :               dump.outdent ();
   15762      2397788 :               current = NULL;
   15763              :             }
   15764              :         }
   15765              : 
   15766       309365 :       if (!reached_unreached)
   15767              :         break;
   15768              : 
   15769              :       /* It's possible the we reached the unreached before we
   15770              :          processed it in the above loop, so we'll be doing this an
   15771              :          extra time.  However, to avoid that we have to do some
   15772              :          bit shuffling that also involves a scan of the list.
   15773              :          Swings & roundabouts I guess.  */
   15774         1121 :       std::swap (worklist, unreached);
   15775              :     }
   15776              : 
   15777       308244 :   unreached.release ();
   15778       308244 : }
   15779              : 
   15780              : /* Compare two entries of a single binding.  TYPE_DECL before
   15781              :    non-exported before exported.  */
   15782              : 
   15783              : static int
   15784       918640 : binding_cmp (const void *a_, const void *b_)
   15785              : {
   15786       918640 :   depset *a = *(depset *const *)a_;
   15787       918640 :   depset *b = *(depset *const *)b_;
   15788              : 
   15789       918640 :   tree a_ent = a->get_entity ();
   15790       918640 :   tree b_ent = b->get_entity ();
   15791       918640 :   gcc_checking_assert (a_ent != b_ent
   15792              :                        && !a->is_binding ()
   15793              :                        && !b->is_binding ());
   15794              : 
   15795              :   /* Implicit typedefs come first.  */
   15796       918640 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15797       918640 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15798       918514 :   if (a_implicit || b_implicit)
   15799              :     {
   15800              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15801          252 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15802          378 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15803              :     }
   15804              : 
   15805              :   /* TU-local before non-TU-local.  */
   15806       918388 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15807       918388 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15808       918388 :   if (a_internal != b_internal)
   15809            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15810              : 
   15811              :   /* Hidden before non-hidden.  */
   15812       918388 :   bool a_hidden = a->is_hidden ();
   15813       918388 :   bool b_hidden = b->is_hidden ();
   15814       918388 :   if (a_hidden != b_hidden)
   15815            0 :     return a_hidden ? -1 : +1;
   15816              : 
   15817       918388 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15818       918388 :   bool a_export;
   15819       918388 :   if (a_using)
   15820              :     {
   15821       292434 :       a_export = OVL_EXPORT_P (a_ent);
   15822       292434 :       a_ent = OVL_FUNCTION (a_ent);
   15823              :     }
   15824       625954 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15825            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15826       625954 :            && DECL_MODULE_EXPORT_P (a_ent))
   15827              :     a_export = true;
   15828              :   else
   15829       625954 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15830              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15831              :                                      : STRIP_TEMPLATE (a_ent));
   15832              : 
   15833       918388 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15834       918388 :   bool b_export;
   15835       918388 :   if (b_using)
   15836              :     {
   15837       306168 :       b_export = OVL_EXPORT_P (b_ent);
   15838       306168 :       b_ent = OVL_FUNCTION (b_ent);
   15839              :     }
   15840       612220 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15841            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15842       612220 :            && DECL_MODULE_EXPORT_P (b_ent))
   15843              :     b_export = true;
   15844              :   else
   15845       612220 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15846              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15847              :                                      : STRIP_TEMPLATE (b_ent));
   15848              : 
   15849              :   /* Non-exports before exports.  */
   15850       918388 :   if (a_export != b_export)
   15851       218404 :     return a_export ? +1 : -1;
   15852              : 
   15853              :   /* At this point we don't care, but want a stable sort.  */
   15854              : 
   15855       778671 :   if (a_using != b_using)
   15856              :     /* using first.  */
   15857        24563 :     return a_using? -1 : +1;
   15858              : 
   15859       760989 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15860              : }
   15861              : 
   15862              : /* True iff TMPL has an explicit instantiation definition.
   15863              : 
   15864              :    This is local to module.cc because register_specialization skips adding most
   15865              :    instantiations unless module_maybe_has_cmi_p.  */
   15866              : 
   15867              : static bool
   15868           76 : template_has_explicit_inst (tree tmpl)
   15869              : {
   15870           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15871              :     {
   15872           24 :       tree spec = TREE_VALUE (t);
   15873           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15874           24 :           && !DECL_REALLY_EXTERN (spec))
   15875              :         return true;
   15876              :     }
   15877              :   return false;
   15878              : }
   15879              : 
   15880              : /* Complain about DEP that exposes a TU-local entity.
   15881              : 
   15882              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15883              :    if we explained anything.  */
   15884              : 
   15885              : bool
   15886          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15887              : {
   15888          127 :   tree decl = dep->get_entity ();
   15889              : 
   15890              :   /* Don't need to walk if we're not going to be emitting
   15891              :      any diagnostics anyway.  */
   15892          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15893           21 :                                      OPT_Wexpose_global_module_tu_local))
   15894              :     return false;
   15895              : 
   15896          523 :   for (depset *rdep : dep->deps)
   15897          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15898          369 :         && !is_exposure_of_member_type (dep, rdep))
   15899              :       {
   15900              :         // FIXME:QOI Better location information?  We're
   15901              :         // losing, so it doesn't matter about efficiency.
   15902          118 :         tree exposed = rdep->get_entity ();
   15903          118 :         auto_diagnostic_group d;
   15904          118 :         if (strict)
   15905              :           {
   15906              :             /* Allow suppressing the warning from the point of declaration
   15907              :                of the otherwise-exposed decl, for cases we know that
   15908              :                exposures will never be 'bad'.  */
   15909           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15910           27 :                                     OPT_Wexpose_global_module_tu_local)
   15911           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15912           18 :                             OPT_Wexpose_global_module_tu_local,
   15913              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15914              :               {
   15915           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15916           18 :                 gcc_checking_assert (informed);
   15917              :                 return true;
   15918              :               }
   15919              :           }
   15920              :         else
   15921              :           {
   15922           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15923              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15924           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15925           91 :             gcc_checking_assert (informed);
   15926           91 :             if (dep->is_tu_local (/*strict=*/true))
   15927            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15928              :                       "%qD is also TU-local but has been exposed elsewhere",
   15929              :                       decl);
   15930           91 :             return true;
   15931              :           }
   15932          118 :       }
   15933              : 
   15934              :   return false;
   15935              : }
   15936              : 
   15937              : /* Warn about a template DEP that references a TU-local entity.
   15938              : 
   15939              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15940              :    if we explained anything.  */
   15941              : 
   15942              : bool
   15943           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   15944              : {
   15945           94 :   tree decl = dep->get_entity ();
   15946              : 
   15947              :   /* Don't bother walking if we know we won't be emitting anything.  */
   15948           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15949           94 :                            OPT_Wtemplate_names_tu_local)
   15950              :       /* Only warn strictly if users haven't silenced this warning here.  */
   15951          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15952           27 :                                          OPT_Wexpose_global_module_tu_local)))
   15953            0 :     return false;
   15954              : 
   15955              :   /* Friend decls in a class body are ignored, but this is harmless:
   15956              :      it should not impact any consumers.  */
   15957           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   15958              :     return false;
   15959              : 
   15960              :   /* We should now only be warning about templates.  */
   15961           76 :   gcc_checking_assert
   15962              :     (TREE_CODE (decl) == TEMPLATE_DECL
   15963              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   15964              : 
   15965              :   /* Don't warn if we've seen any explicit instantiation definitions,
   15966              :      the intent might be for importers to only use those.  */
   15967           76 :   if (template_has_explicit_inst (decl))
   15968              :     return false;
   15969              : 
   15970          268 :   for (depset *rdep : dep->deps)
   15971          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   15972              :       {
   15973           67 :         tree ref = rdep->get_entity ();
   15974           67 :         auto_diagnostic_group d;
   15975           67 :         if (strict)
   15976              :           {
   15977           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   15978           15 :                                     OPT_Wexpose_global_module_tu_local)
   15979           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   15980            6 :                                OPT_Wtemplate_names_tu_local,
   15981              :                                "%qD refers to TU-local entity %qD, which may "
   15982              :                                "cause issues when instantiating in other TUs",
   15983              :                                decl, ref))
   15984              :               {
   15985            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   15986            6 :                 return true;
   15987              :               }
   15988              :           }
   15989           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   15990           52 :                              OPT_Wtemplate_names_tu_local,
   15991              :                              "%qD refers to TU-local entity %qD and cannot "
   15992              :                              "be instantiated in other TUs", decl, ref))
   15993              :           {
   15994           52 :             is_tu_local_entity (ref, /*explain=*/true);
   15995           52 :             return true;
   15996              :           }
   15997           67 :       }
   15998              : 
   15999              :   return false;
   16000              : }
   16001              : 
   16002              : /* Sort the bindings, issue errors about bad internal refs.  */
   16003              : 
   16004              : bool
   16005         2776 : depset::hash::finalize_dependencies ()
   16006              : {
   16007         2776 :   bool ok = true;
   16008      3810106 :   for (depset *dep : *this)
   16009              :     {
   16010      1903665 :       if (dep->is_binding ())
   16011              :         {
   16012              :           /* Keep the containing namespace dep first.  */
   16013       155019 :           gcc_checking_assert (dep->deps.length () > 1
   16014              :                                && (dep->deps[0]->get_entity_kind ()
   16015              :                                    == EK_NAMESPACE)
   16016              :                                && (dep->deps[0]->get_entity ()
   16017              :                                    == dep->get_entity ()));
   16018       155019 :           if (dep->deps.length () > 2)
   16019        14851 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16020              :                        sizeof (dep->deps[1]), binding_cmp);
   16021              : 
   16022              :           /* Bindings shouldn't refer to imported entities.  */
   16023       155019 :           if (CHECKING_P)
   16024       834416 :             for (depset *entity : dep->deps)
   16025       369359 :               gcc_checking_assert (!entity->is_import ());
   16026       155019 :           continue;
   16027       155019 :         }
   16028              : 
   16029              :       /* Otherwise, we'll check for bad internal refs.
   16030              :          Don't complain about any references from TU-local entities.  */
   16031      1748646 :       if (dep->is_tu_local ())
   16032          264 :         continue;
   16033              : 
   16034              :       /* We already complained about usings of non-external entities in
   16035              :          check_can_export_using_decl, don't do it again here.  */
   16036      1748382 :       if (dep->get_entity_kind () == EK_USING)
   16037        39087 :         continue;
   16038              : 
   16039      1709295 :       if (dep->is_exposure ())
   16040              :         {
   16041          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16042              : 
   16043              :           /* A TU-local variable will always be considered an exposure,
   16044              :              so we don't have to worry about strict-only handling.  */
   16045          106 :           tree decl = dep->get_entity ();
   16046          106 :           if (!explained
   16047           15 :               && VAR_P (decl)
   16048          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16049            6 :                   || DECL_INLINE_VAR_P (decl)))
   16050              :             {
   16051           15 :               auto_diagnostic_group d;
   16052           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16053            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16054              :                           "%qD is declared %<constexpr%> and is initialized to "
   16055              :                           "a TU-local value", decl);
   16056              :               else
   16057              :                 {
   16058              :                   /* This can only occur with references.  */
   16059            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16060            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16061              :                             "%qD is a reference declared %<inline%> and is "
   16062              :                             "constant-initialized to a TU-local value", decl);
   16063              :                 }
   16064           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16065              :                                                  /*explain=*/true);
   16066           15 :               gcc_checking_assert (informed);
   16067           15 :               explained = true;
   16068           15 :             }
   16069              : 
   16070              :           /* We should have emitted an error above, unless the warning was
   16071              :              silenced.  */
   16072          106 :           gcc_checking_assert (explained);
   16073          106 :           ok = false;
   16074          106 :           continue;
   16075          106 :         }
   16076              : 
   16077              :       /* In all other cases, we're just warning (rather than erroring).
   16078              :          We don't want to do too much warning, so let's just bail after
   16079              :          the first warning we successfully emit.  */
   16080      1709207 :       if (warn_expose_global_module_tu_local
   16081      1709189 :           && !dep->is_tu_local (/*strict=*/true)
   16082      1709151 :           && dep->is_exposure (/*strict=*/true)
   16083      1709210 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16084           18 :         continue;
   16085              : 
   16086      1709223 :       if (warn_template_names_tu_local
   16087       270941 :           && dep->refs_tu_local ()
   16088      1709238 :           && diagnose_template_names_tu_local (dep))
   16089           52 :         continue;
   16090              : 
   16091      1709119 :       if (warn_template_names_tu_local
   16092       270889 :           && warn_expose_global_module_tu_local
   16093       270889 :           && !dep->is_tu_local (/*strict=*/true)
   16094       270865 :           && dep->refs_tu_local (/*strict=*/true)
   16095           30 :           && !dep->is_exposure (/*strict=*/true)
   16096      1709146 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16097              :         continue;
   16098              :     }
   16099              : 
   16100         2776 :   return ok;
   16101              : }
   16102              : 
   16103              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16104              :    within a graph.  See https://en.wikipedia.org/wiki/
   16105              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16106              : 
   16107              :    We use depset::section as lowlink.  Completed nodes have
   16108              :    depset::cluster containing the cluster number, with the top
   16109              :    bit set.
   16110              : 
   16111              :    A useful property is that the output vector is a reverse
   16112              :    topological sort of the resulting DAG.  In our case that means
   16113              :    dependent SCCs are found before their dependers.  We make use of
   16114              :    that property.  */
   16115              : 
   16116              : void
   16117      2551787 : depset::tarjan::connect (depset *v)
   16118              : {
   16119      2551787 :   gcc_checking_assert (v->is_binding ()
   16120              :                        || !(v->is_tu_local ()
   16121              :                             || v->is_unreached ()
   16122              :                             || v->is_import ()));
   16123              : 
   16124      2551787 :   v->cluster = v->section = ++index;
   16125      2551787 :   stack.safe_push (v);
   16126              : 
   16127              :   /* Walk all our dependencies, ignore a first marked slot  */
   16128     22451164 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16129              :     {
   16130      8678931 :       depset *dep = v->deps[ix];
   16131              : 
   16132      8678931 :       if (dep->is_binding ()
   16133     17143827 :           || !(dep->is_import () || dep->is_tu_local ()))
   16134              :         {
   16135      8667392 :           unsigned lwm = dep->cluster;
   16136              : 
   16137      8667392 :           if (!dep->cluster)
   16138              :             {
   16139              :               /* A new node.  Connect it.  */
   16140      1427875 :               connect (dep);
   16141      1427875 :               lwm = dep->section;
   16142              :             }
   16143              : 
   16144      8667392 :           if (dep->section && v->section > lwm)
   16145      1348732 :             v->section = lwm;
   16146              :         }
   16147              :     }
   16148              : 
   16149      2551787 :   if (v->section == v->cluster)
   16150              :     {
   16151              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16152              :       unsigned num = v->cluster;
   16153      2551787 :       depset *p;
   16154      2551787 :       do
   16155              :         {
   16156      2551787 :           p = stack.pop ();
   16157      2551787 :           p->cluster = num;
   16158      2551787 :           p->section = 0;
   16159      2551787 :           result.quick_push (p);
   16160              :         }
   16161      2551787 :       while (p != v);
   16162              :     }
   16163      2551787 : }
   16164              : 
   16165              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16166              :    just trying to get consistency.  */
   16167              : 
   16168              : static int
   16169    122719465 : depset_cmp (const void *a_, const void *b_)
   16170              : {
   16171    122719465 :   depset *a = *(depset *const *)a_;
   16172    122719465 :   depset *b = *(depset *const *)b_;
   16173              : 
   16174    122719465 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16175    122719465 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16176              : 
   16177    122719465 :   if  (a_kind != b_kind)
   16178              :     /* Different entity kinds, order by that.  */
   16179      6162433 :     return a_kind < b_kind ? -1 : +1;
   16180              : 
   16181    118373567 :   tree a_decl = a->get_entity ();
   16182    118373567 :   tree b_decl = b->get_entity ();
   16183    118373567 :   if (a_kind == depset::EK_USING)
   16184              :     {
   16185              :       /* If one is a using, the other must be too.  */
   16186      2245117 :       a_decl = OVL_FUNCTION (a_decl);
   16187      2245117 :       b_decl = OVL_FUNCTION (b_decl);
   16188              :     }
   16189              : 
   16190    118373567 :   if (a_decl != b_decl)
   16191              :     /* Different entities, order by their UID.  */
   16192    110751443 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16193              : 
   16194      7622124 :   if (a_kind == depset::EK_BINDING)
   16195              :     {
   16196              :       /* Both are bindings.  Order by identifier hash.  */
   16197      7618930 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16198      7618930 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16199      7618930 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16200     11351126 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16201              :     }
   16202              : 
   16203              :   /* They are the same decl.  This can happen with two using decls
   16204              :      pointing to the same target.  The best we can aim for is
   16205              :      consistently telling qsort how to order them.  Hopefully we'll
   16206              :      never have to debug a case that depends on this.  Oh, who am I
   16207              :      kidding?  Good luck.  */
   16208         3194 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16209              : 
   16210              :   /* Order by depset address.  Not the best, but it is something.  */
   16211         3194 :   return a < b ? -1 : +1;
   16212              : }
   16213              : 
   16214              : /* Sort the clusters in SCC such that those that depend on one another
   16215              :    are placed later.   */
   16216              : 
   16217              : // FIXME: I am not convinced this is needed and, if needed,
   16218              : // sufficient.  We emit the decls in this order but that emission
   16219              : // could walk into later decls (from the body of the decl, or default
   16220              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16221              : // if it DTRT why do we need to sort here -- won't things naturally
   16222              : // work?  I think part of the issue is that when we're going to refer
   16223              : // to an entity by name, and that entity is in the same cluster as us,
   16224              : // we need to actually walk that entity, if we've not already walked
   16225              : // it.
   16226              : static void
   16227       305468 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16228              : {
   16229       305468 :   depset::hash table (size, original);
   16230              : 
   16231       305468 :   dump.indent ();
   16232              : 
   16233              :   /* Place bindings last, usings before that.  It's not strictly
   16234              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16235              :   unsigned bind_lwm = size;
   16236              :   unsigned use_lwm = size;
   16237      1675754 :   for (unsigned ix = 0; ix != use_lwm;)
   16238              :     {
   16239      1370286 :       depset *dep = scc[ix];
   16240      1370286 :       switch (dep->get_entity_kind ())
   16241              :         {
   16242       154762 :         case depset::EK_BINDING:
   16243              :           /* Move to end.  No increment.  Notice this could be moving
   16244              :              a using decl, which we'll then move again.  */
   16245       154762 :           if (--bind_lwm != ix)
   16246              :             {
   16247        88830 :               scc[ix] = scc[bind_lwm];
   16248        88830 :               scc[bind_lwm] = dep;
   16249              :             }
   16250       154762 :           if (use_lwm > bind_lwm)
   16251              :             {
   16252       123147 :               use_lwm--;
   16253       123147 :               break;
   16254              :             }
   16255              :           /* We must have copied a using or TU-local, so move it too.  */
   16256        31615 :           dep = scc[ix];
   16257        31615 :           gcc_checking_assert
   16258              :             (dep->get_entity_kind () == depset::EK_USING
   16259              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16260              :           /* FALLTHROUGH  */
   16261              : 
   16262        70699 :         case depset::EK_USING:
   16263        70699 :         case depset::EK_TU_LOCAL:
   16264        70699 :           if (--use_lwm != ix)
   16265              :             {
   16266        53321 :               scc[ix] = scc[use_lwm];
   16267        53321 :               scc[use_lwm] = dep;
   16268              :             }
   16269              :           break;
   16270              : 
   16271      1176440 :         case depset::EK_DECL:
   16272      1176440 :         case depset::EK_SPECIALIZATION:
   16273      1176440 :         case depset::EK_PARTIAL:
   16274      1176440 :           table.add_mergeable (dep);
   16275      1176440 :           ix++;
   16276      1176440 :           break;
   16277              : 
   16278            0 :         default:
   16279            0 :           gcc_unreachable ();
   16280              :         }
   16281              :     }
   16282              : 
   16283       305468 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16284       305756 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16285              : 
   16286       305468 :   table.find_dependencies (nullptr);
   16287              : 
   16288       305468 :   auto_vec<depset *> order = table.connect ();
   16289       610936 :   gcc_checking_assert (order.length () == use_lwm);
   16290              : 
   16291              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16292              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16293              :      we can get multi-entity clusters, in which case all but one must
   16294              :      only be reached from within the cluster.  This happens for
   16295              :      something like:
   16296              : 
   16297              :      template<typename T>
   16298              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16299              : 
   16300              :      The instantiation of TPL will be in the specialization table, and
   16301              :      refer to Foo via arg.  But we can only get to that specialization
   16302              :      from Foo's declaration, so we only need to treat Foo as mergable
   16303              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16304              : 
   16305              :      We approximate finding the single cluster entry dep by checking for
   16306              :      entities recursively depending on a dep first seen when streaming
   16307              :      its own merge key; the first dep we see in such a cluster should be
   16308              :      the first one streamed.  */
   16309              :   unsigned entry_pos = ~0u;
   16310              :   unsigned cluster = ~0u;
   16311      2963816 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16312              :     {
   16313      1176440 :       gcc_checking_assert (order[ix]->is_special ());
   16314      1176440 :       bool tight = order[ix]->cluster == cluster;
   16315      1176440 :       depset *dep = order[ix]->deps[0];
   16316      1177433 :       dump (dumper::MERGE)
   16317         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16318          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16319      1176440 :       scc[ix] = dep;
   16320      1176440 :       if (tight)
   16321              :         {
   16322          124 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16323          124 :           if (dep->is_entry ())
   16324              :             {
   16325              :               /* There should only be one entry dep in a cluster.  */
   16326            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16327            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16328            9 :               scc[ix] = scc[entry_pos];
   16329            9 :               scc[entry_pos] = dep;
   16330              :             }
   16331              :         }
   16332              :       else
   16333              :         entry_pos = ix;
   16334      1176440 :       cluster = order[ix]->cluster;
   16335              :     }
   16336              : 
   16337       305756 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16338       305468 :   dump.outdent ();
   16339       305468 : }
   16340              : 
   16341              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16342              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16343              :    its cluster number.  Each SCC has a unique cluster number, and are
   16344              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16345              : 
   16346              : vec<depset *>
   16347       308215 : depset::hash::connect ()
   16348              : {
   16349       308215 :   tarjan connector (size ());
   16350       308215 :   vec<depset *> deps;
   16351       308215 :   deps.create (size ());
   16352      6466573 :   for (depset *item : *this)
   16353              :     {
   16354      3079179 :       entity_kind kind = item->get_entity_kind ();
   16355      2924417 :       if (kind == EK_BINDING
   16356      2924417 :           || !(kind == EK_REDIRECT
   16357      2900355 :                || item->is_tu_local ()
   16358      2900224 :                || item->is_unreached ()
   16359      2405632 :                || item->is_import ()))
   16360      2551787 :         deps.quick_push (item);
   16361              :     }
   16362              : 
   16363              :   /* Iteration over the hash table is an unspecified ordering.  While
   16364              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16365              :      builds are tricky.  Secondly creating testcases that check
   16366              :      dependencies are correct by making sure a bad ordering would
   16367              :      happen if that was wrong.  */
   16368      1740342 :   deps.qsort (depset_cmp);
   16369              : 
   16370      2860002 :   while (deps.length ())
   16371              :     {
   16372      2551787 :       depset *v = deps.pop ();
   16373      2551787 :       dump (dumper::CLUSTER) &&
   16374         1800 :         (v->is_binding ()
   16375          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16376         1590 :          : dump ("Connecting %s %s %C:%N",
   16377         1590 :                  is_key_order () ? "key-order"
   16378          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16379         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16380              :                  v->get_entity ()));
   16381      2551787 :       if (!v->cluster)
   16382      1123912 :         connector.connect (v);
   16383              :     }
   16384              : 
   16385       308215 :   deps.release ();
   16386       616430 :   return connector.result;
   16387       308215 : }
   16388              : 
   16389              : /* Initialize location spans.  */
   16390              : 
   16391              : void
   16392         4913 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16393              : {
   16394         4913 :   gcc_checking_assert (!init_p ());
   16395         4913 :   spans = new vec<span> ();
   16396         4913 :   spans->reserve (20);
   16397              : 
   16398         4913 :   span interval;
   16399         4913 :   interval.ordinary.first = 0;
   16400         4913 :   interval.macro.second = MAX_LOCATION_T + 1;
   16401         4913 :   interval.ordinary_delta = interval.macro_delta = 0;
   16402              : 
   16403              :   /* A span for reserved fixed locs.  */
   16404         4913 :   interval.ordinary.second
   16405         4913 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16406         4913 :   interval.macro.first = interval.macro.second;
   16407         4913 :   dump (dumper::LOCATION)
   16408           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16409              :              interval.ordinary.first, interval.ordinary.second,
   16410              :              interval.macro.first, interval.macro.second);
   16411         4913 :   spans->quick_push (interval);
   16412              : 
   16413              :   /* A span for command line & forced headers.  */
   16414         4913 :   interval.ordinary.first = interval.ordinary.second;
   16415         4913 :   interval.macro.second = interval.macro.first;
   16416         4913 :   if (map)
   16417              :     {
   16418         4907 :       interval.ordinary.second = map->start_location;
   16419         4907 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16420              :     }
   16421         4913 :   dump (dumper::LOCATION)
   16422           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16423              :              interval.ordinary.first, interval.ordinary.second,
   16424              :              interval.macro.first, interval.macro.second);
   16425         4913 :   spans->quick_push (interval);
   16426              : 
   16427              :   /* Start an interval for the main file.  */
   16428         4913 :   interval.ordinary.first = interval.ordinary.second;
   16429         4913 :   interval.macro.second = interval.macro.first;
   16430         4913 :   dump (dumper::LOCATION)
   16431           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16432              :              interval.ordinary.first, interval.macro.second);
   16433         4913 :   spans->quick_push (interval);
   16434         4913 : }
   16435              : 
   16436              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16437              :    be propagated in our own location table.  I.e. we are the primary
   16438              :    interface and we're importing a partition.  */
   16439              : 
   16440              : bool
   16441         3056 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16442              : {
   16443         3056 :   bool opened = (module_interface_p () && !module_partition_p ()
   16444         3517 :                  && import->is_partition ());
   16445          172 :   if (opened)
   16446          172 :     open (hwm);
   16447         3056 :   return opened;
   16448              : }
   16449              : 
   16450              : /* Open a new linemap interval.  The just-created ordinary map is the
   16451              :    first map of the interval.  */
   16452              : 
   16453              : void
   16454         1073 : loc_spans::open (location_t hwm)
   16455              : {
   16456         1073 :   span interval;
   16457         1073 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16458         2146 :   interval.macro.first = interval.macro.second
   16459         1073 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16460         1073 :   interval.ordinary_delta = interval.macro_delta = 0;
   16461         1073 :   dump (dumper::LOCATION)
   16462            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16463            0 :              spans->length (), interval.ordinary.first,
   16464              :              interval.macro.second);
   16465         1073 :   if (spans->length ())
   16466              :     {
   16467              :       /* No overlapping!  */
   16468         1073 :       auto &last = spans->last ();
   16469         1073 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16470         1073 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16471              :     }
   16472         1073 :   spans->safe_push (interval);
   16473         1073 : }
   16474              : 
   16475              : /* Close out the current linemap interval.  The last maps are within
   16476              :    the interval.  */
   16477              : 
   16478              : void
   16479         5983 : loc_spans::close ()
   16480              : {
   16481         5983 :   span &interval = spans->last ();
   16482              : 
   16483         5983 :   interval.ordinary.second
   16484         5983 :     = ((line_table->highest_location
   16485         5983 :         + (loc_one << line_table->default_range_bits))
   16486         5983 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16487         5983 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16488         5983 :   dump (dumper::LOCATION)
   16489           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16490           21 :              spans->length () - 1,
   16491              :              interval.ordinary.first,interval.ordinary.second,
   16492              :              interval.macro.first, interval.macro.second);
   16493         5983 : }
   16494              : 
   16495              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16496              :    in.  NULL if it is not in an interval.  */
   16497              : 
   16498              : const loc_spans::span *
   16499     38200546 : loc_spans::ordinary (location_t loc)
   16500              : {
   16501     38200546 :   unsigned len = spans->length ();
   16502     76275123 :   unsigned pos = 0;
   16503     76285084 :   while (len)
   16504              :     {
   16505     76270182 :       unsigned half = len / 2;
   16506     76270182 :       const span &probe = (*spans)[pos + half];
   16507     76270182 :       if (loc < probe.ordinary.first)
   16508              :         len = half;
   16509     76260221 :       else if (loc < probe.ordinary.second)
   16510              :         return &probe;
   16511              :       else
   16512              :         {
   16513     38074577 :           pos += half + 1;
   16514     38074577 :           len = len - (half + 1);
   16515              :         }
   16516              :     }
   16517              :   return NULL;
   16518              : }
   16519              : 
   16520              : /* Likewise, given a macro location LOC, return the lmap interval it
   16521              :    resides in.   */
   16522              : 
   16523              : const loc_spans::span *
   16524      3014642 : loc_spans::macro (location_t loc)
   16525              : {
   16526      3014642 :   unsigned len = spans->length ();
   16527      6026262 :   unsigned pos = 0;
   16528      6026278 :   while (len)
   16529              :     {
   16530      6026250 :       unsigned half = len / 2;
   16531      6026250 :       const span &probe = (*spans)[pos + half];
   16532      6026250 :       if (loc >= probe.macro.second)
   16533              :         len = half;
   16534      6026234 :       else if (loc >= probe.macro.first)
   16535              :         return &probe;
   16536              :       else
   16537              :         {
   16538      3011620 :           pos += half + 1;
   16539      3011620 :           len = len - (half + 1);
   16540              :         }
   16541              :     }
   16542              :   return NULL;
   16543              : }
   16544              : 
   16545              : /* Return the ordinary location closest to FROM.  */
   16546              : 
   16547              : static location_t
   16548         6867 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16549              : {
   16550        13737 :   while (!IS_ORDINARY_LOC (from))
   16551              :     {
   16552            3 :       if (IS_ADHOC_LOC (from))
   16553            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16554            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16555              :         {
   16556              :           /* Find the ordinary location nearest FROM.  */
   16557            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16558            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16559            0 :           from = mac_map->get_expansion_point_location ();
   16560              :         }
   16561              :     }
   16562         6867 :   return from;
   16563              : }
   16564              : 
   16565              : static module_state **
   16566        12308 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16567              : {
   16568        12308 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16569        12308 :   hashval_t hv = module_state_hash::hash (ct);
   16570              : 
   16571        12308 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16572              : }
   16573              : 
   16574              : static module_state *
   16575       140860 : get_primary (module_state *parent)
   16576              : {
   16577       144864 :   while (parent->is_partition ())
   16578          652 :     parent = parent->parent;
   16579              : 
   16580       144212 :   if (!parent->name)
   16581              :     // Implementation unit has null name
   16582        85651 :     parent = parent->parent;
   16583              : 
   16584       139706 :   return parent;
   16585              : }
   16586              : 
   16587              : /* Find or create module NAME & PARENT in the hash table.  */
   16588              : 
   16589              : module_state *
   16590        12308 : get_module (tree name, module_state *parent, bool partition)
   16591              : {
   16592              :   /* We might be given an empty NAME if preprocessing fails to handle
   16593              :      a header-name token.  */
   16594        12308 :   if (name && TREE_CODE (name) == STRING_CST
   16595        15182 :       && TREE_STRING_LENGTH (name) == 0)
   16596              :     return nullptr;
   16597              : 
   16598        12308 :   if (partition)
   16599              :     {
   16600         1066 :       if (!parent)
   16601          226 :         parent = get_primary (this_module ());
   16602              : 
   16603         1066 :       if (!parent->is_partition () && !parent->flatname)
   16604          250 :         parent->set_flatname ();
   16605              :     }
   16606              : 
   16607        12308 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16608        12308 :   module_state *state = *slot;
   16609        12308 :   if (!state)
   16610              :     {
   16611         6666 :       state = (new (ggc_alloc<module_state> ())
   16612         6666 :                module_state (name, parent, partition));
   16613         6666 :       *slot = state;
   16614              :     }
   16615              :   return state;
   16616              : }
   16617              : 
   16618              : /* Process string name PTR into a module_state.  */
   16619              : 
   16620              : static module_state *
   16621          450 : get_module (const char *ptr)
   16622              : {
   16623              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16624              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16625              :      which clearly starts as pathnames as header-names and everything else is
   16626              :      treated as a (possibly malformed) named moduled.  */
   16627          450 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16628              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16629              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16630              : #endif
   16631              :       || false)
   16632              :     /* A header name.  */
   16633          111 :     return get_module (build_string (strlen (ptr), ptr));
   16634              : 
   16635              :   bool partition = false;
   16636              :   module_state *mod = NULL;
   16637              : 
   16638         1312 :   for (const char *probe = ptr;; probe++)
   16639         1651 :     if (!*probe || *probe == '.' || *probe == ':')
   16640              :       {
   16641          426 :         if (probe == ptr)
   16642              :           return NULL;
   16643              : 
   16644          426 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16645              :                           mod, partition);
   16646          426 :         ptr = probe;
   16647          426 :         if (*ptr == ':')
   16648              :           {
   16649           84 :             if (partition)
   16650              :               return NULL;
   16651              :             partition = true;
   16652              :           }
   16653              : 
   16654          426 :         if (!*ptr++)
   16655              :           break;
   16656              :       }
   16657         1225 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16658           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16659              :       return NULL;
   16660              : 
   16661              :   return mod;
   16662              : }
   16663              : 
   16664              : /* Create a new mapper connecting to OPTION.  */
   16665              : 
   16666              : module_client *
   16667         4913 : make_mapper (location_t loc, class mkdeps *deps)
   16668              : {
   16669         4913 :   timevar_start (TV_MODULE_MAPPER);
   16670         4913 :   const char *option = module_mapper_name;
   16671         4913 :   if (!option)
   16672         4865 :     option = getenv ("CXX_MODULE_MAPPER");
   16673              : 
   16674         9826 :   mapper = module_client::open_module_client
   16675         4913 :     (loc, option, deps, &set_cmi_repo,
   16676         4913 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16677         4913 :      && save_decoded_options[0].arg != progname
   16678              :      ? save_decoded_options[0].arg : nullptr);
   16679              : 
   16680         4913 :   timevar_stop (TV_MODULE_MAPPER);
   16681              : 
   16682         4913 :   return mapper;
   16683              : }
   16684              : 
   16685              : static unsigned lazy_snum;
   16686              : 
   16687              : static bool
   16688        11945 : recursive_lazy (unsigned snum = ~0u)
   16689              : {
   16690        11945 :   if (lazy_snum)
   16691              :     {
   16692            0 :       error_at (input_location, "recursive lazy load");
   16693            0 :       return true;
   16694              :     }
   16695              : 
   16696        11945 :   lazy_snum = snum;
   16697        11945 :   return false;
   16698              : }
   16699              : 
   16700              : /* If THIS has an interface dependency on itself, report an error and
   16701              :    return false.  */
   16702              : 
   16703              : bool
   16704         2904 : module_state::check_circular_import (location_t from)
   16705              : {
   16706         2904 :   if (this == this_module ())
   16707              :     {
   16708              :       /* Cannot import the current module.  */
   16709            9 :       auto_diagnostic_group d;
   16710            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16711            9 :       if (!header_module_p ())
   16712            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16713            9 :       return false;
   16714            9 :     }
   16715              :   return true;
   16716              : }
   16717              : 
   16718              : /* Module name substitutions.  */
   16719              : static vec<module_state *,va_heap> substs;
   16720              : 
   16721              : void
   16722         9085 : module_state::mangle (bool include_partition)
   16723              : {
   16724         9085 :   if (subst)
   16725          425 :     mangle_module_substitution (subst);
   16726              :   else
   16727              :     {
   16728         8660 :       if (parent)
   16729          858 :         parent->mangle (include_partition);
   16730         8660 :       if (include_partition || !is_partition ())
   16731              :         {
   16732              :           // Partitions are significant for global initializer
   16733              :           // functions
   16734         8463 :           bool partition = is_partition () && !parent->is_partition ();
   16735         8463 :           subst = mangle_module_component (name, partition);
   16736         8463 :           substs.safe_push (this);
   16737              :         }
   16738              :     }
   16739         9085 : }
   16740              : 
   16741              : void
   16742         8227 : mangle_module (int mod, bool include_partition)
   16743              : {
   16744         8227 :   module_state *imp = (*modules)[mod];
   16745              : 
   16746         8227 :   gcc_checking_assert (!imp->is_header ());
   16747              : 
   16748         8227 :   if (!imp->name)
   16749              :     /* Set when importing the primary module interface.  */
   16750          223 :     imp = imp->parent;
   16751              : 
   16752              :   /* Ensure this is actually a module unit.  */
   16753          223 :   gcc_checking_assert (imp);
   16754              : 
   16755         8227 :   imp->mangle (include_partition);
   16756         8227 : }
   16757              : 
   16758              : /* Clean up substitutions.  */
   16759              : void
   16760         7756 : mangle_module_fini ()
   16761              : {
   16762        16219 :   while (substs.length ())
   16763         8463 :     substs.pop ()->subst = 0;
   16764         7756 : }
   16765              : 
   16766              : /* Announce WHAT about the module.  */
   16767              : 
   16768              : void
   16769        12647 : module_state::announce (const char *what) const
   16770              : {
   16771        12647 :   if (noisy_p ())
   16772              :     {
   16773            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16774            0 :       fflush (stderr);
   16775              :     }
   16776        12647 : }
   16777              : 
   16778              : /* A human-readable README section.  The contents of this section to
   16779              :    not contribute to the CRC, so the contents can change per
   16780              :    compilation.  That allows us to embed CWD, hostname, build time and
   16781              :    what not.  It is a STRTAB that may be extracted with:
   16782              :      readelf -pgnu.c++.README $(module).gcm */
   16783              : 
   16784              : void
   16785         2747 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16786              : {
   16787         2747 :   bytes_out readme (to);
   16788              : 
   16789         2747 :   readme.begin (false);
   16790              : 
   16791         2747 :   readme.printf ("GNU C++ %s",
   16792         2747 :                  is_header () ? "header unit"
   16793         1848 :                  : !is_partition () ? "primary interface"
   16794          196 :                  : is_interface () ? "interface partition"
   16795              :                  : "internal partition");
   16796              : 
   16797              :   /* Compiler's version.  */
   16798         2747 :   readme.printf ("compiler: %s", version_string);
   16799              : 
   16800              :   /* Module format version.  */
   16801         2747 :   verstr_t string;
   16802         2747 :   version2string (MODULE_VERSION, string);
   16803         2747 :   readme.printf ("version: %s", string);
   16804              : 
   16805              :   /* Module information.  */
   16806         2747 :   readme.printf ("module: %s", get_flatname ());
   16807         2747 :   readme.printf ("source: %s", main_input_filename);
   16808         2747 :   readme.printf ("dialect: %s", dialect);
   16809         2747 :   if (extensions)
   16810           30 :     readme.printf ("extensions: %s%s%s",
   16811              :                    extensions & SE_OPENMP ? "-fopenmp"
   16812            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16813              :                    (extensions & SE_OPENACC)
   16814            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16815              :                    ? " " : "",
   16816           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16817              : 
   16818              :   /* The following fields could be expected to change between
   16819              :      otherwise identical compilations.  Consider a distributed build
   16820              :      system.  We should have a way of overriding that.  */
   16821         2747 :   if (char *cwd = getcwd (NULL, 0))
   16822              :     {
   16823         2747 :       readme.printf ("cwd: %s", cwd);
   16824         2747 :       free (cwd);
   16825              :     }
   16826         5494 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16827              : #if NETWORKING
   16828              :   {
   16829              :     char hostname[64];
   16830              :     if (!gethostname (hostname, sizeof (hostname)))
   16831              :       readme.printf ("host: %s", hostname);
   16832              :   }
   16833              : #endif
   16834         2747 :   {
   16835              :     /* This of course will change!  */
   16836         2747 :     time_t stampy;
   16837         2747 :     auto kind = cpp_get_date (reader, &stampy);
   16838         2747 :     if (kind != CPP_time_kind::UNKNOWN)
   16839              :       {
   16840         2747 :         struct tm *time;
   16841              : 
   16842         2747 :         time = gmtime (&stampy);
   16843         2747 :         readme.print_time ("build", time, "UTC");
   16844              : 
   16845         2747 :         if (kind == CPP_time_kind::DYNAMIC)
   16846              :           {
   16847         2747 :             time = localtime (&stampy);
   16848         2747 :             readme.print_time ("local", time,
   16849              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16850              :                                time->tm_zone
   16851              : #else
   16852              :                                ""
   16853              : #endif
   16854              :                                );
   16855              :           }
   16856              :       }
   16857              :   }
   16858              : 
   16859              :   /* Its direct imports.  */
   16860         3414 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16861              :     {
   16862          667 :       module_state *state = (*modules)[ix];
   16863              : 
   16864          667 :       if (state->is_direct ())
   16865          979 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16866              :                        state->get_flatname (), state->filename);
   16867              :     }
   16868              : 
   16869         2747 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16870         2747 : }
   16871              : 
   16872              : /* Sort environment var names in reverse order.  */
   16873              : 
   16874              : static int
   16875            0 : env_var_cmp (const void *a_, const void *b_)
   16876              : {
   16877            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16878            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16879              : 
   16880            0 :   for (unsigned ix = 0; ; ix++)
   16881              :     {
   16882            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16883            0 :       if (a[ix] == b[ix])
   16884              :         {
   16885            0 :           if (a_end)
   16886              :             break;
   16887              :         }
   16888              :       else
   16889              :         {
   16890            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16891              : 
   16892            0 :           if (!a_end && !b_end)
   16893            0 :             return a[ix] < b[ix] ? +1 : -1;
   16894            0 :           if (a_end && b_end)
   16895              :             break;
   16896            0 :           return a_end ? +1 : -1;
   16897              :         }
   16898            0 :     }
   16899              : 
   16900              :   return 0;
   16901              : }
   16902              : 
   16903              : /* Write the environment. It is a STRTAB that may be extracted with:
   16904              :      readelf -pgnu.c++.ENV $(module).gcm */
   16905              : 
   16906              : void
   16907            0 : module_state::write_env (elf_out *to)
   16908              : {
   16909            0 :   vec<const char *> vars;
   16910            0 :   vars.create (20);
   16911              : 
   16912            0 :   extern char **environ;
   16913            0 :   while (const char *var = environ[vars.length ()])
   16914            0 :     vars.safe_push (var);
   16915            0 :   vars.qsort (env_var_cmp);
   16916              : 
   16917            0 :   bytes_out env (to);
   16918            0 :   env.begin (false);
   16919            0 :   while (vars.length ())
   16920            0 :     env.printf ("%s", vars.pop ());
   16921            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16922              : 
   16923            0 :   vars.release ();
   16924            0 : }
   16925              : 
   16926              : /* Write the direct or indirect imports.
   16927              :    u:N
   16928              :    {
   16929              :      u:index
   16930              :      s:name
   16931              :      u32:crc
   16932              :      s:filename (direct)
   16933              :      u:exported (direct)
   16934              :    } imports[N]
   16935              :  */
   16936              : 
   16937              : void
   16938          904 : module_state::write_imports (bytes_out &sec, bool direct)
   16939              : {
   16940          904 :   unsigned count = 0;
   16941              : 
   16942         1912 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16943              :     {
   16944         1008 :       module_state *imp = (*modules)[ix];
   16945              : 
   16946         1008 :       if (imp->remap && imp->is_direct () == direct)
   16947          483 :         count++;
   16948              :     }
   16949              : 
   16950          904 :   gcc_assert (!direct || count);
   16951              : 
   16952          904 :   sec.u (count);
   16953         1912 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16954              :     {
   16955         1008 :       module_state *imp = (*modules)[ix];
   16956              : 
   16957         1008 :       if (imp->remap && imp->is_direct () == direct)
   16958              :         {
   16959          645 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   16960              :                            !direct ? "indirect "
   16961           81 :                            : imp->exported_p ? "exported " : "",
   16962              :                            ix, imp->remap, imp, imp->crc);
   16963          483 :           sec.u (imp->remap);
   16964          483 :           sec.str (imp->get_flatname ());
   16965          483 :           sec.u32 (imp->crc);
   16966          483 :           if (direct)
   16967              :             {
   16968          474 :               write_location (sec, imp->imported_from ());
   16969          474 :               sec.str (imp->filename);
   16970          474 :               int exportedness = 0;
   16971          474 :               if (imp->exported_p)
   16972              :                 exportedness = +1;
   16973          280 :               else if (!imp->is_purview_direct ())
   16974           13 :                 exportedness = -1;
   16975          474 :               sec.i (exportedness);
   16976              :             }
   16977              :         }
   16978              :     }
   16979          904 : }
   16980              : 
   16981              : /* READER, LMAPS  != NULL == direct imports,
   16982              :    == NUL == indirect imports.  */
   16983              : 
   16984              : unsigned
   16985          766 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   16986              : {
   16987          766 :   unsigned count = sec.u ();
   16988          766 :   unsigned loaded = 0;
   16989              : 
   16990         1937 :   while (count--)
   16991              :     {
   16992          405 :       unsigned ix = sec.u ();
   16993          405 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   16994              :         {
   16995            0 :           sec.set_overrun ();
   16996            0 :           break;
   16997              :         }
   16998              : 
   16999          405 :       const char *name = sec.str (NULL);
   17000          405 :       module_state *imp = get_module (name);
   17001          405 :       unsigned crc = sec.u32 ();
   17002          405 :       int exportedness = 0;
   17003              : 
   17004              :       /* If the import is a partition, it must be the same primary
   17005              :          module as this TU.  */
   17006          405 :       if (imp && imp->is_partition () &&
   17007              :           (!named_module_p ()
   17008          135 :            || (get_primary (this_module ()) != get_primary (imp))))
   17009              :         imp = NULL;
   17010              : 
   17011          405 :       if (!imp)
   17012            0 :         sec.set_overrun ();
   17013          405 :       if (sec.get_overrun ())
   17014              :         break;
   17015              : 
   17016          405 :       if (lmaps)
   17017              :         {
   17018              :           /* A direct import, maybe load it.  */
   17019          401 :           location_t floc = read_location (sec);
   17020          401 :           const char *fname = sec.str (NULL);
   17021          401 :           exportedness = sec.i ();
   17022              : 
   17023          401 :           if (sec.get_overrun ())
   17024              :             break;
   17025              : 
   17026          401 :           if (!imp->check_circular_import (floc))
   17027            3 :             continue;
   17028              : 
   17029          398 :           if (imp->loadedness == ML_NONE)
   17030              :             {
   17031          314 :               imp->loc = floc;
   17032          314 :               imp->crc = crc;
   17033          314 :               if (!imp->get_flatname ())
   17034          271 :                 imp->set_flatname ();
   17035              : 
   17036          314 :               unsigned n = dump.push (imp);
   17037              : 
   17038          314 :               if (!imp->filename && fname)
   17039          271 :                 imp->filename = xstrdup (fname);
   17040              : 
   17041          314 :               if (imp->is_partition ())
   17042           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17043              : 
   17044          314 :               if (!imp->do_import (reader, false))
   17045            3 :                 imp = NULL;
   17046          314 :               dump.pop (n);
   17047          314 :               if (!imp)
   17048            3 :                 continue;
   17049              :             }
   17050              : 
   17051          395 :           if (is_partition ())
   17052              :             {
   17053           66 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17054              :                 {
   17055           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17056           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17057              :                 }
   17058           66 :               if (exportedness > 0)
   17059            6 :                 imp->exported_p = true;
   17060              :             }
   17061              :         }
   17062              :       else
   17063              :         {
   17064              :           /* An indirect import, find it, it should already be here.  */
   17065            4 :           if (imp->loadedness == ML_NONE)
   17066              :             {
   17067            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17068            0 :               continue;
   17069              :             }
   17070              :         }
   17071              : 
   17072          399 :       if (imp->crc != crc)
   17073            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17074              : 
   17075          399 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17076              : 
   17077          399 :       if (lmaps && exportedness >= 0)
   17078          381 :         set_import (imp, bool (exportedness));
   17079          579 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17080           90 :                        : exportedness > 0 ? "exported "
   17081           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17082              :                        imp->mod);
   17083          399 :       loaded++;
   17084              :     }
   17085              : 
   17086          766 :   return loaded;
   17087              : }
   17088              : 
   17089              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17090              : 
   17091              : void
   17092          452 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17093              : {
   17094          530 :   dump () && dump ("Writing imports");
   17095          452 :   dump.indent ();
   17096              : 
   17097          452 :   bytes_out sec (to);
   17098          452 :   sec.begin ();
   17099              : 
   17100          452 :   write_imports (sec, true);
   17101          452 :   write_imports (sec, false);
   17102              : 
   17103          452 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17104          452 :   dump.outdent ();
   17105          452 : }
   17106              : 
   17107              : bool
   17108          383 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17109              : {
   17110          383 :   bytes_in sec;
   17111              : 
   17112          383 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17113              :     return false;
   17114              : 
   17115          470 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17116          383 :   dump.indent ();
   17117              : 
   17118              :   /* Read the imports.  */
   17119          383 :   unsigned direct = read_imports (sec, reader, lmaps);
   17120          383 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17121          383 :   if (direct + indirect + 1 != slurp->remap->length ())
   17122            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17123              : 
   17124          383 :   dump.outdent ();
   17125          383 :   if (!sec.end (from ()))
   17126              :     return false;
   17127              :   return true;
   17128          383 : }
   17129              : 
   17130              : /* We're the primary module interface, but have partitions.  Document
   17131              :    them so that non-partition module implementation units know which
   17132              :    have already been loaded.  */
   17133              : 
   17134              : void
   17135          133 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17136              : {
   17137          157 :   dump () && dump ("Writing %u elided partitions", count);
   17138          133 :   dump.indent ();
   17139              : 
   17140          133 :   bytes_out sec (to);
   17141          133 :   sec.begin ();
   17142              : 
   17143          341 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17144              :     {
   17145          208 :       module_state *imp = (*modules)[ix];
   17146          208 :       if (imp->is_partition ())
   17147              :         {
   17148          223 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17149              :                            imp, imp->crc);
   17150          184 :           sec.str (imp->get_flatname ());
   17151          184 :           sec.u32 (imp->crc);
   17152          359 :           write_location (sec, imp->is_direct ()
   17153          175 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17154          184 :           sec.str (imp->filename);
   17155              :         }
   17156              :     }
   17157              : 
   17158          133 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17159          133 :   dump.outdent ();
   17160          133 : }
   17161              : 
   17162              : bool
   17163           27 : module_state::read_partitions (unsigned count)
   17164              : {
   17165           27 :   bytes_in sec;
   17166           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17167              :     return false;
   17168              : 
   17169           33 :   dump () && dump ("Reading %u elided partitions", count);
   17170           27 :   dump.indent ();
   17171              : 
   17172           66 :   while (count--)
   17173              :     {
   17174           39 :       const char *name = sec.str (NULL);
   17175           39 :       unsigned crc = sec.u32 ();
   17176           39 :       location_t floc = read_location (sec);
   17177           39 :       const char *fname = sec.str (NULL);
   17178              : 
   17179           39 :       if (sec.get_overrun ())
   17180              :         break;
   17181              : 
   17182           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17183              : 
   17184           39 :       module_state *imp = get_module (name);
   17185           39 :       if (!imp  /* Partition should be ...  */
   17186           39 :           || !imp->is_partition () /* a partition ...  */
   17187           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17188           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17189              :         {
   17190            0 :           sec.set_overrun ();
   17191            0 :           break;
   17192              :         }
   17193              : 
   17194           39 :       if (!imp->has_location ())
   17195           30 :         imp->loc = floc;
   17196           39 :       imp->crc = crc;
   17197           39 :       if (!imp->filename && fname[0])
   17198           30 :         imp->filename = xstrdup (fname);
   17199              :     }
   17200              : 
   17201           27 :   dump.outdent ();
   17202           27 :   if (!sec.end (from ()))
   17203              :     return false;
   17204              :   return true;
   17205           27 : }
   17206              : 
   17207              : /* Data for config reading and writing.  */
   17208              : struct module_state_config {
   17209              :   const char *dialect_str = get_dialect ();
   17210              :   line_map_uint_t ordinary_locs = 0;
   17211              :   line_map_uint_t macro_locs = 0;
   17212              :   unsigned num_imports = 0;
   17213              :   unsigned num_partitions = 0;
   17214              :   unsigned num_entities = 0;
   17215              :   unsigned loc_range_bits = 0;
   17216              :   unsigned active_init = 0;
   17217              : 
   17218        96822 :   static void release ()
   17219              :   {
   17220        96822 :     XDELETEVEC (dialect);
   17221        96822 :     dialect = NULL;
   17222              :   }
   17223              : 
   17224              : private:
   17225              :   static const char *get_dialect ();
   17226              :   static char *dialect;
   17227              : };
   17228              : 
   17229              : char *module_state_config::dialect;
   17230              : 
   17231              : /* Generate a string of the significant compilation options.
   17232              :    Generally assume the user knows what they're doing, in the same way
   17233              :    that object files can be mixed.  */
   17234              : 
   17235              : const char *
   17236         5936 : module_state_config::get_dialect ()
   17237              : {
   17238         5936 :   if (!dialect)
   17239         9398 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17240              :                       /* C++ implies these, only show if disabled.  */
   17241         4699 :                       flag_exceptions ? "" : "/no-exceptions",
   17242         4699 :                       flag_rtti ? "" : "/no-rtti",
   17243         4699 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17244              :                       /* C++ 20 implies concepts and coroutines.  */
   17245         1473 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17246         4699 :                       (cxx_dialect < cxx20 && flag_coroutines
   17247              :                        ? "/coroutines" : ""),
   17248         4699 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17249         4699 :                       flag_contracts ? "/contracts" : "",
   17250              :                       NULL);
   17251              : 
   17252         5936 :   return dialect;
   17253              : }
   17254              : 
   17255              : /* Contents of a cluster.  */
   17256              : enum cluster_tag {
   17257              :   ct_decl,      /* A decl.  */
   17258              :   ct_defn,      /* A definition.  */
   17259              :   ct_bind,      /* A binding.  */
   17260              :   ct_hwm
   17261              : };
   17262              : 
   17263              : /* Binding modifiers.  */
   17264              : enum ct_bind_flags
   17265              : {
   17266              :   cbf_export = 0x1,     /* An exported decl.  */
   17267              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17268              :   cbf_using = 0x4,      /* A using decl.  */
   17269              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17270              : };
   17271              : 
   17272              : /* DEP belongs to a different cluster, seed it to prevent
   17273              :    unfortunately timed duplicate import.  */
   17274              : // FIXME: QOI For inter-cluster references we could just only pick
   17275              : // one entity from an earlier cluster.  Even better track
   17276              : // dependencies between earlier clusters
   17277              : 
   17278              : void
   17279      8701093 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17280              : {
   17281      8701093 :   if (dep->is_tu_local ())
   17282              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17283      8700884 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17284              :     {
   17285      3841108 :       tree ent = dep->get_entity ();
   17286      3841108 :       if (!TREE_VISITED (ent))
   17287              :         {
   17288      1599087 :           sec.tree_node (ent);
   17289      1599537 :           dump (dumper::CLUSTER)
   17290          450 :             && dump ("Seeded %s %N",
   17291          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17292              :         }
   17293              :     }
   17294      8701093 : }
   17295              : 
   17296              : /* Write the cluster of depsets in SCC[0-SIZE).
   17297              :    dep->section -> section number
   17298              :    dep->cluster -> entity number
   17299              :  */
   17300              : 
   17301              : unsigned
   17302       305468 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17303              :                              depset::hash &table, unsigned *counts,
   17304              :                              unsigned *crc_ptr)
   17305              : {
   17306       307002 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17307       305468 :   dump.indent ();
   17308              : 
   17309       305468 :   trees_out sec (to, this, table, table.section);
   17310       305468 :   sec.begin ();
   17311       305468 :   unsigned index_lwm = counts[MSC_entities];
   17312              : 
   17313              :   /* Determine entity numbers, mark for writing.   */
   17314       305777 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17315      1675754 :   for (unsigned ix = 0; ix != size; ix++)
   17316              :     {
   17317      1370286 :       depset *b = scc[ix];
   17318              : 
   17319      1370286 :       switch (b->get_entity_kind ())
   17320              :         {
   17321            0 :         default:
   17322            0 :           gcc_unreachable ();
   17323              : 
   17324       154762 :         case depset::EK_BINDING:
   17325       154762 :           {
   17326       154762 :             dump (dumper::CLUSTER)
   17327          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17328              :                        b->get_entity (), b->get_name ());
   17329       154762 :             depset *ns_dep = b->deps[0];
   17330       154762 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17331              :                                  == depset::EK_NAMESPACE
   17332              :                                  && ns_dep->get_entity () == b->get_entity ());
   17333       368845 :             for (unsigned jx = b->deps.length (); --jx;)
   17334              :               {
   17335       214083 :                 depset *dep = b->deps[jx];
   17336              :                 // We could be declaring something that is also a
   17337              :                 // (merged) import
   17338       253215 :                 gcc_checking_assert (dep->is_import ()
   17339              :                                      || TREE_VISITED (dep->get_entity ())
   17340              :                                      || (dep->get_entity_kind ()
   17341              :                                          == depset::EK_USING)
   17342              :                                      || (dep->get_entity_kind ()
   17343              :                                          == depset::EK_TU_LOCAL));
   17344              :               }
   17345              :           }
   17346              :           break;
   17347              : 
   17348      1176440 :         case depset::EK_DECL:
   17349      1176440 :         case depset::EK_SPECIALIZATION:
   17350      1176440 :         case depset::EK_PARTIAL:
   17351      1176440 :           b->cluster = counts[MSC_entities]++;
   17352      1176440 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17353              :           /* FALLTHROUGH  */
   17354              : 
   17355      1215524 :         case depset::EK_USING:
   17356      1215524 :         case depset::EK_TU_LOCAL:
   17357      2431048 :           gcc_checking_assert (!b->is_import ()
   17358              :                                && !b->is_unreached ());
   17359      1373460 :           dump (dumper::CLUSTER)
   17360         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17361          729 :                      b->has_defn () ? "definition" : "declaration",
   17362              :                      b->get_entity ());
   17363              :           break;
   17364              :         }
   17365              :     }
   17366       305777 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17367              : 
   17368              :   /* Ensure every out-of-cluster decl is referenced before we start
   17369              :      streaming.  We must do both imports *and* earlier clusters,
   17370              :      because the latter could reach into the former and cause a
   17371              :      duplicate loop.   */
   17372       305468 :   sec.set_importing (+1);
   17373      1675754 :   for (unsigned ix = 0; ix != size; ix++)
   17374              :     {
   17375      1370286 :       depset *b = scc[ix];
   17376     17470761 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17377              :         {
   17378      7368984 :           depset *dep = b->deps[jx];
   17379              : 
   17380      7368984 :           if (dep->is_binding ())
   17381              :             {
   17382      1760179 :               for (unsigned ix = dep->deps.length (); --ix;)
   17383              :                 {
   17384      1332109 :                   depset *bind = dep->deps[ix];
   17385      1332109 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17386       466691 :                     bind = bind->deps[1];
   17387              : 
   17388      1332109 :                   intercluster_seed (sec, index_lwm, bind);
   17389              :                 }
   17390              :               /* Also check the namespace itself.  */
   17391       214035 :               dep = dep->deps[0];
   17392              :             }
   17393              : 
   17394      7368984 :           intercluster_seed (sec, index_lwm, dep);
   17395              :         }
   17396              :     }
   17397       305468 :   sec.tree_node (NULL_TREE);
   17398              :   /* We're done importing now.  */
   17399       305468 :   sec.set_importing (-1);
   17400              : 
   17401              :   /* Write non-definitions.  */
   17402      1675754 :   for (unsigned ix = 0; ix != size; ix++)
   17403              :     {
   17404      1370286 :       depset *b = scc[ix];
   17405      1370286 :       tree decl = b->get_entity ();
   17406      1370286 :       switch (b->get_entity_kind ())
   17407              :         {
   17408            0 :         default:
   17409            0 :           gcc_unreachable ();
   17410       154762 :           break;
   17411              : 
   17412       154762 :         case depset::EK_BINDING:
   17413       154762 :           {
   17414       154762 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17415       155960 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17416              :                              decl, b->get_name ());
   17417       154762 :             sec.u (ct_bind);
   17418       154762 :             sec.tree_node (decl);
   17419       154762 :             sec.tree_node (b->get_name ());
   17420              : 
   17421              :             /* Write in reverse order, so reading will see the exports
   17422              :                first, thus building the overload chain will be
   17423              :                optimized.  */
   17424       523607 :             for (unsigned jx = b->deps.length (); --jx;)
   17425              :               {
   17426       214083 :                 depset *dep = b->deps[jx];
   17427       214083 :                 tree bound = dep->get_entity ();
   17428       214083 :                 unsigned flags = 0;
   17429       214083 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17430              :                   flags |= cbf_internal;
   17431       214035 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17432              :                   {
   17433        39084 :                     tree ovl = bound;
   17434        39084 :                     bound = OVL_FUNCTION (bound);
   17435        39084 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17436         4847 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17437         4097 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17438              :                       /* An unscoped enumerator in its enumeration's
   17439              :                          scope is not a using.  */
   17440              :                       flags |= cbf_using;
   17441        39084 :                     if (OVL_EXPORT_P (ovl))
   17442        37997 :                       flags |= cbf_export;
   17443              :                   }
   17444              :                 else
   17445              :                   {
   17446              :                     /* An implicit typedef must be at one.  */
   17447       174951 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17448       174951 :                     if (dep->is_hidden ())
   17449              :                       flags |= cbf_hidden;
   17450       174863 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17451       138757 :                       flags |= cbf_export;
   17452              :                   }
   17453              : 
   17454       214083 :                 gcc_checking_assert (DECL_P (bound));
   17455              : 
   17456       214083 :                 sec.i (flags);
   17457       214083 :                 if (flags & cbf_internal)
   17458              :                   {
   17459           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17460           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17461              :                   }
   17462              :                 else
   17463       214035 :                   sec.tree_node (bound);
   17464              :               }
   17465              : 
   17466              :             /* Terminate the list.  */
   17467       154762 :             sec.i (-1);
   17468              :           }
   17469       154762 :           break;
   17470              : 
   17471        39084 :         case depset::EK_USING:
   17472        39084 :         case depset::EK_TU_LOCAL:
   17473        39111 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17474           27 :                            TREE_CODE (decl), decl);
   17475              :           break;
   17476              : 
   17477      1176440 :         case depset::EK_SPECIALIZATION:
   17478      1176440 :         case depset::EK_PARTIAL:
   17479      1176440 :         case depset::EK_DECL:
   17480      1179587 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17481              :                            b->entity_kind_name (), b->cluster,
   17482         3147 :                            TREE_CODE (decl), decl);
   17483              : 
   17484      1176440 :           sec.u (ct_decl);
   17485      1176440 :           sec.tree_node (decl);
   17486              : 
   17487      1373433 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17488         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17489              :           break;
   17490              :         }
   17491              :     }
   17492              : 
   17493              :   depset *namer = NULL;
   17494              : 
   17495              :   /* Write out definitions  */
   17496      1675754 :   for (unsigned ix = 0; ix != size; ix++)
   17497              :     {
   17498      1370286 :       depset *b = scc[ix];
   17499      1370286 :       tree decl = b->get_entity ();
   17500      1370286 :       switch (b->get_entity_kind ())
   17501              :         {
   17502              :         default:
   17503              :           break;
   17504              : 
   17505      1176440 :         case depset::EK_SPECIALIZATION:
   17506      1176440 :         case depset::EK_PARTIAL:
   17507      1176440 :         case depset::EK_DECL:
   17508      1176440 :           if (!namer)
   17509       290193 :             namer = b;
   17510              : 
   17511      1176440 :           if (b->has_defn ())
   17512              :             {
   17513       454069 :               sec.u (ct_defn);
   17514       454069 :               sec.tree_node (decl);
   17515       455012 :               dump () && dump ("Writing definition %N", decl);
   17516       454069 :               sec.write_definition (decl, b->refs_tu_local ());
   17517              : 
   17518       454069 :               if (!namer->has_defn ())
   17519      1370286 :                 namer = b;
   17520              :             }
   17521              :           break;
   17522              :         }
   17523              :     }
   17524              : 
   17525              :   /* We don't find the section by name.  Use depset's decl's name for
   17526              :      human friendliness.  */
   17527       305468 :   unsigned name = 0;
   17528       305468 :   tree naming_decl = NULL_TREE;
   17529       305468 :   if (namer)
   17530              :     {
   17531       290193 :       naming_decl = namer->get_entity ();
   17532       290193 :       if (namer->get_entity_kind () == depset::EK_USING)
   17533              :         /* This unfortunately names the section from the target of the
   17534              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17535            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17536       290193 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17537              :         /* Lose any anonymousness.  */
   17538       101910 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17539       290193 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17540              :     }
   17541              : 
   17542       305468 :   unsigned bytes = sec.pos;
   17543       305468 :   unsigned snum = sec.end (to, name, crc_ptr);
   17544              : 
   17545      1675754 :   for (unsigned ix = size; ix--;)
   17546      1370286 :     gcc_checking_assert (scc[ix]->section == snum);
   17547              : 
   17548       305468 :   dump.outdent ();
   17549       307002 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17550              : 
   17551       305468 :   return bytes;
   17552       305468 : }
   17553              : 
   17554              : /* Read a cluster from section SNUM.  */
   17555              : 
   17556              : bool
   17557       207557 : module_state::read_cluster (unsigned snum)
   17558              : {
   17559       207557 :   trees_in sec (this);
   17560              : 
   17561       207557 :   if (!sec.begin (loc, from (), snum))
   17562              :     return false;
   17563              : 
   17564       208808 :   dump () && dump ("Reading section:%u", snum);
   17565       207557 :   dump.indent ();
   17566              : 
   17567              :   /* We care about structural equality.  */
   17568       207557 :   comparing_dependent_aliases++;
   17569              : 
   17570              :   /* First seed the imports.  */
   17571      1288578 :   while (tree import = sec.tree_node ())
   17572      2369785 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17573              : 
   17574      1527723 :   while (!sec.get_overrun () && sec.more_p ())
   17575              :     {
   17576      1320166 :       unsigned ct = sec.u ();
   17577      1320166 :       switch (ct)
   17578              :         {
   17579            0 :         default:
   17580            0 :           sec.set_overrun ();
   17581            0 :           break;
   17582              : 
   17583       108387 :         case ct_bind:
   17584              :           /* A set of namespace bindings.  */
   17585       108387 :           {
   17586       108387 :             tree ns = sec.tree_node ();
   17587       108387 :             tree name = sec.tree_node ();
   17588       108387 :             tree decls = NULL_TREE;
   17589       108387 :             tree visible = NULL_TREE;
   17590       108387 :             tree internal = NULL_TREE;
   17591       108387 :             tree type = NULL_TREE;
   17592       108387 :             bool dedup = false;
   17593       108387 :             bool global_p = is_header ();
   17594              : 
   17595              :             /* We rely on the bindings being in the reverse order of
   17596              :                the resulting overload set.  */
   17597       254689 :             for (;;)
   17598              :               {
   17599       254689 :                 int flags = sec.i ();
   17600       254689 :                 if (flags < 0)
   17601              :                   break;
   17602              : 
   17603       146302 :                 if ((flags & cbf_hidden)
   17604           63 :                     && (flags & (cbf_using | cbf_export)))
   17605            0 :                   sec.set_overrun ();
   17606       146302 :                 if ((flags & cbf_internal)
   17607           15 :                     && flags != cbf_internal)
   17608            0 :                   sec.set_overrun ();
   17609              : 
   17610            0 :                 if (flags & cbf_internal)
   17611              :                   {
   17612           15 :                     tree name = sec.tree_node ();
   17613           15 :                     location_t loc = read_location (sec);
   17614           15 :                     if (sec.get_overrun ())
   17615              :                       break;
   17616              : 
   17617           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17618           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17619           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17620           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17621           15 :                     continue;
   17622           15 :                   }
   17623              : 
   17624       146287 :                 tree decl = sec.tree_node ();
   17625       146287 :                 if (sec.get_overrun ())
   17626              :                   break;
   17627              : 
   17628       146287 :                 if (!global_p)
   17629              :                   {
   17630              :                     /* Check if the decl could require GM merging.  */
   17631         8882 :                     tree orig = get_originating_module_decl (decl);
   17632         8882 :                     tree inner = STRIP_TEMPLATE (orig);
   17633         8882 :                     if (!DECL_LANG_SPECIFIC (inner)
   17634        17764 :                         || !DECL_MODULE_ATTACH_P (inner))
   17635              :                       global_p = true;
   17636              :                   }
   17637              : 
   17638       146287 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17639              :                   {
   17640              :                     /* Stat hack.  */
   17641           51 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17642            0 :                       sec.set_overrun ();
   17643              : 
   17644           51 :                     if (flags & cbf_using)
   17645              :                       {
   17646            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17647              :                                                     USING_DECL,
   17648            3 :                                                     DECL_NAME (decl),
   17649              :                                                     NULL_TREE);
   17650            3 :                         USING_DECL_DECLS (type) = decl;
   17651            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17652            3 :                         DECL_CONTEXT (type) = ns;
   17653              : 
   17654            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17655            3 :                         if (flags & cbf_export)
   17656            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17657              :                       }
   17658              :                     else
   17659              :                       type = decl;
   17660              :                   }
   17661              :                 else
   17662              :                   {
   17663       146236 :                     if ((flags & cbf_using) &&
   17664        17224 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17665              :                       {
   17666              :                         /* We should only see a single non-function using-decl
   17667              :                            for a binding; more than that would clash.  */
   17668         4792 :                         if (decls)
   17669            0 :                           sec.set_overrun ();
   17670              : 
   17671              :                         /* FIXME: Propagate the location of the using-decl
   17672              :                            for use in diagnostics.  */
   17673         4792 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17674              :                                                      USING_DECL,
   17675         4792 :                                                      DECL_NAME (decl),
   17676              :                                                      NULL_TREE);
   17677         4792 :                         USING_DECL_DECLS (decls) = decl;
   17678              :                         /* We don't currently record the actual scope of the
   17679              :                            using-declaration, but this approximation should
   17680              :                            generally be good enough.  */
   17681         4792 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17682         4792 :                         DECL_CONTEXT (decls) = ns;
   17683              : 
   17684         4792 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17685         4792 :                         if (flags & cbf_export)
   17686         4780 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17687              :                       }
   17688       141444 :                     else if (decls
   17689       103580 :                              || (flags & (cbf_hidden | cbf_using))
   17690       238683 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17691              :                       {
   17692        59947 :                         decls = ovl_make (decl, decls);
   17693        59947 :                         if (flags & cbf_using)
   17694              :                           {
   17695        12432 :                             dedup = true;
   17696        12432 :                             OVL_USING_P (decls) = true;
   17697        12432 :                             OVL_PURVIEW_P (decls) = true;
   17698        12432 :                             if (flags & cbf_export)
   17699        12417 :                               OVL_EXPORT_P (decls) = true;
   17700              :                           }
   17701              : 
   17702        59947 :                         if (flags & cbf_hidden)
   17703           63 :                           OVL_HIDDEN_P (decls) = true;
   17704        59884 :                         else if (dedup)
   17705        15523 :                           OVL_DEDUP_P (decls) = true;
   17706              :                       }
   17707              :                     else
   17708              :                       decls = decl;
   17709              : 
   17710       146224 :                     if (flags & cbf_export
   17711       146236 :                         || (!(flags & cbf_hidden)
   17712         8874 :                             && (is_module () || is_partition ())))
   17713              :                       visible = decls;
   17714              :                   }
   17715              :               }
   17716              : 
   17717       108387 :             if (!decls && !internal)
   17718            0 :               sec.set_overrun ();
   17719              : 
   17720       108387 :             if (sec.get_overrun ())
   17721              :               break; /* Bail.  */
   17722              : 
   17723       109260 :             dump () && dump ("Binding of %P", ns, name);
   17724       108387 :             if (!set_module_binding (ns, name, mod, global_p,
   17725       108387 :                                      is_module () || is_partition (),
   17726              :                                      decls, type, visible, internal))
   17727            0 :               sec.set_overrun ();
   17728              :           }
   17729              :           break;
   17730              : 
   17731       874009 :         case ct_decl:
   17732              :           /* A decl.  */
   17733       874009 :           {
   17734       874009 :             tree decl = sec.tree_node ();
   17735      2405426 :             dump () && dump ("Read declaration of %N", decl);
   17736              :           }
   17737              :           break;
   17738              : 
   17739       337770 :         case ct_defn:
   17740       337770 :           {
   17741       337770 :             tree decl = sec.tree_node ();
   17742       339075 :             dump () && dump ("Reading definition of %N", decl);
   17743       337770 :             sec.read_definition (decl);
   17744              :           }
   17745       337770 :           break;
   17746              :         }
   17747              :     }
   17748              : 
   17749              :   /* When lazy loading is in effect, we can be in the middle of
   17750              :      parsing or instantiating a function.  Save it away.
   17751              :      push_function_context does too much work.   */
   17752       207557 :   tree old_cfd = current_function_decl;
   17753       207557 :   struct function *old_cfun = cfun;
   17754       391286 :   for (const post_process_data& pdata : sec.post_process ())
   17755              :     {
   17756       139671 :       tree decl = pdata.decl;
   17757              : 
   17758       139671 :       bool abstract = false;
   17759       139671 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17760              :         {
   17761        87080 :           abstract = true;
   17762        87080 :           decl = DECL_TEMPLATE_RESULT (decl);
   17763              :         }
   17764              : 
   17765       139671 :       current_function_decl = decl;
   17766       139671 :       allocate_struct_function (decl, abstract);
   17767       139671 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17768       139671 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17769       139671 :       cfun->function_start_locus = pdata.start_locus;
   17770       139671 :       cfun->function_end_locus = pdata.end_locus;
   17771       139671 :       cfun->language->returns_value = pdata.returns_value;
   17772       139671 :       cfun->language->returns_null = pdata.returns_null;
   17773       139671 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17774       139671 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17775       139671 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17776              : 
   17777              :       /* Make sure we emit explicit instantiations.
   17778              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17779       139671 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17780       139671 :           && !DECL_EXTERNAL (decl))
   17781           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17782              : 
   17783       139671 :       if (abstract)
   17784              :         ;
   17785        52591 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17786         8895 :         vec_safe_push (post_load_decls, decl);
   17787              :       else
   17788              :         {
   17789        43696 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17790              : #ifdef PCC_STATIC_STRUCT_RETURN
   17791              :           cfun->returns_pcc_struct = aggr;
   17792              : #endif
   17793        43696 :           cfun->returns_struct = aggr;
   17794        43696 :           expand_or_defer_fn (decl);
   17795              : 
   17796              :           /* If we first see this function after at_eof, it doesn't get
   17797              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17798              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17799              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17800              :              FIXME handle this outside module.cc after GCC 15.  */
   17801         4281 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17802        45327 :               && DECL_NOT_REALLY_EXTERN (decl))
   17803         1588 :             DECL_EXTERNAL (decl) = false;
   17804              :         }
   17805              : 
   17806              :     }
   17807       207633 :   for (const tree& type : sec.post_process_type ())
   17808              :     {
   17809              :       /* Attempt to complete an array type now in case its element type
   17810              :          had a definition streamed later in the cluster.  */
   17811           36 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17812           36 :       complete_type (type);
   17813              :     }
   17814       207557 :   set_cfun (old_cfun);
   17815       207557 :   current_function_decl = old_cfd;
   17816       207557 :   comparing_dependent_aliases--;
   17817              : 
   17818       207557 :   dump.outdent ();
   17819       208808 :   dump () && dump ("Read section:%u", snum);
   17820              : 
   17821       207557 :   loaded_clusters++;
   17822              : 
   17823       207557 :   if (!sec.end (from ()))
   17824              :     return false;
   17825              : 
   17826              :   return true;
   17827       207557 : }
   17828              : 
   17829              : void
   17830       157642 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17831              : {
   17832       157642 :   unsigned ns_num = dep->cluster;
   17833       157642 :   unsigned ns_import = 0;
   17834              : 
   17835       157642 :   if (dep->is_import ())
   17836            0 :     ns_import = dep->section;
   17837       157642 :   else if (dep->get_entity () != global_namespace)
   17838       103070 :     ns_num++;
   17839              : 
   17840       157642 :   sec.u (ns_import);
   17841       157642 :   sec.u (ns_num);
   17842       157642 : }
   17843              : 
   17844              : tree
   17845       193199 : module_state::read_namespace (bytes_in &sec)
   17846              : {
   17847       193199 :   unsigned ns_import = sec.u ();
   17848       193199 :   unsigned ns_num = sec.u ();
   17849       193199 :   tree ns = NULL_TREE;
   17850              : 
   17851       193199 :   if (ns_import || ns_num)
   17852              :     {
   17853       122675 :       if (!ns_import)
   17854       122675 :         ns_num--;
   17855              : 
   17856       122675 :       if (unsigned origin = slurp->remap_module (ns_import))
   17857              :         {
   17858       122675 :           module_state *from = (*modules)[origin];
   17859       122675 :           if (ns_num < from->entity_num)
   17860              :             {
   17861       122675 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17862              : 
   17863       122675 :               if (!slot.is_lazy ())
   17864       122675 :                 ns = slot;
   17865              :             }
   17866              :         }
   17867              :       else
   17868            0 :         sec.set_overrun ();
   17869              :     }
   17870              :   else
   17871        70524 :     ns = global_namespace;
   17872              : 
   17873       193199 :   return ns;
   17874              : }
   17875              : 
   17876              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17877              :    to MOD_SNAME_PFX.nms section.   */
   17878              : 
   17879              : void
   17880          583 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17881              :                                 unsigned num, unsigned *crc_p)
   17882              : {
   17883          641 :   dump () && dump ("Writing namespaces");
   17884          583 :   dump.indent ();
   17885              : 
   17886          583 :   bytes_out sec (to);
   17887          583 :   sec.begin ();
   17888              : 
   17889         3151 :   for (unsigned ix = 0; ix != num; ix++)
   17890              :     {
   17891         2568 :       depset *b = spaces[ix];
   17892         2568 :       tree ns = b->get_entity ();
   17893              : 
   17894              :       /* This could be an anonymous namespace even for a named module,
   17895              :          since we can still emit no-linkage decls.  */
   17896         2568 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17897              : 
   17898         2568 :       unsigned flags = 0;
   17899         2568 :       if (TREE_PUBLIC (ns))
   17900         2510 :         flags |= 1;
   17901         2568 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17902          483 :         flags |= 2;
   17903         2568 :       if (DECL_MODULE_PURVIEW_P (ns))
   17904         2022 :         flags |= 4;
   17905         2568 :       if (DECL_MODULE_EXPORT_P (ns))
   17906         1803 :         flags |= 8;
   17907         2568 :       if (TREE_DEPRECATED (ns))
   17908           17 :         flags |= 16;
   17909              : 
   17910         2708 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17911              :                        b->cluster, ns,
   17912          140 :                        flags & 1 ? ", public" : "",
   17913          140 :                        flags & 2 ? ", inline" : "",
   17914          140 :                        flags & 4 ? ", purview" : "",
   17915          140 :                        flags & 8 ? ", export" : "",
   17916          140 :                        flags & 16 ? ", deprecated" : "");
   17917         2568 :       sec.u (b->cluster);
   17918         2568 :       sec.u (to->name (DECL_NAME (ns)));
   17919         2568 :       write_namespace (sec, b->deps[0]);
   17920              : 
   17921         2568 :       sec.u (flags);
   17922         2568 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17923              : 
   17924         2568 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17925              :         {
   17926          483 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17927              :             {
   17928          125 :               tree tags = TREE_VALUE (attr);
   17929          125 :               sec.u (list_length (tags));
   17930          253 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17931          128 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17932              :             }
   17933              :           else
   17934          358 :             sec.u (0);
   17935              :         }
   17936              :     }
   17937              : 
   17938          583 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17939          583 :   dump.outdent ();
   17940          583 : }
   17941              : 
   17942              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   17943              :    SPACES from that data.  */
   17944              : 
   17945              : bool
   17946          598 : module_state::read_namespaces (unsigned num)
   17947              : {
   17948          598 :   bytes_in sec;
   17949              : 
   17950          598 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   17951              :     return false;
   17952              : 
   17953          668 :   dump () && dump ("Reading namespaces");
   17954          598 :   dump.indent ();
   17955              : 
   17956         3411 :   for (unsigned ix = 0; ix != num; ix++)
   17957              :     {
   17958         2813 :       unsigned entity_index = sec.u ();
   17959         2813 :       unsigned name = sec.u ();
   17960              : 
   17961         2813 :       tree parent = read_namespace (sec);
   17962              : 
   17963              :       /* See comment in write_namespace about why not bits.  */
   17964         2813 :       unsigned flags = sec.u ();
   17965         2813 :       location_t src_loc = read_location (sec);
   17966         2813 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   17967              : 
   17968         2813 :       if (entity_index >= entity_num
   17969         2813 :           || !parent
   17970         2813 :           || (flags & 0xc) == 0x8)
   17971            0 :         sec.set_overrun ();
   17972              : 
   17973              :       tree tags = NULL_TREE;
   17974         2953 :       while (tags_count--)
   17975              :         {
   17976          140 :           size_t len;
   17977          140 :           const char *str = sec.str (&len);
   17978          140 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   17979          140 :           tags = nreverse (tags);
   17980              :         }
   17981              : 
   17982         2813 :       if (sec.get_overrun ())
   17983              :         break;
   17984              : 
   17985         5568 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   17986              : 
   17987         3007 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   17988              :                        entity_index, parent, id,
   17989           97 :                        flags & 1 ? ", public" : "",
   17990              :                        flags & 2 ? ", inline" : "",
   17991           97 :                        flags & 4 ? ", purview" : "",
   17992           97 :                        flags & 8 ? ", export" : "",
   17993           97 :                        flags & 16 ? ", deprecated" : "");
   17994         2813 :       bool visible_p = ((flags & 8)
   17995         2813 :                         || ((flags & 1)
   17996          533 :                             && (flags & 4)
   17997          101 :                             && (is_partition () || is_module ())));
   17998         2813 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   17999              :                                            bool (flags & 2), visible_p);
   18000         2813 :       if (!inner)
   18001              :         {
   18002            0 :           sec.set_overrun ();
   18003            0 :           break;
   18004              :         }
   18005              : 
   18006         2813 :       if (is_partition ())
   18007              :         {
   18008           48 :           if (flags & 4)
   18009           42 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18010           48 :           if (flags & 8)
   18011           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18012              :         }
   18013              : 
   18014         2813 :       if (flags & 16)
   18015           19 :         TREE_DEPRECATED (inner) = true;
   18016              : 
   18017         2813 :       if (tags)
   18018          137 :         DECL_ATTRIBUTES (inner)
   18019          274 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18020              : 
   18021              :       /* Install the namespace.  */
   18022         2813 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18023         2813 :       if (DECL_MODULE_IMPORT_P (inner))
   18024              :         {
   18025            0 :           bool existed;
   18026            0 :           unsigned *slot = &entity_map->get_or_insert
   18027            0 :             (DECL_UID (inner), &existed);
   18028            0 :           if (existed)
   18029              :             /* If it existed, it should match.  */
   18030            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18031              :           else
   18032            0 :             *slot = entity_lwm + entity_index;
   18033              :         }
   18034              :     }
   18035              : 
   18036          598 :   dump.outdent ();
   18037          598 :   if (!sec.end (from ()))
   18038              :     return false;
   18039              :   return true;
   18040          598 : }
   18041              : 
   18042              : unsigned
   18043          583 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18044              :                                       vec<depset *> spaces, unsigned *crc_p)
   18045              : {
   18046          641 :   dump () && dump ("Writing using-directives");
   18047          583 :   dump.indent ();
   18048              : 
   18049          583 :   bytes_out sec (to);
   18050          583 :   sec.begin ();
   18051              : 
   18052          583 :   unsigned num = 0;
   18053         3734 :   auto emit_one_ns = [&](depset *parent_dep)
   18054              :     {
   18055         3151 :       tree parent = parent_dep->get_entity ();
   18056         3676 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18057              :         {
   18058          179 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18059           17 :             continue;
   18060          162 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18061          162 :           tree target = USING_DECL_DECLS (udir);
   18062          162 :           depset *target_dep = table.find_dependency (target);
   18063              : 
   18064              :           /* An using-directive imported from a different module might not
   18065              :              have been walked earlier (PR c++/122915).  But importers will
   18066              :              be able to just refer to the decl in that module unless it was
   18067              :              a partition anyway, so we don't have anything to do here.  */
   18068          162 :           if (!target_dep)
   18069              :             {
   18070            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18071            6 :               continue;
   18072              :             }
   18073              : 
   18074          180 :           dump () && dump ("Writing using-directive in %N for %N",
   18075              :                            parent, target);
   18076          156 :           sec.u (exported);
   18077          156 :           write_namespace (sec, parent_dep);
   18078          156 :           write_namespace (sec, target_dep);
   18079          156 :           ++num;
   18080              :         }
   18081         3734 :     };
   18082              : 
   18083          583 :   emit_one_ns (table.find_dependency (global_namespace));
   18084         4317 :   for (depset *parent_dep : spaces)
   18085         2568 :     emit_one_ns (parent_dep);
   18086              : 
   18087          583 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18088          583 :   dump.outdent ();
   18089              : 
   18090          583 :   return num;
   18091          583 : }
   18092              : 
   18093              : bool
   18094          157 : module_state::read_using_directives (unsigned num)
   18095              : {
   18096          157 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18097              :     {
   18098           12 :       dump () && dump ("Ignoring using-directives because module %M "
   18099              :                        "is not visible in this TU", this);
   18100            9 :       return true;
   18101              :     }
   18102              : 
   18103          148 :   bytes_in sec;
   18104              : 
   18105          148 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18106              :     return false;
   18107              : 
   18108          160 :   dump () && dump ("Reading using-directives");
   18109          148 :   dump.indent ();
   18110              : 
   18111          324 :   for (unsigned ix = 0; ix != num; ++ix)
   18112              :     {
   18113          176 :       bool exported = sec.u ();
   18114          176 :       tree parent = read_namespace (sec);
   18115          176 :       tree target = read_namespace (sec);
   18116          176 :       if (sec.get_overrun ())
   18117              :         break;
   18118              : 
   18119          188 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18120          176 :       if (exported || is_module () || is_partition ())
   18121          149 :         add_imported_using_namespace (parent, target);
   18122              :     }
   18123              : 
   18124          148 :   dump.outdent ();
   18125          148 :   if (!sec.end (from ()))
   18126              :     return false;
   18127              :   return true;
   18128          148 : }
   18129              : 
   18130              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18131              : 
   18132              : unsigned
   18133         2747 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18134              : {
   18135         3047 :   dump () && dump ("Writing binding table");
   18136         2747 :   dump.indent ();
   18137              : 
   18138         2747 :   unsigned num = 0;
   18139         2747 :   bytes_out sec (to);
   18140         2747 :   sec.begin ();
   18141              : 
   18142      2756188 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18143              :     {
   18144      1375347 :       depset *b = sccs[ix];
   18145      1375347 :       if (b->is_binding ())
   18146              :         {
   18147       154762 :           tree ns = b->get_entity ();
   18148       155960 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18149              :                            b->section);
   18150       154762 :           sec.u (to->name (b->get_name ()));
   18151       154762 :           write_namespace (sec, b->deps[0]);
   18152       154762 :           sec.u (b->section);
   18153       154762 :           num++;
   18154              :         }
   18155              :     }
   18156              : 
   18157         2747 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18158         2747 :   dump.outdent ();
   18159              : 
   18160         2747 :   return num;
   18161         2747 : }
   18162              : 
   18163              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18164              : 
   18165              : bool
   18166         2932 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18167              : {
   18168         2932 :   bytes_in sec;
   18169              : 
   18170         2932 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18171              :     return false;
   18172              : 
   18173         3461 :   dump () && dump ("Reading binding table");
   18174         2932 :   dump.indent ();
   18175       192966 :   for (; !sec.get_overrun () && num--;)
   18176              :     {
   18177       190034 :       const char *name = from ()->name (sec.u ());
   18178       190034 :       tree ns = read_namespace (sec);
   18179       190034 :       unsigned snum = sec.u ();
   18180              : 
   18181       190034 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18182            0 :         sec.set_overrun ();
   18183       190034 :       if (!sec.get_overrun ())
   18184              :         {
   18185       190034 :           tree id = get_identifier (name);
   18186       191545 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18187       190034 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18188              :             break;
   18189              :         }
   18190              :     }
   18191              : 
   18192         2932 :   dump.outdent ();
   18193         2932 :   if (!sec.end (from ()))
   18194              :     return false;
   18195              :   return true;
   18196         2932 : }
   18197              : 
   18198              : /* Write the entity table to MOD_SNAME_PFX.ent
   18199              : 
   18200              :    Each entry is a section number.  */
   18201              : 
   18202              : void
   18203         2487 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18204              :                               unsigned count, unsigned *crc_p)
   18205              : {
   18206         2734 :   dump () && dump ("Writing entities");
   18207         2487 :   dump.indent ();
   18208              : 
   18209         2487 :   bytes_out sec (to);
   18210         2487 :   sec.begin ();
   18211              : 
   18212         2487 :   unsigned current = 0;
   18213      1377813 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18214              :     {
   18215      1375326 :       depset *d = depsets[ix];
   18216              : 
   18217      2556821 :       switch (d->get_entity_kind ())
   18218              :         {
   18219              :         default:
   18220              :           break;
   18221              : 
   18222         5055 :         case depset::EK_NAMESPACE:
   18223         5055 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18224              :             {
   18225         2568 :               gcc_checking_assert (d->cluster == current);
   18226         2568 :               current++;
   18227         2568 :               sec.u (0);
   18228              :             }
   18229              :           break;
   18230              : 
   18231      1176440 :         case depset::EK_DECL:
   18232      1176440 :         case depset::EK_SPECIALIZATION:
   18233      1176440 :         case depset::EK_PARTIAL:
   18234      2352880 :           gcc_checking_assert (!d->is_unreached ()
   18235              :                                && !d->is_import ()
   18236              :                                && d->cluster == current
   18237              :                                && d->section);
   18238      1176440 :           current++;
   18239      1176440 :           sec.u (d->section);
   18240      1176440 :           break;
   18241              :         }
   18242              :     }
   18243         2487 :   gcc_assert (count == current);
   18244         2487 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18245         2487 :   dump.outdent ();
   18246         2487 : }
   18247              : 
   18248              : bool
   18249         2706 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18250              : {
   18251         2706 :   trees_in sec (this);
   18252              : 
   18253         2706 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18254              :     return false;
   18255              : 
   18256         3181 :   dump () && dump ("Reading entities");
   18257         2706 :   dump.indent ();
   18258              : 
   18259      1300531 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18260              :     {
   18261      1297825 :       unsigned snum = sec.u ();
   18262      1297825 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18263            0 :         sec.set_overrun ();
   18264      1297825 :       if (sec.get_overrun ())
   18265              :         break;
   18266              : 
   18267      1297825 :       if (snum)
   18268      1295012 :         slot->set_lazy (snum << 2);
   18269              :     }
   18270              : 
   18271         2706 :   dump.outdent ();
   18272         2706 :   if (!sec.end (from ()))
   18273              :     return false;
   18274              :   return true;
   18275         2706 : }
   18276              : 
   18277              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18278              : 
   18279              :    The pending table holds information about clusters that need to be
   18280              :    loaded because they contain information about something that is not
   18281              :    found by namespace-scope lookup.
   18282              : 
   18283              :    The three cases are:
   18284              : 
   18285              :    (a) Template (maybe-partial) specializations that we have
   18286              :    instantiated or defined.  When an importer needs to instantiate
   18287              :    that template, they /must have/ the partial, explicit & extern
   18288              :    specializations available.  If they have the other specializations
   18289              :    available, they'll have less work to do.  Thus, when we're about to
   18290              :    instantiate FOO, we have to be able to ask 'are there any
   18291              :    specialization of FOO in our imports?'.
   18292              : 
   18293              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18294              :    be defined in one header, and an inline member defined in a
   18295              :    different header (this occurs in the STL).  Similarly, like the
   18296              :    specialization case, an implicit member function could have been
   18297              :    'instantiated' in one module, and it'd be nice to not have to
   18298              :    reinstantiate it in another.
   18299              : 
   18300              :    (c) Classes completed elsewhere.  A class could be declared in one
   18301              :    header and defined in another.  We need to know to load the class
   18302              :    definition before looking in it.  It does highlight an issue --
   18303              :    there could be an intermediate import between the outermost containing
   18304              :    namespace-scope class and the innermost being-defined class.  This is
   18305              :    actually possible with all of these cases, so be aware -- we're not
   18306              :    just talking of one level of import to get to the innermost namespace.
   18307              : 
   18308              :    This gets complicated fast, it took me multiple attempts to even
   18309              :    get something remotely working.  Partially because I focussed on
   18310              :    optimizing what I think turns out to be a smaller problem, given
   18311              :    the known need to do the more general case *anyway*.  I document
   18312              :    the smaller problem, because it does appear to be the natural way
   18313              :    to do it.  It's trap!
   18314              : 
   18315              :    **** THE TRAP
   18316              : 
   18317              :    Let's refer to the primary template or the containing class as the
   18318              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18319              :    avoid having to say those mouthfuls all the time.)
   18320              : 
   18321              :    In either case, we have an entity and we need some way of mapping
   18322              :    that to a set of entities that need to be loaded before we can
   18323              :    proceed with whatever processing of the entity we were going to do.
   18324              : 
   18325              :    We need to link the key to the pending-entity in some way.  Given a
   18326              :    key, tell me the pending-entities I need to have loaded.  However
   18327              :    we tie the key to the pending-entity must not rely on the key being
   18328              :    loaded -- that'd defeat the lazy loading scheme.
   18329              : 
   18330              :    As the key will be an import in we know its entity number (either
   18331              :    because we imported it, or we're writing it out too).  Thus we can
   18332              :    generate a map of key-indices to pending-entities.  The
   18333              :    pending-entity indices will be into our span of the entity table,
   18334              :    and thus allow them to be lazily loaded.  The key index will be
   18335              :    into another slot of the entity table.  Notice that this checking
   18336              :    could be expensive, we don't want to iterate over a bunch of
   18337              :    pending-entity indices (across multiple imports), every time we're
   18338              :    about do to the thing with the key.  We need to quickly determine
   18339              :    'definitely nothing needed'.
   18340              : 
   18341              :    That's almost good enough, except that key indices are not unique
   18342              :    in a couple of cases :( Specifically the Global Module or a module
   18343              :    partition can result in multiple modules assigning an entity index
   18344              :    for the key.  The decl-merging on loading will detect that so we
   18345              :    only have one Key loaded, and in the entity hash it'll indicate the
   18346              :    entity index of first load.  Which might be different to how we
   18347              :    know it.  Notice this is restricted to GM entities or this-module
   18348              :    entities.  Foreign imports cannot have this.
   18349              : 
   18350              :    We can simply resolve this in the direction of how this module
   18351              :    referred to the key to how the importer knows it.  Look in the
   18352              :    entity table slot that we nominate, maybe lazy load it, and then
   18353              :    lookup the resultant entity in the entity hash to learn how the
   18354              :    importer knows it.
   18355              : 
   18356              :    But we need to go in the other direction :( Given the key, find all
   18357              :    the index-aliases of that key.  We can partially solve that by
   18358              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18359              :    augment a mapping from the entity (or its entity-index) to the
   18360              :    newly-discovered index.  Then when we look for pending entities of
   18361              :    a key, we also iterate over this aliases this mapping provides.
   18362              : 
   18363              :    But that requires the alias to be loaded.  And that's not
   18364              :    necessarily true.
   18365              : 
   18366              :    *** THE SIMPLER WAY
   18367              : 
   18368              :    The remaining fixed thing we have is the innermost namespace
   18369              :    containing the ultimate namespace-scope container of the key and
   18370              :    the name of that container (which might be the key itself).  I.e. a
   18371              :    namespace-decl/identifier/module tuple.  Let's call this the
   18372              :    top-key.  We'll discover that the module is not important here,
   18373              :    because of cross-module possibilities mentioned in case #c above.
   18374              :    We can't markup namespace-binding slots.  The best we can do is
   18375              :    mark the binding vector with 'there's something here', and have
   18376              :    another map from namespace/identifier pairs to a vector of pending
   18377              :    entity indices.
   18378              : 
   18379              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18380              :    maps to a vector of pending-entity indices.  On the binding vector
   18381              :    have flags saying whether the pending-name-entity map has contents.
   18382              :    (We might want to further extend the key to be GM-vs-Partition and
   18383              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18384              : 
   18385              :    For every key-like entity, find the outermost namespace-scope
   18386              :    name.  Use that to lookup in the pending-entity map and then make
   18387              :    sure the specified entities are loaded.
   18388              : 
   18389              :    An optimization might be to have a flag in each key-entity saying
   18390              :    that its top key might be in the entity table.  It's not clear to
   18391              :    me how to set that flag cheaply -- cheaper than just looking.
   18392              : 
   18393              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18394              :    even try this.  We can have a 'already done' flag, that we set when
   18395              :    we've done KLASS's lazy pendings.  When we import a module that
   18396              :    registers pendings on the same top-key as KLASS we need to clear
   18397              :    the flag.  A recursive walk of the top-key clearing the bit will
   18398              :    suffice.  Plus we only need to recurse on classes that have the bit
   18399              :    set.  (That means we need to set the bit on parents of KLASS here,
   18400              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18401              : 
   18402              : unsigned
   18403         2747 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18404              :                               depset::hash &table, unsigned *crc_p)
   18405              : {
   18406         3047 :   dump () && dump ("Writing pending-entities");
   18407         2747 :   dump.indent ();
   18408              : 
   18409         2747 :   trees_out sec (to, this, table);
   18410         2747 :   sec.begin ();
   18411              : 
   18412         2747 :   unsigned count = 0;
   18413         2747 :   tree cache_ns = NULL_TREE;
   18414         2747 :   tree cache_id = NULL_TREE;
   18415         2747 :   unsigned cache_section = ~0;
   18416      1378094 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18417              :     {
   18418      1375347 :       depset *d = depsets[ix];
   18419              : 
   18420      1375347 :       if (d->is_binding ())
   18421       830729 :         continue;
   18422              : 
   18423      1220585 :       if (d->is_import ())
   18424            0 :         continue;
   18425              : 
   18426      1220585 :       if (!d->is_pending_entity ())
   18427       675967 :         continue;
   18428              : 
   18429       544618 :       tree key_decl = nullptr;
   18430       544618 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18431       544618 :       tree key_name = DECL_NAME (key_decl);
   18432              : 
   18433       544618 :       if (IDENTIFIER_ANON_P (key_name))
   18434              :         {
   18435            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18436           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18437            6 :             key_name = DECL_NAME (attached);
   18438              :           else
   18439              :             {
   18440              :               /* There's nothing to attach it to.  Must
   18441              :                  always reinstantiate.  */
   18442            0 :               dump ()
   18443            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18444            0 :                          d->get_entity_kind () == depset::EK_DECL
   18445              :                          ? "Member" : "Specialization", d->get_entity (),
   18446              :                          d->cluster, d->section);
   18447            0 :               continue;
   18448              :             }
   18449              :         }
   18450              : 
   18451       544618 :       char const *also = "";
   18452       544618 :       if (d->section == cache_section
   18453       364108 :           && key_ns == cache_ns
   18454       364108 :           && key_name == cache_id)
   18455              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18456              :         also = "also ";
   18457              :       else
   18458              :         {
   18459       245033 :           cache_ns = key_ns;
   18460       245033 :           cache_id = key_name;
   18461       245033 :           cache_section = d->section;
   18462       245033 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18463       245033 :           sec.tree_node (cache_ns);
   18464       245033 :           sec.tree_node (cache_id);
   18465       245033 :           sec.u (d->cluster);
   18466       245033 :           count++;
   18467              :         }
   18468       546074 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18469          728 :                        d->get_entity_kind () == depset::EK_DECL
   18470              :                        ? "member" : "specialization", d->get_entity (),
   18471              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18472              :       }
   18473         2747 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18474         2747 :   dump.outdent ();
   18475              : 
   18476         2747 :   return count;
   18477         2747 : }
   18478              : 
   18479              : bool
   18480         1563 : module_state::read_pendings (unsigned count)
   18481              : {
   18482         1563 :   trees_in sec (this);
   18483              : 
   18484         1563 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18485              :     return false;
   18486              : 
   18487         1882 :   dump () && dump ("Reading %u pendings", count);
   18488         1563 :   dump.indent ();
   18489              : 
   18490       268739 :   for (unsigned ix = 0; ix != count; ix++)
   18491              :     {
   18492       267176 :       pending_key key;
   18493       267176 :       unsigned index;
   18494              : 
   18495       267176 :       key.ns = sec.tree_node ();
   18496       267176 :       key.id = sec.tree_node ();
   18497       267176 :       index = sec.u ();
   18498              : 
   18499       267176 :       if (!key.ns || !key.id
   18500       267176 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18501       267176 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18502       267176 :           || !identifier_p (key.id)
   18503       534352 :           || index >= entity_num)
   18504            0 :         sec.set_overrun ();
   18505              : 
   18506       267176 :       if (sec.get_overrun ())
   18507              :         break;
   18508              : 
   18509       267957 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18510              : 
   18511       267176 :       index += entity_lwm;
   18512       267176 :       auto &vec = pending_table->get_or_insert (key);
   18513       267176 :       vec.safe_push (index);
   18514              :     }
   18515              : 
   18516         1563 :   dump.outdent ();
   18517         1563 :   if (!sec.end (from ()))
   18518              :     return false;
   18519              :   return true;
   18520         1563 : }
   18521              : 
   18522              : /* Read & write locations.  */
   18523              : enum loc_kind {
   18524              :   LK_ORDINARY,
   18525              :   LK_MACRO,
   18526              :   LK_IMPORT_ORDINARY,
   18527              :   LK_IMPORT_MACRO,
   18528              :   LK_ADHOC,
   18529              :   LK_RESERVED,
   18530              : };
   18531              : 
   18532              : static const module_state *
   18533         7447 : module_for_ordinary_loc (location_t loc)
   18534              : {
   18535         7447 :   unsigned pos = 0;
   18536        14894 :   unsigned len = ool->length () - pos;
   18537              : 
   18538         7450 :   while (len)
   18539              :     {
   18540         7450 :       unsigned half = len / 2;
   18541         7450 :       module_state *probe = (*ool)[pos + half];
   18542         7450 :       if (loc < probe->ordinary_locs.first)
   18543              :         len = half;
   18544         7447 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18545              :         return probe;
   18546              :       else
   18547              :         {
   18548            0 :           pos += half + 1;
   18549            0 :           len = len - (half + 1);
   18550              :         }
   18551              :     }
   18552              : 
   18553              :   return nullptr;
   18554              : }
   18555              : 
   18556              : static const module_state *
   18557           14 : module_for_macro_loc (location_t loc)
   18558              : {
   18559           14 :   unsigned pos = 1;
   18560           14 :   unsigned len = modules->length () - pos;
   18561              : 
   18562           14 :   while (len)
   18563              :     {
   18564           14 :       unsigned half = len / 2;
   18565           14 :       module_state *probe = (*modules)[pos + half];
   18566           14 :       if (loc < probe->macro_locs.first)
   18567              :         {
   18568            0 :           pos += half + 1;
   18569            0 :           len = len - (half + 1);
   18570              :         }
   18571           14 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18572              :         len = half;
   18573              :       else
   18574              :         return probe;
   18575              :     }
   18576              : 
   18577              :   return NULL;
   18578              : }
   18579              : 
   18580              : location_t
   18581         1305 : module_state::imported_from () const
   18582              : {
   18583         1305 :   location_t from = loc;
   18584         1305 :   line_map_ordinary const *fmap
   18585         1305 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18586              : 
   18587         1305 :   if (MAP_MODULE_P (fmap))
   18588         1305 :     from = linemap_included_from (fmap);
   18589              : 
   18590         1305 :   return from;
   18591              : }
   18592              : 
   18593              : /* Note that LOC will need writing.  This allows us to prune locations
   18594              :    that are not needed.  */
   18595              : 
   18596              : bool
   18597     26530632 : module_state::note_location (location_t loc)
   18598              : {
   18599     26530632 :   bool added = false;
   18600     26530632 :   if (!macro_loc_table && !ord_loc_table)
   18601              :     ;
   18602     26530632 :   else if (loc < RESERVED_LOCATION_COUNT)
   18603              :     ;
   18604     24077019 :   else if (IS_ADHOC_LOC (loc))
   18605              :     {
   18606      2997033 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18607      2997033 :       note_location (locus);
   18608      2997033 :       source_range range = get_range_from_loc (line_table, loc);
   18609      2997033 :       if (range.m_start != locus)
   18610      2891913 :         note_location (range.m_start);
   18611      2997033 :       note_location (range.m_finish);
   18612              :     }
   18613     21079986 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18614              :     {
   18615      1511665 :       if (spans.macro (loc))
   18616              :         {
   18617      1511651 :           const line_map *map = linemap_lookup (line_table, loc);
   18618      1511651 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18619      1511651 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18620      1511651 :           macro_loc_info *slot
   18621      1511651 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18622      1511651 :           if (!slot->src)
   18623              :             {
   18624       159954 :               slot->src = mac_map;
   18625       159954 :               slot->remap = 0;
   18626              :               // Expansion locations could themselves be from a
   18627              :               // macro, we need to note them all.
   18628       159954 :               note_location (mac_map->m_expansion);
   18629       159954 :               gcc_checking_assert (mac_map->n_tokens);
   18630       159954 :               location_t tloc = UNKNOWN_LOCATION;
   18631      5991204 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18632      5831250 :                 if (mac_map->macro_locations[ix] != tloc)
   18633              :                   {
   18634      3077764 :                     tloc = mac_map->macro_locations[ix];
   18635      3077764 :                     note_location (tloc);
   18636              :                   }
   18637              :               added = true;
   18638              :             }
   18639              :         }
   18640              :     }
   18641     19568321 :   else if (IS_ORDINARY_LOC (loc))
   18642              :     {
   18643     19568321 :       if (spans.ordinary (loc))
   18644              :         {
   18645     19560866 :           const line_map *map = linemap_lookup (line_table, loc);
   18646     19560866 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18647     19560866 :           ord_loc_info lkup;
   18648     19560866 :           lkup.src = ord_map;
   18649     19560866 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18650     19560866 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18651     19560866 :           lkup.remap = 0;
   18652     19560866 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18653     19560866 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18654     19560866 :           if (!slot->src)
   18655              :             {
   18656      2257800 :               *slot = lkup;
   18657      2257800 :               added = true;
   18658              :             }
   18659              :         }
   18660              :     }
   18661              :   else
   18662            0 :     gcc_unreachable ();
   18663     26530632 :   return added;
   18664              : }
   18665              : 
   18666              : /* If we're not streaming, record that we need location LOC.
   18667              :    Otherwise stream it.  */
   18668              : 
   18669              : void
   18670     39704384 : module_state::write_location (bytes_out &sec, location_t loc)
   18671              : {
   18672     39704384 :   if (!sec.streaming_p ())
   18673              :     {
   18674     14129711 :       note_location (loc);
   18675     14129711 :       return;
   18676              :     }
   18677              : 
   18678     25574673 :   if (loc < RESERVED_LOCATION_COUNT)
   18679              :     {
   18680      2516359 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18681      2516341 :       sec.loc (LK_RESERVED + loc);
   18682              :     }
   18683     23058332 :   else if (IS_ADHOC_LOC (loc))
   18684              :     {
   18685      2923133 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18686      2923130 :       sec.u (LK_ADHOC);
   18687      2923130 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18688      2923130 :       write_location (sec, locus);
   18689      2923130 :       source_range range = get_range_from_loc (line_table, loc);
   18690      2923130 :       if (range.m_start == locus)
   18691              :         /* Compress.  */
   18692        98489 :         range.m_start = UNKNOWN_LOCATION;
   18693      2923130 :       write_location (sec, range.m_start);
   18694      2923130 :       write_location (sec, range.m_finish);
   18695      2923130 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18696      2923130 :       sec.u (discriminator);
   18697              :     }
   18698     20135202 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18699              :     {
   18700      1502977 :       const macro_loc_info *info = nullptr;
   18701      1502977 :       line_map_uint_t offset = 0;
   18702      1502977 :       if (unsigned hwm = macro_loc_remap->length ())
   18703              :         {
   18704      1502971 :           info = macro_loc_remap->begin ();
   18705     22200200 :           while (hwm != 1)
   18706              :             {
   18707     19194258 :               unsigned mid = hwm / 2;
   18708     19194258 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18709              :                 {
   18710      9708189 :                   info += mid;
   18711      9708189 :                   hwm -= mid;
   18712              :                 }
   18713              :               else
   18714              :                 hwm = mid;
   18715              :             }
   18716      1502971 :           offset = loc - MAP_START_LOCATION (info->src);
   18717      1502971 :           if (offset > info->src->n_tokens)
   18718            8 :             info = nullptr;
   18719              :         }
   18720              : 
   18721      1502977 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18722              : 
   18723      1502977 :       if (info)
   18724              :         {
   18725      1502963 :           offset += info->remap;
   18726      1502963 :           sec.u (LK_MACRO);
   18727      1502963 :           sec.loc (offset);
   18728      1502963 :           dump (dumper::LOCATION)
   18729            9 :             && dump ("Macro location %K output %K", loc, offset);
   18730              :         }
   18731           14 :       else if (const module_state *import = module_for_macro_loc (loc))
   18732              :         {
   18733           14 :           auto off = loc - import->macro_locs.first;
   18734           14 :           sec.u (LK_IMPORT_MACRO);
   18735           14 :           sec.u (import->remap);
   18736           14 :           sec.loc (off);
   18737           14 :           dump (dumper::LOCATION)
   18738            0 :             && dump ("Imported macro location %K output %u:%K",
   18739            0 :                      loc, import->remap, off);
   18740              :         }
   18741              :       else
   18742            0 :         gcc_unreachable ();
   18743              :     }
   18744     18632225 :   else if (IS_ORDINARY_LOC (loc))
   18745              :     {
   18746              :       /* If we ran out of locations for imported decls, this location could
   18747              :          be a module unit's location.  In that case, remap the location
   18748              :          to be where we imported the module from.  */
   18749     18632225 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18750              :         {
   18751     18632225 :           const line_map_ordinary *map
   18752     18632225 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18753     18632225 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18754              :             {
   18755            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18756            0 :               write_location (sec, linemap_included_from (map));
   18757            0 :               return;
   18758              :             }
   18759              :         }
   18760              : 
   18761     18632225 :       const ord_loc_info *info = nullptr;
   18762     18632225 :       line_map_uint_t offset = 0;
   18763     18632225 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18764              :         {
   18765     18632225 :           info = ord_loc_remap->begin ();
   18766    276530337 :           while (hwm != 1)
   18767              :             {
   18768    239265887 :               auto mid = hwm / 2;
   18769    239265887 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18770              :                 {
   18771    124639781 :                   info += mid;
   18772    124639781 :                   hwm -= mid;
   18773              :                 }
   18774              :               else
   18775              :                 hwm = mid;
   18776              :             }
   18777     18632225 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18778     18632225 :           if (offset > info->span)
   18779         7447 :             info = nullptr;
   18780              :         }
   18781              : 
   18782     18632225 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18783              : 
   18784     18632225 :       if (info)
   18785              :         {
   18786     18624778 :           offset += info->remap;
   18787     18624778 :           sec.u (LK_ORDINARY);
   18788     18624778 :           sec.loc (offset);
   18789              : 
   18790     18624778 :           dump (dumper::LOCATION)
   18791           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18792              :         }
   18793         7447 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18794              :         {
   18795         7447 :           auto off = loc - import->ordinary_locs.first;
   18796         7447 :           sec.u (LK_IMPORT_ORDINARY);
   18797         7447 :           sec.u (import->remap);
   18798         7447 :           sec.loc (off);
   18799         7447 :           dump (dumper::LOCATION)
   18800            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18801            0 :                      loc, import->remap, off);
   18802              :         }
   18803              :       else
   18804            0 :         gcc_unreachable ();
   18805              :     }
   18806              :   else
   18807            0 :     gcc_unreachable ();
   18808              : }
   18809              : 
   18810              : location_t
   18811     21250260 : module_state::read_location (bytes_in &sec) const
   18812              : {
   18813     21250260 :   location_t locus = UNKNOWN_LOCATION;
   18814     21250260 :   unsigned kind = sec.u ();
   18815     21250260 :   switch (kind)
   18816              :      {
   18817      1981213 :     default:
   18818      1981213 :       {
   18819      1981213 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18820      1981213 :           locus = location_t (kind - LK_RESERVED);
   18821              :         else
   18822            0 :           sec.set_overrun ();
   18823      1981213 :         dump (dumper::LOCATION)
   18824            0 :           && dump ("Reserved location %K", locus);
   18825              :       }
   18826              :       break;
   18827              : 
   18828      2271762 :      case LK_ADHOC:
   18829      2271762 :       {
   18830      2271762 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18831      2271762 :         locus = read_location (sec);
   18832      2271762 :         source_range range;
   18833      2271762 :         range.m_start = read_location (sec);
   18834      2271762 :         if (range.m_start == UNKNOWN_LOCATION)
   18835        69943 :           range.m_start = locus;
   18836      2271762 :         range.m_finish = read_location (sec);
   18837      2271762 :         unsigned discriminator = sec.u ();
   18838      2271762 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18839      2271762 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18840              :                                                           nullptr, discriminator);
   18841              :       }
   18842              :       break;
   18843              : 
   18844      1588736 :     case LK_MACRO:
   18845      1588736 :       {
   18846      1588736 :         auto off = sec.loc ();
   18847              : 
   18848      1588736 :         if (macro_locs.second)
   18849              :           {
   18850      1588736 :             if (off < macro_locs.second)
   18851      1588736 :               locus = off + macro_locs.first;
   18852              :             else
   18853            0 :               sec.set_overrun ();
   18854              :           }
   18855              :         else
   18856            0 :           locus = loc;
   18857      1588736 :         dump (dumper::LOCATION)
   18858            0 :           && dump ("Macro %K becoming %K", off, locus);
   18859              :       }
   18860              :       break;
   18861              : 
   18862     15404847 :     case LK_ORDINARY:
   18863     15404847 :       {
   18864     15404847 :         auto off = sec.loc ();
   18865     15404847 :         if (ordinary_locs.second)
   18866              :           {
   18867     15404847 :             if (off < ordinary_locs.second)
   18868     15404847 :               locus = off + ordinary_locs.first;
   18869              :             else
   18870            0 :               sec.set_overrun ();
   18871              :           }
   18872              :         else
   18873            0 :           locus = loc;
   18874              : 
   18875     15404847 :         dump (dumper::LOCATION)
   18876            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18877              :       }
   18878              :       break;
   18879              : 
   18880         3702 :      case LK_IMPORT_MACRO:
   18881         3702 :      case LK_IMPORT_ORDINARY:
   18882         3702 :        {
   18883         3702 :          unsigned mod = sec.u ();
   18884         3702 :          location_t off = sec.loc ();
   18885         3702 :          const module_state *import = NULL;
   18886              : 
   18887         3702 :          if (!mod && !slurp->remap)
   18888              :            /* This is an early read of a partition location during the
   18889              :               read of our ordinary location map.  */
   18890              :            import = this;
   18891              :          else
   18892              :            {
   18893         3702 :              mod = slurp->remap_module (mod);
   18894         3702 :              if (!mod)
   18895            0 :                sec.set_overrun ();
   18896              :              else
   18897         3702 :                import = (*modules)[mod];
   18898              :            }
   18899              : 
   18900         3702 :          if (import)
   18901              :            {
   18902         3702 :              if (kind == LK_IMPORT_MACRO)
   18903              :                {
   18904           22 :                  if (!import->macro_locs.second)
   18905            0 :                    locus = import->loc;
   18906           22 :                  else if (off < import->macro_locs.second)
   18907           22 :                    locus = off + import->macro_locs.first;
   18908              :                  else
   18909            0 :                    sec.set_overrun ();
   18910              :                }
   18911              :              else
   18912              :                {
   18913         3680 :                  if (!import->ordinary_locs.second)
   18914            0 :                    locus = import->loc;
   18915         3680 :                  else if (off < import->ordinary_locs.second)
   18916         3680 :                    locus = import->ordinary_locs.first + off;
   18917              :                  else
   18918            0 :                    sec.set_overrun ();
   18919              :                }
   18920              :            }
   18921              :        }
   18922              :        break;
   18923              :     }
   18924              : 
   18925     21250260 :   return locus;
   18926              : }
   18927              : 
   18928              : /* Allocate hash tables to record needed locations.  */
   18929              : 
   18930              : void
   18931         2776 : module_state::write_init_maps ()
   18932              : {
   18933         2776 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18934         2776 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18935         2776 : }
   18936              : 
   18937              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18938              :    this point every needed location must have been seen by
   18939              :    note_location.  */
   18940              : 
   18941              : range_t
   18942         2747 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   18943              : {
   18944         3047 :   dump () && dump ("Preparing locations");
   18945         2747 :   dump.indent ();
   18946              : 
   18947         3047 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   18948          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   18949          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   18950          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   18951          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   18952              : 
   18953         2747 :   range_t info {0, 0};
   18954              : 
   18955              :   // Sort the noted lines.
   18956         2747 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18957         2747 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18958      4490983 :        iter != end; ++iter)
   18959      2244118 :     ord_loc_remap->quick_push (*iter);
   18960         2747 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   18961              : 
   18962              :   // Note included-from maps.
   18963         2747 :   bool added = false;
   18964         2747 :   const line_map_ordinary *current = nullptr;
   18965      2252359 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18966      2246865 :        iter != end; ++iter)
   18967      2244118 :     if (iter->src != current)
   18968              :       {
   18969        33173 :         current = iter->src;
   18970        13200 :         for (auto probe = current;
   18971        33173 :              auto from = linemap_included_from (probe);
   18972        13200 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   18973              :           {
   18974        30062 :             if (has_partitions)
   18975              :               {
   18976              :                 // Partition locations need to elide their module map
   18977              :                 // entry.
   18978          220 :                 probe
   18979          220 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   18980          220 :                 if (MAP_MODULE_P (probe))
   18981          187 :                   from = linemap_included_from (probe);
   18982              :               }
   18983              : 
   18984        30062 :             if (!note_location (from))
   18985              :               break;
   18986        13200 :             added = true;
   18987        13200 :           }
   18988              :       }
   18989         2747 :   if (added)
   18990              :     {
   18991              :       // Reconstruct the line array as we added items to the hash table.
   18992          496 :       vec_free (ord_loc_remap);
   18993          496 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18994          496 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18995      4491274 :            iter != end; ++iter)
   18996      2245389 :         ord_loc_remap->quick_push (*iter);
   18997          496 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   18998              :     }
   18999         2747 :   delete ord_loc_table;
   19000         2747 :   ord_loc_table = nullptr;
   19001              : 
   19002              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19003         2747 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19004         5494 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19005         2747 :   auto dst = begin;
   19006         2747 :   line_map_uint_t offset = 0;
   19007         2747 :   unsigned range_bits = 0;
   19008         2747 :   ord_loc_info *base = nullptr;
   19009      2260065 :   for (auto iter = begin; iter != end; ++iter)
   19010              :     {
   19011      2257318 :       if (base && iter->src == base->src)
   19012              :         {
   19013      4231264 :           if (base->offset + base->span +
   19014      2228833 :               ((adjacency << base->src->m_column_and_range_bits)
   19015              :                // If there are few c&r bits, allow further separation.
   19016      2228833 :                | (adjacency << 4))
   19017      2228833 :               >= iter->offset)
   19018              :             {
   19019              :               // Merge.
   19020      2002431 :               offset -= base->span;
   19021      2002431 :               base->span = iter->offset + iter->span - base->offset;
   19022      2002431 :               offset += base->span;
   19023      2002431 :               continue;
   19024              :             }
   19025              :         }
   19026        28485 :       else if (range_bits < iter->src->m_range_bits)
   19027         2651 :         range_bits = iter->src->m_range_bits;
   19028              : 
   19029       254887 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19030       254887 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19031       254887 :       iter->remap = offset;
   19032       254887 :       offset += iter->span;
   19033       254887 :       base = dst;
   19034       254887 :       *dst++ = *iter;
   19035              :     }
   19036         2747 :   ord_loc_remap->truncate (dst - begin);
   19037              : 
   19038         2747 :   info.first = ord_loc_remap->length ();
   19039         2747 :   cfg->ordinary_locs = offset;
   19040         2747 :   cfg->loc_range_bits = range_bits;
   19041         3047 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19042              :                    info.first,
   19043              :                    cfg->ordinary_locs,
   19044              :                    cfg->loc_range_bits);
   19045              : 
   19046              :   // Remap the macro locations.
   19047         2747 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19048         2747 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19049       322655 :        iter != end; ++iter)
   19050       159954 :     macro_loc_remap->quick_push (*iter);
   19051         2747 :   delete macro_loc_table;
   19052         2747 :   macro_loc_table = nullptr;
   19053              : 
   19054         2747 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19055         2747 :   offset = 0;
   19056         8241 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19057       162701 :        iter != end; ++iter)
   19058              :     {
   19059       159954 :       auto mac = iter->src;
   19060       159954 :       iter->remap = offset;
   19061       159954 :       offset += mac->n_tokens;
   19062              :     }
   19063         2747 :   info.second = macro_loc_remap->length ();
   19064         2747 :   cfg->macro_locs = offset;
   19065              : 
   19066         3047 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19067              : 
   19068         2747 :   dump.outdent ();
   19069              : 
   19070              :   // If we have no ordinary locs, we must also have no macro locs.
   19071         2747 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19072              : 
   19073         2747 :   return info;
   19074              : }
   19075              : 
   19076              : bool
   19077         2983 : module_state::read_prepare_maps (const module_state_config *cfg)
   19078              : {
   19079         2983 :   location_t ordinary = line_table->highest_location + 1;
   19080         2983 :   ordinary += cfg->ordinary_locs;
   19081              : 
   19082         2983 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19083         2983 :   macro -= cfg->macro_locs;
   19084              : 
   19085         2983 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19086         2983 :       && macro >= LINE_MAP_MAX_LOCATION)
   19087              :     /* OK, we have enough locations.  */
   19088              :     return true;
   19089              : 
   19090            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19091            0 :   macro_locs.first = macro_locs.second = 0;
   19092              : 
   19093            0 :   spans.report_location_exhaustion (loc);
   19094              : 
   19095              :   return false;
   19096              : }
   19097              : 
   19098              : /* Write & read the location maps. Not called if there are no
   19099              :    locations.   */
   19100              : 
   19101              : void
   19102         2651 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19103              :                                    bool has_partitions, unsigned *crc_p)
   19104              : {
   19105         2929 :   dump () && dump ("Writing ordinary location maps");
   19106         2651 :   dump.indent ();
   19107              : 
   19108         2651 :   vec<const char *> filenames;
   19109         2651 :   filenames.create (20);
   19110              : 
   19111              :   /* Determine the unique filenames.  */
   19112         2651 :   const line_map_ordinary *current = nullptr;
   19113       262840 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19114       257538 :        iter != end; ++iter)
   19115       254887 :     if (iter->src != current)
   19116              :       {
   19117        28485 :         current = iter->src;
   19118        28485 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19119              : 
   19120              :         /* We should never find a module linemap in an interval.  */
   19121        28485 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19122              : 
   19123              :         /* We expect very few filenames, so just an array.
   19124              :            (Not true when headers are still in play :()  */
   19125      2096981 :         for (unsigned jx = filenames.length (); jx--;)
   19126              :           {
   19127      2053466 :             const char *name = filenames[jx];
   19128      2053466 :             if (0 == strcmp (name, fname))
   19129              :               {
   19130              :                 /* Reset the linemap's name, because for things like
   19131              :                    preprocessed input we could have multiple instances
   19132              :                    of the same name, and we'd rather not percolate
   19133              :                    that.  */
   19134        13455 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19135        13455 :                 fname = NULL;
   19136        13455 :                 break;
   19137              :               }
   19138              :           }
   19139        28485 :         if (fname)
   19140        15030 :           filenames.safe_push (fname);
   19141              :       }
   19142              : 
   19143         2651 :   bytes_out sec (to);
   19144         2651 :   sec.begin ();
   19145              : 
   19146              :   /* Write the filenames.  */
   19147         2651 :   unsigned len = filenames.length ();
   19148         2651 :   sec.u (len);
   19149         2929 :   dump () && dump ("%u source file names", len);
   19150        17681 :   for (unsigned ix = 0; ix != len; ix++)
   19151              :     {
   19152        15030 :       const char *fname = filenames[ix];
   19153        15045 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19154        15030 :       sec.str (fname);
   19155              :     }
   19156              : 
   19157         2651 :   sec.loc (info.first); /* Num maps.  */
   19158         2651 :   const ord_loc_info *base = nullptr;
   19159       262840 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19160       257538 :        iter != end; ++iter)
   19161              :     {
   19162       254887 :       dump (dumper::LOCATION)
   19163           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19164           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19165           18 :                  MAP_START_LOCATION (iter->src),
   19166              :                  iter->offset, iter->span, iter->remap,
   19167              :                  iter->span);
   19168              : 
   19169       254887 :       if (!base || iter->src != base->src)
   19170        28485 :         base = iter;
   19171       254887 :       sec.loc (iter->offset - base->offset);
   19172       254887 :       if (base == iter)
   19173              :         {
   19174        28485 :           sec.u (iter->src->sysp);
   19175        28485 :           sec.u (iter->src->m_range_bits);
   19176        28485 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19177              : 
   19178        28485 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19179      6402774 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19180      3201387 :             if (filenames[ix] == fname)
   19181              :               {
   19182        28485 :                 sec.u (ix);
   19183        28485 :                 break;
   19184              :               }
   19185        28485 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19186        28485 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19187        28485 :           sec.u (line);
   19188              :         }
   19189       254887 :       sec.loc (iter->remap);
   19190       254887 :       if (base == iter)
   19191              :         {
   19192              :           /* Write the included from location, which means reading it
   19193              :              while reading in the ordinary maps.  So we'd better not
   19194              :              be getting ahead of ourselves.  */
   19195        28485 :           location_t from = linemap_included_from (iter->src);
   19196        28485 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19197        28485 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19198              :             {
   19199              :               /* A partition's span will have a from pointing at a
   19200              :                  MODULE_INC.  Find that map's from.  */
   19201          214 :               line_map_ordinary const *fmap
   19202          214 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19203          214 :               if (MAP_MODULE_P (fmap))
   19204          181 :                 from = linemap_included_from (fmap);
   19205              :             }
   19206        28485 :           write_location (sec, from);
   19207              :         }
   19208              :     }
   19209              : 
   19210         2651 :   filenames.release ();
   19211              : 
   19212         2651 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19213         2651 :   dump.outdent ();
   19214         2651 : }
   19215              : 
   19216              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19217              : 
   19218              : static const char *
   19219          999 : dk_string (enum diagnostics::kind dk)
   19220              : {
   19221          999 :   gcc_assert (dk > diagnostics::kind::unspecified
   19222              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19223          999 :   if (dk == diagnostics::kind::ignored)
   19224              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19225              :     return "ignored: ";
   19226              :   else
   19227            0 :     return diagnostics::get_text_for_kind (dk);
   19228              : }
   19229              : 
   19230              : /* Dump one #pragma GCC diagnostic entry.  */
   19231              : 
   19232              : static bool
   19233         2035 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19234              : {
   19235         2035 :   if (dk == diagnostics::kind::pop)
   19236         1036 :     return dump (" Index %u: pop from %d", index, opt);
   19237              :   else
   19238          999 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19239         1998 :                  cl_options[opt].opt_text);
   19240              : }
   19241              : 
   19242              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19243              : 
   19244              : void
   19245         5398 : module_state::write_diagnostic_classification (elf_out *to,
   19246              :                                                diagnostics::context *dc,
   19247              :                                                unsigned *crc_p)
   19248              : {
   19249         5398 :   auto &changes = dc->get_classification_history ();
   19250              : 
   19251         5398 :   bytes_out sec (to);
   19252         5398 :   if (sec.streaming_p ())
   19253              :     {
   19254         2651 :       sec.begin ();
   19255         2929 :       dump () && dump ("Writing diagnostic change locations");
   19256         2651 :       dump.indent ();
   19257              :     }
   19258              : 
   19259         5398 :   unsigned len = changes.length ();
   19260              : 
   19261              :   /* We don't want to write out any entries that came from one of our imports.
   19262              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19263              :      targets to match the index in our actual output.  So remember how many
   19264              :      lines we had skipped at each step, where -1 means this line itself
   19265              :      is skipped.  */
   19266         5398 :   int skips = 0;
   19267         5398 :   auto_vec<int> skips_at (len);
   19268         5398 :   skips_at.safe_grow (len);
   19269              : 
   19270        65740 :   for (unsigned i = 0; i < len; ++i)
   19271              :     {
   19272        60342 :       const auto &c = changes[i];
   19273        60342 :       skips_at[i] = skips;
   19274        60342 :       if (linemap_location_from_module_p (line_table, c.location))
   19275              :         {
   19276        13576 :           ++skips;
   19277        13576 :           skips_at[i] = -1;
   19278        13576 :           continue;
   19279              :         }
   19280              :     }
   19281              : 
   19282         5398 :   if (sec.streaming_p ())
   19283              :     {
   19284         2651 :       sec.u (len - skips);
   19285         2929 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19286              :     }
   19287              : 
   19288        65740 :   for (unsigned i = 0; i < len; ++i)
   19289              :     {
   19290        60342 :       if (skips_at[i] == -1)
   19291        13576 :         continue;
   19292              : 
   19293        46766 :       const auto &c = changes[i];
   19294        46766 :       write_location (sec, c.location);
   19295        46766 :       if (sec.streaming_p ())
   19296              :         {
   19297        23383 :           unsigned opt = c.option;
   19298        23383 :           if (c.kind == diagnostics::kind::pop)
   19299        11987 :             opt -= skips_at[opt];
   19300        23383 :           sec.u (opt);
   19301        23383 :           sec.u (static_cast<unsigned> (c.kind));
   19302        62310 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19303              :         }
   19304              :     }
   19305              : 
   19306         5398 :   if (sec.streaming_p ())
   19307              :     {
   19308         2651 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19309         2651 :       dump.outdent ();
   19310              :     }
   19311         5398 : }
   19312              : 
   19313              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19314              : 
   19315              : bool
   19316         2925 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19317              : {
   19318         2925 :   bytes_in sec;
   19319              : 
   19320         2925 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19321              :     return false;
   19322              : 
   19323         3442 :   dump () && dump ("Reading diagnostic change locations");
   19324         2925 :   dump.indent ();
   19325              : 
   19326         2925 :   unsigned len = sec.u ();
   19327         3442 :   dump () && dump ("Diagnostic changes: %u", len);
   19328              : 
   19329         2925 :   auto &changes = dc->get_classification_history ();
   19330         2925 :   int offset = changes.length ();
   19331         2925 :   changes.reserve (len + 1);
   19332        26845 :   for (unsigned i = 0; i < len; ++i)
   19333              :     {
   19334        23920 :       location_t loc = read_location (sec);
   19335        23920 :       int opt = sec.u ();
   19336        23920 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19337        23920 :       if (kind == diagnostics::kind::pop)
   19338              :         /* For a pop, opt is the 'changes' index to return to.  */
   19339        12274 :         opt += offset;
   19340        23920 :       changes.quick_push ({ loc, opt, kind });
   19341        23987 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19342              :     }
   19343              : 
   19344              :   /* Did the import pop all its diagnostic changes?  */
   19345         2925 :   bool last_was_reset = (len == 0);
   19346         2925 :   if (len)
   19347          228 :     for (int i = changes.length () - 1; ; --i)
   19348              :       {
   19349        10543 :         gcc_checking_assert (i >= offset);
   19350              : 
   19351        10543 :         const auto &c = changes[i];
   19352        10543 :         if (c.kind != diagnostics::kind::pop)
   19353              :           break;
   19354        10534 :         else if (c.option == offset)
   19355              :           {
   19356              :             last_was_reset = true;
   19357              :             break;
   19358              :           }
   19359              :         else
   19360              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19361              :              i so we actually jump to c.option - 1.  */
   19362        10429 :           i = c.option;
   19363        10429 :       }
   19364         2925 :   if (!last_was_reset)
   19365              :     {
   19366              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19367              :          imports.  */
   19368            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19369            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19370           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19371              :     }
   19372              : 
   19373         2925 :   dump.outdent ();
   19374         2925 :   if (!sec.end (from ()))
   19375              :     return false;
   19376              : 
   19377              :   return true;
   19378         2925 : }
   19379              : 
   19380              : void
   19381          123 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19382              : {
   19383          135 :   dump () && dump ("Writing macro location maps");
   19384          123 :   dump.indent ();
   19385              : 
   19386          123 :   bytes_out sec (to);
   19387          123 :   sec.begin ();
   19388              : 
   19389          135 :   dump () && dump ("Macro maps:%K", info.second);
   19390          123 :   sec.loc (info.second);
   19391              : 
   19392          123 :   line_map_uint_t macro_num = 0;
   19393          246 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19394       160077 :        iter-- != begin;)
   19395              :     {
   19396       159954 :       auto mac = iter->src;
   19397       159954 :       sec.loc (iter->remap);
   19398       159954 :       sec.u (mac->n_tokens);
   19399       159954 :       sec.cpp_node (mac->macro);
   19400       159954 :       write_location (sec, mac->m_expansion);
   19401       159954 :       const location_t *locs = mac->macro_locations;
   19402              :       /* There are lots of identical runs.  */
   19403       159954 :       location_t prev = UNKNOWN_LOCATION;
   19404       159954 :       unsigned count = 0;
   19405       159954 :       unsigned runs = 0;
   19406      5991204 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19407              :         {
   19408      5831250 :           location_t tok_loc = locs[jx];
   19409      5831250 :           if (tok_loc == prev)
   19410              :             {
   19411      2753486 :               count++;
   19412      2753486 :               continue;
   19413              :             }
   19414      3077764 :           runs++;
   19415      3077764 :           sec.u (count);
   19416      3077764 :           count = 1;
   19417      3077764 :           prev = tok_loc;
   19418      3077764 :           write_location (sec, tok_loc);
   19419              :         }
   19420       159954 :       sec.u (count);
   19421       159954 :       dump (dumper::LOCATION)
   19422            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19423            9 :                  macro_num, identifier (mac->macro),
   19424              :                  runs, mac->n_tokens,
   19425              :                  MAP_START_LOCATION (mac),
   19426            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19427              :                  iter->remap);
   19428       159954 :       macro_num++;
   19429              :     }
   19430          123 :   gcc_assert (macro_num == info.second);
   19431              : 
   19432          123 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19433          123 :   dump.outdent ();
   19434          123 : }
   19435              : 
   19436              : bool
   19437         2925 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19438              :                                   unsigned range_bits)
   19439              : {
   19440         2925 :   bytes_in sec;
   19441              : 
   19442         2925 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19443              :     return false;
   19444         3442 :   dump () && dump ("Reading ordinary location maps");
   19445         2925 :   dump.indent ();
   19446              : 
   19447              :   /* Read the filename table.  */
   19448         2925 :   unsigned len = sec.u ();
   19449         3442 :   dump () && dump ("%u source file names", len);
   19450         2925 :   vec<const char *> filenames;
   19451         2925 :   filenames.create (len);
   19452        19574 :   for (unsigned ix = 0; ix != len; ix++)
   19453              :     {
   19454        16649 :       size_t l;
   19455        16649 :       const char *buf = sec.str (&l);
   19456        16649 :       char *fname = XNEWVEC (char, l + 1);
   19457        16649 :       memcpy (fname, buf, l + 1);
   19458        16649 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19459              :       /* We leak these names into the line-map table.  But it
   19460              :          doesn't own them.  */
   19461        16649 :       filenames.quick_push (fname);
   19462              :     }
   19463              : 
   19464         2925 :   line_map_uint_t num_ordinary = sec.loc ();
   19465         3442 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19466              :                    num_ordinary, range_bits);
   19467              : 
   19468         2925 :   location_t offset = line_table->highest_location + 1;
   19469         2925 :   offset += ((loc_one << range_bits) - 1);
   19470         2925 :   offset &= ~((loc_one << range_bits) - 1);
   19471         2925 :   ordinary_locs.first = offset;
   19472              : 
   19473         2925 :   bool propagated = spans.maybe_propagate (this, offset);
   19474         2925 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19475         2925 :     (line_map_new_raw (line_table, false, num_ordinary));
   19476              : 
   19477         2925 :   const line_map_ordinary *base = nullptr;
   19478       296640 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19479              :     {
   19480       293715 :       line_map_ordinary *map = &maps[ix];
   19481              : 
   19482       293715 :       location_t offset = sec.loc ();
   19483       293715 :       if (!offset)
   19484              :         {
   19485        32447 :           map->reason = LC_RENAME;
   19486        32447 :           map->sysp = sec.u ();
   19487        32447 :           map->m_range_bits = sec.u ();
   19488        32447 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19489        32447 :           unsigned fnum = sec.u ();
   19490        64894 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19491        32447 :           map->to_line = sec.u ();
   19492        32447 :           base = map;
   19493              :         }
   19494              :       else
   19495              :         {
   19496       261268 :           *map = *base;
   19497       261268 :           map->to_line += offset >> map->m_column_and_range_bits;
   19498              :         }
   19499       293715 :       location_t remap = sec.loc ();
   19500       293715 :       map->start_location = remap + ordinary_locs.first;
   19501       293715 :       if (base == map)
   19502              :         {
   19503              :           /* Root the outermost map at our location.  */
   19504        32447 :           ordinary_locs.second = remap;
   19505        32447 :           location_t from = read_location (sec);
   19506        32447 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19507              :         }
   19508              :     }
   19509              : 
   19510         2925 :   ordinary_locs.second = num_ord_locs;
   19511              :   /* highest_location is the one handed out, not the next one to
   19512              :      hand out.  */
   19513         2925 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19514              : 
   19515         2925 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19516              :     /* We shouldn't run out of locations, as we checked before
   19517              :        starting.  */
   19518            0 :     sec.set_overrun ();
   19519         3442 :   dump () && dump ("Ordinary location [%K,+%K)",
   19520              :                    ordinary_locs.first, ordinary_locs.second);
   19521              : 
   19522         2925 :   if (propagated)
   19523          169 :     spans.close ();
   19524              : 
   19525         2925 :   filenames.release ();
   19526              : 
   19527         2925 :   dump.outdent ();
   19528         2925 :   if (!sec.end (from ()))
   19529              :     return false;
   19530              : 
   19531              :   return true;
   19532         2925 : }
   19533              : 
   19534              : bool
   19535          131 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19536              : {
   19537          131 :   bytes_in sec;
   19538              : 
   19539          131 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19540              :     return false;
   19541          140 :   dump () && dump ("Reading macro location maps");
   19542          131 :   dump.indent ();
   19543              : 
   19544          131 :   line_map_uint_t num_macros = sec.loc ();
   19545          140 :   dump () && dump ("Macro maps:%K locs:%K",
   19546              :                    num_macros, num_macro_locs);
   19547              : 
   19548          262 :   bool propagated = spans.maybe_propagate (this,
   19549          131 :                                            line_table->highest_location + 1);
   19550              : 
   19551          131 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19552          131 :   macro_locs.second = num_macro_locs;
   19553          131 :   macro_locs.first = offset - num_macro_locs;
   19554              : 
   19555          140 :   dump () && dump ("Macro loc delta %K", offset);
   19556          140 :   dump () && dump ("Macro locations [%K,%K)",
   19557              :                    macro_locs.first, macro_locs.second);
   19558              : 
   19559       202369 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19560              :     {
   19561       202238 :       location_t offset = sec.loc ();
   19562       202238 :       unsigned n_tokens = sec.u ();
   19563       202238 :       cpp_hashnode *node = sec.cpp_node ();
   19564       202238 :       location_t exp_loc = read_location (sec);
   19565              : 
   19566       202238 :       const line_map_macro *macro
   19567       202238 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19568       202238 :       if (!macro)
   19569              :         /* We shouldn't run out of locations, as we checked that we
   19570              :            had enough before starting.  */
   19571              :         break;
   19572       202238 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19573              :                            == offset + macro_locs.first);
   19574              : 
   19575       202238 :       location_t *locs = macro->macro_locations;
   19576       202238 :       location_t tok_loc = UNKNOWN_LOCATION;
   19577       202238 :       unsigned count = sec.u ();
   19578       202238 :       unsigned runs = 0;
   19579      7432898 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19580              :         {
   19581     11057783 :           while (!count-- && !sec.get_overrun ())
   19582              :             {
   19583      3827123 :               runs++;
   19584      3827123 :               tok_loc = read_location (sec);
   19585      3827123 :               count = sec.u ();
   19586              :             }
   19587      7230660 :           locs[jx] = tok_loc;
   19588              :         }
   19589       202238 :       if (count)
   19590            0 :         sec.set_overrun ();
   19591       202268 :       dump (dumper::LOCATION)
   19592            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19593              :                  ix, identifier (node), runs, n_tokens,
   19594              :                  MAP_START_LOCATION (macro),
   19595            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19596              :     }
   19597              : 
   19598          140 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19599          131 :   if (propagated)
   19600            3 :     spans.close ();
   19601              : 
   19602          131 :   dump.outdent ();
   19603          131 :   if (!sec.end (from ()))
   19604              :     return false;
   19605              : 
   19606              :   return true;
   19607          131 : }
   19608              : 
   19609              : /* Serialize the definition of MACRO.  */
   19610              : 
   19611              : void
   19612        75754 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19613              : {
   19614        75754 :   sec.u (macro->count);
   19615              : 
   19616        75754 :   bytes_out::bits_out bits = sec.stream_bits ();
   19617        75754 :   bits.b (macro->fun_like);
   19618        75754 :   bits.b (macro->variadic);
   19619        75754 :   bits.b (macro->syshdr);
   19620        75754 :   bits.bflush ();
   19621              : 
   19622        75754 :   write_location (sec, macro->line);
   19623        75754 :   if (macro->fun_like)
   19624              :     {
   19625         9596 :       sec.u (macro->paramc);
   19626         9596 :       const cpp_hashnode *const *parms = macro->parm.params;
   19627        24161 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19628        14565 :         sec.cpp_node (parms[ix]);
   19629              :     }
   19630              : 
   19631              :   unsigned len = 0;
   19632       243396 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19633              :     {
   19634       167642 :       const cpp_token *token = &macro->exp.tokens[ix];
   19635       167642 :       write_location (sec, token->src_loc);
   19636       167642 :       sec.u (token->type);
   19637       167642 :       sec.u (token->flags);
   19638       167642 :       switch (cpp_token_val_index (token))
   19639              :         {
   19640            0 :         default:
   19641            0 :           gcc_unreachable ();
   19642              : 
   19643        12815 :         case CPP_TOKEN_FLD_ARG_NO:
   19644              :           /* An argument reference.  */
   19645        12815 :           sec.u (token->val.macro_arg.arg_no);
   19646        12815 :           sec.cpp_node (token->val.macro_arg.spelling);
   19647        12815 :           break;
   19648              : 
   19649        32895 :         case CPP_TOKEN_FLD_NODE:
   19650              :           /* An identifier.  */
   19651        32895 :           sec.cpp_node (token->val.node.node);
   19652        32895 :           if (token->val.node.spelling == token->val.node.node)
   19653              :             /* The spelling will usually be the same.  so optimize
   19654              :                that.  */
   19655        32895 :             sec.str (NULL, 0);
   19656              :           else
   19657            0 :             sec.cpp_node (token->val.node.spelling);
   19658              :           break;
   19659              : 
   19660              :         case CPP_TOKEN_FLD_NONE:
   19661              :           break;
   19662              : 
   19663        53844 :         case CPP_TOKEN_FLD_STR:
   19664              :           /* A string, number or comment.  Not always NUL terminated,
   19665              :              we stream out in a single contatenation with embedded
   19666              :              NULs as that's a safe default.  */
   19667        53844 :           len += token->val.str.len + 1;
   19668        53844 :           sec.u (token->val.str.len);
   19669        53844 :           break;
   19670              : 
   19671            0 :         case CPP_TOKEN_FLD_SOURCE:
   19672            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19673            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19674              :           /* These do not occur inside a macro itself.  */
   19675            0 :           gcc_unreachable ();
   19676              :         }
   19677              :     }
   19678              : 
   19679        75754 :   if (len)
   19680              :     {
   19681        50299 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19682        50299 :       len = 0;
   19683       150643 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19684              :         {
   19685       100344 :           const cpp_token *token = &macro->exp.tokens[ix];
   19686       100344 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19687              :             {
   19688        53844 :               memcpy (ptr + len, token->val.str.text,
   19689        53844 :                       token->val.str.len);
   19690        53844 :               len += token->val.str.len;
   19691        53844 :               ptr[len++] = 0;
   19692              :             }
   19693              :         }
   19694              :     }
   19695        75754 : }
   19696              : 
   19697              : /* Read a macro definition.  */
   19698              : 
   19699              : cpp_macro *
   19700          774 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19701              : {
   19702          774 :   unsigned count = sec.u ();
   19703              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19704              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19705              :      a wrapper for ggc_alloc_atomic.  */
   19706          774 :   cpp_macro *macro
   19707         1548 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19708          774 :                                      + sizeof (cpp_token) * (count - !!count));
   19709          774 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19710              : 
   19711          774 :   macro->count = count;
   19712          774 :   macro->kind = cmk_macro;
   19713          774 :   macro->imported_p = true;
   19714              : 
   19715          774 :   bytes_in::bits_in bits = sec.stream_bits ();
   19716          774 :   macro->fun_like = bits.b ();
   19717          774 :   macro->variadic = bits.b ();
   19718          774 :   macro->syshdr = bits.b ();
   19719          774 :   bits.bflush ();
   19720              : 
   19721          774 :   macro->line = read_location (sec);
   19722              : 
   19723          774 :   if (macro->fun_like)
   19724              :     {
   19725           83 :       unsigned paramc = sec.u ();
   19726           83 :       cpp_hashnode **params
   19727           83 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19728           83 :       macro->paramc = paramc;
   19729           83 :       macro->parm.params = params;
   19730          177 :       for (unsigned ix = 0; ix != paramc; ix++)
   19731           94 :         params[ix] = sec.cpp_node ();
   19732              :     }
   19733              : 
   19734              :   unsigned len = 0;
   19735         2160 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19736              :     {
   19737         1386 :       cpp_token *token = &macro->exp.tokens[ix];
   19738         1386 :       token->src_loc = read_location (sec);
   19739         1386 :       token->type = cpp_ttype (sec.u ());
   19740         1386 :       token->flags = sec.u ();
   19741         1386 :       switch (cpp_token_val_index (token))
   19742              :         {
   19743            0 :         default:
   19744            0 :           sec.set_overrun ();
   19745            0 :           break;
   19746              : 
   19747           77 :         case CPP_TOKEN_FLD_ARG_NO:
   19748              :           /* An argument reference.  */
   19749           77 :           {
   19750           77 :             unsigned arg_no = sec.u ();
   19751           77 :             if (arg_no - 1 >= macro->paramc)
   19752            0 :               sec.set_overrun ();
   19753           77 :             token->val.macro_arg.arg_no = arg_no;
   19754           77 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19755              :           }
   19756           77 :           break;
   19757              : 
   19758          271 :         case CPP_TOKEN_FLD_NODE:
   19759              :           /* An identifier.  */
   19760          271 :           token->val.node.node = sec.cpp_node ();
   19761          271 :           token->val.node.spelling = sec.cpp_node ();
   19762          271 :           if (!token->val.node.spelling)
   19763          271 :             token->val.node.spelling = token->val.node.node;
   19764              :           break;
   19765              : 
   19766              :         case CPP_TOKEN_FLD_NONE:
   19767              :           break;
   19768              : 
   19769          597 :         case CPP_TOKEN_FLD_STR:
   19770              :           /* A string, number or comment.  */
   19771          597 :           token->val.str.len = sec.u ();
   19772          597 :           len += token->val.str.len + 1;
   19773          597 :           break;
   19774              :         }
   19775              :     }
   19776              : 
   19777          774 :   if (len)
   19778          594 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19779              :       {
   19780              :         /* There should be a final NUL.  */
   19781          594 :         if (ptr[len-1])
   19782            0 :           sec.set_overrun ();
   19783              :         /* cpp_alloc_token_string will add a final NUL.  */
   19784          594 :         const unsigned char *buf
   19785          594 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19786          594 :         len = 0;
   19787         1511 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19788              :           {
   19789          917 :             cpp_token *token = &macro->exp.tokens[ix];
   19790          917 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19791              :               {
   19792          597 :                 token->val.str.text = buf + len;
   19793          597 :                 len += token->val.str.len;
   19794          597 :                 if (buf[len++])
   19795            0 :                   sec.set_overrun ();
   19796              :               }
   19797              :           }
   19798              :       }
   19799              : 
   19800          774 :   if (sec.get_overrun ())
   19801            0 :     return NULL;
   19802              :   return macro;
   19803          774 : }
   19804              : 
   19805              : /* Exported macro data.  */
   19806              : struct GTY(()) macro_export {
   19807              :   cpp_macro *def;
   19808              :   location_t undef_loc;
   19809              : 
   19810       107230 :   macro_export ()
   19811       107230 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19812              :   {
   19813              :   }
   19814              : };
   19815              : 
   19816              : /* Imported macro data.  */
   19817              : class macro_import {
   19818              : public:
   19819              :   struct slot {
   19820              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19821              :     int offset;
   19822              : #endif
   19823              :     /* We need to ensure we don't use the LSB for representation, as
   19824              :        that's the union discriminator below.  */
   19825              :     unsigned bits;
   19826              : 
   19827              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19828              :     int offset;
   19829              : #endif
   19830              : 
   19831              :   public:
   19832              :     enum Layout {
   19833              :       L_DEF = 1,
   19834              :       L_UNDEF = 2,
   19835              :       L_BOTH = 3,
   19836              :       L_MODULE_SHIFT = 2
   19837              :     };
   19838              : 
   19839              :   public:
   19840              :     /* Not a regular ctor, because we put it in a union, and that's
   19841              :        not allowed in C++ 98.  */
   19842       191122 :     static slot ctor (unsigned module, unsigned defness)
   19843              :     {
   19844       191122 :       gcc_checking_assert (defness);
   19845       191122 :       slot s;
   19846       191122 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19847       191122 :       s.offset = -1;
   19848       191122 :       return s;
   19849              :     }
   19850              : 
   19851              :   public:
   19852       154422 :     unsigned get_defness () const
   19853              :     {
   19854       154422 :       return bits & L_BOTH;
   19855              :     }
   19856       109957 :     unsigned get_module () const
   19857              :     {
   19858       109957 :       return bits >> L_MODULE_SHIFT;
   19859              :     }
   19860           12 :     void become_undef ()
   19861              :     {
   19862           12 :       bits &= ~unsigned (L_DEF);
   19863           12 :       bits |= unsigned (L_UNDEF);
   19864              :     }
   19865              :   };
   19866              : 
   19867              : private:
   19868              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19869              :   union either {
   19870              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19871              :        there will be exactly one slot per macro, hence the effort of
   19872              :        packing that.  */
   19873              :     ary_t *ary;
   19874              :     slot single;
   19875              :   } u;
   19876              : 
   19877              : public:
   19878       155452 :   macro_import ()
   19879       155452 :   {
   19880       155452 :     u.ary = NULL;
   19881              :   }
   19882              : 
   19883              : private:
   19884      8357775 :   bool single_p () const
   19885              :   {
   19886      8357775 :     return u.single.bits & slot::L_BOTH;
   19887              :   }
   19888      8513346 :   bool occupied_p () const
   19889              :   {
   19890      8513346 :     return u.ary != NULL;
   19891              :   }
   19892              : 
   19893              : public:
   19894         2280 :   unsigned length () const
   19895              :   {
   19896         2280 :     gcc_checking_assert (occupied_p ());
   19897         2280 :     return single_p () ? 1 : u.ary->length ();
   19898              :   }
   19899      8213375 :   slot &operator[] (unsigned ix)
   19900              :   {
   19901      8213375 :     gcc_checking_assert (occupied_p ());
   19902      8213375 :     if (single_p ())
   19903              :       {
   19904      8109344 :         gcc_checking_assert (!ix);
   19905      8109344 :         return u.single;
   19906              :       }
   19907              :     else
   19908       104031 :       return (*u.ary)[ix];
   19909              :   }
   19910              : 
   19911              : public:
   19912              :   slot &exported ();
   19913              :   slot &append (unsigned module, unsigned defness);
   19914              : };
   19915              : 
   19916              : /* O is a new import to append to the list for.  If we're an empty
   19917              :    set, initialize us.  */
   19918              : 
   19919              : macro_import::slot &
   19920       191122 : macro_import::append (unsigned module, unsigned defness)
   19921              : {
   19922       191122 :   if (!occupied_p ())
   19923              :     {
   19924       155452 :       u.single = slot::ctor (module, defness);
   19925       155452 :       return u.single;
   19926              :     }
   19927              :   else
   19928              :     {
   19929        35670 :       bool single = single_p ();
   19930        35670 :       ary_t *m = single ? NULL : u.ary;
   19931        35670 :       vec_safe_reserve (m, 1 + single);
   19932        35670 :       if (single)
   19933        35667 :         m->quick_push (u.single);
   19934        35670 :       u.ary = m;
   19935        35670 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19936              :     }
   19937              : }
   19938              : 
   19939              : /* We're going to export something.  Make sure the first import slot
   19940              :    is us.  */
   19941              : 
   19942              : macro_import::slot &
   19943       106569 : macro_import::exported ()
   19944              : {
   19945       106569 :   if (occupied_p () && !(*this)[0].get_module ())
   19946              :     {
   19947          119 :       slot &res = (*this)[0];
   19948          119 :       res.bits |= slot::L_DEF;
   19949          119 :       return res;
   19950              :     }
   19951              : 
   19952       106450 :   slot *a = &append (0, slot::L_DEF);
   19953       106450 :   if (!single_p ())
   19954              :     {
   19955        31387 :       slot &f = (*this)[0];
   19956        31387 :       std::swap (f, *a);
   19957        31387 :       a = &f;
   19958              :     }
   19959              :   return *a;
   19960              : }
   19961              : 
   19962              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   19963              :    indexes this array (offset by 1, so zero means 'not present'.  */
   19964              : 
   19965              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   19966              : 
   19967              : /* The exported macros.  A macro_import slot's zeroth element's offset
   19968              :    indexes this array.  If the zeroth slot is not for module zero,
   19969              :    there is no export.  */
   19970              : 
   19971              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   19972              : 
   19973              : /* The reachable set of header imports from this TU.  */
   19974              : 
   19975              : static GTY(()) bitmap headers;
   19976              : 
   19977              : /* Get the (possibly empty) macro imports for NODE.  */
   19978              : 
   19979              : static macro_import &
   19980       159854 : get_macro_imports (cpp_hashnode *node)
   19981              : {
   19982       159854 :   if (node->deferred)
   19983         4402 :     return (*macro_imports)[node->deferred - 1];
   19984              : 
   19985       155452 :   vec_safe_reserve (macro_imports, 1);
   19986       155452 :   node->deferred = macro_imports->length () + 1;
   19987       155452 :   return *vec_safe_push (macro_imports, macro_import ());
   19988              : }
   19989              : 
   19990              : /* Get the macro export for export EXP of NODE.  */
   19991              : 
   19992              : static macro_export &
   19993       106569 : get_macro_export (macro_import::slot &slot)
   19994              : {
   19995       106569 :   if (slot.offset >= 0)
   19996          119 :     return (*macro_exports)[slot.offset];
   19997              : 
   19998       106450 :   vec_safe_reserve (macro_exports, 1);
   19999       106450 :   slot.offset = macro_exports->length ();
   20000       106450 :   return *macro_exports->quick_push (macro_export ());
   20001              : }
   20002              : 
   20003              : /* If NODE is an exportable macro, add it to the export set.  */
   20004              : 
   20005              : static int
   20006      3939241 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20007              : {
   20008      3939241 :   bool exporting = false;
   20009              : 
   20010      3939241 :   if (cpp_user_macro_p (node))
   20011       506412 :     if (cpp_macro *macro = node->value.macro)
   20012              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20013       505955 :       if (!macro->imported_p
   20014       505955 :           && !macro->lazy && macro->line >= spans.main_start ())
   20015              :         {
   20016        75182 :           gcc_checking_assert (macro->kind == cmk_macro);
   20017              :           /* I don't want to deal with this corner case, that I suspect is
   20018              :              a devil's advocate reading of the standard.  */
   20019        75182 :           gcc_checking_assert (!macro->extra_tokens);
   20020              : 
   20021        75182 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20022        75182 :           macro_export &exp = get_macro_export (slot);
   20023        75182 :           exp.def = macro;
   20024        75182 :           exporting = true;
   20025              :         }
   20026              : 
   20027      3864059 :   if (!exporting && node->deferred)
   20028              :     {
   20029          610 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20030          610 :       macro_import::slot &slot = imports[0];
   20031          610 :       if (!slot.get_module ())
   20032              :         {
   20033          579 :           gcc_checking_assert (slot.get_defness ());
   20034              :           exporting = true;
   20035              :         }
   20036              :     }
   20037              : 
   20038        75182 :   if (exporting)
   20039        75761 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20040              : 
   20041      3939241 :   return 1; /* Don't stop.  */
   20042              : }
   20043              : 
   20044              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20045              : 
   20046              : static int
   20047      3959880 : macro_loc_cmp (const void *a_, const void *b_)
   20048              : {
   20049      3959880 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20050      3959880 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20051      3959880 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20052      3959880 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20053              : 
   20054      3959880 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20055      3959880 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20056      3959880 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20057      3959880 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20058              : 
   20059      3959880 :   if (loc_a < loc_b)
   20060              :     return +1;
   20061      2034328 :   else if (loc_a > loc_b)
   20062              :     return -1;
   20063              :   else
   20064            0 :     return 0;
   20065              : }
   20066              : 
   20067              : /* Gather the macro definitions and undefinitions that we will need to
   20068              :    write out.   */
   20069              : 
   20070              : vec<cpp_hashnode *> *
   20071          899 : module_state::prepare_macros (cpp_reader *reader)
   20072              : {
   20073          899 :   vec<cpp_hashnode *> *macros;
   20074          899 :   vec_alloc (macros, 100);
   20075              : 
   20076          899 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20077              : 
   20078          923 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20079              : 
   20080          899 :   macros->qsort (macro_loc_cmp);
   20081              : 
   20082              :   // Note the locations.
   20083        77559 :   for (unsigned ix = macros->length (); ix--;)
   20084              :     {
   20085        75761 :       cpp_hashnode *node = (*macros)[ix];
   20086        75761 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20087        75761 :       macro_export &mac = (*macro_exports)[slot.offset];
   20088              : 
   20089        75761 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20090            1 :         continue;
   20091              : 
   20092        75760 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20093           12 :         note_location (mac.undef_loc);
   20094        75760 :       if (mac.def)
   20095              :         {
   20096        75754 :           note_location (mac.def->line);
   20097       243396 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20098       167642 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20099              :         }
   20100              :     }
   20101              : 
   20102          899 :   return macros;
   20103              : }
   20104              : 
   20105              : /* Write out the exported defines.  This is two sections, one
   20106              :    containing the definitions, the other a table of node names.  */
   20107              : 
   20108              : unsigned
   20109          899 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20110              :                             unsigned *crc_p)
   20111              : {
   20112          966 :   dump () && dump ("Writing macros");
   20113          899 :   dump.indent ();
   20114              : 
   20115              :   /* Write the defs */
   20116          899 :   bytes_out sec (to);
   20117          899 :   sec.begin ();
   20118              : 
   20119          899 :   unsigned count = 0;
   20120        77559 :   for (unsigned ix = macros->length (); ix--;)
   20121              :     {
   20122        75761 :       cpp_hashnode *node = (*macros)[ix];
   20123        75761 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20124        75761 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20125              : 
   20126        75761 :       macro_export &mac = (*macro_exports)[slot.offset];
   20127        75761 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20128              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20129              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20130              :                   == (mac.def != NULL));
   20131              : 
   20132        75761 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20133              :         {
   20134            1 :           warning_at (mac.def->line, 0,
   20135              :                       "not exporting %<#define %E%> as it is a keyword",
   20136              :                       identifier (node));
   20137            1 :           slot.offset = 0;
   20138            1 :           continue;
   20139              :         }
   20140              : 
   20141        75760 :       count++;
   20142        75760 :       slot.offset = sec.pos;
   20143        75760 :       dump (dumper::MACRO)
   20144           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20145           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20146              :                  ? "#undef" : "",
   20147           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20148              :                  ? " & " : "",
   20149           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20150              :                  ? "#define" : "",
   20151              :                  identifier (node), slot.offset);
   20152        75760 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20153           12 :         write_location (sec, mac.undef_loc);
   20154        75760 :       if (mac.def)
   20155        75754 :         write_define (sec, mac.def);
   20156              :     }
   20157          899 :   if (count)
   20158              :     // We may have ended on a tokenless macro with a very short
   20159              :     // location, that will cause problems reading its bit flags.
   20160          144 :     sec.u (0);
   20161          899 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20162              : 
   20163          899 :   if (count)
   20164              :     {
   20165              :       /* Write the table.  */
   20166          144 :       bytes_out sec (to);
   20167          144 :       sec.begin ();
   20168          144 :       sec.u (count);
   20169              : 
   20170        76048 :       for (unsigned ix = macros->length (); ix--;)
   20171              :         {
   20172        75760 :           const cpp_hashnode *node = (*macros)[ix];
   20173        75760 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20174              : 
   20175        75760 :           if (slot.offset)
   20176              :             {
   20177        75760 :               sec.cpp_node (node);
   20178        75760 :               sec.u (slot.get_defness ());
   20179        75760 :               sec.u (slot.offset);
   20180              :             }
   20181              :         }
   20182          144 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20183          144 :     }
   20184              : 
   20185          899 :   dump.outdent ();
   20186          899 :   return count;
   20187          899 : }
   20188              : 
   20189              : bool
   20190          931 : module_state::read_macros ()
   20191              : {
   20192              :   /* Get the def section.  */
   20193          931 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20194              :     return false;
   20195              : 
   20196              :   /* Get the tbl section, if there are defs. */
   20197          931 :   if (slurp->macro_defs.more_p ()
   20198          931 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20199              :     return false;
   20200              : 
   20201              :   return true;
   20202              : }
   20203              : 
   20204              : /* Install the macro name table.  */
   20205              : 
   20206              : void
   20207          937 : module_state::install_macros ()
   20208              : {
   20209          937 :   bytes_in &sec = slurp->macro_tbl;
   20210          937 :   if (!sec.size)
   20211              :     return;
   20212              : 
   20213          203 :   dump () && dump ("Reading macro table %M", this);
   20214          181 :   dump.indent ();
   20215              : 
   20216          181 :   unsigned count = sec.u ();
   20217          203 :   dump () && dump ("%u macros", count);
   20218        84853 :   while (count--)
   20219              :     {
   20220        84672 :       cpp_hashnode *node = sec.cpp_node ();
   20221        84672 :       macro_import &imp = get_macro_imports (node);
   20222        84672 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20223        84672 :       if (!flags)
   20224            0 :         sec.set_overrun ();
   20225              : 
   20226        84672 :       if (sec.get_overrun ())
   20227              :         break;
   20228              : 
   20229        84672 :       macro_import::slot &slot = imp.append (mod, flags);
   20230        84672 :       slot.offset = sec.u ();
   20231              : 
   20232        84672 :       dump (dumper::MACRO)
   20233           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20234           30 :                  imp.length () > 1 ? "add" : "new",
   20235           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20236              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20237           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20238              :                  identifier (node), slot.offset);
   20239              : 
   20240              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20241              :          here.  But that only happens when we've had to resolve the
   20242              :          deferred macro before this import -- why are you doing
   20243              :          that?  */
   20244        84672 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20245        31375 :         if (!cur->imported_p)
   20246              :           {
   20247        31375 :             macro_import::slot &slot = imp.exported ();
   20248        31375 :             macro_export &exp = get_macro_export (slot);
   20249        31375 :             exp.def = cur;
   20250       116228 :             dump (dumper::MACRO)
   20251            0 :               && dump ("Saving current #define %I", identifier (node));
   20252              :           }
   20253              :     }
   20254              : 
   20255              :   /* We're now done with the table.  */
   20256          181 :   elf_in::release (slurp->from, sec);
   20257              : 
   20258          181 :   dump.outdent ();
   20259              : }
   20260              : 
   20261              : /* Import the transitive macros.  */
   20262              : 
   20263              : void
   20264          895 : module_state::import_macros ()
   20265              : {
   20266          895 :   bitmap_ior_into (headers, slurp->headers);
   20267              : 
   20268          895 :   bitmap_iterator bititer;
   20269          895 :   unsigned bitnum;
   20270         1832 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20271          937 :     (*modules)[bitnum]->install_macros ();
   20272          895 : }
   20273              : 
   20274              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20275              :    necessary.  */
   20276              : 
   20277              : void
   20278       288530 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20279              : {
   20280       288530 :   if (!node->deferred)
   20281              :     /* The macro is not imported, so our undef is irrelevant.  */
   20282              :     return;
   20283              : 
   20284           12 :   unsigned n = dump.push (NULL);
   20285              : 
   20286           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20287           12 :   macro_export &exp = get_macro_export (slot);
   20288              : 
   20289           12 :   exp.undef_loc = loc;
   20290           12 :   slot.become_undef ();
   20291           12 :   exp.def = NULL;
   20292              : 
   20293           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20294              : 
   20295           12 :   dump.pop (n);
   20296              : }
   20297              : 
   20298              : /* NODE is a deferred macro node.  Determine the definition and return
   20299              :    it, with NULL if undefined.  May issue diagnostics.
   20300              : 
   20301              :    This can leak memory, when merging declarations -- the string
   20302              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20303              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20304              :    expect this to be common -- mostly macros have a single point of
   20305              :    definition.  Perhaps we could restore the objstack to its position
   20306              :    after the first imported definition (if that wins)?  The macros
   20307              :    themselves are GC'd.  */
   20308              : 
   20309              : cpp_macro *
   20310          750 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20311              :                               cpp_hashnode *node)
   20312              : {
   20313          750 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20314              : 
   20315          750 :   unsigned n = dump.push (NULL);
   20316          756 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20317              : 
   20318          750 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20319              : 
   20320          750 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20321            0 :         && !imports[0].get_module ()))
   20322              :     {
   20323              :       /* Calculate the set of visible header imports.  */
   20324          750 :       bitmap_copy (visible, headers);
   20325         1790 :       for (unsigned ix = imports.length (); ix--;)
   20326              :         {
   20327         1040 :           const macro_import::slot &slot = imports[ix];
   20328         1040 :           unsigned mod = slot.get_module ();
   20329         1040 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20330         1040 :               && bitmap_bit_p (visible, mod))
   20331              :             {
   20332           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20333           12 :               bitmap_and_compl_into (visible, arg);
   20334           12 :               bitmap_set_bit (visible, mod);
   20335              :             }
   20336              :         }
   20337              :     }
   20338          750 :   bitmap_set_bit (visible, 0);
   20339              : 
   20340              :   /* Now find the macros that are still visible.  */
   20341          750 :   bool failed = false;
   20342          750 :   cpp_macro *def = NULL;
   20343          750 :   vec<macro_export> defs;
   20344          750 :   defs.create (imports.length ());
   20345         1790 :   for (unsigned ix = imports.length (); ix--;)
   20346              :     {
   20347         1040 :       const macro_import::slot &slot = imports[ix];
   20348         1040 :       unsigned mod = slot.get_module ();
   20349         1040 :       if (bitmap_bit_p (visible, mod))
   20350              :         {
   20351         1028 :           macro_export *pushed = NULL;
   20352         1028 :           if (mod)
   20353              :             {
   20354          780 :               const module_state *imp = (*modules)[mod];
   20355          780 :               bytes_in &sec = imp->slurp->macro_defs;
   20356          780 :               if (!sec.get_overrun ())
   20357              :                 {
   20358          780 :                   dump (dumper::MACRO)
   20359            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20360            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20361              :                              ? "#undef" : "",
   20362            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20363              :                              ? " & " : "",
   20364            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20365              :                              ? "#define" : "",
   20366            6 :                              identifier (node), imp, slot.offset);
   20367          780 :                   sec.random_access (slot.offset);
   20368              : 
   20369          780 :                   macro_export exp;
   20370          780 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20371           12 :                     exp.undef_loc = imp->read_location (sec);
   20372          780 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20373          774 :                     exp.def = imp->read_define (sec, reader);
   20374          780 :                   if (sec.get_overrun ())
   20375            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20376            0 :                               imp->name);
   20377              :                   else
   20378          780 :                     pushed = defs.quick_push (exp);
   20379              :                 }
   20380              :             }
   20381              :           else
   20382          248 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20383         1028 :           if (pushed && pushed->def)
   20384              :             {
   20385         1022 :               if (!def)
   20386              :                 def = pushed->def;
   20387          275 :               else if (cpp_compare_macros (def, pushed->def))
   20388         1040 :                 failed = true;
   20389              :             }
   20390              :         }
   20391              :     }
   20392              : 
   20393          750 :   if (failed)
   20394              :     {
   20395              :       /* If LOC is the first loc, this is the end of file check, which
   20396              :          is a warning.  */
   20397           15 :       auto_diagnostic_group d;
   20398           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20399            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20400              :                     "inconsistent imported macro definition %qE",
   20401              :                     identifier (node));
   20402              :       else
   20403            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20404              :                   identifier (node));
   20405           60 :       for (unsigned ix = defs.length (); ix--;)
   20406              :         {
   20407           30 :           macro_export &exp = defs[ix];
   20408           30 :           if (exp.undef_loc)
   20409            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20410           30 :           if (exp.def)
   20411           30 :             inform (exp.def->line, "%<#define %s%>",
   20412              :                     cpp_macro_definition (reader, node, exp.def));
   20413              :         }
   20414           15 :       def = NULL;
   20415           15 :     }
   20416              : 
   20417          750 :   defs.release ();
   20418              : 
   20419          750 :   dump.pop (n);
   20420              : 
   20421          750 :   return def;
   20422              : }
   20423              : 
   20424              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20425              :    iostream) contain static vars, and rely on them to run global
   20426              :    ctors.  */
   20427              : unsigned
   20428          899 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20429              : {
   20430          899 :   if (!static_aggregates && !tls_aggregates)
   20431              :     return 0;
   20432              : 
   20433           45 :   dump () && dump ("Writing initializers");
   20434           45 :   dump.indent ();
   20435              : 
   20436           45 :   static_aggregates = nreverse (static_aggregates);
   20437           45 :   tls_aggregates = nreverse (tls_aggregates);
   20438              : 
   20439           45 :   unsigned count = 0;
   20440           45 :   trees_out sec (to, this, table, ~0u);
   20441           45 :   sec.begin ();
   20442              : 
   20443           45 :   tree list = static_aggregates;
   20444          135 :   for (int passes = 0; passes != 2; passes++)
   20445              :     {
   20446          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20447          168 :         if (TREE_LANG_FLAG_0 (init))
   20448              :           {
   20449          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20450              :               {
   20451              :                 /* Ensure that in the returned result chain if the
   20452              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20453              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20454              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20455           21 :                 int phase = 0;
   20456           21 :                 tree last = NULL_TREE;
   20457           21 :                 for (tree init2 = TREE_CHAIN (init);
   20458          102 :                      init2; init2 = TREE_CHAIN (init2))
   20459              :                   {
   20460          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20461              :                       ;
   20462          102 :                     else if (phase == 0
   20463          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20464              :                       {
   20465          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20466              :                         last = init2;
   20467              :                       }
   20468           81 :                     else if (IN_RANGE (phase, 1, 2)
   20469          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20470              :                       {
   20471           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20472           81 :                           phase = 2;
   20473              :                         last = init2;
   20474              :                       }
   20475              :                     else
   20476              :                       break;
   20477              :                   }
   20478           21 :                 if (phase == 2)
   20479              :                   {
   20480              :                     /* In that case, add markers about it so that the
   20481              :                        STATIC_INIT_DECOMP_BASE_P and
   20482              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20483           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20484           21 :                                                   2 * passes + 1));
   20485           21 :                     phase = 1;
   20486          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20487          102 :                          init2 = TREE_CHAIN (init2))
   20488          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20489              :                         {
   20490          102 :                           tree decl = TREE_VALUE (init2);
   20491          102 :                           if (phase == 1
   20492          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20493              :                             {
   20494           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20495           21 :                                                             2 * passes + 2));
   20496           21 :                               phase = 2;
   20497              :                             }
   20498          102 :                           dump ("Initializer:%u for %N", count, decl);
   20499          102 :                           sec.tree_node (decl);
   20500          102 :                           ++count;
   20501              :                         }
   20502           21 :                     sec.tree_node (integer_zero_node);
   20503           21 :                     init = last;
   20504           21 :                     continue;
   20505           21 :                   }
   20506              :               }
   20507              : 
   20508          123 :             tree decl = TREE_VALUE (init);
   20509              : 
   20510          123 :             dump ("Initializer:%u for %N", count, decl);
   20511          123 :             sec.tree_node (decl);
   20512          123 :             ++count;
   20513              :           }
   20514              : 
   20515           90 :       list = tls_aggregates;
   20516              :     }
   20517              : 
   20518           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20519           45 :   dump.outdent ();
   20520              : 
   20521           45 :   return count;
   20522           45 : }
   20523              : 
   20524              : /* We have to defer some post-load processing until we've completed
   20525              :    reading, because they can cause more reading.  */
   20526              : 
   20527              : static void
   20528        12534 : post_load_processing ()
   20529              : {
   20530              :   /* We mustn't cause a GC, our caller should have arranged for that
   20531              :      not to happen.  */
   20532        12534 :   gcc_checking_assert (function_depth);
   20533              : 
   20534        12534 :   if (!post_load_decls)
   20535              :     return;
   20536              : 
   20537         7969 :   tree old_cfd = current_function_decl;
   20538         7969 :   struct function *old_cfun = cfun;
   20539        16874 :   while (post_load_decls->length ())
   20540              :     {
   20541         8905 :       tree decl = post_load_decls->pop ();
   20542              : 
   20543         8960 :       dump () && dump ("Post-load processing of %N", decl);
   20544              : 
   20545         8905 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20546              :         {
   20547           10 :           if (!DECL_SIZE (decl))
   20548              :             {
   20549            3 :               push_to_top_level ();
   20550            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20551            3 :               pop_from_top_level ();
   20552              :             }
   20553           10 :           continue;
   20554              :         }
   20555              : 
   20556         8895 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20557         8895 :       expand_or_defer_fn (decl);
   20558              :       /* As in module_state::read_cluster.  */
   20559          877 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20560         9163 :           && DECL_NOT_REALLY_EXTERN (decl))
   20561          227 :         DECL_EXTERNAL (decl) = false;
   20562              :     }
   20563              : 
   20564         7969 :   set_cfun (old_cfun);
   20565         7969 :   current_function_decl = old_cfd;
   20566              : }
   20567              : 
   20568              : bool
   20569           45 : module_state::read_inits (unsigned count)
   20570              : {
   20571           45 :   trees_in sec (this);
   20572           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20573              :     return false;
   20574           57 :   dump () && dump ("Reading %u initializers", count);
   20575           45 :   dump.indent ();
   20576              : 
   20577           45 :   lazy_snum = ~0u;
   20578           45 :   int decomp_phase = 0;
   20579           45 :   tree *aggrp = NULL;
   20580          270 :   for (unsigned ix = 0; ix != count; ix++)
   20581              :     {
   20582          225 :       tree last = NULL_TREE;
   20583          225 :       if (decomp_phase)
   20584          102 :         last = *aggrp;
   20585              :       /* Merely referencing the decl causes its initializer to be read
   20586              :          and added to the correct list.  */
   20587          225 :       tree decl = sec.tree_node ();
   20588              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20589              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20590              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20591              :          entries follow in static_aggregates, 3 means
   20592              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20593              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20594              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20595          225 :       if (tree_fits_shwi_p (decl))
   20596              :         {
   20597           63 :           if (sec.get_overrun ())
   20598              :             break;
   20599           63 :           decomp_phase = tree_to_shwi (decl);
   20600           63 :           if (decomp_phase)
   20601              :             {
   20602           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20603              :               last = *aggrp;
   20604              :             }
   20605           63 :           decl = sec.tree_node ();
   20606              :         }
   20607              : 
   20608          225 :       if (sec.get_overrun ())
   20609              :         break;
   20610          225 :       if (decl)
   20611          225 :         dump ("Initializer:%u for %N", ix, decl);
   20612          225 :       if (decomp_phase)
   20613              :         {
   20614          102 :           tree init = *aggrp;
   20615          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20616          102 :           if ((decomp_phase & 1) != 0)
   20617           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20618              :           else
   20619           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20620              :         }
   20621              :     }
   20622           45 :   if (decomp_phase && !sec.get_overrun ())
   20623              :     {
   20624            0 :       tree decl = sec.tree_node ();
   20625            0 :       gcc_assert (integer_zerop (decl));
   20626              :     }
   20627           45 :   lazy_snum = 0;
   20628           45 :   post_load_processing ();
   20629           45 :   dump.outdent ();
   20630           45 :   if (!sec.end (from ()))
   20631              :     return false;
   20632              :   return true;
   20633           45 : }
   20634              : 
   20635              : void
   20636         2747 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20637              :                             unsigned *crc_ptr)
   20638              : {
   20639         2747 :   bytes_out cfg (to);
   20640              : 
   20641         2747 :   cfg.begin ();
   20642              : 
   20643        27470 :   for (unsigned ix = MSC_HWM; ix--;)
   20644        24723 :     cfg.u (counts[ix]);
   20645              : 
   20646         2747 :   if (dump ())
   20647              :     {
   20648          300 :       dump ("Cluster sections are [%u,%u)",
   20649              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20650          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20651          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20652          300 :       dump ("Entities %u", counts[MSC_entities]);
   20653          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20654          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20655          300 :       dump ("Macros %u", counts[MSC_macros]);
   20656          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20657              :     }
   20658              : 
   20659         2747 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20660         2747 : }
   20661              : 
   20662              : bool
   20663         2932 : module_state::read_counts (unsigned counts[MSC_HWM])
   20664              : {
   20665         2932 :   bytes_in cfg;
   20666              : 
   20667         2932 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20668              :     return false;
   20669              : 
   20670        29320 :   for (unsigned ix = MSC_HWM; ix--;)
   20671        26388 :     counts[ix] = cfg.u ();
   20672              : 
   20673         2932 :   if (dump ())
   20674              :     {
   20675          529 :       dump ("Declaration sections are [%u,%u)",
   20676              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20677          529 :       dump ("Bindings %u", counts[MSC_bindings]);
   20678          529 :       dump ("Pendings %u", counts[MSC_pendings]);
   20679          529 :       dump ("Entities %u", counts[MSC_entities]);
   20680          529 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20681          529 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20682          529 :       dump ("Macros %u", counts[MSC_macros]);
   20683          529 :       dump ("Initializers %u", counts[MSC_inits]);
   20684              :     }
   20685              : 
   20686         2932 :   return cfg.end (from ());
   20687         2932 : }
   20688              : 
   20689              : /* Tool configuration:  MOD_SNAME_PFX .config
   20690              : 
   20691              :    This is data that confirms current state (or fails).  */
   20692              : 
   20693              : void
   20694         2747 : module_state::write_config (elf_out *to, module_state_config &config,
   20695              :                             unsigned inner_crc)
   20696              : {
   20697         2747 :   bytes_out cfg (to);
   20698              : 
   20699         2747 :   cfg.begin ();
   20700              : 
   20701              :   /* Write version and inner crc as u32 values, for easier
   20702              :      debug inspection.  */
   20703         3047 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20704              :                    MODULE_VERSION, inner_crc);
   20705         2747 :   cfg.u32 (unsigned (MODULE_VERSION));
   20706         2747 :   cfg.u32 (inner_crc);
   20707              : 
   20708         2747 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20709              : 
   20710              :   /* Configuration. */
   20711         3047 :   dump () && dump ("Writing target='%s', host='%s'",
   20712              :                    TARGET_MACHINE, HOST_MACHINE);
   20713         2747 :   unsigned target = to->name (TARGET_MACHINE);
   20714         2747 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20715              :                    ? target : to->name (HOST_MACHINE));
   20716         2747 :   cfg.u (target);
   20717         2747 :   cfg.u (host);
   20718              : 
   20719         2747 :   cfg.str (config.dialect_str);
   20720         2747 :   cfg.u (extensions);
   20721              : 
   20722              :   /* Global tree information.  We write the globals crc separately,
   20723              :      rather than mix it directly into the overall crc, as it is used
   20724              :      to ensure data match between instances of the compiler, not
   20725              :      integrity of the file.  */
   20726         3047 :   dump () && dump ("Writing globals=%u, crc=%x",
   20727              :                    fixed_trees->length (), global_crc);
   20728         2747 :   cfg.u (fixed_trees->length ());
   20729         2747 :   cfg.u32 (global_crc);
   20730              : 
   20731         2747 :   if (is_partition ())
   20732          196 :     cfg.u (is_interface ());
   20733              : 
   20734         2747 :   cfg.u (config.num_imports);
   20735         2747 :   cfg.u (config.num_partitions);
   20736         2747 :   cfg.u (config.num_entities);
   20737              : 
   20738         2747 :   cfg.loc (config.ordinary_locs);
   20739         2747 :   cfg.loc (config.macro_locs);
   20740         2747 :   cfg.u (config.loc_range_bits);
   20741              : 
   20742         2747 :   cfg.u (config.active_init);
   20743              : 
   20744              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20745              :      of its serialization above.  */
   20746         2747 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20747         3047 :   dump () && dump ("Writing CRC=%x", crc);
   20748         2747 : }
   20749              : 
   20750              : void
   20751           40 : module_state::note_cmi_name ()
   20752              : {
   20753           40 :   if (!cmi_noted_p && filename)
   20754              :     {
   20755           40 :       cmi_noted_p = true;
   20756           40 :       inform (loc, "compiled module file is %qs",
   20757              :               maybe_add_cmi_prefix (filename));
   20758              :     }
   20759           40 : }
   20760              : 
   20761              : bool
   20762         3042 : module_state::read_config (module_state_config &config, bool complain)
   20763              : {
   20764         3042 :   bytes_in cfg;
   20765              : 
   20766         3042 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20767              :     return false;
   20768              : 
   20769              :   /* Check version.  */
   20770         3042 :   unsigned my_ver = MODULE_VERSION;
   20771         3042 :   unsigned their_ver = cfg.u32 ();
   20772         3574 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20773              :                     : "Expecting %V found %V", my_ver, their_ver);
   20774         3042 :   if (their_ver != my_ver)
   20775              :     {
   20776              :       /* The compiler versions differ.  Close enough? */
   20777            0 :       verstr_t my_string, their_string;
   20778              : 
   20779            0 :       version2string (my_ver, my_string);
   20780            0 :       version2string (their_ver, their_string);
   20781              : 
   20782              :       /* Reject when either is non-experimental or when experimental
   20783              :          major versions differ.  */
   20784            0 :       auto_diagnostic_group d;
   20785            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20786              :                         || !IS_EXPERIMENTAL (their_ver)
   20787            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20788              :                        /* The 'I know what I'm doing' switch.  */
   20789            0 :                        && !flag_module_version_ignore);
   20790            0 :       bool inform_p = true;
   20791            0 :       if (!complain)
   20792              :         inform_p = false;
   20793            0 :       else if (reject_p)
   20794              :         {
   20795            0 :           cfg.set_overrun ();
   20796            0 :           error_at (loc, "compiled module is %sversion %s",
   20797              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20798              :                     their_string);
   20799              :         }
   20800              :       else
   20801            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20802              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20803              :                              their_string);
   20804              : 
   20805            0 :       if (inform_p)
   20806              :         {
   20807            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20808              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20809              :                   my_string,
   20810            0 :                   reject_p ? "" : flag_module_version_ignore
   20811            0 :                   ? ", be it on your own head!" : ", close enough?",
   20812              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20813            0 :           note_cmi_name ();
   20814              :         }
   20815              : 
   20816            0 :       if (reject_p)
   20817            0 :         goto done;
   20818            0 :     }
   20819              : 
   20820              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20821              :       back and forget it.  */
   20822         3042 :   cfg.u32 ();
   20823              : 
   20824              :   /* Check module name.  */
   20825         3042 :   {
   20826         3042 :     const char *their_name = from ()->name (cfg.u ());
   20827         3042 :     const char *our_name = "";
   20828              : 
   20829         3042 :     if (!is_header ())
   20830         2009 :       our_name = get_flatname ();
   20831              : 
   20832              :     /* Header units can be aliased, so name checking is
   20833              :        inappropriate.  */
   20834         3042 :     if (0 != strcmp (their_name, our_name))
   20835              :       {
   20836            0 :         error_at (loc,
   20837            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20838              :                   : their_name[0]
   20839              :                   ? G_("header module expected, module %qs found")
   20840              :                   : G_("module %qs expected, header module found"),
   20841            0 :                   their_name[0] ? their_name : our_name);
   20842            0 :         cfg.set_overrun ();
   20843            0 :         goto done;
   20844              :       }
   20845              :   }
   20846              : 
   20847              :   /* Check the CRC after the above sanity checks, so that the user is
   20848              :      clued in.  */
   20849         3042 :   {
   20850         3042 :     unsigned e_crc = crc;
   20851         3042 :     crc = cfg.get_crc ();
   20852         3574 :     dump () && dump ("Reading CRC=%x", crc);
   20853              :     /* When not complaining we haven't set directness yet, so ignore the
   20854              :        mismatch. */
   20855         3042 :     if (complain && !is_direct () && crc != e_crc)
   20856              :       {
   20857            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20858            3 :         cfg.set_overrun ();
   20859            3 :         goto done;
   20860              :       }
   20861              :   }
   20862              : 
   20863              :   /* Check target & host.  */
   20864         3039 :   {
   20865         3039 :     const char *their_target = from ()->name (cfg.u ());
   20866         3039 :     const char *their_host = from ()->name (cfg.u ());
   20867         3571 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20868         3039 :     if (strcmp (their_target, TARGET_MACHINE)
   20869         3039 :         || strcmp (their_host, HOST_MACHINE))
   20870              :       {
   20871            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20872              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20873            0 :         cfg.set_overrun ();
   20874            0 :         goto done;
   20875              :       }
   20876              :   }
   20877              : 
   20878              :   /* Check compilation dialect.  This must match.  */
   20879         3039 :   {
   20880         3039 :     const char *their_dialect = cfg.str ();
   20881         3039 :     if (strcmp (their_dialect, config.dialect_str))
   20882              :       {
   20883            1 :         if (complain)
   20884            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20885              :                     their_dialect, config.dialect_str);
   20886            1 :         cfg.set_overrun ();
   20887            1 :         goto done;
   20888              :       }
   20889              :   }
   20890              : 
   20891              :   /* Check for extensions.  If they set any, we must have them set
   20892              :      too.  */
   20893         3038 :   {
   20894         3038 :     unsigned ext = cfg.u ();
   20895         3038 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20896         3038 :     if (flag_openmp_simd)
   20897            3 :       allowed |= SE_OPENMP_SIMD;
   20898         3038 :     if (flag_openacc)
   20899            3 :       allowed |= SE_OPENACC;
   20900              : 
   20901         3038 :     if (unsigned bad = ext & ~allowed)
   20902              :       {
   20903            9 :         if (bad & SE_OPENMP)
   20904            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20905            6 :         else if (bad & SE_OPENMP_SIMD)
   20906            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20907              :                          "%<-fopenmp-simd%> to enable");
   20908            9 :         if (bad & SE_OPENACC)
   20909            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20910              :                          "enable");
   20911            9 :         cfg.set_overrun ();
   20912            9 :         goto done;
   20913              :       }
   20914         3029 :     extensions = ext;
   20915              :   }
   20916              : 
   20917              :   /* Check global trees.  */
   20918         3029 :   {
   20919         3029 :     unsigned their_fixed_length = cfg.u ();
   20920         3029 :     unsigned their_fixed_crc = cfg.u32 ();
   20921         3561 :     dump () && dump ("Read globals=%u, crc=%x",
   20922              :                      their_fixed_length, their_fixed_crc);
   20923         3029 :     if (!flag_preprocess_only
   20924         3029 :         && (their_fixed_length != fixed_trees->length ()
   20925         2975 :             || their_fixed_crc != global_crc))
   20926              :       {
   20927            0 :         error_at (loc, "fixed tree mismatch");
   20928            0 :         cfg.set_overrun ();
   20929            0 :         goto done;
   20930              :       }
   20931              :   }
   20932              : 
   20933              :   /* All non-partitions are interfaces.  */
   20934         3029 :   interface_p = !is_partition () || cfg.u ();
   20935              : 
   20936         3029 :   config.num_imports = cfg.u ();
   20937         3029 :   config.num_partitions = cfg.u ();
   20938         3029 :   config.num_entities = cfg.u ();
   20939              : 
   20940         3029 :   config.ordinary_locs = cfg.loc ();
   20941         3029 :   config.macro_locs = cfg.loc ();
   20942         3029 :   config.loc_range_bits = cfg.u ();
   20943              : 
   20944         3029 :   config.active_init = cfg.u ();
   20945              : 
   20946         3042 :  done:
   20947         3042 :   return cfg.end (from ());
   20948         3042 : }
   20949              : 
   20950              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   20951              : 
   20952              : static int
   20953          124 : ool_cmp (const void *a_, const void *b_)
   20954              : {
   20955          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   20956          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   20957          124 :   if (a == b)
   20958              :     return 0;
   20959          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   20960              :     return -1;
   20961              :   else
   20962           52 :     return +1;
   20963              : }
   20964              : 
   20965              : /* Use ELROND format to record the following sections:
   20966              :      qualified-names        : binding value(s)
   20967              :      MOD_SNAME_PFX.README   : human readable, strings
   20968              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   20969              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   20970              :      MOD_SNAME_PFX.udi      : namespace using-directives
   20971              :      MOD_SNAME_PFX.bnd      : binding table
   20972              :      MOD_SNAME_PFX.spc      : specialization table
   20973              :      MOD_SNAME_PFX.imp      : import table
   20974              :      MOD_SNAME_PFX.ent      : entity table
   20975              :      MOD_SNAME_PFX.prt      : partitions table
   20976              :      MOD_SNAME_PFX.olm      : ordinary line maps
   20977              :      MOD_SNAME_PFX.mlm      : macro line maps
   20978              :      MOD_SNAME_PFX.def      : macro definitions
   20979              :      MOD_SNAME_PFX.mac      : macro index
   20980              :      MOD_SNAME_PFX.ini      : inits
   20981              :      MOD_SNAME_PFX.cnt      : counts
   20982              :      MOD_SNAME_PFX.cfg      : config data
   20983              : */
   20984              : 
   20985              : bool
   20986         2776 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   20987              :                            module_state_config &config, unsigned &crc)
   20988              : {
   20989              :   /* Figure out remapped module numbers, which might elide
   20990              :      partitions.  */
   20991         2776 :   bitmap partitions = NULL;
   20992         2776 :   if (!is_header () && !is_partition ())
   20993         1681 :     partitions = BITMAP_GGC_ALLOC ();
   20994         2776 :   write_init_maps ();
   20995              : 
   20996         2776 :   unsigned mod_hwm = 1;
   20997         3450 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   20998              :     {
   20999          674 :       module_state *imp = (*modules)[ix];
   21000              : 
   21001              :       /* Promote any non-partition direct import from a partition, unless
   21002              :          we're a partition.  */
   21003          617 :       if (!is_partition () && !imp->is_partition ()
   21004         1107 :           && imp->is_partition_direct ())
   21005           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21006              : 
   21007              :       /* Write any import that is not a partition, unless we're a
   21008              :          partition.  */
   21009          674 :       if (!partitions || !imp->is_partition ())
   21010          490 :         imp->remap = mod_hwm++;
   21011              :       else
   21012              :         {
   21013          223 :           dump () && dump ("Partition %M %u", imp, ix);
   21014          184 :           bitmap_set_bit (partitions, ix);
   21015          184 :           imp->remap = 0;
   21016              :           /* All interface partitions must be exported.  */
   21017          184 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21018              :             {
   21019            3 :               error_at (imp->loc, "interface partition is not exported");
   21020            3 :               bitmap_set_bit (exports, imp->mod);
   21021              :             }
   21022              : 
   21023              :           /* All the partition entities should have been loaded when
   21024              :              loading the partition.  */
   21025              :           if (CHECKING_P)
   21026         1233 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21027              :               {
   21028         1049 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21029         1049 :                 gcc_checking_assert (!slot->is_lazy ());
   21030              :               }
   21031              :         }
   21032              : 
   21033          674 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21034          656 :         note_location (imp->imported_from ());
   21035              :     }
   21036              : 
   21037         2776 :   if (partitions && bitmap_empty_p (partitions))
   21038              :     /* No partitions present.  */
   21039              :     partitions = nullptr;
   21040              : 
   21041              :   /* Find the set of decls we must write out.  */
   21042         2776 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21043              :   /* Add the specializations before the writables, so that we can
   21044              :      detect injected friend specializations.  */
   21045         2776 :   table.add_specializations (true);
   21046         2776 :   table.add_specializations (false);
   21047         2776 :   if (partial_specializations)
   21048              :     {
   21049          210 :       table.add_partial_entities (partial_specializations);
   21050          210 :       partial_specializations = NULL;
   21051              :     }
   21052         2776 :   table.add_namespace_entities (global_namespace, partitions);
   21053         2776 :   if (class_members)
   21054              :     {
   21055           12 :       table.add_class_entities (class_members);
   21056           12 :       class_members = NULL;
   21057              :     }
   21058              : 
   21059              :   /* Now join everything up.  */
   21060         2776 :   table.find_dependencies (this);
   21061              : 
   21062         2776 :   if (!table.finalize_dependencies ())
   21063              :     return false;
   21064              : 
   21065              : #if CHECKING_P
   21066              :   /* We're done verifying at-most once reading, reset to verify
   21067              :      at-most once writing.  */
   21068         2747 :   note_defs = note_defs_table_t::create_ggc (1000);
   21069              : #endif
   21070              : 
   21071              :   /* Determine Strongly Connected Components.  This will also strip any
   21072              :      unnecessary dependencies on imported or TU-local entities.  */
   21073         2747 :   vec<depset *> sccs = table.connect ();
   21074              : 
   21075         2747 :   vec_alloc (ool, modules->length ());
   21076         3414 :   for (unsigned ix = modules->length (); --ix;)
   21077              :     {
   21078          667 :       auto *import = (*modules)[ix];
   21079          667 :       if (import->loadedness > ML_NONE
   21080          667 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21081          483 :         ool->quick_push (import);
   21082              :     }
   21083         2747 :   ool->qsort (ool_cmp);
   21084              : 
   21085         2747 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21086              : 
   21087         2747 :   vec<cpp_hashnode *> *macros = nullptr;
   21088         2747 :   if (is_header ())
   21089          899 :     macros = prepare_macros (reader);
   21090              : 
   21091         2747 :   config.num_imports = mod_hwm;
   21092         2747 :   config.num_partitions = modules->length () - mod_hwm;
   21093         2747 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21094         2747 :   unsigned counts[MSC_HWM];
   21095         2747 :   memset (counts, 0, sizeof (counts));
   21096              : 
   21097              :   /* depset::cluster is the cluster number,
   21098              :      depset::section is unspecified scratch value.
   21099              : 
   21100              :      The following loops make use of the tarjan property that
   21101              :      dependencies will be earlier in the SCCS array.  */
   21102              : 
   21103              :   /* This first loop determines the number of depsets in each SCC, and
   21104              :      also the number of namespaces we're dealing with.  During the
   21105              :      loop, the meaning of a couple of depset fields now change:
   21106              : 
   21107              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21108              :      depset::section -> section number of cluster (if !namespace). */
   21109              : 
   21110         2747 :   unsigned n_spaces = 0;
   21111         2747 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21112       313276 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21113              :     {
   21114       310529 :       depset **base = &sccs[ix];
   21115              : 
   21116       587335 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21117              :         {
   21118         5061 :           n_spaces++;
   21119         5061 :           size = 1;
   21120              :         }
   21121              :       else
   21122              :         {
   21123              :           /* Count the members in this cluster.  */
   21124      1370286 :           for (size = 1; ix + size < sccs.length (); size++)
   21125      1367870 :             if (base[size]->cluster != base[0]->cluster)
   21126              :               break;
   21127              : 
   21128      1675754 :           for (unsigned jx = 0; jx != size; jx++)
   21129              :             {
   21130              :               /* Set the section number.  */
   21131      1370286 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21132      1370286 :               base[jx]->section = counts[MSC_sec_hwm];
   21133              :             }
   21134              : 
   21135              :           /* Save the size in the first member's cluster slot.  */
   21136       305468 :           base[0]->cluster = size;
   21137              : 
   21138       305468 :           counts[MSC_sec_hwm]++;
   21139              :         }
   21140              :     }
   21141              : 
   21142              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21143              :      The meaning of depset::cluster changes to provide the
   21144              :      unnamed-decl count of the depset's decl (and remains zero for
   21145              :      non-decls and non-unnamed).  */
   21146         2747 :   unsigned bytes = 0;
   21147         2747 :   vec<depset *> spaces;
   21148         2747 :   spaces.create (n_spaces);
   21149              : 
   21150       313276 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21151              :     {
   21152       310529 :       depset **base = &sccs[ix];
   21153              : 
   21154       310529 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21155              :         {
   21156         5061 :           tree decl = base[0]->get_entity ();
   21157         5061 :           if (decl == global_namespace)
   21158         2493 :             base[0]->cluster = 0;
   21159         2568 :           else if (!base[0]->is_import ())
   21160              :             {
   21161         2568 :               base[0]->cluster = counts[MSC_entities]++;
   21162         2568 :               spaces.quick_push (base[0]);
   21163         2568 :               counts[MSC_namespaces]++;
   21164         2568 :               if (CHECKING_P)
   21165              :                 {
   21166              :                   /* Add it to the entity map, such that we can tell it is
   21167              :                      part of us.  */
   21168         2568 :                   bool existed;
   21169         2568 :                   unsigned *slot = &entity_map->get_or_insert
   21170         2568 :                     (DECL_UID (decl), &existed);
   21171         2568 :                   if (existed)
   21172              :                     /* It must have come from a partition.  */
   21173            0 :                     gcc_checking_assert
   21174              :                       (import_entity_module (*slot)->is_partition ());
   21175         2568 :                   *slot = ~base[0]->cluster;
   21176              :                 }
   21177       313127 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21178              :             }
   21179              :           size = 1;
   21180              :         }
   21181              :       else
   21182              :         {
   21183       305468 :           size = base[0]->cluster;
   21184              : 
   21185              :           /* Cluster is now used to number entities.  */
   21186       305468 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21187              : 
   21188       305468 :           sort_cluster (&table, base, size);
   21189              : 
   21190              :           /* Record the section for consistency checking during stream
   21191              :              out -- we don't want to start writing decls in different
   21192              :              sections.  */
   21193       305468 :           table.section = base[0]->section;
   21194       305468 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21195       305468 :           table.section = 0;
   21196              :         }
   21197              :     }
   21198              : 
   21199              :   /* depset::cluster - entity number (on entities)
   21200              :      depset::section - cluster number  */
   21201              :   /* We'd better have written as many sections and found as many
   21202              :      namespaces as we predicted.  */
   21203         5494 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21204              :               && spaces.length () == counts[MSC_namespaces]);
   21205              : 
   21206              :   /* Write the entitites.  None happens if we contain namespaces or
   21207              :      nothing. */
   21208         2747 :   config.num_entities = counts[MSC_entities];
   21209         2747 :   if (counts[MSC_entities])
   21210         2487 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21211              : 
   21212              :   /* Write the namespaces.  */
   21213         2747 :   if (counts[MSC_namespaces])
   21214          583 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21215              : 
   21216              :   /* Write any using-directives.  */
   21217         2747 :   if (counts[MSC_namespaces])
   21218          583 :     counts[MSC_using_directives]
   21219          583 :       = write_using_directives (to, table, spaces, &crc);
   21220              : 
   21221              :   /* Write the bindings themselves.  */
   21222         2747 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21223              : 
   21224              :   /* Write the unnamed.  */
   21225         2747 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21226              : 
   21227              :   /* Write the import table.  */
   21228         2747 :   if (config.num_imports > 1)
   21229          452 :     write_imports (to, &crc);
   21230              : 
   21231              :   /* Write elided partition table.  */
   21232         2747 :   if (config.num_partitions)
   21233          133 :     write_partitions (to, config.num_partitions, &crc);
   21234              : 
   21235              :   /* Write the line maps.  */
   21236         2747 :   if (config.ordinary_locs)
   21237              :     {
   21238         2651 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21239         2651 :       write_diagnostic_classification (to, global_dc, &crc);
   21240              :     }
   21241         2747 :   if (config.macro_locs)
   21242          123 :     write_macro_maps (to, map_info, &crc);
   21243              : 
   21244         2747 :   if (is_header ())
   21245              :     {
   21246          899 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21247          899 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21248          899 :       vec_free (macros);
   21249              :     }
   21250              : 
   21251         2747 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21252         2747 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21253          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21254         2747 :   trees_out::instrument ();
   21255              : 
   21256         2747 :   write_counts (to, counts, &crc);
   21257              : 
   21258         2747 :   spaces.release ();
   21259         2747 :   sccs.release ();
   21260              : 
   21261         2747 :   vec_free (macro_loc_remap);
   21262         2747 :   vec_free (ord_loc_remap);
   21263         2747 :   vec_free (ool);
   21264              : 
   21265              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21266              :   // information (which might leak data you do not want to leak).
   21267              :   // Perhaps (some of) the write_readme contents should also be
   21268              :   // so-controlled.
   21269         2747 :   if (false)
   21270              :     write_env (to);
   21271              : 
   21272         2747 :   return true;
   21273         2776 : }
   21274              : 
   21275              : // Finish module writing after we've emitted all dynamic initializers.
   21276              : 
   21277              : void
   21278         2747 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21279              :                          module_state_config &config, unsigned &crc)
   21280              : {
   21281              :   /* And finish up.  */
   21282         2747 :   write_config (to, config, crc);
   21283              : 
   21284              :   /* Human-readable info.  */
   21285         2747 :   write_readme (to, reader, config.dialect_str);
   21286              : 
   21287         3047 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21288         2747 : }
   21289              : 
   21290              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21291              :    maps.  */
   21292              : 
   21293              : bool
   21294         2996 : module_state::read_initial (cpp_reader *reader)
   21295              : {
   21296         2996 :   module_state_config config;
   21297         2996 :   bool ok = true;
   21298              : 
   21299         2996 :   if (ok && !read_config (config))
   21300              :     ok = false;
   21301              : 
   21302         2983 :   bool have_locs = ok && read_prepare_maps (&config);
   21303              : 
   21304              :   /* Ordinary maps before the imports.  */
   21305         2983 :   if (!(have_locs && config.ordinary_locs))
   21306           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21307         2925 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21308              :     ok = false;
   21309              : 
   21310              :   /* Allocate the REMAP vector.  */
   21311         2996 :   slurp->alloc_remap (config.num_imports);
   21312              : 
   21313         2996 :   if (ok)
   21314              :     {
   21315              :       /* Read the import table.  Decrement current to stop this CMI
   21316              :          from being evicted during the import. */
   21317         2983 :       slurp->current--;
   21318         2983 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21319              :         ok = false;
   21320         2983 :       slurp->current++;
   21321              :     }
   21322              : 
   21323              :   /* Read the elided partition table, if we're the primary partition.  */
   21324         2983 :   if (ok && config.num_partitions && is_module ()
   21325         3010 :       && !read_partitions (config.num_partitions))
   21326              :     ok = false;
   21327              : 
   21328              :   /* Determine the module's number.  */
   21329         2996 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21330         2996 :   gcc_checking_assert (this != this_module ());
   21331              : 
   21332         2996 :   {
   21333              :     /* Allocate space in the entities array now -- that array must be
   21334              :        monotonically in step with the modules array.  */
   21335         2996 :     entity_lwm = vec_safe_length (entity_ary);
   21336         2996 :     entity_num = config.num_entities;
   21337         2996 :     gcc_checking_assert (modules->length () == 1
   21338              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21339         2996 :     vec_safe_reserve (entity_ary, config.num_entities);
   21340              : 
   21341         2996 :     binding_slot slot;
   21342         2996 :     slot.u.binding = NULL_TREE;
   21343      1300842 :     for (unsigned count = config.num_entities; count--;)
   21344      1297846 :       entity_ary->quick_push (slot);
   21345              :   }
   21346              : 
   21347              :   /* We'll run out of other resources before we run out of module
   21348              :      indices.  */
   21349         2996 :   mod = modules->length ();
   21350         2996 :   vec_safe_push (modules, this);
   21351              : 
   21352              :   /* We always import and export ourselves. */
   21353         2996 :   bitmap_set_bit (imports, mod);
   21354         2996 :   bitmap_set_bit (exports, mod);
   21355              : 
   21356         2996 :   if (ok)
   21357         2983 :     (*slurp->remap)[0] = mod << 1;
   21358         3525 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21359              : 
   21360              :   /* We should not have been frozen during the importing done by
   21361              :      read_config.  */
   21362         2996 :   gcc_assert (!from ()->is_frozen ());
   21363              : 
   21364              :   /* Macro maps after the imports.  */
   21365         2996 :   if (!(ok && have_locs && config.macro_locs))
   21366         2865 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21367          131 :   else if (!read_macro_maps (config.macro_locs))
   21368              :     ok = false;
   21369              : 
   21370              :   /* Diagnostic classification streaming needs to come after reading
   21371              :      macro maps to handle _Pragmas in macros.  */
   21372         2983 :   if (ok && have_locs && config.ordinary_locs
   21373         5921 :       && !read_diagnostic_classification (global_dc))
   21374              :     ok = false;
   21375              : 
   21376              :   /* Note whether there's an active initializer.  */
   21377         2996 :   active_init_p = !is_header () && bool (config.active_init);
   21378              : 
   21379         2996 :   gcc_assert (slurp->current == ~0u);
   21380         2996 :   return ok;
   21381              : }
   21382              : 
   21383              : /* Read a preprocessor state.  */
   21384              : 
   21385              : bool
   21386          937 : module_state::read_preprocessor (bool outermost)
   21387              : {
   21388          937 :   gcc_checking_assert (is_header () && slurp
   21389              :                        && slurp->remap_module (0) == mod);
   21390              : 
   21391          937 :   if (loadedness == ML_PREPROCESSOR)
   21392            6 :     return !(from () && from ()->get_error ());
   21393              : 
   21394          931 :   bool ok = true;
   21395              : 
   21396              :   /* Read direct header imports.  */
   21397          931 :   unsigned len = slurp->remap->length ();
   21398          973 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21399              :     {
   21400           42 :       unsigned map = (*slurp->remap)[ix];
   21401           42 :       if (map & 1)
   21402              :         {
   21403           42 :           module_state *import = (*modules)[map >> 1];
   21404           42 :           if (import->is_header ())
   21405              :             {
   21406           42 :               ok = import->read_preprocessor (false);
   21407           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21408              :             }
   21409              :         }
   21410              :     }
   21411              : 
   21412              :   /* Record as a direct header.  */
   21413          931 :   if (ok)
   21414          931 :     bitmap_set_bit (slurp->headers, mod);
   21415              : 
   21416          931 :   if (ok && !read_macros ())
   21417              :     ok = false;
   21418              : 
   21419          931 :   loadedness = ML_PREPROCESSOR;
   21420          931 :   announce ("macros");
   21421              : 
   21422          931 :   if (flag_preprocess_only)
   21423              :     /* We're done with the string table.  */
   21424           39 :     from ()->release ();
   21425              : 
   21426          931 :   return check_read (outermost, ok);
   21427              : }
   21428              : 
   21429              : /* Read language state.  */
   21430              : 
   21431              : bool
   21432         3016 : module_state::read_language (bool outermost)
   21433              : {
   21434         3016 :   gcc_checking_assert (!lazy_snum);
   21435              : 
   21436         3016 :   if (loadedness == ML_LANGUAGE)
   21437           84 :     return !(slurp && from () && from ()->get_error ());
   21438              : 
   21439         2932 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21440              :                        && slurp->remap_module (0) == mod);
   21441              : 
   21442         2932 :   bool ok = true;
   21443              : 
   21444              :   /* Read direct imports.  */
   21445         2932 :   unsigned len = slurp->remap->length ();
   21446         3331 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21447              :     {
   21448          399 :       unsigned map = (*slurp->remap)[ix];
   21449          399 :       if (map & 1)
   21450              :         {
   21451          395 :           module_state *import = (*modules)[map >> 1];
   21452          395 :           if (!import->read_language (false))
   21453          399 :             ok = false;
   21454              :         }
   21455              :     }
   21456              : 
   21457         2932 :   unsigned counts[MSC_HWM];
   21458              : 
   21459         2932 :   if (ok && !read_counts (counts))
   21460              :     ok = false;
   21461              : 
   21462         2932 :   function_depth++; /* Prevent unexpected GCs.  */
   21463              : 
   21464         2932 :   if (ok && counts[MSC_entities] != entity_num)
   21465              :     ok = false;
   21466         2932 :   if (ok && counts[MSC_entities]
   21467         2706 :       && !read_entities (counts[MSC_entities],
   21468              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21469              :     ok = false;
   21470              : 
   21471              :   /* Read the namespace hierarchy.  */
   21472         2932 :   if (ok && counts[MSC_namespaces]
   21473         3530 :       && !read_namespaces (counts[MSC_namespaces]))
   21474              :     ok = false;
   21475              : 
   21476              :   /* Read any using-directives.  */
   21477         2932 :   if (ok && counts[MSC_using_directives]
   21478         3089 :       && !read_using_directives (counts[MSC_using_directives]))
   21479              :     ok = false;
   21480              : 
   21481         2932 :   if (ok && !read_bindings (counts[MSC_bindings],
   21482              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21483              :     ok = false;
   21484              : 
   21485              :   /* And unnamed.  */
   21486         2932 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21487              :     ok = false;
   21488              : 
   21489         2932 :   if (ok)
   21490              :     {
   21491         2932 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21492         2932 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21493              :     }
   21494              : 
   21495         2932 :   if (!flag_module_lazy
   21496         2932 :       || (is_partition ()
   21497          235 :           && module_interface_p ()
   21498          208 :           && !module_partition_p ()))
   21499              :     {
   21500              :       /* Read the sections in forward order, so that dependencies are read
   21501              :          first.  See note about tarjan_connect.  */
   21502          544 :       ggc_collect ();
   21503              : 
   21504          544 :       lazy_snum = ~0u;
   21505              : 
   21506          544 :       unsigned hwm = counts[MSC_sec_hwm];
   21507       143518 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21508       142974 :         if (!load_section (ix, NULL))
   21509              :           {
   21510              :             ok = false;
   21511              :             break;
   21512              :           }
   21513          544 :       lazy_snum = 0;
   21514          544 :       post_load_processing ();
   21515              : 
   21516          544 :       ggc_collect ();
   21517              : 
   21518          544 :       if (ok && CHECKING_P)
   21519       515563 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21520       515019 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21521              :     }
   21522              : 
   21523              :   // If the import is a header-unit, we need to register initializers
   21524              :   // of any static objects it contains (looking at you _Ioinit).
   21525              :   // Notice, the ordering of these initializers will be that of a
   21526              :   // dynamic initializer at this point in the current TU.  (Other
   21527              :   // instances of these objects in other TUs will be initialized as
   21528              :   // part of that TU's global initializers.)
   21529         2932 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21530              :     ok = false;
   21531              : 
   21532         2932 :   function_depth--;
   21533              : 
   21534         3292 :   announce (flag_module_lazy ? "lazy" : "imported");
   21535         2932 :   loadedness = ML_LANGUAGE;
   21536              : 
   21537         2932 :   gcc_assert (slurp->current == ~0u);
   21538              : 
   21539              :   /* We're done with the string table.  */
   21540         2932 :   from ()->release ();
   21541              : 
   21542         2932 :   return check_read (outermost, ok);
   21543              : }
   21544              : 
   21545              : bool
   21546       207557 : module_state::maybe_defrost ()
   21547              : {
   21548       207557 :   bool ok = true;
   21549       207557 :   if (from ()->is_frozen ())
   21550              :     {
   21551            9 :       if (lazy_open >= lazy_limit)
   21552            3 :         freeze_an_elf ();
   21553           18 :       dump () && dump ("Defrosting '%s'", filename);
   21554            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21555            9 :       lazy_open++;
   21556              :     }
   21557              : 
   21558       207557 :   return ok;
   21559              : }
   21560              : 
   21561              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21562              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21563              :    when reading back in.  */
   21564              : 
   21565              : bool
   21566       207557 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21567              : {
   21568       207557 :   if (from ()->get_error ())
   21569              :     return false;
   21570              : 
   21571       207557 :   if (snum >= slurp->current)
   21572            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21573       207557 :   else if (maybe_defrost ())
   21574              :     {
   21575       207557 :       unsigned old_current = slurp->current;
   21576       207557 :       slurp->current = snum;
   21577       207557 :       slurp->lru = 0;  /* Do not swap out.  */
   21578       207557 :       slurp->remaining--;
   21579       207557 :       read_cluster (snum);
   21580       207557 :       slurp->lru = ++lazy_lru;
   21581       207557 :       slurp->current = old_current;
   21582              :     }
   21583              : 
   21584       207557 :   if (mslot && mslot->is_lazy ())
   21585              :     {
   21586              :       /* Oops, the section didn't set this slot.  */
   21587            0 :       from ()->set_error (elf::E_BAD_DATA);
   21588            0 :       *mslot = NULL_TREE;
   21589              :     }
   21590              : 
   21591       207557 :   bool ok = !from ()->get_error ();
   21592       207557 :   if (!ok)
   21593              :     {
   21594            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21595            0 :                 snum, from ()->get_error (filename));
   21596            0 :       note_cmi_name ();
   21597              :     }
   21598              : 
   21599       207557 :   maybe_completed_reading ();
   21600              : 
   21601       207557 :   return ok;
   21602              : }
   21603              : 
   21604              : void
   21605       214400 : module_state::maybe_completed_reading ()
   21606              : {
   21607       214400 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21608              :     {
   21609         2422 :       lazy_open--;
   21610              :       /* We no longer need the macros, all tokenizing has been done.  */
   21611         2422 :       slurp->release_macros ();
   21612              : 
   21613         2422 :       from ()->end ();
   21614         2422 :       slurp->close ();
   21615         2422 :       slurped ();
   21616              :     }
   21617       214400 : }
   21618              : 
   21619              : /* After a reading operation, make sure things are still ok.  If not,
   21620              :    emit an error and clean up.  */
   21621              : 
   21622              : bool
   21623         6880 : module_state::check_read (bool outermost, bool ok)
   21624              : {
   21625         6880 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21626              : 
   21627         6880 :   if (!ok)
   21628           34 :     from ()->set_error ();
   21629              : 
   21630         6880 :   if (int e = from ()->get_error ())
   21631              :     {
   21632           40 :       auto_diagnostic_group d;
   21633           40 :       error_at (loc, "failed to read compiled module: %s",
   21634           40 :                 from ()->get_error (filename));
   21635           40 :       note_cmi_name ();
   21636              : 
   21637           40 :       if (e == EMFILE
   21638           40 :           || e == ENFILE
   21639              : #if MAPPED_READING
   21640           40 :           || e == ENOMEM
   21641              : #endif
   21642              :           || false)
   21643            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21644              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21645              :                 " or increasing the per-process file descriptor limit",
   21646              :                 param_lazy_modules);
   21647           40 :       else if (e == ENOENT)
   21648           21 :         inform (loc, "imports must be built before being imported");
   21649              : 
   21650           40 :       if (outermost)
   21651           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21652              : 
   21653            3 :       ok = false;
   21654            3 :     }
   21655              : 
   21656         6843 :   maybe_completed_reading ();
   21657              : 
   21658         6843 :   return ok;
   21659              : }
   21660              : 
   21661              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21662              :    including dots.  */
   21663              : 
   21664              : char const *
   21665          408 : module_name (unsigned ix, bool header_ok)
   21666              : {
   21667          408 :   if (modules)
   21668              :     {
   21669          408 :       module_state *imp = (*modules)[ix];
   21670              : 
   21671          408 :       if (ix && !imp->name)
   21672            0 :         imp = imp->parent;
   21673              : 
   21674          408 :       if (header_ok || !imp->is_header ())
   21675          408 :         return imp->get_flatname ();
   21676              :     }
   21677              : 
   21678              :   return NULL;
   21679              : }
   21680              : 
   21681              : /* Return the bitmap describing what modules are imported.  Remember,
   21682              :    we always import ourselves.  */
   21683              : 
   21684              : bitmap
   21685       130921 : get_import_bitmap ()
   21686              : {
   21687       130921 :   return this_module ()->imports;
   21688              : }
   21689              : 
   21690              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21691              :    if we're not an instantiation.  */
   21692              : 
   21693              : static tree
   21694       210108 : orig_decl_for_instantiation (tinst_level *tinst)
   21695              : {
   21696       210108 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21697              :     return NULL_TREE;
   21698              : 
   21699       101850 :   tree decl = tinst->tldcl;
   21700       101850 :   if (TREE_CODE (decl) == TREE_LIST)
   21701            0 :     decl = TREE_PURPOSE (decl);
   21702       101850 :   if (TYPE_P (decl))
   21703        15117 :     decl = TYPE_NAME (decl);
   21704              :   return decl;
   21705              : }
   21706              : 
   21707              : /* Return the visible imports and path of instantiation for an
   21708              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21709              :    instantiation, and thus will return the visible imports of the
   21710              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21711              :    the tinst level itself.  */
   21712              : 
   21713              : static bitmap
   21714       154735 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21715              : {
   21716       154735 :   gcc_checking_assert (modules_p ());
   21717              : 
   21718       154735 :   tree decl = orig_decl_for_instantiation (tinst);
   21719       154735 :   if (!decl)
   21720              :     {
   21721        69664 :       gcc_assert (!tinst || !tinst->next);
   21722              :       /* Not inside an instantiation, just the regular case.  */
   21723        69664 :       *path_map_p = nullptr;
   21724        69664 :       return get_import_bitmap ();
   21725              :     }
   21726              : 
   21727        85071 :   if (!tinst->path)
   21728              :     {
   21729              :       /* Calculate.  */
   21730        25493 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21731        25493 :       bitmap path_map = *path_map_p;
   21732              : 
   21733        25493 :       if (!path_map)
   21734              :         {
   21735         3197 :           path_map = BITMAP_GGC_ALLOC ();
   21736         3197 :           bitmap_set_bit (path_map, 0);
   21737              :         }
   21738              : 
   21739        25493 :       if (unsigned mod = get_originating_module (decl))
   21740         3842 :         if (!bitmap_bit_p (path_map, mod))
   21741              :           {
   21742              :             /* This is brand new information!  */
   21743          170 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21744          170 :             bitmap_copy (new_path, path_map);
   21745          170 :             bitmap_set_bit (new_path, mod);
   21746          170 :             path_map = new_path;
   21747              : 
   21748          170 :             bitmap imports = (*modules)[mod]->imports;
   21749          170 :             if (bitmap_intersect_compl_p (imports, visible))
   21750              :               {
   21751              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21752           32 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21753              : 
   21754           32 :                 bitmap_ior (new_visible, visible, imports);
   21755           32 :                 visible = new_visible;
   21756              :               }
   21757              :           }
   21758              : 
   21759        25493 :       tinst->path = path_map;
   21760        25493 :       tinst->visible = visible;
   21761              :     }
   21762              : 
   21763        85071 :   *path_map_p = tinst->path;
   21764        85071 :   return tinst->visible;
   21765              : }
   21766              : 
   21767              : /* Return the bitmap describing what modules are visible along the
   21768              :    path of instantiation.  If we're not an instantiation, this will be
   21769              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21770              :    modules owning the instantiation path -- we see the module-linkage
   21771              :    entities of those modules.  */
   21772              : 
   21773              : bitmap
   21774     31956390 : visible_instantiation_path (bitmap *path_map_p)
   21775              : {
   21776     31956390 :   if (!modules_p ())
   21777              :     return NULL;
   21778              : 
   21779       129242 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21780              : }
   21781              : 
   21782              : /* Returns the bitmap describing what modules were visible from the
   21783              :    module that the current instantiation originated from.  If we're
   21784              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21785              :    the originating module of the definition for this instantiation.  */
   21786              : 
   21787              : bitmap
   21788        55373 : visible_from_instantiation_origination (unsigned *module_p)
   21789              : {
   21790        55373 :   if (!modules_p ())
   21791              :     return NULL;
   21792              : 
   21793        55373 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21794        55373 :   if (!decl)
   21795              :     return NULL;
   21796              : 
   21797        16779 :   *module_p = get_originating_module (decl);
   21798        16779 :   return (*modules)[*module_p]->imports;
   21799              : }
   21800              : 
   21801              : /* We've just directly imported IMPORT.  Update our import/export
   21802              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21803              : 
   21804              : void
   21805         3083 : module_state::set_import (module_state const *import, bool is_export)
   21806              : {
   21807         3083 :   gcc_checking_assert (this != import);
   21808              : 
   21809              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21810              :      the primary interface or a partition we'll see its imports.  */
   21811         3083 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21812              :                    ? import->imports : import->exports);
   21813              : 
   21814         3083 :   if (is_export)
   21815              :     /* We'll export OTHER's exports.  */
   21816          480 :     bitmap_ior_into (exports, import->exports);
   21817         3083 : }
   21818              : 
   21819              : /* Return the declaring entity of DECL.  That is the decl determining
   21820              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21821              :    it's the global module.  */
   21822              : 
   21823              : tree
   21824    108108091 : get_originating_module_decl (tree decl)
   21825              : {
   21826              :   /* An enumeration constant.  */
   21827    108108091 :   if (TREE_CODE (decl) == CONST_DECL
   21828         8381 :       && DECL_CONTEXT (decl)
   21829    108116472 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21830         7604 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21831    108100487 :   else if (TREE_CODE (decl) == FIELD_DECL
   21832    108067713 :            || TREE_CODE (decl) == USING_DECL
   21833    216148065 :            || CONST_DECL_USING_P (decl))
   21834              :     {
   21835        53686 :       decl = DECL_CONTEXT (decl);
   21836        53686 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21837        53686 :         decl = TYPE_NAME (decl);
   21838              :     }
   21839              : 
   21840    108108091 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21841              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21842              :                        || TREE_CODE (decl) == TYPE_DECL
   21843              :                        || TREE_CODE (decl) == VAR_DECL
   21844              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21845              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21846              : 
   21847    110649633 :   for (;;)
   21848              :     {
   21849              :       /* Uninstantiated template friends are owned by the befriending
   21850              :          class -- not their context.  */
   21851    109378862 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21852    109378862 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21853        11478 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21854              : 
   21855              :       /* An imported temploid friend is attached to the same module the
   21856              :          befriending class was.  */
   21857    109378862 :       if (imported_temploid_friends)
   21858      3730434 :         if (tree *slot = imported_temploid_friends->get (decl))
   21859          728 :           decl = *slot;
   21860              : 
   21861    109378862 :       int use;
   21862    109378862 :       if (tree ti = node_template_info (decl, use))
   21863              :         {
   21864       959796 :           decl = TI_TEMPLATE (ti);
   21865       959796 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21866              :             {
   21867              :               /* A friend template specialization.  */
   21868           43 :               gcc_checking_assert (OVL_P (decl));
   21869           43 :               return global_namespace;
   21870              :             }
   21871              :         }
   21872              :       else
   21873              :         {
   21874    108419066 :           tree ctx = CP_DECL_CONTEXT (decl);
   21875    108419066 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21876              :             break;
   21877              : 
   21878       311018 :           if (TYPE_P (ctx))
   21879              :             {
   21880       279567 :               ctx = TYPE_NAME (ctx);
   21881       279567 :               if (!ctx)
   21882              :                 {
   21883              :                   /* Some kind of internal type.  */
   21884            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21885            0 :                   return global_namespace;
   21886              :                 }
   21887              :             }
   21888       311018 :           decl = ctx;
   21889              :         }
   21890      1270771 :     }
   21891              : 
   21892    108108048 :   return decl;
   21893              : }
   21894              : 
   21895              : /* If DECL is imported, return which module imported it, or 0 for the current
   21896              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21897              :    global module.  */
   21898              : 
   21899              : int
   21900      1554673 : get_originating_module (tree decl, bool global_m1)
   21901              : {
   21902      1554673 :   tree owner = get_originating_module_decl (decl);
   21903      1554673 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21904              : 
   21905      1554673 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21906       440678 :     return global_m1 ? -1 : 0;
   21907              : 
   21908      2175819 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21909              :     return -1;
   21910              : 
   21911       118785 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21912       118785 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21913              :   return mod;
   21914              : }
   21915              : 
   21916              : /* DECL is imported, return which module imported it.
   21917              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21918              : 
   21919              : unsigned
   21920        15938 : get_importing_module (tree decl, bool flexible)
   21921              : {
   21922        15938 :   unsigned index = import_entity_index (decl, flexible);
   21923        15938 :   if (index == ~(~0u >> 1))
   21924              :     return -1;
   21925        15938 :   module_state *module = import_entity_module (index);
   21926              : 
   21927        15938 :   return module->mod;
   21928              : }
   21929              : 
   21930              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21931              : 
   21932              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21933              :    the current scope's module and attachment.  */
   21934              : 
   21935              : bool
   21936       195929 : module_may_redeclare (tree olddecl, tree newdecl)
   21937              : {
   21938       195929 :   tree decl = olddecl;
   21939       265683 :   for (;;)
   21940              :     {
   21941       230806 :       tree ctx = CP_DECL_CONTEXT (decl);
   21942       230806 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21943              :         // Found the namespace-scope decl.
   21944              :         break;
   21945        36251 :       if (!CLASS_TYPE_P (ctx))
   21946              :         // We've met a non-class scope.  Such a thing is not
   21947              :         // reopenable, so we must be ok.
   21948              :         return true;
   21949        34877 :       decl = TYPE_NAME (ctx);
   21950        34877 :     }
   21951              : 
   21952       194555 :   int use_tpl = 0;
   21953       194555 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   21954              :     // Specializations of any kind can be redeclared anywhere.
   21955              :     // FIXME: Should we be checking this in more places on the scope chain?
   21956              :     return true;
   21957              : 
   21958       139526 :   module_state *old_mod = get_primary (this_module ());
   21959       139526 :   module_state *new_mod = old_mod;
   21960              : 
   21961       139526 :   tree old_origin = get_originating_module_decl (decl);
   21962       139526 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   21963       139526 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   21964       214353 :                              && DECL_MODULE_ATTACH_P (old_inner));
   21965       214353 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21966              :     {
   21967          843 :       unsigned index = import_entity_index (old_origin);
   21968          843 :       old_mod = get_primary (import_entity_module (index));
   21969              :     }
   21970              : 
   21971       139526 :   bool newdecl_attached_p = module_attach_p ();
   21972       139526 :   if (newdecl)
   21973              :     {
   21974        35267 :       tree new_origin = get_originating_module_decl (newdecl);
   21975        35267 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   21976        35267 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   21977        68331 :                             && DECL_MODULE_ATTACH_P (new_inner));
   21978        68331 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   21979              :         {
   21980          175 :           unsigned index = import_entity_index (new_origin);
   21981          175 :           new_mod = get_primary (import_entity_module (index));
   21982              :         }
   21983              :     }
   21984              : 
   21985              :   /* Module attachment needs to match.  */
   21986       139526 :   if (olddecl_attached_p == newdecl_attached_p)
   21987              :     {
   21988       139382 :       if (!olddecl_attached_p)
   21989              :         /* Both are GM entities, OK.  */
   21990              :         return true;
   21991              : 
   21992         1425 :       if (new_mod == old_mod)
   21993              :         /* Both attached to same named module, OK.  */
   21994              :         return true;
   21995              :     }
   21996              : 
   21997              :   /* Attached to different modules, error.  */
   21998          171 :   decl = newdecl ? newdecl : olddecl;
   21999          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22000          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22001              :     {
   22002            3 :       if (newdecl_attached_p)
   22003            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22004              :                   "in global module", decl, new_mod->get_flatname ());
   22005              :       else
   22006            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22007              :     }
   22008          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22009              :     {
   22010          153 :       auto_diagnostic_group d;
   22011          153 :       if (newdecl_attached_p)
   22012           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22013              :                   decl, new_mod->get_flatname ());
   22014              :       else
   22015           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22016              :                   decl);
   22017              : 
   22018          153 :       if (olddecl_attached_p)
   22019          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22020              :                 "import declared attached to module %qs",
   22021              :                 old_mod->get_flatname ());
   22022              :       else
   22023           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22024              :                 "import declared in global module");
   22025          153 :     }
   22026              :   else
   22027              :     {
   22028           15 :       auto_diagnostic_group d;
   22029           15 :       if (newdecl_attached_p)
   22030           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22031              :                   decl, new_mod->get_flatname ());
   22032              :       else
   22033            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22034              :                   decl);
   22035              : 
   22036           15 :       if (olddecl_attached_p)
   22037            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22038              :                 "previously declared in module %qs",
   22039              :                 old_mod->get_flatname ());
   22040              :       else
   22041           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22042              :                 "previously declared in global module");
   22043           15 :     }
   22044              :   return false;
   22045              : }
   22046              : 
   22047              : /* DECL is being created by this TU.  Record it came from here.  We
   22048              :    record module purview, so we can see if partial or explicit
   22049              :    specialization needs to be written out, even though its purviewness
   22050              :    comes from the most general template.  */
   22051              : 
   22052              : void
   22053    929384678 : set_instantiating_module (tree decl)
   22054              : {
   22055    929384678 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22056              :               || VAR_P (decl)
   22057              :               || TREE_CODE (decl) == TYPE_DECL
   22058              :               || TREE_CODE (decl) == CONCEPT_DECL
   22059              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22060              :               || TREE_CODE (decl) == CONST_DECL
   22061              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22062              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22063              : 
   22064    929384678 :   if (!modules_p ())
   22065              :     return;
   22066              : 
   22067      3600042 :   decl = STRIP_TEMPLATE (decl);
   22068              : 
   22069      3600042 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22070       362610 :     retrofit_lang_decl (decl);
   22071              : 
   22072      3600042 :   if (DECL_LANG_SPECIFIC (decl))
   22073              :     {
   22074      2769495 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22075              :       /* If this was imported, we'll still be in the entity_hash.  */
   22076      2769495 :       DECL_MODULE_IMPORT_P (decl) = false;
   22077              :     }
   22078              : }
   22079              : 
   22080              : /* If DECL is a class member, whose class is not defined in this TU
   22081              :    (it was imported), remember this decl.  */
   22082              : 
   22083              : void
   22084       143647 : set_defining_module (tree decl)
   22085              : {
   22086       143647 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22087              :                        || !DECL_MODULE_IMPORT_P (decl));
   22088              : 
   22089       143647 :   if (module_maybe_has_cmi_p ())
   22090              :     {
   22091              :       /* We need to track all declarations within a module, not just those
   22092              :          in the module purview, because we don't necessarily know yet if
   22093              :          this module will require a CMI while in the global fragment.  */
   22094        97479 :       tree ctx = DECL_CONTEXT (decl);
   22095        97479 :       if (ctx
   22096        97479 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22097        20604 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22098       108744 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22099              :         {
   22100              :           /* This entity's context is from an import.  We may need to
   22101              :              record this entity to make sure we emit it in the CMI.
   22102              :              Template specializations are in the template hash tables,
   22103              :              so we don't need to record them here as well.  */
   22104           39 :           int use_tpl = -1;
   22105           39 :           tree ti = node_template_info (decl, use_tpl);
   22106           39 :           if (use_tpl <= 0)
   22107              :             {
   22108           39 :               if (ti)
   22109              :                 {
   22110           21 :                   gcc_checking_assert (!use_tpl);
   22111              :                   /* Get to the TEMPLATE_DECL.  */
   22112           21 :                   decl = TI_TEMPLATE (ti);
   22113              :                 }
   22114              : 
   22115              :               /* Record it on the class_members list.  */
   22116           39 :               vec_safe_push (class_members, decl);
   22117              :             }
   22118              :         }
   22119              :     }
   22120       143647 : }
   22121              : 
   22122              : /* Also remember DECL if it's a newly declared class template partial
   22123              :    specialization, because these are not necessarily added to the
   22124              :    instantiation tables.  */
   22125              : 
   22126              : void
   22127      8002083 : set_defining_module_for_partial_spec (tree decl)
   22128              : {
   22129      8002083 :   if (module_maybe_has_cmi_p ()
   22130        24065 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22131      8022469 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22132        20386 :     vec_safe_push (partial_specializations, decl);
   22133      8002083 : }
   22134              : 
   22135              : /* Record that DECL is declared in this TU, and note attachment and
   22136              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22137              :    this is a friend declaration.  */
   22138              : 
   22139              : void
   22140    302059887 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22141              : {
   22142    302059887 :   set_instantiating_module (decl);
   22143              : 
   22144              :   /* DECL_CONTEXT may not be set yet when we're called for
   22145              :      non-namespace-scope entities.  */
   22146    302059887 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22147              :     return;
   22148              : 
   22149    112394655 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22150              : 
   22151    112394655 :   if (module_attach_p ())
   22152              :     {
   22153         4270 :       retrofit_lang_decl (decl);
   22154         4270 :       DECL_MODULE_ATTACH_P (decl) = true;
   22155              :     }
   22156              : 
   22157              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22158              :      at the point this function is called we don't yet always know whether this
   22159              :      declaration has internal linkage; instead we defer this check for callers
   22160              :      to do once visibility has been determined.  */
   22161    112394655 :   if (module_exporting_p ())
   22162       154670 :     DECL_MODULE_EXPORT_P (decl) = true;
   22163              : }
   22164              : 
   22165              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22166              :    Must be called after visibility for DECL has been finalised.  */
   22167              : 
   22168              : void
   22169    378848720 : check_module_decl_linkage (tree decl)
   22170              : {
   22171    378848720 :   if (!module_has_cmi_p ())
   22172              :     return;
   22173              : 
   22174              :   /* A header unit shall not contain a definition of a non-inline function
   22175              :      or variable (not template) whose name has external linkage.  */
   22176       522589 :   if (header_module_p ()
   22177       471083 :       && !processing_template_decl
   22178       155669 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22179        92212 :            && !DECL_DECLARED_INLINE_P (decl))
   22180       120278 :           || (TREE_CODE (decl) == VAR_DECL
   22181        36227 :               && !DECL_INLINE_VAR_P (decl)))
   22182        44489 :       && decl_defined_p (decl)
   22183         4380 :       && !(DECL_LANG_SPECIFIC (decl)
   22184         4380 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22185       526969 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22186           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22187              :               "external linkage definition of %qD in header module must "
   22188              :               "be declared %<inline%>", decl);
   22189              : 
   22190              :   /* An internal-linkage declaration cannot be generally be exported.
   22191              :      But it's OK to export any declaration from a header unit, including
   22192              :      internal linkage declarations.  */
   22193       522589 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22194              :     {
   22195              :       /* Let's additionally treat any exported declaration within an
   22196              :          internal namespace as exporting a declaration with internal
   22197              :          linkage, as this would also implicitly export the internal
   22198              :          linkage namespace.  */
   22199         1976 :       if (decl_anon_ns_mem_p (decl))
   22200              :         {
   22201           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22202              :                     "exporting declaration %qD declared in unnamed namespace",
   22203              :                     decl);
   22204           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22205              :         }
   22206         1926 :       else if (decl_linkage (decl) == lk_internal)
   22207              :         {
   22208           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22209              :                     "exporting declaration %qD with internal linkage", decl);
   22210           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22211              :         }
   22212              :     }
   22213              : }
   22214              : 
   22215              : /* Given a scope CTX, find the scope we want to attach the key to,
   22216              :    or NULL if no key scope is required.  */
   22217              : 
   22218              : static tree
   22219         2804 : adjust_key_scope (tree ctx)
   22220              : {
   22221              :   /* For members, key it to the containing type to handle deduplication
   22222              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22223              :      dep and so would only be streamed after the lambda type, defeating
   22224              :      our ability to merge them.
   22225              : 
   22226              :      Other class-scope key decls might depend on the type of the lambda
   22227              :      but be within the same cluster; we need to ensure that we never
   22228              :      first see the key decl while streaming the lambda type as merging
   22229              :      would then fail when comparing the partially-streamed lambda type
   22230              :      of the key decl with the existing (PR c++/122310).
   22231              : 
   22232              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22233              :      this is a simple workaround (and might down on the number of
   22234              :      entries in keyed_table as a bonus).  */
   22235         4685 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22236         3762 :     if (DECL_CLASS_SCOPE_P (ctx))
   22237         1767 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22238              :     else
   22239          114 :       ctx = DECL_CONTEXT (ctx);
   22240              : 
   22241         2804 :   return ctx;
   22242              : }
   22243              : 
   22244              : /* DECL is keyed to CTX for odr purposes.  */
   22245              : 
   22246              : void
   22247      1708850 : maybe_key_decl (tree ctx, tree decl)
   22248              : {
   22249      1708850 :   if (!modules_p ())
   22250              :     return;
   22251              : 
   22252              :   /* We only need to deal here with decls attached to var, field,
   22253              :      parm, type, function, or concept decls.  */
   22254        11660 :   if (TREE_CODE (ctx) != VAR_DECL
   22255        11660 :       && TREE_CODE (ctx) != FIELD_DECL
   22256              :       && TREE_CODE (ctx) != PARM_DECL
   22257              :       && TREE_CODE (ctx) != TYPE_DECL
   22258              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22259              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22260              :     return;
   22261              : 
   22262        23317 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22263              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22264              : 
   22265              :   /* We don't need to use the keyed map for functions with definitions,
   22266              :      as we can instead use the MK_local_type handling for streaming.  */
   22267        11660 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22268        11660 :       && (has_definition (ctx)
   22269              :           /* If we won't be streaming this definition there's also no
   22270              :              need to record the key, as it will not be useful for merging
   22271              :              (this function is non-inline and so a matching declaration
   22272              :              will always be an ODR violation anyway).  */
   22273          120 :           || !module_maybe_has_cmi_p ()))
   22274              :     return;
   22275              : 
   22276          648 :   ctx = adjust_key_scope (ctx);
   22277              : 
   22278          648 :   if (!keyed_table)
   22279          158 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22280              : 
   22281          648 :   auto &vec = keyed_table->get_or_insert (ctx);
   22282          648 :   if (!vec.length ())
   22283              :     {
   22284          596 :       retrofit_lang_decl (ctx);
   22285          596 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22286              :     }
   22287          648 :   if (CHECKING_P)
   22288          810 :     for (tree t : vec)
   22289           58 :       gcc_checking_assert (t != decl);
   22290              : 
   22291          648 :   vec.safe_push (decl);
   22292              : }
   22293              : 
   22294              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22295              : 
   22296              : static tree
   22297         2192 : get_keyed_decl_scope (tree decl)
   22298              : {
   22299         2192 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22300              : 
   22301         6576 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22302         4156 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22303         2192 :                 : CP_DECL_CONTEXT (decl));
   22304         2192 :   if (!scope)
   22305              :     return NULL_TREE;
   22306              : 
   22307         2156 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22308              :                        || TREE_CODE (scope) == FIELD_DECL
   22309              :                        || TREE_CODE (scope) == PARM_DECL
   22310              :                        || TREE_CODE (scope) == TYPE_DECL
   22311              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22312              :                            && !has_definition (scope))
   22313              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22314              : 
   22315         2156 :   scope = adjust_key_scope (scope);
   22316              : 
   22317         4312 :   gcc_checking_assert (scope
   22318              :                        && DECL_LANG_SPECIFIC (scope)
   22319              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22320              :   return scope;
   22321              : }
   22322              : 
   22323              : /* DECL is an instantiated friend that should be attached to the same
   22324              :    module that ORIG is.  */
   22325              : 
   22326              : void
   22327      2488191 : propagate_defining_module (tree decl, tree orig)
   22328              : {
   22329      2488191 :   if (!modules_p ())
   22330              :     return;
   22331              : 
   22332         7233 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22333        14416 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22334              :     {
   22335          126 :       tree inner = STRIP_TEMPLATE (decl);
   22336          126 :       retrofit_lang_decl (inner);
   22337          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22338              :     }
   22339              : 
   22340        14416 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22341              :     {
   22342          387 :       bool exists = imported_temploid_friends->put (decl, orig);
   22343              : 
   22344              :       /* We should only be called if lookup for an existing decl
   22345              :          failed, in which case there shouldn't already be an entry
   22346              :          in the map.  */
   22347          387 :       gcc_assert (!exists);
   22348              :     }
   22349              : }
   22350              : 
   22351              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22352              :    onto OLDDECL.  We've already validated attachment matches.  */
   22353              : 
   22354              : void
   22355     19428902 : transfer_defining_module (tree olddecl, tree newdecl)
   22356              : {
   22357     19428902 :   if (!modules_p ())
   22358              :     return;
   22359              : 
   22360        59030 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22361        59030 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22362              : 
   22363        59030 :   if (DECL_LANG_SPECIFIC (new_inner))
   22364              :     {
   22365        58890 :       gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
   22366        58890 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22367        22359 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22368        58890 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22369        58890 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22370              :     }
   22371              : 
   22372        59030 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22373              :     {
   22374           70 :       tree orig = *p;
   22375           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22376           70 :       if (!slot)
   22377           47 :         slot = orig;
   22378           23 :       else if (slot != orig)
   22379              :         /* This can happen when multiple classes declare the same
   22380              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22381              :            make sure we at least attach to the same module.  */
   22382            3 :         gcc_checking_assert (get_originating_module (slot)
   22383              :                              == get_originating_module (orig));
   22384              :     }
   22385              : }
   22386              : 
   22387              : /* DECL is being freed, clear data we don't need anymore.  */
   22388              : 
   22389              : void
   22390        41852 : remove_defining_module (tree decl)
   22391              : {
   22392        41852 :   if (!modules_p ())
   22393              :     return;
   22394              : 
   22395        41852 :   if (imported_temploid_friends)
   22396        41852 :     imported_temploid_friends->remove (decl);
   22397              : }
   22398              : 
   22399              : /* Create the flat name string.  It is simplest to have it handy.  */
   22400              : 
   22401              : void
   22402         6356 : module_state::set_flatname ()
   22403              : {
   22404         6356 :   gcc_checking_assert (!flatname);
   22405         6356 :   if (parent)
   22406              :     {
   22407          677 :       auto_vec<tree,5> ids;
   22408          677 :       size_t len = 0;
   22409          677 :       char const *primary = NULL;
   22410          677 :       size_t pfx_len = 0;
   22411              : 
   22412          677 :       for (module_state *probe = this;
   22413         1657 :            probe;
   22414          980 :            probe = probe->parent)
   22415         1489 :         if (is_partition () && !probe->is_partition ())
   22416              :           {
   22417          509 :             primary = probe->get_flatname ();
   22418          509 :             pfx_len = strlen (primary);
   22419          509 :             break;
   22420              :           }
   22421              :         else
   22422              :           {
   22423          980 :             ids.safe_push (probe->name);
   22424          980 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22425              :           }
   22426              : 
   22427          677 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22428          677 :       flatname = flat;
   22429              : 
   22430          677 :       if (primary)
   22431              :         {
   22432          509 :           memcpy (flat, primary, pfx_len);
   22433          509 :           flat += pfx_len;
   22434          509 :           *flat++ = ':';
   22435              :         }
   22436              : 
   22437         1657 :       for (unsigned len = 0; ids.length ();)
   22438              :         {
   22439          980 :           if (len)
   22440          303 :             flat[len++] = '.';
   22441          980 :           tree elt = ids.pop ();
   22442          980 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22443          980 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22444          980 :           len += l;
   22445              :         }
   22446          677 :     }
   22447         5679 :   else if (is_header ())
   22448         1913 :     flatname = TREE_STRING_POINTER (name);
   22449              :   else
   22450         3766 :     flatname = IDENTIFIER_POINTER (name);
   22451         6356 : }
   22452              : 
   22453              : /* Open the GCM file and prepare to read.  Return whether that was
   22454              :    successful.  */
   22455              : 
   22456              : bool
   22457         3066 : module_state::open_slurp (cpp_reader *reader)
   22458              : {
   22459         3066 :   if (slurp)
   22460              :     return true;
   22461              : 
   22462         3020 :   if (lazy_open >= lazy_limit)
   22463            9 :     freeze_an_elf ();
   22464              : 
   22465         3020 :   int fd = -1;
   22466         3020 :   int e = ENOENT;
   22467         3020 :   if (filename)
   22468              :     {
   22469         3020 :       const char *file = maybe_add_cmi_prefix (filename);
   22470         3549 :       dump () && dump ("CMI is %s", file);
   22471         3020 :       if (note_module_cmi_yes || inform_cmi_p)
   22472           12 :         inform (loc, "reading CMI %qs", file);
   22473              :       /* Add the CMI file to the dependency tracking. */
   22474         3020 :       if (cpp_get_deps (reader))
   22475           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22476         3020 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22477         3020 :       e = errno;
   22478              :     }
   22479              : 
   22480         3020 :   gcc_checking_assert (!slurp);
   22481         6016 :   slurp = new slurping (new elf_in (fd, e));
   22482              : 
   22483         3020 :   bool ok = from ()->begin (loc);
   22484         3020 :   if (ok)
   22485              :     {
   22486         2996 :       lazy_open++;
   22487         2996 :       slurp->lru = ++lazy_lru;
   22488              :     }
   22489              :   return ok;
   22490              : }
   22491              : 
   22492              : /* Return whether importing this GCM would work without an error in
   22493              :    read_config.  */
   22494              : 
   22495              : bool
   22496           52 : module_state::check_importable (cpp_reader *reader)
   22497              : {
   22498           52 :   if (loadedness > ML_CONFIG)
   22499              :     return true;
   22500           49 :   if (!open_slurp (reader))
   22501              :     return false;
   22502           46 :   module_state_config config;
   22503           46 :   return read_config (config, /*complain*/false);
   22504              : }
   22505              : 
   22506              : /* Read the CMI file for a module.  */
   22507              : 
   22508              : bool
   22509         3017 : module_state::do_import (cpp_reader *reader, bool outermost)
   22510              : {
   22511         3017 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22512              : 
   22513              :   /* If this TU is a partition of the module we're importing,
   22514              :      that module is the primary module interface.  */
   22515         3017 :   if (this_module ()->is_partition ()
   22516         3068 :       && this == get_primary (this_module ()))
   22517            9 :     module_p = true;
   22518              : 
   22519         3017 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22520              : 
   22521         3017 :   bool ok = open_slurp (reader);
   22522         3017 :   if (!from ()->get_error ())
   22523              :     {
   22524         2996 :       announce ("importing");
   22525         2996 :       loadedness = ML_CONFIG;
   22526         2996 :       ok = read_initial (reader);
   22527              :     }
   22528              : 
   22529         3017 :   gcc_assert (slurp->current == ~0u);
   22530              : 
   22531         3017 :   return check_read (outermost, ok);
   22532              : }
   22533              : 
   22534              : /* Attempt to increase the file descriptor limit.  */
   22535              : 
   22536              : static bool
   22537         4919 : try_increase_lazy (unsigned want)
   22538              : {
   22539         4919 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22540              : 
   22541              :   /* If we're increasing, saturate at hard limit.  */
   22542         4919 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22543         4919 :     want = lazy_hard_limit;
   22544              : 
   22545              : #if HAVE_SETRLIMIT
   22546         4919 :   if ((!lazy_limit || !param_lazy_modules)
   22547         4907 :       && lazy_hard_limit
   22548         4907 :       && want <= lazy_hard_limit)
   22549              :     {
   22550         4907 :       struct rlimit rlimit;
   22551         4907 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22552         4907 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22553         4907 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22554         4907 :         lazy_limit = want;
   22555              :     }
   22556              : #endif
   22557              : 
   22558         4919 :   return lazy_open < lazy_limit;
   22559              : }
   22560              : 
   22561              : /* Pick a victim module to freeze its reader.  */
   22562              : 
   22563              : void
   22564           12 : module_state::freeze_an_elf ()
   22565              : {
   22566           12 :   if (try_increase_lazy (lazy_open * 2))
   22567              :     return;
   22568              : 
   22569           12 :   module_state *victim = NULL;
   22570           12 :   for (unsigned ix = modules->length (); ix--;)
   22571              :     {
   22572           30 :       module_state *candidate = (*modules)[ix];
   22573           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22574           60 :           && candidate->from ()->is_freezable ()
   22575           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22576              :         victim = candidate;
   22577              :     }
   22578              : 
   22579           12 :   if (victim)
   22580              :     {
   22581           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22582            9 :       if (victim->slurp->macro_defs.size)
   22583              :         /* Save the macro definitions to a buffer.  */
   22584            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22585            9 :       if (victim->slurp->macro_tbl.size)
   22586              :         /* Save the macro definitions to a buffer.  */
   22587            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22588            9 :       victim->from ()->freeze ();
   22589            9 :       lazy_open--;
   22590              :     }
   22591              :   else
   22592            3 :     dump () && dump ("No module available for freezing");
   22593              : }
   22594              : 
   22595              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22596              : 
   22597              : bool
   22598        58715 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22599              : {
   22600        58715 :   unsigned n = dump.push (this);
   22601              : 
   22602        58715 :   gcc_checking_assert (function_depth);
   22603              : 
   22604        58715 :   unsigned cookie = mslot->get_lazy ();
   22605        58715 :   unsigned snum = cookie >> 2;
   22606        59095 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22607              : 
   22608        58715 :   bool ok = load_section (snum, mslot);
   22609              : 
   22610        58715 :   dump.pop (n);
   22611              : 
   22612        58715 :   return ok;
   22613              : }
   22614              : 
   22615              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22616              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22617              :    for diagnostics).  */
   22618              : 
   22619              : void
   22620         5868 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22621              : {
   22622         5868 :   int count = errorcount + warningcount;
   22623              : 
   22624         5868 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22625              : 
   22626              :   /* Make sure lazy loading from a template context behaves as if
   22627              :      from a non-template context.  */
   22628         5868 :   processing_template_decl_sentinel ptds;
   22629              : 
   22630              :   /* Stop GC happening, even in outermost loads (because our caller
   22631              :      could well be building up a lookup set).  */
   22632         5868 :   function_depth++;
   22633              : 
   22634         5868 :   gcc_checking_assert (mod);
   22635         5868 :   module_state *module = (*modules)[mod];
   22636         5868 :   unsigned n = dump.push (module);
   22637              : 
   22638         5868 :   unsigned snum = mslot->get_lazy ();
   22639         6184 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22640              :                    module->name, snum);
   22641              : 
   22642         5868 :   bool ok = !recursive_lazy (snum);
   22643         5868 :   if (ok)
   22644              :     {
   22645         5868 :       ok = module->load_section (snum, mslot);
   22646         5868 :       lazy_snum = 0;
   22647         5868 :       post_load_processing ();
   22648              :     }
   22649              : 
   22650         5868 :   dump.pop (n);
   22651              : 
   22652         5868 :   function_depth--;
   22653              : 
   22654         5868 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22655              : 
   22656         5868 :   if (!ok)
   22657            0 :     fatal_error (input_location,
   22658            0 :                  module->is_header ()
   22659              :                  ? G_("failed to load binding %<%E%s%E%>")
   22660              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22661            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22662              :                  module->get_flatname ());
   22663              : 
   22664         5868 :   if (count != errorcount + warningcount)
   22665           27 :     inform (input_location,
   22666           27 :             module->is_header ()
   22667              :             ? G_("during load of binding %<%E%s%E%>")
   22668              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22669           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22670              :             module->get_flatname ());
   22671         5868 : }
   22672              : 
   22673              : /* Load any pending entities keyed to NS and NAME.
   22674              :    Used to find pending types if we don't yet have a decl built.  */
   22675              : 
   22676              : void
   22677     36128324 : lazy_load_pendings (tree ns, tree name)
   22678              : {
   22679              :   /* Make sure lazy loading from a template context behaves as if
   22680              :      from a non-template context.  */
   22681     36128324 :   processing_template_decl_sentinel ptds;
   22682              : 
   22683     36128324 :   pending_key key;
   22684     36128324 :   key.ns = ns;
   22685     36128324 :   key.id = name;
   22686              : 
   22687     36128324 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22688     36123413 :   if (!pending_vec)
   22689     36122247 :     return;
   22690              : 
   22691         6077 :   int count = errorcount + warningcount;
   22692              : 
   22693         6077 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22694         6077 :   bool ok = !recursive_lazy ();
   22695         6077 :   if (ok)
   22696              :     {
   22697         6077 :       function_depth++; /* Prevent GC */
   22698         6077 :       unsigned n = dump.push (NULL);
   22699         6543 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22700              :                        pending_vec->length (), key.ns, key.id);
   22701         6077 :       for (unsigned ix = pending_vec->length (); ix--;)
   22702              :         {
   22703        68732 :           unsigned index = (*pending_vec)[ix];
   22704        68732 :           binding_slot *slot = &(*entity_ary)[index];
   22705              : 
   22706        68732 :           if (slot->is_lazy ())
   22707              :             {
   22708         6472 :               module_state *import = import_entity_module (index);
   22709         6472 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22710        68732 :                 ok = false;
   22711              :             }
   22712       137069 :           else if (dump ())
   22713              :             {
   22714          338 :               module_state *import = import_entity_module (index);
   22715          338 :               dump () && dump ("Entity %M[%u] already loaded",
   22716          338 :                                import, index - import->entity_lwm);
   22717              :             }
   22718              :         }
   22719              : 
   22720         6077 :       pending_table->remove (key);
   22721         6077 :       dump.pop (n);
   22722         6077 :       lazy_snum = 0;
   22723         6077 :       post_load_processing ();
   22724         6077 :       function_depth--;
   22725              :     }
   22726              : 
   22727         6077 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22728              : 
   22729         6077 :   if (!ok)
   22730            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22731            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22732              : 
   22733         6077 :   if (count != errorcount + warningcount)
   22734            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22735            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22736     36128324 : }
   22737              : 
   22738              : /* Load any pending entities keyed to the top-key of DECL.  */
   22739              : 
   22740              : void
   22741     36040303 : lazy_load_pendings (tree decl)
   22742              : {
   22743     36040303 :   tree key_decl;
   22744     36040303 :   tree ns = find_pending_key (decl, &key_decl);
   22745     36040303 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22746              : }
   22747              : 
   22748              : static void
   22749         2739 : direct_import (module_state *import, cpp_reader *reader)
   22750              : {
   22751         2739 :   timevar_start (TV_MODULE_IMPORT);
   22752         2739 :   unsigned n = dump.push (import);
   22753              : 
   22754         2739 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22755         2739 :   if (import->loadedness == ML_NONE)
   22756         1798 :     if (!import->do_import (reader, true))
   22757            0 :       gcc_unreachable ();
   22758              : 
   22759         2702 :   this_module ()->set_import (import, import->exported_p);
   22760              : 
   22761         2702 :   if (import->loadedness < ML_LANGUAGE)
   22762              :     {
   22763         2621 :       if (!keyed_table)
   22764         2302 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22765         2621 :       import->read_language (true);
   22766              :     }
   22767              : 
   22768         2702 :   dump.pop (n);
   22769         2702 :   timevar_stop (TV_MODULE_IMPORT);
   22770         2702 : }
   22771              : 
   22772              : /* Import module IMPORT.  */
   22773              : 
   22774              : void
   22775         2506 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22776              :                tree, cpp_reader *reader)
   22777              : {
   22778              :   /* A non-partition implementation unit has no name.  */
   22779         2506 :   if (!this_module ()->name && this_module ()->parent == import)
   22780              :     {
   22781            3 :       auto_diagnostic_group d;
   22782            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22783              :                 import->get_flatname());
   22784            3 :       inform (import->loc, "module declared here");
   22785            3 :       return;
   22786            3 :     }
   22787              : 
   22788         2503 :   if (!import->check_circular_import (from_loc))
   22789              :     return;
   22790              : 
   22791         2497 :   if (!import->is_header () && current_lang_depth ())
   22792              :     /* Only header units should appear inside language
   22793              :        specifications.  The std doesn't specify this, but I think
   22794              :        that's an error in resolving US 033, because language linkage
   22795              :        is also our escape clause to getting things into the global
   22796              :        module, so we don't want to confuse things by having to think
   22797              :        about whether 'extern "C++" { import foo; }' puts foo's
   22798              :        contents into the global module all of a sudden.  */
   22799            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22800              :              import->get_flatname ());
   22801              : 
   22802         2497 :   if (exporting_p || module_exporting_p ())
   22803          325 :     import->exported_p = true;
   22804              : 
   22805         2497 :   if (import->loadedness != ML_NONE)
   22806              :     {
   22807          938 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22808          938 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22809              :     }
   22810              : 
   22811         2497 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22812              : 
   22813         2497 :   direct_import (import, reader);
   22814              : }
   22815              : 
   22816              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22817              :    true if this TU is the exporting module unit.  */
   22818              : 
   22819              : void
   22820         3148 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22821              :                 tree, cpp_reader *reader)
   22822              : {
   22823         3148 :   gcc_assert (global_namespace == current_scope ());
   22824              : 
   22825         3148 :   module_state *current = this_module ();
   22826         3148 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22827              :     {
   22828            6 :       auto_diagnostic_group d;
   22829           12 :       error_at (from_loc, module_purview_p ()
   22830              :                 ? G_("module already declared")
   22831              :                 : G_("module already imported"));
   22832            6 :       if (module_purview_p ())
   22833            0 :         module = current;
   22834           12 :       inform (module->loc, module_purview_p ()
   22835              :               ? G_("module %qs declared here")
   22836              :               : G_("module %qs imported here"),
   22837              :               module->get_flatname ());
   22838            6 :       return;
   22839            6 :     }
   22840              : 
   22841         3142 :   gcc_checking_assert (module->is_module ());
   22842         3142 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22843              : 
   22844              :   /* Yer a module, 'arry.  */
   22845         3142 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22846              : 
   22847              :   // Even in header units, we consider the decls to be purview
   22848         3142 :   module_kind |= MK_PURVIEW;
   22849              : 
   22850         3142 :   if (module->is_partition ())
   22851          205 :     module_kind |= MK_PARTITION;
   22852         3142 :   if (exporting_p)
   22853              :     {
   22854         2835 :       module->interface_p = true;
   22855         2835 :       module_kind |= MK_INTERFACE;
   22856              :     }
   22857              : 
   22858         3142 :   if (module_has_cmi_p ())
   22859              :     {
   22860              :       /* Copy the importing information we may have already done.  We
   22861              :          do not need to separate out the imports that only happen in
   22862              :          the GMF, inspite of what the literal wording of the std
   22863              :          might imply.  See p2191, the core list had a discussion
   22864              :          where the module implementors agreed that the GMF of a named
   22865              :          module is invisible to importers.  */
   22866         2900 :       module->imports = current->imports;
   22867              : 
   22868         2900 :       module->mod = 0;
   22869         2900 :       (*modules)[0] = module;
   22870              :     }
   22871              :   else
   22872              :     {
   22873          242 :       module->interface_p = true;
   22874          242 :       current->parent = module; /* So mangler knows module identity. */
   22875          242 :       direct_import (module, reader);
   22876              :     }
   22877              : }
   22878              : 
   22879              : /* Return true IFF we must emit a module global initializer function
   22880              :    (which will be called by importers' init code).  */
   22881              : 
   22882              : bool
   22883       104210 : module_global_init_needed ()
   22884              : {
   22885       104210 :   return module_has_cmi_p () && !header_module_p ();
   22886              : }
   22887              : 
   22888              : /* Calculate which, if any, import initializers need calling.  */
   22889              : 
   22890              : bool
   22891        96822 : module_determine_import_inits ()
   22892              : {
   22893        96822 :   if (!modules || header_module_p ())
   22894              :     return false;
   22895              : 
   22896              :   /* Prune active_init_p.  We need the same bitmap allocation
   22897              :      scheme as for the imports member.  */
   22898         3818 :   function_depth++; /* Disable GC.  */
   22899         3818 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22900              : 
   22901         3818 :   bool any = false;
   22902              : 
   22903              :   /* Because indirect imports are before their direct import, and
   22904              :      we're scanning the array backwards, we only need one pass!  */
   22905         6651 :   for (unsigned ix = modules->length (); --ix;)
   22906              :     {
   22907         2833 :       module_state *import = (*modules)[ix];
   22908              : 
   22909         2833 :       if (!import->active_init_p)
   22910              :         ;
   22911           52 :       else if (bitmap_bit_p (covered_imports, ix))
   22912            6 :         import->active_init_p = false;
   22913              :       else
   22914              :         {
   22915              :           /* Everything this imports is therefore handled by its
   22916              :              initializer, so doesn't need initializing by us.  */
   22917           46 :           bitmap_ior_into (covered_imports, import->imports);
   22918           46 :           any = true;
   22919              :         }
   22920              :     }
   22921         3818 :   function_depth--;
   22922              : 
   22923         3818 :   return any;
   22924              : }
   22925              : 
   22926              : /* Emit calls to each direct import's global initializer.  Including
   22927              :    direct imports of directly imported header units.  The initializers
   22928              :    of (static) entities in header units will be called by their
   22929              :    importing modules (for the instance contained within that), or by
   22930              :    the current TU (for the instances we've brought in).  Of course
   22931              :    such header unit behaviour is evil, but iostream went through that
   22932              :    door some time ago.  */
   22933              : 
   22934              : void
   22935           46 : module_add_import_initializers ()
   22936              : {
   22937           46 :   if (!modules || header_module_p ())
   22938            0 :     return;
   22939              : 
   22940           46 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22941           46 :   releasing_vec args;  // There are no args
   22942              : 
   22943          104 :   for (unsigned ix = modules->length (); --ix;)
   22944              :     {
   22945           58 :       module_state *import = (*modules)[ix];
   22946           58 :       if (import->active_init_p)
   22947              :         {
   22948           46 :           tree name = mangle_module_global_init (ix);
   22949           46 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   22950              : 
   22951           46 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   22952           46 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   22953           46 :           TREE_PUBLIC (fndecl) = true;
   22954           46 :           determine_visibility (fndecl);
   22955              : 
   22956           46 :           tree call = cp_build_function_call_vec (fndecl, &args,
   22957              :                                                   tf_warning_or_error);
   22958           46 :           finish_expr_stmt (call);
   22959              :         }
   22960              :     }
   22961           46 : }
   22962              : 
   22963              : /* NAME & LEN are a preprocessed header name, possibly including the
   22964              :    surrounding "" or <> characters.  Return the raw string name of the
   22965              :    module to which it refers.  This will be an absolute path, or begin
   22966              :    with ./, so it is immediately distinguishable from a (non-header
   22967              :    unit) module name.  If READER is non-null, ask the preprocessor to
   22968              :    locate the header to which it refers using the appropriate include
   22969              :    path.  Note that we do never do \ processing of the string, as that
   22970              :    matches the preprocessor's behaviour.  */
   22971              : 
   22972              : static const char *
   22973        24543 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   22974              :                           const char *str, size_t &len_r)
   22975              : {
   22976        24543 :   size_t len = len_r;
   22977        24543 :   static char *buf = 0;
   22978        24543 :   static size_t alloc = 0;
   22979              : 
   22980        24543 :   if (!unquoted)
   22981              :     {
   22982            4 :       gcc_checking_assert (len >= 2
   22983              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   22984              :                                || (str[0] == '"' && str[len-1] == '"')));
   22985            4 :       str += 1;
   22986            4 :       len -= 2;
   22987              :     }
   22988              : 
   22989        24543 :   if (reader)
   22990              :     {
   22991            4 :       gcc_assert (!unquoted);
   22992              : 
   22993            4 :       if (len >= alloc)
   22994              :         {
   22995            4 :           alloc = len + 1;
   22996            4 :           buf = XRESIZEVEC (char, buf, alloc);
   22997              :         }
   22998            4 :       memcpy (buf, str, len);
   22999            4 :       buf[len] = 0;
   23000              : 
   23001            8 :       if (const char *hdr
   23002            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23003              :         {
   23004            4 :           len = strlen (hdr);
   23005            4 :           str = hdr;
   23006              :         }
   23007              :       else
   23008            0 :         str = buf;
   23009              :     }
   23010              : 
   23011        24543 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23012              :     {
   23013              :       /* Prepend './'  */
   23014            9 :       if (len + 3 > alloc)
   23015              :         {
   23016            9 :           alloc = len + 3;
   23017            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23018              :         }
   23019              : 
   23020            9 :       buf[0] = '.';
   23021            9 :       buf[1] = DIR_SEPARATOR;
   23022            9 :       memmove (buf + 2, str, len);
   23023            9 :       len += 2;
   23024            9 :       buf[len] = 0;
   23025            9 :       str = buf;
   23026              :     }
   23027              : 
   23028        24543 :   len_r = len;
   23029        24543 :   return str;
   23030              : }
   23031              : 
   23032              : /* Set the CMI name from a cody packet.  Issue an error if
   23033              :    ill-formed.  */
   23034              : 
   23035         5772 : void module_state::set_filename (const Cody::Packet &packet)
   23036              : {
   23037         5772 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23038              :     {
   23039              :       /* If we've seen this import before we better have the same CMI.  */
   23040         5769 :       const std::string &path = packet.GetString ();
   23041         5769 :       if (!filename)
   23042         5766 :         filename = xstrdup (packet.GetString ().c_str ());
   23043            3 :       else if (filename != path)
   23044            0 :         error_at (loc, "mismatching compiled module interface: "
   23045              :                   "had %qs, got %qs", filename, path.c_str ());
   23046              :     }
   23047              :   else
   23048              :     {
   23049            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23050            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23051            3 :                    packet.GetString ().c_str ());
   23052              :     }
   23053         5769 : }
   23054              : 
   23055              : /* The list of importable headers from C++ Table 24.  */
   23056              : 
   23057              : static const char *
   23058              : importable_headers[] =
   23059              :   {
   23060              :     "algorithm", "any", "array", "atomic",
   23061              :     "barrier", "bit", "bitset",
   23062              :     "charconv", "chrono", "compare", "complex", "concepts",
   23063              :     "condition_variable", "contracts", "coroutine",
   23064              :     "debugging", "deque",
   23065              :     "exception", "execution", "expected",
   23066              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23067              :     "fstream", "functional", "future",
   23068              :     "generator",
   23069              :     "hazard_pointer", "hive",
   23070              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23071              :     "iostream", "istream", "iterator",
   23072              :     "latch", "limits", "linalg", "list", "locale",
   23073              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23074              :     "new", "numbers", "numeric",
   23075              :     "optional", "ostream",
   23076              :     "print",
   23077              :     "queue",
   23078              :     "random", "ranges", "ratio", "rcu", "regex",
   23079              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23080              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23081              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23082              :     "string_view", "syncstream", "system_error",
   23083              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23084              :     "unordered_map", "unordered_set",
   23085              :     "utility",
   23086              :     "valarray", "variant", "vector", "version"
   23087              :   };
   23088              : 
   23089              : /* True iff <name> is listed as an importable standard header.  */
   23090              : 
   23091              : static bool
   23092        22290 : is_importable_header (const char *name)
   23093              : {
   23094        22290 :   unsigned lo = 0;
   23095        22290 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23096       169008 :   while (hi > lo)
   23097              :     {
   23098       149370 :       unsigned mid = (lo + hi)/2;
   23099       149370 :       int cmp = strcmp (name, importable_headers[mid]);
   23100       149370 :       if (cmp > 0)
   23101        34667 :         lo = mid + 1;
   23102       114703 :       else if (cmp < 0)
   23103              :         hi = mid;
   23104              :       else
   23105              :         return true;
   23106              :     }
   23107              :   return false;
   23108              : }
   23109              : 
   23110              : /* Figure out whether to treat HEADER as an include or an import.  */
   23111              : 
   23112              : static char *
   23113        23616 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23114              :                          _cpp_file *file, bool angle, const char **alternate)
   23115              : {
   23116        23616 :   if (!modules_p ())
   23117              :     {
   23118              :       /* Turn off.  */
   23119            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23120            0 :       return nullptr;
   23121              :     }
   23122              : 
   23123        23616 :   const char *path = _cpp_get_file_path (file);
   23124              : 
   23125        23616 :   dump.push (NULL);
   23126              : 
   23127        25135 :   dump () && dump ("Checking include translation '%s'", path);
   23128        23616 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23129              : 
   23130        23616 :   size_t len = strlen (path);
   23131        23616 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23132        23616 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23133              : 
   23134        23616 :   enum class xlate_kind {
   23135              :     unknown, text, import, invalid
   23136        23616 :   } translate = xlate_kind::unknown;
   23137              : 
   23138        23616 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23139        23567 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23140           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23141              :     {
   23142              :       /* Record the CMI name for when we do the import.
   23143              :          We may already know about this import, but libcpp doesn't yet.  */
   23144           49 :       module_state *import = get_module (build_string (len, path));
   23145           49 :       import->set_filename (packet);
   23146           49 :       if (import->check_importable (reader))
   23147              :         translate = xlate_kind::import;
   23148              :       else
   23149            0 :         translate = xlate_kind::invalid;
   23150              :     }
   23151              :   else
   23152              :     {
   23153            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23154            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23155            0 :                 path, packet.GetString ().c_str ());
   23156              :     }
   23157              : 
   23158        23616 :   bool note = (translate == xlate_kind::invalid);
   23159        23616 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23160              :     note = true;
   23161        23611 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23162              :     note = true;
   23163        23608 :   else if (note_includes)
   23164              :     /* We do not expect the note_includes vector to be large, so O(N)
   23165              :        iteration.  */
   23166          432 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23167          216 :       if (!strcmp ((*note_includes)[ix], path))
   23168            1 :         note = true;
   23169              : 
   23170              :   /* Maybe try importing a different header instead.  */
   23171        23616 :   if (alternate && translate == xlate_kind::unknown)
   23172              :     {
   23173        23176 :       const char *fname = _cpp_get_file_name (file);
   23174              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23175              :       /* ??? Generalize to use a .json.  */
   23176        23176 :       expanded_location eloc = expand_location (loc);
   23177        25814 :       auto indir = [](const char *f, const char *d)
   23178              :       {
   23179         2638 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23180              :         /* Also check canonical paths (c++/123879).  */
   23181          880 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23182          880 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23183          880 :         free (cf); free (cd);
   23184          880 :         return r;
   23185              :       };
   23186        22290 :       if (angle && is_importable_header (fname)
   23187              :           /* Exclude <version> which often goes with import std.  */
   23188         2652 :           && strcmp (fname, "version") != 0
   23189              :           /* Don't redirect #includes between headers under the same include
   23190              :              path directory (i.e. between library headers); if the import
   23191              :              brings in the current file we then get redefinition errors.  */
   23192         2638 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23193              :           /* ??? These are needed when running a toolchain from the build
   23194              :              directory, because libsupc++ headers aren't linked into
   23195              :              libstdc++-v3/include with the other headers.  */
   23196          809 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23197        23646 :           && !strstr (eloc.file, "libsupc++"))
   23198          394 :         *alternate = "bits/stdc++.h";
   23199              :     }
   23200              : 
   23201        23616 :   if (note)
   23202           12 :     inform (loc, translate == xlate_kind::import
   23203              :             ? G_("include %qs translated to import")
   23204              :             : translate == xlate_kind::invalid
   23205            3 :             ? G_("import of %qs failed, falling back to include")
   23206              :             : G_("include %qs processed textually"), path);
   23207              : 
   23208        26651 :   dump () && dump (translate == xlate_kind::import
   23209              :                    ? "Translating include to import"
   23210              :                    : "Keeping include as include");
   23211        23616 :   dump.pop (0);
   23212              : 
   23213        23616 :   if (translate != xlate_kind::import)
   23214              :     return nullptr;
   23215              : 
   23216              :   /* Create the translation text.  */
   23217           49 :   loc = ordinary_loc_of (lmaps, loc);
   23218           49 :   const line_map_ordinary *map
   23219           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23220           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23221           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23222              : 
   23223           49 :   unsigned alloc = len + col + 60;
   23224           49 :   char *res = XNEWVEC (char, alloc);
   23225              : 
   23226           49 :   strcpy (res, "__import");
   23227           49 :   unsigned actual = 8;
   23228           49 :   if (col > actual)
   23229              :     {
   23230              :       /* Pad out so the filename appears at the same position.  */
   23231           46 :       memset (res + actual, ' ', col - actual);
   23232           46 :       actual = col;
   23233              :     }
   23234              :   /* No need to encode characters, that's not how header names are
   23235              :      handled.  */
   23236           49 :   actual += snprintf (res + actual, alloc - actual,
   23237              :                       "\"%s\" [[__translated]];\n", path);
   23238           49 :   gcc_checking_assert (actual < alloc);
   23239              : 
   23240              :   /* cpplib will delete the buffer.  */
   23241              :   return res;
   23242        23616 : }
   23243              : 
   23244              : static void
   23245          923 : begin_header_unit (cpp_reader *reader)
   23246              : {
   23247              :   /* Set the module header name from the main_input_filename.  */
   23248          923 :   const char *main = main_input_filename;
   23249          923 :   size_t len = strlen (main);
   23250          923 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23251          923 :   module_state *module = get_module (build_string (len, main));
   23252              : 
   23253          923 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23254          923 : }
   23255              : 
   23256              : /* We've just properly entered the main source file.  I.e. after the
   23257              :    command line, builtins and forced headers.  Record the line map and
   23258              :    location of this map.  Note we may be called more than once.  The
   23259              :    first call sticks.  */
   23260              : 
   23261              : void
   23262        98741 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23263              :                        const line_map_ordinary *map)
   23264              : {
   23265        98741 :   gcc_checking_assert (lmaps == line_table);
   23266        98741 :   if (modules_p () && !spans.init_p ())
   23267              :     {
   23268         4907 :       unsigned n = dump.push (NULL);
   23269         4907 :       spans.init (lmaps, map);
   23270         4907 :       dump.pop (n);
   23271         4907 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23272              :         {
   23273              :           /* Tell the preprocessor this is an include file.  */
   23274          914 :           cpp_retrofit_as_include (reader);
   23275          914 :           begin_header_unit (reader);
   23276              :         }
   23277              :     }
   23278        98741 : }
   23279              : 
   23280              : /* Process the pending_import queue, making sure we know the
   23281              :    filenames.   */
   23282              : 
   23283              : static void
   23284         5814 : name_pending_imports (cpp_reader *reader)
   23285              : {
   23286         5814 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23287              : 
   23288         5814 :   if (!vec_safe_length (pending_imports))
   23289              :     /* Not doing anything.  */
   23290              :     return;
   23291              : 
   23292         4971 :   timevar_start (TV_MODULE_MAPPER);
   23293              : 
   23294         4971 :   auto n = dump.push (NULL);
   23295         5590 :   dump () && dump ("Resolving direct import names");
   23296         4971 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23297         4971 :                     || cpp_get_deps (reader));
   23298         4971 :   bool any = false;
   23299              : 
   23300        10878 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23301              :     {
   23302         5907 :       module_state *module = (*pending_imports)[ix];
   23303         5907 :       gcc_checking_assert (module->is_direct ());
   23304         5907 :       if (!module->filename && !module->visited_p)
   23305              :         {
   23306         5795 :           bool export_p = (module->is_module ()
   23307         5795 :                            && (module->is_partition ()
   23308         3015 :                                || module->is_exported ()));
   23309              : 
   23310         5795 :           Cody::Flags flags = Cody::Flags::None;
   23311         5795 :           if (flag_preprocess_only
   23312         5795 :               && !(module->is_header () && !export_p))
   23313              :             {
   23314          141 :               if (!want_deps)
   23315           72 :                 continue;
   23316              :               flags = Cody::Flags::NameOnly;
   23317              :             }
   23318              : 
   23319         5723 :           if (!any)
   23320              :             {
   23321         4897 :               any = true;
   23322         4897 :               mapper->Cork ();
   23323              :             }
   23324         5723 :           if (export_p)
   23325         2954 :             mapper->ModuleExport (module->get_flatname (), flags);
   23326              :           else
   23327         2769 :             mapper->ModuleImport (module->get_flatname (), flags);
   23328         5723 :           module->visited_p = true;
   23329              :         }
   23330              :     }
   23331              : 
   23332         4971 :   if (any)
   23333              :     {
   23334         4897 :       auto response = mapper->Uncork ();
   23335         4897 :       auto r_iter = response.begin ();
   23336        10712 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23337              :         {
   23338         5818 :           module_state *module = (*pending_imports)[ix];
   23339         5818 :           if (module->visited_p)
   23340              :             {
   23341         5723 :               module->visited_p = false;
   23342         5723 :               gcc_checking_assert (!module->filename);
   23343              : 
   23344         5723 :               module->set_filename (*r_iter);
   23345         5720 :               ++r_iter;
   23346              :             }
   23347              :         }
   23348         4894 :     }
   23349              : 
   23350         4968 :   dump.pop (n);
   23351              : 
   23352         4968 :   timevar_stop (TV_MODULE_MAPPER);
   23353              : }
   23354              : 
   23355              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23356              :    the module as a direct import, and possibly load up its macro
   23357              :    state.  Returns the primary module, if this is a module
   23358              :    declaration.  */
   23359              : /* Perhaps we should offer a preprocessing mode where we read the
   23360              :    directives from the header unit, rather than require the header's
   23361              :    CMI.  */
   23362              : 
   23363              : module_state *
   23364         5927 : preprocess_module (module_state *module, location_t from_loc,
   23365              :                    bool in_purview, bool is_import, bool is_export,
   23366              :                    cpp_reader *reader)
   23367              : {
   23368         5927 :   if (!is_import)
   23369              :     {
   23370         3304 :       if (in_purview || module->loc)
   23371              :         {
   23372              :           /* We've already seen a module declaration.  If only preprocessing
   23373              :              then we won't complain in declare_module, so complain here.  */
   23374           42 :           if (flag_preprocess_only)
   23375            6 :             error_at (from_loc,
   23376              :                       in_purview
   23377              :                       ? G_("module already declared")
   23378              :                       : G_("module already imported"));
   23379              :           /* Always pretend this was an import to aid error recovery.  */
   23380              :           is_import = true;
   23381              :         }
   23382              :       else
   23383              :         {
   23384              :           /* Record it is the module.  */
   23385         3262 :           module->module_p = true;
   23386         3262 :           if (is_export)
   23387              :             {
   23388         2937 :               module->exported_p = true;
   23389         2937 :               module->interface_p = true;
   23390              :             }
   23391              :         }
   23392              :     }
   23393              : 
   23394         5927 :   if (module->directness < MD_DIRECT + in_purview)
   23395              :     {
   23396              :       /* Mark as a direct import.  */
   23397         5880 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23398              : 
   23399              :       /* Set the location to be most informative for users.  */
   23400         5880 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23401         5880 :       if (module->loadedness != ML_NONE)
   23402            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23403              :       else
   23404              :         {
   23405              :           /* Don't overwrite the location if we're importing ourselves
   23406              :              after already having seen a module-declaration.  */
   23407         5874 :           if (!(is_import && module->is_module ()))
   23408         5844 :             module->loc = from_loc;
   23409         5874 :           if (!module->flatname)
   23410         5835 :             module->set_flatname ();
   23411              :         }
   23412              :     }
   23413              : 
   23414         5927 :   auto desired = ML_CONFIG;
   23415         5927 :   if (is_import
   23416         2665 :       && module->is_header ()
   23417         6848 :       && (!cpp_get_options (reader)->preprocessed
   23418            3 :           || cpp_get_options (reader)->directives_only))
   23419              :     /* We need preprocessor state now.  */
   23420              :     desired = ML_PREPROCESSOR;
   23421              : 
   23422         5927 :   if (!is_import || module->loadedness < desired)
   23423              :     {
   23424         5907 :       vec_safe_push (pending_imports, module);
   23425              : 
   23426         5907 :       if (desired == ML_PREPROCESSOR)
   23427              :         {
   23428          901 :           unsigned n = dump.push (NULL);
   23429              : 
   23430         1100 :           dump () && dump ("Reading %M preprocessor state", module);
   23431          901 :           name_pending_imports (reader);
   23432              : 
   23433              :           /* Preserve the state of the line-map.  */
   23434          901 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23435              : 
   23436              :           /* We only need to close the span, if we're going to emit a
   23437              :              CMI.  But that's a little tricky -- our token scanner
   23438              :              needs to be smarter -- and this isn't much state.
   23439              :              Remember, we've not parsed anything at this point, so
   23440              :              our module state flags are inadequate.  */
   23441          901 :           spans.maybe_init ();
   23442          901 :           spans.close ();
   23443              : 
   23444          901 :           timevar_start (TV_MODULE_IMPORT);
   23445              : 
   23446              :           /* Load the config of each pending import -- we must assign
   23447              :              module numbers monotonically.  */
   23448         1990 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23449              :             {
   23450         1089 :               auto *import = (*pending_imports)[ix];
   23451         1266 :               if (!(import->is_module ()
   23452          177 :                     && (import->is_partition () || import->is_exported ()))
   23453          918 :                   && import->loadedness == ML_NONE
   23454         2006 :                   && (!flag_preprocess_only
   23455           51 :                       || (import->is_header ()
   23456              :                           /* Allow a missing/unimportable GCM with -MG.
   23457              :                              FIXME We should also try falling back to #include
   23458              :                              before giving up entirely.  */
   23459           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23460            3 :                               || import->check_importable (reader)))))
   23461              :                 {
   23462          905 :                   unsigned n = dump.push (import);
   23463          905 :                   import->do_import (reader, true);
   23464          905 :                   dump.pop (n);
   23465              :                 }
   23466              :             }
   23467          901 :           vec_free (pending_imports);
   23468              : 
   23469              :           /* Restore the line-map state.  */
   23470          901 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23471              : 
   23472              :           /* Now read the preprocessor state of this particular
   23473              :              import.  */
   23474          901 :           if (module->loadedness == ML_CONFIG
   23475          901 :               && module->read_preprocessor (true))
   23476          895 :             module->import_macros ();
   23477              : 
   23478          901 :           timevar_stop (TV_MODULE_IMPORT);
   23479              : 
   23480          901 :           dump.pop (n);
   23481              :         }
   23482              :     }
   23483              : 
   23484         5927 :   return is_import ? NULL : get_primary (module);
   23485              : }
   23486              : 
   23487              : /* We've completed phase-4 translation.  Emit any dependency
   23488              :    information for the not-yet-loaded direct imports, and fill in
   23489              :    their file names.  We'll have already loaded up the direct header
   23490              :    unit wavefront.  */
   23491              : 
   23492              : void
   23493         4913 : preprocessed_module (cpp_reader *reader)
   23494              : {
   23495         4913 :   unsigned n = dump.push (NULL);
   23496              : 
   23497         5496 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23498              : 
   23499         4913 :   name_pending_imports (reader);
   23500         4910 :   vec_free (pending_imports);
   23501              : 
   23502         4910 :   spans.maybe_init ();
   23503         4910 :   spans.close ();
   23504              : 
   23505         4910 :   using iterator = hash_table<module_state_hash>::iterator;
   23506         4910 :   if (mkdeps *deps = cpp_get_deps (reader))
   23507              :     {
   23508              :       /* Walk the module hash, informing the dependency machinery.  */
   23509           57 :       iterator end = modules_hash->end ();
   23510          342 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23511              :         {
   23512          114 :           module_state *module = *iter;
   23513              : 
   23514          114 :           if (module->is_direct ())
   23515              :             {
   23516           90 :               if (module->is_module ()
   23517           90 :                   && (module->is_interface () || module->is_partition ()))
   23518           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23519           36 :                                         maybe_add_cmi_prefix (module->filename),
   23520           36 :                                         module->is_header (),
   23521           36 :                                         module->is_exported ());
   23522              :               else
   23523           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23524              :             }
   23525              :         }
   23526              :     }
   23527              : 
   23528         4910 :   if (flag_header_unit && !flag_preprocess_only)
   23529              :     {
   23530              :       /* Find the main module -- remember, it's not yet in the module
   23531              :          array.  */
   23532          911 :       iterator end = modules_hash->end ();
   23533         1930 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23534              :         {
   23535          965 :           module_state *module = *iter;
   23536          965 :           if (module->is_module ())
   23537              :             {
   23538          911 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23539          911 :               module_kind |= MK_EXPORTING;
   23540          911 :               break;
   23541              :             }
   23542              :         }
   23543              :     }
   23544              : 
   23545         4910 :   dump.pop (n);
   23546         4910 : }
   23547              : 
   23548              : /* VAL is a global tree, add it to the global vec if it is
   23549              :    interesting.  Add some of its targets, if they too are
   23550              :    interesting.  We do not add identifiers, as they can be re-found
   23551              :    via the identifier hash table.  There is a cost to the number of
   23552              :    global trees.  */
   23553              : 
   23554              : static int
   23555      3044513 : maybe_add_global (tree val, unsigned &crc)
   23556              : {
   23557      3044513 :   int v = 0;
   23558              : 
   23559      3044513 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23560              :     {
   23561       936838 :       TREE_VISITED (val) = true;
   23562       936838 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23563       936838 :       vec_safe_push (fixed_trees, val);
   23564       936838 :       v++;
   23565              : 
   23566       936838 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23567       936838 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23568       936838 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23569       614123 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23570              :     }
   23571              : 
   23572      3044513 :   return v;
   23573              : }
   23574              : 
   23575              : /* Initialize module state.  Create the hash table, determine the
   23576              :    global trees.  Create the module for current TU.  */
   23577              : 
   23578              : void
   23579         4913 : init_modules (cpp_reader *reader)
   23580              : {
   23581              :   /* PCH should not be reachable because of lang-specs, but the
   23582              :      user could have overriden that.  */
   23583         4913 :   if (pch_file)
   23584            0 :     fatal_error (input_location,
   23585              :                  "C++ modules are incompatible with precompiled headers");
   23586              : 
   23587         4913 :   if (cpp_get_options (reader)->traditional)
   23588            0 :     fatal_error (input_location,
   23589              :                  "C++ modules are incompatible with traditional preprocessing");
   23590              : 
   23591              :   /* :: is always exported.  */
   23592         4913 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23593              : 
   23594         4913 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23595         4913 :   vec_safe_reserve (modules, 20);
   23596              : 
   23597              :   /* Create module for current TU.  */
   23598         4913 :   module_state *current
   23599         4913 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23600         4913 :   current->mod = 0;
   23601         4913 :   bitmap_set_bit (current->imports, 0);
   23602         4913 :   modules->quick_push (current);
   23603              : 
   23604         4913 :   gcc_checking_assert (!fixed_trees);
   23605              : 
   23606         4913 :   headers = BITMAP_GGC_ALLOC ();
   23607              : 
   23608         4913 :   if (note_includes)
   23609              :     /* Canonicalize header names.  */
   23610            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23611              :       {
   23612            1 :         const char *hdr = (*note_includes)[ix];
   23613            1 :         size_t len = strlen (hdr);
   23614              : 
   23615            1 :         bool system = hdr[0] == '<';
   23616            1 :         bool user = hdr[0] == '"';
   23617            1 :         bool delimed = system || user;
   23618              : 
   23619            1 :         if (len <= (delimed ? 2 : 0)
   23620            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23621            0 :           error ("invalid header name %qs", hdr);
   23622              : 
   23623            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23624              :                                         0, !delimed, hdr, len);
   23625            1 :         char *path = XNEWVEC (char, len + 1);
   23626            1 :         memcpy (path, hdr, len);
   23627            1 :         path[len] = 0;
   23628              : 
   23629            1 :         (*note_includes)[ix] = path;
   23630              :       }
   23631              : 
   23632         4913 :   if (note_cmis)
   23633              :     /* Canonicalize & mark module names.  */
   23634           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23635              :       {
   23636            6 :         const char *name = (*note_cmis)[ix];
   23637            6 :         size_t len = strlen (name);
   23638              : 
   23639            6 :         bool is_system = name[0] == '<';
   23640            6 :         bool is_user = name[0] == '"';
   23641            6 :         bool is_pathname = false;
   23642            6 :         if (!(is_system || is_user))
   23643           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23644            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23645            6 :         if (is_system || is_user || is_pathname)
   23646              :           {
   23647            3 :             if (len <= (is_pathname ? 0 : 2)
   23648            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23649              :               {
   23650            0 :                 error ("invalid header name %qs", name);
   23651            0 :                 continue;
   23652              :               }
   23653              :             else
   23654            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23655              :                                                0, is_pathname, name, len);
   23656              :           }
   23657            6 :         if (auto module = get_module (name))
   23658            6 :           module->inform_cmi_p = 1;
   23659              :         else
   23660            0 :           error ("invalid module name %qs", name);
   23661              :       }
   23662              : 
   23663         4913 :   dump.push (NULL);
   23664              : 
   23665              :   /* Determine lazy handle bound.  */
   23666         4913 :   {
   23667         4913 :     unsigned limit = 1000;
   23668              : #if HAVE_GETRLIMIT
   23669         4913 :     struct rlimit rlimit;
   23670         4913 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23671              :       {
   23672         4913 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23673         4913 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23674         4913 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23675         4913 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23676         4913 :         if (rlimit.rlim_cur < limit)
   23677            0 :           limit = unsigned (rlimit.rlim_cur);
   23678              :       }
   23679              : #endif
   23680         4913 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23681              : 
   23682         4913 :     if (unsigned parm = param_lazy_modules)
   23683              :       {
   23684         4913 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23685            6 :           lazy_limit = parm;
   23686              :       }
   23687              :     else
   23688            0 :       lazy_limit = limit;
   23689              :   }
   23690              : 
   23691         4913 :   if (dump ())
   23692              :     {
   23693          583 :       verstr_t ver;
   23694          583 :       version2string (MODULE_VERSION, ver);
   23695          583 :       dump ("Source: %s", main_input_filename);
   23696          583 :       dump ("Compiler: %s", version_string);
   23697          583 :       dump ("Modules: %s", ver);
   23698          583 :       dump ("Checking: %s",
   23699              : #if CHECKING_P
   23700              :             "checking"
   23701              : #elif ENABLE_ASSERT_CHECKING
   23702              :             "asserting"
   23703              : #else
   23704              :             "release"
   23705              : #endif
   23706              :             );
   23707          583 :       dump ("Compiled by: "
   23708              : #ifdef __GNUC__
   23709              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23710              : #ifdef __OPTIMIZE__
   23711              :             "optimizing"
   23712              : #else
   23713              :             "not optimizing"
   23714              : #endif
   23715              : #else
   23716              :             "not GCC"
   23717              : #endif
   23718              :             );
   23719          583 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23720          583 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23721          583 :       dump ("Lazy limit: %u", lazy_limit);
   23722          583 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23723          583 :       dump ("");
   23724              :     }
   23725              : 
   23726              :   /* Construct the global tree array.  This is an array of unique
   23727              :      global trees (& types).  Do this now, rather than lazily, as
   23728              :      some global trees are lazily created and we don't want that to
   23729              :      mess with our syndrome of fixed trees.  */
   23730         4913 :   unsigned crc = 0;
   23731         4913 :   vec_alloc (fixed_trees, 250);
   23732              : 
   23733         5496 :   dump () && dump ("+Creating globals");
   23734              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23735         4913 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23736         4913 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23737        29478 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23738              :     {
   23739        24565 :       const tree *ptr = global_tree_arys[jx].first;
   23740        24565 :       unsigned limit = global_tree_arys[jx].second;
   23741              : 
   23742      1503378 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23743              :         {
   23744      1478813 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23745      1478813 :           unsigned v = maybe_add_global (*ptr, crc);
   23746      1654296 :           dump () && dump ("+%u", v);
   23747              :         }
   23748              :     }
   23749              :   /* OS- and machine-specific types are dynamically registered at
   23750              :      runtime, so cannot be part of global_tree_arys.  */
   23751         4913 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23752        19652 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23753              :     {
   23754        14739 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23755        16488 :       dump () && dump ("+%u", v);
   23756              :     }
   23757         4913 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23758         4913 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23759              :                      fixed_trees->length (), global_crc);
   23760       946664 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23761       941751 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23762              : 
   23763         4913 :   dump.pop (0);
   23764              : 
   23765         4913 :   if (!flag_module_lazy)
   23766              :     /* Get the mapper now, if we're not being lazy.  */
   23767          312 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23768              : 
   23769         4913 :   if (!flag_preprocess_only)
   23770              :     {
   23771         4769 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23772         4769 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23773         4769 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23774         4769 :       imported_temploid_friends
   23775         4769 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23776              :     }
   23777              : 
   23778              : #if CHECKING_P
   23779         4913 :   note_defs = note_defs_table_t::create_ggc (1000);
   23780              : #endif
   23781              : 
   23782         4913 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23783            9 :     begin_header_unit (reader);
   23784              : 
   23785              :   /* Collect here to make sure things are tagged correctly (when
   23786              :      aggressively GC'd).  */
   23787         4913 :   ggc_collect ();
   23788         4913 : }
   23789              : 
   23790              : /* If NODE is a deferred macro, load it.  */
   23791              : 
   23792              : static int
   23793        83065 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23794              : {
   23795        83065 :   location_t main_loc
   23796        83065 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23797              : 
   23798        83065 :   if (cpp_user_macro_p (node)
   23799        83065 :       && !node->value.macro)
   23800              :     {
   23801           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23802           72 :       dump () && dump ("Loaded macro #%s %I",
   23803              :                        macro ? "define" : "undef", identifier (node));
   23804              :     }
   23805              : 
   23806        83065 :   return 1;
   23807              : }
   23808              : 
   23809              : /* At the end of tokenizing, we no longer need the macro tables of
   23810              :    imports.  But the user might have requested some checking.  */
   23811              : 
   23812              : void
   23813        97029 : maybe_check_all_macros (cpp_reader *reader)
   23814              : {
   23815        97029 :   if (!warn_imported_macros)
   23816              :     return;
   23817              : 
   23818              :   /* Force loading of any remaining deferred macros.  This will
   23819              :      produce diagnostics if they are ill-formed.  */
   23820           21 :   unsigned n = dump.push (NULL);
   23821           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23822           21 :   dump.pop (n);
   23823              : }
   23824              : 
   23825              : // State propagated from finish_module_processing to fini_modules
   23826              : 
   23827              : struct module_processing_cookie
   23828              : {
   23829              :   elf_out out;
   23830              :   module_state_config config;
   23831              :   char *cmi_name;
   23832              :   char *tmp_name;
   23833              :   unsigned crc;
   23834              :   bool began;
   23835              : 
   23836         2894 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23837         2894 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23838              :   {
   23839              :   }
   23840         2894 :   ~module_processing_cookie ()
   23841              :   {
   23842         2894 :     XDELETEVEC (tmp_name);
   23843         2894 :     XDELETEVEC (cmi_name);
   23844         2894 :   }
   23845              : };
   23846              : 
   23847              : /* Write the CMI, if we're a module interface.  */
   23848              : 
   23849              : void *
   23850        96822 : finish_module_processing (cpp_reader *reader)
   23851              : {
   23852        96822 :   module_processing_cookie *cookie = nullptr;
   23853              : 
   23854        96822 :   if (header_module_p ())
   23855          911 :     module_kind &= ~MK_EXPORTING;
   23856              : 
   23857        96822 :   if (!modules || !this_module ()->name)
   23858              :     {
   23859        93925 :       if (flag_module_only)
   23860            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23861              :     }
   23862         2897 :   else if (!flag_syntax_only)
   23863              :     {
   23864         2894 :       int fd = -1;
   23865         2894 :       int e = -1;
   23866              : 
   23867         2894 :       timevar_start (TV_MODULE_EXPORT);
   23868              : 
   23869              :       /* Force a valid but empty line map at the end.  This simplifies
   23870              :          the line table preparation and writing logic.  */
   23871         2894 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23872              : 
   23873              :       /* We write to a tmpname, and then atomically rename.  */
   23874         2894 :       char *cmi_name = NULL;
   23875         2894 :       char *tmp_name = NULL;
   23876         2894 :       module_state *state = this_module ();
   23877              : 
   23878         2894 :       unsigned n = dump.push (state);
   23879         2894 :       state->announce ("creating");
   23880         2894 :       if (state->filename)
   23881              :         {
   23882         2894 :           size_t len = 0;
   23883         2894 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23884         2894 :           tmp_name = XNEWVEC (char, len + 3);
   23885         2894 :           memcpy (tmp_name, cmi_name, len);
   23886         2894 :           strcpy (&tmp_name[len], "~");
   23887              : 
   23888         2894 :           if (!errorcount)
   23889           43 :             for (unsigned again = 2; ; again--)
   23890              :               {
   23891         2825 :                 fd = open (tmp_name,
   23892              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23893              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23894         2825 :                 e = errno;
   23895         2825 :                 if (fd >= 0 || !again || e != ENOENT)
   23896              :                   break;
   23897           43 :                 create_dirs (tmp_name);
   23898              :               }
   23899         2894 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23900            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23901         3194 :           dump () && dump ("CMI is %s", cmi_name);
   23902              :         }
   23903              : 
   23904         2894 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23905              : 
   23906         2894 :       if (errorcount)
   23907              :         /* Don't write the module if we have reported errors.  */;
   23908         2782 :       else if (erroneous_templates
   23909         2782 :                && !erroneous_templates->is_empty ())
   23910              :         {
   23911              :           /* Don't write the module if it contains an erroneous template.
   23912              :              Also emit notes about where errors occurred in case
   23913              :              -Wno-template-body was passed.  */
   23914            6 :           auto_diagnostic_group d;
   23915            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23916              :                     "in template bodies", state->get_flatname ());
   23917            6 :           if (!warn_template_body)
   23918            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23919           12 :           for (auto e : *erroneous_templates)
   23920            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23921            6 :         }
   23922         2776 :       else if (cookie->out.begin ())
   23923              :         {
   23924              :           /* So crashes finger-point the module decl.  */
   23925         2776 :           iloc_sentinel ils = state->loc;
   23926         2776 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23927         2776 :                                   cookie->crc))
   23928         2747 :             cookie->began = true;
   23929         2776 :         }
   23930              : 
   23931         2894 :       dump.pop (n);
   23932         2894 :       timevar_stop (TV_MODULE_EXPORT);
   23933              : 
   23934         2894 :       ggc_collect ();
   23935              :     }
   23936              : 
   23937        96822 :   if (modules)
   23938              :     {
   23939         4729 :       unsigned n = dump.push (NULL);
   23940         5312 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23941         5312 :       dump () && dump ("Containing %u clusters", available_clusters);
   23942         4729 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   23943          583 :                        (loaded_clusters * 100 + available_clusters / 2) /
   23944          583 :                        (available_clusters + !available_clusters));
   23945         4729 :       dump.pop (n);
   23946              :     }
   23947              : 
   23948        96822 :   return cookie;
   23949              : }
   23950              : 
   23951              : // Do the final emission of a module.  At this point we know whether
   23952              : // the module static initializer is a NOP or not.
   23953              : 
   23954              : static void
   23955         2894 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   23956              :                     bool init_fn_non_empty)
   23957              : {
   23958         2894 :   timevar_start (TV_MODULE_EXPORT);
   23959              : 
   23960         2894 :   module_state *state = this_module ();
   23961         2894 :   unsigned n = dump.push (state);
   23962         2894 :   state->announce ("finishing");
   23963              : 
   23964         2894 :   cookie->config.active_init = init_fn_non_empty;
   23965         2894 :   if (cookie->began)
   23966         2747 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   23967              : 
   23968         2894 :   if (cookie->out.end () && cookie->cmi_name)
   23969              :     {
   23970              :       /* Some OS's do not replace NEWNAME if it already exists.
   23971              :          This'll have a race condition in erroneous concurrent
   23972              :          builds.  */
   23973         2782 :       unlink (cookie->cmi_name);
   23974         2782 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   23975              :         {
   23976            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   23977            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   23978            0 :           cookie->out.set_error (errno);
   23979              :         }
   23980              :     }
   23981              : 
   23982         2894 :   if (cookie->out.get_error () && cookie->began)
   23983              :     {
   23984            0 :       error_at (state->loc, "failed to write compiled module: %s",
   23985            0 :                 cookie->out.get_error (state->filename));
   23986            0 :       state->note_cmi_name ();
   23987              :     }
   23988              : 
   23989         2894 :   if (!errorcount)
   23990              :     {
   23991         2741 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23992         2741 :       mapper->ModuleCompiled (state->get_flatname ());
   23993              :     }
   23994          153 :   else if (cookie->cmi_name)
   23995              :     {
   23996              :       /* We failed, attempt to erase all evidence we even tried.  */
   23997          153 :       unlink (cookie->tmp_name);
   23998          153 :       unlink (cookie->cmi_name);
   23999              :     }
   24000              : 
   24001         2894 :   delete cookie;
   24002         2894 :   dump.pop (n);
   24003         2894 :   timevar_stop (TV_MODULE_EXPORT);
   24004         2894 : }
   24005              : 
   24006              : void
   24007        96822 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24008              : {
   24009        96822 :   if (cookie)
   24010         2894 :     late_finish_module (reader,
   24011              :                         static_cast<module_processing_cookie *> (cookie),
   24012              :                         has_inits);
   24013              : 
   24014              :   /* We're done with the macro tables now.  */
   24015        96822 :   vec_free (macro_exports);
   24016        96822 :   vec_free (macro_imports);
   24017        96822 :   headers = NULL;
   24018              : 
   24019              :   /* We're now done with everything but the module names.  */
   24020        96822 :   set_cmi_repo (NULL);
   24021        96822 :   if (mapper)
   24022              :     {
   24023         4729 :       timevar_start (TV_MODULE_MAPPER);
   24024         4729 :       module_client::close_module_client (0, mapper);
   24025         4729 :       mapper = nullptr;
   24026         4729 :       timevar_stop (TV_MODULE_MAPPER);
   24027              :     }
   24028        96822 :   module_state_config::release ();
   24029              : 
   24030              : #if CHECKING_P
   24031        96822 :   note_defs = NULL;
   24032              : #endif
   24033              : 
   24034        96822 :   if (modules)
   24035         7667 :     for (unsigned ix = modules->length (); --ix;)
   24036         2938 :       if (module_state *state = (*modules)[ix])
   24037         2938 :         state->release ();
   24038              : 
   24039              :   /* No need to lookup modules anymore.  */
   24040        96822 :   modules_hash = NULL;
   24041              : 
   24042              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24043        96822 :   vec_free (entity_ary);
   24044        96822 :   entity_ary = NULL;
   24045              : 
   24046              :   /* Or remember any pending entities.  */
   24047       101551 :   delete pending_table;
   24048        96822 :   pending_table = NULL;
   24049              : 
   24050              :   /* Or any keys -- Let it go!  */
   24051        99282 :   delete keyed_table;
   24052        96822 :   keyed_table = NULL;
   24053              : 
   24054              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24055        96822 :   ggc_collect ();
   24056        96822 : }
   24057              : 
   24058              : /* If CODE is a module option, handle it & return true.  Otherwise
   24059              :    return false.  For unknown reasons I cannot get the option
   24060              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24061              :    make a string type option variable.  */
   24062              : 
   24063              : bool
   24064      1926615 : handle_module_option (unsigned code, const char *str, int)
   24065              : {
   24066      1926615 :   auto hdr = CMS_header;
   24067              : 
   24068      1926615 :   switch (opt_code (code))
   24069              :     {
   24070           48 :     case OPT_fmodule_mapper_:
   24071           48 :       module_mapper_name = str;
   24072           48 :       return true;
   24073              : 
   24074           12 :     case OPT_fmodule_header_:
   24075           12 :       {
   24076           12 :         if (!strcmp (str, "user"))
   24077              :           hdr = CMS_user;
   24078           12 :         else if (!strcmp (str, "system"))
   24079              :           hdr = CMS_system;
   24080              :         else
   24081            0 :           error ("unknown header kind %qs", str);
   24082              :       }
   24083              :       /* Fallthrough.  */
   24084              : 
   24085          926 :     case OPT_fmodule_header:
   24086          926 :       flag_header_unit = hdr;
   24087          926 :       flag_modules = 1;
   24088          926 :       return true;
   24089              : 
   24090            1 :     case OPT_flang_info_include_translate_:
   24091            1 :       vec_safe_push (note_includes, str);
   24092            1 :       return true;
   24093              : 
   24094            6 :     case OPT_flang_info_module_cmi_:
   24095            6 :       vec_safe_push (note_cmis, str);
   24096            6 :       return true;
   24097              : 
   24098              :     default:
   24099              :       return false;
   24100              :     }
   24101              : }
   24102              : 
   24103              : /* Set preprocessor callbacks and options for modules.  */
   24104              : 
   24105              : void
   24106        98538 : module_preprocess_options (cpp_reader *reader)
   24107              : {
   24108        98538 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24109        98538 :   if (modules_p ())
   24110              :     {
   24111         4913 :       auto *cb = cpp_get_callbacks (reader);
   24112              : 
   24113         4913 :       cb->translate_include = maybe_translate_include;
   24114         4913 :       cb->user_deferred_macro = module_state::deferred_macro;
   24115         4913 :       if (flag_header_unit)
   24116              :         {
   24117              :           /* If the preprocessor hook is already in use, that
   24118              :              implementation will call the undef langhook.  */
   24119          923 :           if (cb->undef)
   24120            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24121              :           else
   24122          923 :             cb->undef = module_state::undef_macro;
   24123              :         }
   24124         4913 :       auto *opt = cpp_get_options (reader);
   24125         4913 :       opt->module_directives = true;
   24126         4913 :       if (flag_no_output)
   24127           18 :         opt->directives_only = true;
   24128         4913 :       if (opt->main_search == CMS_none)
   24129         4908 :         opt->main_search = cpp_main_search (flag_header_unit);
   24130              :     }
   24131        98538 : }
   24132              : 
   24133              : #include "gt-cp-module.h"
        

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.